:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f0eee8;
  --text: #1d1f23;
  --muted: #5d636e;
  --border: #e2dfd6;
  --accent: #2f5bd3;
  --accent-soft: #e4ebfb;
  --accent-text: #ffffff;
  --good: #1f7a4d;
  --good-soft: #e1f2e8;
  --bad: #b3362d;
  --bad-soft: #f8e3e1;
  --band: rgba(47, 91, 211, 0.18);
  --track: #ebe8e0;
  --shadow: 0 1px 2px rgba(20, 20, 30, .06), 0 4px 16px rgba(20, 20, 30, .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 {
    --bg: #121418;
    --surface: #1b1e24;
    --surface-2: #23272f;
    --text: #e9eaee;
    --muted: #9aa1ad;
    --border: #2e333c;
    --accent: #7c9cf5;
    --accent-soft: #252f4a;
    --accent-text: #0e1220;
    --good: #5cc28e;
    --good-soft: #1b3127;
    --bad: #ef7d73;
    --bad-soft: #3a2220;
    --band: rgba(124, 156, 245, 0.25);
    --track: #2a2f38;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1 { font-size: clamp(1.5rem, 3vw, 2rem); line-height: 1.2; margin: 0 0 .75rem; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
h3 { font-size: 1rem; margin: 0 0 .75rem; }
h2.sub { margin-top: 2rem; }
kbd {
  font: 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px;
  padding: 2px 5px; background: var(--surface-2); color: var(--muted);
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .75rem clamp(16px, 3vw, 32px);
  border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 650; color: var(--accent); }
.brand span { color: var(--text); }
.topnav { display: flex; gap: .25rem; flex-wrap: nowrap; }
@media (max-width: 560px) {
  .brand span { display: none; }
  .topnav .btn { padding: .4rem .55rem; font-size: .9rem; }
}

/* ---------- layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 24px;
  max-width: 1240px; margin: 0 auto; padding: 24px clamp(16px, 3vw, 32px) 64px;
}
.layout:has(.sidebar[hidden]) { grid-template-columns: minmax(0, 1fr); max-width: 860px; }
@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}
.main { min-width: 0; }
.lead { font-size: 1.075rem; color: var(--muted); max-width: 62ch; }
.hint { color: var(--muted); font-size: .9rem; }
.how { padding-left: 1.2rem; max-width: 62ch; }
.how li { margin: .25rem 0; }
.actions { display: flex; gap: .75rem; flex-wrap: wrap; margin: 1.25rem 0; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 550; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .6rem 1rem; border-radius: 10px; transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .option:focus-visible, select:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }
.btn.small { padding: .35rem .7rem; font-size: .9rem; }
.btn[disabled] { opacity: .5; cursor: default; }

/* ---------- cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: clamp(16px, 3vw, 28px); margin-bottom: 20px;
}

.field-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.field-tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.field-tile b { display: block; margin-bottom: 2px; }
.field-tile small { color: var(--muted); }

/* ---------- quiz ---------- */
.quiz-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.pill {
  display: inline-block; font-size: .8rem; font-weight: 650; letter-spacing: .02em; text-transform: uppercase;
  background: var(--accent-soft); color: var(--accent); padding: .2rem .6rem; border-radius: 999px; margin-right: .5rem;
}
.phase-desc { color: var(--muted); font-size: .92rem; }
.focus { display: flex; align-items: center; gap: .5rem; color: var(--muted); font-size: .9rem; }
.focus select {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: .35rem .5rem; max-width: 220px;
}

.progress { margin: .5rem 0 1rem; }
.progress-track { position: relative; height: 10px; background: var(--track); border-radius: 999px; overflow: visible; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width .4s ease; }
.progress-target { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--text); opacity: .55; border-radius: 1px; }
.progress-label { font-size: .88rem; color: var(--muted); margin-top: .4rem; }

.notice {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  background: var(--good-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--good) 35%, transparent);
  border-radius: 12px; padding: .75rem 1rem; margin-bottom: 1rem; font-size: .95rem;
}

.q-meta { display: flex; align-items: center; gap: .4rem; color: var(--muted); font-size: .9rem; flex-wrap: wrap; }
.q-meta .sep { opacity: .6; }
.difficulty { margin-left: auto; letter-spacing: 2px; color: var(--accent); font-size: .85rem; white-space: nowrap; }
.difficulty .off { color: var(--track); }
.q-text { font-size: clamp(1.15rem, 2.4vw, 1.4rem); line-height: 1.35; margin: .75rem 0 1.25rem; font-weight: 600; }
.options { display: grid; gap: 10px; }
.option {
  display: flex; align-items: center; gap: .75rem; text-align: left; width: 100%;
  font: inherit; color: var(--text); background: var(--surface); cursor: pointer;
  border: 1.5px solid var(--border); border-radius: 12px; padding: .8rem 1rem; transition: border-color .15s, background .15s;
}
.option:hover:not([disabled]) { border-color: var(--accent); background: var(--accent-soft); }
.option .key {
  flex: none; width: 28px; height: 28px; display: grid; place-items: center; border-radius: 8px;
  background: var(--surface-2); color: var(--muted); font-weight: 650; font-size: .9rem;
}
.option[disabled] { cursor: default; }
.option.correct { border-color: var(--good); background: var(--good-soft); }
.option.correct .key { background: var(--good); color: var(--surface); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.option.wrong .key { background: var(--bad); color: var(--surface); }
.option.dim { opacity: .55; }
.q-footer { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin-top: 1rem; }
.kbd-hint { color: var(--muted); font-size: .82rem; }
@media (hover: none) { .kbd-hint, .btn kbd { display: none; } }

.feedback { margin-top: 1.25rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.feedback-title { font-weight: 700; margin-bottom: .25rem; }
.feedback-title.good { color: var(--good); }
.feedback-title.bad { color: var(--bad); }
.feedback p { margin: 0 0 1rem; color: var(--text); }

/* ---------- sidebar ---------- */
.sidebar {
  position: sticky; top: 76px; align-self: start; max-height: calc(100vh - 96px); overflow: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px;
}
@media (max-width: 960px) { .sidebar { position: static; max-height: none; } }
.side-tiles, .tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 1rem; }
.tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 20px; }
.tile { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.tiles .tile { background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); padding: 14px 16px; }
.tile .label { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.tile .value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.25; }
.tile .note { font-size: .8rem; color: var(--muted); }

.legend { display: flex; gap: 1rem; font-size: .8rem; color: var(--muted); margin-bottom: .5rem; }
.legend i { display: inline-block; vertical-align: -1px; margin-right: 5px; }
.lg-band { width: 16px; height: 10px; background: var(--band); border-radius: 3px; }
.lg-dot { width: 10px; height: 10px; background: var(--accent); border-radius: 50%; }

.field-row { padding: .6rem 0; border-top: 1px solid var(--border); }
.field-row:first-child { border-top: 0; }
.field-row > button {
  all: unset; display: block; width: 100%; cursor: pointer; box-sizing: border-box;
}
.field-row > button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.field-row.active .fr-name::before { content: "● "; color: var(--accent); }
.fr-top { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.fr-name { font-weight: 600; font-size: .95rem; }
.fr-level { font-variant-numeric: tabular-nums; font-size: .88rem; color: var(--muted); white-space: nowrap; }
.fr-level b { color: var(--text); }
.fr-sub { margin: .35rem 0 0 .75rem; }
.fr-sub .fr-name { font-weight: 500; font-size: .88rem; }
.fr-sub .field-row { padding: .4rem 0; }

/* level scale bar: 1..5 */
.scale { position: relative; height: 14px; margin: .35rem 0 .2rem; }
.scale-track { position: absolute; inset: 5px 0; background: var(--track); border-radius: 999px; }
.scale-ticks { position: absolute; inset: 0; pointer-events: none; }
.scale-ticks i { position: absolute; top: 2px; bottom: 2px; width: 1px; background: var(--border); }
.scale-band { position: absolute; top: 3px; bottom: 3px; background: var(--band); border-radius: 999px; transition: left .4s, width .4s; }
.scale-dot { position: absolute; top: 2px; width: 10px; height: 10px; margin-left: -5px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 2px var(--surface); transition: left .4s; }
.scale.unmeasured .scale-dot { background: var(--muted); opacity: .5; }
.fr-certainty { display: flex; align-items: center; gap: .5rem; font-size: .78rem; color: var(--muted); }
.meter { flex: 1; height: 4px; background: var(--track); border-radius: 999px; overflow: hidden; max-width: 120px; }
.meter > i { display: block; height: 100%; background: var(--good); border-radius: 999px; transition: width .4s; }

/* ---------- results ---------- */
.results-grid { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 20px; }
@media (max-width: 760px) { .results-grid { grid-template-columns: minmax(0, 1fr); } }
.results-grid .card { margin-bottom: 0; }
.results-grid + .card { margin-top: 20px; }
#radar svg { width: 100%; height: auto; display: block; max-width: 460px; margin: 0 auto; }
#radar text { fill: var(--muted); font: 13px var(--font); }
#radar .axis { stroke: var(--border); }
#radar .ring { fill: none; stroke: var(--border); }
#radar .band { fill: var(--band); stroke: none; fill-rule: evenodd; }
#radar .est { fill: color-mix(in srgb, var(--accent) 12%, transparent); stroke: var(--accent); stroke-width: 2; }
#radar .pt { fill: var(--accent); }

.highlights { list-style: none; padding: 0; margin: 0 0 1.25rem; }
.highlights li { display: flex; justify-content: space-between; gap: .75rem; padding: .5rem 0; border-top: 1px solid var(--border); font-size: .95rem; }
.highlights li:first-child { border-top: 0; }
.highlights small { color: var(--muted); display: block; }
.highlights .lvl { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.highlights .empty { color: var(--muted); font-size: .9rem; }

.field-table .field-row > button { cursor: pointer; }
.field-table .scale { max-width: 100%; }

.history summary { cursor: pointer; font-weight: 650; }
.history ol { padding-left: 1.25rem; margin: 1rem 0 0; }
.history li { margin-bottom: .9rem; }
.history .h-q { font-weight: 550; }
.history .h-a { font-size: .9rem; color: var(--muted); }
.history .ok { color: var(--good); font-weight: 650; }
.history .no { color: var(--bad); font-weight: 650; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); padding: .7rem 1rem; border-radius: 10px; box-shadow: var(--shadow); z-index: 20;
}
