:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #17202b;
  --muted: #5a6675;
  --border: #d9dee5;
  --accent: #0b5cad;
  --accent-hover: #094c8f;
  --accent-soft: #e3eefa;
  --on-accent: #ffffff;
  --good: #17813a;
  --good-bg: #e4f4e9;
  --bad: #c42b2b;
  --bad-bg: #fcebeb;
  --flag: #c76a00;
  --seen: #9ec2ea;
  --mastered: #17813a;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141a;
    --surface: #18202a;
    --surface-2: #212b37;
    --text: #e6ebf1;
    --muted: #9aa7b6;
    --border: #2c3846;
    --accent: #5aa6f0;
    --accent-hover: #7ab8f4;
    --accent-soft: #1b3350;
    --on-accent: #0b1622;
    --good: #4cc274;
    --good-bg: #173322;
    --bad: #f07070;
    --bad-bg: #3a1c1f;
    --flag: #f0a040;
    --seen: #3a6894;
    --mastered: #4cc274;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141a;
  --surface: #18202a;
  --surface-2: #212b37;
  --text: #e6ebf1;
  --muted: #9aa7b6;
  --border: #2c3846;
  --accent: #5aa6f0;
  --accent-hover: #7ab8f4;
  --accent-soft: #1b3350;
  --on-accent: #0b1622;
  --good: #4cc274;
  --good-bg: #173322;
  --bad: #f07070;
  --bad-bg: #3a1c1f;
  --flag: #f0a040;
  --seen: #3a6894;
  --mastered: #4cc274;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 760px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 16px;
}
.brand {
  display: flex; align-items: center; gap: 8px;
  color: var(--text); text-decoration: none; font-weight: 700; white-space: nowrap;
}
.brand-tag {
  font-size: 0.7rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--accent); background: var(--accent-soft); border-radius: 6px; padding: 1px 6px;
}
.nav { display: flex; gap: 2px; }
.nav a {
  display: flex; align-items: center; min-height: 40px; padding: 0 12px;
  border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 600; font-size: 0.95rem;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { color: var(--accent); background: var(--accent-soft); }
@media (max-width: 480px) {
  .brand-tag { display: none; }
  .nav a { padding: 0 9px; }
}

.container {
  max-width: 760px; margin: 0 auto;
  padding: 16px 16px calc(48px + env(safe-area-inset-bottom));
  outline: none;
}
.loading { text-align: center; color: var(--muted); padding: 48px 0; }

/* ---------- Cards, buttons ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.875rem; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: 0.5; cursor: default; }

.chip {
  min-height: 34px; padding: 0 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  font-size: 0.85rem; font-weight: 600; cursor: pointer;
}
.chip.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

/* ---------- Home ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .num { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 0.8rem; color: var(--muted); }

.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.quick .btn { flex-direction: column; min-height: 72px; padding: 8px; text-align: center; line-height: 1.2; }
.quick .btn small { font-weight: 500; color: inherit; opacity: 0.8; }
@media (max-width: 480px) {
  .quick { grid-template-columns: 1fr 1fr; }
  .quick .btn:first-child { grid-column: 1 / -1; }
}

.bar {
  position: relative; height: 8px; border-radius: 999px;
  background: var(--surface-2); overflow: hidden;
}
.bar span { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 999px; }
.bar .seen { background: var(--seen); }
.bar .mastered { background: var(--mastered); }
.bar .fill { background: var(--accent); }

.legend { display: flex; gap: 14px; font-size: 0.8rem; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.sections { display: grid; gap: 8px; }
details.section { padding: 0; }
details.section > summary {
  list-style: none; cursor: pointer; padding: 14px 16px;
  display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: center;
}
details.section > summary::-webkit-details-marker { display: none; }
.sid {
  font-weight: 700; color: var(--accent); background: var(--accent-soft);
  border-radius: 8px; padding: 2px 8px; font-size: 0.9rem; align-self: start;
}
.stitle { font-weight: 600; }
.smeta { font-size: 0.8rem; color: var(--muted); }
.chev { transition: transform 0.15s; color: var(--muted); }
details[open] .chev { transform: rotate(90deg); }
.summary-bar { grid-column: 2 / 4; }
.groups { border-top: 1px solid var(--border); padding: 8px 16px 16px; }
.group {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--border);
}
.group:last-of-type { border-bottom: 0; }
.group .gtitle { font-size: 0.9rem; }
.group .gtitle b { color: var(--accent); margin-right: 6px; }
.group .bar { grid-column: 1 / 2; height: 6px; }

/* ---------- Question ---------- */
.study-head { display: grid; gap: 8px; margin-bottom: 12px; }
.crumbs { font-size: 0.9rem; }
.crumbs a { text-decoration: none; font-weight: 600; }
.qcount { font-size: 0.85rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.q-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 0.85rem; color: var(--muted); margin-bottom: 8px; }
.qid { font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.streak { display: inline-flex; gap: 3px; margin-left: auto; }
.streak i { width: 8px; height: 8px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--border); }
.streak i.on { background: var(--mastered); border-color: var(--mastered); }
.q-text { font-size: 1.1rem; font-weight: 600; margin-bottom: 14px; }

.fig { margin: 0 0 14px; }
.fig img {
  display: block; width: 100%; max-width: 520px; height: auto;
  background: #fff; border-radius: 8px; border: 1px solid var(--border);
}
.fig figcaption { font-size: 0.8rem; color: var(--muted); margin-top: 4px; }

.choices { display: grid; gap: 8px; }
.choice {
  display: grid; grid-template-columns: 32px 1fr; gap: 10px; align-items: start;
  width: 100%; text-align: left; padding: 12px; min-height: 52px;
  border: 1.5px solid var(--border); border-radius: 10px;
  background: var(--surface); cursor: pointer;
}
.choice:hover { border-color: var(--accent); }
.choice .letter {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--surface-2); font-weight: 700; font-size: 0.9rem;
}
.choice .ctext { padding-top: 2px; }
.choice.selected { border-color: var(--accent); background: var(--accent-soft); }
.choice.selected .letter { background: var(--accent); color: var(--on-accent); }
.choice.correct { border-color: var(--good); background: var(--good-bg); }
.choice.correct .letter { background: var(--good); color: #fff; }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }
.choice.wrong .letter { background: var(--bad); color: #fff; }
.choice.dim { opacity: 0.6; }
.choices.locked .choice { cursor: default; }
.choices.locked .choice:hover { border-color: inherit; }
.choices.locked .choice.correct:hover { border-color: var(--good); }
.choices.locked .choice.wrong:hover { border-color: var(--bad); }
.choices.locked .choice.dim:hover { border-color: var(--border); }

.feedback { margin-top: 14px; padding: 12px 14px; border-radius: 10px; font-weight: 600; }
.feedback.good { background: var(--good-bg); color: var(--good); }
.feedback.bad { background: var(--bad-bg); color: var(--bad); }
.feedback .small { font-weight: 400; color: var(--text); }

.actions {
  display: flex; gap: 8px; justify-content: space-between; align-items: center;
  margin-top: 14px;
}
.actions .btn.primary { min-width: 140px; margin-left: auto; }
.kbd-hint { font-size: 0.8rem; color: var(--muted); }
@media (hover: none) { .kbd-hint { display: none; } }

/* ---------- Exam ---------- */
.exam-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.flag-btn.on { color: var(--flag); border-color: var(--flag); }
.grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 6px;
  margin-top: 16px;
}
.grid button {
  position: relative; height: 38px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); cursor: pointer;
  font-size: 0.85rem; font-weight: 600; font-variant-numeric: tabular-nums;
}
.grid button.answered { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.grid button.current { outline: 2px solid var(--text); outline-offset: 1px; }
.grid button.flagged::after {
  content: ""; position: absolute; top: 3px; right: 3px;
  width: 7px; height: 7px; border-radius: 50%; background: var(--flag);
}
.grid button.good { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.grid button.bad { background: var(--bad-bg); border-color: var(--bad); color: var(--bad); }

.score { text-align: center; padding: 24px 16px; }
.score .big { font-size: 3rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.badge {
  display: inline-block; padding: 4px 14px; border-radius: 999px;
  font-weight: 800; letter-spacing: 0.06em; margin-top: 8px;
}
.badge.pass { background: var(--good-bg); color: var(--good); }
.badge.fail { background: var(--bad-bg); color: var(--bad); }

table.breakdown { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
table.breakdown td { padding: 8px 4px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.breakdown td.n { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; width: 1%; }
table.breakdown td.b { width: 35%; }

.history { list-style: none; margin: 0; padding: 0; }
.history li { border-bottom: 1px solid var(--border); }
.history li:last-child { border-bottom: 0; }
.history a { display: flex; justify-content: space-between; gap: 8px; padding: 10px 0; color: var(--text); text-decoration: none; }
.history .s { font-weight: 700; font-variant-numeric: tabular-nums; }
.history .s.pass { color: var(--good); }
.history .s.fail { color: var(--bad); }

.review-item + .review-item { margin-top: 12px; }

/* ---------- Browse ---------- */
.filters { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 14rem); gap: 8px; }
@media (max-width: 480px) { .filters { grid-template-columns: minmax(0, 1fr); } }
.filters input, .filters select, select.input {
  min-height: 44px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
  width: 100%;
}
.browse-list { display: grid; gap: 10px; }
.browse-item .choices { gap: 4px; }
.browse-item .choice { min-height: 0; padding: 6px 8px; cursor: default; font-size: 0.95rem; border-width: 1px; }
.browse-item .choice:hover { border-color: var(--border); }
.browse-item .choice.correct:hover { border-color: var(--good); }
.browse-item .q-text { font-size: 1rem; margin-bottom: 10px; }
.browse-item.hidden-answers .choice.correct { border-color: var(--border); background: var(--surface); }
.browse-item.hidden-answers .choice.correct .letter { background: var(--surface-2); color: inherit; }
.reveal { margin-top: 8px; }

/* ---------- Settings ---------- */
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.setting:last-child { border-bottom: 0; }
.setting label { font-weight: 600; }
.setting p { margin: 2px 0 0; }
.switch { position: relative; width: 48px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 100%; height: 100%; margin: 0; position: absolute; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: background 0.15s; }
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px;
  border-radius: 50%; background: #fff; transition: transform 0.15s; box-shadow: var(--shadow);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.sync-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.sync-dot.on { background: var(--good); }
.sync-dot.err { background: var(--bad); }

/* ---------- Buy Me a Coffee ---------- */
.btn.coffee { background: #ffdd00; border-color: #ffdd00; color: #000; }
.btn.coffee:hover { background: #f5d300; }
.coffee-inline { font-weight: 600; white-space: nowrap; }
.site-footer {
  max-width: 760px; margin: 0 auto;
  padding: 0 16px calc(24px + env(safe-area-inset-bottom));
  text-align: center; font-size: 0.875rem; color: var(--muted);
}
.site-footer:empty { display: none; }

.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
