:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --ink: #1f2328;
  --muted: #6b7280;
  --line: #e3e1dc;
  --card: #fbfaf8;
  --accent: #5b6b8a;
  --accent-ink: #ffffff;
  --done: #4f8a5b;
  --user: #e9ecf2;
  --danger: #a24b4b;
  color-scheme: light;
}
:root:not([data-theme="light"]) {
  @media (prefers-color-scheme: dark) {
    --bg: #17191d; --panel: #1f2227; --ink: #e6e6e3; --muted: #9aa0a8; --line: #2e323a;
    --card: #24272d; --accent: #8b9bbf; --accent-ink: #14161a; --done: #6fae7c; --user: #2b3140; --danger: #d07474;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17191d; --panel: #1f2227; --ink: #e6e6e3; --muted: #9aa0a8; --line: #2e323a;
  --card: #24272d; --accent: #8b9bbf; --accent-ink: #14161a; --done: #6fae7c; --user: #2b3140; --danger: #d07474;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.6 "IBM Plex Sans", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  display: flex; flex-direction: column; height: 100vh; overflow: hidden;
}
button { font: inherit; color: inherit; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
button:hover { border-color: var(--accent); }
button.icon { width: 36px; height: 36px; padding: 0; font-size: 18px; line-height: 1; display: inline-flex; align-items: center; justify-content: center; flex: none; }
button.icon svg { display: block; }
button.small { padding: 3px 9px; font-size: 13px; }
button.danger { color: var(--danger); }
.spacer { flex: 1; }

#top {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  /* room on the right for the floating account button (account.js pins it at top:12px right:12px),
     which otherwise sits on top of the chat and settings buttons */
  padding-right: 96px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: none;
}
/* signed in, that button shows a name and level, so leave more room where the header can spare it */
@media (min-width: 700px) { #top { padding-right: 190px; } }
#crumb { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#progressPill { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 1px 7px; white-space: nowrap; font-variant-numeric: tabular-nums; }

#layout { display: flex; flex: 1; min-height: 0; }

#nav { width: 300px; flex: none; overflow-y: auto; background: var(--panel); border-right: 1px solid var(--line); padding: 8px 0 40px; }
#nav.hidden { display: none; }
.phase { margin: 4px 0; }
.phaseHead { padding: 10px 14px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); display: flex; align-items: center; gap: 8px; cursor: pointer; }
.phaseHead .count { margin-left: auto; font-size: 11px; }
.phase.collapsed .chapList { display: none; }
.chapItem { display: flex; align-items: center; gap: 8px; padding: 6px 14px 6px 18px; cursor: pointer; font-size: 14px; border-left: 3px solid transparent; }
.chapItem:hover { background: var(--card); }
.chapItem.active { border-left-color: var(--accent); background: var(--card); }
.chapItem .num { color: var(--muted); font-size: 12px; width: 26px; flex: none; }
.chapItem .tick { margin-left: auto; color: var(--done); flex: none; }
.chapItem .zh { color: var(--muted); font-size: 12px; margin-left: 4px; }

#main { flex: 1; overflow-y: auto; padding: 24px 32px 80px; min-width: 0; }
#main h1 { margin: 0 0 8px; font-size: 28px; font-family: "Source Serif 4", "Noto Serif SC", Georgia, serif; font-weight: 600; letter-spacing: -0.01em; text-wrap: balance; }
.card h3, .phaseHead { font-variant-numeric: tabular-nums; }
.lead { color: var(--muted); max-width: 720px; }
#homeGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-top: 20px; }
.homeCard { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; cursor: pointer; }
.homeCard:hover { border-color: var(--accent); }
.homeCard h3 { margin: 0 0 4px; font-size: 15px; }
.homeCard p { margin: 0; color: var(--muted); font-size: 13px; }
.homeCard .bar { height: 4px; background: var(--line); border-radius: 2px; margin-top: 10px; overflow: hidden; }
.homeCard .bar i { display: block; height: 100%; background: var(--done); }

article { max-width: 820px; }
article h1 { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
article h1 .zh { color: var(--muted); font-size: 18px; font-weight: 500; }
.chapMeta { display: flex; gap: 10px; align-items: center; margin: 4px 0 18px; flex-wrap: wrap; }
.chapMeta .tag { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 1px 9px; }
.intro { color: var(--ink); margin-bottom: 20px; }
h2.sec { font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 10px; }

.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 14px; margin: 10px 0; position: relative;
}
.card.pt { cursor: pointer; }
.card.pt:hover { border-color: var(--accent); }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.card .body p:first-child { margin-top: 0; }
.card .body p:last-child { margin-bottom: 0; }
.card .ask { position: absolute; top: 8px; right: 8px; font-size: 12px; padding: 2px 8px; opacity: 0; transition: opacity .15s; }
.card:hover .ask { opacity: 1; }
.card.ex .q { font-weight: 500; }
.card.ex details { margin-top: 8px; }
.card.ex summary, .card.pr summary { cursor: pointer; color: var(--accent); font-size: 13px; }
.card.pr .q { margin-bottom: 8px; }
.card.pr .work { display: flex; gap: 8px; align-items: flex-start; margin-top: 6px; }
.card.pr textarea { flex: 1; font: inherit; font-size: 14px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: inherit; resize: vertical; min-height: 34px; }
.card.pr .ans { margin-top: 8px; padding: 8px 10px; background: var(--card); border-radius: 8px; }
.card.pr.doneQ { border-color: var(--done); }
.card .num { color: var(--muted); font-size: 12px; margin-right: 6px; }
.chapFoot { display: flex; gap: 10px; align-items: center; margin-top: 36px; padding-top: 18px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.chapFoot button.done { background: var(--done); color: #fff; border-color: var(--done); }
code { background: var(--card); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
table { border-collapse: collapse; margin: 8px 0; }
td, th { border: 1px solid var(--line); padding: 4px 10px; font-size: 14px; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

#chat { width: 380px; flex: none; display: flex; flex-direction: column; background: var(--panel); border-left: 1px solid var(--line); }
#chat.hidden { display: none; }
#chatHead { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex: none; }
#chatCtx { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 160px; }
#msgs { flex: 1; overflow-y: auto; padding: 12px; }
.msg { margin: 0 0 12px; font-size: 14px; }
.msg.user { background: var(--user); border-radius: 12px 12px 2px 12px; padding: 8px 12px; margin-left: 30px; white-space: pre-wrap; }
.msg.bot { padding: 0 2px; }
.msg.bot p:first-child { margin-top: 0; }
.msg.bot p:last-child { margin-bottom: 0; }
.msg.bot.thinking { color: var(--muted); font-style: italic; }
.msg.err { color: var(--danger); font-size: 13px; }
.msg.user .quote { display: block; font-size: 12px; color: var(--muted); border-left: 2px solid var(--line); padding-left: 8px; margin-bottom: 4px; max-height: 80px; overflow: hidden; white-space: normal; }
#noKey { padding: 10px 12px; font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); }
#composer { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); flex: none; }
#input { flex: 1; font: inherit; font-size: 14px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: inherit; resize: none; }
#send { width: 40px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-size: 18px; }
#send.stop { background: var(--danger); border-color: var(--danger); }

#askSel { position: fixed; z-index: 50; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-size: 13px; padding: 4px 10px; box-shadow: 0 4px 12px rgba(0,0,0,.2); }

#settings { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(460px, 92vw); }
.modal h2 { margin: 0 0 12px; font-size: 18px; }
.modal label { display: block; margin: 12px 0 4px; font-size: 13px; color: var(--muted); }
.modal input, .modal select { display: block; width: 100%; margin-top: 4px; font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: inherit; }
.modal .hint { font-size: 12px; color: var(--muted); margin: 4px 0 0; }
.modal .row { display: flex; align-items: center; gap: 8px; margin-top: 18px; }
.modal .ok { color: var(--done); font-size: 13px; }

@media (max-width: 900px) {
  #nav { position: absolute; left: 0; top: 52px; bottom: 0; z-index: 20; box-shadow: 4px 0 16px rgba(0,0,0,.15); }
  #chat { position: absolute; right: 0; top: 52px; bottom: 0; z-index: 20; width: min(420px, 100vw); box-shadow: -4px 0 16px rgba(0,0,0,.15); }
  #main { padding: 16px 16px 80px; }
}

/* ---------- worksheet ---------- */
#wsDialog { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: center; justify-content: center; z-index: 100; }
#sheet { position: fixed; inset: 0; z-index: 90; background: #e9e8e4; overflow-y: auto; color: #111; }
#sheetBar { position: sticky; top: 0; z-index: 2; display: flex; gap: 10px; align-items: center; padding: 10px 16px; background: var(--panel); color: var(--ink); border-bottom: 1px solid var(--line); }
/* same clearance as the header, so "back to chapter" is not under the account button */
#sheetBar { padding-right: 96px; }
@media (min-width: 700px) { #sheetBar { padding-right: 190px; } }
#sheetPages { padding: 24px 0 60px; }
.page {
  background: #fff; color: #111; width: 210mm; min-height: 297mm; margin: 0 auto 20px; padding: 16mm 18mm;
  box-shadow: 0 2px 14px rgba(0,0,0,.12); font-family: "IBM Plex Sans", "Noto Sans SC", system-ui, sans-serif; font-size: 10.5pt; line-height: 1.45;
}
.page h1 { font-family: "Source Serif 4", Georgia, serif; font-size: 16pt; margin: 0 0 1mm; }
.page .sub { color: #555; font-size: 9pt; margin-bottom: 4mm; display: flex; gap: 16px; flex-wrap: wrap; }
.page .nameLine { display: flex; gap: 24px; margin-bottom: 5mm; font-size: 9.5pt; }
.page .nameLine span { flex: 1; border-bottom: 1px solid #333; padding-bottom: 2px; }
.page .formulas { border: 1px solid #bbb; padding: 2mm 4mm; margin-bottom: 5mm; font-size: 9pt; background: #fafaf8; }
.page .formulas b { display: block; font-size: 9.5pt; text-transform: uppercase; letter-spacing: .06em; color: #555; margin-bottom: 1mm; }
.page .qs { display: grid; grid-template-columns: 1fr 1fr; gap: 6mm 8mm; }
.page .qs.one { grid-template-columns: 1fr; }
.page .wq { break-inside: avoid; border: 1px solid #ccc; padding: 2.5mm 3.5mm; min-height: 62mm; display: flex; flex-direction: column; }
.page .wq.tall { min-height: 95mm; }
.page .wq .n { font-weight: 600; color: #333; font-size: 9pt; margin-bottom: 0.5mm; }
.page .wq .qt { margin-bottom: 2mm; }
.page .wq .space { flex: 1; }
.page.key { break-before: page; }
.page.key ol { padding-left: 1.5em; columns: 2; column-gap: 10mm; }
.page.key li { break-inside: avoid; margin-bottom: 2mm; font-size: 9.5pt; }
.page .foot { margin-top: 8mm; font-size: 9pt; color: #777; text-align: right; }
#sheetPages .katex { font-size: 1em; color: #111; }

@media print {
  @page { size: A4; margin: 0; }
  html, body { height: auto !important; overflow: visible !important; display: block !important; background: #fff !important; }
  body > *:not(#sheet) { display: none !important; }
  #sheet { position: static; background: #fff; overflow: visible; }
  #sheetBar { display: none; }
  #sheetPages { padding: 0; }
  .page { width: auto; min-height: 0; margin: 0; box-shadow: none; padding: 14mm 16mm; }
  .page + .page { break-before: page; }
}
@media (max-width: 900px) { .page { width: 100%; min-height: 0; padding: 20px; } .page .qs { grid-template-columns: 1fr; } }
