:root {
  --night: #191726;
  --surface: #232036;
  --surface-2: #2b2742;
  --line: #3A3550;
  --paper: #ECE5D6;
  --muted: #A49DB5;
  --garance: #D9738B;
  --guede: #8FA3E6;
  --danger: #E08A7A;
  --prose: "Spectral", Georgia, serif;
  --ui: "Instrument Sans", system-ui, sans-serif;
  --gutter: 16px;
  --column: 36em;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; background: var(--night); color: var(--paper); }

body {
  font-family: var(--ui);
  font-size: 16px;
  line-height: 1.45;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

a { color: var(--paper); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Top bar */
.bar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 10px;
  background: var(--night);
  border-bottom: 1px solid var(--line);
}
.mark {
  font-family: var(--prose);
  font-size: 22px;
  text-decoration: none;
  letter-spacing: 0.02em;
}
.menu-button {
  margin-left: auto;
  background: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 14px;
  color: var(--muted);
  cursor: pointer;
}
.menu {
  position: absolute;
  top: 100%;
  right: var(--gutter);
  display: flex;
  flex-direction: column;
  min-width: 180px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 30px rgb(0 0 0 / 0.35);
}
.menu a {
  padding: 10px 12px;
  border-radius: 8px;
  text-decoration: none;
}
.menu a:hover, .menu a[aria-current="page"] { background: var(--surface-2); }

@media (min-width: 760px) {
  .menu-button { display: none; }
  .menu, .menu[hidden] {
    display: flex !important;
    position: static;
    flex-direction: row;
    margin-left: auto;
    padding: 0;
    background: none;
    border: 0;
    box-shadow: none;
    min-width: 0;
  }
  .menu a { color: var(--muted); padding: 6px 10px; }
  .menu a[aria-current="page"] { color: var(--paper); background: none; }
}

main {
  max-width: 760px;
  margin: 0 auto;
  padding: 20px var(--gutter) 40px;
  outline: none;
}

h1, h2, h3 { font-weight: 500; margin: 0; }
h1 { font-family: var(--prose); font-size: 28px; line-height: 1.2; }
h2 { font-size: 18px; }
h3 { font-size: 16px; }

.muted { color: var(--muted); }
.small { font-size: 14px; }
.error { color: var(--danger); }

.head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Buttons and fields */
.button, button.primary, button.quiet, button.danger, label.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
button.primary { background: var(--paper); color: var(--night); border-color: var(--paper); font-weight: 600; }
button.quiet { background: none; color: var(--muted); }
button.danger { background: none; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }
button:disabled { opacity: 0.5; cursor: default; }
label.button input[type="file"] { display: none; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > span { color: var(--muted); font-size: 14px; }
.field small { color: var(--muted); }
input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--muted); }
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
textarea.prose-input { font-family: var(--prose); font-size: 17px; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 0 12px; }
.check { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 14px; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--paper); }

/* Switch */
.switch { position: relative; display: inline-block; width: 40px; height: 24px; flex: none; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch span {
  position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background 0.15s;
  pointer-events: none;
}
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--muted); transition: transform 0.15s, background 0.15s;
}
.switch input:checked + span { background: color-mix(in srgb, var(--guede) 55%, var(--line)); }
.switch input:checked + span::after { transform: translateX(16px); background: var(--paper); }
.switch input:focus-visible + span { outline: 2px solid var(--muted); outline-offset: 2px; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.list > li { border-bottom: 1px solid var(--line); }
.list a.item, .list .item {
  display: block;
  padding: 16px 2px;
  text-decoration: none;
}
.item-title { font-family: var(--prose); font-size: 21px; line-height: 1.25; }
.item-sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.empty { color: var(--muted); padding: 24px 0; }
.badge {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted); margin-left: 6px; vertical-align: middle;
}
.badge.live { color: var(--guede); border-color: color-mix(in srgb, var(--guede) 50%, transparent); }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 14px;
}
.card .card-head { display: flex; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 12px; }

fieldset { border: 0; padding: 0; margin: 0 0 22px; min-width: 0; }
legend { font-weight: 600; margin-bottom: 10px; padding: 0; }

/* Reading view */
.reader { max-width: none; padding: 0 0 0; }
.story-head {
  max-width: calc(var(--column) + 64px);
  margin: 0 auto;
  padding: 18px var(--gutter) 6px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
}
.story-head .item-sub { margin-top: 4px; }
.story {
  max-width: calc(var(--column) + 64px);
  margin: 0 auto;
  padding: 8px var(--gutter) 24px;
}
.prose {
  font-family: var(--prose);
  font-size: 19px;
  line-height: 1.7;
  max-width: var(--column);
  overflow-wrap: break-word;
}
.prose p { margin: 0 0 1em; }
.prose hr { border: 0; text-align: center; margin: 1.6em 0; color: var(--muted); }
.prose hr::after { content: "* * *"; letter-spacing: 0.6em; }
.prose em { font-style: italic; }
.prose blockquote { margin: 0 0 1em; padding-left: 1em; border-left: 2px solid var(--line); color: var(--muted); }
.prose details { font-family: var(--ui); font-size: 14px; line-height: 1.5; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin: 0 0 1em; }
.prose summary { cursor: pointer; color: var(--muted); }
.prose img { max-width: 100%; height: auto; }
.prose table { border-collapse: collapse; font-family: var(--ui); font-size: 14px; }
.prose td, .prose th { border: 1px solid var(--line); padding: 4px 8px; }

/* The weave: each turn of Lila or the author is a coloured thread in the margin. */
.turn { position: relative; padding-left: 28px; margin: 0 0 8px; }
.turn::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 6px;
  bottom: 10px;
  width: 3px;
  border-radius: 3px;
  background: var(--thread, transparent);
  opacity: 0.85;
}
.turn.lila { --thread: var(--garance); }
.turn.author { --thread: var(--guede); }
.turn.plain::before { background: transparent; }
.cue {
  position: relative;
  font-family: var(--ui);
  font-size: 15px;
  color: var(--thread);
  margin: 0 0 14px;
  white-space: pre-wrap;
}
.cue::before {
  content: "";
  position: absolute;
  left: -24px;
  top: 6px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--night);
  border: 3px solid var(--thread);
}
.cue .who { display: block; font-size: 12px; letter-spacing: 0.04em; opacity: 0.8; margin-bottom: 2px; }
.passage { position: relative; cursor: pointer; border-radius: 6px; }
.passage.selected { background: color-mix(in srgb, var(--surface) 55%, transparent); box-shadow: 0 0 0 8px color-mix(in srgb, var(--surface) 55%, transparent); }
.passage.streaming::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 1em;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--muted);
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }
.passage-tools {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
  margin: -6px 0 18px;
  font-family: var(--ui);
}
.passage-tools button { min-height: 34px; padding: 4px 12px; font-size: 14px; }
.versions { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 14px; margin-right: 6px; }
.versions button { min-width: 34px; padding: 4px 8px; }
.notice { font-size: 14px; color: var(--muted); margin: -4px 0 18px; }

.start-card { text-align: center; padding: 40px 0; }

/* Composer */
.composer {
  position: sticky;
  bottom: 0;
  z-index: 10;
  background: linear-gradient(to bottom, transparent, var(--night) 18px);
  padding: 18px var(--gutter) calc(10px + env(safe-area-inset-bottom));
}
.composer-inner {
  max-width: calc(var(--column) + 64px);
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 8px;
  border-left: 4px solid var(--mode-color, var(--garance));
}
.composer.author { --mode-color: var(--guede); }
.composer textarea {
  background: none;
  border: 0;
  min-height: 48px;
  max-height: 40vh;
  font-family: var(--prose);
  font-size: 17px;
  padding: 6px 8px;
  resize: none;
}
.composer-row { display: flex; align-items: center; gap: 8px; padding: 2px; }
.modes { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 2px; }
.modes button {
  border: 0; background: none; border-radius: 999px; padding: 6px 12px; cursor: pointer; color: var(--muted); font-size: 14px;
}
.modes button[aria-pressed="true"].lila { background: color-mix(in srgb, var(--garance) 22%, transparent); color: var(--garance); }
.modes button[aria-pressed="true"].author { background: color-mix(in srgb, var(--guede) 22%, transparent); color: var(--guede); }
.composer-row .spacer { flex: 1; }
.composer-row button.primary, .composer-row button.quiet { min-height: 36px; padding: 6px 14px; }

/* Panels */
dialog.sheet {
  width: min(560px, calc(100vw - 2 * var(--gutter)));
  max-height: calc(100dvh - 40px);
  background: var(--surface);
  color: var(--paper);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 20px;
}
dialog.sheet::backdrop { background: rgb(10 9 16 / 0.7); }
dialog.sheet h2 { margin-bottom: 14px; font-family: var(--prose); font-size: 22px; }
dialog form > button.primary { width: 100%; margin-top: 6px; }
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; flex-wrap: wrap; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: calc(100vw - 2 * var(--gutter));
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--paper);
  color: var(--night);
  box-shadow: 0 10px 30px rgb(0 0 0 / 0.4);
}
.toast.error { background: var(--danger); }

/* Modules */
.module { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--line); }
.module:last-child { border-bottom: 0; }
.module-body { flex: 1; min-width: 0; }
.module-name { overflow-wrap: anywhere; }
.module-meta { color: var(--muted); font-size: 13px; }
.module-edit { margin-top: 10px; }
.module-edit textarea { min-height: 160px; font-family: ui-monospace, monospace; font-size: 13px; }
.module.marker .module-name { color: var(--muted); font-style: italic; }
.module-description { margin: 6px 0 0; color: var(--muted); font-size: 14px; line-height: 1.5; }
.token-summary { margin: 4px 0 8px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; font-size: 14px; line-height: 1.5; }
.preset-notes { color: var(--muted); font-size: 15px; line-height: 1.55; margin: 0 0 18px; max-width: 42em; }
.section-label { color: var(--muted); font-size: 14px; margin: 22px 0 4px; }
code, .mono { font-family: ui-monospace, monospace; font-size: 13px; overflow-wrap: anywhere; }

.repeat { border-left: 2px solid var(--line); padding-left: 12px; margin-bottom: 16px; }
.repeat-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }

@media (max-width: 520px) {
  h1 { font-size: 24px; }
  .prose { font-size: 18px; }
  .turn { padding-left: 22px; }
  .turn::before { left: 5px; }
  .cue::before { left: -21px; }
  /* Reader: one compact line of title and buttons, so the story starts high on the screen. */
  .story-head { align-items: center; padding-top: 12px; }
  .story-head > div:first-child { min-width: 0; }
  .story-head h1 { font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .story-head .item-sub { font-size: 13px; margin-top: 0; }
  .story-head .actions { flex-wrap: nowrap; gap: 6px; }
  .story-head .actions button { padding: 6px 10px; }
  .story { padding-left: 10px; padding-right: 12px; }
  .composer { padding-left: 8px; padding-right: 8px; padding-top: 12px; }
  .composer-row { gap: 6px; }
  .composer-row button.primary, .composer-row button.quiet { padding: 6px 10px; }
}

/* Fingers, not a mouse: every control at least 40px tall. */
@media (pointer: coarse) {
  .passage-tools button, .composer-row button.primary, .composer-row button.quiet { min-height: 40px; }
  .versions button { min-width: 40px; }
  .modes button { min-height: 36px; padding: 6px 14px; }
  .menu-button { min-height: 40px; }
  .menu a { padding: 13px 14px; }
  input[type="radio"], input[type="checkbox"] { width: 20px; height: 20px; }
}
.passage-tools.quick { margin-top: -10px; }
.passage-tools.quick button { color: var(--muted); }

/* AU assistant */
.working { display: flex; gap: 12px; align-items: flex-start; margin: 12px 0; }
.spinner { flex: none; width: 20px; height: 20px; margin-top: 2px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--guede); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.clock { font-variant-numeric: tabular-nums; color: var(--muted); }
.change .check { margin-bottom: 8px; }
.change-text { white-space: pre-wrap; margin: 8px 0 0; font-size: 15px; line-height: 1.55; }
.change details summary { color: var(--muted); font-size: 14px; cursor: pointer; }

.update {
  display: block; width: 100%; border: 0; padding: 10px var(--gutter);
  background: var(--guede); color: var(--night); font-weight: 600; cursor: pointer; text-align: center;
}

/* Credit */
.stat-big { margin: 8px 0 10px; }
.stat-big .stat-value { font-family: var(--prose); font-size: 30px; line-height: 1.2; }
.meter { height: 6px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.meter-fill { display: block; height: 100%; border-radius: inherit; background: var(--guede); }
.meter-fill.warn { background: var(--danger); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin: 18px 0 12px; }
.stat { padding: 12px 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.stat-value { font-size: 20px; font-weight: 500; margin: 2px 0; }
.stat-value.warn { color: var(--danger); }
.days { display: flex; align-items: flex-end; gap: 4px; height: 96px; }
.day { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; min-width: 0; }
.day-bar { width: 100%; max-width: 22px; border-radius: 4px 4px 0 0; background: var(--garance); }
.day-label { color: var(--muted); font-size: 11px; }
.spend-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.spend-line { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; font-size: 15px; margin-bottom: 4px; }

/* Chapters */
.chapter-heading {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-family: var(--prose);
  font-size: 24px;
  margin: 28px 0 18px 28px;
  scroll-margin-top: 80px;
}
.chapter-heading span { font-family: var(--ui); font-size: 13px; }
.chapter-hint {
  margin: 8px 0 18px 28px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 15px;
}
.chapter-hint p { margin: 0 0 10px; }
.rewrite { margin: -6px 0 18px; }
.rewrite textarea { min-height: 60px; font-size: 15px; }
.rewrite .passage-tools { margin: 8px 0 4px; }

.chapters {
  position: fixed;
  z-index: 30;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(360px, 88vw);
  padding: calc(env(safe-area-inset-top) + 16px) var(--gutter) calc(env(safe-area-inset-bottom) + 16px);
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -12px 0 30px rgb(0 0 0 / 0.35);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 0.2s ease, visibility 0.2s;
}
.chapters.open { transform: none; visibility: visible; }
.chapters-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.chapters-head h2 { font-family: var(--prose); font-size: 22px; }
.chapter-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.chapter { padding: 10px 0; border-bottom: 1px solid var(--line); }
.chapter.current { border-bottom: 0; }
.chapter-title {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  width: 100%;
  padding: 0;
  background: none;
  border: 0;
  text-align: left;
  font-family: var(--prose);
  font-size: 18px;
  line-height: 1.3;
  cursor: pointer;
}
div.chapter-title { cursor: default; }
.chapter-title span { font-family: var(--ui); font-size: 12px; }
.chapter details { margin-top: 6px; }
.chapter summary { color: var(--muted); font-size: 14px; cursor: pointer; }
.chapter-summary { white-space: pre-wrap; font-size: 14px; line-height: 1.55; margin: 8px 0; }
.chapter-stale { color: var(--danger); font-size: 13px; margin: 4px 0 0; }
.chapter .actions button { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.chapter.current .button { margin-top: 8px; }

@media (min-width: 1100px) {
  .reader { padding-right: 340px; }
  .chapters {
    top: 57px;
    width: 340px;
    transform: none;
    visibility: visible;
    box-shadow: none;
    background: var(--night);
    padding-top: 20px;
  }
  button.chapters-toggle, button.chapters-close { display: none; }
}

@media (max-width: 520px) {
  .chapter-heading, .chapter-hint { margin-left: 22px; }
}

/* Continuity, goal, notebook */
.continuity {
  margin: 8px 0 18px 28px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line));
  border-radius: 12px;
  font-size: 15px;
}
.continuity p { margin: 0 0 6px; color: var(--danger); }
.continuity ul { margin: 0 0 10px; padding-left: 18px; line-height: 1.5; }
.goal { margin-top: 8px; }
.goal-text { font-size: 14px; line-height: 1.5; margin: 0 0 4px; }
.goal .actions button, .notebook .actions button { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.goal > button.quiet { min-height: 30px; padding: 2px 10px; font-size: 13px; }
.notebook { margin-top: 22px; padding-top: 14px; border-top: 1px solid var(--line); }
.note-group { margin-bottom: 12px; }
.note-group h3 { color: var(--muted); font-size: 13px; font-weight: 500; margin-bottom: 4px; }
.note-group ul { margin: 0; padding-left: 16px; font-size: 14px; line-height: 1.5; }
.notebook .field textarea { min-height: 52px; font-size: 14px; }

@media (max-width: 520px) {
  .continuity { margin-left: 22px; }
}

/* One chapter per page */
.chapter-page-head { margin: 4px 0 22px 28px; }
.chapter-page-head .chapter-heading { margin: 0 0 10px; }
.chapter-page-head > .button { min-height: 34px; padding: 4px 12px; font-size: 14px; }
.previously { font-size: 14px; color: var(--muted); max-width: var(--column); }
.previously summary { cursor: pointer; }
.previously .chapter-summary { color: var(--paper); opacity: 0.85; }
.fresh { margin-left: 28px; font-size: 15px; }
.chapter-title[aria-current="page"] { color: var(--paper); }
.chapter-title[aria-current="page"]::before { content: ""; display: block; width: 18px; height: 3px; border-radius: 3px; background: var(--guede); margin-bottom: 4px; }
button.chapter-title { color: inherit; }

@media (max-width: 520px) {
  .chapter-page-head, .fresh { margin-left: 22px; }
}
.chapter-page-foot { margin: 28px 0 40px 28px; }

/* Ideas for the chapter */
.ideas { margin: 0 0 24px 28px; max-width: var(--column); }
.ideas .section-label { margin-top: 0; }
.idea-list { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 8px; }
.idea {
  display: flex; flex-direction: column; gap: 4px; width: 100%; text-align: left;
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
  color: var(--paper); cursor: pointer; font-size: 15px; line-height: 1.45;
}
.idea strong { font-family: var(--prose); font-size: 18px; font-weight: 500; }
.idea span { color: var(--muted); }
.idea:hover, .idea:focus-visible { border-color: var(--muted); }
.ideas .actions button { min-height: 36px; padding: 4px 12px; font-size: 14px; }
.goal .actions { margin-top: 4px; }
@media (max-width: 520px) { .ideas { margin-left: 22px; } }
