:root,
:root[data-mode="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #243247;
  --border: #334155;
  --text: #f8fafc;
  --muted: #94a3b8;
  --accent: #7dd3fc;
  --correct: #10b981;
  --correct-bg: rgba(16, 185, 129, 0.14);
  --wrong: #f87171;
  --wrong-bg: rgba(248, 113, 113, 0.14);
  --warn: #fbbf24;
  --shadow: 0 2px 4px rgba(2, 6, 23, 0.25), 0 12px 20px -12px rgba(2, 6, 23, 0.5);
  color-scheme: dark;
}

:root[data-mode="sepia"] {
  --bg: #f4ecd8;
  --surface: #fbf6e9;
  --surface-2: #f1e7cf;
  --border: #dccfae;
  --text: #3b3026;
  --muted: #7a6a55;
  --accent: #8a5a2b;
  --correct: #2f7d4f;
  --correct-bg: rgba(47, 125, 79, 0.13);
  --wrong: #b4412f;
  --wrong-bg: rgba(180, 65, 47, 0.12);
  --warn: #a8661a;
  --shadow: 0 1px 2px rgba(92, 64, 30, 0.08), 0 12px 20px -12px rgba(92, 64, 30, 0.2);
  color-scheme: light;
}

:root[data-mode="light"] {
  --bg: #eef2f6;
  --surface: #ffffff;
  --surface-2: #f3f6f9;
  --border: #d5dde6;
  --text: #1e293b;
  --muted: #64748b;
  --accent: #0369a1;
  --correct: #047857;
  --correct-bg: rgba(4, 120, 87, 0.1);
  --wrong: #b91c1c;
  --wrong-bg: rgba(185, 28, 28, 0.08);
  --warn: #b45309;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 12px 20px -12px rgba(15, 23, 42, 0.16);
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { height: 100%; }
html, body { margin: 0; }

body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  transition: background-color .25s ease, color .25s ease;
}

button, select { font: inherit; color: inherit; }

::selection { background: color-mix(in srgb, var(--accent) 35%, transparent); color: var(--text); }
html { scrollbar-color: var(--border) var(--bg); caret-color: var(--accent); }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: .02em; font-size: 15px; white-space: nowrap; }
.stats { display: flex; gap: 22px; margin: 0 auto; }
.stats div { text-align: center; }
.stats dt { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.stats dd { margin: 0; font-weight: 650; font-variant-numeric: tabular-nums; font-size: 16px; line-height: 1.3; }
#stat-time { min-width: 4ch; }
#stat-time.low { color: var(--warn); }
.controls { display: flex; gap: 8px; align-items: center; }
.bar-btn {
  height: 34px; border-radius: 8px;
  background: var(--surface); border: 1px solid var(--border);
  cursor: pointer;
}
.bar-btn svg { display: none; flex: none; }
:root[data-mode="dark"] .i-dark, :root:not([data-mode]) .i-dark,
:root[data-mode="sepia"] .i-sepia,
:root[data-mode="light"] .i-light { display: block; }
.bar-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 0 12px; font-size: 14px; font-weight: 600; white-space: nowrap;
  color: var(--muted); transition: color .15s, border-color .15s, background-color .15s;
}
.bar-btn:hover { color: var(--text); }
.bar-btn:hover { border-color: var(--muted); }

/* ---------- Stage ---------- */
.stage {
  flex: 1;
  width: 100%; max-width: 760px;
  margin: 0 auto;
  padding: 24px 16px 32px;
}
.progress-line { height: 3px; background: var(--border); border-radius: 3px; overflow: hidden; }
.progress-line span { display: block; height: 100%; background: var(--accent); transform: scaleX(0); transform-origin: left; transition: transform .3s cubic-bezier(.2, .7, .2, 1); }
.session-meta { margin: 8px 2px 16px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.loading { text-align: center; color: var(--muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 26px;
  box-shadow: var(--shadow);
  animation: enter .32s cubic-bezier(.2, .7, .2, 1);
}
.card.leaving { animation: leave .16s ease forwards; }
@keyframes enter { from { opacity: 0; transform: translateX(18px); } to { opacity: 1; transform: none; } }
@keyframes leave { to { opacity: 0; transform: translateX(-14px); } }

.question { font-size: 20px; font-weight: 600; line-height: 1.55; margin: 0 0 20px; }

.options { display: grid; gap: 10px; }
.option {
  display: flex; align-items: flex-start; gap: 12px;
  width: 100%; text-align: left;
  padding: 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  line-height: 1.5;
  transition: border-color .15s, background-color .15s, transform .1s;
}
.option:hover:not(:disabled) { border-color: var(--accent); }
.option:active:not(:disabled) { transform: scale(.995); }
.option:focus-visible, .btn:focus-visible, .bar-btn:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.option .key {
  flex: none; width: 26px; height: 26px; margin-top: -1px;
  display: grid; place-items: center;
  border-radius: 7px; border: 1px solid var(--border);
  font-size: 13px; font-weight: 700; color: var(--muted);
}
.option:disabled { cursor: default; }
.option.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface-2)); }
.option.selected .key { background: var(--accent); border-color: var(--accent); color: var(--bg); }

.status { min-height: 1.6em; margin: 14px 2px 0; font-size: 14px; color: var(--muted); }

.actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 10px; flex-wrap: wrap; }
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px;
  border: 1px solid var(--border); background: transparent;
  cursor: pointer; font-size: 15px; font-weight: 600;
  transition: background-color .15s, border-color .15s, opacity .15s;
}
.btn.ghost:hover { background: var(--surface-2); border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
kbd {
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 3px 5px; border-radius: 5px;
  border: 1px solid currentColor; opacity: .6;
}

.btn.danger { background: var(--wrong); border-color: var(--wrong); color: #fff; }
.btn.danger:hover { filter: brightness(1.08); }
:root[data-mode="dark"] .btn.danger, :root:not([data-mode]) .btn.danger { color: var(--bg); }

/* ---------- Reset confirmation ---------- */
.dialog {
  width: min(420px, calc(100% - 32px));
  padding: 22px 24px; margin: auto;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(2, 6, 23, 0.55); backdrop-filter: blur(2px); }
.dialog h2 { margin: 0 0 8px; font-size: 19px; }
.dialog p { margin: 0; color: var(--muted); font-size: 15px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ---------- Start screen ---------- */
.intro { text-align: center; padding: 36px 26px; }
.intro h1 { margin: 0 0 18px; font-size: 24px; }
.rules {
  list-style: none; padding: 0; margin: 0 auto 16px; max-width: 460px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
}
.rules li {
  padding: 10px 6px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2); font-size: 13px; color: var(--muted); line-height: 1.35;
}
.rules strong { display: block; font-size: 20px; color: var(--text); font-variant-numeric: tabular-nums; }
.intro p { margin: 0 auto; max-width: 460px; font-size: 14px; }
.intro-actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 20px; }

/* ---------- Round picker ---------- */
.landing { padding: 30px 26px; }
.landing h1 { margin: 0 0 4px; font-size: 24px; text-align: center; }
.landing > p { margin: 0; text-align: center; font-size: 14px; }
.round-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-top: 20px; }
.round-tile {
  display: flex; flex-direction: column; gap: 2px; align-items: flex-start;
  padding: 12px 14px; text-align: left; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  transition: border-color .15s, background-color .15s;
}
.round-tile:hover { border-color: var(--accent); }
.round-tile:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.rt-name { font-weight: 650; font-size: 16px; }
.rt-info { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; line-height: 1.4; }
.round-tile.tried { border-color: color-mix(in srgb, var(--correct) 45%, var(--border)); }
.round-tile.tried .rt-info { color: var(--correct); }
.round-tile.demo { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }
.round-tile.locked { background: transparent; border-style: dashed; }
.round-tile.locked .rt-name { color: var(--muted); }
.round-tile.locked .rt-info { display: inline-flex; align-items: center; gap: 4px; }
.rt-tag {
  display: inline-block; vertical-align: 2px; margin-left: 4px;
  padding: 1px 7px; border-radius: 999px; font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--bg); background: var(--accent);
}

.unlock-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px 16px; flex-wrap: wrap;
  margin: 18px 0 0; padding: 14px 16px;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border)); border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 8%, var(--surface));
}
.unlock-bar p { margin: 0; flex: 1 1 260px; font-size: 14px; }
.unlocked-note { margin: 14px 0 0; text-align: center; font-size: 14px; color: var(--correct); }

/* ---------- Unlock dialog: 1 number → 2 pay → 3 code ---------- */
.unlock-dialog { position: relative; width: min(440px, calc(100% - 32px)); max-height: calc(100dvh - 32px); overflow-y: auto; padding: 24px 24px 18px; }
.unlock-dialog h2 { margin: 0 36px 14px 0; font-size: 21px; }
.unlock-dialog p { margin: 0; color: var(--text); line-height: 1.55; }
.dialog-x {
  position: absolute; top: 14px; right: 14px; width: 34px; height: 34px;
  display: grid; place-items: center; border: 0; border-radius: 50%;
  background: transparent; color: var(--muted); font-size: 24px; line-height: 1; cursor: pointer;
}
.dialog-x:hover { background: var(--surface-2); color: var(--text); }
.dialog-x:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.unlock-steps { display: flex; gap: 6px; margin: 0 0 18px; padding: 0; list-style: none; counter-reset: none; }
.unlock-steps li {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 12.5px; font-weight: 600; color: var(--muted); text-align: center; position: relative;
}
.unlock-steps li span {
  width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%;
  border: 2px solid var(--border); background: var(--surface); font-size: 13px; font-weight: 700; z-index: 1;
}
.unlock-steps li + li::before {
  content: ""; position: absolute; top: 13px; right: 50%; width: 100%; height: 2px; background: var(--border);
}
.unlock-steps li.on { color: var(--text); }
.unlock-steps li.on span { border-color: var(--accent); background: var(--accent); color: var(--bg); }
.unlock-steps li.done span { border-color: var(--accent); color: var(--accent); }
.unlock-steps li.done + li::before, .unlock-steps li.on::before { background: var(--accent); }

.unlock-price {
  display: inline-block; margin-bottom: 12px !important; padding: 4px 12px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--border); font-size: 15px; font-weight: 700;
}
.unlock-label { display: block; margin: 16px 0 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.unlock-input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  font: 600 20px/1.3 system-ui, -apple-system, "Segoe UI", sans-serif; letter-spacing: .04em; font-variant-numeric: tabular-nums;
}
.unlock-input.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .08em; text-transform: uppercase; }
.unlock-input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.unlock-dialog .unlock-msg { min-height: 1.5em; margin: 6px 0 4px; font-size: 14px; color: var(--muted); }
.unlock-dialog .unlock-msg.bad { color: var(--wrong); }
.btn.block { display: flex; width: 100%; justify-content: center; padding: 13px 16px; font-size: 16px; border-radius: 12px; }

.unlock-qr {
  display: block; width: min(300px, 100%); height: auto; margin: 0 auto 16px;
  padding: 10px; border-radius: 14px; background: #fff; box-shadow: 0 0 0 1px var(--border);
}
.unlock-dialog .pay-note { margin-bottom: 18px; font-size: 15px; }
.unlock-dialog .pay-note strong { white-space: nowrap; padding: 0 4px; border-radius: 6px; background: var(--surface-2); }

.unlock-alt { margin-top: 14px !important; text-align: center; font-size: 14px; color: var(--muted) !important; }
.link-btn {
  padding: 0; border: 0; background: none; cursor: pointer;
  color: var(--accent); font-size: 14px; font-weight: 600; text-decoration: underline; text-underline-offset: 3px;
}
.link-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.unlock-dialog .unlock-fine { margin-top: 12px; font-size: 12.5px; color: var(--muted); }
.unlock-dialog .unlock-contact { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--border); text-align: center; font-size: 13.5px; color: var(--muted); }
.unlock-dialog .unlock-contact a { color: var(--muted); font-weight: 600; text-decoration: none; }
.unlock-dialog .unlock-contact a:hover { color: var(--text); text-decoration: underline; }

/* ---------- Results ---------- */
.results { padding: 30px 26px; }
.results > .eyebrow, .results > .res-score, .results > .res-rule { text-align: center; }
.eyebrow { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.res-score { margin: 4px 0 0; font-size: 48px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.res-out { font-size: 22px; font-weight: 600; color: var(--muted); }
.res-rule { margin: 2px 0 0; font-size: 13px; }
.res-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  margin: 22px 0 0;
}
.res-grid div { padding: 10px 8px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); text-align: center; }
.res-grid dt { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.res-grid dd { margin: 2px 0 0; font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.res-grid small { font-size: 12px; font-weight: 600; }
.ok { color: var(--correct); }
.bad { color: var(--wrong); }
.res-actions { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 22px; }

.review { margin-top: 24px; border-top: 1px solid var(--border); padding-top: 14px; }
.review summary { cursor: pointer; font-weight: 600; font-size: 15px; border-radius: 6px; }
.review ol { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 10px; }
.review li { padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface-2); }
.review p { margin: 0; font-size: 15px; line-height: 1.5; }
.review .rq { font-weight: 600; margin-bottom: 4px; }
.review .ra { font-size: 14px; }

.history { margin-top: 24px; border-top: 1px solid var(--border); padding-top: 14px; }
.history h2 { margin: 0 0 10px; font-size: 15px; }
.table-wrap { overflow-x: auto; }
.history table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.history th { text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 6px 10px 6px 0; }
.history td { padding: 7px 10px 7px 0; border-top: 1px solid var(--border); }
.history tr.current td { font-weight: 650; color: var(--accent); }

.muted { color: var(--muted); }

.hint { text-align: center; font-size: 12px; color: var(--muted); padding: 0 16px 16px; }

@media (max-width: 640px) {
  body { font-size: 16px; }
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 16px; }
  .card { padding: 18px 16px; border-radius: 14px; }
  .question { font-size: 18px; }
  .actions .btn { flex: 1; justify-content: center; }
  .intro, .results { padding: 24px 16px; }
  .rules { grid-template-columns: repeat(2, 1fr); }
  .landing { padding: 22px 14px; }
  .round-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .intro-actions .btn, .res-actions .btn { flex: 1 1 auto; justify-content: center; }
  .res-grid { grid-template-columns: repeat(2, 1fr); }
  .res-grid div:last-child { grid-column: span 2; }
  .res-score { font-size: 40px; }
  kbd, .hint { display: none; }
  .controls { gap: 6px; margin-left: auto; }
  .bar-btn { padding: 0 10px; font-size: 13px; }
}

@media (max-width: 400px) {
  .bar-btn svg { display: none !important; }
}

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

/* ---------- Terms ---------- */
.unlock-terms { margin-top: 10px !important; text-align: center; font-size: 12.5px; color: var(--muted) !important; }
.unlock-dialog .pay-terms { margin: -6px 0 14px !important; }
.unlock-terms a, .site-legal a, .legal a { color: var(--accent); text-underline-offset: 3px; }
.site-legal { text-align: center; font-size: 12px; line-height: 1.6; color: var(--muted); padding: 0 16px 20px; }
.legal-page { display: block; }
.legal-home { color: var(--text); text-decoration: none; }
.legal { max-width: 760px; margin: 0 auto; padding: 24px 20px 48px; line-height: 1.7; font-size: 15.5px; }
.legal h1 { font-size: 26px; line-height: 1.3; margin: 8px 0 12px; }
.legal h2 { font-size: 18px; margin: 32px 0 8px; padding-top: 20px; border-top: 1px solid var(--border); }
.legal p, .legal li { color: var(--text); }
.legal ul { padding-left: 22px; margin: 8px 0; }
.legal li { margin: 8px 0; }
.legal-meta { color: var(--muted) !important; font-size: 14px; }
.legal-back { font-size: 14px; margin: 0; }
.legal-end { margin-top: 36px; }
.legal-summary { padding: 14px 16px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); }
.legal ul ul { margin: 6px 0; }
.legal ul ul li { margin: 4px 0; }
