/* SPM Maths Drill – the main site's layout and controls (spm-math.com), in indigo instead of teal, with a
   ruled drill sheet. */

/* ------------------------------------------------------------------ tokens */
:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #edf0f7;
  --text: #171c2c;
  --muted: #5a6178;
  --border: #d7dce8;
  --accent: #3848a8;
  --accent-strong: #2a388a;
  --on-accent: #ffffff;
  --accent-soft: #e0e5fa;
  --focus: #b4552e;
  --e: #15803d;
  --m: #b45309;
  --a: #b91c1c;
  --rule: #c9cfe0;
  --shadow: 0 1px 2px rgba(20, 28, 60, 0.06), 0 6px 20px rgba(20, 28, 60, 0.06);
  --paper: #ffffff;
  --paper-text: #111111;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11131b;
    --surface: #1a1d29;
    --surface-2: #232737;
    --text: #e5e8f2;
    --muted: #9aa1ba;
    --border: #30364a;
    --accent: #8d9cf2;
    --accent-strong: #b0bbf7;
    --on-accent: #0e1433;
    --accent-soft: #252d55;
    --focus: #e29066;
    --e: #4ade80;
    --m: #fbbf24;
    --a: #f87171;
    --rule: #343b52;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
    --paper: #1c2030;
    --paper-text: #e9ecf4;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11131b;
  --surface: #1a1d29;
  --surface-2: #232737;
  --text: #e5e8f2;
  --muted: #9aa1ba;
  --border: #30364a;
  --accent: #8d9cf2;
  --accent-strong: #b0bbf7;
  --on-accent: #0e1433;
  --accent-soft: #252d55;
  --focus: #e29066;
  --e: #4ade80;
  --m: #fbbf24;
  --a: #f87171;
  --rule: #343b52;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
  --paper: #1c2030;
  --paper-text: #e9ecf4;
  color-scheme: dark;
}

/* ------------------------------------------------------------------- base */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; border-radius: 6px; z-index: 50; }
.skip:focus { left: 8px; }
h1, h2, h3, p { margin: 0; }

/* ----------------------------------------------------------------- header */
.app-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  border-top: 3px solid var(--accent);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { color: var(--accent); display: grid; }
.brand h1 { font-size: 17px; line-height: 1.2; font-weight: 650; letter-spacing: -0.01em; }
.tag { font-size: 12.5px; color: var(--muted); }
.header-tools { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
}
.icon-btn:hover { border-color: var(--accent); }
.sister-btn { width: auto; display: flex; gap: 6px; padding: 0 12px; font-size: 13px; color: inherit; text-decoration: none; }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

/* segmented control */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; flex-wrap: wrap; max-width: 100%; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 6px; color: var(--muted); font-weight: 550; min-height: 32px; }
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.seg.wide button { padding: 6px 10px; }
.opt > .seg { justify-self: start; }

/* ----------------------------------------------------------------- layout */
.layout { display: grid; grid-template-columns: minmax(280px, 350px) minmax(0, 1fr); gap: 20px; padding: 20px; max-width: 1500px; margin: 0 auto; align-items: start; }
.sidebar {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; position: sticky; top: 71px; max-height: calc(100vh - 91px); display: flex; flex-direction: column; min-height: 0;
}
.side-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.side-head h2 { font-size: 15px; font-weight: 650; }
.count { font-size: 12.5px; color: var(--accent-strong); font-weight: 600; }
.search { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.side-actions { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 0 6px; }
.link-btn { background: none; border: 0; padding: 2px 0; color: var(--accent-strong); font-size: 12.5px; text-decoration: underline; text-underline-offset: 2px; }
.tree { overflow: auto; margin: 0 -6px; padding: 0 6px; flex: 1; min-height: 0; }

/* tree */
.tree details { margin: 0; }
.tree summary { list-style: none; display: flex; align-items: center; gap: 6px; padding: 5px 4px; border-radius: 6px; cursor: pointer; user-select: none; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary:hover { background: var(--surface-2); }
.tree summary::before {
  content: ""; width: 7px; height: 7px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg); transition: transform 0.12s; margin: 0 4px 0 2px; flex-shrink: 0;
}
.tree details[open] > summary::before { transform: rotate(45deg); }
.tree .form-sum { font-weight: 650; font-size: 14px; }
.tree .form-body { margin-left: 8px; border-left: 1px solid var(--border); padding-left: 6px; }
.tree .ch-sum { font-size: 13.5px; }
.tree .ch-sum .no { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 1.6em; }
.tree .ch-sum .n { margin-left: auto; font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tree .topics { margin: 0 0 4px 22px; display: grid; gap: 1px; }
.tree label.row { display: flex; align-items: flex-start; gap: 7px; padding: 3px 4px; border-radius: 6px; font-size: 13px; cursor: pointer; line-height: 1.35; }
.tree label.row:hover { background: var(--surface-2); }
.tree input[type="checkbox"] { accent-color: var(--accent); width: 15px; height: 15px; margin: 2px 0 0; flex-shrink: 0; }
.tree .tid { color: var(--muted); font-variant-numeric: tabular-nums; flex-shrink: 0; min-width: 2.6em; }
.tree .empty { color: var(--muted); font-size: 13px; padding: 8px 4px; }
.tree .hide { display: none; }
.sum-label { display: flex; align-items: center; gap: 7px; flex: 1; min-width: 0; }

/* ---------------------------------------------------------------- options */
.main { min-width: 0; display: grid; gap: 16px; }
.options { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px 24px; align-items: start; }
.opt { display: grid; gap: 6px; align-content: start; min-width: 0; }
/* wide screens: Difficulty gets the room its four BM labels need on one row */
@media (min-width: 1240px) { .options { grid-template-columns: minmax(250px, 0.8fr) minmax(380px, 1.1fr) minmax(250px, 1fr); } }
.opt label, .lbl { font-size: 12.5px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.count-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#count { width: 66px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.chips { display: flex; gap: 4px; flex-wrap: wrap; }
.chips button { padding: 4px 10px; border-radius: 99px; border: 1px solid var(--border); background: var(--surface-2); font-size: 13px; min-height: 30px; }
.chips button:hover, .chips button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent-strong); }
.chips button[aria-pressed="true"] { background: var(--accent-soft); }
.hint { font-size: 12.5px; color: var(--muted); }
.hint.bad { color: var(--a); }
.hint[hidden] { display: none; }
.more { grid-column: 1 / -1; }
.more-grid { display: flex; flex-wrap: wrap; gap: 10px 24px; align-items: center; }
.check { display: inline-flex; gap: 9px; align-items: center; font-size: 14px; cursor: pointer; }
/* switch-style toggles: the native checkbox restyled, so app.js still reads/writes .checked */
.check input[role="switch"] { appearance: none; -webkit-appearance: none; position: relative; flex-shrink: 0; width: 38px; height: 22px; margin: 0; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--muted); cursor: pointer; transition: background-color 0.15s, border-color 0.15s; }
.check input[role="switch"]::before { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: transform 0.15s; }
.check input[role="switch"]:checked { background: var(--accent); border-color: var(--accent); }
.check input[role="switch"]:checked::before { transform: translateX(16px); background: var(--on-accent); }
.check:hover input[role="switch"]:not(:checked) { border-color: var(--accent); }
@media (prefers-reduced-motion: reduce) { .check input[role="switch"], .check input[role="switch"]::before { transition: none; } }
.more .check, .more .seed { font-size: 14px; font-weight: 550; color: var(--text); text-transform: none; letter-spacing: normal; }
.seed { display: inline-flex; gap: 8px; align-items: center; flex: 1 1 280px; min-width: 0; }
.seed input { flex: 1; min-width: 0; padding: 4px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.seed input[aria-invalid="true"] { border-color: var(--a); outline: 1px solid var(--a); }
.actions { grid-column: 1 / -1; display: flex; gap: 10px; flex-wrap: wrap; }
.options > .hint { grid-column: 1 / -1; margin-top: -6px; }
.btn { display: inline-flex; align-items: center; gap: 8px; padding: 9px 16px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); font-weight: 600; min-height: 40px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

/* ------------------------------------------------------------------ sheet */
.sheet { background: var(--paper); color: var(--paper-text); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 34px 34px; font-family: "Latin Modern Roman", "Times New Roman", Georgia, serif; font-size: 16px; line-height: 1.45; min-height: 240px; overflow-wrap: anywhere; }
.sheet .empty { text-align: center; padding: 48px 12px; color: var(--muted); font-family: system-ui, sans-serif; }
.sheet .empty h3 { font-size: 18px; color: var(--text); margin-bottom: 4px; }
.sheet-head { border-bottom: 2px solid currentColor; padding-bottom: 8px; margin-bottom: 6px; }
.sheet-head h2 { font-size: 1.3em; line-height: 1.25; }
.sheet-head .sub { font-size: 0.92em; opacity: 0.75; margin-top: 2px; }
.code-row { display: flex; align-items: center; gap: 10px; margin-top: 3px; flex-wrap: wrap; }
.sheet .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.72em; opacity: 0.7; }
.brand-pill { font: 600 11px/1 system-ui, sans-serif; letter-spacing: 0.02em; color: var(--accent-strong); border: 1px solid currentColor; border-radius: 999px; padding: 3px 9px; text-decoration: none; }
.idline { display: flex; gap: 22px; margin-top: 12px; font-size: 0.95em; }
.idline span { display: flex; gap: 6px; align-items: flex-end; }
.idline span::after { content: ""; flex: 1; border-bottom: 1px solid currentColor; height: 1em; }
.idline .nm { flex: 3; } .idline .cl { flex: 1.2; } .idline .dt { flex: 1.2; }
.idline .sc { flex: 1.1; }
.idline .sc::after { display: none; }
.idline .sc i { flex: 1; min-width: 2.5em; border-bottom: 1px solid currentColor; height: 1em; }

/* one drill = a lettered section: the instruction, then a grid of items */
.drill { margin-top: 18px; }
.drill-head { display: grid; grid-template-columns: minmax(1.9em, auto) minmax(0, 1fr); column-gap: 0.4em; align-items: baseline; break-after: avoid; page-break-after: avoid; margin-bottom: 8px; }
.drill-letter { display: inline-grid; place-items: center; min-width: 1.45em; padding: 0 0.2em; height: 1.45em; white-space: nowrap; border-radius: 5px; background: var(--accent); color: var(--on-accent); font: 700 0.85em/1 system-ui, sans-serif; }
.drill-ins { font-weight: 700; }
.drill-ref { font: 500 0.7em system-ui, sans-serif; opacity: 0.6; margin-left: 8px; white-space: nowrap; }
ol.items { list-style: none; margin: 0 0 0 1.9em; padding: 0; display: grid; grid-template-columns: repeat(var(--cols, 2), minmax(0, 1fr)); gap: 4px 22px; }
.one-col ol.items { grid-template-columns: minmax(0, 1fr); }
.item { display: grid; grid-template-columns: 1.9em minmax(0, 1fr); align-content: start; break-inside: avoid; page-break-inside: avoid; padding: 4px 0 2px; }
.item .qn { font-weight: 700; font-variant-numeric: tabular-nums; }
.item .qb { min-width: 0; }
.item .qt { display: block; }
/* the working space: ruled (like an exercise book) or blank */
.item .space { margin-top: 4px; height: calc(var(--lines) * 7.5mm); }
.sp-lined .item .space {
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent calc(7.5mm - 1px), var(--rule) calc(7.5mm - 1px), var(--rule) 7.5mm);
}
.sp-none .item .space { display: none; }
.sp-none ol.items { row-gap: 2px; }
.dmark { display: none; width: 0.5em; height: 0.5em; border-radius: 50%; margin-left: 6px; vertical-align: 0.15em; }
.show-marks .dmark { display: inline-block; }
.dmark.e { background: var(--e); } .dmark.m { background: var(--m); } .dmark.a { background: var(--a); }
.dmark.m { border-radius: 1px; } .dmark.a { border-radius: 0; transform: rotate(45deg) scale(0.9); }

table.qt { border-collapse: collapse; margin: 4px 0; font-size: 0.95em; overflow-wrap: normal; }
table.qt th, table.qt td { border: 1px solid currentColor; padding: 2px 9px; text-align: center; min-width: 2em; }
.sheet .katex { font-size: 1.07em; }
.nb { white-space: nowrap; }

/* answers */
.answers { margin-top: 26px; border-top: 2px solid currentColor; padding-top: 12px; }
.ans-toggle { display: inline-flex; align-items: center; gap: 8px; background: var(--accent-soft); color: var(--accent-strong); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 14px; font: 650 14px system-ui, sans-serif; min-height: 38px; }
.ans-toggle .chev { width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(-45deg); transition: transform 0.12s; }
.ans-toggle[aria-expanded="true"] .chev { transform: rotate(45deg); }
.ans-print-title { display: none; }
.ans-body { margin-top: 14px; }
.answers.is-collapsed .ans-body { display: none; }
.answers.is-collapsed { border-top-style: dashed; }
.ans-drill { display: grid; grid-template-columns: minmax(1.9em, auto) minmax(0, 1fr); column-gap: 0.4em; margin-bottom: 10px; break-inside: avoid-page; }
.ans-drill .drill-letter { margin-top: 2px; }
ol.ans-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--acol, 150px), 1fr)); gap: 0 18px; }
ol.ans-list > li { display: grid; grid-template-columns: 1.9em minmax(0, 1fr); break-inside: avoid; padding: 2px 0; border-bottom: 1px dotted color-mix(in srgb, currentColor 22%, transparent); }
ol.ans-list .an { font-weight: 700; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
  .sidebar { position: static; max-height: 460px; }
  .app-header { padding: 8px 16px; position: static; }
  .tag { display: none; }
  .sheet { padding: 18px 16px 22px; }
}
@media (max-width: 640px) {
  ol.items { grid-template-columns: repeat(min(var(--cols, 2), 2), minmax(0, 1fr)); margin-left: 0; }
  .sister-btn span { display: none; }
  .sister-btn { padding: 0; width: 36px; justify-content: center; }
}
@media (max-width: 520px) {
  .options { grid-template-columns: 1fr; }
  .seg button { padding: 6px 10px; }
  .brand h1 { font-size: 15px; }
  .idline { flex-wrap: wrap; gap: 8px 16px; }
  .idline .nm { flex-basis: 100%; }
  ol.items { grid-template-columns: minmax(0, 1fr); }
}

/* ------------------------------------------------------------------ print */
@page { size: A4; margin: 13mm 14mm 15mm; @bottom-center { content: counter(page); font: 9pt "Times New Roman", serif; color: #444; } }
@media print {
  :root, :root[data-theme="dark"] {
    --bg: #fff; --surface: #fff; --text: #000; --paper: #fff; --paper-text: #000; --border: #999; --e: #000; --m: #000; --a: #000;
    --accent: #000; --on-accent: #fff; --accent-soft: #fff; --accent-strong: #000; --muted: #444; --rule: #c4c4c4;
    color-scheme: light;
  }
  html, body { background: #fff !important; color: #000 !important; font-size: 11.5pt; }
  .skip, .app-header, .sidebar, .options, .ans-toggle { display: none !important; }
  .layout { display: block; padding: 0; max-width: none; }
  .main { display: block; }
  .sheet { border: 0; box-shadow: none; border-radius: 0; padding: 0; font-size: 11.5pt; line-height: 1.4; min-height: 0; overflow: visible; }
  .sheet-head { padding-bottom: 5px; }
  .drill { margin-top: 12px; }
  .answers { break-before: page; page-break-before: always; margin-top: 0; border-top: 0; padding-top: 0; }
  .answers.is-collapsed .ans-body, .ans-body { display: block !important; }
  .ans-print-title { display: block; font-size: 1.3em; font-weight: 700; border-bottom: 2px solid #000; padding-bottom: 5px; margin-bottom: 10px; }
  .ans-print-title small { font-weight: 400; font-size: 0.65em; margin-left: 8px; opacity: 0.8; }
  .ans-body { margin-top: 0; font-size: 10.5pt; }
  .brand-pill { color: #fff !important; background: #000; border-color: #000; font-size: 8.5pt; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
