/* ---------- Fonts ---------- */
/* TeX Gyre Heros, a free Helvetica clone (GUST Font License), bundled in
   vendor/texgyreheros so the tiles look the same on every device, offline too. */
@font-face {
  font-family: "TeX Gyre Heros"; font-weight: 400; font-style: normal; font-display: swap;
  src: url("../vendor/texgyreheros/texgyreheros-regular.woff2") format("woff2");
}
@font-face {
  font-family: "TeX Gyre Heros"; font-weight: 700; font-style: normal; font-display: swap;
  src: url("../vendor/texgyreheros/texgyreheros-bold.woff2") format("woff2");
}
@font-face {
  font-family: "TeX Gyre Heros"; font-weight: 400; font-style: italic; font-display: swap;
  src: url("../vendor/texgyreheros/texgyreheros-italic.woff2") format("woff2");
}
@font-face {
  font-family: "TeX Gyre Heros"; font-weight: 700; font-style: italic; font-display: swap;
  src: url("../vendor/texgyreheros/texgyreheros-bolditalic.woff2") format("woff2");
}

/* ---------- Tokens ---------- */
:root {
  --bg: #f5f2ec;
  --surface: #fffdf9;
  --surface-2: #ece6db;
  --ink: #1f2430;
  --ink-soft: #5b6170;
  --line: #d9d1c3;
  --accent: #2f6f73;
  --accent-ink: #ffffff;
  --gold: #b88a3b;
  --correct: #3c9a5f;
  --correct-soft: rgba(60, 154, 95, .14);
  --swap: #d08a2e;
  --wrong: #c0483c;
  --shadow: 0 1px 2px rgba(31, 36, 48, .06), 0 4px 14px rgba(31, 36, 48, .07);
  --shadow-lift: 0 14px 34px rgba(31, 36, 48, .22);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-story: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
  --font-tile: "TeX Gyre Heros", "Helvetica Neue", Helvetica, Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171a20;
    --surface: #22262e;
    --surface-2: #2c313b;
    --ink: #eceae5;
    --ink-soft: #a5a9b3;
    --line: #3a404b;
    --accent: #5fb3b3;
    --accent-ink: #0f1a1b;
    --gold: #d8b36a;
    --correct: #5cc285;
    --correct-soft: rgba(92, 194, 133, .16);
    --swap: #e7a24a;
    --wrong: #e57265;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
    --shadow-lift: 0 14px 34px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171a20; --surface: #22262e; --surface-2: #2c313b; --ink: #eceae5;
  --ink-soft: #a5a9b3; --line: #3a404b; --accent: #5fb3b3; --accent-ink: #0f1a1b;
  --gold: #d8b36a; --correct: #5cc285; --correct-soft: rgba(92, 194, 133, .16);
  --swap: #e7a24a; --wrong: #e57265; --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 14px rgba(0, 0, 0, .25);
  --shadow-lift: 0 14px 34px rgba(0, 0, 0, .5); color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { 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;
}
h1, h2 { line-height: 1.15; margin: 0; }
a { color: inherit; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
body.is-dragging { cursor: grabbing; user-select: none; -webkit-user-select: none; }

.screen { max-width: 1400px; margin: 0 auto; padding: 24px 16px 48px; }
@media (min-width: 700px) { .screen { padding: 32px 32px 64px; } }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4em;
  min-height: 44px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--ink);
  font: 600 1rem var(--font); text-decoration: none; cursor: pointer;
}
.btn:hover { border-color: var(--ink-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.quiet { background: transparent; }

/* ---------- Menu ---------- */
.menu-head {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between;
  gap: 20px 32px; margin-bottom: 28px;
}
.menu-head h1 { font-size: clamp(2rem, 5vw, 2.8rem); letter-spacing: -.02em; }
.lede { margin: 6px 0 0; color: var(--ink-soft); font-size: 1.1rem; }

.mode { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.segmented {
  display: inline-flex; padding: 4px; gap: 4px;
  background: var(--surface-2); border-radius: 999px;
}
.seg {
  min-width: 88px; min-height: 40px; padding: 0 16px;
  border: 0; border-radius: 999px; background: transparent;
  font: 600 1rem var(--font); color: var(--ink-soft); cursor: pointer;
}
.seg[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.mode-hint { margin: 0; font-size: .9rem; color: var(--ink-soft); max-width: 34ch; text-align: right; }
@media (max-width: 600px) {
  .mode { align-items: flex-start; }
  .mode-hint { text-align: left; }
}

.cards {
  display: grid; gap: 20px;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
}
.card {
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  overflow: hidden; text-decoration: none; box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
}
.card:hover { transform: translateY(-3px); box-shadow: var(--shadow-lift); border-color: var(--accent); }
.card:active { transform: translateY(-1px); }
.card-art {
  aspect-ratio: 2 / 1; background: var(--surface-2);
  display: grid; place-items: center; overflow: hidden;
}
.art-count-box {
  display: flex; flex-direction: column; align-items: center; line-height: 1;
  color: var(--accent);
}
.art-count { font: 700 3rem var(--font-story); }
.art-label { font-size: .85rem; letter-spacing: .12em; text-transform: uppercase; margin-top: 6px; color: var(--ink-soft); }
.card-text { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-text h2 { font-size: 1.3rem; }
.card-text p { margin: 0; color: var(--ink-soft); }
.card-meta { margin-top: auto; padding-top: 6px; font-size: .85rem; font-weight: 600; color: var(--gold); }

/* ---------- Play screen ---------- */
.play-bar {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px 16px;
}
.play-bar h1 { font-size: clamp(1.3rem, 3.5vw, 1.9rem); text-align: center; }
.stats { display: flex; align-items: center; gap: 12px; font-size: .95rem; color: var(--ink-soft); }
.badge {
  padding: 3px 10px; border-radius: 999px; font-size: .8rem; font-weight: 700;
  letter-spacing: .04em; text-transform: uppercase;
}
.badge-easy { background: var(--correct-soft); color: var(--correct); }
.badge-hard { background: var(--surface-2); color: var(--ink); }
@media (max-width: 600px) {
  .play-bar { grid-template-columns: auto 1fr; }
  .play-bar h1 { grid-column: 1 / -1; grid-row: 1; text-align: left; }
  .stats { justify-self: end; }
}
.instructions { margin: 12px 0 20px; text-align: center; color: var(--ink-soft); }
.instructions.done { color: var(--correct); font-weight: 600; }

.stat-chip { display: inline-flex; align-items: center; gap: 5px; }
.stat-chip b { color: var(--ink); font-variant-numeric: tabular-nums; }
.stat-chip::before { font-weight: 700; }
.chip-time::before { content: "⏱"; font-weight: 400; }
.chip-right::before { content: "✓"; color: var(--correct); }
.chip-wrong::before { content: "✗"; color: var(--wrong); }

.progress {
  display: flex; align-items: center; gap: 12px;
  max-width: 560px; margin: 14px auto 0;
  font-size: .9rem; color: var(--ink-soft); font-variant-numeric: tabular-nums;
}
.progress-track { flex: 1; height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress-fill { display: block; height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .5s ease; }
.progress-text { white-space: nowrap; }

/* ---------- Matching board ---------- */
.stage { max-width: 1100px; margin: 0 auto; }
.board-wrap { animation: deal-in .35s ease both; }
.board-wrap.leaving { animation: deal-out .3s ease both; }
@keyframes deal-in { from { opacity: 0; transform: translateY(12px); } }
@keyframes deal-out { to { opacity: 0; transform: translateY(-12px) scale(.98); } }

.match-board {
  display: grid; column-gap: 10px;
  grid-template-columns: minmax(min-content, var(--q-fr, 1fr)) 28px minmax(min-content, var(--a-fr, 1fr));
}
@media (min-width: 700px) {
  .match-board { column-gap: 14px; grid-template-columns: minmax(min-content, var(--q-fr, 1fr)) 36px minmax(min-content, var(--a-fr, 1fr)); }
}
.match-head {
  grid-row: 1; margin-bottom: -4px; padding: 0 4px;
  font-size: .8rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft);
}
.match-board { row-gap: 12px; position: relative; }

.tile {
  position: relative;
  display: flex; align-items: center;
  min-height: 4.5em; padding: 12px 16px;
  border-radius: var(--radius); border: 1px solid var(--line);
  font: 1.05rem/1.45 var(--font-tile);
  overflow-wrap: break-word;
}
.tile-body { min-width: 0; }
.tile strong { font-weight: 700; }
.tile .katex { font-size: 1.2em; }
.tile .katex-display { margin: .2em 0; }
@media (max-width: 600px) {
  .tile { padding: 10px 12px; font-size: .95rem; }
}

.question { grid-column: 1; background: var(--surface-2); border-color: transparent; }
.answer {
  grid-column: 3;
  background: var(--surface); box-shadow: var(--shadow);
  touch-action: none;           /* let pointer events drive dragging on touch screens */
  user-select: none; -webkit-user-select: none;
  cursor: grab;
}
.answer:hover { border-color: var(--ink-soft); }
.match-board.solved .answer { cursor: default; }

/* The little bridge between a question and the answer across from it */
.link { grid-column: 2; display: grid; place-items: center; color: var(--line); }
.link::before {
  content: ""; width: 100%; height: 2px; border-radius: 2px; background: currentColor;
  transition: background .2s ease;
}
.link.correct { color: var(--correct); }

/* Easy-mode hint (and the finished state): a green edge on the whole row */
.tile.correct { border-color: var(--correct); box-shadow: inset 0 0 0 2px var(--correct), var(--shadow); }
.question.correct { box-shadow: inset 0 0 0 2px var(--correct); background: color-mix(in srgb, var(--correct-soft), var(--surface-2)); }
.match-board.solved .tile { background: color-mix(in srgb, var(--correct-soft), var(--surface)); }
.match-board.dragging .answer.correct { box-shadow: inset 0 0 0 2px var(--correct); }

.tile.placeholder {
  background: transparent !important;
  box-shadow: none !important;
  border: 2px dashed var(--line) !important;
}
.tile.placeholder > * { visibility: hidden; }
.tile.swap-target { box-shadow: inset 0 0 0 3px var(--swap), var(--shadow-lift) !important; }
.answer.swap-target { transform: scale(.98); }
.tile.held { box-shadow: inset 0 0 0 3px var(--accent), var(--shadow-lift) !important; transform: scale(1.02); z-index: 1; }

.tile.ghost {
  position: fixed; left: 0; top: 0; margin: 0; z-index: 100;
  pointer-events: none; cursor: grabbing;
  box-shadow: var(--shadow-lift);
  opacity: .96;
}
.tile.missed { animation: nope .4s ease; }
@keyframes nope { 20%, 60% { translate: -5px 0; } 40%, 80% { translate: 5px 0; } }

/* ---------- Win overlay ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  backdrop-filter: blur(3px);
  animation: fade-in .35s ease both;
}
.overlay.leaving { animation: fade-out .25s ease both; }
.win {
  width: min(100%, 560px);
  padding: 36px 28px 28px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 24px;
  box-shadow: var(--shadow-lift); text-align: center;
  animation: rise-in .5s cubic-bezier(.2, .8, .3, 1) both;
}
.win h2 {
  font: 700 clamp(2.6rem, 9vw, 4rem)/1.05 var(--font-story);
  color: var(--accent); letter-spacing: -.01em;
}
.win-stats { display: flex; justify-content: center; flex-wrap: wrap; gap: 12px 36px; margin: 24px 0 8px; }
.stat { display: flex; flex-direction: column; }
.stat-value { font-size: clamp(1.8rem, 6vw, 2.4rem); font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: .85rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .08em; }
.win-mode { margin: 8px 0 24px; color: var(--ink-soft); }
.win-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(16px) scale(.97); } }

.error { max-width: 640px; }
.error p { color: var(--ink-soft); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
