:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1d232c;
  --muted: #5f6b7a;
  --line: #dde1e7;
  --accent: #1f6feb;
  --accent-soft: #e7f0ff;
  --right: #1a7f37;
  --right-soft: #e3f5e8;
  --wrong: #c2410c;
  --wrong-soft: #fdeee5;
  --ms: #fbf7ec;
  --ms-line: #eadfbf;
  --sol: #f3f7ff;
  --sol-line: #d6e2fb;
  --serif: "Iowan Old Style", "Charter", "Georgia", "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c; --panel: #1c2027; --ink: #e6e9ee; --muted: #9aa5b4; --line: #303641;
    --accent: #6ea8ff; --accent-soft: #1d2b44; --right: #5cc77a; --right-soft: #183022;
    --wrong: #f29b6b; --wrong-soft: #3a2418; --ms: #221f17; --ms-line: #4a4126; --sol: #172131; --sol-line: #2b3b57;
    color-scheme: dark;
  }
  /* Figures are black line art on white: keep them on a light card. */
  .fig img { background: #fff; border-radius: 4px; padding: 4px; }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--sans); font-size: 15px; line-height: 1.5; }
button { font: inherit; color: inherit; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 10px 24px; background: #111827; color: #fff;
}
.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; }
.brand-mark { display: grid; place-items: center; width: 36px; height: 36px; border-radius: 9px; background: #14b8c4; color: #08262a; font-weight: 800; }
.brand-title { font-weight: 700; }
.brand-sub { font-size: 12px; color: #a9b4c4; }
.lang { display: flex; background: #1f2937; border-radius: 8px; padding: 3px; }
.lang button { border: 0; background: none; color: #cbd5e1; padding: 4px 10px; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 13px; }
.lang button[aria-pressed="true"] { background: #14b8c4; color: #08262a; }
.filters-toggle { display: none; border: 1px solid #374151; background: none; color: #fff; border-radius: 8px; padding: 6px 12px; cursor: pointer; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 24px; max-width: 1240px; margin: 0 auto; padding: 24px; }
.sidebar { position: sticky; top: 80px; align-self: start; max-height: calc(100vh - 100px); overflow-y: auto; padding-right: 4px; }
.search input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); color: var(--ink); font: inherit;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.filter-group { margin-top: 20px; }
.filter-group h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 0 0 8px; }
.topic-section h3 { font-size: 13px; margin: 12px 0 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: 4px 12px; cursor: pointer; font-size: 13px; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.check { display: flex; align-items: flex-start; gap: 8px; padding: 3px 0; font-size: 14px; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--accent); }
.check .count { margin-left: auto; color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.session-list { columns: 2; column-gap: 12px; }
.session-list .check { break-inside: avoid; }
.reset { margin-top: 20px; border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; text-decoration: underline; }

/* ---------- main ---------- */
.unit-head h1 { font-size: 22px; margin: 0; }
.result-count { color: var(--muted); margin: 4px 0 16px; }
.questions { display: grid; gap: 18px; }
.q { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; scroll-margin-top: 80px; }
.q:target { box-shadow: 0 0 0 3px var(--accent); }
.q-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 10px 18px; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.q-src { font-weight: 600; color: var(--ink); }
.q-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { background: var(--accent-soft); color: var(--accent); border-radius: 6px; padding: 1px 8px; font-size: 12px; }
.q-link { margin-left: auto; color: var(--muted); text-decoration: none; font-weight: 700; }
.q-link:hover { color: var(--accent); }

.q-body { padding: 16px 22px 8px; font-family: var(--serif); font-size: 17px; line-height: 1.55; }
.q-body p { margin: 0 0 10px; }
.q-stem { display: grid; grid-template-columns: 30px minmax(0, 1fr); }
.q-number { font-weight: 700; }
.part { display: grid; grid-template-columns: 34px minmax(0, 1fr); margin-top: 6px; }
.part.depth-1, .lead.depth-1 { margin-left: 30px; }
.lead { margin-top: 10px; }
.notice { text-align: center; font-weight: 700; margin: 2px 0 12px; }
.bullets { margin: 0 0 10px; padding-left: 22px; }
.bullets li { margin: 2px 0; }
.part-label { color: var(--ink); }
.part-marks { text-align: right; color: var(--muted); font-family: var(--sans); font-size: 14px; margin: -6px 0 8px; }
.display-math { margin: 6px 0 14px; overflow-x: auto; overflow-y: hidden; }
.display-math .katex-display { margin: 0.4em 0; }
.fig { margin: 10px 0 14px; text-align: center; }
.fig img { max-width: 100%; height: auto; }
.data-lines { margin: 4px 0 12px; }
.data-table { border-collapse: collapse; margin: 8px auto 14px; }
.data-table th, .data-table td { border: 1px solid var(--line); padding: 6px 14px; text-align: center; }
.data-table td:first-child { text-align: left; }

.q-foot { padding: 6px 22px 14px; }
.ms-toggle { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 6px 14px; cursor: pointer; font-weight: 600; }
.ms-toggle[aria-expanded="true"] { background: var(--ms); border-color: var(--ms-line); }
.q-foot { display: flex; flex-wrap: wrap; gap: 8px; }
.sol-toggle { border: 1px solid var(--accent); background: var(--accent); color: #fff; border-radius: 8px; padding: 6px 14px; cursor: pointer; font-weight: 600; }
.sol-toggle[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent); }
.sol { background: var(--sol); border-top: 1px solid var(--sol-line); padding: 14px 22px 18px; font-size: 16px; line-height: 1.6; }
.sol p { margin: 0 0 10px; }
.sol-part { display: grid; grid-template-columns: 70px minmax(0, 1fr); gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--sol-line); }
.sol-part:last-child { border-bottom: 0; }
.sol-label { font-weight: 700; }
.sol-answer { margin: 10px 0; padding: 8px 12px; border: 1px solid var(--right); border-radius: 8px; background: var(--right-soft); }
.sol-label-answer { font-weight: 700; color: var(--right); }
.sol-tip { margin: 10px 0 4px; padding: 8px 12px; border-left: 3px solid var(--wrong); background: var(--wrong-soft); border-radius: 0 8px 8px 0; font-size: 15px; }
.sol-label-tip { display: block; font-weight: 700; color: var(--wrong); font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 2px; }
.sol-note { color: var(--muted); font-size: 14px; }
.ms { background: var(--ms); border-top: 1px solid var(--ms-line); padding: 14px 22px 18px; font-size: 15px; }
.ms-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; color: var(--muted); font-size: 13px; padding-bottom: 8px; border-bottom: 1px solid var(--ms-line); }
.ms-lang { border: 1px solid var(--ms-line); background: var(--panel); border-radius: 6px; padding: 2px 10px; cursor: pointer; font-size: 13px; font-weight: 600; }
.ms-general { padding: 10px 0; border-bottom: 1px solid var(--ms-line); font-weight: 600; }
.ms-general p { margin: 0 0 4px; }
.ms-part { display: grid; grid-template-columns: 70px minmax(0, 1fr); gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--ms-line); }
.ms-part:last-of-type { border-bottom: 0; }
.ms-label { font-weight: 700; }
.ms-rows { display: grid; }
.ms-row { display: grid; grid-template-columns: minmax(0, 1fr) 64px; gap: 12px; padding: 8px 0; border-top: 1px dashed var(--ms-line); }
.ms-row:first-child { border-top: 0; padding-top: 0; }
.ms-cell p { margin: 0 0 4px; }
.ms-cell .display-math { margin: 2px 0 6px; }
.ms-scheme { font-family: var(--serif); font-size: 16px; }
.ms-note { color: var(--muted); font-size: 14px; }
.ms-marks { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; }
.mk { font-weight: 700; font-variant-numeric: tabular-nums; }
.ms-alt { margin-top: 10px; padding: 8px 12px; border: 1px dashed var(--ms-line); border-radius: 8px; }
.ms-alt-title { font-weight: 700; margin-bottom: 4px; }
.ms-notes { margin-top: 10px; font-size: 14px; }
.ms-notes p { margin: 0 0 6px; }
.ms-total { text-align: right; font-weight: 700; margin-top: 6px; }
.ms-legend { margin-top: 12px; font-size: 13px; color: var(--muted); }
.ms-legend summary { cursor: pointer; font-weight: 600; }
.ms-legend dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 2px 12px; margin: 8px 0 0; }
.ms-legend dt { font-weight: 700; color: var(--ink); }
.ms-legend dd { margin: 0; }

.more { text-align: center; margin: 18px 0; }
.more button { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 18px; cursor: pointer; }
.empty { color: var(--muted); text-align: center; padding: 40px 0; }
.credit { color: var(--muted); font-size: 12px; margin: 32px 0 8px; }

/* ---------- phones ---------- */
@media (max-width: 860px) {
  .topbar { padding: 10px 16px; }
  .brand-sub { display: none; }
  .filters-toggle { display: block; }
  .layout { grid-template-columns: minmax(0, 1fr); padding: 16px; }
  .sidebar { display: none; position: static; max-height: none; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; }
  body.filters-open .sidebar { display: block; }
  .q-body { padding: 14px 16px 6px; font-size: 16px; }
  .q-stem { grid-template-columns: 24px minmax(0, 1fr); }
  .ms, .sol { padding: 12px 16px 16px; }
  .ms-part, .sol-part { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .ms-row { grid-template-columns: minmax(0, 1fr) 48px; gap: 8px; }
  /* Long formulas scroll inside their box; a smaller display size keeps most of them on screen. */
  .display-math .katex-display > .katex { font-size: 1.05em; }
  .part.depth-1, .lead.depth-1 { margin-left: 0; }
}
