:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --ink: #1b1a17;
  --ink-2: #4a473f;
  --muted: #7a766c;
  --line: #e2ddd2;
  --accent: #8b2f2f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e6e3;
  --gold: #b7791f;
  --gold-soft: #fbf1dc;
  --blue: #2b4c7e;
  --blue-soft: #e4ebf5;
  --green: #2f6b45;
  --green-soft: #e3f0e7;
  --code-bg: #f1eee7;
  --sidebar-w: 268px;
  --font-body: "Source Serif 4", Georgia, "Times New Roman", serif;
  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --surface: #1d1f25;
    --surface-2: #24272f;
    --ink: #ebe8e1;
    --ink-2: #c7c2b6;
    --muted: #8d8a80;
    --line: #2f333c;
    --accent: #e08a7d;
    --accent-ink: #1b1a17;
    --accent-soft: #3a2724;
    --gold: #e0b356;
    --gold-soft: #35301f;
    --blue: #8fb0e6;
    --blue-soft: #222c3d;
    --green: #8ccb9e;
    --green-soft: #1f2f25;
    --code-bg: #262932;
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 17px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.skip { position: absolute; left: -999px; top: 0; background: var(--accent); color: var(--accent-ink); padding: 8px 12px; }
.skip:focus { left: 8px; z-index: 100; }

.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: 100vh; }

/* Sidebar */
.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--line);
  padding: 28px 20px 24px; display: flex; flex-direction: column; gap: 18px;
  font-family: var(--font-ui);
}
.brand { display: flex; flex-direction: column; gap: 2px; color: var(--ink); }
.brand:hover { text-decoration: none; }
.brand-code { font-weight: 700; font-size: 22px; letter-spacing: -0.02em; color: var(--accent); }
.brand-name { font-size: 12.5px; color: var(--muted); font-weight: 500; letter-spacing: 0.02em; }
.nav-toggle { display: none; }
.nav { display: flex; flex-direction: column; gap: 16px; }
.nav-group { display: flex; flex-direction: column; gap: 2px; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 600; padding: 0 10px 6px; }
.nav-link { display: block; padding: 7px 10px; border-radius: 6px; color: var(--ink-2); font-size: 14px; font-weight: 500; line-height: 1.3; }
.nav-link:hover { background: var(--surface-2); text-decoration: none; color: var(--ink); }
.nav-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sidebar-foot { margin-top: auto; font-size: 12px; color: var(--muted); line-height: 1.5; }

/* Main */
.main { padding: 48px 56px 64px; max-width: 900px; width: 100%; min-width: 0; overflow-wrap: break-word; }
.page-head { margin-bottom: 36px; padding-bottom: 28px; border-bottom: 1px solid var(--line); }
.eyebrow { font-family: var(--font-ui); font-size: 12px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent); margin-bottom: 10px; }
h1 { font-size: 40px; line-height: 1.12; margin: 0 0 14px; letter-spacing: -0.015em; font-weight: 700; }
.lede { font-size: 19px; color: var(--ink-2); margin: 0 0 18px; line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-family: var(--font-ui); }
.chips-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600; margin-right: 4px; }
.chip { display: inline-flex; align-items: stretch; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; font-size: 12.5px; font-weight: 600; background: var(--surface); color: var(--ink); }
.chip:hover { text-decoration: none; border-color: var(--accent); }
.chip-lec { padding: 4px 9px 4px 11px; background: var(--accent); color: var(--accent-ink); }
.chip-date { padding: 4px 11px 4px 9px; color: var(--ink-2); }

/* Content typography */
.content h2 { font-size: 27px; margin: 48px 0 14px; letter-spacing: -0.01em; line-height: 1.2; padding-top: 6px; }
.content h2:first-child { margin-top: 0; }
.content h3 { font-size: 20px; margin: 30px 0 10px; }
.content p { margin: 0 0 16px; }
.content ul, .content ol { margin: 0 0 18px; padding-left: 26px; }
.content li { margin-bottom: 6px; }
.content li > ul, .content li > ol { margin-top: 6px; margin-bottom: 6px; }
.content strong { font-weight: 600; }
.content code { font-family: var(--font-mono); font-size: 0.86em; background: var(--code-bg); padding: 1px 6px; border-radius: 4px; }
.content pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 14px 16px; overflow-x: auto; font-family: var(--font-mono); font-size: 13.5px; line-height: 1.55; margin: 0 0 18px; }
.content pre code { background: none; padding: 0; font-size: inherit; }
.content hr { border: 0; border-top: 1px solid var(--line); margin: 36px 0; }
.muted { color: var(--muted); }
.mono { font-family: var(--font-mono); font-size: 0.85em; overflow-wrap: anywhere; }
.small { font-size: 0.9em; }

/* Section tag (which lecture a subsection comes from) */
.sec-tag { display: inline-block; font-family: var(--font-ui); font-size: 11.5px; font-weight: 600; color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; margin-left: 10px; vertical-align: middle; white-space: nowrap; }

/* Callouts */
.callout { border-left: 4px solid var(--line); background: var(--surface); padding: 14px 18px; border-radius: 0 8px 8px 0; margin: 0 0 20px; }
.callout p:last-child, .callout ul:last-child { margin-bottom: 0; }
.callout.key { border-color: var(--accent); background: var(--accent-soft); }
.callout.note { border-color: var(--blue); background: var(--blue-soft); }
.callout.warn { border-color: var(--gold); background: var(--gold-soft); }
.callout.ex { border-color: var(--green); background: var(--green-soft); }
.callout-title { font-family: var(--font-ui); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; margin-bottom: 6px; }
.key .callout-title { color: var(--accent); }
.note .callout-title { color: var(--blue); }
.warn .callout-title { color: var(--gold); }
.ex .callout-title { color: var(--green); }

/* Formula cards */
.formula { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 14px 18px 8px; margin: 0 0 20px; overflow-x: auto; }
.formula-name { font-family: var(--font-ui); font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 4px; }
.katex-display { margin: 10px 0 14px; overflow-x: auto; overflow-y: hidden; }
.content :not(.katex-display) > .katex { display: inline-block; max-width: 100%; overflow-x: auto; overflow-y: hidden; vertical-align: middle; padding-block: 0.15em; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 0 20px; }
table { border-collapse: collapse; width: 100%; font-size: 15px; font-family: var(--font-ui); background: var(--surface); }
th, td { border: 1px solid var(--line); padding: 8px 11px; text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-weight: 600; font-size: 13px; letter-spacing: 0.02em; }
td.num, th.num { text-align: right; font-family: var(--font-mono); font-size: 13.5px; }
tr.hl td { background: var(--gold-soft); }

/* Index course map */
.map { display: grid; gap: 22px; margin-bottom: 8px; }
.map-part { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 20px 22px; }
.map-part-label { font-family: var(--font-ui); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; color: var(--accent); margin-bottom: 4px; }
.map-part h2 { margin: 0 0 6px; font-size: 22px; }
.map-part > p { color: var(--ink-2); margin-bottom: 14px; }
.map-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.map-item { display: grid; grid-template-columns: 30px 1fr auto; gap: 12px; align-items: start; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); }
.map-item:hover { background: var(--accent-soft); }
.map-item .n { font-family: var(--font-ui); font-weight: 700; color: var(--accent); font-size: 14px; padding-top: 2px; }
.map-item .t { font-weight: 600; font-size: 17px; }
.map-item .d { color: var(--muted); font-size: 14.5px; display: block; margin-top: 2px; }
.map-item .lecs { display: flex; gap: 5px; flex-wrap: wrap; justify-content: flex-end; font-family: var(--font-ui); }
.mini { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); color: var(--ink-2); white-space: nowrap; }

.flow { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin: 0 0 22px; font-family: var(--font-ui); }
.flow-step { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; position: relative; }
.flow-step .k { font-size: 11px; font-weight: 700; color: var(--accent); letter-spacing: 0.08em; text-transform: uppercase; }
.flow-step .v { font-weight: 600; font-size: 15px; margin: 3px 0 2px; }
.flow-step .s { font-size: 13px; color: var(--muted); line-height: 1.4; }

/* Lecture timeline */
.lec-grid { display: grid; gap: 18px; }
.lec-card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 20px 22px; }
.lec-head { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--font-ui); }
.lec-num { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.1em; color: var(--accent); }
.lec-date { font-size: 14px; color: var(--muted); font-weight: 500; }
.lec-card h2 { margin: 6px 0 4px; font-size: 22px; }
.topic-list { margin: 0; }

/* Glossary */
.gloss { display: grid; gap: 10px; }
.gloss-item { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 16px; }
.gloss-item .term { font-weight: 700; font-family: var(--font-ui); font-size: 15px; }
.gloss-item .def { margin: 4px 0 0; }
.gloss-item .where { font-family: var(--font-ui); font-size: 12.5px; color: var(--muted); margin-top: 4px; }

/* Pager */
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 56px; padding-top: 24px; border-top: 1px solid var(--line); font-family: var(--font-ui); }
.pager-link { display: flex; flex-direction: column; gap: 3px; padding: 14px 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--ink); }
.pager-link:hover { border-color: var(--accent); text-decoration: none; }
.pager-link.next { text-align: right; }
.pager-dir { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; }
.pager-title { font-weight: 600; font-size: 15px; }
.foot { margin-top: 40px; font-family: var(--font-ui); font-size: 12.5px; color: var(--muted); line-height: 1.5; }

/* Responsive */
@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: relative; height: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 16px 18px; flex-direction: row; flex-wrap: wrap; align-items: center; gap: 12px; }
  .nav-toggle { display: inline-block; margin-left: auto; font: 600 13px var(--font-ui); padding: 7px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--ink); cursor: pointer; }
  .nav { display: none; width: 100%; }
  .nav.open { display: flex; }
  .sidebar-foot { display: none; }
  .main { padding: 28px 20px 48px; }
  h1 { font-size: 31px; }
  .lede { font-size: 17px; }
  .content h2 { font-size: 23px; }
  .sec-tag { display: block; margin: 8px 0 0; white-space: normal; }
  .map-item { grid-template-columns: 26px 1fr; }
  .map-item .lecs { grid-column: 2; justify-content: flex-start; }
  .pager { grid-template-columns: 1fr; }
}

/* Language switch + Mandarin typography */
.lang-switch { display: inline-block; align-self: flex-start; font-family: var(--font-ui); font-size: 12.5px; font-weight: 600; padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; color: var(--ink-2); background: var(--surface-2); }
.lang-switch:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
body.lang-zh { --font-body: "Source Serif 4", "Noto Serif SC", "Songti SC", "SimSun", Georgia, serif; --font-ui: "Inter", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif; }
body.lang-zh .content { line-height: 1.75; }
body.lang-zh h1 { letter-spacing: 0; }
@media (max-width: 900px) {
  .lang-switch { align-self: center; }
}
