/* Scribe Atelier — design system and shell.
   Tokens first (light by default, dark through the system or the explicit toggle), then
   base elements, shared components, the application shell and finally each space. */

/* ---------------------------------------------------------------------------------- */
/* 1. Tokens                                                                           */
/* ---------------------------------------------------------------------------------- */
:root {
  /* Typography */
  --font-ui:
    -apple-system, BlinkMacSystemFont, 'Segoe UI Variable Text', 'Segoe UI', system-ui, Roboto,
    'Helvetica Neue', 'Noto Sans', Arial, sans-serif;
  --font-read:
    Charter, 'Bitstream Charter', 'Iowan Old Style', 'Sitka Text', 'Source Serif 4',
    'Source Serif Pro', Cambria, 'Noto Serif', Georgia, serif;
  --font-mono: ui-monospace, 'SF Mono', 'Cascadia Mono', Menlo, Consolas, monospace;
  --text-2xs: 0.6875rem;
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-base: 0.9375rem;
  --text-md: 1rem;
  --text-lg: 1.125rem;
  --text-xl: 1.375rem;
  --text-2xl: 1.75rem;
  --text-3xl: 2.125rem;
  --leading-tight: 1.25;
  --leading: 1.55;
  --leading-read: 1.75;
  --measure: 40.5rem; /* ≈ 68 characters of the reading face */
  /* Space, radius, motion */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 20px;
  --s6: 24px;
  --s7: 32px;
  --s8: 48px;
  --r-sm: 6px;
  --r-md: 8px;
  --r-lg: 12px;
  --r-xl: 16px;
  --r-pill: 999px;
  --dur: 140ms;
  --dur-slow: 220ms;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  /* Shell */
  --topbar-h: 56px;
  --sidebar-w: 236px;
  --rail-w: 68px;
  --tabbar-h: 62px;
  --control-h: 38px;
  --control-sm: 32px;

  /* Light theme */
  --bg: #f7f6f3;
  --sidebar-bg: #f1efea;
  --surface: #ffffff;
  --surface-2: #f2f0eb;
  --surface-3: #e9e6df;
  --border: #e4e0d8;
  --border-strong: #cfcac0;
  --text: #1c1b19;
  --muted: #5c584f;
  --faint: #736e64;
  --accent: #8a5a16;
  --accent-fill: #8a5a16;
  --accent-fill-hover: #774d12;
  --on-accent: #ffffff;
  --accent-soft: #f4ead9;
  --accent-on-soft: #6b440d;
  --ring: rgba(138, 90, 22, 0.32);
  --paper: #fffefb;
  --ink: #23211d;
  --paper-muted: #6b665b;
  --selection: #f0d9a8;
  --danger: #b3261e;
  --danger-soft: #fbeae8;
  --danger-border: #f0c4bf;
  --success: #2c6a4c;
  --success-soft: #e5f2eb;
  --success-border: #bcdcca;
  --warning: #85570a;
  --warning-soft: #fbf1dc;
  --warning-border: #ecd3a1;
  --info: #2c5a87;
  --info-soft: #e7eef7;
  --info-border: #c3d4ea;
  --toast-bg: #1f1e1c;
  --toast-text: #f6f4ef;
  --toast-error: #8f1f19;
  --backdrop: rgba(28, 26, 22, 0.38);
  --shadow-sm: 0 1px 2px rgba(28, 27, 25, 0.06);
  --shadow-md: 0 1px 3px rgba(28, 27, 25, 0.06), 0 6px 18px rgba(28, 27, 25, 0.07);
  --shadow-lg: 0 2px 8px rgba(28, 27, 25, 0.08), 0 18px 48px rgba(28, 27, 25, 0.16);
  --mark-typo: #d9e4ef;
  --mark-voice: #f6d6cf;
  --mark-slop: #f3e3ad;
  --mark-rhythm: #d6e4f1;
  --mark-coherence: #e5dbf2;
  --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a857a' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  --search-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%238a857a' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E");
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #111113;
    --sidebar-bg: #141416;
    --surface: #19191c;
    --surface-2: #202024;
    --surface-3: #2a2a2f;
    --border: #2a2a30;
    --border-strong: #3b3b42;
    --text: #ececef;
    --muted: #a3a3ac;
    --faint: #8a8a93;
    --accent: #dcba7d;
    --accent-fill: #d6b273;
    --accent-fill-hover: #e2c088;
    --on-accent: #1b1408;
    --accent-soft: #2a2418;
    --accent-on-soft: #e8cc95;
    --ring: rgba(220, 186, 125, 0.35);
    --paper: #161618;
    --ink: #e6e2da;
    --paper-muted: #9e988c;
    --selection: #5b4a28;
    --danger: #ff9e92;
    --danger-soft: #34201d;
    --danger-border: #5c2f2a;
    --success: #8fd1b0;
    --success-soft: #16261e;
    --success-border: #2c4a3b;
    --warning: #e9c47c;
    --warning-soft: #2c2415;
    --warning-border: #554527;
    --info: #9fc4ec;
    --info-soft: #172331;
    --info-border: #2d425c;
    --toast-bg: #2b2b31;
    --toast-text: #f3f3f5;
    --toast-error: #6b241e;
    --backdrop: rgba(0, 0, 0, 0.58);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 2px 10px rgba(0, 0, 0, 0.45), 0 24px 64px rgba(0, 0, 0, 0.55);
    --mark-typo: #24384d;
    --mark-voice: #4a2621;
    --mark-slop: #43381a;
    --mark-rhythm: #22364d;
    --mark-coherence: #382a4d;
    color-scheme: dark;
  }
}
:root[data-theme='dark'] {
  --bg: #111113;
  --sidebar-bg: #141416;
  --surface: #19191c;
  --surface-2: #202024;
  --surface-3: #2a2a2f;
  --border: #2a2a30;
  --border-strong: #3b3b42;
  --text: #ececef;
  --muted: #a3a3ac;
  --faint: #8a8a93;
  --accent: #dcba7d;
  --accent-fill: #d6b273;
  --accent-fill-hover: #e2c088;
  --on-accent: #1b1408;
  --accent-soft: #2a2418;
  --accent-on-soft: #e8cc95;
  --ring: rgba(220, 186, 125, 0.35);
  --paper: #161618;
  --ink: #e6e2da;
  --paper-muted: #9e988c;
  --selection: #5b4a28;
  --danger: #ff9e92;
  --danger-soft: #34201d;
  --danger-border: #5c2f2a;
  --success: #8fd1b0;
  --success-soft: #16261e;
  --success-border: #2c4a3b;
  --warning: #e9c47c;
  --warning-soft: #2c2415;
  --warning-border: #554527;
  --info: #9fc4ec;
  --info-soft: #172331;
  --info-border: #2d425c;
  --toast-bg: #2b2b31;
  --toast-text: #f3f3f5;
  --toast-error: #6b241e;
  --backdrop: rgba(0, 0, 0, 0.58);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 2px 10px rgba(0, 0, 0, 0.45), 0 24px 64px rgba(0, 0, 0, 0.55);
  --mark-typo: #24384d;
  --mark-voice: #4a2621;
  --mark-slop: #43381a;
  --mark-rhythm: #22364d;
  --mark-coherence: #382a4d;
  color-scheme: dark;
}
:root {
  /* Historical names used by the space stylesheets. */
  --panel: var(--surface);
}
@media (pointer: coarse), (max-width: 760px) {
  :root {
    --control-h: 44px;
    --control-sm: 40px;
  }
}

/* ---------------------------------------------------------------------------------- */
/* 2. Base                                                                             */
/* ---------------------------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}
body {
  margin: 0;
  min-height: 100dvh;
  font: var(--text-base) / var(--leading) var(--font-ui);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}
button,
a,
input,
select,
textarea,
summary {
  -webkit-tap-highlight-color: transparent;
}
a {
  color: var(--accent);
  text-underline-offset: 3px;
}
h1,
h2,
h3,
h4 {
  color: var(--text);
  text-wrap: balance;
}
h1 {
  font-family: var(--font-read);
  font-size: clamp(1.75rem, 3.2vw, var(--text-3xl));
  font-weight: 500;
  letter-spacing: -0.01em;
  line-height: var(--leading-tight);
  margin: var(--s2) 0;
}
h2 {
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: var(--leading-tight);
  letter-spacing: -0.005em;
}
h3 {
  font-size: var(--text-md);
  font-weight: 600;
  margin-top: var(--s6);
}
p {
  line-height: var(--leading);
}
hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: var(--s5) 0;
}
::selection {
  background: var(--selection);
  color: inherit;
}
.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;
}
[hidden] {
  display: none !important;
}
.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}
svg.i {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.skip-link {
  position: absolute;
  left: var(--s3);
  top: -80px;
  z-index: 100;
  padding: var(--s2) var(--s4);
  border-radius: var(--r-md);
  background: var(--surface);
  box-shadow: var(--shadow-md);
}
.skip-link:focus {
  top: var(--s3);
}
#main-start {
  display: block;
  height: 0;
}

/* ---------------------------------------------------------------------------------- */
/* 3. Components                                                                       */
/* ---------------------------------------------------------------------------------- */

/* Buttons: secondary by default, .primary, .ghost / .text-button, .danger. */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--control-h);
  padding: 7px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-sm);
  font-weight: 500;
  line-height: 1.3;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition:
    background-color var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease),
    transform var(--dur) var(--ease);
}
button:hover {
  background: var(--surface-2);
}
button:active:not(:disabled) {
  transform: translateY(0.5px);
}
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: none;
}
button[aria-busy='true'] {
  cursor: progress;
}
.primary,
button.primary {
  background: var(--accent-fill);
  color: var(--on-accent);
  border-color: transparent;
  font-weight: 600;
}
button.primary:hover:not(:disabled) {
  background: var(--accent-fill-hover);
}
button.ghost,
.text-button {
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  color: var(--muted);
}
button.ghost:hover,
.text-button:hover {
  background: var(--surface-2);
  color: var(--text);
}
.text-button {
  min-height: 0;
  padding: 2px 4px;
}
button.danger {
  color: var(--danger);
}
button.danger:hover:not(:disabled) {
  background: var(--danger-soft);
  border-color: var(--danger-border);
}
.icon-button {
  width: var(--control-h);
  padding: 0;
}
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:focus-visible,
summary:focus-visible {
  outline-offset: 2px;
}
/* Headings focused by code after a navigation are not interactive targets. */
[tabindex='-1']:focus-visible,
dialog:focus-visible {
  outline: none;
}

/* Fields */
input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  min-height: var(--control-h);
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-base);
  transition:
    border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease);
}
input::placeholder,
textarea::placeholder {
  color: var(--faint);
}
input:hover,
select:hover,
textarea:hover {
  border-color: var(--faint);
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}
input[type='checkbox'],
input[type='radio'] {
  width: 18px;
  min-width: 18px;
  height: 18px;
  min-height: 0;
  padding: 0;
  accent-color: var(--accent-fill);
  box-shadow: none;
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 36px;
  background-image: var(--chevron);
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
  text-overflow: ellipsis;
}
select[multiple] {
  background-image: none;
  padding-right: 12px;
}
input[type='search'] {
  padding-left: 36px;
  background-image: var(--search-icon);
  background-repeat: no-repeat;
  background-position: 11px center;
  background-size: 16px;
}
textarea {
  resize: vertical;
  line-height: var(--leading);
}
label {
  display: block;
  font-size: var(--text-sm);
  font-weight: 550;
  color: var(--text);
  margin: var(--s4) 0 6px;
}
fieldset {
  border: 1px solid var(--border);
  border-radius: var(--r-md);
}
details > summary {
  cursor: pointer;
}
summary {
  border-radius: var(--r-sm);
}

/* Text helpers */
.eyebrow,
.brand span.eyebrow {
  display: inline-block;
  font-family: var(--font-ui);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.quiet {
  color: var(--muted);
  font-size: var(--text-sm);
  line-height: var(--leading);
}
.error,
#create-error,
#studio-edit-error,
#reader-error {
  color: var(--danger);
}
.row {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex-wrap: wrap;
}
.wide {
  width: 100%;
  margin-top: var(--s3);
}
.kbd,
kbd {
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--muted);
  font: 600 var(--text-2xs) / 1 var(--font-ui);
  letter-spacing: 0.02em;
}

/* Badges (status) */
.badge,
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 22px;
  padding: 1px 9px;
  border: 1px solid var(--border);
  border-radius: var(--r-pill);
  background: var(--surface-2);
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: 0;
  text-transform: none;
  white-space: nowrap;
}
.status-pill:empty {
  display: none;
}
.badge.state-done,
.status-pill[data-status='draft'] {
  background: var(--accent-soft);
  border-color: transparent;
  color: var(--accent-on-soft);
}
.badge.state-applied,
.status-pill[data-status='canon'] {
  background: var(--success-soft);
  border-color: var(--success-border);
  color: var(--success);
}
.badge.state-running,
.badge.state-queued {
  background: var(--info-soft);
  border-color: var(--info-border);
  color: var(--info);
}
.badge.state-running::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.badge.state-failed {
  background: var(--danger-soft);
  border-color: var(--danger-border);
  color: var(--danger);
}

/* Loading skeletons and empty states */
.skeleton {
  display: grid;
  gap: 12px;
  padding: var(--s4) 0;
  max-width: 720px;
}
.skeleton-line {
  height: 14px;
  border-radius: var(--r-sm);
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--surface-3) 50%, var(--surface-2) 100%);
  background-size: 200% 100%;
}
.skeleton-line:nth-child(2) {
  width: 92%;
}
.skeleton-line:nth-child(3) {
  width: 78%;
}
.skeleton-line:nth-child(4) {
  width: 56%;
}
.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s2);
  padding: var(--s7) var(--s4);
  border: 1px dashed var(--border-strong);
  border-radius: var(--r-lg);
  color: var(--muted);
  text-align: center;
}
.empty-state::before {
  content: '';
  width: 28px;
  height: 28px;
  background: var(--faint);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 7h16v11a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z'/%3E%3Cpath d='M4 7l2.5-3h11L20 7'/%3E%3Cpath d='M9.5 11.5h5'/%3E%3C/svg%3E")
    center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 7h16v11a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z'/%3E%3Cpath d='M4 7l2.5-3h11L20 7'/%3E%3Cpath d='M9.5 11.5h5'/%3E%3C/svg%3E")
    center / contain no-repeat;
}
.empty-state p {
  margin: 0;
  max-width: 44ch;
  font-size: var(--text-sm);
}

/* Toast: one feedback zone for the whole app (#notice). */
#notice {
  position: fixed;
  z-index: 70;
  left: 50%;
  bottom: calc(var(--s6) + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: min(560px, calc(100vw - 32px));
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: 11px 16px;
  border-radius: var(--r-lg);
  background: var(--toast-bg);
  color: var(--toast-text);
  box-shadow: var(--shadow-lg);
  font-size: var(--text-sm);
  line-height: 1.45;
}
#notice:empty {
  display: none;
}
#notice.error {
  background: var(--toast-error);
  color: #fff;
}
#notice.toast-action {
  flex-wrap: wrap;
  gap: var(--s2) var(--s4);
}
.toast-button,
#notice button {
  min-height: 32px;
  padding: 4px 12px;
  background: transparent;
  color: inherit;
  border: 1px solid rgba(255, 255, 255, 0.32);
  box-shadow: none;
}
#notice button:hover {
  background: rgba(255, 255, 255, 0.1);
}
body.offline #notice {
  outline: 2px solid var(--danger);
}

/* Dialogs */
dialog {
  width: min(1120px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  padding: 0 var(--s6) var(--s6);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  overflow: auto;
  overscroll-behavior: contain;
}
dialog::backdrop {
  background: var(--backdrop);
  backdrop-filter: blur(2px);
}
.dialog-head {
  position: sticky;
  top: 0;
  z-index: 3;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--s3);
  margin: 0 calc(-1 * var(--s6)) var(--s4);
  padding: var(--s4) var(--s6) var(--s3);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.dialog-head h2 {
  margin: 0;
  font-size: var(--text-lg);
}
@media (prefers-reduced-motion: no-preference) {
  dialog[open] {
    animation: dialog-in var(--dur-slow) var(--ease);
  }
  #notice:not(:empty) {
    animation: toast-in var(--dur-slow) var(--ease);
  }
  .skeleton-line {
    animation: shimmer 1.3s ease-in-out infinite;
  }
}
@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.99);
  }
}
@keyframes toast-in {
  from {
    opacity: 0;
    translate: 0 8px;
  }
}
@keyframes shimmer {
  from {
    background-position: 100% 0;
  }
  to {
    background-position: -100% 0;
  }
}

/* In-app confirmation (replaces window.confirm) */
dialog.confirm-dialog {
  width: min(440px, calc(100vw - 32px));
  height: auto;
  max-height: calc(100dvh - 48px);
  margin: auto;
  padding: var(--s6);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
}
.confirm-dialog h2 {
  margin: 0 0 var(--s2);
  font-size: var(--text-lg);
}
.confirm-message {
  margin: 0;
  color: var(--muted);
  white-space: pre-line;
}
.confirm-actions {
  justify-content: flex-end;
  margin-top: var(--s6);
}
button.danger-fill {
  background: var(--danger);
  border-color: transparent;
  color: var(--surface);
  font-weight: 600;
}
button.danger-fill:hover:not(:disabled) {
  background: var(--danger);
  filter: brightness(1.08);
}

/* Cards and list entries */
.entry {
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s2);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  background: var(--surface);
  overflow-wrap: anywhere;
}
.entry > strong {
  display: block;
  font-size: var(--text-sm);
}
.entry p {
  white-space: pre-wrap;
  margin: 6px 0;
  font-size: var(--text-sm);
}
.entry small {
  color: var(--muted);
  font-size: var(--text-xs);
}
.entry .row {
  margin-top: var(--s2);
}
.entry button {
  min-height: var(--control-sm);
  padding: 4px 10px;
  font-size: var(--text-xs);
  margin-top: 6px;
}
.entry-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0;
}
.job-controls {
  margin-top: var(--s2);
}
.job-controls button {
  margin-top: 0;
}
.job-technical {
  flex-basis: 100%;
  font-size: var(--text-xs);
  color: var(--muted);
}
.job-technical > summary {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 28px;
  padding: 2px 4px;
  list-style: none;
}
.job-technical > summary::-webkit-details-marker {
  display: none;
}
.job-technical > summary::before {
  content: '›';
  display: inline-block;
  transition: transform var(--dur) var(--ease);
}
.job-technical[open] > summary::before {
  transform: rotate(90deg);
}
blockquote {
  margin: var(--s3) 0;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--r-sm) var(--r-sm) 0;
  background: var(--surface-2);
  white-space: pre-wrap;
  font: var(--text-sm) / 1.55 var(--font-read);
  overflow-wrap: anywhere;
  max-height: 160px;
  overflow: auto;
}

/* Safe Markdown rendering for model replies */
.rich-text > :first-child {
  margin-top: 0;
}
.rich-text p {
  margin: 0 0 0.8em;
}
.rich-text h3,
.rich-text h4 {
  margin: 1.1em 0 0.4em;
  font-size: var(--text-md);
}
.rich-text ul,
.rich-text ol {
  padding-left: 22px;
  margin: 0 0 0.8em;
}
.rich-text li + li {
  margin-top: 0.25em;
}
.rich-text code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
}
.rich-text blockquote {
  max-height: none;
  font-family: inherit;
  font-size: inherit;
}
.rich-text a,
.foundation-prose a {
  color: var(--accent);
}
.foundation-prose strong,
.rich-text strong {
  font-weight: 650;
}

/* ---------------------------------------------------------------------------------- */
/* 4. Shell: top bar, project navigation (sidebar / rail / bottom tab bar)             */
/* ---------------------------------------------------------------------------------- */
header.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: var(--s3);
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) var(--s5) 0;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-weight: 650;
  font-size: var(--text-md);
  letter-spacing: -0.01em;
  border-radius: var(--r-md);
  padding: 4px 6px 4px 2px;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--text);
  color: var(--bg);
  font: 500 19px / 1 var(--font-read);
}
.crumb {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 550;
}
.crumb::before {
  content: '/';
  margin-right: 10px;
  color: var(--border-strong);
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: auto;
}
.header-actions button {
  min-height: 36px;
}
.palette-trigger {
  justify-content: flex-start;
  min-width: 220px;
  margin-right: var(--s2);
  border: 1px solid var(--border) !important;
  background: var(--surface) !important;
  color: var(--faint) !important;
}
.palette-trigger:hover {
  border-color: var(--border-strong) !important;
  color: var(--muted) !important;
}
.palette-trigger .kbd {
  margin-left: auto;
}
#theme-toggle[data-mode='dark'] svg {
  transform: rotate(180deg);
}

/* Project navigation */
#studio-nav {
  z-index: 25;
}
.studio-project {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  padding: var(--s2) 10px var(--s3);
}
.studio-project strong {
  font-size: var(--text-md);
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#studio-nav nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#studio-nav nav button {
  justify-content: flex-start;
  gap: 10px;
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
  color: var(--muted);
  font-size: var(--text-sm);
  font-weight: 550;
}
#studio-nav nav button:hover {
  background: var(--surface-3);
  color: var(--text);
}
#studio-nav nav button[aria-current='page'] {
  background: var(--accent-soft);
  color: var(--accent-on-soft);
  font-weight: 650;
}
#studio-nav [data-studio='foundations'] {
  margin-top: var(--s4);
}
#nav-more {
  display: none !important;
}

/* Laptop: fixed sidebar, compact rail while writing. */
@media (min-width: 1024px) {
  body.has-studio-nav {
    padding-left: calc(var(--sidebar-w) + env(safe-area-inset-left));
  }
  body.has-studio-nav.space-write {
    padding-left: calc(var(--rail-w) + env(safe-area-inset-left));
  }
  #studio-nav {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: var(--sidebar-w);
    padding: var(--s3) var(--s3) var(--s5);
    padding-left: calc(var(--s3) + env(safe-area-inset-left));
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    overflow-y: auto;
  }
  .studio-project {
    min-height: calc(var(--topbar-h) - var(--s3));
    justify-content: center;
    padding-top: 0;
    margin-bottom: var(--s2);
  }
  body.has-studio-nav .crumb,
  body.has-studio-nav #studio-eyebrow {
    display: none;
  }
  /* Laptop: toasts sit in the bottom-right corner, away from the reading column. */
  #notice {
    left: auto;
    right: var(--s6);
    transform: none;
    max-width: 420px;
  }
  /* Writing: the navigation folds into an icon rail to give the text its width. */
  .space-write #studio-nav {
    width: var(--rail-w);
    padding-left: 10px;
    padding-right: 10px;
  }
  .space-write .studio-project {
    visibility: hidden;
  }
  .space-write #studio-nav nav button {
    justify-content: center;
    padding: 0;
    min-height: 44px;
  }
  .space-write #studio-nav nav button span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  .space-write #studio-nav nav button svg.i {
    width: 20px;
    height: 20px;
  }
}

/* Tablet: one horizontal row under the top bar. */
@media (min-width: 761px) and (max-width: 1023px) {
  #studio-nav {
    display: flex;
    align-items: center;
    padding: 6px var(--s4);
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    overflow-x: auto;
    scrollbar-width: none;
  }
  .studio-project {
    display: none;
  }
  /* Every space visible without scrolling: icon above a short label, equal columns. */
  #studio-nav nav {
    display: grid;
    grid-template-columns: repeat(9, minmax(0, 1fr));
    width: 100%;
  }
  #studio-nav nav button {
    flex-direction: column;
    gap: 2px;
    width: auto;
    min-height: 52px;
    padding: 4px 2px;
    font-size: var(--text-2xs);
    white-space: nowrap;
  }
  #studio-nav nav span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #studio-nav [data-studio='foundations'] {
    margin-top: 0;
  }
}

/* Phone: bottom tab bar at thumb height, secondary spaces behind "Plus". */
@media (max-width: 760px) {
  header.topbar {
    position: relative;
    height: calc(52px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) var(--s3) 0 var(--s4);
    gap: var(--s2);
  }
  .brand-name {
    display: none;
  }
  .crumb::before {
    margin-right: 8px;
  }
  .header-actions .label,
  .palette-label,
  .palette-trigger .kbd {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  .header-actions button {
    width: 44px;
    min-height: 44px;
    padding: 0;
  }
  .palette-trigger {
    min-width: 0;
    margin-right: 0;
    border-color: transparent !important;
    background: transparent !important;
    color: var(--muted) !important;
  }
  body.has-studio-nav {
    padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--s4));
  }
  #studio-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    padding: 0 max(var(--s2), env(safe-area-inset-right)) env(safe-area-inset-bottom)
      max(var(--s2), env(safe-area-inset-left));
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: saturate(1.4) blur(14px);
    -webkit-backdrop-filter: saturate(1.4) blur(14px);
    border-top: 1px solid var(--border);
  }
  .studio-project {
    display: none;
  }
  #studio-nav nav {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 0;
  }
  #studio-nav nav button {
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    min-height: var(--tabbar-h);
    padding: 6px 2px;
    font-size: var(--text-2xs);
    font-weight: 600;
    letter-spacing: 0.01em;
    border-radius: var(--r-md);
  }
  #studio-nav nav button:hover {
    background: transparent;
  }
  #studio-nav nav button[aria-current='page'] {
    background: transparent;
    color: var(--accent);
  }
  #studio-nav nav button svg.i {
    width: 22px;
    height: 22px;
  }
  #studio-nav nav span {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #nav-more {
    display: flex !important;
  }
  #nav-more[aria-expanded='true'] {
    color: var(--text);
  }
  /* A secondary space is open: "Plus" carries the active state. */
  #studio-nav:not(.more-open):has(.nav-secondary[aria-current='page']) #nav-more {
    color: var(--accent);
  }
  #studio-nav .nav-secondary {
    display: none;
  }
  #studio-nav [data-studio='foundations'] {
    margin-top: 0;
  }
  /* "Plus" opens a sheet: the secondary spaces stack above the tab bar. */
  #studio-nav.more-open {
    border-radius: var(--r-xl) var(--r-xl) 0 0;
    box-shadow: var(--shadow-lg);
    background: var(--surface);
  }
  #studio-nav.more-open::before {
    content: '';
    position: fixed;
    inset: 0;
    z-index: -1;
    background: var(--backdrop);
  }
  #studio-nav.more-open .nav-secondary {
    display: flex;
    order: -1;
    grid-column: 1 / -1;
    flex-direction: row;
    justify-content: flex-start;
    gap: var(--s3);
    min-height: 48px;
    padding: 0 var(--s4);
    font-size: var(--text-base);
    font-weight: 550;
    color: var(--text);
  }
  #studio-nav.more-open .nav-secondary:first-of-type {
    margin-top: var(--s3);
  }
  #studio-nav.more-open .nav-secondary:last-of-type {
    margin-bottom: var(--s2);
    border-bottom: 1px solid var(--border);
    border-radius: var(--r-md) var(--r-md) 0 0;
  }
  #studio-nav.more-open .nav-secondary[aria-current='page'] {
    background: var(--accent-soft);
    color: var(--accent-on-soft);
  }
  #notice {
    bottom: calc(var(--s3) + env(safe-area-inset-bottom));
  }
  body.has-studio-nav #notice {
    bottom: calc(var(--tabbar-h) + var(--s3) + env(safe-area-inset-bottom));
  }
  dialog:not(.palette):not(.confirm-dialog) {
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 100dvh;
    height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
    padding: 0 var(--s4) calc(var(--s5) + env(safe-area-inset-bottom));
  }
  .dialog-head {
    margin: 0 calc(-1 * var(--s4)) var(--s4);
    padding: calc(var(--s3) + env(safe-area-inset-top)) var(--s4) var(--s3);
  }
}

/* Typing on a phone whose keyboard shrinks the viewport: the tab bar steps aside. Only on
   a short viewport, so a tap that moves the focus never lands on a bar that reappears. */
@media (max-width: 760px) and (max-height: 540px) {
  body:has(textarea:focus, input:not([type='checkbox']):not([type='radio']):focus) #studio-nav {
    display: none;
  }
  body:has(#studio-message:focus) #studio-chat-form {
    bottom: var(--s2);
  }
}

/* ---------------------------------------------------------------------------------- */
/* 5. Login and project library                                                        */
/* ---------------------------------------------------------------------------------- */
#login {
  display: grid;
  place-items: center;
  min-height: calc(100dvh - var(--topbar-h));
  padding: var(--s6) var(--s4);
}
.login-box {
  width: min(400px, 100%);
  padding: var(--s7);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-md);
}
.login-box h1 {
  font-size: var(--text-3xl);
  margin: var(--s2) 0 var(--s1);
}
.login-box > p:not(.quiet) {
  color: var(--muted);
  margin-top: 0;
}
#show-password {
  min-height: 32px;
  margin-top: 6px;
  padding: 2px 6px;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  color: var(--muted);
  font-size: var(--text-xs);
}
#show-password:hover {
  color: var(--text);
  background: var(--surface-2);
}
.login-box .primary {
  margin-top: var(--s5);
  width: 100%;
  min-height: 44px;
  font-size: var(--text-base);
}
.intro,
.project-actions,
.project-grid {
  max-width: 960px;
  margin-left: auto;
  margin-right: auto;
  padding: 0 var(--s6);
}
.intro {
  margin-top: var(--s8);
  margin-bottom: var(--s6);
}
.intro h1 {
  font-size: clamp(1.9rem, 4vw, 2.5rem);
}
.intro p {
  color: var(--muted);
  max-width: 52ch;
}
.project-actions {
  margin-bottom: var(--s5);
}
.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
  gap: var(--s4);
  padding-bottom: var(--s8);
}
.project-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-start;
  min-height: 176px;
  padding: var(--s6);
  text-align: left;
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.project-card:hover {
  background: var(--surface);
  border-color: var(--border-strong);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}
.project-card h2 {
  font: 500 var(--text-2xl) / 1.15 var(--font-read);
  margin: var(--s3) 0 0;
}
.project-card span {
  margin-top: auto;
  padding-top: var(--s5);
  color: var(--accent);
  font-size: var(--text-sm);
  font-weight: 600;
}

/* ---------------------------------------------------------------------------------- */
/* 6. Writing space                                                                    */
/* ---------------------------------------------------------------------------------- */
.workbar {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: 10px var(--s6);
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
}
@media (min-width: 761px) {
  .workbar {
    position: sticky;
    top: var(--topbar-h);
    z-index: 20;
    backdrop-filter: saturate(1.3) blur(10px);
    -webkit-backdrop-filter: saturate(1.3) blur(10px);
  }
}
.workbar-title h1 {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}
#back-projects {
  display: none;
}
.work-actions {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex: 1;
  min-width: 0;
}
#scene-picker {
  flex: 0 1 360px;
  min-width: 0;
  font-weight: 550;
  font-size: var(--text-sm);
}
.work-buttons {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: auto;
}
.work-buttons #edit-scene {
  margin-right: var(--s2);
}
.writing-more {
  position: relative;
}
.writing-more > summary {
  display: grid;
  place-items: center;
  width: var(--control-h);
  height: var(--control-h);
  border-radius: var(--r-md);
  color: var(--muted);
  list-style: none;
}
.writing-more > summary::-webkit-details-marker {
  display: none;
}
.writing-more > summary:hover,
.writing-more[open] > summary {
  background: var(--surface-2);
  color: var(--text);
}
.writing-more .menu,
.writing-more > div {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 35;
  display: grid;
  gap: 2px;
  min-width: 232px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
}
.writing-more .menu button {
  justify-content: flex-start;
  width: 100%;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
  font-weight: 500;
}
.writing-more .menu button:hover {
  background: var(--surface-2);
}
.workspace-grid {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 340px;
  gap: var(--s6);
  padding: var(--s5) var(--s6) var(--s8);
  align-items: start;
}
#scenes {
  position: sticky;
  top: calc(var(--topbar-h) + 72px);
  max-height: calc(100dvh - var(--topbar-h) - 92px);
  overflow: auto;
  padding: 0 4px var(--s5) 0;
  scrollbar-width: thin;
}
#scenes details {
  margin-bottom: var(--s3);
}
#scenes summary {
  padding: 6px 10px;
  font-size: var(--text-2xs);
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  line-height: 1.5;
  list-style: none;
}
#scenes summary::-webkit-details-marker {
  display: none;
}
#scenes summary::before {
  content: '▾';
  display: inline-block;
  width: 12px;
  color: var(--faint);
  transition: transform var(--dur) var(--ease);
}
#scenes details:not([open]) > summary::before {
  transform: rotate(-90deg);
}
#scenes button {
  display: block;
  width: 100%;
  min-height: 0;
  margin: 0 0 1px;
  padding: 6px 10px;
  text-align: left;
  border: 0;
  border-radius: var(--r-md);
  background: none;
  box-shadow: none;
  color: var(--muted);
  font-size: var(--text-sm);
}
#scenes button:hover {
  background: var(--surface-2);
  color: var(--text);
}
#scenes button.active {
  background: var(--accent-soft);
  color: var(--accent-on-soft);
}
#scenes .chapter-number {
  display: block;
  color: var(--faint);
  font-size: var(--text-2xs);
  font-weight: 600;
  letter-spacing: 0.04em;
  margin-bottom: 1px;
}
#scenes button span {
  display: block;
  line-height: 1.4;
}
#scenes button.active .chapter-number {
  color: var(--accent);
}
.paper {
  min-width: 0;
  min-height: 70vh;
  padding: clamp(28px, 4vw, 56px) clamp(20px, 4.5vw, 64px) var(--s8);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}
.paper > * {
  max-width: var(--measure);
  margin-left: auto;
  margin-right: auto;
}
.paper-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
  font-size: var(--text-xs);
  color: var(--paper-muted);
}
.paper .eyebrow {
  color: var(--paper-muted);
}
.paper h2#scene-title,
.paper > h2 {
  font: 500 clamp(1.6rem, 2.6vw, 2.1rem) / 1.2 var(--font-read);
  letter-spacing: -0.01em;
  color: var(--ink);
  margin-top: var(--s5);
  margin-bottom: var(--s6);
}
.paper > .row {
  margin-bottom: var(--s2);
}
.paper > .row:has(#validate-scene[hidden]):has(#validate-status:empty) {
  display: none;
}
#manuscript {
  font-family: var(--font-read);
  font-size: 1.1875rem;
  line-height: var(--leading-read);
  font-kerning: normal;
  font-variant-ligatures: common-ligatures;
  hanging-punctuation: first;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  hyphens: manual;
  color: var(--ink);
  border-radius: var(--r-sm);
}
#manuscript:focus-visible {
  outline-offset: 8px;
}
#manuscript::selection,
#manuscript *::selection {
  background: var(--selection);
}
#empty-scene {
  color: var(--paper-muted);
  font-style: italic;
}
#editor {
  margin-bottom: var(--s4);
}
#editor > label {
  margin-top: 0;
  color: var(--paper-muted);
}
#editor .row {
  position: sticky;
  top: calc(var(--topbar-h) + 64px);
  z-index: 4;
  margin: var(--s2) 0 var(--s3);
  padding: var(--s2) 0;
  background: var(--paper);
}
#scene-editor {
  min-height: 60vh;
  padding: var(--s4) var(--s5);
  background: var(--paper);
  color: var(--ink);
  font: 1.125rem / var(--leading-read) var(--font-read);
}
#editor .quiet {
  color: var(--paper-muted);
}
.scene-steps {
  justify-content: space-between;
  margin-top: var(--s8);
  padding-top: var(--s5);
  border-top: 1px solid var(--border);
}
.scene-steps button {
  background: transparent;
  box-shadow: none;
  color: var(--paper-muted);
  max-width: 48%;
}
.scene-steps button:hover {
  color: var(--ink);
}
.scene-steps [data-step='next'] {
  margin-left: auto;
}
#tools {
  position: sticky;
  top: calc(var(--topbar-h) + 72px);
  max-height: calc(100dvh - var(--topbar-h) - 92px);
  overflow: auto;
  padding: 0 2px var(--s5);
  scrollbar-width: thin;
}
.tabs {
  display: flex;
  gap: 2px;
  margin-bottom: var(--s4);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs button {
  min-height: 40px;
  padding: 0 10px;
  margin-bottom: -1px;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  background: none;
  box-shadow: none;
  color: var(--muted);
  font-size: var(--text-sm);
  white-space: nowrap;
}
.tabs button:hover {
  background: none;
  color: var(--text);
}
.tabs button[aria-selected='true'] {
  border-bottom-color: var(--accent);
  color: var(--text);
  font-weight: 650;
}
aside section h2 {
  margin: var(--s2) 0 var(--s2);
  font-size: var(--text-md);
}
#tools h3 {
  font-size: var(--text-xs);
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
#back-to-text {
  width: 100%;
  margin-bottom: var(--s3);
}
.jobs-status:empty {
  display: none;
}
.jobs-status {
  margin: var(--s3) 0 var(--s2);
  font-weight: 600;
}
#versions button {
  display: flex;
  justify-content: flex-start;
  width: 100%;
  margin-bottom: 4px;
  text-align: left;
  font-size: var(--text-xs);
  background: transparent;
  box-shadow: none;
  border-color: var(--border);
}
#versions button:hover {
  background: var(--surface-2);
}
.source-index-state {
  margin: var(--s4) 0 0;
  padding-top: var(--s3);
  border-top: 1px solid var(--border);
  font-size: var(--text-xs);
}
#source-list .entry small {
  margin-left: 6px;
}
#source-content {
  white-space: pre-wrap;
  font: var(--text-sm) / 1.65 var(--font-read);
  overflow-wrap: anywhere;
}
.source-button {
  display: block;
  width: 100%;
  margin: 6px 0;
  text-align: left;
}
.selection-action {
  position: fixed;
  bottom: calc(var(--s5) + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 45;
  min-height: 44px;
  padding: 0 var(--s5);
  border-radius: var(--r-pill);
  box-shadow: var(--shadow-lg);
  white-space: nowrap;
}
.selection-action:active:not(:disabled) {
  transform: translateX(-50%) translateY(0.5px);
}
.reading #workspace .workspace-grid {
  grid-template-columns: minmax(0, 880px);
  justify-content: center;
}
.reading #workspace #tools,
.reading #workspace #scenes {
  display: none;
}
#reading-mode[aria-pressed='true'] {
  background: var(--accent-soft);
  color: var(--accent-on-soft);
}

@media (min-width: 761px) and (max-width: 1150px) {
  /* Narrow laptop / tablet: secondary writing actions become icons (with a tooltip). */
  .work-buttons .label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  .work-buttons button.ghost {
    width: var(--control-h);
    padding: 0;
  }
  .work-buttons #edit-scene {
    white-space: nowrap;
  }
}
@media (max-width: 1150px) {
  .workspace-grid {
    grid-template-columns: minmax(0, 1fr) 320px;
  }
  #scenes {
    display: none;
  }
}
@media (max-width: 760px) {
  .workbar {
    padding: var(--s2) var(--s3);
    border-bottom: 0;
  }
  .work-actions {
    flex-wrap: wrap;
    gap: 6px;
  }
  #scene-picker {
    flex: 1 1 100%;
  }
  .work-buttons {
    width: 100%;
    margin-left: 0;
    gap: 0;
  }
  .work-buttons #edit-scene {
    flex: 1;
    margin-right: var(--s1);
    white-space: nowrap;
  }
  .work-buttons #edit-scene svg {
    display: none;
  }
  .work-buttons .label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }
  .work-buttons button.ghost {
    width: 44px;
    padding: 0;
  }
  .work-buttons .label-extra {
    display: none;
  }
  .writing-more > summary {
    width: 44px;
    height: 44px;
  }
  .workspace-grid {
    display: block;
    padding: 0 0 var(--s7);
  }
  .paper {
    min-height: 40vh;
    padding: var(--s4) var(--s4) var(--s7);
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }
  .paper h2#scene-title,
  .paper > h2 {
    font-size: 1.5rem;
    margin: var(--s3) auto var(--s4);
  }
  #manuscript {
    font-size: 1.125rem;
    line-height: 1.7;
  }
  #editor .row {
    top: 0;
  }
  #scene-editor {
    min-height: 60dvh;
    padding: var(--s3);
    font-size: 1.0625rem;
  }
  #tools {
    position: static;
    max-height: none;
    padding: var(--s5) var(--s4);
    border-top: 8px solid var(--surface-2);
  }
  .selection-action {
    bottom: calc(var(--tabbar-h) + var(--s3) + env(safe-area-inset-bottom));
  }
  .reading #workspace .workspace-grid {
    display: block;
  }
  .row input:not([type='checkbox']):not([type='radio']) {
    flex: 1;
    min-width: 160px;
  }
}

/* Comparison reader */
.comparison {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--s4);
}
.comparison.single {
  grid-template-columns: minmax(0, 760px);
  justify-content: center;
}
.compare-text {
  padding: var(--s6);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--paper);
  color: var(--ink);
  font: 1.0625rem / var(--leading-read) var(--font-read);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.compare-text.rich-text {
  white-space: normal;
  font: var(--text-base) / 1.65 var(--font-ui);
}
.compare-label {
  margin-bottom: var(--s2);
  font-size: var(--text-2xs);
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
#reader-note {
  margin-top: 0;
}
#reader .reader-head {
  flex-wrap: wrap;
  gap: var(--s2);
}
#reader .reader-head h2 {
  flex: 1 1 auto;
}
.compare-tabs {
  display: none;
}
#comparison input {
  margin-top: var(--s4);
}
#comparison a {
  color: var(--accent);
}
@media (max-width: 760px) {
  .comparison {
    grid-template-columns: 1fr;
  }
  #reader .reader-head h2 {
    flex: 1 1 0;
    min-width: 0;
  }
  #reader .reader-head #apply {
    order: 3;
    flex: 1 1 100%;
  }
  #reader .reader-head .compare-tabs {
    order: 4;
  }
  #close-reader {
    background: transparent;
    border-color: transparent;
    box-shadow: none;
  }
  .compare-tabs:not([hidden]) {
    display: flex;
    flex: 1 1 100%;
    gap: 2px;
    padding: 3px;
    border-radius: var(--r-md);
    background: var(--surface-2);
  }
  .compare-tabs button {
    flex: 1;
    min-height: var(--control-sm);
    border-color: transparent;
    background: transparent;
    box-shadow: none;
    color: var(--muted);
  }
  .compare-tabs [aria-selected='true'] {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
  }
  .comparison[data-show='0'] > section:nth-child(2),
  .comparison[data-show='1'] > section:nth-child(1) {
    display: none;
  }
  .compare-text {
    padding: var(--s4);
  }
}

/* Small dialogs */
#create-dialog,
#studio-edit {
  max-width: 600px;
}
#export-dialog {
  max-width: 720px;
}
#research-dialog {
  max-width: 880px;
}
#kdrive-link {
  display: block;
  color: var(--accent);
  padding: var(--s4) 0;
}
#export-list a {
  color: var(--accent);
}
#studio-field-present_characters {
  min-height: 120px;
}
#studio-edit-fields > div:has(#studio-field-chapter) {
  max-width: 220px;
}
#studio-edit-form > .primary,
#create-form > .primary {
  margin-top: var(--s5);
}
#research-results .research-summary {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.6;
}
#research-results a {
  color: var(--accent);
  overflow-wrap: anywhere;
}
#research-results label {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: var(--text-base);
  font-weight: 400;
  line-height: 1.5;
}
#research-results details {
  border-top: 1px solid var(--border);
  padding: var(--s4) 0;
}
#research-results summary {
  font-size: var(--text-md);
  line-height: 1.5;
}
#research-results .source-note {
  white-space: pre-wrap;
  font-size: var(--text-sm);
  color: var(--muted);
}
#research-results .source-choice {
  padding: var(--s3) 0;
}
#research-notice {
  color: var(--accent);
}

/* ---------------------------------------------------------------------------------- */
/* 7. Studio spaces: overview, chat, plan, bible                                       */
/* ---------------------------------------------------------------------------------- */
#studio-page {
  max-width: 1120px;
  margin: 0 auto;
  padding: var(--s7) var(--s7) var(--s8);
}
.studio-heading {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: var(--s5);
  margin-bottom: var(--s6);
}
.studio-heading h1 {
  margin: 2px 0 0;
  font-size: clamp(1.75rem, 3vw, 2.25rem);
}
.studio-heading .row {
  justify-content: flex-end;
}
#studio-notice:empty {
  display: none;
}
#studio-notice {
  color: var(--danger);
}
#studio-content > h2 {
  margin: var(--s7) 0 var(--s3);
  font-size: var(--text-md);
}
#studio-content > button {
  display: block;
  margin: 10px 0;
  max-width: 100%;
  text-align: left;
}
#studio-content > input[type='search'],
.studio-filter input {
  max-width: 420px;
  margin-bottom: var(--s4);
}
.studio-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--s3);
}
.studio-stats > div {
  padding: var(--s4) var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.studio-stats strong {
  display: block;
  font-size: var(--text-2xl);
  font-weight: 650;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.studio-stats span {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: var(--text-sm);
}
.studio-next {
  margin: var(--s4) 0;
  padding: var(--s5) var(--s6);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: linear-gradient(135deg, var(--accent-soft), var(--surface) 70%);
}
.studio-next h2 {
  margin: 0 0 var(--s1);
  font: 500 var(--text-xl) / 1.25 var(--font-read);
}
.studio-next p {
  margin: 0 0 var(--s4);
  color: var(--muted);
}
.studio-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
  gap: var(--s3);
  margin-top: var(--s4);
}
.studio-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--s2);
  min-width: 0;
  padding: var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  transition:
    border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease);
}
.studio-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-sm);
}
.studio-card h2 {
  margin: 0;
  font-size: var(--text-md);
}
.studio-card p {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.studio-card > button {
  margin-top: auto;
}
.studio-card > p + button {
  margin-top: var(--s2);
}

/* Proposals (chat, overview) */
.studio-proposal,
.proposal-card {
  margin-bottom: var(--s2);
  padding: var(--s3) var(--s4);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.proposal-head {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.proposal-head strong {
  font-size: var(--text-base);
}
.studio-proposal summary {
  display: inline-block;
  margin-top: 6px;
  font-size: var(--text-sm);
  color: var(--accent);
  line-height: 1.5;
}
.studio-proposal dl {
  margin: var(--s2) 0 0;
}
.studio-proposal dd {
  margin: 2px 0 var(--s2);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: var(--text-sm);
}
.studio-proposal dt {
  color: var(--muted);
  font-size: var(--text-xs);
}
.proposal-actions,
.proposal-confirm {
  margin-top: var(--s3);
}
.proposal-confirm span {
  font-size: var(--text-sm);
  font-weight: 600;
}
.proposal-card[data-state='applied'],
.proposal-card[data-state='dismissed'] {
  background: var(--surface-2);
}
.proposal-card[data-state='applied'] .proposal-head strong,
.proposal-card[data-state='dismissed'] .proposal-head strong {
  color: var(--muted);
}

/* Chat */
.studio-chat-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: var(--s7);
  align-items: start;
}
.studio-chat-grid > section {
  min-width: 0;
}
.chat-dialogue {
  display: flex;
  flex-direction: column;
  gap: var(--s2);
}
.chat-dialogue > .quiet:first-child {
  margin: 0;
}
@media (min-width: 761px) {
  .chat-dialogue {
    height: calc(100dvh - var(--topbar-h) - 150px);
    min-height: 420px;
  }
  .chat-thread {
    flex: 1;
    min-height: 0;
    max-height: none;
  }
  .chat-dialogue > :not(.chat-thread) {
    flex: none;
  }
}
.chat-decisions {
  position: sticky;
  top: calc(var(--topbar-h) + var(--s5));
  padding: var(--s4);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.chat-decisions h2 {
  margin: 0 0 var(--s2);
  font-size: var(--text-md);
}
.chat-thread {
  list-style: none;
  margin: 0;
  padding: 0 6px 0 0;
  max-height: min(64vh, 780px);
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}
.chat-turn {
  display: flex;
  flex-direction: column;
  padding: var(--s4) 0 var(--s5);
  border-bottom: 1px solid var(--border);
}
.chat-turn:last-child {
  border-bottom: 0;
}
.chat-turn > .eyebrow {
  align-self: flex-end;
  margin: 0 0 6px;
}
.chat-bubble.mine {
  align-self: flex-end;
  max-width: min(85%, 60ch);
  margin: 0 0 var(--s3);
  padding: 9px 14px;
  border-radius: 16px 16px 4px 16px;
  background: var(--accent-soft);
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 240px;
  overflow-y: auto;
}
.chat-turn.pending .chat-bubble.mine {
  opacity: 0.7;
}
.chat-answer {
  max-width: var(--measure);
  font-size: var(--text-md);
  line-height: 1.65;
}
.chat-answer::before {
  content: 'Scribe';
  display: block;
  margin-bottom: 4px;
  font-size: var(--text-2xs);
  font-weight: 650;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}
.chat-stage {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 4px 0;
  color: var(--muted);
  font-size: var(--text-sm);
}
.chat-stage::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}
@media (prefers-reduced-motion: no-preference) {
  .chat-stage::before {
    animation: pulse 1.4s ease-in-out infinite;
  }
}
@keyframes pulse {
  50% {
    opacity: 0.25;
    transform: scale(0.8);
  }
}
.chat-error {
  color: var(--danger);
}
.chat-turn > button {
  align-self: flex-start;
  margin-top: var(--s2);
}
.chat-jump {
  position: sticky;
  bottom: var(--s2);
  align-self: center;
  border-radius: var(--r-pill);
  box-shadow: var(--shadow-md);
}
.chat-older {
  align-self: center;
}
.chat-starters h2 {
  font: 500 var(--text-xl) / 1.3 var(--font-read);
  margin: var(--s4) 0 var(--s3);
}
.chat-followups button,
.chat-starters button {
  min-height: var(--control-sm);
  padding: 5px 14px;
  border-radius: var(--r-pill);
  font-size: var(--text-sm);
  font-weight: 500;
  text-align: left;
}
.chat-hint {
  margin-left: auto;
  color: var(--faint);
}
.chat-proposals {
  margin-top: var(--s3);
}
.chat-proposals > .eyebrow {
  margin: 0 0 6px;
}
#studio-chat-form {
  padding: var(--s2) var(--s2) var(--s2) var(--s3);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-lg);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition:
    border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease);
}
#studio-chat-form:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}
#studio-chat-form > label {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}
#studio-chat-form textarea {
  min-height: 56px;
  padding: 6px 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  resize: none;
}
#studio-chat-form textarea:focus-visible {
  box-shadow: none;
}
#studio-chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  column-gap: var(--s2);
}
#studio-chat-form > .draft-offer {
  grid-column: 1 / -1;
}
#studio-chat-form .row {
  display: contents;
}
#studio-chat-form .chat-hint {
  grid-column: 1 / -1;
  margin: 2px 0 0;
  font-size: var(--text-xs);
}
#studio-chat-form textarea {
  field-sizing: content;
  max-height: 32vh;
}
#studio-chat-form > .row > button {
  margin-top: 0;
}
.studio-chat-grid form > button {
  margin-top: var(--s3);
}

/* Plan */
.studio-outline {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr));
  gap: var(--s3);
  align-items: start;
}
.studio-outline > h2 {
  grid-column: 1 / -1;
  margin: var(--s5) 0 0;
  padding-bottom: var(--s2);
  border-bottom: 1px solid var(--border);
  font-size: var(--text-xs);
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
}
.studio-outline > h2:first-child {
  margin-top: 0;
}
.studio-scene {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: var(--s4) var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
}
.studio-scene h3 {
  margin: 6px 0 4px;
  font: 500 var(--text-lg) / 1.3 var(--font-read);
}
.studio-scene > p {
  margin: 0 0 var(--s2);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.studio-scene dl {
  margin: 0 0 var(--s2);
}
.studio-scene p,
.studio-scene dd {
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.studio-scene dt {
  color: var(--muted);
  font-size: var(--text-2xs);
  font-weight: 650;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.studio-scene dd {
  margin: 2px 0 var(--s2);
  font-size: var(--text-sm);
}
.studio-scene > .row {
  margin-top: auto;
  padding-top: var(--s2);
  gap: 4px;
}
.studio-scene > .row button {
  min-height: var(--control-sm);
  padding: 4px 10px;
  font-size: var(--text-xs);
}
.studio-scene > .row .move {
  width: var(--control-sm);
  padding: 0;
  font-size: var(--text-sm);
}
.studio-scene > .row .move:first-of-type {
  margin-left: auto;
}
.studio-research-list details {
  padding: var(--s4) 0;
  border-bottom: 1px solid var(--border);
}
.studio-research-list summary {
  line-height: 1.6;
}
.studio-research-list button {
  margin-top: var(--s4);
}

@media (max-width: 760px) {
  #studio-page {
    padding: var(--s5) var(--s4) var(--s7);
  }
  .studio-heading {
    display: block;
    margin-bottom: var(--s4);
  }
  .studio-heading h1 {
    font-size: 1.75rem;
  }
  .studio-heading .row {
    justify-content: flex-start;
    margin-top: var(--s3);
  }
  .studio-stats {
    grid-template-columns: 1fr 1fr;
    gap: var(--s2);
  }
  .studio-stats > div {
    padding: var(--s3) var(--s4);
  }
  .studio-stats strong {
    font-size: var(--text-xl);
  }
  .studio-next {
    padding: var(--s4);
  }
  .studio-chat-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--s5);
  }
  .chat-decisions {
    position: static;
  }
  .chat-thread {
    max-height: none;
    overflow: visible;
  }
  .chat-hint {
    display: none;
  }
  #studio-chat-form {
    position: sticky;
    bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--s2));
    z-index: 5;
    box-shadow: 0 0 0 var(--s2) var(--bg);
  }
  #studio-chat-form:focus-within {
    box-shadow:
      0 0 0 3px var(--ring),
      0 0 0 var(--s2) var(--bg);
  }
  #studio-chat-form textarea {
    min-height: 44px;
  }
  .studio-outline {
    grid-template-columns: minmax(0, 1fr);
  }
}

/* Citations and passages (sources) */
.citation {
  display: inline;
  min-height: 0;
  padding: 1px 5px;
  margin: 0 2px;
  border-color: transparent;
  box-shadow: none;
  background: var(--accent-soft);
  color: var(--accent-on-soft);
  font-size: 0.85em;
}
.document-passage {
  padding: var(--s4) 0;
  border-bottom: 1px solid var(--border);
}
.document-passage strong {
  color: var(--accent);
  font-size: var(--text-xs);
}
.document-passage p {
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.cited-passage {
  border-left: 3px solid var(--accent);
  padding-left: var(--s4);
}

/* ---------------------------------------------------------------------------------- */
/* 8. Foundations (a document screen)                                                  */
/* ---------------------------------------------------------------------------------- */
#foundations-dialog {
  width: 100%;
  max-width: 1200px;
  min-height: calc(100dvh - var(--topbar-h));
  margin: 0 auto;
  padding: var(--s6) var(--s7) var(--s8);
}
#foundations-dialog .dialog-head {
  position: static;
  margin: 0 0 var(--s4);
  padding: 0;
  border: 0;
  background: none;
  align-items: flex-start;
}
#foundations-title {
  font-size: clamp(1.75rem, 3vw, 2.25rem);
  margin: 2px 0 0;
}
.foundation-phases {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.foundation-phases button {
  min-height: var(--control-sm);
  border-color: transparent;
  background: transparent;
  box-shadow: none;
  color: var(--muted);
}
.foundation-phases [aria-pressed='true'] {
  background: var(--surface);
  color: var(--text);
  font-weight: 650;
  box-shadow: var(--shadow-sm);
}
.foundation-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  gap: var(--s7);
  margin-top: var(--s4);
}
.foundation-grid section {
  min-width: 0;
}
#foundations-dialog #foundation-conversation {
  max-width: 760px;
}
.foundation-prose,
.voice-profile dd {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.65;
}
.foundation-exchange {
  padding: var(--s4) 0;
  border-bottom: 1px solid var(--border);
}
.foundation-exchange > summary {
  font-size: var(--text-sm);
}
.foundation-exchange > button {
  margin-top: var(--s3);
}
#foundation-reference summary,
#foundation-editor > summary {
  line-height: 1.6;
}
.human-message {
  border-left: 3px solid var(--accent);
  padding-left: var(--s3);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  color: var(--muted);
}
.voice-sample {
  margin: var(--s4) 0;
  padding: var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--paper);
  color: var(--ink);
  font: 1.0625rem / 1.75 var(--font-read);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.voice-profile dt {
  margin-top: var(--s4);
  font-size: var(--text-sm);
  color: var(--accent);
}
.voice-profile dd {
  margin: 6px 0 var(--s4);
  font-size: var(--text-sm);
}
#foundation-form {
  margin-top: var(--s5);
  padding-top: 2px;
  border-top: 1px solid var(--border);
}
#foundation-form label {
  font-weight: 600;
}
#foundation-form .row,
#foundation-save-form .row {
  margin-top: var(--s4);
}
#foundation-reference details {
  margin-bottom: var(--s4);
}
#foundation-notice {
  color: var(--accent);
}
#foundation-notice:empty,
#foundation-guidance:empty {
  display: none;
}
#foundation-editor {
  margin-top: var(--s5);
  padding-top: var(--s4);
  border-top: 1px solid var(--accent);
}
.foundation-next {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s2);
  margin: var(--s2) 0 var(--s4);
}
.foundation-next:empty {
  display: none;
}
.foundation-next p {
  display: none;
}
#foundation-working:empty {
  display: none;
}
#foundation-working summary {
  color: var(--accent);
  line-height: 1.6;
  padding: 10px 0;
}
#foundation-working .row {
  margin: var(--s4) 0;
}
#foundation-reference h4 {
  font-weight: 500;
  color: var(--muted);
  font-size: var(--text-sm);
}
#foundation-history {
  margin-top: var(--s6);
  padding-top: var(--s4);
  border-top: 1px solid var(--border);
}
#foundation-history > summary,
#foundation-records > summary,
.foundation-request > summary {
  padding: 10px 0;
  color: var(--muted);
  line-height: 1.5;
}
.foundation-request {
  margin-bottom: var(--s3);
}
.foundation-request .human-message {
  font-size: var(--text-sm);
}
#foundation-history-list .foundation-exchange {
  margin-left: 10px;
}
#foundation-records {
  margin: var(--s3) 0;
}
.foundation-dossier {
  padding: var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface);
  align-self: start;
}
.foundation-dossier > h3:first-child {
  margin-top: 0;
}
#foundation-stage-status {
  margin-top: 0;
}
#foundation-conversation article {
  padding-top: 0;
}
#foundation-current-title {
  margin-top: var(--s3);
}
@media (max-width: 760px) {
  #foundations-dialog {
    padding: var(--s5) var(--s4) var(--s7);
  }
  #foundations-dialog .dialog-head {
    flex-direction: column;
    gap: var(--s2);
  }
  .foundation-phases {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }
  .foundation-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--s5);
  }
  .foundation-dossier {
    padding: var(--s4);
  }
  .voice-sample {
    padding: var(--s4);
  }
}

/* ---------------------------------------------------------------------------------- */
/* 9. Narrative map                                                                    */
/* ---------------------------------------------------------------------------------- */
#map-dialog {
  width: min(1220px, calc(100vw - 32px));
}
#map-title {
  overflow-wrap: anywhere;
}
.map-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s3);
  align-items: center;
}
.map-toolbar > .row {
  gap: 2px;
  padding: 3px;
  border-radius: var(--r-md);
  background: var(--surface-2);
}
.map-toolbar > .row button {
  min-height: var(--control-sm);
  border-color: transparent;
  background: transparent;
  box-shadow: none;
  color: var(--muted);
}
.map-toolbar [aria-pressed='true'] {
  background: var(--surface) !important;
  color: var(--text) !important;
  font-weight: 650;
  box-shadow: var(--shadow-sm) !important;
}
.map-toolbar input {
  width: 240px;
  flex: 1;
  min-width: 130px;
}
.map-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.8fr) minmax(240px, 1fr);
  gap: var(--s7);
  align-items: start;
}
#map-board {
  min-width: 0;
}
.map-root {
  padding: var(--s4) 0;
  border-bottom: 2px solid var(--accent);
  font: 500 var(--text-xl) var(--font-read);
  color: var(--text);
}
.map-part {
  margin: 0;
  padding: var(--s3) 0;
  border: 0;
  border-radius: 0;
}
.map-part > summary {
  padding: 10px 0;
  font-weight: 650;
}
.map-branches {
  display: grid;
  gap: var(--s3);
  margin: 0 0 0 var(--s3);
  padding: var(--s2) 0 var(--s2) var(--s6);
  border-left: 1px solid var(--border-strong);
}
.map-branch {
  position: relative;
  min-width: 0;
}
.map-branch::before {
  content: '';
  position: absolute;
  left: calc(-1 * var(--s6));
  top: 24px;
  width: var(--s6);
  border-top: 1px solid var(--border-strong);
}
.map-node {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  width: 100%;
  min-height: 68px;
  padding: var(--s3) var(--s4);
  text-align: left;
  overflow-wrap: anywhere;
}
.map-node small,
.map-branch > small {
  color: var(--muted);
  font-size: var(--text-xs);
}
.map-node strong {
  font-weight: 600;
}
.map-node[aria-pressed='true'] {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.map-cast {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
  gap: var(--s3);
}
.map-branch > small {
  display: block;
  padding: 5px 0;
}
.map-network {
  margin-top: var(--s6);
  border-top: 1px solid var(--border);
}
#map-detail {
  min-width: 0;
  padding: var(--s4) var(--s5);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  background: var(--surface-2);
  overflow-wrap: anywhere;
}
#map-detail:empty {
  display: none;
}
#map-detail h3 {
  margin-top: var(--s3);
}
#map-detail button {
  display: flex;
  margin: var(--s2) 0;
  max-width: 100%;
  text-align: left;
}
.map-prose {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: var(--text-sm);
  line-height: 1.6;
}
#map-notes {
  margin-top: var(--s6);
}
#map-foundations details {
  margin: var(--s3) 0;
}
@media (max-width: 700px) {
  .map-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--s5);
  }
  .map-branches {
    padding-left: var(--s4);
  }
  .map-branch::before {
    left: calc(-1 * var(--s4));
    width: var(--s4);
  }
  .map-toolbar input {
    width: 100%;
    flex-basis: 100%;
  }
}

/* ---------------------------------------------------------------------------------- */
/* 10. Multi-device helpers                                                             */
/* ---------------------------------------------------------------------------------- */
.draft-offer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s2);
  margin: var(--s2) 0;
  padding: var(--s2) var(--s3);
  border: 1px dashed var(--accent);
  border-radius: var(--r-md);
  background: var(--accent-soft);
  font-size: var(--text-sm);
}
.draft-offer button {
  min-height: var(--control-sm);
  padding: 4px 12px;
}

/* ---------------------------------------------------------------------------------- */
/* 11. Command palette                                                                 */
/* ---------------------------------------------------------------------------------- */
dialog.palette {
  width: min(620px, calc(100vw - 24px));
  height: auto;
  max-height: min(560px, calc(100dvh - 120px));
  margin: 12vh auto auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
dialog.palette[open] {
  display: flex;
}
.palette-field {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: 0 var(--s4);
  border-bottom: 1px solid var(--border);
  color: var(--faint);
}
.palette-field svg.i {
  width: 20px;
  height: 20px;
}
.palette-field input {
  min-height: 56px;
  border: 0;
  padding: 0;
  background: none;
  font-size: var(--text-lg);
  color: var(--text);
  box-shadow: none !important;
}
.palette-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.palette-group {
  padding: 10px 10px 4px;
  font-size: var(--text-2xs);
  font-weight: 650;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--faint);
}
.palette-item {
  display: flex;
  align-items: center;
  gap: var(--s3);
  min-height: 40px;
  padding: 6px 10px;
  border-radius: var(--r-md);
  color: var(--text);
  cursor: pointer;
}
.palette-item svg.i {
  color: var(--muted);
}
.palette-item .palette-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-sm);
}
.palette-item small {
  color: var(--faint);
  font-size: var(--text-xs);
  white-space: nowrap;
}
.palette-item[aria-selected='true'] {
  background: var(--accent-soft);
  color: var(--accent-on-soft);
}
.palette-item[aria-selected='true'] svg.i {
  color: var(--accent);
}
.palette-empty {
  padding: var(--s5);
  color: var(--muted);
  text-align: center;
}
.palette-foot {
  margin: 0;
  padding: 8px var(--s4);
  border-top: 1px solid var(--border);
  color: var(--faint);
  font-size: var(--text-xs);
}
@media (max-width: 760px) {
  dialog.palette {
    width: calc(100vw - 16px);
    height: auto;
    max-height: calc(100dvh - 24px);
    margin: calc(8px + env(safe-area-inset-top)) auto auto;
    border: 1px solid var(--border);
    border-radius: var(--r-xl);
  }
  .palette-foot {
    display: none;
  }
}

/* ---------------------------------------------------------------------------------- */
/* 12. Motion preferences                                                              */
/* ---------------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
