:root {
  --bg: #f3eee3;
  --paper: #fbf8f1;
  --ink: #23211c;
  --muted: #6f695d;
  --faint: #9c9585;
  --line: #e2d9c6;
  --line-strong: #cfc3aa;
  --model: #1f5c57;
  --model-soft: rgba(31, 92, 87, 0.08);
  --model-strong: #174541;
  --warn: #9a4b1f;
  --shadow: 0 1px 0 rgba(60, 45, 20, 0.04), 0 12px 32px -18px rgba(60, 45, 20, 0.35);
  --serif: "Literata", Georgia, "Times New Roman", serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12110e;
    --paper: #1a1915;
    --ink: #ece6d8;
    --muted: #a39c8c;
    --faint: #756f62;
    --line: #2b2923;
    --line-strong: #3a372f;
    --model: #83c9bd;
    --model-soft: rgba(131, 201, 189, 0.09);
    --model-strong: #a8dcd2;
    --warn: #e0a36d;
    --shadow: 0 1px 0 rgba(0, 0, 0, 0.3), 0 16px 40px -20px rgba(0, 0, 0, 0.8);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 13px;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--model);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

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

button {
  cursor: pointer;
}

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

/* ---------- top bar ---------- */

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
}

.wordmark {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.wordmark:hover {
  text-decoration: none;
}
.mark {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: var(--model);
  color: var(--paper);
  font-family: Georgia, serif;
  font-size: 17px;
  line-height: 1;
  padding-bottom: 3px;
}

.tagline {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.links {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 18px;
  font-size: 12px;
}
.links a {
  color: var(--muted);
}
.links a:hover {
  color: var(--ink);
}

.icon-btn {
  background: none;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  color: var(--muted);
}
.panel-toggle {
  display: none;
}

/* ---------- layout ---------- */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  min-height: 0;
}

.desk {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 28px 28px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: calc(100vh - 58px);
}

/* ---------- loader ---------- */

.loader {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  background: var(--paper);
  transition: opacity 0.4s ease, transform 0.4s ease;
}
.loader.done {
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
}
.loader.gone {
  display: none;
}
.loader.error {
  border-color: var(--warn);
}
.loader-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.loader-title {
  font-weight: 600;
}
.loader.error .loader-title {
  color: var(--warn);
}
.loader-meta {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.bar {
  height: 4px;
  background: var(--line);
  border-radius: 99px;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  width: 0;
  background: var(--model);
  border-radius: 99px;
  transition: width 0.25s ease;
}
.loader-note {
  margin-top: 8px;
  color: var(--faint);
  font-size: 11.5px;
  line-height: 1.5;
}

/* ---------- manuscript ---------- */

.page-toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
}
.story-title {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 4px 0;
  outline: none;
}
.story-title::placeholder {
  color: var(--faint);
}
.tools {
  display: flex;
  gap: 4px;
}
.tool {
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 4px 9px;
  color: var(--muted);
  font-size: 12px;
}
.tool:hover:not(:disabled) {
  border-color: var(--line-strong);
  color: var(--ink);
}
.tool:disabled {
  opacity: 0.35;
  cursor: default;
}

.manuscript {
  flex: 1;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 44px 56px 52px;
  min-height: 320px;
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.8;
  overflow-wrap: anywhere;
}

.story {
  white-space: pre-wrap;
}
.story:empty {
  display: none;
}

.seg.you {
  color: var(--ink);
}
.seg.model {
  color: var(--model);
  border-radius: 3px;
  transition: background 0.15s ease;
}
.seg.model:hover {
  background: var(--model-soft);
}
.seg.model.live {
  background: none;
}

.caret {
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-left: 1px;
  vertical-align: -0.16em;
  background: var(--model);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink {
  to {
    visibility: hidden;
  }
}

.the-end {
  display: block;
  margin-top: 26px;
  text-align: center;
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--faint);
}
.the-end::before,
.the-end::after {
  content: "";
  display: inline-block;
  width: 36px;
  height: 1px;
  margin: 0 12px;
  vertical-align: middle;
  background: var(--line-strong);
}

.starter-lede {
  margin: 0 0 22px;
  color: var(--muted);
  font-style: italic;
}
.ink-model {
  color: var(--model);
  font-style: normal;
}
.starter-chips {
  display: grid;
  gap: 8px;
}
.chip {
  text-align: left;
  background: none;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 10px 14px;
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.5;
  color: var(--ink);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.chip:hover {
  border-style: solid;
  border-color: var(--model);
  background: var(--model-soft);
}

/* ---------- composer ---------- */

.composer {
  position: sticky;
  bottom: 12px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px;
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.composer:focus-within {
  border-color: var(--model);
}
.composer textarea {
  flex: 1;
  resize: none;
  border: 0;
  outline: none;
  background: none;
  padding: 8px 8px 6px;
  max-height: 180px;
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.5;
}
.composer textarea::placeholder {
  color: var(--faint);
}
.go {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 0;
  border-radius: 10px;
  padding: 9px 14px;
  background: var(--model);
  color: var(--paper);
  font-weight: 600;
  white-space: nowrap;
}
.go:hover:not(:disabled) {
  background: var(--model-strong);
}
.go:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.go kbd {
  font-family: var(--mono);
  font-size: 11px;
  opacity: 0.75;
}
.go.stop {
  background: var(--ink);
}
.composer-hint {
  margin: 0;
  text-align: center;
  color: var(--faint);
  font-size: 11.5px;
}

/* ---------- side panel ---------- */

.panel {
  border-left: 1px solid var(--line);
  padding: 20px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  align-self: start;
}

.card h2 {
  margin: 0 0 10px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--faint);
}

.fine {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.55;
}

.new-story {
  width: 100%;
  text-align: left;
  background: none;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  margin-bottom: 6px;
}
.new-story:hover {
  color: var(--model);
  border-color: var(--model);
}

.library {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 200px;
  overflow-y: auto;
}
.library li {
  display: flex;
  align-items: center;
  border-radius: 6px;
}
.library li.active {
  background: var(--model-soft);
}
.library .open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: 0;
  padding: 7px 8px;
  font-family: var(--serif);
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.library li.active .open {
  color: var(--model);
}
.library .meta {
  color: var(--faint);
  font-size: 10.5px;
  padding-right: 4px;
  white-space: nowrap;
}
.library .del {
  background: none;
  border: 0;
  color: var(--faint);
  padding: 4px 8px;
  opacity: 0;
}
.library li:hover .del,
.library .del:focus-visible {
  opacity: 1;
}
.library .del:hover {
  color: var(--warn);
}

/* context meter */
.ctx-meter {
  display: flex;
  height: 10px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
}
.ctx-prompt {
  background: var(--ink);
  opacity: 0.75;
  transition: width 0.25s ease;
}
.ctx-new {
  background: repeating-linear-gradient(-45deg, var(--model) 0 3px, transparent 3px 6px);
  transition: width 0.25s ease;
}
.ctx-legend {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.ctx-legend b {
  color: var(--ink);
  font-weight: 500;
}
.ctx-total {
  margin-left: auto;
}
.sw {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  margin-right: 5px;
  vertical-align: -1px;
}
.sw-prompt {
  background: var(--ink);
  opacity: 0.75;
}
.sw-new {
  background: repeating-linear-gradient(-45deg, var(--model) 0 2px, transparent 2px 4px);
  border: 1px solid var(--model);
}
#ctxNote.over {
  color: var(--warn);
}

/* knobs */
.knob {
  display: block;
  margin-bottom: 12px;
}
.knob span {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 11.5px;
  margin-bottom: 4px;
}
.knob output {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  margin-left: auto;
  padding-right: 4px;
}
.knob input[type="range"] {
  width: 100%;
  accent-color: var(--model);
}
.reset {
  background: none;
  border: 0;
  padding: 0;
  color: var(--faint);
  font-size: 11px;
  text-decoration: underline;
  text-underline-offset: 3px;
}
.reset:hover {
  color: var(--ink);
}

/* readouts */
.readout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 5px 12px;
  margin: 0;
  font-size: 12px;
}
.readout dt {
  color: var(--faint);
}
.readout dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.readout dd.ok {
  color: var(--model);
}
.readout dd.bad {
  color: var(--warn);
}

/* ---------- toast & scrim ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 20px);
  background: var(--ink);
  color: var(--paper);
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 60;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(20, 16, 10, 0.35);
  z-index: 40;
}

/* ---------- responsive ---------- */

@media (max-width: 980px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
  .panel-toggle {
    display: inline-block;
  }
  .panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(340px, 88vw);
    max-height: none;
    background: var(--bg);
    border-left: 1px solid var(--line-strong);
    z-index: 50;
    transform: translateX(100%);
    transition: transform 0.25s ease;
  }
  .panel.open {
    transform: none;
  }
  .tagline {
    display: none;
  }
}

@media (max-width: 640px) {
  .topbar {
    padding: 12px 16px;
    gap: 12px;
  }
  .links {
    gap: 12px;
  }
  .desk {
    padding: 16px 16px 12px;
  }
  .manuscript {
    padding: 26px 22px 30px;
    font-size: 17.5px;
    line-height: 1.75;
  }
  .story-title {
    font-size: 20px;
  }
  .go kbd {
    display: none;
  }
}

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