  :root {
    --bg: #12161d;
    --surface: #1a2028;
    --surface-2: #222a34;
    --surface-3: #2b3542;
    --text: #e8ecf1;
    --text-dim: #93a1b0;
    --text-faint: #5f6b7a;
    --accent: #e2a13a;
    --accent-ink: #1a1206;
    --accent-soft: rgba(226, 161, 58, 0.14);
    --good: #46a688;
    --good-soft: rgba(70, 166, 136, 0.14);
    --bad: #c1485a;
    --bad-soft: rgba(193, 72, 90, 0.14);
    --border: rgba(255, 255, 255, 0.08);
    --code-bg: #0d1015;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  }

  @media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) {
      --bg: #eef1f5;
      --surface: #ffffff;
      --surface-2: #e6eaf0;
      --surface-3: #dae0e8;
      --text: #1b232d;
      --text-dim: #55636f;
      --text-faint: #8493a1;
      --accent: #c9822a;
      --accent-ink: #fff9f0;
      --accent-soft: rgba(201, 130, 42, 0.12);
      --good: #2f7a63;
      --good-soft: rgba(47, 122, 99, 0.1);
      --bad: #a83b4c;
      --bad-soft: rgba(168, 59, 76, 0.1);
      --border: rgba(20, 30, 40, 0.1);
      --code-bg: #e7ebf0;
      --shadow: 0 8px 24px rgba(30, 40, 55, 0.1);
    }
  }

  :root[data-theme="dark"] {
    --bg: #12161d; --surface: #1a2028; --surface-2: #222a34; --surface-3: #2b3542;
    --text: #e8ecf1; --text-dim: #93a1b0; --text-faint: #5f6b7a;
    --accent: #e2a13a; --accent-ink: #1a1206; --accent-soft: rgba(226, 161, 58, 0.14);
    --good: #46a688; --good-soft: rgba(70, 166, 136, 0.14);
    --bad: #c1485a; --bad-soft: rgba(193, 72, 90, 0.14);
    --border: rgba(255, 255, 255, 0.08); --code-bg: #0d1015;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  }

  :root[data-theme="light"] {
    --bg: #eef1f5; --surface: #ffffff; --surface-2: #e6eaf0; --surface-3: #dae0e8;
    --text: #1b232d; --text-dim: #55636f; --text-faint: #8493a1;
    --accent: #c9822a; --accent-ink: #fff9f0; --accent-soft: rgba(201, 130, 42, 0.12);
    --good: #2f7a63; --good-soft: rgba(47, 122, 99, 0.1);
    --bad: #a83b4c; --bad-soft: rgba(168, 59, 76, 0.1);
    --border: rgba(20, 30, 40, 0.1); --code-bg: #e7ebf0;
    --shadow: 0 8px 24px rgba(30, 40, 55, 0.1);
  }

  * { box-sizing: border-box; }

  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
  }

  h1, h2, h3, .display {
    font-family: Georgia, "Iowan Old Style", "Palatino Linotype", "Times New Roman", serif;
    text-wrap: balance;
    margin: 0;
  }

  code, pre, .mono, .tabular {
    font-family: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Consolas, monospace;
  }

  .tabular { font-variant-numeric: tabular-nums; }

  button {
    font: inherit;
    color: inherit;
  }

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

  a { color: var(--accent); }

  /* ---------- Header ---------- */

  .topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px 20px;
    padding: 18px 24px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 10;
  }

  .topbar-title {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-right: auto;
  }

  .topbar-title h1 {
    font-size: 1.35rem;
    font-weight: 600;
    letter-spacing: 0.2px;
  }

  .topbar-title .eyebrow {
    font-size: 0.7rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-faint);
  }

  .topbar-home {
    display: flex;
    flex-direction: column;
    gap: 2px;
    background: none;
    border: none;
    padding: 0;
    text-align: left;
    cursor: pointer;
  }

  /* ---------- Welcome / landing ---------- */

  .welcome-view {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
  }

  .welcome-card {
    max-width: 620px;
    width: 100%;
    text-align: center;
  }

  .welcome-card .eyebrow {
    font-size: 0.7rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-faint);
  }

  .welcome-card h1 {
    font-size: 2.3rem;
    margin: 10px 0 18px;
  }

  .welcome-copy {
    color: var(--text-dim);
    font-size: 1rem;
    line-height: 1.6;
    margin: 0 0 32px;
  }

  .welcome-options {
    display: grid;
    gap: 16px;
    text-align: left;
  }

  .welcome-option {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    box-shadow: var(--shadow);
  }

  .welcome-option.featured {
    border-color: var(--accent);
    background: var(--accent-soft);
  }

  .welcome-option-icon {
    font-size: 1.8rem;
    flex: none;
  }

  .welcome-option > div {
    flex: 1;
  }

  .welcome-option h3 {
    font-size: 1.05rem;
    margin: 0 0 4px;
  }

  .welcome-option p {
    margin: 0;
    font-size: 0.86rem;
    color: var(--text-dim);
    line-height: 1.4;
  }

  .welcome-option .nav-btn {
    flex: none;
  }

  @media (max-width: 560px) {
    .welcome-option { flex-direction: column; align-items: flex-start; }
    .welcome-option .nav-btn { align-self: stretch; }
  }

  .chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 13px;
    border-radius: 999px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    font-size: 0.82rem;
    color: var(--text-dim);
    white-space: nowrap;
  }

  .chip.countdown {
    background: var(--accent-soft);
    color: var(--accent);
    border-color: transparent;
    font-weight: 600;
  }

  .chip .num { color: var(--text); font-weight: 700; }
  .chip.countdown .num { color: var(--accent); }

  .reset-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-faint);
    border-radius: 999px;
    padding: 7px 13px;
    font-size: 0.78rem;
    cursor: pointer;
  }
  .reset-btn:hover { color: var(--text); border-color: var(--text-faint); }
  .reset-btn.confirming { color: var(--bad); border-color: var(--bad); }

  /* ---------- Shell ---------- */

  .shell {
    display: grid;
    grid-template-columns: 300px 1fr;
    gap: 0;
    max-width: 1180px;
    margin: 0 auto;
    align-items: start;
  }

  @media (max-width: 860px) {
    .shell { grid-template-columns: 1fr; }
  }

  /* ---------- Sidebar ---------- */

  .sidebar {
    padding: 20px 16px 40px;
    border-right: 1px solid var(--border);
    position: sticky;
    top: 66px;
    max-height: calc(100vh - 66px);
    overflow-y: auto;
  }

  @media (max-width: 860px) {
    .sidebar {
      position: static;
      max-height: none;
      border-right: none;
      border-bottom: 1px solid var(--border);
      padding: 12px 16px;
    }
  }

  .sidebar-label {
    font-size: 0.7rem;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--text-faint);
    padding: 4px 10px 10px;
  }

  .topic-list {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .topic-row {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    text-align: left;
    background: none;
    border: 1px solid transparent;
    border-radius: 10px;
    padding: 9px 10px;
    cursor: pointer;
    color: var(--text-dim);
  }

  .topic-row:hover { background: var(--surface-2); }

  .topic-row.active {
    background: var(--surface-2);
    border-color: var(--border);
    color: var(--text);
  }

  .topic-idx {
    font-size: 0.72rem;
    color: var(--text-faint);
    width: 20px;
    flex: none;
    font-variant-numeric: tabular-nums;
  }

  .topic-row.active .topic-idx { color: var(--accent); }

  .topic-name {
    flex: 1;
    font-size: 0.86rem;
    line-height: 1.25;
  }

  .topic-progress {
    flex: none;
    font-size: 0.72rem;
    color: var(--text-faint);
    font-variant-numeric: tabular-nums;
  }

  .topic-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-faint);
    flex: none;
  }
  .topic-dot.done { background: var(--good); }
  .topic-dot.partial { background: var(--accent); }

  /* ---------- Main ---------- */

  .main {
    padding: 28px 28px 80px;
    min-width: 0;
  }

  @media (max-width: 640px) {
    .main { padding: 20px 16px 60px; }
  }

  .crumb {
    font-size: 0.78rem;
    color: var(--text-faint);
    margin-bottom: 14px;
    display: flex;
    gap: 8px;
    align-items: center;
  }

  .qgrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(30px, 1fr));
    gap: 6px;
    margin-bottom: 22px;
    max-width: 420px;
  }

  .qgrid button {
    aspect-ratio: 1;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-faint);
    font-size: 0.72rem;
    cursor: pointer;
    font-variant-numeric: tabular-nums;
  }
  .qgrid button.current { border-color: var(--accent); color: var(--text); }
  .qgrid button.correct { background: var(--good-soft); color: var(--good); border-color: transparent; }
  .qgrid button.incorrect { background: var(--bad-soft); color: var(--bad); border-color: transparent; }

  .card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 26px;
    box-shadow: var(--shadow);
  }

  @media (max-width: 640px) {
    .card { padding: 18px; border-radius: 12px; }
  }

  .card h2.display {
    font-size: 1.6rem;
    color: var(--accent);
    font-weight: 400;
    margin-bottom: 4px;
  }

  .topic-title {
    font-size: 0.95rem;
    color: var(--text-dim);
    margin-bottom: 18px;
  }

  .question-text {
    font-size: 1.02rem;
    line-height: 1.55;
    white-space: pre-wrap;
    margin-bottom: 22px;
  }

  .question-text.code {
    font-family: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Consolas, monospace;
    font-size: 0.86rem;
    line-height: 1.6;
    background: var(--code-bg);
    color: var(--text);
    padding: 16px;
    border-radius: 10px;
    overflow-x: auto;
    border: 1px solid var(--border);
  }

  .options {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
  }

  .option {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    text-align: left;
    padding: 13px 15px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    cursor: pointer;
    font-size: 0.94rem;
    line-height: 1.4;
    color: var(--text);
  }

  .option:hover:not([disabled]) { background: var(--surface-3); }

  .option .letter {
    flex: none;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    color: var(--text-faint);
  }

  .option.correct {
    background: var(--good-soft);
    border-color: transparent;
  }
  .option.correct .letter { background: var(--good); color: #fff; border-color: transparent; }

  .option.incorrect {
    background: var(--bad-soft);
    border-color: transparent;
  }
  .option.incorrect .letter { background: var(--bad); color: #fff; border-color: transparent; }

  .option[disabled] { cursor: default; }

  .mark {
    margin-left: auto;
    flex: none;
    font-size: 0.95rem;
  }
  .option.correct .mark { color: var(--good); }
  .option.incorrect .mark { color: var(--bad); }

  .explanation {
    background: var(--surface-2);
    border-left: 3px solid var(--text-faint);
    border-radius: 0 10px 10px 0;
    padding: 14px 16px;
    font-size: 0.9rem;
    line-height: 1.55;
    color: var(--text-dim);
  }
  .explanation.good { border-left-color: var(--good); }
  .explanation.bad { border-left-color: var(--bad); }
  .explanation .label {
    font-size: 0.68rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-faint);
    margin-bottom: 6px;
    display: block;
  }

  .nav-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 22px;
  }

  .nav-btn {
    padding: 10px 18px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    cursor: pointer;
    font-size: 0.88rem;
    color: var(--text);
  }
  .nav-btn:hover:not([disabled]) { background: var(--surface-3); }
  .nav-btn[disabled] { opacity: 0.4; cursor: default; }

  .nav-btn.primary {
    background: var(--accent);
    color: var(--accent-ink);
    border-color: transparent;
    font-weight: 600;
  }
  .nav-btn.primary:hover { filter: brightness(1.06); }

  .nav-spacer { flex: 1; }

  .nav-status {
    font-size: 0.8rem;
    color: var(--text-faint);
    font-variant-numeric: tabular-nums;
  }

  @media (prefers-reduced-motion: no-preference) {
    .option, .topic-row, .nav-btn, .qgrid button { transition: background 0.12s ease, border-color 0.12s ease; }
  }

  @media (max-width: 860px) {
    .sidebar-label { display: none; }
    .topic-list {
      flex-direction: row;
      overflow-x: auto;
      padding-bottom: 4px;
    }
    .topic-row { min-width: 220px; flex: 0 0 auto; }
  }

  /* ---------- Mode toggle ---------- */

  .mode-toggle {
    display: flex;
    gap: 2px;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px;
  }

  .mode-btn {
    background: none;
    border: none;
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 0.82rem;
    color: var(--text-faint);
    cursor: pointer;
  }

  .mode-btn.active {
    background: var(--accent);
    color: var(--accent-ink);
    font-weight: 600;
  }

  .option.selected {
    background: var(--accent-soft);
    border-color: var(--accent);
  }
  .option.selected .letter { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

  /* ---------- Exam mode ---------- */

  .exam-shell { max-width: 720px; margin: 0 auto; padding: 24px 20px 80px; }
  @media (max-width: 640px) { .exam-shell { padding: 16px 14px 60px; } }

  .exam-start { text-align: center; padding: 20px 0; }
  .exam-start h2 { font-size: 1.7rem; color: var(--accent); margin-bottom: 14px; }
  .exam-start-copy { color: var(--text-dim); line-height: 1.6; max-width: 46ch; margin: 0 auto 32px; }

  .exam-presets { display: flex; flex-direction: column; gap: 12px; }

  .exam-preset-btn {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 18px 22px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    text-align: left;
    box-shadow: var(--shadow);
  }
  .exam-preset-btn:hover { border-color: var(--accent); }
  .exam-preset-btn .n { font-size: 1.15rem; font-weight: 600; }
  .exam-preset-btn .t { font-size: 0.82rem; color: var(--text-faint); font-variant-numeric: tabular-nums; }

  .exam-resume-note {
    background: var(--accent-soft);
    color: var(--accent);
    border-radius: 10px;
    padding: 12px 16px;
    font-size: 0.85rem;
    margin-bottom: 20px;
  }

  .exam-topbar {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 18px;
    flex-wrap: wrap;
  }

  .exam-timer {
    font-family: ui-monospace, "SF Mono", "Cascadia Code", "Roboto Mono", Consolas, monospace;
    font-size: 1.6rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: var(--text);
    background: var(--surface-2);
    border-radius: 10px;
    padding: 6px 16px;
  }
  .exam-timer.low { color: var(--bad); }

  .exam-progress { color: var(--text-faint); font-size: 0.85rem; font-variant-numeric: tabular-nums; }

  .exam-finish-btn {
    margin-left: auto;
    background: none;
    border: 1px solid var(--bad);
    color: var(--bad);
    border-radius: 999px;
    padding: 7px 14px;
    font-size: 0.8rem;
    cursor: pointer;
  }

  .exam-confirm {
    background: var(--surface);
    border: 1px solid var(--bad);
    border-radius: 16px;
    padding: 22px;
    margin-bottom: 20px;
  }
  .exam-confirm p { margin: 0 0 16px; line-height: 1.5; }
  .exam-confirm-actions { display: flex; gap: 10px; flex-wrap: wrap; }

  .exam-qgrid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(28px, 1fr));
    gap: 5px;
    margin-bottom: 18px;
  }
  .exam-qgrid button {
    aspect-ratio: 1;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-faint);
    font-size: 0.68rem;
    cursor: pointer;
    font-variant-numeric: tabular-nums;
  }
  .exam-qgrid button.current { border-color: var(--accent); color: var(--text); }
  .exam-qgrid button.answered { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

  .exam-summary {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 24px;
    box-shadow: var(--shadow);
    margin-bottom: 24px;
  }
  .exam-summary h2 { font-size: 1.5rem; margin-bottom: 6px; }
  .exam-summary .big-score { font-size: 2.4rem; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
  .exam-summary-meta { color: var(--text-dim); font-size: 0.9rem; margin: 8px 0 18px; display: flex; gap: 18px; flex-wrap: wrap; }
  .exam-summary-actions { display: flex; gap: 10px; flex-wrap: wrap; }

  .review-item { margin-bottom: 20px; }
  .review-item .topic-title { font-size: 0.82rem; }

  .is-hidden { display: none !important; }
