:root {
  color-scheme: light;
  --paper: #eff0f2;
  --white: #f8f9fa;
  --ink: #292c32;
  --chrome: #e9ebee;
  --sheet: #f8f8f7;
  --module: #e9ebef;
  --module-header: #e3e6eb;
  --module-edge: #ccd1d9;
  --canvas: #e1e5eb;
  --muted: #4f5661;
  --subtle: #646b77;
  --edge: #d1d5db;
  --quiet: #e6e8ec;
  --canvas-dot: #c4cbd5;
  --graph-link: #667489;
  --graph-conflict: #657282;
  --graph-active: var(--accent-dark);
  --accent-soft: color-mix(in srgb, var(--accent) 12%, var(--white));
  --accent-dark: var(--accent);
  --primary-hover: #3d424b;
  --field-edge: color-mix(in srgb, var(--edge) 83%, var(--muted));
}
:root[data-skin="night"] {
  color-scheme: dark;
  --paper: #0e0f11;
  --white: #17191e;
  --ink: #dfe3eb;
  --chrome: #13151a;
  --sheet: #141518;
  --module: #19202d;
  --module-header: #202b3d;
  --module-edge: #303c51;
  --canvas: #141d2b;
  --muted: #adb6c5;
  --subtle: #939dac;
  --edge: #2d333e;
  --quiet: #262c36;
  --canvas-dot: #2d3a4d;
  --graph-link: #798aa4;
  --graph-conflict: #9fa8b8;
  --accent-soft: color-mix(in srgb, var(--accent) 20%, var(--white));
  --accent-dark: color-mix(in srgb, var(--accent) 45%, #e6ebf3);
  --primary-hover: #cbd4e3;
  --green: #b5dbc5;
  --danger: #f2a7b1;
}
:root[data-accent="mono"] { --accent: var(--ink); }
:root[data-accent="blue"] { --accent: #245d96; }
:root[data-accent="green"] { --accent: #286650; }
:root[data-accent="rust"] { --accent: #9e453b; }

.sidebar, .topbar { background: var(--chrome); }
.nav-button { color: var(--muted); min-height: 40px; }
.icon-clock::before { inset: 1px; border: 1.4px solid; border-radius: 50%; }
.icon-clock::after { left: 8px; top: 4px; width: 4px; height: 5px; border-left: 1.3px solid; border-bottom: 1.3px solid; }
.icon-appearance::before { inset: 1px; border: 1.4px solid; border-radius: 50%; background: linear-gradient(90deg, currentColor 50%, transparent 50%); }
:focus-visible { outline-color: var(--accent-dark); }
.project-button:hover { border-color: var(--accent-dark); }
.primary { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
fieldset, input, textarea, select { border-color: var(--field-edge); }
select:not([multiple]) {
  background: linear-gradient(45deg, transparent 47%, var(--muted) 51%) right 12px center/5px 5px no-repeat,
    linear-gradient(135deg, var(--muted) 49%, transparent 53%) right 7px center/5px 5px no-repeat, var(--white);
}
dialog, .result-button { background: var(--white); }
dialog::backdrop { background: #11131e78; }
.summary-row, .writing-rel-list li + li { border-color: var(--edge); }
.excerpt { background: var(--quiet); color: var(--ink); border-left-color: var(--edge); }
.heading-tag { background: var(--quiet); color: var(--muted); }
.project-icon { color: var(--muted); }
.writing-sheet { background: var(--sheet); }
.writing-chapter-bar, .writing-footer { background: var(--white); }
.writing-context, .canvas-panel > .panel-foot { background: var(--module); }
.writing-context, .writing-context .writing-graph, .writing-context .writing-graph .panel-foot { border-color: var(--module-edge); }
.writing-context > .panel-head, .canvas-panel > .panel-head { background: var(--module-header); }
.writing-context .writing-rel-list li + li { border-color: var(--module-edge); }
.timeline-era { background: var(--module); border-color: var(--module-edge); }
.writing-input:focus-visible { outline-color: var(--accent-dark); }
.writing-mirror mark { background: color-mix(in srgb, var(--accent) 22%, var(--sheet)); }
.writing-mirror mark.current-mention { background: color-mix(in srgb, var(--accent) 37%, var(--sheet)); box-shadow: 0 1px var(--accent-dark); }
.writing-graph .node[aria-pressed="true"] .avatar, .writing-graph .node[aria-pressed="true"] .node-name { background: var(--accent-soft); color: var(--accent-dark); }
.writing-graph .node[aria-pressed="true"] .avatar { box-shadow: 0 0 0 2px var(--accent-dark); }
.writing-graph .edge-label.muted-edge { color: var(--muted); border-color: var(--edge); background: var(--chrome); }
.toast { background: var(--ink); color: var(--paper); }
.toast button { color: var(--paper); border-color: var(--subtle); }
.notice { border-color: color-mix(in srgb, var(--danger) 36%, var(--edge)); background: color-mix(in srgb, var(--danger) 9%, var(--white)); }
.pill[class], .avatar[class] { color: var(--ink); background: var(--quiet); }

.appearance-panel { max-width: 800px; padding: 18px; }
.appearance-group { margin-top: 14px; }
.appearance-group h3 { font-size: .875rem; margin-bottom: 7px; }
.appearance-options { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(96px, 100%), 1fr)); gap: 8px; }
.appearance-option { display: flex; align-items: center; gap: 7px; min-height: 45px; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--edge); background: var(--white); font-size: .875rem; white-space: nowrap; }
.appearance-option:hover { background: var(--quiet); }
.appearance-option[aria-pressed="true"] { background: var(--quiet); border-color: var(--accent-dark); font-weight: 700; }
.appearance-swatch { width: 19px; height: 19px; border-radius: 5px; border: 1px solid #8885; flex: none; }
.skin-light { background: linear-gradient(135deg, #e9ebef 53%, #292c32 54%); }
.skin-night { background: linear-gradient(135deg, #202b3d 53%, #dfe3eb 54%); }
.accent-mono { background: var(--ink); }
.accent-violet { background: #5544af; }
.accent-blue { background: #245d96; }
.accent-green { background: #286650; }
.accent-rust { background: #9e453b; }

.books-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(295px,100%), 1fr)); gap: 16px; }
.book-card { display: flex; flex-direction: column; }
.book-card-header { display: flex; gap: 12px; align-items: center; padding: 16px; background: var(--module); border-bottom: 1px solid var(--edge); }
.book-card-header div { flex: 1; min-width: 0; }
.book-card-header h2 { font: 700 1.39rem/1.35 var(--serif); overflow-wrap: anywhere; }
.book-card-header p { color: var(--muted); font-size: .8125rem; margin-top: 4px; }
.book-spine { width: 35px; height: 45px; display: grid; place-items: center; background: var(--white); color: var(--accent-dark); border: 1px solid var(--edge); border-radius: 5px 8px 8px 5px; flex: none; }
.book-card-body { flex: 1; padding: 15px 16px; }
.book-card-body > p { color: var(--muted); overflow-wrap: anywhere; }
.book-counts { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 7px; margin: 12px 0; }
.book-counts div { display: flex; flex-direction: column-reverse; min-width: 0; }
.book-counts dd { margin: 0; font-size: 1.18rem; font-weight: 680; font-variant-numeric: tabular-nums; }
.book-counts dt, .book-card-body p.book-chapter { font-size: .8125rem; color: var(--muted); }
.book-card-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px 16px; border-top: 1px solid var(--edge); }
.chapter-directory-row { display: flex; text-align: left; justify-content: space-between; white-space: normal; }
.chapter-directory-row span:first-child { overflow-wrap: anywhere; }

.timeline-controls, .timeline-era, .timeline-card, .timeline-card-actions { display: flex; align-items: center; gap: 12px; }
.timeline-controls { flex-wrap: wrap; justify-content: space-between; margin-bottom: 13px; }
.timeline-controls .field { display: flex; align-items: center; }
.timeline-controls .field > span { white-space: nowrap; padding-right: 5px; color: var(--muted); }
.timeline-controls select { min-width: 106px; }
.timeline-era { padding: 12px 15px; justify-content: space-between; }
.timeline-era span { margin-right: 12px; display: inline-block; color: var(--muted); font-size: .875rem; }
.timeline-era strong { overflow-wrap: anywhere; }
.timeline-list { --time-width: 155px; position: relative; padding: 0; margin: 20px 0 24px; display: grid; gap: 11px; list-style: none; }
.timeline-list::before { content: ""; position: absolute; left: calc(var(--time-width) + 10px); top: 28px; bottom: 26px; width: 1px; background: var(--edge); }
.timeline-event { display: grid; grid-template-columns: var(--time-width) minmax(0,1fr); gap: 23px; }
.timeline-when { position: relative; padding: 15px 0 0; color: var(--muted); font-size: .875rem; font-weight: 650; overflow-wrap: anywhere; }
.timeline-when::after { content: ""; position: absolute; width: 7px; height: 7px; top: 22px; right: -14px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 3px var(--paper); }
.timeline-card { justify-content: space-between; align-items: flex-start; padding: 12px 14px; }
.timeline-card-content { min-width: 0; overflow-wrap: anywhere; }
.timeline-card-content p { margin-top: 3px; color: var(--muted); white-space: pre-wrap; }
.timeline-card-content .timeline-people { font-size: .8125rem; color: var(--accent-dark); }
.timeline-card-content small { display: block; margin-top: 3px; }
.timeline-card-content .btn { padding: 1px 0; min-height: 29px; text-align: left; white-space: normal; }
.timeline-card-actions { gap: 5px; flex: none; }
.timeline-card-actions .btn { padding: 2px 7px; min-height: 32px; }
.timeline-unscheduled { margin-top: 23px; }
.timeline-unscheduled .timeline-when::after { background: var(--subtle); }

.graph-view {
  background: radial-gradient(var(--canvas-dot) .8px, transparent 1px) 0 0/17px 17px, var(--canvas);
}
.graph-view.expanded { touch-action: none; cursor: grab; }
.graph-view.is-panning, .graph-view.is-panning * { cursor: grabbing; }
.graph-view .node { z-index: 2; }
.graph-view .node .avatar { border-color: var(--chrome); box-shadow: 0 0 0 1px var(--edge); }
.graph-view .node .avatar.central { border-color: var(--chrome); box-shadow: 0 0 0 1px var(--accent-dark), 0 0 23px var(--accent-soft); }
.graph-view .node:hover .avatar, .graph-view .node:focus-visible .avatar { box-shadow: 0 0 0 2px var(--accent-dark), 0 0 19px var(--accent-soft); }
.graph-view .node-name { background: var(--canvas); }
.graph-view .edge-label { border-color: var(--edge); background: var(--chrome); color: var(--muted); }
.graph-view .edge-label:hover, .graph-view .edge-label:focus-visible { z-index: 3; border-color: var(--accent-dark); color: var(--accent-dark); }
.graph-view .edge-label.muted-edge:not(:focus-visible):not(:hover) { opacity: .72; }
.graph-view.graph-dense .edge-label:not(:focus-visible):not(:hover) { opacity: 0; }
.graph-view.graph-dense.has-focused-node .edge-label:not(.muted-edge) { opacity: 1; }
.graph-controls { flex-wrap: wrap; justify-content: flex-end; }
.graph-controls select { width: auto; }
.zoom-box { z-index: 4; }

@media (max-width: 1120px) {
  .topbar [data-action="appearance-open"] .icon { display: none; }
}
@media (max-width: 690px) {
  .appearance-options { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .top-actions { gap: 5px; }
  .top-actions .btn { padding-inline: 7px; }
  .timeline-list { padding-left: 17px; }
  .timeline-list::before { left: 3px; }
  .timeline-event { grid-template-columns: 1fr; gap: 4px; }
  .timeline-when { padding-top: 4px; }
  .timeline-when::after { left: -17px; top: 11px; right: auto; }
  .timeline-card { flex-wrap: wrap; }
  .timeline-plot-link { flex-wrap: wrap; }
  .writing-chapter-bar .button-row { flex-wrap: wrap; justify-content: flex-end; gap: 4px; }
}
@media (pointer: coarse) {
  .nav-button, .appearance-option, .timeline-card-actions .btn { min-height: 43px; }
}
@media (prefers-reduced-motion: no-preference) {
  :root, .topbar, .panel, .writing-sheet, .writing-chapter-bar, .writing-footer, .panel-head, .panel-foot, .graph-view, dialog,
  .btn, .nav-button, .nav-button .icon, .nav-button::before, .appearance-option {
    transition: background-color 190ms ease, color 150ms ease, border-color 150ms ease;
  }
  .tabs button { transition: background-color 145ms ease, color 145ms ease, border-color 145ms ease; }
}
