:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef2f7;
  --text: #142030;
  --muted: #5a6778;
  --border: #d9e0e8;
  --accent: #1d4f91;
  --accent-hover: #173f75;
  --accent-soft: #e5edf8;
  --on-accent: #ffffff;
  --good: #17703f;
  --good-bg: #e5f4eb;
  --bad: #b3261e;
  --bad-bg: #fdeceb;
  --warn: #8a5a00;
  --warn-bg: #fff4dc;
  --focus: #2f6fd0;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --radius: 12px;
  --radius-sm: 8px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d131a;
    --surface: #151e28;
    --surface-2: #1c2734;
    --text: #e5ebf2;
    --muted: #9aa8b7;
    --border: #2a3847;
    --accent: #7aaef5;
    --accent-hover: #9cc3fa;
    --accent-soft: #1b2c42;
    --on-accent: #0b1622;
    --good: #5cc98f;
    --good-bg: #12301f;
    --bad: #ff8a80;
    --bad-bg: #3a1916;
    --warn: #f0c060;
    --warn-bg: #332611;
    --focus: #8bb9ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0d131a;
  --surface: #151e28;
  --surface-2: #1c2734;
  --text: #e5ebf2;
  --muted: #9aa8b7;
  --border: #2a3847;
  --accent: #7aaef5;
  --accent-hover: #9cc3fa;
  --accent-soft: #1b2c42;
  --on-accent: #0b1622;
  --good: #5cc98f;
  --good-bg: #12301f;
  --bad: #ff8a80;
  --bad-bg: #3a1916;
  --warn: #f0c060;
  --warn-bg: #332611;
  --focus: #8bb9ff;
  --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;
  min-height: 100vh;
  min-height: 100dvh;
}

h1, h2, h3 { line-height: 1.25; margin: 0 0 0.5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 0.75rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.hidden { display: none !important; }
.mt { margin-top: 0.9rem; }
.gap-top { margin: 0.6rem 0 0; }
.center { text-align: center; }
.grow { flex: 1; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- layout ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0.6rem 16px;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  margin-right: auto;
  min-width: 0;
}
.brand-mark {
  width: 30px;
  height: 30px;
  flex: none;
  border-radius: 8px;
  background: var(--accent);
  color: var(--on-accent);
  display: grid;
  place-items: center;
  font-size: 0.8rem;
  letter-spacing: 0.02em;
}
.brand-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-weight: 500; color: var(--muted); }
.nav { display: flex; gap: 0.25rem; }
.nav a {
  padding: 0.4rem 0.65rem;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.95rem;
}
.nav a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }

main {
  max-width: 760px;
  margin: 0 auto;
  padding: 1.25rem 16px 6rem;
}
main.wide { max-width: 1000px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem;
  margin-bottom: 1rem;
}
.card > :last-child { margin-bottom: 0; }

.section-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 700;
  margin: 0 0 0.6rem;
}

.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.stack > * + * { margin-top: 0.75rem; }

/* ---------- controls ---------- */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 0.7rem 1.1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: var(--on-accent);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  text-decoration: none;
}
.btn:hover { background: var(--accent-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--surface-2); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--bad); color: #fff; }
.btn.small { padding: 0.35rem 0.7rem; min-height: 34px; font-size: 0.875rem; }
.btn.block { width: 100%; }

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 18px; height: 18px; }

label.field { display: block; font-weight: 600; margin-bottom: 0.35rem; }
.input {
  width: 100%;
  font: inherit;
  font-size: 1.05rem;
  padding: 0.7rem 0.8rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-height: 46px;
}
.input:focus { outline: 2px solid var(--focus); outline-offset: 0; border-color: transparent; }
.input.pin { letter-spacing: 0.4em; font-size: 1.3rem; text-align: center; }

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 4px;
  padding: 4px;
  background: var(--surface-2);
  border-radius: 10px;
}
.segmented button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 600;
  padding: 0.55rem 0.4rem;
  border-radius: 7px;
  cursor: pointer;
  min-height: 42px;
}
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented button:disabled { opacity: 0.45; cursor: not-allowed; }

.check {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.6rem 0.2rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.check:last-child { border-bottom: 0; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); flex: none; margin: 0; }
.check .label { flex: 1; }
.check .count { color: var(--muted); font-size: 0.875rem; font-variant-numeric: tabular-nums; }

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 0;
  border-bottom: 1px solid var(--border);
}
.switch-row:last-child { border-bottom: 0; }
.switch-row .desc { display: block; color: var(--muted); font-size: 0.85rem; }
.switch {
  appearance: none;
  width: 46px;
  height: 28px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  flex: none;
  margin: 0;
  transition: background 0.15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }

.alert {
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.85rem;
  margin-bottom: 0.9rem;
  font-size: 0.95rem;
}
.alert.error { background: var(--bad-bg); color: var(--bad); }
.alert.info { background: var(--accent-soft); color: var(--text); }
.alert.warn { background: var(--warn-bg); color: var(--warn); }

.badge {
  display: inline-block;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }

.sticky-actions {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 0.7rem 16px calc(0.7rem + env(safe-area-inset-bottom));
  z-index: 5;
}
.sticky-actions .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 0.6rem; align-items: center; }
.sticky-actions .inner .grow { flex: 1; }

/* ---------- login ---------- */

.auth { max-width: 420px; margin: 2rem auto 0; }
.auth .intro { text-align: center; margin-bottom: 1.25rem; }
.auth .intro .brand-mark { width: 52px; height: 52px; font-size: 1.1rem; border-radius: 14px; margin: 0 auto 0.75rem; }
.steps { display: flex; gap: 6px; margin-bottom: 1rem; }
.steps span { flex: 1; height: 4px; border-radius: 2px; background: var(--border); }
.steps span.on { background: var(--accent); }
.linkish {
  appearance: none;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

/* ---------- quiz ---------- */

.quiz-head { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.6rem; }
.quiz-head .count { font-weight: 700; font-variant-numeric: tabular-nums; }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-bottom: 1.1rem; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width 0.25s; }

.prompt { font-size: 1.2rem; font-weight: 650; margin: 0.5rem 0 1rem; }

.choices { display: grid; gap: 0.6rem; }
.choice {
  appearance: none;
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  text-align: left;
  width: 100%;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 0.9rem;
  cursor: pointer;
  min-height: 56px;
}
.choice:hover:not(:disabled) { border-color: var(--accent); }
.choice:disabled { cursor: default; }
.choice .key {
  flex: none;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 700;
  font-size: 0.85rem;
  display: grid;
  place-items: center;
}
.choice .text { padding-top: 2px; }
.choice.selected { border-color: var(--accent); background: var(--accent-soft); }
.choice.selected .key { background: var(--accent); color: var(--on-accent); }
.choice.correct { border-color: var(--good); background: var(--good-bg); }
.choice.correct .key { background: var(--good); color: #fff; }
.choice.wrong { border-color: var(--bad); background: var(--bad-bg); }
.choice.wrong .key { background: var(--bad); color: #fff; }
.choice.dim { opacity: 0.6; }

.feedback { margin-top: 1rem; border-radius: var(--radius); padding: 1rem; border: 1px solid var(--border); background: var(--surface); }
.feedback .verdict { font-weight: 800; font-size: 1.05rem; margin-bottom: 0.5rem; display: flex; align-items: center; gap: 0.45rem; }
.feedback.good .verdict { color: var(--good); }
.feedback.bad .verdict { color: var(--bad); }
.feedback dl { margin: 0; }
.feedback dt { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 700; margin-top: 0.65rem; }
.feedback dd { margin: 0.15rem 0 0; }
.source { font-size: 0.85rem; color: var(--muted); margin: 0.5rem 0 0; }
.memory-tip {
  margin-top: 0.75rem;
  padding: 0.6rem 0.75rem;
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.95rem;
}
.memory-tip .tip-label {
  display: block;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 0.15rem;
}
.source b { color: var(--text); font-weight: 600; }

/* ---------- results & progress ---------- */

.score-hero { display: flex; align-items: center; gap: 1.1rem; }
.ring { width: 104px; height: 104px; flex: none; }
.ring .track { stroke: var(--surface-2); }
.ring .value { stroke: var(--accent); transition: stroke-dashoffset 0.6s; }
.ring.good .value { stroke: var(--good); }
.ring.bad .value { stroke: var(--bad); }
.ring text { fill: var(--text); font-weight: 800; font-size: 22px; }

.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.6rem; margin-bottom: 1rem; }
@media (min-width: 560px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.8rem; box-shadow: var(--shadow); }
.tile .num { font-size: 1.45rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.tile .lbl { font-size: 0.8rem; color: var(--muted); }

.bars { display: grid; gap: 0.7rem; }
.bar-row { display: grid; gap: 0.25rem; }
.bar-row .top { display: flex; justify-content: space-between; gap: 0.75rem; font-size: 0.92rem; }
.bar-row .top .pct { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > div { height: 100%; border-radius: 4px; background: var(--accent); }
.bar.good > div { background: var(--good); }
.bar.mid > div { background: var(--warn); }
.bar.bad > div { background: var(--bad); }
button.bar-row { appearance: none; border: 0; background: none; font: inherit; color: inherit; text-align: left; padding: 0.3rem; margin: -0.3rem; border-radius: var(--radius-sm); cursor: pointer; }
button.bar-row:hover { background: var(--surface-2); }

.review-item { border-top: 1px solid var(--border); padding: 0.9rem 0; }
.review-item:first-child { border-top: 0; padding-top: 0.2rem; }
.review-item .q { font-weight: 650; margin-bottom: 0.45rem; }
.answer-line { display: flex; gap: 0.5rem; align-items: flex-start; margin: 0.3rem 0; font-size: 0.95rem; }
.answer-line .tag { flex: none; font-size: 0.72rem; font-weight: 800; padding: 0.12rem 0.4rem; border-radius: 5px; margin-top: 2px; }
.answer-line.yours .tag { background: var(--bad-bg); color: var(--bad); }
.answer-line.right .tag { background: var(--good-bg); color: var(--good); }
.why { font-size: 0.9rem; color: var(--muted); margin: 0.1rem 0 0.4rem; }
.explain { font-size: 0.95rem; margin-top: 0.4rem; }

.history { list-style: none; margin: 0; padding: 0; }
.history li + li { border-top: 1px solid var(--border); }
.history a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.2rem;
  color: inherit;
  text-decoration: none;
}
.history a:hover { background: var(--surface-2); }
.history .pct { font-size: 1.15rem; font-weight: 800; width: 3.4rem; text-align: right; font-variant-numeric: tabular-nums; flex: none; }
.history .meta { flex: 1; min-width: 0; }
.history .meta > span { display: block; }
.history .meta .line2 { font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent-soft); }
.chart .dot { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
.chart .median { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 2 4; stroke-linecap: round; }
.chart .trend { stroke-width: 2.5; stroke-dasharray: 8 6; stroke-linecap: round; }
.trend.up { stroke: var(--good); }
.trend.down { stroke: var(--bad); }
.trend.flat { stroke: var(--warn); }

.chart-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1.1rem; margin-top: 0.5rem; font-size: 0.85rem; color: var(--muted); }
.chart-legend span { display: inline-flex; align-items: center; gap: 0.4rem; }
.chart-legend b { color: var(--text); }
.chart-legend i { display: inline-block; width: 22px; height: 0; border-top: 3px solid var(--accent); }
.chart-legend i.key-trend { border-top-style: dashed; }
.chart-legend i.key-trend.up { border-top-color: var(--good); }
.chart-legend i.key-trend.down { border-top-color: var(--bad); }
.chart-legend i.key-trend.flat { border-top-color: var(--warn); }
.chart-legend i.key-median { border-top: 2px dotted var(--muted); }

.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }

/* ---------- dialog & toast ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.2rem;
  width: min(92vw, 420px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
dialog.wide { width: min(94vw, 720px); max-height: 88vh; }
dialog::backdrop { background: rgba(10, 16, 24, 0.55); }
dialog .actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(5.5rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 0.6rem 1rem;
  border-radius: 999px;
  font-size: 0.92rem;
  z-index: 50;
  max-width: calc(100vw - 32px);
}

/* ---------- admin ---------- */

.table-wrap { overflow-x: auto; margin: 0 -1.1rem; padding: 0 1.1rem; }
table { width: 100%; border-collapse: collapse; font-size: 0.93rem; }
th, td { text-align: left; padding: 0.55rem 0.5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.code-display { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 1.6rem; font-weight: 700; letter-spacing: 0.08em; }

@media (max-width: 640px) {
  .user-table thead { display: none; }
  .user-table tr { display: block; border-bottom: 1px solid var(--border); padding: 0.6rem 0; }
  .user-table td { display: flex; justify-content: space-between; border: 0; padding: 0.2rem 0; }
  .user-table td::before { content: attr(data-label); color: var(--muted); font-size: 0.85rem; }
  .user-table td.actions-cell { justify-content: flex-start; flex-wrap: wrap; gap: 0.4rem; padding-top: 0.5rem; }
  .user-table td.actions-cell::before { content: none; }
  .brand-sub { display: none; }
}

@media (max-width: 600px) {
  .topbar .brand-text { display: none; }
}

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