:root {
  --bg: #ece8de;
  --surface: #f8f6f0;
  --raised: #ffffff;
  --ink: #1f1c17;
  --muted: #6f685b;
  --faint: #a59e90;
  --line: #ddd7ca;
  --accent: #b4532a;
  --accent-ink: #ffffff;
  --accent-soft: rgba(180, 83, 42, .12);
  --hl: rgba(255, 196, 61, .42);
  --hl-live: rgba(214, 120, 20, .55);
  --hover: rgba(180, 83, 42, .10);
  --noted: rgba(70, 130, 200, .16);
  --page-filter: none;
  --shadow: 0 1px 2px rgba(40, 30, 10, .08), 0 6px 24px rgba(40, 30, 10, .08);
  --serif: "Iowan Old Style", "Charter", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --side-w: 300px;
  --notes-w: 340px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11100d; --surface: #1a1915; --raised: #23211c; --ink: #ece6d8; --muted: #a39b8b; --faint: #6c665a;
    --line: #2f2c25; --accent: #e48a5c; --accent-ink: #1a0f08; --accent-soft: rgba(228, 138, 92, .16);
    --hl: rgba(255, 190, 60, .38); --hl-live: rgba(255, 150, 50, .6); --hover: rgba(228, 138, 92, .14);
    --noted: rgba(110, 170, 240, .2); --page-filter: brightness(.88) contrast(1.02);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 28px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.45 var(--sans);
  display: grid; overflow: hidden;
  grid-template-columns: var(--side-w) 1fr 0;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "top top top" "side stage notes" "player player player";
  -webkit-tap-highlight-color: transparent;
}
body.notes-open { grid-template-columns: var(--side-w) 1fr var(--notes-w); }
body.side-closed { grid-template-columns: 0 1fr 0; }
body.side-closed.notes-open { grid-template-columns: 0 1fr var(--notes-w); }
[hidden] { display: none !important; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, select, textarea { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

/* ---------- top bar ---------- */
.top {
  grid-area: top; display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 14px 8px; background: var(--surface);
  border-bottom: 1px solid var(--line); min-height: 56px; z-index: 5;
}
.ib { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; color: var(--muted); }
.ib:hover { background: var(--accent-soft); color: var(--ink); }
.ib.sm { width: 30px; height: 30px; border-radius: 8px; }
.ib.sm svg { width: 17px; height: 17px; }
.ib.on { color: var(--accent); background: var(--accent-soft); }
.doc-id { min-width: 0; max-width: 34vw; }
.doc-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
.seg { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 3px; margin-left: 8px; }
.seg button { padding: 5px 12px; border-radius: 8px; font-size: 13px; color: var(--muted); }
.seg button[aria-selected="true"] { background: var(--raised); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.1); }
.pager, .zoom { display: inline-flex; align-items: center; gap: 2px; }
.pgbox { display: inline-flex; align-items: baseline; gap: 4px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.pgbox input { width: 3.4em; text-align: right; border: 1px solid var(--line); background: var(--raised); border-radius: 7px; padding: 3px 6px; color: var(--ink); }
.zlabel { font-size: 12.5px; color: var(--muted); min-width: 46px; font-variant-numeric: tabular-nums; }
body.no-doc .seg, body.no-doc .pager, body.no-doc .zoom { visibility: hidden; }
body.mode-text .zoom { visibility: hidden; }

/* ---------- side panel ---------- */
.side { grid-area: side; background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.tabs { display: flex; gap: 4px; padding: 10px 12px 6px; }
.tabs button { flex: 1; padding: 7px 0; border-radius: 8px; font-size: 13px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.pane { flex: 1; overflow: auto; padding: 6px 12px 20px; overscroll-behavior: contain; }
.add { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 11px; border: 1.5px dashed var(--line); border-radius: 12px; cursor: pointer; color: var(--accent); font-weight: 600; }
.add:hover { border-color: var(--accent); background: var(--accent-soft); }
.add.big { display: inline-flex; padding: 14px 26px; border-style: solid; border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }
.add.big:hover { filter: brightness(1.05); background: var(--accent); }
.linkish { display: block; margin: 6px auto 12px; font-size: 12.5px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.lib, .toc, .thumbs, .note-list { list-style: none; margin: 0; padding: 0; }
.lib li { position: relative; border-radius: 12px; margin-bottom: 4px; }
.lib li.cur { background: var(--accent-soft); }
.lib .open { display: block; width: 100%; text-align: left; padding: 10px 40px 10px 12px; border-radius: 12px; }
.lib .open:hover { background: var(--hover); }
.lib .t { display: block; font-weight: 600; line-height: 1.3; }
.lib .a { display: block; font-size: 12.5px; color: var(--muted); }
.lib .meter { display: block; height: 3px; background: var(--line); border-radius: 2px; margin: 7px 0 4px; overflow: hidden; }
.lib .meter i { display: block; height: 100%; background: var(--accent); }
.lib .s { font-size: 11.5px; color: var(--faint); }
.lib .more { position: absolute; right: 6px; top: 8px; }
.lib .order { position: absolute; right: 8px; bottom: 6px; display: none; gap: 2px; }
.lib li:hover .order { display: flex; }
.lib .order button { font-size: 11px; color: var(--faint); padding: 2px 5px; border-radius: 5px; }
.lib .order button:hover { color: var(--ink); background: var(--hover); }
.toc li .go { display: block; width: 100%; text-align: left; padding: 7px 10px; border-radius: 8px; font-size: 14px; line-height: 1.3; }
.toc li .go:hover { background: var(--hover); }
.toc li .go .pg { float: right; color: var(--faint); font-size: 12px; font-variant-numeric: tabular-nums; margin-left: 8px; }
.toc li.d1 .go { padding-left: 24px; font-size: 13.5px; color: var(--muted); }
.toc li.d2 .go, .toc li.d3 .go { padding-left: 38px; font-size: 13px; color: var(--muted); }
.toc li.cur .go { color: var(--accent); font-weight: 600; }
.toc li { display: flex; align-items: flex-start; gap: 2px; }
.toc li .go { flex: 1; min-width: 0; }
.dlb { flex: none; width: 30px; height: 30px; margin-top: 2px; display: grid; place-items: center; border-radius: 8px; color: var(--faint); }
.dlb svg { width: 16px; height: 16px; }
.dlb:hover { color: var(--accent); background: var(--hover); }
.dlb.done { color: var(--accent); }
.dlb.busy svg { animation: pulse 1s ease-in-out infinite; }
.dlbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 8px 10px 12px; margin-bottom: 6px; border-bottom: 1px solid var(--line); font-size: 12.5px; color: var(--muted); }
.dlbar span { flex-basis: 100%; }
.dlbar button { padding: 5px 12px; border-radius: 999px; border: 1px solid var(--accent); color: var(--accent); font-size: 12.5px; font-weight: 600; }
.dlbar button.quiet { border-color: var(--line); color: var(--muted); font-weight: 400; }
.toc .none { color: var(--muted); font-size: 13px; padding: 10px; }
.thumbs { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px 10px; }
.thumbs li button { display: block; width: 100%; }
.thumbs .tp { display: block; background: #fff; border-radius: 4px; box-shadow: var(--shadow); overflow: hidden; outline: 2px solid transparent; outline-offset: 2px; }
.thumbs .tp canvas { display: block; width: 100%; height: 100%; filter: var(--page-filter); }
.thumbs li.cur .tp { outline-color: var(--accent); }
.thumbs .n { display: block; text-align: center; font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.thumbs li.reading .n { color: var(--accent); font-weight: 600; }
.scrim { display: none; }

/* ---------- stage ---------- */
.stage { grid-area: stage; position: relative; overflow: auto; min-width: 0; overscroll-behavior: contain; scroll-behavior: auto; }
.welcome { max-width: 520px; margin: 12vh auto 0; padding: 0 24px; text-align: center; }
.welcome h1 { font: 600 30px/1.2 var(--serif); margin: 0 0 14px; letter-spacing: -.01em; }
.welcome p { color: var(--muted); margin: 0 0 24px; }
.welcome .fine { font-size: 12.5px; color: var(--faint); margin-top: 22px; }
.pages { padding: 24px 24px 48px; display: flex; flex-direction: column; align-items: center; gap: 18px; }
.pg { position: relative; background: #fff; box-shadow: var(--shadow); border-radius: 2px; flex: none; cursor: text; }
.pg canvas { position: absolute; inset: 0; width: 100%; height: 100%; filter: var(--page-filter); border-radius: 2px; }
.pg .num { position: absolute; left: -2px; top: -18px; font-size: 11px; color: var(--faint); font-variant-numeric: tabular-nums; }
.pg .hl { position: absolute; inset: 0; pointer-events: none; }
.pg .hl i { position: absolute; border-radius: 3px; mix-blend-mode: multiply; }
.pg .hl i.now { background: var(--hl); }
.pg .hl i.live { background: var(--hl-live); }
.pg .hl i.hov { background: var(--hover); }
.pg .hl i.word { background: var(--hl-live); border-radius: 3px; transition: left .08s, width .08s; }
::highlight(word) { background-color: var(--hl-live); }
.pg .hl i.noted { box-shadow: inset 0 -2px 0 rgba(70, 130, 200, .75); background: var(--noted); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pg .hl i { mix-blend-mode: normal; } }
.pg.loading::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, transparent, rgba(0,0,0,.03), transparent); animation: shimmer 1.2s infinite; }
@keyframes shimmer { from { transform: translateX(-30%); } to { transform: translateX(30%); } }
.follow {
  position: sticky; bottom: 18px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 6px; margin: -60px auto 18px;
  width: max-content; padding: 9px 16px 9px 12px; border-radius: 999px; background: var(--ink); color: var(--bg); font-size: 13.5px; font-weight: 600; box-shadow: var(--shadow); z-index: 3;
}
.follow svg { width: 16px; height: 16px; }
.dropzone { position: fixed; inset: 0; background: rgba(180, 83, 42, .12); display: grid; place-items: center; z-index: 50; pointer-events: none; }
.dropzone div { padding: 22px 34px; border-radius: 16px; background: var(--raised); border: 2px dashed var(--accent); font-weight: 600; color: var(--accent); }

.textview { max-width: 680px; margin: 0 auto; padding: 36px 28px 30vh; font: 20px/1.7 var(--serif); }
.textview .pmark { font: 600 11px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--faint); margin: 34px 0 10px; display: flex; align-items: center; gap: 10px; }
.textview .pmark::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.textview p { margin: 0 0 .9em; }
.textview h3 { font: 600 1.15em/1.35 var(--serif); margin: 1.4em 0 .5em; }
.textview .c { cursor: pointer; border-radius: 4px; transition: background-color .2s; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.textview .c:hover { background: var(--hover); }
.textview .c.noted { box-shadow: inset 0 -2px 0 rgba(70, 130, 200, .7); }
.textview .c.now { background: var(--hl); }
.textview .more { display: block; margin: 20px auto; padding: 8px 16px; border-radius: 999px; border: 1px solid var(--line); font: 13px var(--sans); color: var(--muted); }

/* ---------- notes ---------- */
.notes { grid-area: notes; background: var(--surface); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.notes-head { display: flex; align-items: center; gap: 8px; padding: 12px 12px 8px 16px; }
.notes-head h2 { font-size: 15px; margin: 0; flex: 1; }
.notes-head select { border: 1px solid var(--line); background: var(--raised); border-radius: 8px; padding: 4px 6px; font-size: 12.5px; }
.note-list { flex: 1; overflow: auto; padding: 4px 12px 12px; }
.note-list .group { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 14px 4px 6px; }
.note-list .note { background: var(--raised); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; border: 1px solid var(--line); }
.note-list blockquote { margin: 0; font: 14.5px/1.5 var(--serif); border-left: 3px solid var(--accent); padding-left: 10px; color: var(--ink); }
.note-list .body { margin: 8px 0 0; font-size: 14px; white-space: pre-wrap; }
.note-list .meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.note-list .meta button { color: var(--accent); font-size: 12px; padding: 2px 0; }
.note-list .meta button.del { color: var(--faint); }
.note-list .empty { color: var(--muted); font-size: 13.5px; padding: 10px 4px; }
.export { border-top: 1px solid var(--line); padding: 10px 16px calc(12px + env(safe-area-inset-bottom, 0px)); }
.export summary { cursor: pointer; font-size: 13.5px; font-weight: 600; color: var(--muted); }
.export-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 10px; }
.export-grid button { padding: 8px 4px; border: 1px solid var(--line); border-radius: 8px; font-size: 12.5px; background: var(--raised); }
.export-grid button:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- player ---------- */
.player { grid-area: player; background: var(--surface); border-top: 1px solid var(--line); padding: 0 14px calc(8px + env(safe-area-inset-bottom, 0px)); z-index: 5; }
body.no-doc .player { display: none; }
.track { position: relative; height: 18px; margin: 0 -14px; }
.ticks { position: absolute; left: 14px; right: 14px; top: 7px; height: 4px; pointer-events: none; }
.ticks i { position: absolute; top: -2px; width: 2px; height: 8px; background: var(--faint); opacity: .5; border-radius: 1px; }
.ticks i.n { background: rgb(70, 130, 200); opacity: .8; }
#seek { position: absolute; left: 0; right: 0; top: 0; width: 100%; height: 18px; margin: 0; padding: 0 14px; background: transparent; -webkit-appearance: none; appearance: none; cursor: pointer; --p: 0%; }
#seek::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: linear-gradient(to right, var(--accent) var(--p), var(--line) var(--p)); }
#seek::-moz-range-track { height: 4px; border-radius: 2px; background: linear-gradient(to right, var(--accent) var(--p), var(--line) var(--p)); }
#seek::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--accent); margin-top: -5px; border: 2px solid var(--surface); }
#seek::-moz-range-thumb { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); }
.player-row { display: flex; align-items: center; gap: 14px; }
.transport { display: flex; align-items: center; gap: 2px; }
.play { width: 50px; height: 50px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; margin: 0 4px; box-shadow: 0 2px 10px rgba(180, 83, 42, .3); }
.play svg { width: 24px; height: 24px; fill: currentColor; stroke: none; }
.play:hover { filter: brightness(1.06); }
.play.wait svg { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }
.now { flex: 1; min-width: 0; cursor: pointer; }
.now-text { font: 15px/1.4 var(--serif); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.now-meta { display: flex; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 2px; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }
.now-meta .status { color: var(--accent); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.now-meta .status:empty { display: none; }
.extras { display: flex; align-items: center; gap: 4px; }
.chip { padding: 6px 11px; border-radius: 999px; border: 1px solid var(--line); font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- dialogs and toast ---------- */
dialog { border: 0; border-radius: 18px; padding: 0; background: var(--raised); color: var(--ink); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); width: min(480px, calc(100vw - 24px)); max-height: calc(100vh - 40px); }
dialog.wide { width: min(560px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(20, 15, 5, .35); backdrop-filter: blur(2px); }
dialog form { padding: 20px 22px; display: flex; flex-direction: column; gap: 12px; }
dialog h3 { margin: 0; font-size: 17px; }
dialog blockquote { margin: 0; font: 15.5px/1.55 var(--serif); border-left: 3px solid var(--accent); padding: 2px 0 2px 12px; max-height: 30vh; overflow: auto; }
dialog textarea, dialog input:not([type="checkbox"]):not([type="radio"]), dialog select {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 11px; resize: vertical;
}
dialog .loc { font-size: 12.5px; color: var(--muted); }
.qtools { display: flex; flex-wrap: wrap; gap: 6px; }
.qtools button { font-size: 12.5px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.qtools button:hover { color: var(--accent); border-color: var(--accent); }
.dlg-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.dlg-row button { padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line); }
.dlg-row button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: #c23b2b; }
.confirm { margin: 0; font-size: 13.5px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.confirm button { padding: 6px 12px; border-radius: 9px; border: 1px solid currentColor; }
fieldset { border: 0; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
legend { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--faint); margin-bottom: 6px; }
.radio, .check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; cursor: pointer; }
.radio input, .check input { margin-top: 3px; accent-color: var(--accent); }
.radio span { color: var(--muted); } .radio b { color: var(--ink); }
.f { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grp { display: flex; flex-direction: column; gap: 10px; padding: 12px; border-radius: 12px; background: var(--surface); }
.clone { display: flex; flex-direction: column; gap: 8px; padding-top: 10px; border-top: 1px solid var(--line); }
.clone blockquote { font-size: 14px; max-height: none; }
.clone .dlg-row input { flex: 1; min-width: 140px; }
#cloneRec.rec { background: #c23b2b; border-color: #c23b2b; color: #fff; }
.hint { margin: 0; font-size: 12.5px; color: var(--muted); }
.hint code { font-size: 11.5px; }
.toast {
  position: fixed; left: 50%; bottom: calc(110px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 13.5px; max-width: min(520px, calc(100vw - 32px)); box-shadow: var(--shadow);
}

/* ---------- narrow screens ---------- */
@media (max-width: 1100px) {
  body.notes-open { grid-template-columns: var(--side-w) 1fr 0; }
  body.side-closed.notes-open { grid-template-columns: 0 1fr 0; }
  .notes { position: fixed; right: 0; top: 0; bottom: 0; width: min(var(--notes-w), 92vw); z-index: 20; box-shadow: var(--shadow); transform: translateX(105%); transition: transform .25s ease; padding-top: env(safe-area-inset-top, 0px); }
  body.notes-open .notes { transform: none; }
}
@media (max-width: 860px) {
  body, body.notes-open, body.side-closed, body.side-closed.notes-open { grid-template-columns: 0 1fr 0; }
  .side { position: fixed; left: 0; top: 0; bottom: 0; width: min(340px, 88vw); z-index: 20; box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .25s ease; padding-top: env(safe-area-inset-top, 0px); }
  body.side-open .side { transform: none; }
  body.side-open .scrim, body.notes-open .scrim { display: block; position: fixed; inset: 0; background: rgba(20, 15, 5, .3); z-index: 15; }
  .top { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .doc-id { display: none; }
  .seg { margin-right: auto; }
  .zoom { display: none; }
  .pager .ib { display: none; }
  .seg { margin-left: 0; }
  .pages { padding: 14px 8px 40px; gap: 12px; }
  .player-row { flex-wrap: wrap; gap: 4px 8px; }
  .now { order: -1; flex-basis: 100%; padding: 2px 4px 0; }
  .transport { flex: 1; justify-content: center; }
  .chip#bVoice { display: none; }
  .now-meta { flex-wrap: wrap; row-gap: 0; }
  .now-meta .status { flex-basis: 100%; }
  .textview { font-size: 19px; padding: 24px 20px 30vh; }
}
@media (max-width: 420px) {
  .transport .ib { width: 34px; }
  .play { width: 46px; height: 46px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
