:root {
  --bg: #faf9f7; --panel: #ffffff; --ink: #1c1a17; --muted: #6b655c;
  --line: #e7e3dc; --accent: #7c4dff; --accent-ink: #5a34d1;
  --green: #1f9d6b; --green-bg: #e7f6ef; --yellow: #b8860b; --yellow-bg: #fdf4dd;
  --red: #c0392b; --red-bg: #fbe9e7; --blue: #2b6cb0;
  --radius: 10px; --shadow: 0 1px 3px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.05);
  --serif: Georgia, 'Iowan Old Style', 'Palatino Linotype', serif;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'Cascadia Code', Menlo, monospace;
}
[data-theme="dark"] {
  --bg: #17150f; --panel: #201d16; --ink: #ece7dd; --muted: #a29a8b;
  --line: #322d24; --accent: #a98bff; --accent-ink: #c3aeff;
  --green: #4cc38a; --green-bg: #12261d; --yellow: #e0b34a; --yellow-bg: #2a2412;
  --red: #f07267; --red-bg: #2b1512; --blue: #7cb3f0;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font-family: var(--sans); font-size: 15px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; font-size: 14px; cursor: pointer; }
.btn { border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 8px 14px; border-radius: 8px; transition: .15s; }
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-ink); }
.btn-sm { padding: 4px 9px; font-size: 12.5px; border-radius: 6px; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: rgba(124,77,255,.08); }
input, textarea, select { font-family: inherit; font-size: 14px; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; }
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
.muted { color: var(--muted); }
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 20px; letter-spacing: .02em; }

/* ---------- marketing ---------- */
.nav { display: flex; align-items: center; gap: 20px; padding: 16px 6%; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); z-index: 20; }
.brand { font-weight: 800; font-size: 18px; letter-spacing: -.02em; display: flex; align-items: center; gap: 8px; }
.brand .dot { width: 11px; height: 11px; border-radius: 3px; background: var(--accent); display: inline-block; transform: rotate(45deg); }
.nav .spacer { flex: 1; }
.hero { padding: 84px 6% 40px; max-width: 900px; }
.hero h1 { font-size: 52px; line-height: 1.05; letter-spacing: -.03em; margin: 0 0 18px; }
.hero p.lede { font-size: 20px; color: var(--muted); max-width: 620px; line-height: 1.5; }
.hero .cta { margin-top: 30px; display: flex; gap: 12px; }
.kicker { color: var(--accent-ink); font-weight: 700; letter-spacing: .08em; text-transform: uppercase; font-size: 12.5px; margin-bottom: 14px; }
.section { padding: 44px 6%; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); }
.card h3 { margin: 6px 0 8px; font-size: 17px; }
.pipeline-strip { display: flex; gap: 8px; flex-wrap: wrap; margin: 26px 0; }
.pipeline-strip .step { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 7px 15px; font-size: 13px; font-weight: 600; }
.pipeline-strip .step b { color: var(--accent-ink); }
.plans { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; max-width: 980px; }
.plan { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 26px; box-shadow: var(--shadow); }
.plan.featured { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(124,77,255,.15), var(--shadow); }
.plan .price { font-size: 34px; font-weight: 800; margin: 8px 0; }
.plan ul { padding-left: 18px; color: var(--muted); line-height: 1.9; }
footer { padding: 40px 6%; border-top: 1px solid var(--line); color: var(--muted); font-size: 13px; }

/* ---------- auth ---------- */
.auth-wrap { max-width: 400px; margin: 60px auto; padding: 32px; }
.auth-wrap h2 { margin-top: 0; }

/* home: split hero + sign-in */
.home-wrap { min-height: calc(100vh - 64px); display: grid; grid-template-columns: 1.15fr 0.85fr; align-items: center; gap: 40px; padding: 40px 6%;
  background: radial-gradient(1200px 600px at 0% 0%, rgba(124,77,255,.10), transparent 60%), var(--bg); }
.home-hero .brand { margin-bottom: 34px; }
.home-hero h1 { font-size: 60px; line-height: 1.02; letter-spacing: -.035em; margin: 0 0 22px;
  background: linear-gradient(120deg, var(--ink), var(--accent-ink)); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.home-hero p.lede { font-size: 19px; color: var(--muted); max-width: 520px; line-height: 1.55; margin: 0 0 30px; }
.home-auth { display: flex; justify-content: center; }
.auth-card { width: 100%; max-width: 400px; padding: 30px; box-shadow: var(--shadow); }
.auth-card .tabs { border-bottom: 1px solid var(--line); }
.home-foot { text-align: center; font-size: 12px; color: var(--muted); padding: 24px 6% 20px; }
@media (max-width: 860px) {
  .home-wrap { grid-template-columns: 1fr; min-height: auto; padding-top: 56px; padding-bottom: 40px; }
  .home-hero h1 { font-size: 40px; }
  .home-hero .pipeline-strip { margin-bottom: 30px; }
}
.field { display: block; margin-bottom: 14px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 5px; color: var(--muted); }
.field input { width: 100%; }
.err { color: var(--red); font-size: 13px; min-height: 18px; margin: 6px 0; }

/* ---------- app shell ---------- */
.app { display: grid; grid-template-columns: 250px 1fr; height: 100vh; }
.sidebar { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; overflow: hidden; }
.sidebar .top { padding: 16px; border-bottom: 1px solid var(--line); }
.sidebar nav { padding: 10px; overflow-y: auto; flex: 1; }
.sidebar .navitem { display: block; padding: 8px 10px; border-radius: 7px; color: var(--ink); font-size: 14px; margin-bottom: 2px; }
.sidebar .navitem:hover { background: rgba(124,77,255,.08); text-decoration: none; }
.sidebar .navitem.active { background: rgba(124,77,255,.14); color: var(--accent-ink); font-weight: 600; }
.sidebar .navitem { display: flex; align-items: center; gap: 9px; }
.sidebar .navitem svg { width: 16px; height: 16px; stroke-width: 2; flex-shrink: 0; opacity: .8; }
.sidebar .navitem.active svg { opacity: 1; }
.preview-link { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; font-size: 12.5px; color: var(--muted); text-decoration: none; }
.preview-link:hover { color: var(--accent-ink); text-decoration: none; }
.preview-link svg { width: 14px; height: 14px; stroke-width: 2; }
.sidebar .foot { padding: 12px; border-top: 1px solid var(--line); font-size: 12.5px; }
.main { overflow-y: auto; height: 100vh; }
.topbar { display: flex; align-items: center; gap: 14px; padding: 12px 24px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(6px); z-index: 10; }
.topbar .crumbs { font-size: 14px; color: var(--muted); }
.topbar .crumbs b { color: var(--ink); }
.content { padding: 24px; max-width: 1200px; }

/* stage stepper */
.stepper { display: flex; gap: 4px; align-items: stretch; margin: 4px 0 22px; flex-wrap: wrap; }
.stepper .stage { flex: 1; min-width: 108px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; font-size: 12.5px; position: relative; cursor: pointer; transition: .15s; }
.stepper .stage:hover { border-color: var(--accent); }
.stepper .stage .n { font-size: 10.5px; font-weight: 700; color: var(--muted); letter-spacing: .06em; }
.stepper .stage .lbl { font-weight: 700; margin-top: 2px; }
.stepper .stage.active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(124,77,255,.14); }
.stepper .stage.done { background: var(--green-bg); }
.stepper .stage.done .lbl::after { content: ' ✓'; color: var(--green); }

/* banners */
.banner { border-radius: 8px; padding: 11px 14px; font-size: 13.5px; margin-bottom: 16px; display: flex; gap: 10px; align-items: center; }
.banner.warn { background: var(--yellow-bg); color: var(--yellow); border: 1px solid color-mix(in srgb, var(--yellow) 40%, transparent); }
.banner.info { background: var(--panel); border: 1px solid var(--line); color: var(--muted); }

/* tables / ledger */
table.ledger { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.ledger th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 8px; border-bottom: 1px solid var(--line); }
.ledger td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.ledger tr:hover td { background: rgba(124,77,255,.04); }
.ledger input, .ledger select { border: 1px solid transparent; background: transparent; width: 100%; padding: 5px 6px; }
.ledger input:focus, .ledger select:focus { border-color: var(--accent); background: var(--panel); }
.status-verified { color: var(--green); }
.status-unverified { color: var(--yellow); }
.status-disputed, .status-retired { color: var(--red); }
.dot-s { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px; }

/* editor */
.editor-grid { display: grid; grid-template-columns: 1fr 300px; gap: 18px; }
.editor-pane textarea { width: 100%; min-height: 62vh; border-radius: var(--radius); padding: 22px 26px; font-family: var(--serif); font-size: 17px; line-height: 1.72; resize: vertical; max-width: 74ch; }
.ctx-sidebar { font-size: 13px; }
.ctx-sidebar h4 { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 16px 0 8px; }
.ctx-claim { padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; margin-bottom: 6px; background: var(--panel); }
.ctx-claim.verified { border-left: 3px solid var(--green); }
.ctx-claim code { font-family: var(--mono); font-size: 11.5px; color: var(--accent-ink); }

/* dispositions */
.disp { border-radius: 8px; padding: 11px 13px; margin-bottom: 8px; border: 1px solid var(--line); font-size: 13.5px; }
.disp.ledgered_verified { background: var(--green-bg); border-color: color-mix(in srgb, var(--green) 35%, transparent); }
.disp.ledgered_unverified { background: var(--yellow-bg); border-color: color-mix(in srgb, var(--yellow) 35%, transparent); }
.disp.unledgered, .disp.contradicts_ledger { background: var(--red-bg); border-color: color-mix(in srgb, var(--red) 35%, transparent); }
.disp.narrative_invented { background: color-mix(in srgb, var(--blue) 12%, transparent); border-color: color-mix(in srgb, var(--blue) 32%, transparent); }
.disp.narrative_invented b { color: var(--blue); }

/* mode picker (new-book modal) */
.mode-picker { display: grid; gap: 8px; }
.mode-opt { display: block; border: 1px solid var(--line); border-radius: 9px; padding: 11px 13px; cursor: pointer; transition: .15s; }
.mode-opt:hover { border-color: var(--accent); }
.mode-opt.selected { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(124,77,255,.14); background: rgba(124,77,255,.04); }
.mode-opt b { display: block; font-size: 14px; margin-bottom: 2px; }
.mode-opt span { font-size: 12.5px; color: var(--muted); line-height: 1.4; }
.mode-badge.fiction { background: color-mix(in srgb, var(--blue) 16%, transparent); color: var(--blue); border: 1px solid color-mix(in srgb, var(--blue) 30%, transparent); }
.mode-badge { background: var(--panel); border: 1px solid var(--line); color: var(--muted); }

/* in-app manuscript preview overlay */
#previewOverlay { position: fixed; inset: 0; z-index: 200; display: none; flex-direction: column; background: var(--bg); }
#previewOverlay .pv-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
#previewOverlay .pv-title { font-weight: 700; font-size: 14px; }
#previewOverlay .pv-scroll { flex: 1; overflow-y: auto; }
#previewOverlay .pv-inner { max-width: 72ch; margin: 0 auto; padding: 56px 24px 120px;
  font-family: Georgia, 'Iowan Old Style', 'Palatino Linotype', serif; line-height: 1.72; font-size: 19px; color: var(--ink); }
#previewOverlay .title-block { border-bottom: 1px solid var(--line); padding-bottom: 26px; margin-bottom: 8px; }
#previewOverlay h1 { font-size: 40px; line-height: 1.1; letter-spacing: -.02em; margin: 0 0 10px; }
#previewOverlay .subtitle { font-size: 22px; color: var(--muted); font-style: italic; margin: 0 0 14px; }
#previewOverlay .meta { font-family: var(--sans); font-size: 13px; color: var(--muted); }
#previewOverlay .toc { font-family: var(--sans); margin: 30px 0 12px; }
#previewOverlay .toc h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; }
#previewOverlay .toc ol { padding-left: 22px; line-height: 1.9; font-size: 15px; }
#previewOverlay .toc a { color: var(--accent-ink); text-decoration: none; }
#previewOverlay .toc .empty { color: var(--muted); font-style: italic; font-size: 13px; }
#previewOverlay .chapter { margin-top: 60px; }
#previewOverlay .chnum { font-family: var(--sans); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 6px; }
#previewOverlay h2 { font-size: 30px; letter-spacing: -.02em; line-height: 1.15; margin: 0 0 20px; }
#previewOverlay h3 { font-size: 22px; margin: 32px 0 10px; }
#previewOverlay h4 { font-size: 18px; margin: 26px 0 8px; }
#previewOverlay p { margin: 0 0 20px; }
#previewOverlay blockquote { border-left: 3px solid var(--line); margin: 0 0 20px; padding: 2px 0 2px 20px; color: var(--muted); font-style: italic; }
#previewOverlay ul, #previewOverlay ol { margin: 0 0 20px; padding-left: 26px; }
#previewOverlay hr { border: none; border-top: 1px solid var(--line); margin: 36px 0; }
#previewOverlay code { font-family: var(--mono); font-size: .85em; background: rgba(124,77,255,.08); padding: 1px 5px; border-radius: 4px; }
#previewOverlay .cite { font-family: var(--sans); font-size: .78em; color: var(--accent); white-space: nowrap; }
#previewOverlay .needs-claim { font-family: var(--sans); font-size: .8em; color: var(--red); background: var(--red-bg); padding: 0 5px; border-radius: 4px; font-weight: 600; }
#previewOverlay .empty { color: var(--muted); font-style: italic; }
#previewOverlay .pv-foot { margin-top: 70px; padding-top: 20px; border-top: 1px solid var(--line); font-family: var(--sans); font-size: 12px; color: var(--muted); text-align: center; }
.disp .acts { margin-top: 8px; display: flex; gap: 6px; }

/* runs tray */
.tray { position: fixed; right: 18px; bottom: 18px; width: 300px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); z-index: 30; overflow: hidden; }
.tray .h { padding: 10px 14px; font-weight: 700; font-size: 13px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; }
.tray .body { max-height: 220px; overflow-y: auto; }
.tray .job { padding: 9px 14px; border-bottom: 1px solid var(--line); font-size: 12.5px; display: flex; justify-content: space-between; }
.spin { width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; display: inline-block; animation: sp 0.8s linear infinite; }
@keyframes sp { to { transform: rotate(360deg); } }

/* Draft-generation overlay: unmissable feedback while a slow AI call runs. */
.editor-pane { position: relative; }
.draft-overlay {
  position: absolute; inset: 0; z-index: 20;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; padding: 28px; text-align: center;
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  backdrop-filter: blur(3px);
  border-radius: 12px;
  animation: fadeIn .18s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.draft-overlay .big-spin {
  width: 44px; height: 44px; border: 4px solid var(--line);
  border-top-color: var(--accent); border-radius: 50%;
  animation: sp 0.85s linear infinite;
}
.draft-overlay h4 { margin: 0; font-size: 16px; }
.draft-overlay .sub { color: var(--muted); font-size: 13px; max-width: 340px; line-height: 1.5; margin: 0; }
.draft-overlay .timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--accent); font-size: 14px; }
.draft-progress {
  width: min(320px, 80%); height: 7px; border-radius: 99px;
  background: var(--line); overflow: hidden; position: relative;
}
.draft-progress > i {
  position: absolute; top: 0; left: 0; height: 100%; width: 40%;
  border-radius: 99px; background: var(--accent);
  animation: draftbar 1.4s ease-in-out infinite;
}
@keyframes draftbar {
  0% { left: -40%; } 100% { left: 100%; }
}
.draft-phase { font-size: 12px; color: var(--muted); min-height: 16px; }

.row { display: flex; gap: 10px; align-items: center; }
.between { display: flex; justify-content: space-between; align-items: center; }
.stack { display: flex; flex-direction: column; gap: 12px; }
h1.page { font-size: 26px; letter-spacing: -.02em; margin: 4px 0 4px; }
.grid-books { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.book-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); cursor: pointer; transition: .15s; }
.book-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.chapter-row .row-act { opacity: 0; transition: opacity .12s; }
.chapter-row:hover .row-act { opacity: .7; }
.chapter-row .row-act:hover { opacity: 1; }
.chapter-row .row-act svg, .crumbs .btn svg { width: 14px; height: 14px; }
@media (hover: none) { .chapter-row .row-act { opacity: .7; } }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal { background: var(--panel); border-radius: var(--radius); padding: 24px; width: 440px; max-width: 92vw; box-shadow: var(--shadow); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tab { padding: 9px 14px; font-size: 14px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; cursor: pointer; }
.tab.active { color: var(--accent-ink); border-bottom-color: var(--accent); }
.doc-editor textarea { width: 100%; min-height: 340px; font-family: var(--serif); font-size: 16px; line-height: 1.7; padding: 18px; }
.diff-add { background: var(--green-bg); }
.diff-del { background: var(--red-bg); text-decoration: line-through; opacity: .7; }
@media (max-width: 820px) { .app { grid-template-columns: 1fr; } .sidebar { display: none; } .editor-grid, .grid3, .plans { grid-template-columns: 1fr; } .hero h1 { font-size: 38px; } }

/* selection tools under the draft — ask about / rewrite a highlighted passage */
.sel-tools { margin-top: 14px; padding: 14px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.sel-tools input { border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.sel-answer { margin-top: 12px; padding: 12px 14px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; font-size: 14px; line-height: 1.65; white-space: pre-wrap; }
.sel-note { margin-top: 10px; color: var(--muted); font-size: 13px; }
.sel-rewrite { padding: 12px 14px; background: var(--green-bg, #f3faf4); border: 1px solid var(--line); border-radius: 10px; font-family: var(--serif); font-size: 15px; line-height: 1.7; white-space: pre-wrap; }
.sel-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.sel-busy { margin-top: 12px; padding: 12px 14px; background: var(--bg); border: 1px solid var(--accent); border-radius: 10px; font-size: 14px; display: flex; align-items: center; }
.sel-spin { width: 15px; height: 15px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; display: inline-block; margin-right: 10px; animation: selspin .7s linear infinite; flex: none; }
@keyframes selspin { to { transform: rotate(360deg); } }
.sel-error { margin-top: 12px; padding: 12px 14px; background: var(--red-bg, #fdf2f2); border: 1.5px solid #d66; border-radius: 10px; font-size: 14px; }
.sel-sug { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 8px; padding: 10px 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.sel-sug-label { font-size: 13.5px; font-weight: 600; }
.sel-sug button { flex: none; }

/* Modals (weave-in, previews) */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20,17,10,0.5); backdrop-filter: blur(2px); display: flex; align-items: center; justify-content: center; z-index: 200; padding: 20px; animation: mfade 0.15s ease; }
@keyframes mfade { from { opacity: 0; } to { opacity: 1; } }
.modal-card { background: var(--bg); border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 24px 60px rgba(0,0,0,0.28); padding: 22px 24px; max-height: 90vh; overflow: auto; }
.modal-card h3 { color: var(--ink); }
.modal-card textarea, .modal-card input { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; color: var(--ink); box-sizing: border-box; }
.modal-card textarea:focus, .modal-card input:focus { outline: none; border-color: var(--accent); }
