/* Story Outline Tool */

/* Summer Waves theme font (Sorts Mill Goudy, a.k.a. GoudyStM, SIL OFL 1.1, see vendor/fonts). */
@font-face {
  font-family: "GoudyStM";
  src: url("../vendor/fonts/sorts-mill-goudy.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "GoudyStM";
  src: url("../vendor/fonts/sorts-mill-goudy-italic.woff2") format("woff2");
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

/* Handwriting font for the Sketch theme (Patrick Hand, SIL OFL 1.1, see vendor/fonts). */
@font-face {
  font-family: "Patrick Hand";
  src: url("../vendor/fonts/patrick-hand.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}

:root {
  --bg: #f3efe7;
  --board: #ebe5d9;
  --surface: #fffdf9;
  --surface-2: #f6f2ea;
  --border: #ddd4c4;
  --border-strong: #c7bca8;
  --text: #2b2723;
  --muted: #72695d;
  --primary: #3b5bdb;
  --primary-text: #ffffff;
  --danger: #c92a2a;
  --focus: #3b5bdb;
  --shadow-sm: 0 1px 2px rgb(60 45 20 / 0.08), 0 1px 1px rgb(60 45 20 / 0.04);
  --shadow-md: 0 6px 18px rgb(60 45 20 / 0.14), 0 2px 4px rgb(60 45 20 / 0.08);
  --shadow-lg: 0 18px 40px rgb(40 30 10 / 0.22);

  --state-idea: #868e96;
  --state-outlined: #4c6ef5;
  --state-drafting: #e67700;
  --state-drafted: #ae3ec9;
  --state-revising: #d6336c;
  --state-done: #2b8a3e;

  --radius: 10px;
  --column-width: 288px;
  --header-height: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1b1916;
    --board: #151311;
    --surface: #26231f;
    --surface-2: #2e2a25;
    --border: #3a352e;
    --border-strong: #4d473e;
    --text: #ece6dc;
    --muted: #a39a8c;
    --primary: #748ffc;
    --primary-text: #10131f;
    --danger: #ff8787;
    --focus: #91a7ff;
    --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-md: 0 6px 18px rgb(0 0 0 / 0.45);
    --shadow-lg: 0 18px 40px rgb(0 0 0 / 0.6);

    --state-idea: #adb5bd;
    --state-outlined: #748ffc;
    --state-drafting: #ffa94d;
    --state-drafted: #da77f2;
    --state-revising: #f783ac;
    --state-done: #69db7c;

    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1b1916;
  --board: #151311;
  --surface: #26231f;
  --surface-2: #2e2a25;
  --border: #3a352e;
  --border-strong: #4d473e;
  --text: #ece6dc;
  --muted: #a39a8c;
  --primary: #748ffc;
  --primary-text: #10131f;
  --danger: #ff8787;
  --focus: #91a7ff;
  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-md: 0 6px 18px rgb(0 0 0 / 0.45);
  --shadow-lg: 0 18px 40px rgb(0 0 0 / 0.6);
  --state-idea: #adb5bd;
  --state-outlined: #748ffc;
  --state-drafting: #ffa94d;
  --state-drafted: #da77f2;
  --state-revising: #f783ac;
  --state-done: #69db7c;
  color-scheme: dark;
}

/* Summer Waves: from the "Summer Waves" Ditto theme (background #f6eedc, text #1f2a33,
   primary #56738c, GoudyStM / Sorts Mill Goudy, photo background in cover mode). */
:root[data-theme="summer"] {
  --bg: #f6eedc;
  --board: #56738c;
  /* The photo is portrait (1440x1920). On landscape screens `cover` crops it
     top and bottom; 58% keeps the sky with its droplets, the horizon (about a
     third of the way down) and the waves splashing in view. */
  --board-bg: url("../assets/summer-waves.jpg") 50% 58% / cover no-repeat, #56738c;
  --header-bg: #f6eedc;
  --surface: #fbf7ee;
  --surface-2: #f6eedc;
  --border: #d8d3c4;
  --border-strong: #bebbb0;
  --text: #1f2a33;
  --muted: #5a6670;
  --primary: #56738c;
  --primary-text: #ffffff;
  --danger: #b5462f;
  --focus: #56738c;
  --shadow-sm: 0 1px 2px rgb(31 42 51 / 0.10), 0 1px 1px rgb(31 42 51 / 0.05);
  --shadow-md: 0 6px 18px rgb(31 42 51 / 0.18), 0 2px 4px rgb(31 42 51 / 0.08);
  --shadow-lg: 0 18px 40px rgb(31 42 51 / 0.30);
  --state-idea: #8b9198;
  --state-outlined: #4f7cac;
  --state-drafting: #c27a1e;
  --state-drafted: #8c6bb1;
  --state-revising: #c4566b;
  --state-done: #4f8a5b;
  --font: "GoudyStM", "Sorts Mill Goudy", "Goudy Old Style", Garamond, Georgia, serif;
  --font-serif: var(--font);
  color-scheme: light;
}

/* Sketch: pencil on graph paper. */
:root[data-theme="sketch"] {
  --bg: #fbfbf6;
  --board: #fbfbf6;
  --board-bg:
    linear-gradient(#e3e9f3 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, #e3e9f3 1px, transparent 1px) 0 0 / 24px 24px,
    #fbfbf6;
  --surface: #ffffff;
  --surface-2: #fbfbf6;
  --border: #2b2b2b;
  --border-strong: #2b2b2b;
  --text: #1f1f1f;
  --muted: #555555;
  --primary: #1f1f1f;
  --primary-text: #ffffff;
  --danger: #c92a2a;
  --focus: #1971c2;
  --shadow-sm: 2px 2px 0 #2b2b2b;
  --shadow-md: 3px 3px 0 #2b2b2b;
  --shadow-lg: 5px 5px 0 #2b2b2b;
  --state-idea: #6c6c6c;
  --state-outlined: #1864ab;
  --state-drafting: #d9480f;
  --state-drafted: #862e9c;
  --state-revising: #c2255c;
  --state-done: #2b8a3e;
  --wobble: 255px 15px 225px 15px / 15px 225px 15px 255px;
  --wobble-alt: 15px 255px 15px 225px / 225px 15px 255px 15px;
  --font: "Patrick Hand", "Segoe Print", "Bradley Hand", "Chalkboard SE", "Comic Sans MS", cursive;
  --font-serif: var(--font);
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ------------------------------------------------------------------ Buttons */

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface); color: var(--text);
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
a.btn { text-decoration: none; }
.btn svg { width: 16px; height: 16px; }
.btn-small { height: 28px; padding: 0 10px; font-size: 13px; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn-danger:hover { background: var(--danger); filter: brightness(1.08); }
.btn-danger-quiet { border-color: transparent; background: transparent; color: var(--danger); padding: 0 8px; }
.btn-danger-quiet:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 34px; height: 34px; padding: 0;
  border: 1px solid transparent; border-radius: 8px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn-small { width: 28px; height: 28px; }
.icon-btn-small svg { fill: currentColor; stroke: none; }

/* ------------------------------------------------------------------- Header */

.app-header {
  flex: none;
  display: flex; align-items: center; gap: 16px;
  height: var(--header-height);
  padding: 0 16px;
  background: var(--header-bg, var(--surface));
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; flex: none; }
.brand-mark { width: 26px; height: 26px; stroke: none; }
.brand h1 { margin: 0; font: 600 17px/1 var(--font-serif); letter-spacing: 0.01em; white-space: nowrap; }

.tabs { flex: 1; min-width: 0; display: flex; align-items: center; gap: 4px; align-self: stretch; }

.tab-list {
  display: flex; align-items: flex-end; gap: 2px;
  min-width: 0; align-self: stretch;
  /* Extend over the header's bottom border so the active tab can cover it. */
  margin-bottom: -1px;
  /* Scrolls sideways (wheel, trackpad, touch) without a visible scrollbar,
     which would otherwise sit between the tabs and the board. */
  overflow-x: auto; overflow-y: hidden; scrollbar-width: none;
  padding-top: 10px;
}
.tab-list::-webkit-scrollbar { display: none; }

.tab {
  position: relative;
  display: flex; align-items: center;
  max-width: 220px; flex: none;
  border: 1px solid transparent; border-bottom: 0;
  border-radius: 8px 8px 0 0;
  color: var(--muted);
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.is-active {
  background: var(--board);
  border-color: var(--border);
  color: var(--text);
}
.tab-name {
  min-width: 0; max-width: 180px;
  height: 36px; padding: 0 4px 0 14px;
  border: 0; background: none; cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 500;
}
.tab-close {
  display: grid; place-items: center;
  width: 24px; height: 24px; margin-right: 6px;
  border: 0; border-radius: 6px; background: none; color: inherit;
  cursor: pointer; opacity: 0;
}
.tab-close svg { width: 14px; height: 14px; }
.tab:hover .tab-close, .tab.is-active .tab-close, .tab-close:focus-visible { opacity: 0.6; }
.tab-close:hover { opacity: 1; background: color-mix(in srgb, var(--text) 10%, transparent); }
.tab .inline-input { width: 160px; margin: 4px 6px; }
.tab-add { flex: none; }

.header-actions { display: flex; align-items: center; gap: 4px; flex: none; }

/* Columns / Cards layout switch */
.view-switch {
  display: inline-flex; gap: 2px; padding: 2px; margin-right: 6px;
  border: 1px solid var(--border); border-radius: 10px;
}
.view-switch .icon-btn { width: 30px; height: 28px; border-radius: 7px; }
.view-switch .icon-btn[aria-pressed="true"] { background: var(--surface-2); color: var(--text); box-shadow: inset 0 0 0 1px var(--border-strong); }
.save-status {
  display: inline-flex; align-items: center; gap: 6px;
  margin-right: 6px; padding: 4px 8px;
  border: 0; border-radius: 6px; background: none;
  font-size: 12px; color: var(--muted); white-space: nowrap; cursor: pointer;
}
.save-status:hover { background: var(--surface-2); color: var(--text); }
.save-status[data-state]:not([data-state=""])::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.save-status[data-state="synced"]::before { background: var(--state-done); }
.save-status[data-state="syncing"]::before { background: var(--primary); animation: pulse 1s ease-in-out infinite alternate; }
.save-status[data-state="error"] { color: var(--danger); font-weight: 600; }
@keyframes pulse { from { opacity: 0.3; } to { opacity: 1; } }

/* -------------------------------------------------------------------- Board */

.board {
  flex: 1; min-height: 0;
  display: flex; align-items: stretch; gap: 0;
  overflow-x: auto; overflow-y: hidden;
  background: var(--board-bg, var(--board));
  overscroll-behavior-x: contain;
}

.subplots { display: flex; align-items: stretch; gap: 14px; padding: 16px 16px 16px 14px; }

.column {
  flex: none;
  display: flex; flex-direction: column;
  width: var(--column-width);
  max-height: 100%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.column.is-drop-target { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); }

.column-main {
  --column-width: 320px;
  margin: 16px 0 16px 16px;
  background: var(--surface);
  max-height: calc(100% - 32px);
}

@media (min-width: 760px) {
  /* Keep the chronological outline in view while scrolling through subplots. */
  .column-main { position: sticky; left: 16px; z-index: 2; box-shadow: var(--shadow-md); }
}

.column-subplot { border-top: 4px solid var(--accent); }

.column-header { padding: 12px 12px 8px 14px; }
.column-title-row { display: flex; align-items: center; gap: 8px; min-height: 28px; }
.column-title {
  flex: 1; min-width: 0; margin: 0;
  font: 600 16px/1.25 var(--font-serif);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.column-title-btn { border: 0; background: none; padding: 2px 4px; margin-left: -4px; text-align: left; border-radius: 6px; cursor: text; }
.column-title-btn:hover { background: color-mix(in srgb, var(--text) 7%, transparent); }
.column-header .inline-input { flex: 1; min-width: 0; }
.column-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.subplot-dot {
  flex: none; width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 25%, transparent);
}

.card-list {
  flex: 1 1 auto; min-height: 64px;
  list-style: none; margin: 0; padding: 4px 10px 10px;
  /* Vertical scrolling stays inside the list, but sideways swipes must reach
     the board (overflow-y: auto makes the list a scroll container on x too). */
  overflow-x: hidden; overflow-y: auto; overscroll-behavior-y: contain;
  display: flex; flex-direction: column; gap: 8px;
  scrollbar-width: thin;
}
.card-list:empty::before {
  content: attr(data-empty);
  display: block; padding: 18px 12px;
  border: 1.5px dashed var(--border-strong); border-radius: var(--radius);
  color: var(--muted); font-size: 13px; text-align: center;
}

.column-footer { display: flex; flex-direction: column; gap: 6px; padding: 8px 10px 10px; border-top: 1px solid var(--border); }

.quick-add { display: flex; gap: 4px; }
.quick-add-input, .add-existing, .inline-input {
  flex: 1; min-width: 0; height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface);
}
.quick-add-input::placeholder { color: var(--muted); }
.quick-add-input:focus, .inline-input:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
.add-existing { flex: none; width: 100%; color: var(--muted); cursor: pointer; }

.add-subplot {
  flex: none; align-self: flex-start;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 200px; height: 52px;
  border: 1.5px dashed var(--border-strong); border-radius: 12px;
  background: transparent; color: var(--muted); cursor: pointer; font-weight: 500;
}
.add-subplot:hover { color: var(--text); border-color: var(--muted); background: color-mix(in srgb, var(--surface) 50%, transparent); }

/* -------------------------------------------------------------------- Cards */

.card {
  position: relative; flex: none;
  display: flex; align-items: flex-start; gap: 8px;
  padding: 10px 10px 10px 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--state-color, var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  cursor: grab;
  user-select: none; -webkit-user-select: none;
  transition: box-shadow 0.12s, border-color 0.12s, transform 0.12s;
}
.card:hover { box-shadow: var(--shadow-md); }
.card.is-linked { border-color: var(--primary); border-left-color: var(--state-color); box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 25%, transparent); }

[data-state="idea"] { --state-color: var(--state-idea); }
[data-state="outlined"] { --state-color: var(--state-outlined); }
[data-state="drafting"] { --state-color: var(--state-drafting); }
[data-state="drafted"] { --state-color: var(--state-drafted); }
[data-state="revising"] { --state-color: var(--state-revising); }
[data-state="done"] { --state-color: var(--state-done); }

.card-grip {
  display: grid; place-items: center; flex: none;
  width: 20px; height: 28px; margin-top: -2px;
  color: var(--border-strong);
  touch-action: none; cursor: grab;
}
.card-grip svg { width: 16px; height: 16px; fill: currentColor; stroke: none; }
.card:hover .card-grip { color: var(--muted); }

.card-num {
  flex: none; min-width: 28px;
  font: 700 15px/1.5 var(--font-serif);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.column-subplot .card-num { color: var(--accent); }

.card-body { flex: 1; min-width: 0; }
.card-title { font-weight: 600; line-height: 1.35; overflow-wrap: anywhere; }
.card-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 6px; }
.card-description {
  margin: 3px 0 0; font-size: 13px; line-height: 1.4; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere;
}
/* Shown when a chapter has notes; hover for a preview. */
.card-notes { display: inline-grid; place-items: center; color: var(--muted); cursor: help; }
.card-notes svg { width: 15px; height: 15px; }

.state-pill {
  display: inline-flex; align-items: center; gap: 5px;
  height: 20px; padding: 0 8px 0 7px;
  border-radius: 999px;
  font-size: 11.5px; font-weight: 600;
  color: var(--state-color);
  background: color-mix(in srgb, var(--state-color) 13%, transparent);
}
.state-pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.card-chapter-ref { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.card-chapter-ref.is-out-of-order { color: var(--state-drafting); font-weight: 600; }
.card-chapter-ref.is-out-of-order::after { content: " ↑"; }

.card-tags { display: contents; }
/* Subplot a chapter belongs to: its name, with the subplot's colour as a dot. */
.tag {
  display: inline-block; max-width: 160px; height: 20px;
  padding: 0 8px 0 7px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--tag) 15%, transparent);
  color: var(--text);
  font-size: 11.5px; font-weight: 600; line-height: 20px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tag::before {
  content: ""; display: inline-block;
  width: 6px; height: 6px; margin-right: 5px; vertical-align: 1px;
  border-radius: 50%; background: var(--tag);
}

.card-remove {
  display: grid; place-items: center; flex: none;
  width: 24px; height: 24px; margin: -2px -4px 0 0;
  border: 0; border-radius: 6px; background: none; color: var(--muted);
  cursor: pointer; opacity: 0;
}
.card-remove svg { width: 14px; height: 14px; }
.card:hover .card-remove, .card:focus-within .card-remove, .card-remove:focus-visible { opacity: 1; }
.card-remove:hover { background: color-mix(in srgb, var(--text) 9%, transparent); color: var(--text); }
@media (hover: none) { .card-remove { opacity: 0.7; } }

/* ---------------------------------------------------------------- Dragging */

body.is-dragging, body.is-dragging * { cursor: grabbing !important; }
.card.is-drag-source { opacity: 0.4; }
.card.is-hidden { display: none; }

.drag-ghost {
  position: fixed; top: 0; left: 0; z-index: 1000;
  margin: 0; pointer-events: none;
  box-shadow: var(--shadow-lg);
  border-color: var(--primary);
  list-style: none;
}
.drag-ghost.is-copy::after {
  content: "+";
  position: absolute; top: -9px; right: -9px;
  display: grid; place-items: center;
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--primary); color: var(--primary-text);
  font-weight: 700; font-size: 15px; line-height: 1;
}
.drag-ghost .card-remove { display: none; }
/* Several chapters dragged together: a stacked look and a count. */
.drag-ghost.is-multi {
  box-shadow: 6px 6px 0 -1px var(--surface), 6px 6px 0 0 var(--border-strong),
    12px 12px 0 -1px var(--surface), 12px 12px 0 0 var(--border-strong), var(--shadow-lg);
}
.drag-count {
  position: absolute; top: -10px; left: -10px;
  display: grid; place-items: center;
  min-width: 24px; height: 24px; padding: 0 6px;
  border-radius: 12px;
  background: var(--primary); color: var(--primary-text);
  font: 700 12.5px/1 var(--font);
}

/* Multi-selection (Ctrl/⌘+click, Shift+click) */
.card.is-selected {
  border-color: var(--primary);
  border-left-color: var(--state-color);
  background: color-mix(in srgb, var(--primary) 7%, var(--surface));
  box-shadow: 0 0 0 2px var(--primary);
}
.card.is-selected::after {
  content: "✓";
  position: absolute; top: -8px; right: -8px;
  display: grid; place-items: center;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--primary); color: var(--primary-text);
  font-size: 12px; font-weight: 700;
}
.selection-bar {
  position: fixed; left: 50%; bottom: 20px; z-index: 1050;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 14px;
  max-width: calc(100vw - 32px);
  padding: 8px 8px 8px 16px;
  border-radius: 12px;
  background: var(--primary); color: var(--primary-text);
  box-shadow: var(--shadow-lg);
}
.selection-count { font-weight: 700; white-space: nowrap; }
.selection-hint { font-size: 12.5px; opacity: 0.85; }
.selection-bar .btn { background: transparent; color: inherit; border-color: color-mix(in srgb, var(--primary-text) 45%, transparent); }
.selection-bar .btn:hover { background: color-mix(in srgb, var(--primary-text) 15%, transparent); }
body.has-selection .toast { bottom: 76px; }

.drop-placeholder {
  flex: none;
  border: 2px dashed color-mix(in srgb, var(--primary) 60%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--primary) 7%, transparent);
}

/* -------------------------------------------------------------------- Menus */

.menu {
  position: fixed; z-index: 900;
  min-width: 220px; max-width: 300px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
}
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 7px 10px; border: 0; border-radius: 6px;
  background: none; text-align: left; cursor: pointer;
}
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: var(--surface-2); outline: none; }
.menu-item:disabled { cursor: default; color: var(--muted); }
.menu-item.is-danger { color: var(--danger); }
.menu-label { flex: 1; }
.menu-hint { font-size: 11.5px; color: var(--muted); }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--border); }
.menu-note { padding: 6px 10px; font-size: 12.5px; color: var(--muted); }
.menu-swatches { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 10px; }
.swatch {
  width: 22px; height: 22px; padding: 0;
  border: 2px solid var(--surface); border-radius: 50%;
  background: var(--swatch); cursor: pointer;
  box-shadow: 0 0 0 1px var(--border);
}
.swatch.is-selected { box-shadow: 0 0 0 2px var(--text); }

/* ------------------------------------------------------------------ Dialogs */

.dialog {
  width: min(480px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--border); border-radius: 14px;
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-lg);
}
.dialog::backdrop { background: rgb(20 15 8 / 0.45); }
.dialog > form, .dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 20px; }
.dialog-body { gap: 0; padding-top: 16px; }
.dialog-wide { width: min(560px, calc(100vw - 32px)); }
.dialog h2 { margin: 0; font: 600 19px/1.3 var(--font-serif); }
.dialog > form > p { margin: 0; color: var(--muted); }
.dialog-small { width: min(400px, calc(100vw - 32px)); }
.dialog-header { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.dialog-body > .dialog-header { align-items: center; margin-bottom: 4px; }
.dialog-close { margin-right: -8px; }

/* Sections inside the Data / Sync / Settings popups */
.panel { padding: 14px 0; border-top: 1px solid var(--border); }
.dialog-header + .panel, .form-error + div > .panel:first-child { border-top: 0; }
.panel-title { margin: 0 0 6px; font-size: 13px; font-weight: 700; letter-spacing: 0.02em; }
.panel-text { margin: 0 0 10px; font-size: 13px; color: var(--muted); }
.panel-note, .panel-warning {
  margin: 10px 0 0; padding: 10px 12px;
  border-radius: 8px; font-size: 13px; line-height: 1.5;
  background: var(--surface-2); color: var(--text);
}
.panel-warning { background: color-mix(in srgb, var(--state-drafting) 12%, transparent); }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row-end { justify-content: flex-end; margin-top: 8px; }
.btn-block { flex: 1; justify-content: center; }
.panel > .btn-block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.is-busy { cursor: progress; }
.btn-danger-outline { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); justify-content: center; }
.btn-danger-outline:hover:not(:disabled) { background: color-mix(in srgb, var(--danger) 9%, transparent); }
.help-btn {
  flex: none; border: 1px solid var(--border-strong); border-radius: 50%;
  font-weight: 700; color: var(--muted);
}
.help-btn[aria-expanded="true"] { background: var(--surface-2); color: var(--text); }

.form-error {
  margin: 0 0 4px; padding: 10px 12px; border-radius: 8px;
  background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger);
  font-size: 13px; font-weight: 500;
}

.key-field { display: flex; gap: 6px; }
.key-field input {
  flex: 1; min-width: 0; height: 34px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px;
}
.key-field input:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }
.generated-key { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.generated-key .panel-warning { margin: 0; }
.generated-key .btn-primary { align-self: flex-start; }
#sync-key-reveal { margin-top: 10px; }

.signin-options { display: flex; flex-direction: column; gap: 8px; }
.signin-options .btn { align-self: flex-start; }
.signin-options .or { font-size: 12px; color: var(--muted); }

.sync-state { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.sync-state .panel-text { margin: 0; }
.sync-state-title { font-weight: 700; }
.sync-dot { flex: none; width: 10px; height: 10px; margin-top: 5px; border-radius: 50%; background: var(--state-idea); }
.sync-dot[data-state="synced"] { background: var(--state-done); }
.sync-dot[data-state="syncing"] { background: var(--primary); animation: pulse 1s ease-in-out infinite alternate; }
.sync-dot[data-state="error"] { background: var(--danger); }
.sync-facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 12px; font-size: 13px; }
.sync-facts dt { color: var(--muted); }
.sync-facts dd { margin: 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.sync-facts code { font-size: 12px; }
.relays summary { cursor: pointer; font-size: 13px; font-weight: 700; }
.relays summary + .panel-text { margin-top: 8px; }
.relays textarea {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; resize: vertical;
}

.about-nostr {
  margin-top: 6px; padding: 14px 16px;
  border-radius: 10px; background: var(--surface-2);
}
.about-nostr h3 { margin: 0 0 6px; font: 600 16px/1.3 var(--font-serif); }
.about-nostr p { margin: 0; font-size: 13px; line-height: 1.55; color: var(--text); }
.about-nostr a { color: var(--primary); }

.theme-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.theme-option {
  display: flex; flex-direction: column; gap: 6px; align-items: center;
  padding: 8px; border: 1px solid var(--border); border-radius: 10px;
  cursor: pointer; text-align: center; font-size: 12.5px;
}
.theme-option:hover { background: var(--surface-2); }
.theme-option:has(input:checked) { border-color: var(--primary); box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 30%, transparent); }
.theme-option:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }
.theme-preview {
  position: relative; display: block; width: 100%; height: 44px;
  border-radius: 6px; border: 1px solid rgb(0 0 0 / 0.12);
  background: var(--p0); overflow: hidden;
}
.theme-preview span { position: absolute; top: 8px; bottom: 8px; width: 26%; border-radius: 3px; background: var(--p1); box-shadow: 0 1px 1px rgb(0 0 0 / 0.12); }
.theme-preview span:nth-child(1) { left: 8%; }
.theme-preview span:nth-child(2) { left: 37%; }
.theme-preview span:nth-child(3) { left: 66%; border-top: 4px solid var(--p2); }
.theme-option[data-theme-option="sketch"] .theme-name { font-family: "Patrick Hand", "Segoe Print", "Bradley Hand", cursive; font-size: 14.5px; }
.theme-option[data-theme-option="sketch"] .theme-preview {
  background:
    linear-gradient(#dfe6f1 1px, transparent 1px) 0 0 / 8px 8px,
    linear-gradient(90deg, #dfe6f1 1px, transparent 1px) 0 0 / 8px 8px, var(--p0);
}
.theme-option[data-theme-option="sketch"] .theme-preview span { border: 1.5px solid #262626; box-shadow: 1px 1px 0 #262626; border-radius: 6px 2px 5px 2px; }
.theme-option[data-theme-option="summer"] .theme-preview { background: url("../assets/summer-waves.jpg") 50% 70% / cover no-repeat, #56738c; }
.theme-option[data-theme-option="summer"] .theme-name { font-family: "GoudyStM", "Sorts Mill Goudy", Georgia, serif; font-size: 14px; }

.delete-list { display: grid; grid-template-columns: auto 1fr; gap: 10px 14px; align-items: center; }
.delete-list .panel-text { margin: 0; }
.dialog-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.dialog-footer { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.spacer { flex: 1; }

.field { display: flex; flex-direction: column; gap: 6px; margin: 0; padding: 0; border: 0; min-width: 0; }
.field-label { font-size: 12.5px; font-weight: 600; color: var(--muted); padding: 0; }
.field-hint { font-weight: 400; font-size: 12px; color: var(--muted); margin: 0; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface);
}
.field textarea { resize: vertical; min-height: 80px; line-height: 1.5; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: transparent; }

.subplot-checks { display: flex; flex-direction: column; gap: 2px; }
.subplot-check { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; cursor: pointer; }
.subplot-check:hover { background: var(--surface-2); }
.subplot-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
.subplot-check-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* -------------------------------------------------------------------- Toast */

.toast {
  position: fixed; left: 50%; bottom: 20px; z-index: 1100;
  transform: translateX(-50%);
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100vw - 32px);
  padding: 10px 10px 10px 16px;
  border-radius: 10px;
  background: var(--text); color: var(--bg);
  box-shadow: var(--shadow-lg);
}
.toast .btn { background: transparent; color: inherit; border-color: color-mix(in srgb, var(--bg) 40%, transparent); }
.toast .btn:hover { background: color-mix(in srgb, var(--bg) 15%, transparent); }

/* ------------------------------------------------------ Summer Waves theme */

[data-theme="summer"] body { font-size: 15px; }
/* Subplot columns and the "Add subplot" button sit on the photo: frosted sand. */
[data-theme="summer"] .column-subplot,
[data-theme="summer"] .subplot-tile {
  background: rgb(246 238 220 / 0.88);
  border-color: rgb(246 238 220 / 0.6);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
[data-theme="summer"] .add-subplot {
  background: rgb(246 238 220 / 0.72);
  border-color: rgb(246 238 220 / 0.95);
  color: var(--text);
  -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
}
[data-theme="summer"] .add-subplot:hover { background: rgb(246 238 220 / 0.9); }
[data-theme="summer"] .column-main { border-color: rgb(246 238 220 / 0.6); }
[data-theme="summer"] .tab.is-active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }

/* ------------------------------------------------------------ Sketch theme */

[data-theme="sketch"] .card,
[data-theme="sketch"] .btn,
[data-theme="sketch"] .quick-add-input,
[data-theme="sketch"] .add-existing,
[data-theme="sketch"] .inline-input,
[data-theme="sketch"] .key-field input,
[data-theme="sketch"] .theme-option,
[data-theme="sketch"] .menu {
  border-width: 2px; border-radius: var(--wobble);
}
[data-theme="sketch"] .card { border-left-width: 6px; }
[data-theme="sketch"] .column,
[data-theme="sketch"] .subplot-tile,
[data-theme="sketch"] .dialog,
[data-theme="sketch"] .add-subplot,
[data-theme="sketch"] .tab.is-active {
  border-width: 2px; border-radius: var(--wobble-alt);
}
[data-theme="sketch"] .column-subplot { border-top-width: 6px; }
[data-theme="sketch"] .column-main { box-shadow: var(--shadow-md); }
[data-theme="sketch"] .tab.is-active { border-bottom: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
[data-theme="sketch"] .app-header { border-bottom: 2px solid var(--border); }
[data-theme="sketch"] .state-pill { border: 1.5px solid currentColor; background: transparent; }
[data-theme="sketch"] .subplot-dot { border-radius: 45% 55% 40% 60%; }
[data-theme="sketch"] .tag { border: 1.5px solid var(--tag); background: transparent; line-height: 17px; border-radius: var(--wobble); }
[data-theme="sketch"] .drop-placeholder { border-radius: var(--wobble); }
[data-theme="sketch"] body { font-size: 15.5px; }
[data-theme="sketch"] .column-title,
[data-theme="sketch"] .card-title { letter-spacing: 0.01em; }

/* ------------------------------------------------------------- Cards view */

/* One subplot as a column; the rest as cards in a grid that fills the rest. */
.board[data-view="cards"] > .column-subplot { margin: 16px 0 16px 14px; max-height: calc(100% - 32px); }
.subplot-grid {
  flex: 1 1 auto; min-width: 480px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  align-content: start; gap: 14px;
  padding: 16px; overflow-y: auto;
}
.subplot-grid .add-subplot { width: auto; height: auto; min-height: 120px; align-self: stretch; }

.subplot-tile {
  display: flex; flex-direction: column; min-width: 0;
  padding: 10px 10px 12px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border); border-top: 4px solid var(--accent);
  border-radius: 12px;
  cursor: pointer;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.subplot-tile:hover { box-shadow: var(--shadow-md); }
.subplot-tile.is-drop-target { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent); }
.tile-head { display: flex; align-items: center; gap: 8px; min-height: 28px; }
.tile-name {
  flex: 1; min-width: 0; padding: 0; border: 0; background: none; text-align: left;
  font: 600 15px/1.3 var(--font-serif);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer;
}
.tile-chapters { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.tile-chapter {
  display: flex; align-items: baseline; gap: 8px;
  padding: 4px 8px 4px 6px;
  border-left: 3px solid var(--state-color, var(--border));
  border-radius: 6px; background: var(--surface);
  font-size: 13px;
}
.tile-chapter.is-linked { box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 35%, transparent); }
.tile-chapter-num { flex: none; min-width: 1.2em; color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.tile-chapter-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-chapter-ref { flex: none; font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tile-more, .tile-empty { margin: 8px 0 0; font-size: 12.5px; color: var(--muted); }

/* ------------------------------------------------------------ Matrix view */

/* The outline only, as a snake: rows alternate direction, joined by arrows. */
.matrix {
  flex: 1 1 auto; min-width: 0;
  display: flex; flex-direction: column;
  margin: 16px; max-height: calc(100% - 32px);
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow-md);
}
.matrix-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 14px 24px 12px; border-bottom: 1px solid var(--border);
}
.matrix-header .quick-add { width: min(360px, 100%); }
.matrix-snake {
  flex: 1 1 auto; overflow-y: auto; overscroll-behavior-y: contain;
  padding: 20px 24px 24px;
  --row-width: calc(var(--per-row) * var(--matrix-card) + (var(--per-row) - 1) * var(--matrix-arrow));
}
.matrix-row {
  display: flex; align-items: stretch;
  width: var(--row-width); max-width: 100%; margin-inline: auto;
}
.matrix-row.is-reversed { flex-direction: row-reverse; }
.matrix-row > .card { flex: 0 0 var(--matrix-card); width: var(--matrix-card); }
.matrix-arrow { display: grid; place-items: center; color: var(--muted); }
.matrix-arrow svg { width: 22px; height: 22px; stroke-width: 2; }
.matrix-row > .matrix-arrow { flex: 0 0 var(--matrix-arrow); }
.matrix-row.is-reversed > .matrix-arrow svg { transform: rotate(180deg); }
/* Turn: a down arrow under the row's last card (right end, or left end when reversed). */
.matrix-turn { display: flex; justify-content: flex-end; width: var(--row-width); max-width: 100%; height: 36px; margin-inline: auto; }
.matrix-turn.is-reversed { justify-content: flex-start; }
.matrix-turn .matrix-arrow { width: var(--matrix-card); }
.matrix-empty { margin: 0; color: var(--muted); }
/* Where a dragged chapter will land. */
.card.drop-left, .card.drop-right { position: relative; }
.card.drop-left::before, .card.drop-right::before {
  content: ""; position: absolute; top: 4px; bottom: 4px; width: 4px;
  border-radius: 2px; background: var(--primary);
}
.card.drop-left::before { left: calc(var(--matrix-arrow, 16px) / -2 - 2px); }
.card.drop-right::before { right: calc(var(--matrix-arrow, 16px) / -2 - 2px); }
[data-theme="summer"] .matrix { background: rgb(246 238 220 / 0.9); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); border-color: rgb(246 238 220 / 0.6); }
[data-theme="sketch"] .matrix { border-width: 2px; border-radius: var(--wobble-alt); }

/* Column switcher: only shown on narrow screens (see Mobile below). */
.column-nav { display: none; }

/* ------------------------------------------------------------------- Mobile */

@media (max-width: 760px) {
  /* One compact header row: logo, story tabs, actions. */
  .app-header { --header-height: 52px; gap: 6px; padding: 0 6px 0 12px; }
  .brand h1 {
    position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap;
  }
  .brand-mark { width: 24px; height: 24px; }
  .tab-list { padding-top: 8px; }
  .tab-name { max-width: 116px; height: 34px; padding-left: 10px; font-size: 13.5px; }
  .tab-close { width: 22px; margin-right: 4px; }
  .header-actions { gap: 0; }
  .view-switch { display: none; }
  .selection-bar { bottom: calc(64px + env(safe-area-inset-bottom)); }
  .selection-hint { display: none; }
  body.has-selection .toast { bottom: calc(120px + env(safe-area-inset-bottom)); }
  .save-status { display: none; }
  #data-btn { width: 34px; height: 34px; padding: 0; justify-content: center; border-color: transparent; background: none; color: var(--muted); }
  #data-btn .btn-label { display: none; }
  .header-actions .icon-btn { width: 32px; }

  /* One column per screen, snapping into place, with a sliver of the next
     column showing so it's clear the board scrolls sideways. */
  .board {
    gap: 12px; padding: 12px 24px;
    scroll-snap-type: x mandatory; scroll-padding-inline: 24px;
  }
  body.is-dragging .board { scroll-snap-type: none; }
  .subplots { gap: 12px; padding: 0; }
  .column, .add-subplot { scroll-snap-align: center; scroll-snap-stop: always; }
  .column-main, .column-subplot { --column-width: calc(100vw - 48px); }
  .column-main { margin: 0; max-height: 100%; }
  .add-subplot { width: calc(100vw - 48px); }

  .column-header { padding: 10px 10px 6px 12px; }
  .card-list { padding: 2px 8px 8px; }
  .column-footer { padding: 6px 8px 8px; }
  .card { padding: 9px 10px 9px 0; gap: 4px; }
  .card-grip { width: 26px; height: 32px; margin-top: -4px; }
  .card-num { min-width: 22px; }
  .tag { max-width: 130px; }

  .column-nav {
    flex: none; display: flex; gap: 6px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    overflow-x: auto; scrollbar-width: none;
    background: var(--header-bg, var(--surface));
    border-top: 1px solid var(--border);
  }
  .column-nav::-webkit-scrollbar { display: none; }
  .column-nav button {
    flex: none; display: inline-flex; align-items: center; gap: 6px;
    max-width: 180px; height: 32px; padding: 0 12px;
    border: 1px solid var(--border); border-radius: 999px;
    background: var(--surface); color: var(--muted);
    font-size: 13px; white-space: nowrap; cursor: pointer;
  }
  .column-nav button span:last-child { overflow: hidden; text-overflow: ellipsis; }
  .column-nav button[aria-current="true"] { border-color: var(--primary); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--primary); }
  .column-nav .subplot-dot { width: 9px; height: 9px; box-shadow: none; }
  [data-theme="summer"] .column-nav { background: #f6eedc; }

  .theme-grid { grid-template-columns: repeat(2, 1fr); }
  .delete-list { grid-template-columns: 1fr; gap: 4px; }
  .delete-list .panel-text { margin-bottom: 8px; }
  .key-field { flex-wrap: wrap; }
  .key-field input { flex-basis: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
