:root {
  --bg: #f4f3ee;
  --surface: #ffffff;
  --surface-2: #ecebe4;
  --text: #1c1f1d;
  --muted: #5f655f;
  --line: #d9d8cf;
  --accent: #3d6b47;
  --accent-ink: #ffffff;
  --accent-soft: #dce9dd;
  --ok: #2e7d4a;
  --ok-soft: #dff1e4;
  --bad: #b8382f;
  --bad-soft: #f8e1de;
  --warn: #b7791f;
  --warn-soft: #f7ecd8;
  --new: #b9bbb3;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121513; --surface: #1b1f1c; --surface-2: #242a26; --text: #e8ebe7; --muted: #9aa39b;
    --line: #313832; --accent: #6fae7b; --accent-ink: #0f1510; --accent-soft: #23362a;
    --ok: #5cc27e; --ok-soft: #1d3325; --bad: #f07267; --bad-soft: #3b201d; --warn: #e3a64a; --warn-soft: #3a2e1a;
    --new: #535a54; --shadow: 0 1px 2px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121513; --surface: #1b1f1c; --surface-2: #242a26; --text: #e8ebe7; --muted: #9aa39b;
  --line: #313832; --accent: #6fae7b; --accent-ink: #0f1510; --accent-soft: #23362a;
  --ok: #5cc27e; --ok-soft: #1d3325; --bad: #f07267; --bad-soft: #3b201d; --warn: #e3a64a; --warn-soft: #3a2e1a;
  --new: #535a54; --shadow: 0 1px 2px rgba(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-family: var(--font);
  font-size: 16px; line-height: 1.5; min-height: 100vh;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.tiny { font-size: .78rem; }
.num { font-variant-numeric: tabular-nums; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: #1f2a22; color: #eef2ec;
}
.brand { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-weight: 700; letter-spacing: .2px; }
.topnav { display: none; margin-left: auto; gap: 4px; }
.topnav a { color: #cfd8cf; text-decoration: none; padding: 6px 10px; border-radius: 8px; font-size: .95rem; }
.topnav a.active, .topnav a:hover { background: rgba(255,255,255,.1); color: #fff; }
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: flex;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 7px 0 6px; font-size: .7rem; color: var(--muted); text-decoration: none; }
.tabbar a .ti { font-size: 1.15rem; line-height: 1.2; }
.tabbar a.active { color: var(--accent); font-weight: 600; }
body.in-run .tabbar, body.in-run .topbar { display: none; }
body.in-run { padding-bottom: 0; }
@media (min-width: 820px) {
  .topnav { display: flex; }
  .tabbar { display: none; }
  body { padding-bottom: 24px; }
}

main { max-width: 860px; margin: 0 auto; padding: 16px; outline: none; }

/* cards & layout */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card.flat { box-shadow: none; border: 1px solid var(--line); }
.row { display: flex; gap: 12px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
@media (max-width: 420px) { .grid4 { grid-template-columns: repeat(2, 1fr); } }
.section-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 0 8px; font-weight: 600; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 10px 16px; border-radius: 11px; font-weight: 600; cursor: pointer; text-decoration: none;
  min-height: 44px; transition: transform .05s, background .15s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn.sm { min-height: 34px; padding: 6px 12px; font-size: .875rem; border-radius: 9px; }
.btn[disabled] { opacity: .45; cursor: default; }
.btn .badge { background: rgba(0,0,0,.12); border-radius: 99px; padding: 0 8px; font-size: .8rem; }
.btn.primary .badge { background: rgba(255,255,255,.25); }
.actions { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
.actions .btn { width: 100%; }
.actions .wide { grid-column: 1 / -1; }

/* stats tiles */
.tile { background: var(--surface-2); border-radius: 11px; padding: 10px 12px; }
.tile .v { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .l { font-size: .78rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }
.dot.ok { background: var(--ok); } .dot.learn { background: var(--warn); } .dot.bad { background: var(--bad); } .dot.new { background: var(--new); }

/* progress */
.pbar { display: flex; height: 8px; border-radius: 99px; overflow: hidden; background: var(--surface-2); }
.pbar span { display: block; height: 100%; }
.pbar .ok { background: var(--ok); } .pbar .learn { background: var(--warn); } .pbar .bad { background: var(--bad); }
.ring { position: relative; width: 112px; height: 112px; flex: none; }
.ring svg { transform: rotate(-90deg); }
.ring .c { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring .c b { font-size: 1.6rem; font-variant-numeric: tabular-nums; }

/* group list */
.chapter { margin-bottom: 18px; }
.chapter-h { display: flex; align-items: baseline; gap: 8px; margin: 18px 2px 8px; }
.chapter-h h2 { margin: 0; }
.glist { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.gitem { display: block; padding: 12px 16px; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; }
.gitem:last-child { border-bottom: none; }
.gitem:hover { background: var(--surface-2); }
.gitem .t { display: flex; gap: 8px; align-items: baseline; margin-bottom: 6px; }
.gitem .t b { font-weight: 600; flex: 1; }
.gitem .t .r { font-size: .78rem; color: var(--muted); white-space: nowrap; }

/* run screen */
.run-top {
  position: sticky; top: 0; z-index: 15; background: var(--bg);
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px; margin: -16px -16px 12px;
  border-bottom: 1px solid var(--line);
}
.run-top .row { gap: 8px; }
.run-top .title { font-weight: 600; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; padding: 3px 10px; border-radius: 8px; background: var(--surface-2); }
.timer.low { background: var(--bad-soft); color: var(--bad); }
.run-progress { height: 4px; background: var(--surface-2); border-radius: 99px; margin-top: 8px; overflow: hidden; }
.run-progress span { display: block; height: 100%; background: var(--accent); transition: width .2s; }
.qmeta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: .82rem; color: var(--muted); margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 99px; background: var(--surface-2); font-size: .78rem; color: var(--muted); white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); } .chip.bad { background: var(--bad-soft); color: var(--bad); } .chip.warn { background: var(--warn-soft); color: var(--warn); }
.qtext { font-size: 1.12rem; font-weight: 600; margin: 4px 0 14px; }
.qimg { display: block; max-width: 100%; height: auto; margin: 0 auto 12px; border-radius: 10px; background: #fff; }
.answers { display: grid; gap: 10px; }
.ans {
  display: flex; gap: 12px; align-items: flex-start; text-align: left; width: 100%;
  background: var(--surface); border: 2px solid var(--line); border-radius: 12px; padding: 12px 14px; cursor: pointer;
  transition: border-color .12s, background .12s;
}
.ans:hover:not([disabled]) { border-color: var(--accent); }
.ans .L { flex: none; width: 28px; height: 28px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-weight: 700; background: var(--surface-2); }
.ans .T { flex: 1; padding-top: 2px; }
.ans.sel { border-color: var(--accent); background: var(--accent-soft); }
.ans.sel .L { background: var(--accent); color: var(--accent-ink); }
.ans.correct { border-color: var(--ok); background: var(--ok-soft); }
.ans.correct .L { background: var(--ok); color: #fff; }
.ans.wrong { border-color: var(--bad); background: var(--bad-soft); }
.ans.wrong .L { background: var(--bad); color: #fff; }
.ans.dim { opacity: .55; }
.ans[disabled] { cursor: default; }
.feedback { margin-top: 14px; padding: 12px 14px; border-radius: 12px; font-weight: 600; display: flex; gap: 10px; align-items: center; }
.feedback.ok { background: var(--ok-soft); color: var(--ok); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.note-box { margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--warn-soft); border-left: 4px solid var(--warn); white-space: pre-wrap; word-wrap: break-word; }
.note-box .h { font-size: .78rem; font-weight: 700; color: var(--warn); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 4px; }
.run-bottom {
  position: sticky; bottom: 0; background: var(--bg); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); margin: 16px -16px -16px;
  border-top: 1px solid var(--line); display: flex; gap: 8px;
}
.run-bottom .btn { flex: 1; }
.run-bottom .btn.icon { flex: none; width: 48px; padding: 0; }
.star-on { color: var(--warn); }

/* exam grid */
.qgrid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px; }
@media (max-width: 480px) { .qgrid { grid-template-columns: repeat(6, 1fr); } }
.qgrid button { aspect-ratio: 1; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; font-size: .85rem; cursor: pointer; padding: 0; min-height: 36px; }
.qgrid button.done { background: var(--accent-soft); border-color: var(--accent); }
.qgrid button.cur { outline: 2px solid var(--accent); outline-offset: 1px; }
.qgrid button.ok { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.qgrid button.bad { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }

/* result */
.result-hero { text-align: center; padding: 26px 16px; }
.result-hero .big { font-size: 3rem; font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1.1; }
.result-hero.pass { background: var(--ok-soft); color: var(--ok); }
.result-hero.fail { background: var(--bad-soft); color: var(--bad); }

/* question list */
.qlist { display: grid; gap: 10px; }
.qcard { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); padding: 12px 14px; cursor: pointer; }
.qcard .h { display: flex; gap: 8px; align-items: center; margin-bottom: 4px; font-size: .8rem; color: var(--muted); }
.qcard .q { font-weight: 600; }
.qcard .a { margin-top: 6px; color: var(--ok); font-size: .92rem; }
.qcard .a.mine { color: var(--bad); }
.qcard .a b { margin-right: 4px; }
mark { background: var(--warn-soft); color: inherit; border-radius: 3px; padding: 0 2px; }

/* history */
.hist-item { display: flex; gap: 12px; align-items: center; padding: 11px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.hist-item:last-child { border-bottom: 0; }
.hist-item:hover { background: var(--surface-2); }
.score-pill { min-width: 62px; text-align: center; font-weight: 700; border-radius: 9px; padding: 4px 8px; font-variant-numeric: tabular-nums; background: var(--surface-2); }
.score-pill.ok { background: var(--ok-soft); color: var(--ok); } .score-pill.bad { background: var(--bad-soft); color: var(--bad); }
.heat { display: grid; grid-auto-flow: column; grid-template-rows: repeat(7, 1fr); gap: 3px; }
.heat span { width: 100%; aspect-ratio: 1; border-radius: 3px; background: var(--surface-2); }
.heat span.l1 { background: color-mix(in srgb, var(--accent) 30%, var(--surface-2)); }
.heat span.l2 { background: color-mix(in srgb, var(--accent) 55%, var(--surface-2)); }
.heat span.l3 { background: color-mix(in srgb, var(--accent) 80%, var(--surface-2)); }
.heat span.l4 { background: var(--accent); }
.chart { width: 100%; height: auto; display: block; }

/* forms */
input[type="search"], input[type="text"], textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px;
}
textarea { min-height: 120px; resize: vertical; }
label.opt { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
label.opt:last-child { border-bottom: 0; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; cursor: pointer; font-weight: 600; font-size: .88rem; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* modal */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
@media (min-width: 700px) { .modal-back { align-items: center; } }
.modal {
  background: var(--bg); width: 100%; max-width: 680px; max-height: 92vh; overflow: auto;
  border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
}
@media (min-width: 700px) { .modal { border-radius: 18px; } }
.modal-h { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.modal-h h2 { margin: 0; flex: 1; }

.toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px); background: #222; color: #fff; padding: 9px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; font-size: .9rem; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.kbd { font-size: .75rem; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 0 5px; color: var(--muted); }
.btn.primary .kbd { color: inherit; border-color: currentColor; opacity: .6; }
@media (hover: none) { .kbd-hint { display: none; } }
