/* Book Lab: paper and ink. A quiet frame around a serif writing surface. */
:root {
  --bg: #f4f1e8;
  --surface: #fbf9f3;
  --rail: #ece7da;
  --ink: #1f1d19;
  --muted: #6c675b;
  --faint: #9a9484;
  --line: #ddd6c5;
  --accent: #2f6f6a;
  --accent-ink: #ffffff;
  --accent-soft: #dbe9e5;
  --danger: #a33b2c;
  --danger-soft: #f3dcd6;
  --warn: #8a5f0c;
  --warn-soft: #f4e6c4;
  --ok: #2b7549;
  --add: #dcefdc;
  --del: #f5d9d3;
  --shadow: 0 1px 2px rgba(50, 40, 15, .08), 0 10px 30px rgba(50, 40, 15, .12);
  --serif: Georgia, Cambria, "Times New Roman", serif;
  --sans: "Segoe UI Variable Text", "Segoe UI", system-ui, sans-serif;
  --mono: "Cascadia Mono", Consolas, monospace;
  --fs: 18px;
  --radius: 10px;
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #141716;
  --surface: #1a1d1c;
  --rail: #181b1a;
  --ink: #e8e4d9;
  --muted: #a09b8d;
  --faint: #6f6b60;
  --line: #2b302e;
  --accent: #72bab1;
  --accent-ink: #0c1312;
  --accent-soft: #223936;
  --danger: #e68474;
  --danger-soft: #3a2420;
  --warn: #e0b24f;
  --warn-soft: #3a3118;
  --ok: #7cc79a;
  --add: #1f3a27;
  --del: #43231f;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 600; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.num { font-variant-numeric: tabular-nums; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#app { height: 100vh; display: grid; grid-template-rows: auto 1fr; }

/* ---- icons ------------------------------------------------------------------------------- */
.ic { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---- buttons ----------------------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; gap: 7px; height: 32px; padding: 0 13px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink);
  white-space: nowrap;
}
.btn:hover { border-color: var(--faint); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.small { height: 26px; padding: 0 9px; font-size: 12.5px; }
.btn.icon { width: 32px; padding: 0; justify-content: center; }
.btn.icon.small { width: 26px; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 0 12px; height: 30px; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* toggle switch (never a checkbox) */
.tog { display: flex; align-items: flex-start; gap: 12px; cursor: pointer; }
.tog-in { position: absolute; opacity: 0; width: 1px; height: 1px; }
.tog-track { flex: none; width: 36px; height: 20px; border-radius: 10px; background: var(--line); position: relative; margin-top: 1px; transition: background .15s; }
.tog-knob { position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s; }
.tog-in:checked + .tog-track { background: var(--accent); }
.tog-in:checked + .tog-track .tog-knob { transform: translateX(16px); }
.tog-in:focus-visible + .tog-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.tog-text b { display: block; font-weight: 600; }
.tog-text span { display: block; color: var(--muted); font-size: 12.5px; }

/* ---- top bar ----------------------------------------------------------------------------- */
.topbar { display: flex; align-items: center; gap: 12px; height: 48px; padding: 0 14px; border-bottom: 1px solid var(--line); background: var(--surface); }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; letter-spacing: .2px; border: 0; background: transparent; padding: 4px 6px; border-radius: 8px; }
.brand:hover { background: var(--accent-soft); }
.brand .mark { width: 24px; height: 24px; border-radius: 7px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.crumb { color: var(--muted); display: flex; align-items: center; gap: 8px; min-width: 0; }
.crumb b { color: var(--ink); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40vw; }
.spacer { flex: 1; }

/* ---- library ----------------------------------------------------------------------------- */
.library { overflow: auto; padding: 44px 28px 28px; }
.lib-wrap { max-width: 1040px; margin: 0 auto; }
.lib-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 26px; }
.lib-head h1 { font: 600 34px/1.15 var(--serif); letter-spacing: -.3px; }
.lib-head p { margin: 6px 0 0; color: var(--muted); }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(236px, 1fr)); gap: 20px; }
.book {
  text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 6px 12px 12px 6px;
  padding: 20px 20px 16px 24px; min-height: 196px; display: flex; flex-direction: column; gap: 6px; position: relative;
  box-shadow: inset 7px 0 0 var(--accent), 0 1px 2px rgba(50,40,15,.06);
  transition: transform .12s, box-shadow .12s;
}
.book:hover { transform: translateY(-2px); box-shadow: inset 7px 0 0 var(--accent), var(--shadow); }
.book h3 { font: 600 21px/1.2 var(--serif); }
.book .by { color: var(--muted); }
.book .meta { margin-top: auto; color: var(--muted); font-size: 12.5px; display: grid; gap: 6px; }
.book.new { border-style: dashed; background: transparent; box-shadow: none; align-items: center; justify-content: center; color: var(--muted); text-align: center; }
.book.new:hover { color: var(--accent); border-color: var(--accent); box-shadow: none; }
.lib-foot { margin-top: 34px; padding-top: 16px; border-top: 1px solid var(--line); display: flex; align-items: center; gap: 12px; color: var(--muted); flex-wrap: wrap; }
.lib-foot code { font: 12.5px var(--mono); background: var(--rail); padding: 2px 7px; border-radius: 5px; color: var(--ink); }
.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty h2 { font: 600 24px var(--serif); color: var(--ink); margin-bottom: 8px; }

progress { appearance: none; -webkit-appearance: none; width: 100%; height: 6px; border: 0; border-radius: 3px; background: var(--line); overflow: hidden; }
progress::-webkit-progress-bar { background: var(--line); }
progress::-webkit-progress-value { background: var(--accent); border-radius: 3px; }

/* ---- workspace --------------------------------------------------------------------------- */
.ws { display: grid; grid-template-columns: 276px minmax(0, 1fr) auto; min-height: 0; }
.rail { background: var(--rail); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.rail-head { padding: 16px 16px 12px; border-bottom: 1px solid var(--line); }
.rail-head h2 { font: 600 20px/1.25 var(--serif); cursor: pointer; }
.rail-head h2:hover { color: var(--accent); }
.rail-head .by { color: var(--muted); margin-top: 2px; }
.stats { margin-top: 12px; display: grid; gap: 7px; font-size: 12.5px; color: var(--muted); }
.stats .row { display: flex; justify-content: space-between; gap: 8px; }
.stats b { color: var(--ink); font-weight: 600; }
.plus { color: var(--ok); } .minus { color: var(--warn); }
.chapters { list-style: none; margin: 0; padding: 8px; overflow: auto; flex: 1; }
.ch { display: flex; align-items: center; border-radius: 8px; position: relative; }
.ch-main { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 9px; text-align: left; border: 0; background: transparent; padding: 8px 9px; border-radius: 8px; }
.ch:hover { background: rgba(120,110,80,.10); }
.ch.sel { background: var(--accent-soft); }
.ch.sel .ch-t { color: var(--accent); font-weight: 600; }
.ch-n { color: var(--faint); width: 18px; text-align: right; flex: none; font-size: 12px; }
.ch-t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-w { color: var(--faint); font-size: 12px; flex: none; }
.ch-tools { display: none; gap: 2px; padding-right: 4px; }
.ch:hover .ch-tools, .ch.sel .ch-tools, .ch:focus-within .ch-tools { display: flex; }
.ch:hover .ch-w, .ch.sel .ch-w, .ch:focus-within .ch-w { display: none; }
.rail-actions { display: flex; gap: 8px; padding: 8px 12px 12px; }
.rail-actions .btn { flex: 1; justify-content: center; }
.rail-foot { border-top: 1px solid var(--line); padding: 8px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.rail-foot .btn { flex-direction: column; height: 52px; gap: 3px; justify-content: center; font-size: 12px; color: var(--muted); }
.rail-foot .btn:hover { color: var(--accent); }

.center { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; background: var(--bg); }
.toolbar { display: flex; align-items: center; gap: 10px; height: 46px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.toolbar .title { font: 600 15px var(--serif); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.panes { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 1fr; }
.pane { min-width: 0; min-height: 0; overflow: auto; }
.pane.edit { border-right: 1px solid var(--line); background: var(--surface); }
.mode-write .panes { grid-template-columns: 1fr; }
.mode-write .pane.view { display: none; }
.mode-write .pane.edit { border: 0; }
.mode-read .panes { grid-template-columns: 1fr; }
.mode-read .pane.edit { display: none; }
#editor {
  display: block; width: 100%; height: 100%; resize: none; border: 0; outline: 0; background: transparent;
  padding: 34px max(28px, calc((100% - 44em) / 2)) 40vh; color: var(--ink);
  font: var(--fs)/1.75 var(--serif); tab-size: 4; caret-color: var(--accent);
}
#editor::selection { background: var(--accent-soft); }
.prose { font: var(--fs)/1.75 var(--serif); padding: 34px max(28px, calc((100% - 40em) / 2)) 40vh; }
.prose h1 { font-size: 1.85em; line-height: 1.25; margin: 0 0 .7em; letter-spacing: -.2px; }
.prose h2 { font-size: 1.35em; margin: 1.8em 0 .5em; }
.prose h3 { font-size: 1.12em; margin: 1.5em 0 .4em; }
.prose p { margin: 0 0 1em; }
.prose blockquote { margin: 1em 0; padding-left: 1.1em; border-left: 3px solid var(--line); color: var(--muted); }
.prose code { font: .86em var(--mono); background: var(--rail); padding: 1px 5px; border-radius: 4px; }
.prose pre { background: var(--rail); padding: 12px 14px; border-radius: 8px; overflow: auto; }
.prose pre code { background: transparent; padding: 0; }
.prose table { border-collapse: collapse; margin: 1em 0; font-size: .92em; }
.prose th, .prose td { border: 1px solid var(--line); padding: 5px 10px; }
.prose hr { border: 0; text-align: center; margin: 1.6em 0; }
.prose hr::after { content: "* * *"; letter-spacing: .5em; color: var(--faint); }
.prose img { max-width: 100%; }
.mode-read .prose { padding-top: 44px; }

.statusbar { display: flex; align-items: center; gap: 14px; height: 34px; padding: 0 14px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12.5px; background: var(--surface); }
.statusbar .sp { flex: 1; }
.state-saved { color: var(--ok); } .state-saving, .state-dirty { color: var(--muted); } .state-error { color: var(--danger); font-weight: 600; }
.pill { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; background: transparent; color: var(--muted); height: 24px; }
.pill:hover { border-color: var(--faint); }
.pill.on { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.pill .dot.bad { background: var(--warn); }

.center.noch .toolbar, .center.noch .panes, .center.noch .statusbar { display: none; }
.center:not(.noch) .blank { display: none; }
.issue { cursor: pointer; }
.blank { margin: auto; text-align: center; color: var(--muted); max-width: 380px; padding: 30px; }
.blank h2 { font: 600 22px var(--serif); color: var(--ink); margin-bottom: 8px; }
.blank .btn { margin: 14px 4px 0; }

/* ---- side panel -------------------------------------------------------------------------- */
.panel { width: 360px; border-left: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.panel-head { display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab { border: 0; background: transparent; padding: 6px 11px; border-radius: 7px; color: var(--muted); }
.tab[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.panel-body { flex: 1; overflow: auto; padding: 12px; display: grid; gap: 12px; align-content: start; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 11px 12px; background: var(--bg); }
.card h4 { margin: 0 0 4px; font-size: 13.5px; }
.group-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 4px 0 6px; }
.group-head b { font-size: 13px; }
.badge { display: inline-block; min-width: 20px; text-align: center; padding: 0 6px; border-radius: 999px; background: var(--warn-soft); color: var(--warn); font-size: 12px; font-weight: 600; }
.badge.ok { background: var(--accent-soft); color: var(--ok); }
.issue { width: 100%; text-align: left; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); padding: 8px 10px; display: grid; gap: 3px; margin-bottom: 6px; }
.issue:hover { border-color: var(--accent); }
.issue .where { font-size: 11.5px; color: var(--faint); display: flex; justify-content: space-between; align-items: center; }
.issue .msg { font-size: 13px; }
.issue .ex { font: 12.5px/1.45 var(--mono); color: var(--muted); overflow-wrap: anywhere; }
.allgood { text-align: center; padding: 28px 10px; color: var(--muted); }
.allgood b { display: block; font: 600 18px var(--serif); color: var(--ink); margin-bottom: 4px; }
.vlist { display: grid; gap: 10px; }
.v-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.v-top b { font: 600 17px var(--serif); }
.v-actions { display: flex; gap: 6px; margin-top: 9px; flex-wrap: wrap; }
.note { font-size: 12.5px; color: var(--muted); }

/* ---- compare ----------------------------------------------------------------------------- */
.compare { position: absolute; inset: 0; background: var(--bg); display: grid; grid-template-columns: 300px 1fr; grid-template-rows: auto 1fr; z-index: 5; }
.compare-head { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }
.compare-head h2 { font: 600 17px var(--serif); }
.cmp-list { border-right: 1px solid var(--line); overflow: auto; padding: 8px; background: var(--rail); }
.cmp-row { width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 8px; display: flex; gap: 8px; align-items: center; }
.cmp-row:hover { background: rgba(120,110,80,.1); }
.cmp-row[aria-selected="true"] { background: var(--accent-soft); }
.cmp-row .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip { font-size: 11.5px; padding: 1px 8px; border-radius: 999px; background: var(--line); color: var(--muted); white-space: nowrap; }
.chip.chg { background: var(--warn-soft); color: var(--warn); } .chip.add { background: var(--add); color: var(--ok); } .chip.del { background: var(--del); color: var(--danger); }
.cmp-view { overflow: auto; padding: 24px max(24px, calc((100% - 44em) / 2)) 60px; font: calc(var(--fs) - 1px)/1.7 var(--serif); }
.df-p { margin: 0 0 .9em; padding: 4px 10px; border-radius: 6px; white-space: pre-wrap; }
.df-p.add { background: var(--add); } .df-p.del { background: var(--del); }
.df-p ins { background: color-mix(in srgb, var(--ok) 28%, transparent); text-decoration: none; border-radius: 3px; }
.df-p del { background: color-mix(in srgb, var(--danger) 26%, transparent); border-radius: 3px; }
.df-fold { display: block; width: 100%; border: 1px dashed var(--line); background: transparent; color: var(--muted); border-radius: 8px; padding: 6px; margin: 0 0 .9em; font: 13px var(--sans); }
.df-fold:hover { color: var(--accent); border-color: var(--accent); }
.df-p.same { color: var(--muted); }

/* ---- modal and toasts -------------------------------------------------------------------- */
.backdrop { position: fixed; inset: 0; background: rgba(20, 18, 12, .45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); width: min(480px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; }
.modal.wide { width: min(720px, 100%); }
.modal header { padding: 18px 22px 6px; }
.modal header h2 { font: 600 21px var(--serif); }
.modal .body { padding: 8px 22px 6px; overflow: auto; display: grid; gap: 14px; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 22px 18px; }
.field { display: grid; gap: 5px; }
.field label { font-weight: 600; font-size: 12.5px; }
.field input, .field textarea, .field select { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 8px 10px; width: 100%; }
.field input:focus, .field textarea:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
.field textarea { resize: vertical; min-height: 70px; }
.field .hint { color: var(--muted); font-size: 12.5px; }
.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--faint); margin-top: 4px; }
.tog-list { display: grid; gap: 12px; }
.history-grid { display: grid; grid-template-columns: 200px 1fr; gap: 12px; min-height: 280px; }
.history-list { display: grid; gap: 4px; align-content: start; overflow: auto; max-height: 340px; }
.history-list button { text-align: left; border: 1px solid transparent; border-radius: 8px; background: transparent; padding: 7px 9px; }
.history-list button[aria-selected="true"] { background: var(--accent-soft); }
.history-prev { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; overflow: auto; max-height: 340px; white-space: pre-wrap; font: 13.5px/1.6 var(--serif); background: var(--bg); }

#toasts { position: fixed; right: 18px; bottom: 18px; display: grid; gap: 8px; z-index: 80; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); display: flex; gap: 14px; align-items: center; max-width: 440px; }
.toast.error { background: var(--danger); color: #fff; }
.toast button { border: 0; background: transparent; color: inherit; text-decoration: underline; padding: 0; }

@media (max-width: 1180px) { .panel { width: 320px; } .ws { grid-template-columns: 240px minmax(0, 1fr) auto; } }
