/* Kartičky: mobile first, light/dark from the system, no external assets. */
:root {
  --bg: #f3efe7;
  --surface: #fffdf8;
  --paper: #fbf7ee;
  --ink: #2b2722;
  --muted: #6f675c;
  --line: #e2dacb;
  --accent: #b4532a;
  --accent-ink: #fff;
  --yes: #2f8a57;
  --no: #c0533a;
  --focus: #1f6feb;
  --shadow: 0 1px 2px rgb(60 40 20 / .08), 0 10px 30px -12px rgb(60 40 20 / .35);
  --speckle: .55;
  --radius: 18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Noto Serif", serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1a18;
    --surface: #24221f;
    --paper: #2d2a26;
    --ink: #ece6db;
    --muted: #a99f90;
    --line: #3b3732;
    --accent: #e0855a;
    --accent-ink: #1b1a18;
    --yes: #5cc28a;
    --no: #ee8068;
    --focus: #7aa7ff;
    --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 12px 32px -12px rgb(0 0 0 / .8);
    --speckle: .9;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  min-height: 100dvh;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .85rem; margin: .35rem 0 0; }
.status { color: var(--muted); padding: 0 16px; }

/* Buttons */
.btn {
  font: inherit; font-weight: 600;
  min-height: 44px; padding: .55rem 1rem;
  border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink);
  cursor: pointer; touch-action: manipulation;
  display: inline-flex; align-items: center; justify-content: center; gap: .4em;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--no); }
.btn.yes { color: var(--yes); }
.btn.no { color: var(--no); }
.icon-btn {
  width: 44px; height: 44px; border: 0; background: transparent; color: inherit;
  border-radius: 12px; cursor: pointer; display: grid; place-items: center; flex: none;
}
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg circle { fill: currentColor; stroke: none; }

/* Home */
#home { max-width: 760px; margin: 0 auto; padding-bottom: 32px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 16px 16px 8px; }
.topbar h1 { font-family: var(--serif); font-size: 1.7rem; margin: 0; }
.topbar-right { display: flex; align-items: center; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: .3em; white-space: nowrap;
  font-size: .85rem; font-weight: 600; color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  padding: .25rem .6rem; border-radius: 99px;
}
.chip svg { width: 1em; height: 1em; fill: currentColor; }
.tiles { list-style: none; margin: 8px 0 0; padding: 0 16px; display: grid; gap: 12px; }
@media (min-width: 640px) { .tiles { grid-template-columns: 1fr 1fr; } .tiles .all { grid-column: 1 / -1; } }
.tile {
  position: relative; display: flex; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.tile-main {
  flex: 1; min-width: 0; text-align: left; font: inherit; color: inherit;
  background: none; border: 0; padding: 16px; cursor: pointer; border-radius: var(--radius);
}
.tile-main h2 { font-size: 1.05rem; margin: 0 0 .2rem; line-height: 1.3; }
.tile-main p { margin: 0 0 .7rem; color: var(--muted); font-size: .9rem; }
.tile .icon-btn { margin: 6px 4px 0 0; }
.tile.all { background: color-mix(in srgb, var(--accent) 7%, var(--surface)); }
.tile-progress { display: flex; align-items: center; gap: 10px; font-size: .85rem; color: var(--muted); }
.tile-progress .bar { flex: 1; }
.tile-progress span { white-space: nowrap; font-variant-numeric: tabular-nums; }

.bar { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: var(--yes); border-radius: inherit; transition: width .4s ease; }

/* Study */
#study {
  height: 100dvh; display: flex; flex-direction: column;
  max-width: 640px; margin: 0 auto;
  padding: 0 16px max(12px, env(safe-area-inset-bottom));
}
.studybar { display: flex; align-items: center; gap: 4px; padding-top: 8px; margin: 0 -10px; }
.study-title { flex: 1; min-width: 0; margin: 0; font-size: 1rem; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meter { padding: 4px 0 10px; }
.meter-row { display: flex; justify-content: space-between; align-items: center; min-height: 28px; font-size: .88rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.run {
  position: relative; display: inline-flex; align-items: center; gap: 2px;
  color: var(--accent); font-weight: 700;
}
.run svg { width: 18px; height: 18px; fill: currentColor; }
.run.pop { animation: pop .35s ease-out; }
.run.milestone { animation: pop-big .6s ease-out; }
@keyframes pop { 40% { transform: scale(1.25); } }
@keyframes pop-big { 30% { transform: scale(1.6) rotate(-6deg); } 60% { transform: scale(.95); } }
.spark {
  position: absolute; left: 50%; top: 50%; width: 6px; height: 6px; margin: -3px; border-radius: 50%;
  background: var(--c, var(--accent)); pointer-events: none;
  animation: spark .65s ease-out forwards;
}
@keyframes spark {
  from { transform: rotate(var(--a)) translateX(0) scale(1); opacity: 1; }
  to { transform: rotate(var(--a)) translateX(var(--d, 28px)) scale(.3); opacity: 0; }
}

.stage { position: relative; flex: 1; min-height: 0; display: flex; }
.card {
  position: relative; flex: 1; min-height: 260px;
  perspective: 1600px; touch-action: pan-y;
  user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
.card.flipped { cursor: grab; }
.card.dragging { cursor: grabbing; }
.card.settle { transition: transform .25s ease, opacity .25s ease; }
.card.fly { transition: transform .32s cubic-bezier(.4, 0, 1, 1), opacity .32s ease-in; }
.card.enter { animation: enter .28s ease-out; }
@keyframes enter { from { transform: translateY(14px) scale(.97); opacity: 0; } }
.card-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transition: transform .6s cubic-bezier(.3, .7, .2, 1);
}
.card.flipped .card-inner { transform: rotateY(180deg); }
.card.instant .card-inner { transition: none; }
.face {
  position: absolute; inset: 0;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  border-radius: var(--radius);
  border: 1px solid color-mix(in srgb, var(--ink) 10%, transparent);
  box-shadow: var(--shadow);
  padding: 20px 20px 18px;
  display: flex; flex-direction: column;
  /* Paper/linen: fine noise (inline SVG) over a faint woven grid. */
  background-color: var(--paper);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .45 0 0 0 0 .38 0 0 0 0 .28 0 0 0 -.9 .55'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.22'/%3E%3C/svg%3E"),
    repeating-linear-gradient(0deg, color-mix(in srgb, var(--ink) 3%, transparent) 0 1px, transparent 1px 4px),
    repeating-linear-gradient(90deg, color-mix(in srgb, var(--ink) 2.5%, transparent) 0 1px, transparent 1px 5px),
    radial-gradient(120% 90% at 30% 0%, color-mix(in srgb, #fff 18%, transparent), transparent 60%);
}
.back { transform: rotateY(180deg); overflow-y: auto; overscroll-behavior: contain; user-select: text; -webkit-user-select: text; }
.topic { font-size: .75rem; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: none; }
.front .q {
  flex: 1; display: flex; align-items: center; justify-content: center; text-align: center;
  font-family: var(--serif); font-size: clamp(1.2rem, 4.6vw, 1.6rem); line-height: 1.35;
  overflow-wrap: anywhere; overflow-y: auto;
}
.tap-hint { text-align: center; font-size: .78rem; color: var(--muted); opacity: .8; flex: none; }
@media (hover: none) { .tap-hint { font-size: 0; } .tap-hint::after { content: "Klepnutím otočíte"; font-size: .78rem; } }
.answer { font-size: .98rem; line-height: 1.55; overflow-wrap: anywhere; }
.answer > :first-child { margin-top: 0; }
.answer h1, .answer h2, .answer h3, .answer h4 { font-family: var(--serif); line-height: 1.25; margin: 1em 0 .4em; }
.answer h1 { font-size: 1.3rem; } .answer h2 { font-size: 1.18rem; } .answer h3 { font-size: 1.06rem; } .answer h4 { font-size: 1rem; }
.answer ul, .answer ol { padding-left: 1.3em; }
.answer li + li { margin-top: .2em; }
.answer table { display: block; overflow-x: auto; border-collapse: collapse; font-size: .9rem; margin: .8em 0; max-width: 100%; }
.answer th, .answer td { border: 1px solid var(--line); padding: .35em .55em; text-align: left; vertical-align: top; }
.answer th { background: color-mix(in srgb, var(--ink) 5%, transparent); }
.answer blockquote { margin: .8em 0; padding-left: .9em; border-left: 3px solid var(--line); color: var(--muted); }
.answer code { font-size: .9em; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: .05em .3em; border-radius: 4px; }
.sources { list-style: none; margin: auto 0 0; padding: 14px 0 0; font-size: .78rem; color: var(--muted); }
.sources:empty { display: none; }
.sources::before { content: "Zdroje"; display: block; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; font-size: .7rem; margin-bottom: 2px; border-top: 1px solid var(--line); padding-top: 8px; }
.sources a { color: inherit; }

.swipe-hint {
  position: absolute; top: 18px; padding: .2rem .7rem; border: 2.5px solid currentColor; border-radius: 10px;
  font-weight: 800; letter-spacing: .03em; text-transform: uppercase; font-size: .95rem;
  opacity: 0; pointer-events: none; background: var(--paper);
}
.swipe-hint.no { right: 18px; color: var(--no); transform: rotate(8deg); }
.swipe-hint.yes { left: 18px; color: var(--yes); transform: rotate(-8deg); }
.card::after {
  content: ""; position: absolute; inset: 0; border-radius: var(--radius); pointer-events: none;
  box-shadow: inset 0 0 0 3px var(--tint, transparent); opacity: var(--tint-o, 0);
}

.actions { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; padding-top: 12px; }
.actions .btn { padding-inline: .6rem; }

.done { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 16px; position: relative; }
.done h2 { font-family: var(--serif); font-size: 1.8rem; margin: .4rem 0 .2rem; }
.done p { margin: 0 0 1.2rem; color: var(--muted); }
.done-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.done-badge { width: 72px; height: 72px; border-radius: 50%; background: var(--yes); display: grid; place-items: center; animation: pop-big .7s ease-out; }
.done-badge svg { width: 40px; height: 40px; fill: none; stroke: var(--bg); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.confetti {
  position: absolute; left: 50%; top: 38%; width: 8px; height: 12px; border-radius: 2px; pointer-events: none;
  background: var(--c); animation: confetti 1.1s cubic-bezier(.2, .6, .4, 1) forwards;
}
@keyframes confetti {
  from { transform: translate(0, 0) rotate(0); opacity: 1; }
  to { transform: translate(var(--x), var(--y)) rotate(var(--r)); opacity: 0; }
}

/* Dialogs */
.dlg {
  border: 0; padding: 0; border-radius: 20px; background: var(--surface); color: var(--ink);
  width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); box-shadow: var(--shadow);
}
.dlg.wide { width: min(560px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgb(0 0 0 / .45); }
.dlg form { padding: 18px 18px 20px; }
.dlg h2 { margin: 0 0 .5rem; font-size: 1.2rem; }
.dlg h3 { margin: 1.3rem 0 .3rem; font-size: 1rem; }
.dlg p { margin: 0 0 .6rem; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin: -6px -8px 0 0; }
.dlg-head h2 { margin: 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.dlg-actions.wrap { flex-wrap: wrap; justify-content: flex-start; }
.dlg-actions.column { flex-direction: column; align-items: stretch; }
.field label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
.field textarea {
  width: 100%; font: .82rem/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 10px; padding: 8px;
  resize: vertical; word-break: break-all;
}
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.file-btn:focus-within { outline: 3px solid var(--focus); outline-offset: 2px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin: 12px 0 0; }
.stats div { background: var(--bg); border-radius: 12px; padding: 10px 12px; }
.stats dt { font-size: .78rem; color: var(--muted); }
.stats dd { margin: 0; font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); background: var(--ink); color: var(--bg);
  padding: .6rem 1rem; border-radius: 12px; box-shadow: var(--shadow); font-size: .92rem; z-index: 10;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
  .card-inner { transition: transform .15s linear; }
  .card.fly, .card.settle, .bar-fill { transition-duration: .01ms; }
}
