:root {
  --bg: #ffffff;
  --side: #f6f7f8;
  --text: #1b1f23;
  --muted: #5c6670;
  --line: #e3e5e8;
  --accent: #1a56b0;
  --soft: #eaf1fb;
  --math: #f5f7fa;
  --hh: 3rem;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141618; --side: #191c1f; --text: #e3e5e8; --muted: #98a2ad;
    --line: #2a2f35; --accent: #8db4f0; --soft: #1c2533; --math: #1a1e22;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141618; --side: #191c1f; --text: #e3e5e8; --muted: #98a2ad;
  --line: #2a2f35; --accent: #8db4f0; --soft: #1c2533; --math: #1a1e22;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { scroll-padding-top: calc(var(--hh) + .5rem); -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.6 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button, input { font: inherit; color: inherit; }

/* header */
.top {
  position: sticky; top: 0; z-index: 30; background: var(--bg); border-bottom: 1px solid var(--line);
  display: flex; flex-wrap: wrap; align-items: center; gap: .4rem .75rem; padding: .45rem 16px;
}
.brand { margin: 0; font-size: 1rem; font-weight: 400; line-height: 1.3; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand b { font-weight: 700; }
.brand span { color: var(--muted); }
.other { margin-right: auto; font-size: .9rem; }
.other a { text-decoration: none; }
.other a:hover { text-decoration: underline; }
.tools { display: flex; gap: .4rem; align-items: center; }
.tools input { width: 17rem; padding: .3rem .6rem; font-size: .9rem; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.tools button, #menu { padding: .3rem .6rem; font-size: .85rem; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); cursor: pointer; white-space: nowrap; }
.tools button:hover, #menu:hover { border-color: var(--accent); }
#menu { display: none; font-size: 1rem; line-height: 1.2; }
#theme { min-width: 2.2rem; }

/* layout */
.shell { display: grid; grid-template-columns: 290px minmax(0, 1fr); align-items: start; }
.side {
  position: sticky; top: var(--hh); height: calc(100vh - var(--hh)); overflow-y: auto;
  padding: .75rem 8px 2rem; background: var(--side); border-right: 1px solid var(--line); scrollbar-width: thin;
}
.lecs, .qs { list-style: none; margin: 0; padding: 0; }
.lec-link { display: flex; gap: .5rem; padding: .3rem .5rem; border-radius: 5px; color: var(--text); text-decoration: none; line-height: 1.3; font-size: .9rem; }
.lec-link:hover { background: var(--soft); }
.ln { flex: none; width: 1.4rem; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.lt small { display: block; color: var(--muted); font-size: .75rem; }
.lec-item[data-active] > .lec-link { background: var(--soft); color: var(--accent); font-weight: 600; }
.lec-item[data-active] > .lec-link small { font-weight: 400; }
.qs { display: none; margin: .15rem 0 .5rem 1.9rem; font-size: .8rem; line-height: 1.35; }
.lec-item[data-active] > .qs { display: block; }
.qs a { display: block; padding: .2rem .4rem; color: var(--muted); text-decoration: none; border-radius: 4px; }
.qs a:hover { color: var(--text); background: var(--soft); }
.qs a span { font-variant-numeric: tabular-nums; }
.scrim { position: fixed; inset: 0; z-index: 35; background: rgba(0,0,0,.4); }

main { padding: .5rem 24px 3rem; min-width: 0; max-width: 54rem; }
.lec-head { padding: .9rem 0 .4rem; }
.lec-head p { margin: 0; color: var(--muted); font-size: .82rem; }
.lec-head h2 { margin: 0; font-size: 1.35rem; line-height: 1.3; }

/* questions */
.q { margin: 1.1rem 0; }
.q > summary { display: flex; gap: .45rem; padding: .55rem .75rem; cursor: pointer; list-style: none; font-weight: 650; line-height: 1.4; background: var(--soft); color: var(--text); border-radius: 6px; }
.q > summary::-webkit-details-marker { display: none; }
.q > summary::after { content: "−"; margin-left: auto; padding-left: .5rem; color: var(--muted); font-weight: 400; }
.q:not([open]) > summary::after { content: "+"; }
.qn { flex: none; min-width: 1.6rem; color: var(--accent); font-variant-numeric: tabular-nums; }
.a { margin: .6rem 0 0 .75rem; padding: 0 0 .2rem 1rem; border-left: 3px solid var(--line); overflow-wrap: anywhere; }
.a > :first-child { margin-top: 0; }
.a > :last-child { margin-bottom: 0; }
.a p { margin: .5rem 0; }
.a ul, .a ol { margin: .4rem 0; padding-left: 1.3rem; }
.a li { margin: .15rem 0; }
.a li::marker { color: var(--muted); }
.a h3, .a h4, .a h5 { margin: 1rem 0 .3rem; font-size: 1rem; }
.a hr { border: 0; border-top: 1px dashed var(--line); margin: 1rem 0; }
.a table { border-collapse: collapse; margin: .5rem 0; font-size: .92rem; display: block; overflow-x: auto; }
.a th, .a td { border: 1px solid var(--line); padding: .25rem .5rem; text-align: left; }
.a blockquote { margin: .5rem 0; padding-left: .8rem; border-left: 3px solid var(--line); color: var(--muted); }
.a code { font-size: .9em; }
.a .katex-error { color: #c0392b; }
.a li.fig { list-style: none; }
figure { margin: .6rem 0; }
.a img { max-width: 100%; height: auto; border: 1px solid var(--line); border-radius: 4px; background: #fff; vertical-align: middle; }

/* formulas */
.katex { font-size: 1.05em; }
.katex-display { margin: .5rem 0; padding: .35rem .6rem; background: var(--math); border-radius: 4px; overflow-x: auto; overflow-y: hidden; }
.katex-display > .katex { white-space: nowrap; }

footer { margin-top: 2rem; padding-top: .8rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .8rem; }
#empty { color: var(--muted); padding: 2rem 0; }

@media (max-width: 899px) {
  .shell { display: block; }
  #menu { display: block; }
  .side {
    position: fixed; top: 0; left: 0; z-index: 40; width: min(320px, 86vw); height: 100vh; height: 100dvh;
    transform: translateX(-100%); transition: transform .2s, visibility .2s; visibility: hidden;
  }
  .side.open { transform: none; visibility: visible; }
  main { padding: .25rem 16px 3rem; max-width: none; }
}
@media (max-width: 640px) {
  .top { padding: .4rem 12px; }
  .brand { flex: 1; }
  .brand span { display: none; }
  .other { margin-right: 0; }
  .tools { width: 100%; }
  .tools input { flex: 1; width: auto; min-width: 0; }
  main { padding: .25rem 12px 3rem; }
  .a { padding-left: 0; }
  .lec-head h2 { font-size: 1.2rem; }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none; } }
@media print {
  .top, .side, .scrim, footer, #empty { display: none !important; }
  .shell { display: block; }
  main { padding: 0; max-width: none; }
  .lec[hidden] { display: block; }
  .q { break-inside: avoid; }
  .q > summary::after { content: none; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .katex-display { background: none; }
}
