/* IM Past Papers — mobile-first, light/dark via tokens */
:root {
  --bg: #f5f6f4;
  --surface: #ffffff;
  --surface-2: #eef1ee;
  --text: #1a1f1e;
  --muted: #5a6562;
  --line: #dde2df;
  --accent: #0e6b6b;
  --accent-ink: #ffffff;
  --accent-weak: #e1efed;
  --good: #1d7a3e;
  --good-bg: #e5f3e9;
  --bad: #b3261e;
  --bad-bg: #fbe9e7;
  --warn: #8a5a00;
  --warn-bg: #fdf1d6;
  --high: #b3261e;
  --high-bg: #fbe9e7;
  --med: #8a5a00;
  --med-bg: #fdf1d6;
  --low: #5a6562;
  --low-bg: #eef1ee;
  --shadow: 0 1px 2px rgba(20, 30, 28, .06), 0 4px 16px rgba(20, 30, 28, .05);
  --radius: 14px;
  --font: "IBM Plex Sans Thai", "Sarabun", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1417; --surface: #171e21; --surface-2: #1f272b; --text: #e7ecea; --muted: #9aa7a3; --line: #2b3539;
    --accent: #4fb3ad; --accent-ink: #06201f; --accent-weak: #16312f;
    --good: #6fcf8f; --good-bg: #15301f; --bad: #ff8a80; --bad-bg: #3a1a18; --warn: #f0c060; --warn-bg: #352a12;
    --high: #ff8a80; --high-bg: #3a1a18; --med: #f0c060; --med-bg: #352a12; --low: #9aa7a3; --low-bg: #1f272b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1417; --surface: #171e21; --surface-2: #1f272b; --text: #e7ecea; --muted: #9aa7a3; --line: #2b3539;
  --accent: #4fb3ad; --accent-ink: #06201f; --accent-weak: #16312f;
  --good: #6fcf8f; --good-bg: #15301f; --bad: #ff8a80; --bad-bg: #3a1a18; --warn: #f0c060; --warn-bg: #352a12;
  --high: #ff8a80; --high-bg: #3a1a18; --med: #f0c060; --med-bg: #352a12; --low: #9aa7a3; --low-bg: #1f272b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3); 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.6; min-height: 100vh;
  padding-bottom: calc(64px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.3; margin: 0 0 .5rem; font-weight: 600; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 50; background: var(--surface); padding: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---- top bar / nav */
.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: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 600; }
.brand img { border-radius: 7px; }
.topnav { display: none; gap: 4px; margin-left: auto; }
.topnav a { text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: 999px; font-weight: 500; }
.topnav a.active { color: var(--accent); background: var(--accent-weak); }
.sync-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--good); margin-left: auto; }
.sync-dot.off { background: var(--muted); }
.sync-dot.err { background: var(--bad); }
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20; display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 500;
}
.tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a.active { color: var(--accent); }
@media (min-width: 860px) {
  body { padding-bottom: 0; }
  .tabbar { display: none; }
  .topnav { display: flex; }
  .sync-dot { margin-left: 0; }
}

/* ---- layout */
main { max-width: 860px; margin: 0 auto; padding: 16px; outline: none; }
.loading { padding: 48px 0; text-align: center; color: var(--muted); }
.stack > * + * { margin-top: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spread { display: flex; gap: 8px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.grid2 { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid4 { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 640px) { .grid4 { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card.flat { box-shadow: none; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.tiny { font-size: .75rem; }
.hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }
.stat { display: flex; flex-direction: column; }
.stat b { font-size: 1.5rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .8rem; }
.pagehead { display: flex; align-items: end; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 4px; }

/* ---- buttons, chips, inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 8px 16px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 500; text-decoration: none; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn.icon { min-width: 44px; padding: 8px; }
.quick { display: grid; gap: 10px; grid-template-columns: 1fr; }
@media (min-width: 560px) { .quick { grid-template-columns: repeat(2, 1fr); } }
.quick .btn { justify-content: flex-start; text-align: left; flex-direction: column; align-items: flex-start; padding: 12px 16px; }
.quick .btn small { color: var(--muted); font-weight: 400; }
.quick .btn.primary small { color: inherit; opacity: .85; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; min-height: 36px; padding: 4px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: .9rem; user-select: none;
}
.chip[aria-pressed="true"] { background: var(--accent-weak); border-color: var(--accent); color: var(--accent); font-weight: 500; }
.chip .n { color: var(--muted); font-size: .75rem; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: .75rem; font-weight: 500; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.High { background: var(--high-bg); color: var(--high); }
.badge.Medium { background: var(--med-bg); color: var(--med); }
.badge.Low { background: var(--low-bg); color: var(--low); }
.badge.core { background: var(--accent-weak); color: var(--accent); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-weight: 500; font-size: .9rem; }
input[type="search"], input[type="text"], input[type="email"], input[type="password"], input[type="number"], select, textarea {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
}
textarea { min-height: 88px; resize: vertical; }
.toggle { display: flex; align-items: center; gap: 10px; min-height: 40px; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
details.filter { border-top: 1px solid var(--line); padding: 10px 0; }
details.filter summary { cursor: pointer; font-weight: 500; list-style: none; display: flex; justify-content: space-between; align-items: center; min-height: 36px; }
details.filter summary::-webkit-details-marker { display: none; }
details.filter summary::after { content: "▾"; color: var(--muted); }
details.filter[open] summary::after { content: "▴"; }
details.filter .sys { margin: 10px 0 4px; font-size: .8rem; color: var(--muted); font-weight: 500; }
.sticky-foot {
  position: sticky; bottom: calc(64px + env(safe-area-inset-bottom)); z-index: 5; margin-top: 16px; padding: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
@media (min-width: 860px) { .sticky-foot { bottom: 12px; } }

/* ---- question */
.qhead { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.qmeta { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.progressbar { height: 4px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-bottom: 12px; }
.progressbar > i { display: block; height: 100%; background: var(--accent); }
.stem { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 1.02rem; }
.stem img, .qimgs img { display: block; max-height: 360px; width: auto; margin: 10px 0; border-radius: 8px; border: 1px solid var(--line); cursor: zoom-in; background: #fff; }
.options { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.opt {
  display: grid; grid-template-columns: 32px 1fr auto; gap: 10px; align-items: start; text-align: left; width: 100%;
  padding: 10px 12px; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.opt .l { display: inline-flex; width: 28px; height: 28px; border-radius: 50%; align-items: center; justify-content: center; background: var(--surface-2); font-weight: 600; font-size: .85rem; }
.opt .t { overflow-wrap: anywhere; padding-top: 2px; }
.opt .mark { font-weight: 600; padding-top: 2px; }
.opt .why { display: block; margin-top: 4px; font-size: .85rem; line-height: 1.5; color: var(--muted); }
.opt .why.ok { color: var(--good); }
.opt .why.no { color: var(--bad); }
.opt .why b { font-weight: 600; }
.expl h4 { margin: 12px 0 2px; font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.expl p { margin: 0 0 4px; }
.expl ul { margin: 0; padding-left: 20px; font-size: .9rem; }
.opt:hover:not(:disabled) { border-color: var(--accent); }
.opt.chosen { border-color: var(--accent); background: var(--accent-weak); }
.opt.chosen .l { background: var(--accent); color: var(--accent-ink); }
.opt.correct { border-color: var(--good); background: var(--good-bg); }
.opt.correct .l { background: var(--good); color: #fff; }
.opt.wrong { border-color: var(--bad); background: var(--bad-bg); }
.opt.wrong .l { background: var(--bad); color: #fff; }
.opt:disabled { cursor: default; color: inherit; }
.opt.guide { border-color: var(--good); border-style: dashed; }
.answerbox { margin-top: 14px; border-radius: 12px; padding: 12px 14px; border: 1px solid var(--line); background: var(--surface-2); }
.answerbox.good { border-color: var(--good); }
.answerbox.bad { border-color: var(--bad); }
.answerbox h3 { margin-bottom: 4px; }
.note-flag { margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--text); font-size: .9rem; }
.note-flag b { color: var(--warn); }
.qnav { display: flex; gap: 8px; margin-top: 16px; }
.qnav .btn { flex: 1; }
.iconbtn { background: none; border: 0; cursor: pointer; padding: 8px; border-radius: 10px; color: var(--muted); min-width: 44px; min-height: 44px; }
.iconbtn[aria-pressed="true"] { color: var(--warn); }
.iconbtn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.iconbtn[aria-pressed="true"] svg { fill: currentColor; }

/* ---- exam */
.timer { font-variant-numeric: tabular-nums; font-weight: 600; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); }
.timer.low { background: var(--bad-bg); color: var(--bad); }
.qgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 6px; }
.qgrid button { min-height: 40px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-variant-numeric: tabular-nums; font-size: .85rem; }
.qgrid button.done { background: var(--accent-weak); border-color: var(--accent); }
.qgrid button.flag { box-shadow: inset 0 -3px 0 var(--warn); }
.qgrid button.cur { outline: 2px solid var(--accent); }
.qgrid button.ok { background: var(--good-bg); border-color: var(--good); }
.qgrid button.no { background: var(--bad-bg); border-color: var(--bad); }

/* ---- lists */
.list { display: flex; flex-direction: column; }
.list > .item { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.list > .item:first-child { border-top: 0; }
.item .grow { flex: 1; min-width: 0; }
.item .title { font-weight: 500; }
.bar { height: 6px; border-radius: 6px; background: var(--surface-2); overflow: hidden; width: 100%; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bar.good > i { background: var(--good); }
.bar.bad > i { background: var(--bad); }
.bar.warnc > i { background: var(--warn); }
.acc { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 3.2em; text-align: right; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
table.simple { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.simple th, table.simple td { text-align: left; padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: top; }
table.simple th { color: var(--muted); font-weight: 500; border-top: 0; }
table.simple td.num, table.simple th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---- OSCE */
.station .task { font-weight: 500; white-space: pre-wrap; overflow-wrap: anywhere; }
.station .notes { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 8px; padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: .95rem; }
.station details summary { cursor: pointer; color: var(--accent); font-weight: 500; min-height: 36px; display: flex; align-items: center; }

/* ---- misc */
.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 60; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
dialog#lightbox { border: 0; padding: 12px; border-radius: 12px; max-width: 96vw; max-height: 96vh; background: var(--surface); }
dialog#lightbox::backdrop { background: rgba(0, 0, 0, .7); }
dialog#lightbox img { display: block; max-width: 92vw; max-height: 82vh; margin: 0 auto 8px; background: #fff; }
dialog#lightbox .btn { width: 100%; }
.empty { text-align: center; color: var(--muted); padding: 32px 8px; }
.kbd { font-size: .75rem; padding: 0 6px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); }
.hide-sm { display: none; }
@media (min-width: 860px) { .hide-sm { display: inline; } }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
