:root {
  --ink: #1a2332;
  --ink-soft: #4a5568;
  --line: #e2e8f0;
  --bg: #f5f7fa;
  --card: #ffffff;
  --accent: #1f5eff;
  --accent-soft: #e8efff;
  --ok: #0e9f6e;
  --ok-soft: #def7ec;
  --warn: #c27803;
  --warn-soft: #fdf6b2;
  --bad: #e02424;
  --bad-soft: #fde8e8;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg); color: var(--ink); line-height: 1.55; font-size: 15.5px;
}

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  background: #101a33; color: #fff; padding: 10px 22px; position: sticky; top: 0; z-index: 50;
  box-shadow: var(--shadow);
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 9px; white-space: nowrap; }
.brand-mark {
  background: var(--accent); border-radius: 7px; padding: 3px 8px; font-size: 13px; letter-spacing: .5px;
}
.nav { display: flex; gap: 4px; flex-wrap: wrap; }
.nav a {
  color: #c9d4ec; text-decoration: none; padding: 6px 11px; border-radius: 7px; font-size: 14px; font-weight: 500;
}
.nav a:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.nav a.active { background: var(--accent); color: #fff; }
.searchbox { margin-left: auto; }
.searchbox input {
  width: 250px; max-width: 44vw; padding: 7px 12px; border-radius: 8px; border: none;
  background: rgba(255, 255, 255, .14); color: #fff; font-size: 14px; outline: none;
}
.searchbox input::placeholder { color: #a9b7d6; }
.container { max-width: 1080px; margin: 26px auto 60px; padding: 0 22px; }
.footer { text-align: center; color: var(--ink-soft); font-size: 12.5px; padding: 18px 0 30px; }

h1 { font-size: 26px; margin: 6px 0 4px; }
h2 { font-size: 20px; margin: 26px 0 10px; }
h3 { font-size: 16.5px; margin: 18px 0 8px; }
p.sub { color: var(--ink-soft); margin: 0 0 18px; }

/* ---------- cards & grids ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.grid.cols2 { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.grid.cols3 { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.stat-tile { text-align: left; }
.stat-tile .num { font-size: 28px; font-weight: 700; }
.stat-tile .lbl { color: var(--ink-soft); font-size: 13px; }

.group-title { margin: 22px 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: .8px; color: var(--ink-soft); font-weight: 700; }
.chapter-card { display: block; text-decoration: none; color: inherit; transition: transform .06s; }
.chapter-card:hover { transform: translateY(-2px); }
.chapter-card .ch-num { color: var(--accent); font-weight: 700; font-size: 13px; }
.chapter-card .ch-title { font-weight: 600; margin: 2px 0; }
.chapter-card .ch-idea { color: var(--ink-soft); font-size: 13.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.read-badge { float: right; font-size: 11.5px; background: var(--ok-soft); color: var(--ok); padding: 2px 8px; border-radius: 20px; font-weight: 600; }

/* ---------- buttons ---------- */
.btn {
  display: inline-block; border: 1px solid transparent; border-radius: 8px; cursor: pointer;
  padding: 9px 16px; font-size: 14.5px; font-weight: 600; background: var(--accent); color: #fff;
  text-decoration: none; transition: filter .1s;
}
.btn:hover { filter: brightness(1.08); }
.btn.secondary { background: #fff; color: var(--ink); border-color: var(--line); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger { background: var(--bad); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 14px; }

/* ---------- forms ---------- */
select, input[type="text"], input[type="number"] {
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; background: #fff; color: var(--ink);
}
label.inline { display: inline-flex; align-items: center; gap: 7px; margin-right: 14px; font-size: 14px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); margin: 10px 0 16px; }
legend { font-weight: 600; font-size: 13.5px; padding: 0 6px; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 18px; padding: 8px 12px; }
.checks label { display: inline-flex; gap: 6px; align-items: center; font-size: 14px; cursor: pointer; }

/* ---------- chapter reader & markdown ---------- */
.reader-nav { display: flex; justify-content: space-between; margin: 18px 0 8px; gap: 10px; }
.reader-nav a { color: var(--accent); text-decoration: none; font-weight: 600; font-size: 14px; }
.md h1 { font-size: 24px; }
.md h2 { font-size: 18px; border-bottom: 1px solid var(--line); padding-bottom: 6px; margin-top: 30px; }
.md h3 { font-size: 15.5px; color: var(--ink-soft); }
.md table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13.5px; }
.md th, .md td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--accent-soft); }
.md tr:nth-child(even) td { background: #fafbfd; }
.md code { background: #eef1f6; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.md a.ch-link { color: var(--accent); text-decoration: none; font-weight: 600; }
.quickref { background: var(--accent-soft); border: 1px solid #c8d9ff; border-radius: var(--radius); padding: 14px 18px; margin-bottom: 18px; }
.quickref .qi { font-style: italic; }
.card.md { overflow-x: auto; }

/* ---------- flashcards ---------- */
.flashcard {
  perspective: 1200px; height: 300px; margin: 18px auto; max-width: 640px; cursor: pointer;
}
.flashcard .inner {
  position: relative; width: 100%; height: 100%; transition: transform .45s;
  transform-style: preserve-3d;
}
.flashcard.flipped .inner { transform: rotateY(180deg); }
.flashcard .face {
  position: absolute; inset: 0; backface-visibility: hidden; border-radius: 14px;
  display: flex; align-items: center; justify-content: center; padding: 30px; text-align: center;
  background: var(--card); border: 1.5px solid var(--line); box-shadow: var(--shadow);
  font-size: 18px; overflow: auto;
}
.flashcard .face.back { transform: rotateY(180deg); background: #fbfdff; font-size: 16px; text-align: left; }
.flashcard .face .src { position: absolute; top: 10px; left: 14px; font-size: 11.5px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .6px; }
.flashcard .face .hint { position: absolute; bottom: 10px; right: 14px; font-size: 11.5px; color: #9aa7bd; }
.grade-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.grade-row .btn { min-width: 120px; }
.grade-again { background: var(--bad); } .grade-hard { background: var(--warn); }
.grade-good { background: var(--ok); } .grade-easy { background: var(--accent); }

/* ---------- quiz & exam ---------- */
.q-item .stem { font-size: 16px; font-weight: 500; margin: 4px 0 14px; white-space: pre-wrap; }
.q-meta { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 4px; display: flex; gap: 8px; flex-wrap: wrap; }
.tag { background: #eef1f6; border-radius: 5px; padding: 1px 7px; font-weight: 600; }
.tag.sji { background: #f3e8ff; color: #6b21a8; }
.tag.ki_hr { background: var(--accent-soft); color: var(--accent); }
.tag.ki_foundational { background: #e0f2fe; color: #075985; }
.opts { display: grid; gap: 9px; }
.opt {
  text-align: left; border: 1.5px solid var(--line); background: #fff; border-radius: 9px;
  padding: 11px 14px; font-size: 14.5px; cursor: pointer; display: flex; gap: 10px; align-items: flex-start;
}
.opt:hover { border-color: var(--accent); }
.opt .letter { font-weight: 700; color: var(--accent); flex-shrink: 0; }
.opt.selected { border-color: var(--accent); background: var(--accent-soft); }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.opt.dim { opacity: .6; }
.opt.struck span:not(.strike-x) { text-decoration: line-through; opacity: .45; }
.opt.struck { border-style: dashed; }
.strike-x {
  margin-left: auto; flex-shrink: 0; color: #9aa7bd; font-size: 13px; padding: 2px 4px;
  border-radius: 5px; align-self: center; user-select: none;
}
.strike-x:hover { color: var(--bad); background: var(--bad-soft); }
.quiz-timer { padding: 8px 16px; font-size: 14.5px; margin-bottom: 12px; }
.quiz-timer b { font-variant-numeric: tabular-nums; font-size: 16px; }
.quiz-timer b.low { color: var(--bad); }
.reveal { margin-top: 14px; border-radius: 9px; padding: 13px 16px; font-size: 14px; }
.reveal.ok { background: var(--ok-soft); border: 1px solid #a6e9cd; }
.reveal.no { background: var(--bad-soft); border: 1px solid #f1b6b6; }
.reveal b { display: block; margin-bottom: 4px; }

.exam-topbar {
  position: sticky; top: 56px; z-index: 40; background: #101a33; color: #fff; border-radius: var(--radius);
  display: flex; align-items: center; gap: 16px; padding: 10px 16px; margin-bottom: 16px; flex-wrap: wrap;
}
.exam-topbar .timer { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.exam-topbar .timer.low { color: #ff9c9c; }
.navgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; margin: 12px 0; }
.navcell {
  padding: 7px 0; text-align: center; border-radius: 7px; border: 1px solid var(--line); cursor: pointer;
  font-size: 12.5px; font-weight: 600; background: #fff;
}
.navcell.answered { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.navcell.flagged { background: var(--warn-soft); border-color: var(--warn); color: var(--warn); }
.navcell.current { outline: 2.5px solid var(--ink); }
.navcell.locked { background: #eceff4; color: #9aa7bd; cursor: not-allowed; }

.progressbar { height: 8px; background: #e6eaf1; border-radius: 6px; overflow: hidden; margin: 10px 0; }
.progressbar > div { height: 100%; background: var(--accent); transition: width .3s; }

.score-hero { text-align: center; padding: 26px 10px 10px; }
.score-hero .big { font-size: 44px; font-weight: 800; }
.score-hero .scaled { font-size: 20px; color: var(--accent); font-weight: 700; }
.area-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.area-table th, .area-table td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; }
.pct-bar { background: #e6eaf1; border-radius: 5px; height: 9px; min-width: 120px; }
.pct-bar > div { height: 100%; border-radius: 5px; }

/* ---------- reference & search ---------- */
.glossary-list { columns: 2; column-gap: 34px; }
.glossary-list li { break-inside: avoid; margin-bottom: 9px; font-size: 14px; }
.topic-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px; }
.topic-item { display: flex; justify-content: space-between; gap: 10px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 14px; }
.topic-item .chapters a { color: var(--accent); text-decoration: none; font-weight: 600; margin-left: 5px; }
.search-result { padding: 12px 4px; border-bottom: 1px solid var(--line); }
.search-result .kind { font-size: 11px; text-transform: uppercase; letter-spacing: .6px; font-weight: 700; color: var(--ink-soft); }
.search-result a { color: var(--ink); font-weight: 600; text-decoration: none; font-size: 15px; }
.search-result a:hover { color: var(--accent); }
.search-result .snip { color: var(--ink-soft); font-size: 13px; }
kbd { background: #eef1f6; padding: 1px 6px; border-radius: 4px; font-size: 12px; border: 1px solid var(--line); }

.empty { text-align: center; color: var(--ink-soft); padding: 44px 0; }
.warnbox { background: var(--warn-soft); border: 1px solid #f2d98c; border-radius: var(--radius); padding: 13px 16px; margin: 12px 0; font-size: 14px; }

/* ================= responsive / mobile ================= */

/* Tablet and below: tighten layout, keep two-column grids where they fit */
@media (max-width: 900px) {
  .grid.cols2 { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
  .glossary-list { columns: 1; }
}

/* Phones */
@media (max-width: 720px) {
  body { font-size: 15px; }
  .container { padding: 14px; margin: 14px auto 40px; }
  h1 { font-size: 22px; }
  h2 { font-size: 18px; }
  .card { padding: 14px; border-radius: 9px; }

  /* Topbar: brand + search on row 1, horizontally scrollable nav on row 2 */
  .topbar { padding: 8px 12px; gap: 10px; }
  .brand { font-size: 14px; gap: 7px; flex: 1 1 auto; min-width: 0; }
  .brand-mark { padding: 2px 6px; }
  .searchbox { order: 2; margin-left: auto; flex: 0 1 170px; }
  .searchbox input { width: 100%; font-size: 16px; padding: 6px 10px; }
  .nav { order: 3; width: 100%; flex-wrap: nowrap; overflow-x: auto;
         -webkit-overflow-scrolling: touch; scrollbar-width: none; gap: 2px; }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { white-space: nowrap; padding: 7px 10px; font-size: 13.5px; }

  /* Prevent iOS zoom on focus */
  select, input[type="text"], input[type="number"] { font-size: 16px; }
  fieldset { margin: 8px 0 12px; }
  .checks { gap: 8px 12px; padding: 6px 8px; }

  /* Single-column grids */
  .grid.cols2, .grid.cols3 { grid-template-columns: 1fr; }
  .chapter-card .ch-idea { -webkit-line-clamp: 3; }
  .topic-grid { grid-template-columns: 1fr; }

  /* Reader: tables scroll inside the card */
  .md table { font-size: 12.5px; }
  .md th, .md td { padding: 6px 8px; }
  .md h2 { font-size: 16.5px; }
  .reader-nav a { font-size: 16px; padding: 6px 0; }
  .quickref { padding: 12px; font-size: 14px; }

  /* Buttons: comfortable tap targets */
  .btn { min-height: 44px; padding: 10px 14px; }
  .btn.small { min-height: 38px; }
  .btn-row { gap: 8px; }
  .btn-row .btn, .btn-row .btn.secondary { flex: 1 1 auto; text-align: center; }

  /* Flashcards: taller card, 2x2 grade buttons */
  .flashcard { height: min(64vw, 340px); }
  .flashcard .face { font-size: 16.5px; padding: 22px 16px; border-radius: 12px; }
  .grade-row .btn { min-width: 0; flex: 1 1 42%; }

  /* Quiz / exam items */
  .q-item .stem { font-size: 15.5px; }
  .opt { padding: 12px 12px; font-size: 14.5px; min-height: 44px; }
  .q-meta { font-size: 12px; gap: 6px; }
  .q-meta a { margin-left: 0 !important; }

  /* Exam topbar: static (wrapping sticky bars overlap on small screens),
     timer prominent, controls full-width row */
  .exam-topbar { position: static; gap: 8px 12px; padding: 10px 12px; }
  .exam-topbar .timer { font-size: 22px; margin-left: auto; }
  .navgrid { grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 5px; }
  .navcell { padding: 9px 0; font-size: 12px; }
  .navcell.locked { pointer-events: none; }

  /* Results */
  .score-hero .big { font-size: 36px; }
  .area-table { font-size: 13px; }
  .pct-bar { min-width: 70px; }

  .search-result { padding: 10px 2px; }
  .footer { padding: 12px 10px 24px; }
}

/* Very small phones */
@media (max-width: 380px) {
  .searchbox { flex: 0 1 130px; }
  .navgrid { grid-template-columns: repeat(auto-fill, minmax(36px, 1fr)); }
}
