:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #ebe7dd;
  --text: #1d2026;
  --muted: #6b6a66;
  --line: #d9d4c7;
  --accent: #c2410c;
  --accent-soft: #fde7da;
  --good: #15803d;
  --good-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --shadow: 0 1px 2px rgba(30, 25, 10, .06), 0 8px 24px rgba(30, 25, 10, .08);
  --card-shadow: 0 2px 4px rgba(30, 25, 10, .08), 0 18px 40px rgba(30, 25, 10, .14);
  --radius: 14px;
  --font: "Avenir Next", "Nunito Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --surface: #212226;
    --surface-2: #2b2d32;
    --text: #ecebe8;
    --muted: #9c9b97;
    --line: #34363c;
    --accent: #fb923c;
    --accent-soft: #3b2417;
    --good: #4ade80;
    --good-soft: #15301f;
    --bad: #f87171;
    --bad-soft: #3a1b1b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    --card-shadow: 0 2px 4px rgba(0, 0, 0, .3), 0 18px 40px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }

/* ---------- Home ---------- */

.home {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(20px + env(safe-area-inset-top)) 16px calc(120px + env(safe-area-inset-bottom));
}
.home h1 {
  font-size: 28px;
  letter-spacing: -.01em;
  margin: 8px 0 2px;
}
.home .lede { color: var(--muted); margin: 0 0 18px; }

.resume {
  display: flex; align-items: center; gap: 12px;
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 18px;
}
.resume p { margin: 0; flex: 1; font-size: 15px; }
.resume strong { display: block; }

.options {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin: 0 0 18px;
}
.toggle {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}
.toggle input { accent-color: var(--accent); margin: 0; width: 16px; height: 16px; }

.topic {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 14px 12px;
  margin-bottom: 12px;
}
.topic-head {
  display: flex; align-items: baseline; gap: 10px;
  margin-bottom: 10px;
}
.topic-num {
  font-size: 13px; font-weight: 700; color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.topic-name { font-weight: 700; font-size: 17px; flex: 1; }
.topic-all {
  background: none; border: 0; padding: 4px 2px;
  color: var(--muted); font-size: 13px; cursor: pointer;
}
.topic-all:hover { color: var(--text); }

.decks { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.deck {
  position: relative;
  text-align: left;
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 9px 10px 10px;
  cursor: pointer;
  min-width: 0;
  transition: border-color .15s, background .15s;
}
.deck:hover { border-color: var(--line); }
.deck[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.deck-name { display: block; font-weight: 700; font-size: 14px; line-height: 1.2; }
.deck-count { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.deck-bar {
  display: block; height: 3px; border-radius: 3px;
  background: var(--line); margin-top: 7px; overflow: hidden;
}
.deck-bar > i { display: block; height: 100%; background: var(--good); }
.deck-check {
  position: absolute; top: 7px; right: 8px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); color: #fff;
  font-size: 12px; line-height: 18px; text-align: center;
  opacity: 0; transform: scale(.6); transition: .15s;
}
.deck[aria-pressed="true"] .deck-check { opacity: 1; transform: none; }

.topic-browse { margin-top: 9px; font-size: 13px; color: var(--muted); }
.topic-browse a { color: var(--muted); }
.topic-browse a:hover { color: var(--text); }

@media (max-width: 420px) {
  .decks { gap: 6px; }
  .deck { padding: 8px 8px 9px; }
  .deck-name { font-size: 13px; }
}

.startbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
  display: flex; justify-content: center; gap: 10px;
  pointer-events: none;
}
.startbar > * { pointer-events: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 999px;
  padding: 13px 22px;
  font-weight: 700; font-size: 16px;
  cursor: pointer;
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow);
  transition: transform .1s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; cursor: default; transform: none; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.small { padding: 8px 14px; font-size: 14px; }
.btn.ghost { background: transparent; box-shadow: none; }
.startbar .btn.primary { min-width: min(100%, 340px); }

.foot { color: var(--muted); font-size: 13px; text-align: center; margin-top: 22px; }
.foot button { background: none; border: 0; color: inherit; text-decoration: underline; cursor: pointer; padding: 0; font-size: 13px; }

/* ---------- Study ---------- */

.study {
  height: 100dvh;
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
  overflow: hidden;
}
.study-top {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px 6px;
  max-width: 760px; width: 100%; margin: 0 auto;
}
.iconbtn {
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: var(--surface); box-shadow: var(--shadow);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; flex: none;
}
.iconbtn:disabled { opacity: .35; cursor: default; }
.iconbtn svg, .btn svg { width: 20px; height: 20px; display: block; }
.progress { flex: 1; min-width: 0; }
.progress-label {
  display: flex; justify-content: space-between; gap: 8px;
  font-size: 13px; color: var(--muted); margin-bottom: 5px;
}
.progress-label b { color: var(--text); font-variant-numeric: tabular-nums; }
.progress-label span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress-track { height: 5px; background: var(--surface-2); border-radius: 5px; overflow: hidden; display: flex; }
.progress-track .ok { background: var(--good); }
.progress-track .ko { background: var(--bad); }

.stage {
  flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 8px 16px;
  perspective: 1600px;
}

.flash {
  --ar: 1.1;
  position: relative;
  width: min(100%, 600px, calc((100dvh - 210px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * var(--ar)));
  aspect-ratio: var(--ar);
  cursor: pointer;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
.flash-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.3, .7, .3, 1);
}
.flash.flipped .flash-inner { transform: rotateY(180deg); }
.flash.dragging .flash-inner { transition: none; }
.side {
  position: absolute; inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: #fffefb;
  color: #1d2026;
  border-radius: 18px;
  box-shadow: var(--card-shadow);
  display: flex; align-items: center; justify-content: center;
  padding: 5.5%;
  overflow: hidden;
}
.side.back { transform: rotateY(180deg); }
.side-tag {
  position: absolute; bottom: 2.6%; right: 4%;
  font-size: 11px; color: #a3a097; letter-spacing: .04em; text-transform: uppercase;
}

.hint {
  position: absolute; top: 18px; padding: 6px 12px; border-radius: 8px;
  font-weight: 700; font-size: 15px; opacity: 0; pointer-events: none;
  transform: translateZ(1px);
  z-index: 3;
}
.hint.ok { left: 18px; color: var(--good); background: var(--good-soft); border: 2px solid var(--good); }
.hint.ko { right: 18px; color: var(--bad); background: var(--bad-soft); border: 2px solid var(--bad); }

.enter { animation: enter .28s cubic-bezier(.2, .8, .3, 1); }
@keyframes enter { from { opacity: 0; transform: translateY(14px) scale(.97); } }
.leave-ok { transition: transform .3s ease-in, opacity .3s; transform: translateX(120%) rotate(12deg) !important; opacity: 0; }
.leave-ko { transition: transform .3s ease-in, opacity .3s; transform: translateX(-120%) rotate(-12deg) !important; opacity: 0; }

.actions {
  display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: center;
  max-width: 600px; width: 100%; margin: 0 auto;
  padding: 6px 16px 14px;
}
.actions .btn { padding: 14px 10px; }
.btn.ko { background: var(--bad-soft); color: var(--bad); box-shadow: none; }
.btn.ok { background: var(--good-soft); color: var(--good); box-shadow: none; }
.btn.flip { padding: 14px 18px; }
.keys { text-align: center; color: var(--muted); font-size: 12px; padding-bottom: 8px; }
@media (hover: none) { .keys { display: none; } }

/* ---------- Browse ---------- */

.browse { max-width: 1100px; margin: 0 auto; padding: calc(12px + env(safe-area-inset-top)) 16px 40px; }
.browse-top {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 0; background: var(--bg);
}
.browse-top strong { display: block; font-size: 17px; }
.browse-top span { color: var(--muted); font-size: 13px; }
.browse-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 480px), 1fr)); gap: 14px; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.pair.known .mini { outline: 2px solid var(--good); outline-offset: -2px; }
.mini { background: #fffefb; color: #1d2026; border-radius: 10px; padding: 6%; box-shadow: var(--shadow); }

/* ---------- Summary ---------- */

.summary {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  padding: 24px 20px calc(24px + env(safe-area-inset-bottom));
  gap: 6px;
}
.summary .big { font-size: 56px; font-weight: 700; letter-spacing: -.02em; line-height: 1; }
.summary h2 { margin: 6px 0 0; font-size: 22px; }
.summary p { color: var(--muted); margin: 0 0 16px; }
.summary .row { display: flex; flex-direction: column; gap: 10px; width: min(100%, 340px); }

.loading { min-height: 100dvh; display: flex; align-items: center; justify-content: center; color: var(--muted); }

/* ---------- Card face (redrawn from the PDF) ---------- */

.face {
  position: relative;
  width: 100%;
  container-type: inline-size;
  font-family: var(--font);
  line-height: 1;
}
.face .r {
  position: absolute;
  white-space: pre;
}
.face .r.c { transform: translateX(-50%); }
.face .r.badge,
.face .r.pill {
  display: flex; align-items: center; justify-content: center;
  border-radius: 999px;
}
.face .r.badge { background: #1d2026; color: #fff; border-radius: calc(var(--u) * 3); }
.face .r.pill { border-style: solid; border-color: #1d2026; }
.face .r.c.badge, .face .r.c.pill { transform: translateX(-50%); }
.face .u {
  text-decoration: underline;
  text-decoration-thickness: max(1px, calc(var(--u) * .75));
  text-underline-offset: calc(var(--u) * 1.6);
}
.face .rule { position: absolute; }
