:root {
  color-scheme: dark;
  --bg: #1c1c1c;
  --editor-bg: #262626;
  --preview-bg: #272727;
  --gutter-bg: #262626;
  --divider: #383838;
  --active-line: #323232;
  --text: #f1e8cc;
  --muted: #a09b91;
  --secondary-text: #c5c0b6;
  --syntax-comment: #a49889;
  --syntax-teal: #80b6ab;
  --syntax-green: #a4c58b;
  --syntax-red: #f18076;
  --choice: #cbc7be;
  --choice-hover: #fff2d5;
  --control-bg: #2c2c2c;
  --control-border: #51504c;
  --selection: #484a42;
  --ui-font: system-ui, -apple-system, sans-serif;
  --editor-font: Menlo, Monaco, Consolas, monospace;
  --editor-size: 14px;
  --editor-line-height: 1.5;
  --preview-size: 16px;
  --preview-line-height: 1.6;
}

* { box-sizing: border-box; }
body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px var(--ui-font);
}
h1, h2, p { margin: 0; }
header {
  padding: .65rem 1rem;
  border-bottom: 1px solid var(--divider);
}
h1 { font-size: .95rem; font-weight: 550; }
h2 { font-size: .8rem; font-weight: 550; color: var(--secondary-text); }
main { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); min-height: 0; }
.editor, .player { display: grid; min-height: 0; }
.editor {
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--editor-bg);
  border-right: 1px solid var(--divider);
}
.player { grid-template-rows: auto minmax(0, 1fr); background: var(--preview-bg); }
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  min-height: 2.6rem;
  padding: .3rem 1rem;
  border-bottom: 1px solid var(--divider);
}
button { font: inherit; color: inherit; cursor: pointer; }
#compile {
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  border-radius: 3px;
  padding: .25rem .65rem;
  font-size: .8rem;
}
#compile:hover { background: var(--active-line); }
:focus-visible { outline: 2px solid var(--syntax-teal); outline-offset: 2px; }

#source { min-width: 0; min-height: 0; }
#source .cm-editor { height: 100%; }
#source .cm-editor.cm-focused { outline: none; }
#source .cm-scroller { font: var(--editor-size)/var(--editor-line-height) var(--editor-font); }
#source .cm-content { padding: .5rem 0; caret-color: var(--text); }
#source .cm-line { padding: 0 .75rem; }
#source .cm-gutters { border: 0; color: var(--muted); background: var(--gutter-bg); }
#source .cm-lineNumbers .cm-gutterElement { min-width: 3em; padding: 0 .65em 0 .35em; }
#source .cm-activeLine, #source .cm-activeLineGutter { background: var(--active-line); }
#source .cm-activeLineGutter { color: var(--secondary-text); }
#source .cm-content ::selection { background: var(--selection); }
.ink-comment { color: var(--syntax-comment); }
.ink-keyword { color: var(--syntax-red); }
.ink-reference { color: var(--syntax-teal); }
.ink-variable { color: var(--syntax-green); }
.ink-string { color: var(--text); }

#status {
  padding: .4rem 1rem;
  min-height: 1.9rem;
  max-height: 22vh;
  overflow: auto;
  border-top: 1px solid var(--divider);
  background: var(--bg);
  color: var(--muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: .78rem;
}
#status[data-error="true"] { color: var(--syntax-red); }
#scene {
  text-align: var(--story-text-align, left);
  padding: 1.25rem clamp(1rem, 3vw, 3rem);
  overflow: auto;
  overflow-wrap: anywhere;
  font: var(--preview-size)/var(--preview-line-height) var(--ui-font);
}
#scene button {
  display: block;
  width: 100%;
  max-width: 100%;
  border: 0;
  background: transparent;
  padding: .2rem 0;
  text-align: inherit;
}

@media (prefers-reduced-motion: no-preference) {
  #scene > button { animation: var(--story-fade-animation, story-fade-in) var(--story-fade-duration) ease-out; }
}
@keyframes story-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

#scene button:hover { color: var(--choice-hover); }
#scene .choice { color: var(--choice); }
#scene .choice + .choice { margin-top: .35rem; }
#scene .choice:hover { color: var(--choice-hover); }

@media (max-width: 640px) {
  main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 3fr) minmax(0, 2fr); }
  .editor { border-right: 0; border-bottom: 1px solid var(--divider); }
}

header { display: flex; align-items: center; gap: 1rem; }
.menu { position: relative; font-size: .82rem; color: var(--secondary-text); }
.menu summary { cursor: pointer; }
.menu-items {
  position: absolute;
  z-index: 10;
  min-width: max-content;
  padding: .25rem;
  border: 1px solid var(--control-border);
  background: var(--bg);
}
.menu-items button, .navigation button {
  background: transparent;
  border: 0;
  color: var(--secondary-text);
  padding: .3rem .6rem;
}
.menu-items button { display: block; width: 100%; text-align: left; }
.menu-items button:hover, .navigation button:hover:enabled { background: var(--active-line); }
.navigation { display: flex; gap: .25rem; }
.navigation button { font-size: 1rem; }
.navigation button:disabled { opacity: .4; cursor: default; }
#source-title { min-width: 0; overflow-wrap: anywhere; }
#compile { flex: none; }
#filename { margin-left: .4rem; font-weight: 400; }
#dirty, #stale { color: var(--muted); font-weight: 400; }
#diagnostics {
  border-top: 1px solid var(--divider);
  padding: .35rem 1rem;
  background: var(--bg);
  color: var(--secondary-text);
  font-size: .78rem;
}
#diagnostics summary { cursor: pointer; }
#issues {
  margin: .25rem 0 0;
  padding: .25rem;
  list-style: none;
  max-height: 20vh;
  overflow: auto;
  overflow-wrap: anywhere;
}
#issues li { padding: .3rem 0; }
#issues button { border: 0; background: transparent; text-align: left; padding: 0; }
#issues button:hover { text-decoration: underline; }
#issues p { color: var(--secondary-text); margin-top: .15rem; }
#issues .error { color: var(--syntax-red); }
#issues .warning { color: var(--text); }
#issues .info { color: var(--syntax-teal); }

#new-project-dialog {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--control-border);
  padding: 1rem;
}
.dialog-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; }
.dialog-actions button {
  background: var(--control-bg);
  border: 1px solid var(--control-border);
  padding: .3rem .65rem;
}

.settings > summary { display: block; color: var(--text); }
.settings > summary::-webkit-details-marker { display: none; }
.settings-panel { min-width: 15rem; max-width: calc(100vw - 2rem); padding: .75rem; }
.settings-panel h2 { margin-bottom: .75rem; }
.fade-label, .fade-toggle { display: flex; align-items: center; gap: .5rem; }
.fade-label { justify-content: space-between; }
#blank-delay, #fade-duration { display: block; width: 100%; margin: .35rem 0 .65rem; }
#blank-delay, #fade-duration, #fade-enabled { accent-color: var(--syntax-teal); }

.text-alignment { border: 0; margin: .75rem 0 0; padding: 0; }
.text-alignment legend { padding: 0; margin-bottom: .35rem; }
.alignment-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: .25rem; }
.alignment-options label { position: relative; cursor: pointer; }
.alignment-options input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.alignment-options span {
  display: block;
  padding: .25rem .35rem;
  border: 1px solid var(--control-border);
  text-align: center;
}
.alignment-options input:checked + span {
  color: var(--text);
  background: var(--active-line);
  border-color: var(--syntax-teal);
}
.alignment-options input:focus-visible + span { outline: 2px solid var(--syntax-teal); outline-offset: 2px; }
#source .cm-ink-jump { background: var(--selection); }
#preview-section { color: var(--muted); font-weight: 400; }
#play-from-here { font-size: .8rem; }
#draft-dialog { background: var(--bg); color: var(--text); border: 1px solid var(--control-border); }
#draft-list { display: grid; gap: .35rem; margin: .65rem 0; max-height: 45vh; overflow: auto; }
#draft-status { color: var(--muted); font-size: .76rem; padding: 0 .75rem .3rem; }
#draft-status:empty { display: none; }
#live-compile, #show-story-map { accent-color: var(--syntax-teal); }
#source .cm-tooltip-autocomplete { background: var(--bg); color: var(--text); border: 1px solid var(--divider); }
#source .cm-tooltip-autocomplete > ul > li[aria-selected] { background: var(--selection); color: var(--text); }
#source .cm-completionDetail { color: var(--muted); }

.menu-items button[hidden] { display: none; }
#file-status { color: var(--muted); font-size: .76rem; padding: 0 .75rem .3rem; }
#file-status:empty { display: none; }

.story-tabs { justify-content: flex-start; gap: .4rem; }
.story-tabs button {
  padding: .3rem .55rem;
  border: 0;
  border-bottom: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font-size: .82rem;
}
.story-tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--syntax-teal); }
.story-pane { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; }
.story-pane[hidden] { display: none; }
.story-controls { min-height: 2rem; padding-block: .15rem; }
.story-controls p { color: var(--muted); font-size: .78rem; }
#map-panel .navigation button { font-size: .78rem; white-space: nowrap; }
#story-map {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--editor-bg);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
#story-map:focus-visible { outline-offset: -3px; }
#story-map.map-dragging { cursor: grabbing; }
.map-edge { fill: none; stroke: var(--muted); stroke-width: 1.25; }
.map-conditional { stroke-dasharray: 4 3; }
#map-arrow { fill: var(--muted); }
.map-edge-label {
  fill: var(--secondary-text);
  stroke: var(--editor-bg);
  stroke-width: 4px;
  paint-order: stroke;
  text-anchor: middle;
  dominant-baseline: central;
  font-size: 12px;
}
.map-node { cursor: pointer; }
.map-node rect { fill: var(--control-bg); stroke: var(--control-border); stroke-width: 1.2; }
.map-node text {
  fill: var(--text);
  text-anchor: middle;
  dominant-baseline: central;
  font: 13px var(--editor-font);
  pointer-events: none;
}
.map-stitch rect, .map-entry rect { stroke: var(--syntax-teal); }
.map-unresolved { cursor: inherit; }
.map-unresolved rect { stroke: var(--syntax-red); stroke-dasharray: 4 3; }
.map-unresolved text { fill: var(--syntax-red); }
.map-node[role="button"]:hover rect, .map-node:focus-visible rect { stroke: var(--syntax-teal); stroke-width: 2; }
.map-current rect { fill: var(--active-line); stroke: var(--syntax-teal); stroke-width: 2.5; }

#stale:not([hidden]) + #preview-section:not(:empty)::before { content: " · "; }

#map-surface { position: relative; min-height: 0; overflow: hidden; }
.map-return { stroke: #77776c; stroke-width: 1; stroke-dasharray: 3 4; }
#map-return-arrow { fill: #8a897d; }
#map-tooltip {
  position: absolute;
  z-index: 1;
  max-width: min(22rem, calc(100% - 16px));
  max-height: calc(100% - 16px);
  overflow: auto;
  padding: .5rem .65rem;
  border: 1px solid var(--control-border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  font-size: .8rem;
  overflow-wrap: anywhere;
  pointer-events: none;
}
#map-tooltip ul { padding-left: 1rem; margin: .35rem 0 0; color: var(--secondary-text); }
#map-tooltip li + li { margin-top: .2rem; }
