/* ---------- Theme tokens ---------- */
:root {
  --font-scale: 1;
  --sidebar-w: 312px;
  --onpage-w: 230px;
  --content-w: 780px;

  --font-body: "Charter", "Bitstream Charter", "Sitka Text", "Iowan Old Style", Cambria, Georgia, serif;
  --font-ui: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "Cascadia Code", "JetBrains Mono", "Fira Code", Consolas, "SFMono-Regular", Menlo, monospace;
}

[data-theme="light"] {
  --bg: #fbfaf7;
  --surface: #ffffff;
  --sidebar-bg: #f4f2ec;
  --border: #e3dfd5;
  --text: #22262b;
  --text-soft: #555c66;
  --text-faint: #8a9099;
  --accent: #0f766e;
  --accent-soft: #e2f3f1;
  --accent-text: #0b5e58;
  --link: #0b6bcb;
  --code-bg: #f6f5f1;
  --code-border: #e6e2d8;
  --inline-code-bg: #efece4;
  --mark: #fde68a;

  --c-key: #0f766e;   --c-key-bg: #eaf6f4;
  --c-trap: #b42318;  --c-trap-bg: #fdf0ee;
  --c-int: #6d28d9;   --c-int-bg: #f3effd;
  --c-sq: #b45309;    --c-sq-bg: #fdf4e7;
  --c-note: #475467;  --c-note-bg: #f2f4f7;
  --c-proj: #1d4ed8;  --c-proj-bg: #edf2fe;

  --hl-keyword: #a626a4; --hl-type: #0b6bcb; --hl-string: #4d7c0f; --hl-number: #b45309;
  --hl-comment: #8a9099; --hl-title: #1e40af; --hl-meta: #a16207; --hl-built: #0f766e;
  --shadow: 0 1px 2px rgba(20, 20, 10, .06), 0 4px 16px rgba(20, 20, 10, .05);
}

[data-theme="dark"] {
  --bg: #111418;
  --surface: #171b21;
  --sidebar-bg: #14181d;
  --border: #262c35;
  --text: #e3e6ea;
  --text-soft: #a9b0ba;
  --text-faint: #6f7782;
  --accent: #2dd4bf;
  --accent-soft: #10302d;
  --accent-text: #5eead4;
  --link: #6cb6ff;
  --code-bg: #0d1014;
  --code-border: #232a33;
  --inline-code-bg: #222831;
  --mark: #6b5b12;

  --c-key: #2dd4bf;   --c-key-bg: #0f2624;
  --c-trap: #f87171;  --c-trap-bg: #2a1616;
  --c-int: #a78bfa;   --c-int-bg: #1f1a33;
  --c-sq: #fbbf24;    --c-sq-bg: #2a2111;
  --c-note: #9aa4b2;  --c-note-bg: #1b2028;
  --c-proj: #7aa2ff;  --c-proj-bg: #151e33;

  --hl-keyword: #c792ea; --hl-type: #82aaff; --hl-string: #a5d6a7; --hl-number: #f78c6c;
  --hl-comment: #6f7782; --hl-title: #82aaff; --hl-meta: #ffcb6b; --hl-built: #5eead4;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { scroll-padding-top: 72px; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; }

.app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  position: fixed; inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 30;
}
.sidebar-head { padding: 18px 18px 12px; border-bottom: 1px solid var(--border); }
.brand { display: flex; gap: 12px; align-items: center; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-mark {
  flex: none; width: 42px; height: 42px; border-radius: 10px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 15px; letter-spacing: .5px;
}
[data-theme="dark"] .brand-mark { color: #06201d; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-title { font-weight: 700; font-size: 15px; line-height: 1.25; }
.brand-sub { font-size: 12px; color: var(--text-soft); margin-top: 2px; line-height: 1.3; }

.progress { display: flex; align-items: center; gap: 10px; margin: 14px 0 12px; }
.progress-bar { flex: 1; height: 6px; border-radius: 99px; background: var(--border); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .3s; }
.progress-label { font-size: 12px; color: var(--text-soft); font-variant-numeric: tabular-nums; }

.search input {
  width: 100%; padding: 9px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: 14px var(--font-ui); outline: none;
}
.search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.toc, .search-results { overflow-y: auto; flex: 1; padding: 8px 10px 40px; }
.toc-part { margin-top: 6px; }
.toc-part-title {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: none; border: 0; cursor: pointer;
  padding: 8px 8px; border-radius: 6px;
  color: var(--text-soft); font-size: 11.5px; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em; text-align: left;
}
.toc-part-title:hover { background: var(--border); }
.toc-part-title .chev { transition: transform .15s; font-size: 10px; width: 10px; }
.toc-part.collapsed .chev { transform: rotate(-90deg); }
.toc-part.collapsed .toc-list { display: none; }
.toc-part-count { margin-left: auto; font-weight: 600; color: var(--text-faint); letter-spacing: 0; }
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-link {
  display: flex; gap: 10px; align-items: baseline;
  padding: 6px 8px 6px 10px; border-radius: 6px;
  color: var(--text); font-size: 14px; line-height: 1.35;
  border-left: 3px solid transparent;
}
.toc-link:hover { background: var(--border); text-decoration: none; }
.toc-link.active { background: var(--accent-soft); color: var(--accent-text); border-left-color: var(--accent); font-weight: 600; }
.toc-num { flex: none; width: 22px; color: var(--text-faint); font-size: 12px; font-variant-numeric: tabular-nums; }
.toc-link.active .toc-num { color: var(--accent-text); }
.toc-title { flex: 1; }
.toc-done { flex: none; color: var(--accent); font-size: 13px; }
.toc-link.missing { color: var(--text-faint); }

.search-results .sr-head { font-size: 12px; color: var(--text-soft); padding: 6px 8px 10px; }
.sr-item { display: block; padding: 10px 10px; border-radius: 8px; color: var(--text); border: 1px solid transparent; }
.sr-item:hover { background: var(--surface); border-color: var(--border); text-decoration: none; }
.sr-title { font-weight: 600; font-size: 14px; }
.sr-meta { font-size: 11.5px; color: var(--text-faint); margin-left: 6px; font-weight: 400; }
.sr-snip { font-size: 12.5px; color: var(--text-soft); margin-top: 4px; line-height: 1.45; }
.sr-snip mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }

/* ---------- Main ---------- */
.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 22px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.crumbs { flex: 1; min-width: 0; font-size: 13px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crumbs b { color: var(--text); font-weight: 600; }
.top-actions { display: flex; gap: 6px; }
.icon-btn {
  height: 34px; min-width: 34px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text);
  cursor: pointer; font-size: 14px;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent-text); }
.menu-btn { display: none; }

.page {
  display: flex; justify-content: center; gap: 48px;
  padding: 40px 32px 96px;
}
.chapter { width: 100%; max-width: var(--content-w); min-width: 0; outline: none; }

/* On-page TOC */
.onpage {
  width: var(--onpage-w); flex: none;
  position: sticky; top: 88px; align-self: flex-start;
  max-height: calc(100vh - 110px); overflow-y: auto;
  font-size: 13px;
}
.onpage-title { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); margin-bottom: 10px; }
.onpage a { display: block; color: var(--text-soft); padding: 4px 0 4px 12px; border-left: 2px solid var(--border); line-height: 1.35; }
.onpage a:hover { color: var(--text); text-decoration: none; }
.onpage a.active { color: var(--accent-text); border-left-color: var(--accent); font-weight: 600; }
.onpage .onpage-actions { margin-top: 18px; display: flex; flex-direction: column; gap: 8px; }

/* ---------- Chapter header / footer ---------- */
.ch-kicker { font-size: 12.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-text); margin-bottom: 10px; }
.ch-meta-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 6px 0 26px; color: var(--text-faint); font-size: 13px; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px; border: 1px solid var(--border); background: var(--surface); color: var(--text-soft); font-size: 12.5px; }
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: 600 13.5px var(--font-ui); cursor: pointer;
}
.btn:hover { border-color: var(--accent); color: var(--accent-text); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
[data-theme="dark"] .btn.primary { color: #06201d; }
.btn.primary:hover { filter: brightness(1.05); color: #fff; }
[data-theme="dark"] .btn.primary:hover { color: #06201d; }
.btn.done { background: var(--accent-soft); color: var(--accent-text); border-color: var(--accent); }

.ch-footer { margin-top: 56px; padding-top: 28px; border-top: 1px solid var(--border); }
.ch-complete { display: flex; justify-content: center; margin-bottom: 28px; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.pager a {
  display: block; padding: 16px 18px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.pager a:hover { border-color: var(--accent); text-decoration: none; }
.pager .dir { font-size: 12px; color: var(--text-faint); text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }
.pager .ttl { margin-top: 4px; font-weight: 600; font-size: 15px; }
.pager .next { text-align: right; grid-column: 2; }
.kbd-hint { text-align: center; margin-top: 18px; color: var(--text-faint); font-size: 12.5px; }
kbd { font: 11.5px var(--font-mono); padding: 1px 6px; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; background: var(--surface); }

/* ---------- Prose ---------- */
.prose {
  font-family: var(--font-body);
  font-size: calc(18px * var(--font-scale));
  line-height: 1.72;
  color: var(--text);
}
.prose h1, .prose h2, .prose h3, .prose h4 { font-family: var(--font-ui); line-height: 1.25; color: var(--text); }
.prose h1 { font-size: 2.05em; margin: 0 0 .2em; letter-spacing: -.01em; }
.prose h2 { font-size: 1.42em; margin: 2.1em 0 .7em; padding-top: .3em; border-top: 1px solid var(--border); }
.prose h3 { font-size: 1.14em; margin: 1.7em 0 .5em; }
.prose h4 { font-size: 1em; margin: 1.4em 0 .4em; }
.prose h2 .anchor, .prose h3 .anchor { opacity: 0; margin-left: 8px; font-size: .8em; color: var(--text-faint); }
.prose h2:hover .anchor, .prose h3:hover .anchor { opacity: 1; }
.prose p { margin: .85em 0; }
.prose ul, .prose ol { padding-left: 1.5em; margin: .7em 0; }
.prose li { margin: .3em 0; }
.prose li > p { margin: .35em 0; }
.prose strong { font-weight: 700; }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2.2em 0; }
.prose img { max-width: 100%; }

.prose :not(pre) > code {
  font-family: var(--font-mono); font-size: .84em;
  background: var(--inline-code-bg); padding: .12em .38em; border-radius: 5px;
  white-space: break-spaces;
}

/* Code blocks */
.codeblock { position: relative; margin: 1.1em 0 1.3em; border: 1px solid var(--code-border); border-radius: 10px; background: var(--code-bg); overflow: hidden; }
.codeblock pre { margin: 0; padding: 16px 18px; overflow-x: auto; font-size: calc(14px * var(--font-scale)); line-height: 1.6; }
.codeblock code { font-family: var(--font-mono); font-size: calc(14px * var(--font-scale)); line-height: 1.6; background: none; font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
.prose code { font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
.code-tools { position: absolute; top: 6px; right: 8px; display: flex; gap: 6px; align-items: center; opacity: .0; transition: opacity .15s; }
.codeblock:hover .code-tools, .code-tools:focus-within { opacity: 1; }
.code-lang { font: 11px var(--font-ui); text-transform: uppercase; letter-spacing: .06em; color: var(--text-faint); }
.copy-btn { font: 600 11.5px var(--font-ui); padding: 3px 9px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text-soft); cursor: pointer; }
.copy-btn:hover { color: var(--accent-text); border-color: var(--accent); }

.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-doctag { color: var(--hl-keyword); }
.hljs-type, .hljs-class .hljs-title, .hljs-title.class_ { color: var(--hl-type); }
.hljs-string, .hljs-regexp, .hljs-addition { color: var(--hl-string); }
.hljs-number, .hljs-symbol, .hljs-bullet { color: var(--hl-number); }
.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-title, .hljs-title.function_, .hljs-section { color: var(--hl-title); }
.hljs-meta, .hljs-meta .hljs-keyword { color: var(--hl-meta); }
.hljs-meta .hljs-string { color: var(--hl-string); }
.hljs-built_in, .hljs-variable.language_, .hljs-attr, .hljs-attribute { color: var(--hl-built); }
.hljs-deletion { color: var(--c-trap); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 1.1em 0 1.4em; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.prose table { border-collapse: collapse; width: 100%; font-family: var(--font-ui); font-size: .82em; line-height: 1.5; }
.prose th, .prose td { padding: 9px 13px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.prose th { background: var(--sidebar-bg); font-weight: 700; color: var(--text); white-space: nowrap; }
.prose tr:last-child td { border-bottom: 0; }
.prose td code, .prose th code { white-space: nowrap; }

/* Blockquotes and callouts */
.prose blockquote {
  margin: 1.2em 0; padding: .7em 1.1em;
  border-left: 4px solid var(--border); background: var(--surface);
  border-radius: 0 10px 10px 0; color: var(--text-soft);
}
.prose blockquote > :first-child { margin-top: 0; }
.prose blockquote > :last-child { margin-bottom: 0; }
.callout { color: var(--text) !important; font-size: .96em; }
.callout-label { display: inline-block; font: 700 11.5px var(--font-ui); text-transform: uppercase; letter-spacing: .07em; margin-right: 6px; }
.callout.key    { border-left-color: var(--c-key);  background: var(--c-key-bg); }  .callout.key .callout-label  { color: var(--c-key); }
.callout.trap   { border-left-color: var(--c-trap); background: var(--c-trap-bg); } .callout.trap .callout-label { color: var(--c-trap); }
.callout.interview { border-left-color: var(--c-int); background: var(--c-int-bg); } .callout.interview .callout-label { color: var(--c-int); }
.callout.squarepoint { border-left-color: var(--c-sq); background: var(--c-sq-bg); } .callout.squarepoint .callout-label { color: var(--c-sq); }
.callout.note   { border-left-color: var(--c-note); background: var(--c-note-bg); } .callout.note .callout-label { color: var(--c-note); }
.callout.projects { border-left-color: var(--c-proj); background: var(--c-proj-bg); } .callout.projects .callout-label { color: var(--c-proj); }

.chapter-meta {
  font-family: var(--font-ui); font-size: .8em !important; line-height: 1.6;
  border-left: 0 !important; border-radius: 12px !important;
  background: var(--surface) !important; border: 1px solid var(--border) !important;
  padding: 14px 18px !important; color: var(--text-soft) !important; margin: 0 0 1.6em !important;
}
.chapter-meta p { margin: .25em 0 !important; }
.chapter-meta strong { color: var(--text); }

/* Answers */
.prose details {
  margin: .6em 0 1.4em; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); font-size: .95em;
}
.prose details > summary {
  cursor: pointer; list-style: none; padding: 9px 14px;
  font: 600 13.5px var(--font-ui); color: var(--accent-text);
  display: flex; align-items: center; gap: 8px; user-select: none;
}
.prose details > summary::-webkit-details-marker { display: none; }
.prose details > summary::before { content: "▸"; transition: transform .15s; display: inline-block; }
.prose details[open] > summary::before { transform: rotate(90deg); }
.prose details[open] > summary { border-bottom: 1px solid var(--border); }
.prose details > :not(summary) { margin-left: 16px; margin-right: 16px; }
.prose details > :nth-child(2) { margin-top: 12px; }
.prose details > :last-child { margin-bottom: 14px; }

mark.hit { background: var(--mark); color: inherit; border-radius: 3px; padding: 0 2px; }

.empty-state { text-align: center; padding: 80px 20px; color: var(--text-soft); font-family: var(--font-ui); }
.empty-state h1 { font-size: 26px; color: var(--text); }

/* ---------- Responsive ---------- */
@media (max-width: 1280px) {
  .onpage { display: none; }
}
@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .main { margin-left: 0; }
  .menu-btn { display: inline-block; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 25; display: none; }
  .scrim.show { display: block; }
  .page { padding: 26px 18px 80px; }
  .prose { font-size: calc(17px * var(--font-scale)); }
}

@media print {
  .sidebar, .topbar, .onpage, .ch-footer, .code-tools { display: none !important; }
  .main { margin: 0; }
  .prose details { border: 0; }
  .prose details > summary { display: none; }
}
