/* ------------------------------------------------------------------ tokens */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #16202a;
  --muted: #5b6875;
  --border: #d8dee5;
  --accent: #0f766e;
  --accent-strong: #0b5c56;
  --on-accent: #ffffff;
  --accent-soft: #d9f0ed;
  --focus: #1d4ed8;
  --e: #15803d;
  --m: #b45309;
  --a: #b91c1c;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 6px 20px rgba(15, 23, 42, 0.06);
  --paper: #ffffff;
  --paper-text: #111111;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1418;
    --surface: #182027;
    --surface-2: #212b34;
    --text: #e6ebf0;
    --muted: #9aa8b5;
    --border: #2d3944;
    --accent: #3fc1b4;
    --accent-strong: #6fd9ce;
    --on-accent: #06211e;
    --accent-soft: #123a36;
    --focus: #7aa2ff;
    --e: #4ade80;
    --m: #fbbf24;
    --a: #f87171;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
    --paper: #1b242c;
    --paper-text: #e9eef2;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1418;
  --surface: #182027;
  --surface-2: #212b34;
  --text: #e6ebf0;
  --muted: #9aa8b5;
  --border: #2d3944;
  --accent: #3fc1b4;
  --accent-strong: #6fd9ce;
  --on-accent: #06211e;
  --accent-soft: #123a36;
  --focus: #7aa2ff;
  --e: #4ade80;
  --m: #fbbf24;
  --a: #f87171;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
  --paper: #1b242c;
  --paper-text: #e9eef2;
  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);
  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); }
.fb-btn { width: auto; display: flex; gap: 6px; padding: 0 12px; font-size: 13px; color: inherit; text-decoration: none; }
.fb-btn[hidden] { display: 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, 340px) 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: 68px; max-height: calc(100vh - 88px); 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 .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; }
.badge { font-size: 10.5px; padding: 0 6px; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; margin-left: 4px; }
.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(290px, 1fr)); gap: 14px 24px; align-items: start; }
.opt { display: grid; gap: 6px; align-content: start; min-width: 0; }
.options { container-type: inline-size; }
@container (min-width: 1000px) { .opt.qlang { padding-left: 24px; } } /* only when it shares a row with Difficulty */
/* wide screens: narrower first column moves Difficulty left; Difficulty (400px) fits BM labels on one row, Question language stays put */
@media (min-width: 1460px) {
  .options { grid-template-columns: 300px 400px minmax(270px, 1fr); }
  .opt.qlang { padding-left: 0; }
}
.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: 74px; 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 9px; 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); }
.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); }
.check input[role="switch"]:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .check input[role="switch"], .check input[role="switch"]::before { transition: none; } }
.check.off { opacity: 0.45; pointer-events: 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; font-size: 14px; flex: 1 1 260px; 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); }
.seed-wrap { position: relative; display: flex; flex: 1 1 260px; min-width: 0; }
.seed-wrap .seed { flex: 1; }
.seed-pop { position: absolute; z-index: 20; top: calc(100% + 8px); right: 0; width: min(420px, 100%); padding: 12px 36px 12px 14px; border: 1px solid var(--a); border-radius: var(--radius); background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-size: 13px; font-weight: 400; line-height: 1.45; }
.seed-pop[hidden] { display: none; }
.seed-pop::before { content: ""; position: absolute; top: -6px; right: 40px; width: 10px; height: 10px; background: var(--surface); border-left: 1px solid var(--a); border-top: 1px solid var(--a); transform: rotate(45deg); }
.seed-pop strong { display: block; color: var(--a); font-size: 14px; margin-bottom: 4px; }
.seed-pop p { margin: 0 0 6px; }
.seed-pop ul { margin: 0; padding-left: 18px; }
.seed-pop li { margin: 2px 0; }
.seed-pop code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
.seed-pop-x { position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border: 0; border-radius: 6px; background: none; color: var(--muted); font-size: 18px; line-height: 1; cursor: pointer; }
.seed-pop-x:hover { background: var(--surface-2); color: var(--text); }
@media print { .seed-pop { display: none !important; } }
.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); }
:root[data-theme="dark"] .btn.primary:hover { color: var(--on-accent); }

/* ------------------------------------------------------------------ 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: 14px; }
.sheet-head h2 { font-size: 1.3em; line-height: 1.25; }
.sheet-head .sub { font-size: 0.92em; opacity: 0.75; margin-top: 2px; }
.sheet .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.72em; opacity: 0.7; }
.sheet-head .code { margin-top: 2px; }
.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.3; } .idline .dt { flex: 1.3; }

ol.questions { list-style: none; margin: 0; padding: 0; }
.q { display: grid; grid-template-columns: 2.1em minmax(0, 1fr); break-inside: avoid; page-break-inside: avoid; }
.q .qn { font-weight: 700; padding-top: 0; }
.q .qb { min-width: 0; }
.q .qt-text { display: block; }
.dtag { display: inline-block; font: 600 0.62em/1.5 system-ui, sans-serif; letter-spacing: 0.03em; padding: 0 6px; border-radius: 99px; border: 1px solid currentColor; vertical-align: 0.15em; margin-right: 6px; text-transform: uppercase; }
.dtag.e { color: var(--e); } .dtag.m { color: var(--m); } .dtag.a { color: var(--a); }
.hide-labels .dtag { display: none; }
.q .space { border-radius: 6px; }
.fmt-normal .q { margin-bottom: 10px; }
.fmt-normal .q .space { border: 1px dashed color-mix(in srgb, currentColor 22%, transparent); margin-top: 6px; }
.fmt-compact .q { margin-bottom: 0; padding: 3px 0 4px; }
.fmt-compact .q + .q { border-top: 1px solid color-mix(in srgb, currentColor 10%, transparent); }
.fmt-compact.cols-2 ol.questions { column-count: 2; column-gap: 9mm; column-rule: 1px solid color-mix(in srgb, currentColor 15%, transparent); }
.fig-wrap { margin: 6px 0 2px; text-align: center; }
.fig-wrap .fig { max-width: 100%; height: auto; color: var(--paper-text); --bg: var(--paper); }
.fig-wrap.left { text-align: left; }
.fig-wrap.inline-2 { display: flex; gap: 16px; justify-content: center; flex-wrap: wrap; }

ol.parts { list-style: none; margin: 4px 0 0; padding: 0; counter-reset: p; }
ol.parts > li { counter-increment: p; position: relative; padding-left: 2em; margin: 2px 0; }
ol.parts > li::before { content: "(" counter(p, lower-alpha) ")"; position: absolute; left: 0; }
ol.aparts { list-style: none; margin: 0; padding: 0; counter-reset: p; }
ol.aparts > li { counter-increment: p; position: relative; padding-left: 1.8em; }
ol.aparts > li::before { content: "(" counter(p, lower-alpha) ")"; position: absolute; left: 0; }

table.qt { border-collapse: collapse; margin: 6px auto; font-size: 0.95em; }
table.qt th, table.qt td { border: 1px solid currentColor; padding: 2px 9px; text-align: center; min-width: 2em; }
table.qt.plain th, table.qt.plain td { border: 0; }
table.qt.left td, table.qt.left th { text-align: left; }
.q .katex { font-size: 1.08em; }
.blank { display: inline-block; min-width: 3em; border-bottom: 1px solid currentColor; height: 1em; vertical-align: baseline; }
.box { display: inline-block; min-width: 1.6em; height: 1.4em; border: 1px solid currentColor; vertical-align: middle; }

/* 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; }
ol.ans-list { list-style: none; margin: 0; padding: 0; column-width: 300px; column-gap: 9mm; }
ol.ans-list > li { display: grid; grid-template-columns: 2.1em minmax(0, 1fr); break-inside: avoid; padding: 3px 0; border-bottom: 1px dotted color-mix(in srgb, currentColor 22%, transparent); }
ol.ans-list .an { font-weight: 700; }
.ans-final { font-weight: 400; }
.ans-work { font-size: 0.88em; opacity: 0.8; margin-top: 1px; }

/* ------------------------------------------------------------- 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: 520px) {
  .options { grid-template-columns: 1fr; }
  .seg button { padding: 6px 10px; }
  .brand h1 { font-size: 15px; }
  .fb-btn span { display: none; }
  .fb-btn { padding: 0; width: 36px; justify-content: center; }
  .idline { flex-direction: column; gap: 8px; }
}

/* ------------------------------------------------------------------ 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-soft: #fff; --accent-strong: #000; --muted: #444;
    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; margin-bottom: 9px; }
  .fmt-normal .q .space { border: 0; }
  .fmt-compact { font-size: 11pt; }
  .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: 8px; }
  .ans-print-title small { font-weight: 400; font-size: 0.65em; margin-left: 8px; opacity: 0.8; }
  .ans-print-title small.code { display: block; margin-left: 0; font-size: 0.5em; }
  .ans-body { margin-top: 0; }
  ol.ans-list { column-count: 2; column-width: auto; font-size: 10.5pt; }
  .dtag { border-color: #000; }
  .fig-wrap .fig { color: #000; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
