/* ---------- tokens ---------- */
:root {
  --bg: #efebe2;
  --surface: #fffdf8;
  --surface-2: #f6f2e9;
  --ink: #1c2630;
  --muted: #5d6874;
  --line: #e0d9cb;
  --accent: #0f6b62;
  --accent-ink: #ffffff;
  --accent-soft: #d9eee9;
  --clinical: #fdf1dc;
  --clinical-line: #d9962b;
  --danger: #c0392b;
  --shadow: 0 6px 24px rgba(30, 30, 20, .14);
  --hl-yellow: #ffe45c; --hl-green: #a5e8a0; --hl-pink: #ffb3cf; --hl-blue: #a9d6ff; --hl-orange: #ffc98a;
  --hl-mix: 62%;
  --fs: 1;
  --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, "Iowan Old Style", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  --top: 52px;
  --side: 280px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1418;
  --surface: #171e24;
  --surface-2: #1e272f;
  --ink: #e4e8eb;
  --muted: #97a3ae;
  --line: #2b3640;
  --accent: #55cdbc;
  --accent-ink: #06231f;
  --accent-soft: #17332f;
  --clinical: #2a2314;
  --clinical-line: #c98a2a;
  --danger: #ff7b6b;
  --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  --hl-mix: 42%;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font-family: var(--sans); font-size: 16px;
  overflow: hidden; overflow: clip; -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.spacer { flex: 1; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--top); z-index: 40;
  display: flex; align-items: center; gap: 2px; padding: 0 8px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.ibtn {
  position: relative; width: 42px; height: 42px; border-radius: 10px;
  display: grid; place-items: center; color: var(--muted);
}
.ibtn:hover { background: var(--surface-2); color: var(--ink); }
.ibtn.txt { font-weight: 650; font-size: 14px; letter-spacing: -.02em; }
.ibtn.sm { width: 34px; height: 34px; }
.ibtn .badge {
  position: absolute; top: 4px; right: 3px; min-width: 17px; height: 17px; padding: 0 4px;
  border-radius: 9px; background: var(--accent); color: var(--accent-ink);
  font-size: 11px; font-weight: 700; line-height: 17px; text-align: center;
}
.crumb { display: flex; align-items: baseline; gap: 10px; min-width: 0; padding-left: 6px; }
.crumb-book { font-weight: 700; color: var(--accent); white-space: nowrap; }
.crumb-ch { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- sidebar ---------- */
.sidebar {
  position: fixed; top: var(--top); bottom: 0; left: 0; width: var(--side); z-index: 30;
  background: var(--surface); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 12px 10px 120px; transition: transform .22s ease;
}
body.nav-closed .sidebar { transform: translateX(-100%); }
.search {
  display: flex; align-items: center; gap: 8px; padding: 0 10px; height: 40px; margin-bottom: 10px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; color: var(--muted);
}
.search input { flex: 1; min-width: 0; border: 0; background: none; outline: 0; font: inherit; color: var(--ink); }
.toc, .toc ul, .results { list-style: none; margin: 0; padding: 0; }
.toc a {
  display: flex; gap: 10px; align-items: baseline; padding: 9px 10px; border-radius: 8px;
  color: var(--ink); text-decoration: none; font-weight: 600; font-size: 15px;
}
.toc a:hover { background: var(--surface-2); }
.toc .num { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; width: 12px; }
.toc ul { display: none; margin: 0 0 6px 31px; border-left: 2px solid var(--line); }
.toc li.on > ul { display: block; }
.toc ul a { font-weight: 400; font-size: 14px; color: var(--muted); padding: 6px 10px; border-radius: 0 8px 8px 0; }
.toc li.on > a { background: var(--accent-soft); color: var(--accent); }
.results li a { display: block; padding: 9px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; font-size: 14px; line-height: 1.4; }
.results li a:hover { background: var(--surface-2); }
.results small { display: block; color: var(--accent); font-weight: 650; font-size: 12px; margin-bottom: 2px; }
.results mark { background: var(--hl-yellow); color: #1c2630; border-radius: 2px; }
.results .empty { padding: 14px 10px; color: var(--muted); font-size: 14px; }

/* ---------- reading area ---------- */
#scroller {
  position: fixed; top: var(--top); bottom: 0; left: var(--side); right: 0;
  overflow-y: auto; overflow-x: hidden; outline: 0; scroll-behavior: auto;
  transition: left .22s ease, right .22s ease;
}
body.nav-closed #scroller { left: 0; }
body.panel-open #scroller { right: 340px; }
.chapter {
  position: relative; max-width: 820px; margin: 28px auto 190px; padding: 44px 56px 28px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04);
  font-family: var(--serif); font-size: calc(18px * var(--fs)); line-height: 1.62;
}
.chapter[hidden] { display: none; }
.eyebrow { font-family: var(--sans); font-size: 12px; font-weight: 700; letter-spacing: .14em; text-transform: uppercase; color: var(--accent); margin: 0 0 6px; }
.chapter h1 { font-size: 2em; line-height: 1.15; margin: 0 0 .7em; letter-spacing: -.01em; text-wrap: balance; }
.chapter h2 { font-family: var(--sans); font-size: 1.22em; line-height: 1.25; margin: 2em 0 .6em; padding-bottom: .3em; border-bottom: 2px solid var(--accent); color: var(--accent); scroll-margin-top: 16px; }
.chapter h3 { font-family: var(--sans); font-size: 1.05em; margin: 1.7em 0 .4em; }
.chapter h4 { font-family: var(--sans); font-size: .95em; margin: 1.3em 0 .3em; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.chapter p { margin: 0 0 .9em; }
.chapter ul, .chapter ol { margin: 0 0 .9em; padding-left: 1.35em; }
.chapter li { margin: .28em 0; }
.chapter li > ul, .chapter li > ol { margin: .25em 0 .35em; }
.chapter ul > li::marker { color: var(--accent); }
.chapter ol > li::marker { color: var(--accent); font-family: var(--sans); font-weight: 700; font-size: .85em; }
.chapter strong { font-weight: 700; }
.chapter sub, .chapter sup { font-size: .72em; line-height: 0; }
[dir="rtl"], .chapter :lang(ar) { font-family: "Noto Naskh Arabic", "Segoe UI", Tahoma, sans-serif; }

/* figures */
.fig { margin: 1.5em 0; text-align: center; }
.fig img { max-width: 100%; height: auto; border-radius: 6px; background: #fff; border: 1px solid var(--line); cursor: zoom-in; }
.fig figcaption { font-family: var(--sans); font-size: .78em; line-height: 1.45; color: var(--muted); margin-top: .55em; text-wrap: balance; }
.fig-small img { max-width: min(100%, 320px); }
.fig-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; align-items: end; margin: 1.5em 0; }
.fig-row .fig { margin: 0; }
.fig-row img { max-height: 420px; }

/* tables */
.table-wrap { overflow-x: auto; margin: 1.4em 0; border: 1px solid var(--line); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-family: var(--sans); font-size: .82em; line-height: 1.45; }
th, td { padding: 10px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); }
th:last-child, td:last-child { border-right: 0; }
tr:last-child td { border-bottom: 0; }
th { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.cell-img { text-align: center; background: #fff; }
.cell-img img { max-width: 100%; max-height: 150px; }

/* boxes */
.box { margin: 1.8em 0; padding: 18px 22px 8px; border-radius: 8px; background: var(--surface-2); border-left: 4px solid var(--accent); }
.box-clinical { background: var(--clinical); border-left-color: var(--clinical-line); }
.box-title { font-family: var(--sans); font-weight: 800; font-size: .8em; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); margin-bottom: .8em !important; }
.box-clinical .box-title { color: var(--clinical-line); }
.box h3 { margin-top: .9em; }

/* tree diagram */
.tree { margin: 1.4em 0; overflow-x: auto; font-family: var(--sans); font-size: .8em; line-height: 1.3; padding-bottom: 4px; }
.tree ul { list-style: none; display: flex; justify-content: center; margin: 0; padding: 18px 0 0; position: relative; }
.tree > ul { padding-top: 0; min-width: max-content; }
.tree li { position: relative; padding: 18px 6px 0; margin: 0; text-align: center; }
.tree > ul > li { padding-top: 0; }
.tree li::before, .tree li::after { content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 18px; border-top: 2px solid var(--line); }
.tree li::after { right: auto; left: 50%; border-left: 2px solid var(--line); }
.tree li:only-child::before, .tree li:only-child::after, .tree > ul > li::before, .tree > ul > li::after { display: none; }
.tree li:only-child { padding-top: 0; }
.tree li:first-child::before, .tree li:last-child::after { border-top: 0; }
.tree li:last-child::before { border-right: 2px solid var(--line); border-radius: 0 6px 0 0; }
.tree li:first-child::after { border-radius: 6px 0 0 0; }
.tree ul ul::before { content: ""; position: absolute; top: 0; left: 50%; height: 18px; border-left: 2px solid var(--line); }
.tree span { display: inline-block; padding: 8px 12px; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--line); max-width: 150px; }
.tree > ul > li > span { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; max-width: none; }
.tree > ul > li > ul > li > span { background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 650; }

/* flow */
.flow { list-style: none; padding: 0 !important; margin: 1.3em auto !important; max-width: 430px; font-family: var(--sans); font-size: .85em; }
.flow li { margin: 0 0 26px; padding: 10px 14px; text-align: center; border-radius: 8px; background: var(--accent-soft); color: var(--accent); font-weight: 650; position: relative; }
.flow li:not(:last-child)::after { content: "↓"; position: absolute; left: 0; right: 0; bottom: -25px; color: var(--muted); font-size: 1.1em; }
.flow li:last-child { margin-bottom: 0; }

/* timeline */
.timeline { list-style: none; padding: 0 !important; margin: 1em 0 1.4em !important; border-left: 2px solid var(--line); }
.timeline li { display: grid; grid-template-columns: 6.2em 1fr; gap: 12px; padding: .28em 0 .28em 16px; margin: 0; position: relative; }
.timeline li::before { content: ""; position: absolute; left: -6px; top: .82em; width: 10px; height: 10px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); }
.timeline b { font-family: var(--sans); font-size: .82em; color: var(--accent); font-variant-numeric: tabular-nums; padding-top: .12em; }

/* cover */
.cover { text-align: center; padding-top: 72px; }
.cover-inner { max-width: 560px; margin: 0 auto; }
.cover h1 { font-size: 2.7em; margin-bottom: .25em; }
.cover-sub { font-family: var(--sans); font-size: 1.05em; color: var(--muted); margin-bottom: 2.6em !important; }
.authors { display: grid; gap: 16px; margin-bottom: 2.6em; }
.author { display: grid; font-family: var(--sans); font-size: .82em; color: var(--muted); line-height: 1.5; }
.author strong { color: var(--ink); font-size: 1.12em; }
.cover-toc { list-style: none; padding: 0 !important; display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 8px; text-align: left; font-family: var(--sans); font-size: .85em; }
.cover-toc li { margin: 0; }
.cover-toc a { display: flex; gap: 12px; align-items: baseline; padding: 11px 14px; border: 1px solid var(--line); border-radius: 8px; color: var(--ink); text-decoration: none; font-weight: 600; }
.cover-toc a:hover { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.cover-toc .num { color: var(--accent); font-weight: 800; }

/* pager */
.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 3em; padding-top: 1.2em; border-top: 1px solid var(--line); font-family: var(--sans); font-size: 15px; }
.pg { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-radius: 10px; color: var(--ink); text-decoration: none; font-weight: 650; }
.pg:hover { background: var(--accent-soft); color: var(--accent); }
.pg small { display: block; font-weight: 500; font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.pg-next { text-align: right; }

/* ---------- annotations ---------- */
mark.hl { background: color-mix(in srgb, var(--c) var(--hl-mix), transparent); color: inherit; border-radius: 2px; padding: .08em 0; cursor: pointer; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
mark.hl.ul { background: none; text-decoration: underline; text-decoration-color: var(--c); text-decoration-thickness: .16em; text-underline-offset: .18em; }
mark.hl.has-note::after { content: ""; display: inline-block; width: .62em; height: .62em; margin-left: .18em; vertical-align: .55em; border-radius: 50% 50% 50% 2px; background: var(--accent); }
.c-yellow { --c: var(--hl-yellow); } .c-green { --c: var(--hl-green); } .c-pink { --c: var(--hl-pink); } .c-blue { --c: var(--hl-blue); } .c-orange { --c: var(--hl-orange); }
.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0%, 35% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 35%, transparent); background-color: color-mix(in srgb, var(--accent) 18%, transparent); } 100% { box-shadow: 0 0 0 6px transparent; } }

.ink { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; z-index: 5; color: var(--ink); touch-action: none; }
.ink path { fill: none; stroke-linecap: round; stroke-linejoin: round; }
.ink path.marker { opacity: .38; mix-blend-mode: multiply; }
:root[data-theme="dark"] .ink path.marker { mix-blend-mode: screen; opacity: .45; }
.ink path.laser { stroke: #ff2d2d; filter: drop-shadow(0 0 5px #ff2d2d); transition: opacity .9s ease; }
body[data-tool="pen"] .ink, body[data-tool="marker"] .ink, body[data-tool="eraser"] .ink, body[data-tool="laser"] .ink { pointer-events: auto; }
body[data-tool="pen"] .ink, body[data-tool="marker"] .ink { cursor: crosshair; }
body[data-tool="eraser"] .ink { cursor: cell; }
body[data-tool="laser"] .ink { cursor: crosshair; }
body[data-tool="note"] .chapter { cursor: copy; }
body[data-tool="pen"] .chapter, body[data-tool="marker"] .chapter, body[data-tool="eraser"] .chapter, body[data-tool="laser"] .chapter { user-select: none; -webkit-user-select: none; }

.pins { position: absolute; inset: 0; pointer-events: none; z-index: 6; }
.pin {
  position: absolute; width: 30px; height: 30px; margin: -26px 0 0 -4px; pointer-events: auto;
  border-radius: 6px 6px 6px 0; background: var(--c); color: #1c2630; border: 1px solid rgba(0,0,0,.18);
  box-shadow: 0 2px 6px rgba(0,0,0,.25); display: grid; place-items: center; font-family: var(--sans); font-size: 13px; font-weight: 800;
}
.pin:hover { transform: scale(1.1); }
body.hide-ann .ink, body.hide-ann .pins { display: none; }
body.hide-ann mark.hl { background: none; text-decoration: none; }
body.hide-ann mark.hl::after { display: none; }

/* ---------- toolbar ---------- */
.toolbar {
  position: fixed; left: 50%; bottom: max(14px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50;
  max-width: calc(100vw - 16px); display: grid; gap: 6px; justify-items: center;
}
body:not(.nav-closed) .toolbar { left: calc(50% + var(--side) / 2); max-width: calc(100vw - var(--side) - 16px); }
.tool-row, .tool-opts {
  display: flex; align-items: center; gap: 2px; padding: 5px; max-width: 100%; overflow-x: auto; scrollbar-width: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
.tool-row::-webkit-scrollbar, .tool-opts::-webkit-scrollbar { display: none; }
.tool-opts { gap: 10px; padding: 7px 12px; }
.tool-opts[hidden] { display: none; }
.tbtn {
  flex: none; min-width: 54px; height: 50px; padding: 4px 6px 3px; border-radius: 12px;
  display: grid; justify-items: center; align-content: center; gap: 1px; color: var(--muted);
}
.tbtn span { font-size: 10.5px; font-weight: 600; letter-spacing: .01em; }
.tbtn:hover { background: var(--surface-2); color: var(--ink); }
.tbtn.on { background: var(--accent); color: var(--accent-ink); }
.tbtn:disabled { opacity: .35; pointer-events: none; }
.tbtn.act { min-width: 46px; }
.sep { flex: none; width: 1px; height: 30px; background: var(--line); margin: 0 4px; }
.swatches, .sizes, .dots { display: flex; align-items: center; gap: 8px; }
.sw { width: 28px; height: 28px; border-radius: 50%; background: var(--c); border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); flex: none; }
.sw.on { box-shadow: 0 0 0 2.5px var(--accent); }
.sw.ink-sw { background: var(--ink); }
.sizes { padding-left: 10px; border-left: 1px solid var(--line); }
.sizes[hidden] { display: none; }
.sizes button { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; }
.sizes button i { width: var(--d); height: var(--d); border-radius: 50%; background: var(--ink); }
.sizes button.on { background: var(--accent-soft); }

/* ---------- selection bar & popover ---------- */
.selbar, .pop { position: fixed; z-index: 60; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.selbar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; }
.selbar[hidden], .pop[hidden] { display: none; }
.selbar button:not(.sw) { width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center; color: var(--muted); }
.selbar button:not(.sw):hover { background: var(--surface-2); color: var(--ink); }
.pop { width: min(320px, calc(100vw - 20px)); padding: 10px; }
.pop-head { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.pop blockquote { margin: 0 0 8px; padding: 2px 0 2px 10px; border-left: 3px solid var(--line); font-family: var(--serif); font-size: 14px; line-height: 1.4; color: var(--muted); max-height: 4.3em; overflow: hidden; }
.pop blockquote:empty { display: none; }
.pop textarea { width: 100%; resize: vertical; min-height: 84px; padding: 9px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface-2); color: var(--ink); font: 15px/1.45 var(--sans); outline: 0; }
.pop textarea:focus { border-color: var(--accent); }

/* ---------- notes panel ---------- */
.panel {
  position: fixed; top: var(--top); bottom: 0; right: 0; width: 340px; max-width: 100vw; z-index: 45;
  background: var(--surface); border-left: 1px solid var(--line); display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform .22s ease;
}
body.panel-open .panel { transform: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 8px 4px 16px; }
.panel-head h2 { margin: 0; font-size: 16px; }
.seg { display: flex; margin: 6px 14px 10px; padding: 3px; background: var(--surface-2); border-radius: 10px; }
.seg button { flex: 1; height: 34px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.14); }
.panel-list { flex: 1; overflow-y: auto; padding: 0 12px 12px; }
.panel-list h3 { font-size: 12px; letter-spacing: .1em; text-transform: uppercase; color: var(--accent); margin: 16px 4px 6px; }
.item { position: relative; padding: 10px 34px 10px 14px; margin-bottom: 8px; border-radius: 9px; background: var(--surface-2); border-left: 4px solid var(--c, var(--line)); cursor: pointer; }
.item:hover { outline: 1px solid var(--accent); }
.item q { display: block; quotes: none; font-family: var(--serif); font-size: 14px; line-height: 1.4; color: var(--muted); max-height: 4.2em; overflow: hidden; }
.item p { margin: 5px 0 0; font-size: 14px; line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere; }
.item q:empty + p { margin-top: 0; }
.item .x { position: absolute; top: 5px; right: 4px; width: 28px; height: 28px; border-radius: 7px; display: grid; place-items: center; color: var(--muted); }
.item .x:hover { background: var(--surface); color: var(--danger); }
.item .x .ic { width: 16px; height: 16px; }
.panel-empty { padding: 26px 10px; text-align: center; color: var(--muted); font-size: 14px; line-height: 1.5; }
.panel-foot { display: flex; gap: 6px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.btn { flex: 1; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; gap: 6px; background: var(--surface-2); border: 1px solid var(--line); font-size: 13px; font-weight: 600; }
.btn .ic { width: 17px; height: 17px; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.danger:hover { border-color: var(--danger); color: var(--danger); }
.scrim { display: none; position: fixed; inset: var(--top) 0 0 0; z-index: 28; background: rgba(0,0,0,.35); }

/* ---------- lightbox & toast ---------- */
.lightbox { position: fixed; inset: 0; z-index: 80; background: rgba(8, 12, 15, .93); display: grid; grid-template-rows: 1fr auto; place-items: center; padding: 16px; cursor: zoom-out; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; min-height: 0; object-fit: contain; background: #fff; border-radius: 6px; }
.lightbox p { color: #d5dbe0; font-size: 14px; line-height: 1.45; text-align: center; margin: 10px 0 0; max-width: 760px; }
.toast { position: fixed; left: 50%; top: calc(var(--top) + 14px); transform: translateX(-50%); z-index: 90; background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow); max-width: calc(100vw - 24px); }
.toast[hidden] { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  body.panel-open #scroller { right: 0; }
  .panel { box-shadow: var(--shadow); }
}
@media (max-width: 900px) {
  #scroller, body.nav-closed #scroller { left: 0; }
  .sidebar { box-shadow: var(--shadow); width: min(var(--side), 86vw); transform: translateX(-100%); }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; }
  .toolbar, body:not(.nav-closed) .toolbar { left: 50%; max-width: calc(100vw - 12px); }
}
@media (max-width: 640px) {
  /* trees become an indented outline on phones */
  .tree { overflow: visible; }
  .tree ul, .tree > ul { display: block; min-width: 0; padding: 0 0 0 18px; border-left: 2px solid var(--line); margin-left: 10px; }
  .tree > ul { border-left: 0; padding-left: 0; margin-left: 0; }
  .tree li, .tree > ul > li { padding: 6px 0 0 0; text-align: left; }
  .tree li::before, .tree li::after, .tree ul ul::before { display: none; }
  .tree span { max-width: none; }
  /* toolbar wraps onto two rows */
  .tool-row { flex-wrap: wrap; justify-content: center; overflow: visible; width: min(352px, calc(100vw - 16px)); }
  .tool-row .sep { display: none; }
  .tbtn, .tbtn.act { min-width: 50px; height: 46px; }
  .chapter { margin: 0 0 230px; padding: 26px 18px 20px; border-radius: 0; border-left: 0; border-right: 0; font-size: calc(17px * var(--fs)); }
  .cover { padding-top: 40px; }
  .cover h1 { font-size: 2.1em; }
  .crumb-book { display: none; }
  #btn-full { display: none; }
  .timeline li { grid-template-columns: 5.2em 1fr; gap: 8px; }
  .panel { width: 100vw; }
}

@media print {
  .topbar, .sidebar, .toolbar, .panel, .selbar, .pop, .pager, .scrim { display: none !important; }
  body { overflow: visible; background: #fff; }
  #scroller { position: static; overflow: visible; }
  .chapter { display: block !important; border: 0; box-shadow: none; margin: 0; max-width: none; page-break-after: always; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---------- 3D model figures ---------- */
.model3d { --m3d-bg: #f1ede4; margin: 1.6em 0; font-family: var(--sans); }
:root[data-theme="dark"] .model3d { --m3d-bg: #1d262e; }
.m3d-area { position: relative; aspect-ratio: 4 / 3; max-height: 72vh; border-radius: 10px; overflow: hidden; background: var(--m3d-bg); border: 1px solid var(--line); }
.m3d-poster { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; padding: 4%; }
:root[data-theme="dark"] .m3d-poster { filter: brightness(.88); mix-blend-mode: normal; }
.m3d-open {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); display: flex; align-items: center; gap: 8px;
  padding: 11px 18px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 15px; box-shadow: var(--shadow);
}
.m3d-open:hover { filter: brightness(1.08); }
.m3d-loading .m3d-open span::after { content: "…"; }
.m3d-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: pan-y; cursor: grab; }
.m3d-canvas:active { cursor: grabbing; }
.m3d-labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.m3d-label { position: absolute; left: 0; top: 0; width: 0; height: 0; transition: opacity .2s; will-change: transform; }
.m3d-label i { position: absolute; left: -5px; top: -5px; width: 10px; height: 10px; border-radius: 50%; background: #fff; border: 2px solid #1c2630; box-sizing: border-box; }
.m3d-label span { position: absolute; left: 10px; top: 0; transform: translateY(calc(-50% + var(--dy, 0px))); width: max-content; max-width: 170px; padding: 3px 8px; border-radius: 6px; background: rgba(20, 28, 34, .84); color: #fff; font-size: 12px; font-weight: 600; line-height: 1.3; }
.m3d-label.left span { left: auto; right: 10px; text-align: right; }
.m3d-label.behind { opacity: .38; }
.m3d-label[hidden] { display: none; }
.m3d-tools { position: absolute; top: 8px; right: 8px; z-index: 3; display: none; gap: 6px; }
.m3d-tools button, .m3d-play { width: 38px; height: 38px; border-radius: 10px; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--line); color: var(--muted); }
.m3d-tools button.on, .m3d-play.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.m3d-tools .ic, .m3d-play .ic { width: 19px; height: 19px; }
.m3d-hint { position: absolute; left: 10px; bottom: 6px; margin: 0 !important; font-size: 11.5px; color: var(--muted); display: none; pointer-events: none; }
.m3d-controls, .m3d-stages, .m3d-text { display: none; }
.model3d.m3d-live .m3d-poster, .model3d.m3d-live .m3d-open { display: none; }
.model3d.m3d-live .m3d-tools { display: flex; }
.model3d.m3d-live .m3d-hint { display: block; }
.model3d.m3d-live .m3d-controls { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.model3d.m3d-live .m3d-stages { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin-top: 10px; }
.model3d.m3d-live .m3d-text { display: block; margin: 12px 0 0; font-size: 14.5px; line-height: 1.5; min-height: 4.5em; }
.m3d-slider { flex: 1; accent-color: var(--accent); height: 28px; }
.m3d-stage { display: flex; gap: 8px; align-items: center; text-align: left; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); font-size: 13px; line-height: 1.25; }
.m3d-stage b { flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; background: var(--surface); color: var(--accent); font-size: 13px; }
.m3d-stage.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.m3d-stage.on b { background: var(--accent); color: var(--accent-ink); }
.m3d-error { color: var(--danger); font-size: 14px; margin: 10px 0 0 !important; }
.model3d figcaption { font-size: .78em; color: var(--muted); line-height: 1.45; margin-top: .8em; text-align: center; }
/* full screen 3D */
body.m3d-lock { overflow: hidden; }
.model3d.m3d-fullscreen { position: fixed; inset: 0; z-index: 100; margin: 0; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--surface); display: flex; flex-direction: column; font-size: 16px; }
.model3d.m3d-fullscreen .m3d-area { flex: 1; aspect-ratio: auto; max-height: none; min-height: 0; }
.model3d.m3d-fullscreen .m3d-canvas { touch-action: none; }
.model3d.m3d-fullscreen figcaption { display: none; }
.model3d.m3d-fullscreen .m3d-text { min-height: 0; }
@media (max-width: 640px) {
  .m3d-area { aspect-ratio: 1 / 1; }
  .model3d.m3d-live .m3d-stages { grid-template-columns: repeat(3, 1fr); }
  .m3d-stage { flex-direction: column; text-align: center; padding: 7px 4px; font-size: 11.5px; }
  .m3d-label span { font-size: 11px; padding: 2px 6px; max-width: 120px; }
}
body.m3d-lock #scroller { z-index: 95; }
body.m3d-lock .toolbar, body.m3d-lock .selbar { display: none; }
.model3d.m3d-views .m3d-controls { display: none !important; }
.model3d.m3d-views.m3d-live .m3d-stages { margin-top: 12px; }
