:root {
  --paper: #F3F6F8;
  --grid: #DCE5EB;
  --sheet: #FFFFFF;
  --ink: #14233B;
  --ink-soft: #4A5B72;
  --rule: #C3D0DA;
  --sea: #0B6E78;
  --sea-ink: #FFFFFF;
  --sea-wash: #D9EEF0;
  --marker: #FFE88A;
  --warn: #8A4B00;
  --warn-wash: #FFF1D6;
  --radius: 12px;
  --gutter: 16px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0F1A24;
    --grid: #1A2A38;
    --sheet: #162431;
    --ink: #E8EEF3;
    --ink-soft: #9DB0C2;
    --rule: #2E4357;
    --sea: #52C4CE;
    --sea-ink: #07232A;
    --sea-wash: #14383E;
    --marker: #5B4D0C;
    --warn: #F2C57C;
    --warn-wash: #3A2D14;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
  color: var(--ink);
  font: 17px/1.45 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 0;
}
h1, h2, h3, p { margin: 0; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--sea); outline-offset: 2px; }

/* ---------- top bar ---------- */
.bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px var(--gutter);
  min-height: 56px;
}
.bar h1 { font-size: 1.05rem; font-weight: 700; flex: 1; min-width: 0; line-height: 1.2; }
.bar .count { font-size: .9rem; color: var(--ink-soft); white-space: nowrap; }
.back {
  appearance: none; border: 0; background: transparent; cursor: pointer;
  width: 44px; height: 44px; margin-left: -10px;
  display: grid; place-items: center; border-radius: 50%;
}
.back svg { width: 24px; height: 24px; stroke: var(--ink); fill: none; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- home / topic lists ---------- */
.intro { padding: 18px var(--gutter) 8px; }
.intro h1 { font-size: 1.9rem; line-height: 1.1; font-weight: 800; letter-spacing: -.01em; }
.intro p { color: var(--ink-soft); margin-top: 8px; max-width: 34ch; }
.list { list-style: none; margin: 0; padding: 12px var(--gutter) 24px; display: grid; gap: 10px; }
.row {
  appearance: none; width: 100%; text-align: left; cursor: pointer;
  background: var(--sheet); border: 1px solid var(--rule); border-left: 6px solid var(--sea);
  border-radius: var(--radius); padding: 14px 14px 14px 16px; min-height: 64px;
  display: grid; gap: 4px;
}
.row:active { background: var(--sea-wash); }
.row .name { font-weight: 700; font-size: 1.08rem; line-height: 1.25; }
.row .sub { color: var(--ink-soft); font-size: .93rem; line-height: 1.3; }
.row .eg { overflow-x: auto; padding-top: 2px; color: var(--ink); -webkit-overflow-scrolling: touch; }
.row .eg .katex { font-size: 1.05em; }
.row.mixed { border-left-color: var(--ink); background: var(--sea-wash); }
.row.mixed .sub { color: var(--ink); }
.foot { margin-top: auto; padding: 12px var(--gutter) calc(18px + env(safe-area-inset-bottom)); color: var(--ink-soft); font-size: .82rem; }

/* ---------- practice ---------- */
.page { flex: 1; padding: 6px var(--gutter) 16px; display: flex; flex-direction: column; gap: 14px; }
.sheet {
  background: var(--sheet); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 16px 16px 18px 22px; position: relative;
}
.sheet::before {               /* jotter margin line */
  content: ""; position: absolute; left: 10px; top: 0; bottom: 0; width: 2px; background: var(--sea); opacity: .55;
}
.instr { color: var(--ink-soft); font-size: .98rem; margin-bottom: 10px; }
.instr .katex { font-size: 1em; }
.problem { overflow-x: auto; overflow-y: hidden; padding: 6px 0; -webkit-overflow-scrolling: touch; }
.problem .katex-display { margin: 0; text-align: left; }
.problem .katex-display > .katex { text-align: left; }
.problem .katex { font-size: calc(1.55em * var(--fit, 1)); }
.problem.prose { font-size: 1.08rem; line-height: 1.55; overflow: visible; }
.problem.prose .katex { font-size: 1.08em; display: inline-block; white-space: nowrap; }
.row.strand { border-left-width: 8px; padding-top: 18px; padding-bottom: 18px; }
.row.strand .name { font-size: 1.3rem; }

.answer-label { font-size: .9rem; font-weight: 700; color: var(--ink-soft); margin-bottom: 6px; }
.answer { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.answer .katex-display { margin: 0; text-align: left; }
.answer .katex-display > .katex { text-align: left; }
.answer .katex { font-size: calc(1.55em * var(--fit, 1)); }
.hl {
  display: inline-block; max-width: 100%; padding: 2px 8px; margin: -2px -8px; border-radius: 4px;
  background: linear-gradient(var(--marker), var(--marker)) no-repeat 0 0 / 0% 100%;
  animation: sweep .6s ease-out .05s forwards;
}
@keyframes sweep { to { background-size: 100% 100%; } }
@media (prefers-reduced-motion: reduce) { .hl { animation: none; background-size: 100% 100%; } }
.note { margin-top: 10px; color: var(--ink-soft); font-size: .93rem; }
.note .katex { font-size: 1em; }

.working { display: grid; gap: 8px; }
.working h2 { font-size: .9rem; font-weight: 700; color: var(--ink-soft); }
.method { font-weight: 700; }
.say { line-height: 1.5; }
.say .katex { font-size: 1.05em; }
.say .katex, .watch .katex, .note .katex, .instr .katex { display: inline-block; white-space: nowrap; }
.check { position: relative; display: grid; }
.check .line .katex { font-size: calc(1.15em * var(--fit, 1)); }
.check .tick { color: var(--sea); font-weight: 800; font-size: 1.2rem; line-height: 1; padding: 2px 0 0 2px; }
.line { overflow-x: auto; overflow-y: hidden; padding: 2px 0; -webkit-overflow-scrolling: touch; }
.line .katex-display { margin: 0; text-align: left; }
.line .katex-display > .katex { text-align: left; }
.line .katex { font-size: calc(1.3em * var(--fit, 1)); }
.watch {
  background: var(--warn-wash); color: var(--warn); border-radius: 10px; padding: 10px 12px; font-size: .95rem; line-height: 1.45;
}
.watch .katex { font-size: 1.02em; }

/* ---------- action bar ---------- */
.actions {
  position: sticky; bottom: 0; padding: 12px var(--gutter) calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--paper) 78%, transparent);
  display: grid; gap: 10px;
}
.ask { font-weight: 700; text-align: center; padding: 2px 0 0; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  appearance: none; cursor: pointer; min-height: 52px; padding: 12px 16px; border-radius: var(--radius);
  font-weight: 700; font-size: 1.05rem; border: 2px solid var(--sea); background: var(--sheet); color: var(--sea);
}
.btn.primary { background: var(--sea); color: var(--sea-ink); }
.btn:active { transform: translateY(1px); }

.update {
  position: fixed; z-index: 10; top: calc(8px + env(safe-area-inset-top)); left: 8px; right: 8px; max-width: 540px; margin: 0 auto;
  background: var(--ink); color: var(--paper); border-radius: var(--radius); padding: 8px 8px 8px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: .95rem;
}
.update[hidden] { display: none; }
.update button { appearance: none; border: 0; border-radius: 8px; padding: 8px 14px; min-height: 40px; font-weight: 700; background: var(--sea); color: var(--sea-ink); cursor: pointer; }
.noscript { padding: 24px; }

/* ---------- past paper questions ---------- */
.row.past { border-left-color: var(--warn); }
.pp-label { font-size: .88rem; font-weight: 700; color: var(--warn); margin-bottom: 10px; line-height: 1.35; }
.pp-part + .pp-part { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--rule); }
.pp-sub { font-weight: 800; margin: 2px 0 4px; }
.pp-sub span { font-weight: 500; color: var(--ink-soft); font-size: .88rem; margin-left: 6px; }
.pp-text { line-height: 1.55; margin: 0 0 6px; }
.pp-text .katex { font-size: 1.12em; display: inline-block; white-space: nowrap; }
.ppm { overflow-x: auto; overflow-y: hidden; padding: 4px 0 8px; }
.ppm .katex-display { margin: 0; text-align: left; }
.ppm .katex-display > .katex { text-align: left; }
.ppm .katex { font-size: calc(1.3em * var(--fit, 1)); }
.pp-ack { font-size: .78rem; color: var(--ink-soft); line-height: 1.4; margin: 2px 4px 28px; }
