:root {
  --bg: #f4f6f9; --card: #fff; --ink: #1d2433; --muted: #667085; --line: #e2e6ec;
  --brand: #1f5fbf; --brand-soft: #e7effb; --ok: #16834a; --ok-soft: #e3f5ea; --bad: #c43a31; --bad-soft: #fbe7e5;
  --warn: #b7791f; --radius: 12px; --top: 52px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151b; --card: #1b2029; --ink: #e6e9ef; --muted: #9aa3b2; --line: #2c3340;
    --brand: #6ea2ff; --brand-soft: #1e2a40; --ok: #4cc583; --ok-soft: #173023; --bad: #ff7b72; --bad-soft: #3a1d1b;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }
body.noscroll { overflow: hidden; }
a { color: var(--brand); }
button { font: inherit; cursor: pointer; color: inherit; }
img { max-width: 100%; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 30; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  height: var(--top); padding: 0 16px; background: var(--card); border-bottom: 1px solid var(--line); }
.brand { font-weight: 800; text-decoration: none; color: var(--brand); font-size: 18px; white-space: nowrap; }
.brand span { color: var(--ink); font-weight: 600; }
#bar-right { display: flex; align-items: center; gap: 10px; }
.timer { font-variant-numeric: tabular-nums; font-weight: 700; padding: 4px 10px; border-radius: 8px; background: var(--brand-soft); color: var(--brand); white-space: nowrap; }
.timer.low { background: var(--bad-soft); color: var(--bad); }

main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 80px; }
h1 { font-size: 24px; line-height: 1.3; margin: 4px 0 12px; }
h2 { font-size: 19px; margin: 0 0 6px; }
h2.sec { margin: 28px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.note { font-size: 13px; color: var(--muted); margin-top: 18px; }

/* ---------- buttons / chips ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px; text-decoration: none;
  border: 1px solid var(--line); background: var(--card); color: var(--ink); padding: 8px 14px; border-radius: 10px; }
.btn:hover { border-color: var(--brand); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.big-btn { min-height: 48px; font-size: 16px; font-weight: 700; margin-top: 8px; }
.link { border: 0; background: none; color: var(--brand); padding: 4px 6px; text-decoration: underline; }
.modes { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.modes.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px 16px; padding: 0 16px; }
.modes.scroll::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); padding: 7px 13px; border-radius: 999px; min-height: 36px; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.chip.on .muted { color: rgba(255, 255, 255, .8); }
.chip[disabled] { opacity: .45; cursor: not-allowed; }

/* ---------- cards / home ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.test-card h3 { margin: 0 0 4px; font-size: 17px; }
.test-card .row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.test-card .row.parts .btn { flex: 1 0 36px; padding: 4px 0; }
.best { font-size: 13px; color: var(--ok); font-weight: 600; }
.hero { display: flex; align-items: center; justify-content: space-between; gap: 16px; text-decoration: none; color: inherit;
  border-color: var(--brand); background: linear-gradient(135deg, var(--brand-soft), var(--card)); }
.hero h3 { margin: 0 0 4px; font-size: 18px; color: var(--brand); }
.hist { padding: 0; overflow: hidden; }
.hrow { display: flex; justify-content: space-between; gap: 12px; padding: 10px 16px; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.hrow:first-child { border-top: 0; }
.hrow:hover { background: var(--bg); }
.hscore { text-align: right; font-weight: 700; white-space: nowrap; }

/* ---------- exam layout ---------- */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 260px; gap: 16px; align-items: start; }
.palette { position: sticky; top: calc(var(--top) + 12px); max-height: calc(100vh - var(--top) - 24px); display: flex; flex-direction: column; gap: 10px; }
.pal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.pal-body { overflow: auto; min-height: 0; }
.palette h4 { margin: 10px 0 6px; font-size: 13px; color: var(--muted); }
.palette .nums { display: grid; grid-template-columns: repeat(auto-fill, minmax(34px, 1fr)); gap: 4px; }
.palette .n { font-size: 12px; padding: 5px 0; text-align: center; border: 1px solid var(--line); border-radius: 6px; background: var(--card); }
.palette .n.done { background: var(--brand-soft); border-color: var(--brand); color: var(--brand); }
.palette .n.right { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.palette .n.wrong { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.palette .n.flag { outline: 2px solid var(--warn); }
.backdrop, .mbar, .only-mobile { display: none; }
.part-head { margin: 22px 0 10px; }
.part-head:first-child { margin-top: 0; }
.part-head .dir { font-size: 13px; color: var(--muted); }
.group { margin-bottom: 14px; }
.group audio { width: 100%; margin: 0 0 10px; display: block; }
.group img { width: 100%; max-width: 560px; display: block; border-radius: 10px; margin: 0 0 10px; }
.passage { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin: 0 0 10px; font-size: 14.5px; }
.passage .ptype { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }
.blank { color: var(--brand); }
.gap { white-space: nowrap; color: var(--brand); font-weight: 700; letter-spacing: 1px; }
.graphic { margin: 0 0 10px; overflow-x: auto; }
.graphic table { border-collapse: collapse; font-size: 14px; }
.graphic td { border: 1px solid var(--line); padding: 4px 10px; }
.graphic tr:first-child td { font-weight: 700; background: var(--bg); }
.q { padding: 10px 0; border-top: 1px dashed var(--line); }
.q:first-of-type { border-top: 0; }
.q .stem { margin: 0 0 6px; overflow: hidden; }
.q .num { font-weight: 800; margin-right: 6px; }
.flagbtn { float: right; border: 0; background: none; color: var(--muted); font-size: 13px; padding: 2px 0 2px 8px; }
.flagbtn.on { color: var(--warn); font-weight: 700; }
.opt { display: flex; gap: 8px; align-items: flex-start; padding: 9px 12px; margin: 5px 0; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; background: var(--card); min-height: 44px; }
.opt:hover { border-color: var(--brand); }
.opt input { margin-top: 4px; flex: none; width: 16px; height: 16px; }
.opt.sel { border-color: var(--brand); background: var(--brand-soft); }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt .l { font-weight: 700; min-width: 22px; flex: none; }
.opt[aria-disabled], input:disabled + .l { cursor: default; }
details.script { margin-top: 8px; font-size: 14px; }
details.script summary { cursor: pointer; color: var(--brand); padding: 4px 0; }
details.script p { margin: 4px 0; }
details.explain { margin-top: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 0 12px; font-size: 14px; }
details.explain summary { cursor: pointer; font-weight: 700; padding: 8px 0; color: var(--warn); }
details.explain[open] { padding-bottom: 8px; }
.ex-row { margin: 6px 0 2px; }
.ex-row p { margin: 3px 0 0; line-height: 1.55; }
.ex-tag { display: inline-block; font-size: 11.5px; font-weight: 800; text-transform: uppercase; letter-spacing: .03em; padding: 1px 8px; border-radius: 999px; }
.ex-tag.t1 { background: var(--brand-soft); color: var(--brand); }
.ex-tag.t2 { background: var(--ok-soft); color: var(--ok); }
.ex-tag.t3 { background: #fdf1dc; color: #9a5b00; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .ex-tag.t3 { background: #3a2c12; color: #f5c46b; } }
.submitbar { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ---------- results ---------- */
.score-hero { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.score-hero .big { font-size: 32px; font-weight: 800; line-height: 1.2; }
.bars .bar { display: grid; grid-template-columns: 170px 1fr 56px; gap: 8px; align-items: center; margin: 6px 0; font-size: 14px; }
.track { height: 10px; background: var(--bg); border-radius: 5px; overflow: hidden; border: 1px solid var(--line); }
.fill { height: 100%; background: var(--brand); transition: width .3s; }

/* ---------- practice ---------- */
.form .field { margin-bottom: 16px; }
.form label { display: block; font-weight: 700; margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips.quick { margin-top: 4px; gap: 2px; }
.stats { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 4px; text-align: center; display: flex; flex-direction: column; }
.stat b { font-size: 18px; }
.prog { position: sticky; top: var(--top); z-index: 5; background: var(--bg); padding: 8px 0 10px; margin-top: -8px; }
.prog-top { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.meta { margin: 4px 0 8px; }
.hint { margin-bottom: 8px; }
#pgroup .group { margin-bottom: 10px; }
.verdict { font-weight: 800; font-size: 17px; padding: 10px 14px; border-radius: 10px; margin-bottom: 10px; }
.verdict.good { background: var(--ok-soft); color: var(--ok); }
.verdict.bad { background: var(--bad-soft); color: var(--bad); }
.pactions { display: flex; gap: 8px; justify-content: space-between; }
.pactions .btn.primary { flex: 1; max-width: 320px; min-height: 46px; font-weight: 700; }

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  :root { --top: 48px; }
  main { padding: 14px 12px 96px; }
  h1 { font-size: 20px; }
  .brand span { display: none; }
  .modes.scroll { margin: 0 -12px 12px; padding: 0 12px; }
  .layout { display: block; }
  .only-desktop { display: none; }
  .only-mobile { display: inline-flex; }
  .card { padding: 12px; }
  .group { border-radius: 10px; }
  .opt { padding: 11px 12px; font-size: 15px; }
  .passage { font-size: 14px; padding: 10px 12px; }
  /* question list becomes a bottom sheet */
  .palette { position: fixed; left: 0; right: 0; bottom: 0; top: auto; z-index: 50; max-height: 75vh; border-radius: 16px 16px 0 0;
    padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); transform: translateY(105%); transition: transform .25s ease; box-shadow: 0 -8px 30px rgba(0, 0, 0, .2); }
  .palette.open { transform: none; }
  .palette .nums { grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
  .palette .n { padding: 9px 0; font-size: 13px; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 45; background: rgba(0, 0, 0, .35); opacity: 0; pointer-events: none; transition: opacity .25s; }
  .backdrop.open { opacity: 1; pointer-events: auto; }
  .mbar { display: flex; gap: 8px; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--card); border-top: 1px solid var(--line);
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
  .mbar .btn { flex: 1; min-height: 44px; }
  .score-hero { grid-template-columns: 1fr 1fr; gap: 8px; }
  .score-hero .big { font-size: 26px; }
  .bars .bar { grid-template-columns: 1fr 52px; gap: 2px 8px; }
  .bars .bar > span:first-child { grid-column: 1 / -1; font-size: 13px; }
  .hero { flex-direction: column; align-items: stretch; }
  .stats { grid-template-columns: repeat(4, 1fr); }
  .pactions { position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--card); border-top: 1px solid var(--line);
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); }
  .pactions .btn { min-height: 46px; }
  .pactions .btn.primary { max-width: none; }
}
@media (max-width: 380px) {
  .grid { grid-template-columns: 1fr; }
  .score-hero .big { font-size: 22px; }
}
