:root {
  --bg: #0e0f12;
  --bg-2: #16181d;
  --bg-3: #1d2027;
  --panel: #14161b;
  --line: #2a2e37;
  --ink: #e8e6df;
  --ink-dim: #a5a29a;
  --muted: #6f6d67;
  --gold: #d9b45b;
  --gold-dim: #8a743a;
  --keep: #5bd97e;
  --iterate: #e7b64b;
  --scrap: #e06666;
  --undecided: #6f7480;
  --radius: 6px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  --dim: 0.8;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.55;
}

/* ---------- top bar (compact) ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(180deg, #101216, #0c0d10);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 24px;
  max-width: 1300px;
  margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  color: var(--gold);
  font-size: 16px;
  filter: drop-shadow(0 0 6px rgba(217, 180, 91, 0.4));
}
.topbar h1 { margin: 0; font-size: 14px; font-weight: 600; letter-spacing: 0.3px; }

.topbar-actions { display: flex; align-items: center; gap: 12px; }
.save-indicator {
  font-size: 11.5px;
  color: var(--muted);
  transition: color 0.2s;
}
.save-indicator.dirty { color: var(--iterate); }
.save-indicator.saving { color: var(--gold); }
.save-indicator.saved { color: var(--keep); }
.save-indicator.error { color: var(--scrap); }

.btn {
  border: 1px solid var(--line);
  background: var(--bg-3);
  color: var(--ink);
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 11.5px;
  opacity: var(--dim);
  transition: transform 0.08s, background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { border-color: var(--gold-dim); opacity: 1; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { opacity: 1; }

/* ---------- layout: [Progress+Sections stack][StoryRiver] ---------- */
.layout {
  display: grid;
  grid-template-columns: 230px minmax(0, 1fr);
  gap: 16px;
  max-width: 1300px;
  margin: 0 auto;
  padding: 16px 24px 40px;
  align-items: start;
}
.side-stack {
  position: sticky;
  top: 50px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  opacity: var(--dim);
  transition: opacity 0.15s;
}
.panel:hover, .panel:focus-within { opacity: 1; }
.panel-title {
  margin: 0 0 6px;
  font-size: 10.5px;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--gold);
}

/* ---------- progress (slim bar, no donut) ---------- */
.progress-caption { margin: 0 0 6px; font-size: 10.5px; color: var(--muted); }
.progressbar-slim {
  height: 5px;
  border-radius: 2px;
  overflow: hidden;
  display: flex;
  background: var(--bg-3);
  margin-bottom: 5px;
}
.progressbar-slim > span { height: 100%; }
.progress-pct { font-size: 11px; color: var(--ink-dim); margin-bottom: 8px; }

.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.legend li { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--ink-dim); }
.legend .dot { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.legend .count { margin-left: auto; color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- toc ---------- */
.toc { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 42vh; overflow: auto; }
.toc a {
  display: flex; gap: 8px; align-items: baseline;
  padding: 4px 7px; border-radius: 3px;
  color: var(--ink-dim); text-decoration: none; font-size: 12px;
}
.toc a:hover { background: var(--bg-3); color: var(--ink); }
.toc .toc-num { color: var(--gold-dim); font-variant-numeric: tabular-nums; min-width: 24px; }
.toc .toc-dot { width: 6px; height: 6px; border-radius: 50%; margin-left: auto; flex: none; }

.toc-category { margin: 10px 0 2px; }
.toc-category:first-child { margin-top: 0; }
.toc-cat-label {
  display: block; font-size: 9.5px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--gold); padding: 3px 3px 2px;
}
.toc-group { padding: 0; }
.toc-group > details { border-radius: 3px; }
.toc-group summary {
  cursor: pointer; list-style: none; padding: 5px 7px; border-radius: 3px;
  font-size: 11.5px; color: var(--ink-dim); display: flex; align-items: center; gap: 6px;
}
.toc-group summary::-webkit-details-marker { display: none; }
.toc-group summary::before { content: '▸'; color: var(--gold-dim); flex: none; }
.toc-group details[open] > summary::before { content: '▾'; }
.toc-group summary:hover { background: var(--bg-3); color: var(--ink); }
.toc-group .toc-count {
  margin-left: auto; color: var(--muted); font-size: 10px;
  background: var(--bg-3); border-radius: 3px; padding: 1px 6px;
}
.toc-group ul { list-style: none; margin: 2px 0 4px; padding: 0 0 0 13px; display: grid; gap: 2px; }

.muted { color: var(--muted) !important; }

/* ---------- storyriver (main column) ---------- */
.storyriver { display: block; min-width: 0; }
.categories { display: grid; gap: 26px; }
.category-block { display: grid; gap: 14px; }

.part-header {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 16px;
  opacity: var(--dim);
  transition: opacity 0.15s;
}
.part-header:hover, .part-header:focus-within { opacity: 1; }
.part-header h2 { margin: 0 0 4px; font-size: 16px; }
.part-header .subtitle { margin: 0; color: var(--ink-dim); font-size: 12.5px; max-width: 780px; }

.cat-ready-row { margin-top: 10px; }
.ready-chk {
  display: flex; align-items: center; gap: 7px; font-size: 12px;
  color: var(--ink-dim); cursor: pointer;
}
.ready-chk input { accent-color: var(--gold); width: 13px; height: 13px; cursor: pointer; }

.cat-actions { display: flex; gap: 8px; margin-top: 10px; }

.cat-empty {
  padding: 12px 16px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 12.5px;
  font-style: italic;
  opacity: var(--dim);
}

.cat-sections { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }

.bp-flag-inline {
  color: var(--gold); background: rgba(217, 180, 91, 0.1); border: 1px solid var(--gold-dim);
  border-radius: 3px; padding: 0 6px; font-size: 12px; white-space: nowrap;
}

/* ---------- source-material groups ---------- */
details.group {
  background: transparent;
  margin-bottom: 4px;
}
.group-summary {
  cursor: pointer; list-style: none;
  background: var(--bg-3); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 12px; margin-bottom: 8px;
  font-size: 12.5px; font-weight: 600; color: var(--ink);
  display: flex; align-items: center; gap: 10px;
  opacity: var(--dim);
  transition: opacity 0.15s, border-color 0.15s;
}
.group-summary::-webkit-details-marker { display: none; }
.group-summary::before { content: '▸'; color: var(--gold); flex: none; font-weight: 400; }
details.group[open] > .group-summary::before { content: '▾'; }
.group-summary:hover { border-color: var(--gold-dim); opacity: 1; }
.group-count {
  margin-left: auto; font-weight: 400; font-size: 10.5px; color: var(--muted);
  background: var(--bg-2); border-radius: 3px; padding: 2px 8px;
}
.group-body { display: grid; gap: 14px; margin: 0 0 14px 4px; }

.bp-flag {
  margin: 10px 0 0; padding: 8px 12px;
  background: rgba(90, 160, 200, 0.08); border: 1px dashed #3d6a86;
  border-radius: 4px; font-size: 12.5px; color: #9fc7dc;
}
.bp-flag strong { color: #bfe0f0; }

/* ---------- sections ---------- */
.section {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--undecided);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  scroll-margin-top: 90px;
}
.section[data-current-status="keep"] { border-left-color: var(--keep); }
.section[data-current-status="iterate"] { border-left-color: var(--iterate); }
.section[data-current-status="scrap"] { border-left-color: var(--scrap); }
.section[data-current-status="scrap"] .section-body { opacity: 0.6; }

.section-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--gold);
  background: rgba(217, 180, 91, 0.1);
  border: 1px solid var(--gold-dim);
  border-radius: 4px;
  padding: 1px 7px;
  font-size: 11.5px;
  opacity: var(--dim);
}
.section-title { margin: 0; font-size: 16px; flex: 1; }
.status-chip {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.8px;
  padding: 2px 7px; border-radius: 4px; border: 1px solid var(--line);
  color: var(--undecided);
  opacity: var(--dim);
}
.status-chip[data-s="keep"] { color: var(--keep); border-color: var(--keep); }
.status-chip[data-s="iterate"] { color: var(--iterate); border-color: var(--iterate); }
.status-chip[data-s="scrap"] { color: var(--scrap); border-color: var(--scrap); }
.section:not([data-category="workbench"]) .status-chip { display: none; }

.section-body { color: var(--ink); }
.section-body p { margin: 0 0 10px; }
.section-body ul, .section-body ol { margin: 0 0 10px; padding-left: 20px; }
.section-body li { margin: 3px 0; }
.section-body code, .section-title code, .group-summary code, .bp-flag code {
  background: var(--bg-3); border: 1px solid var(--line);
  padding: 1px 5px; border-radius: 5px; font-size: 12.5px;
  color: var(--gold);
}
.section-body s { color: var(--muted); }

.applied-note {
  margin: 12px 0 0; padding: 8px 12px;
  background: rgba(217, 180, 91, 0.08); border: 1px dashed var(--gold-dim);
  border-radius: 4px; font-size: 12.5px; color: var(--ink-dim);
}
.applied-note strong { color: var(--gold); }

.bp-table { width: 100%; border-collapse: collapse; margin: 6px 0 12px; font-size: 13px; }
.bp-table th, .bp-table td {
  text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top;
}
.bp-table thead th {
  color: var(--gold); font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px;
  border-bottom: 1px solid var(--gold-dim);
  opacity: var(--dim);
}
.bp-table tbody tr:hover { background: var(--bg-2); }

.bp-tree, .section-body pre {
  background: #0b0c0f; border: 1px solid var(--line); border-radius: 4px;
  padding: 12px 14px; overflow: auto; max-width: 100%; font-size: 12.5px; line-height: 1.5;
  color: var(--ink-dim); margin: 0 0 12px;
}
.section-body pre.raw-doc { max-height: 520px; }
.section-body p.bp-empty { color: var(--muted); font-style: italic; }

/* ---------- per-section item checkboxes ----------
   Local, per-section only — for marking a few list/table/chunk items so a note
   like "checked = keep, rest trash" can reference them. No global behaviour. */
.chk-item {
  display: flex; align-items: flex-start; gap: 8px; cursor: pointer;
}
.chk-box {
  flex: none; width: 13px; height: 13px; margin-top: 2px;
  accent-color: var(--gold); cursor: pointer;
}
.chk-text { flex: 1; }
td.chk-cell, th.chk-cell {
  width: 1%; padding: 7px 4px 7px 10px !important; vertical-align: middle !important;
}
td.chk-cell .chk-box { margin-top: 0; }
.chk-box-inline { vertical-align: -2px; margin-right: 2px; }
.chk-summary {
  color: var(--gold-dim); text-transform: none; letter-spacing: 0; font-size: 10.5px;
}

/* ---------- prior + controls ---------- */
.prior {
  margin: 12px 0;
  background: var(--bg-2);
  border: 1px dashed var(--line);
  border-radius: 4px;
  padding: 8px 10px;
  opacity: var(--dim);
}
.prior-label { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.prior-text {
  margin: 6px 0 0; padding-left: 10px; border-left: 2px solid var(--gold-dim);
  color: var(--ink-dim); font-size: 13px; font-style: italic; white-space: pre-line;
}

/* ---------- per-category controls ----------
   Only one of these three shows per section, based on data-category:
   workbench keeps the status system; sorting + saved use move/delete instead.
   Dimmed as a block (chrome, not document text) — sharpens on hover/focus so
   it's still easy to use. */
.section .controls {
  display: none; margin-top: 4px;
  opacity: var(--dim); transition: opacity 0.15s;
}
.section .controls:hover, .section .controls:focus-within { opacity: 1; }
.section[data-category="workbench"] .controls-workbench { display: grid; gap: 10px; }
.section[data-category="sorting"] .controls-sorting { display: grid; gap: 8px; }
.section[data-category="saved"] .controls-saved { display: grid; }

.status-group { display: flex; gap: 5px; flex-wrap: wrap; }
.status-btn {
  border: 1px solid var(--line); background: var(--bg-3); color: var(--ink-dim);
  padding: 4px 11px; border-radius: 4px; cursor: pointer; font-size: 11.5px;
  transition: all 0.12s;
}
.status-btn:hover { color: var(--ink); }
.status-btn[data-status="keep"].active { background: var(--keep); color: #08240f; border-color: var(--keep); }
.status-btn[data-status="iterate"].active { background: var(--iterate); color: #2a1f04; border-color: var(--iterate); }
.status-btn[data-status="scrap"].active { background: var(--scrap); color: #2a0808; border-color: var(--scrap); }
.status-btn[data-status="undecided"].active { background: var(--undecided); color: #0c0e12; border-color: var(--undecided); }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-move-saved { border-color: var(--gold-dim); color: var(--gold); }
.btn-move-saved:hover { background: rgba(217, 180, 91, 0.08); }
.btn-danger { border-color: rgba(224, 102, 102, 0.4); color: var(--scrap); }
.btn-danger:hover { border-color: var(--scrap); background: rgba(224, 102, 102, 0.08); }

.apply-note-chk {
  display: flex; align-items: center; gap: 7px; font-size: 11.5px;
  color: var(--ink-dim); cursor: pointer;
}
.apply-note-chk input { accent-color: var(--gold); width: 13px; height: 13px; cursor: pointer; }

.note-label { display: grid; gap: 5px; margin-top: 10px; font-size: 10.5px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.note {
  width: 100%; resize: vertical; min-height: 38px;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 4px;
  color: var(--ink); padding: 8px 10px; font: inherit; font-size: 13.5px; text-transform: none; letter-spacing: 0;
}
.note:focus { outline: none; border-color: var(--gold-dim); }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .side-stack { position: static; }
}
