:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #121821;
  --muted: #5a6474;
  --border: #dde2ea;
  --primary: #1456d9;
  --primary-ink: #ffffff;
  --primary-soft: #e3ecff;
  --ok: #10805a;
  --ok-soft: #dff4ea;
  --bad: #c42b2b;
  --bad-soft: #fde6e6;
  --warn: #a86200;
  --warn-soft: #fff1d9;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .05);
  --radius: 14px;
  --tabbar-h: 62px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1218;
    --surface: #171c24;
    --surface-2: #1f2630;
    --text: #e8edf4;
    --muted: #9aa5b5;
    --border: #2b3340;
    --primary: #5b8dff;
    --primary-ink: #0a1020;
    --primary-soft: #1c2a4a;
    --ok: #3cc58f;
    --ok-soft: #12352a;
    --bad: #ff6b6b;
    --bad-soft: #3d1d1f;
    --warn: #f0a640;
    --warn-soft: #3a2c14;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1218;
  --surface: #171c24;
  --surface-2: #1f2630;
  --text: #e8edf4;
  --muted: #9aa5b5;
  --border: #2b3340;
  --primary: #5b8dff;
  --primary-ink: #0a1020;
  --primary-soft: #1c2a4a;
  --ok: #3cc58f;
  --ok-soft: #12352a;
  --bad: #ff6b6b;
  --bad-soft: #3d1d1f;
  --warn: #f0a640;
  --warn-soft: #3a2c14;
  --shadow: none;
  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;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  min-height: 100vh;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--primary); }
img { max-width: 100%; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 6px;
  min-height: 56px;
  padding: env(safe-area-inset-top) 12px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { flex: 1; margin: 0; font-size: 18px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-left: 4px; }
#topbar-extra { font-variant-numeric: tabular-nums; font-weight: 600; }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center;
  border: 0; border-radius: 10px; background: transparent; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
main { max-width: 680px; margin: 0 auto; padding: 16px; outline: none; }
.loading { color: var(--muted); text-align: center; margin-top: 40px; }
.stack > * + * { margin-top: 14px; }
.section-title { font-size: 13px; font-weight: 650; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); margin: 22px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 4px; font-size: 18px; }
.card p { margin: 0; }

.notice {
  background: var(--warn-soft); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
  border-radius: 12px; padding: 12px 14px; font-size: 14px;
}

.hero {
  background: var(--primary); color: var(--primary-ink);
  border-radius: var(--radius); padding: 18px;
  display: flex; align-items: center; gap: 14px;
}
.hero .big { font-size: 34px; font-weight: 750; line-height: 1; font-variant-numeric: tabular-nums; }
.hero p { margin: 4px 0 0; opacity: .9; font-size: 14px; }
.hero .btn { margin-left: auto; flex: 0 0 auto; white-space: nowrap; background: var(--primary-ink); color: var(--primary); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border-radius: 12px; border: 1px solid transparent;
  background: var(--primary); color: var(--primary-ink);
  font-weight: 600; text-decoration: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row .btn { flex: 1 1 140px; }

/* ---------- mode grid ---------- */
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile {
  display: flex; flex-direction: column; gap: 6px;
  padding: 14px; min-height: 104px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); text-decoration: none; box-shadow: var(--shadow);
}
.tile:active { transform: scale(.985); }
.tile .ico { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); font-size: 18px; }
.tile strong { font-size: 16px; }
.tile span { font-size: 13px; color: var(--muted); }
.tile.bad .ico { background: var(--bad-soft); color: var(--bad); }
.tile.ok .ico { background: var(--ok-soft); color: var(--ok); }
.tile.warn .ico { background: var(--warn-soft); color: var(--warn); }

/* ---------- ticket grid ---------- */
.tickets { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.ticket {
  position: relative; aspect-ratio: 1; display: grid; place-items: center;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  font-weight: 700; font-size: 18px; text-decoration: none; color: var(--text);
  overflow: hidden;
}
.ticket .fill { position: absolute; left: 0; bottom: 0; height: 4px; background: var(--ok); }
.ticket.done { background: var(--ok-soft); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
.ticket.has-bad { border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; min-height: 38px; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- lists ---------- */
.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px;
  text-decoration: none; color: var(--text); border-top: 1px solid var(--border);
}
.row:first-child { border-top: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; }
.row .sub { font-size: 13px; color: var(--muted); }
.chev { color: var(--muted); }

.bar { height: 6px; border-radius: 6px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--ok); border-radius: 6px; }

/* ---------- question ---------- */
.q-meta { display: flex; justify-content: space-between; align-items: center; font-size: 14px; color: var(--muted); margin-bottom: 10px; }
.dots { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.dots::-webkit-scrollbar { display: none; }
.dot {
  flex: 0 0 auto; min-width: 34px; height: 34px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--surface);
  font-size: 13px; font-weight: 600; cursor: pointer; color: var(--muted);
}
.dot.cur { border-color: var(--primary); color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); }
.dot.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.dot.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.dot.ans { background: var(--primary-soft); color: var(--primary); border-color: transparent; }

.q-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.q-img { display: block; width: 100%; max-height: min(260px, 32vh); object-fit: contain; background: var(--surface-2); padding: 12px; }
.q-text { margin: 0; padding: 16px; font-size: 17px; font-weight: 600; line-height: 1.45; }
.no-img { padding: 8px 16px 0; font-size: 13px; color: var(--muted); }

.options { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 8px; }
.opt {
  width: 100%; display: flex; align-items: flex-start; gap: 12px; text-align: left;
  padding: 13px 14px; border-radius: 12px;
  background: var(--surface); border: 1.5px solid var(--border);
  cursor: pointer; line-height: 1.4; -webkit-tap-highlight-color: transparent;
}
.opt:hover:not(:disabled) { border-color: var(--primary); }
.opt:disabled { cursor: default; }
.opt .k {
  flex: 0 0 auto; width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: var(--surface-2); font-size: 13px; font-weight: 700; color: var(--muted);
}
.opt.sel { border-color: var(--primary); background: var(--primary-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.right .k { background: var(--ok); color: #fff; }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt.wrong .k { background: var(--bad); color: #fff; }
.opt.dim { opacity: .55; }

.explain { margin-top: 12px; padding: 14px; border-radius: 12px; background: var(--surface-2); font-size: 15px; }
.explain strong { display: block; margin-bottom: 4px; }
.verdict { font-weight: 700; margin-top: 12px; }
.verdict.ok { color: var(--ok); }
.verdict.bad { color: var(--bad); }

.q-actions {
  position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  display: flex; gap: 10px; padding: 12px 0; margin-top: 6px;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.q-actions .btn { flex: 1; }

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

.kpis { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.kpi b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; line-height: 1.2; }
.kpi span { font-size: 13px; color: var(--muted); }

.chart { display: flex; align-items: flex-end; gap: 4px; height: 120px; padding-top: 8px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; height: 100%; }
.chart .col i { display: block; width: 100%; max-width: 22px; border-radius: 4px 4px 0 0; background: var(--primary); min-height: 2px; }
.chart .col i.zero { background: var(--surface-2); }
.chart .col small { font-size: 10px; color: var(--muted); }

.boxes { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; text-align: center; }
.boxes div { background: var(--surface-2); border-radius: 10px; padding: 8px 2px; }
.boxes b { display: block; font-size: 18px; }
.boxes span { font-size: 11px; color: var(--muted); }

/* ---------- forms ---------- */
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-top: 1px solid var(--border); }
.field:first-child { border-top: 0; }
.field label { font-weight: 550; }
.field select, .field input[type="number"] {
  min-height: 40px; padding: 0 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface-2); max-width: 150px;
}
.field input[type="number"] { width: 84px; }
.switch { width: 22px; height: 22px; accent-color: var(--primary); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  max-width: calc(100% - 32px);
}

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 11px; color: var(--muted); text-decoration: none; -webkit-tap-highlight-color: transparent;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--primary); }

body.focus .tabbar { display: none; }
body.focus { padding-bottom: env(safe-area-inset-bottom); }
body.focus .q-actions { bottom: env(safe-area-inset-bottom); }

@media (min-width: 560px) {
  .grid { grid-template-columns: repeat(3, 1fr); }
  .kpis { grid-template-columns: repeat(4, 1fr); }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
