:root {
  --bg: #262624;
  --bg2: #30302e;
  --side: #1f1e1c;
  --card: #30302e;
  --card2: #3a3a37;
  --text: #ececea;
  --muted: #9d9b93;
  --accent: #d97757;
  --accent-soft: #d9775733;
  --danger: #c0554f;
  --ok: #6faa6f;
  --warn: #c9b458;
  --border: #3f3e3a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14.5px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); text-decoration: none; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.error { color: var(--danger); }
h1, h2, h3 { font-weight: 600; }

.shell { display: flex; height: 100vh; }

/* ---------------- sidebar ---------------- */
.sidebar {
  width: 264px; flex: 0 0 auto; background: var(--side);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 0.9rem 0.8rem;
}
.brand { color: var(--text); font-weight: 700; font-size: 1.02rem; display: block; }
.spark { color: var(--accent); }
.new-btn {
  display: block; margin: 0.9rem 0 0.4rem; padding: 0.45rem 0.8rem;
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid var(--accent); border-radius: 8px; font-weight: 600;
}
.side-recents { flex: 1; overflow-y: auto; margin-top: 0.8rem; }
.side-recents h4 {
  margin: 0 0 0.3rem 0.3rem; font-size: 0.78rem; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--muted); font-weight: 600;
}
.side-recents ul { list-style: none; margin: 0; padding: 0; }
.side-recents li a {
  display: flex; gap: 0.5rem; align-items: center;
  padding: 0.42rem 0.55rem; border-radius: 8px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.9rem;
}
.side-recents li a:hover, .side-recents li a.active { background: var(--bg2); }
.side-recents .title { overflow: hidden; text-overflow: ellipsis; flex: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; background: var(--muted); }
.dot.st-running, .dot.st-queued { background: var(--accent); animation: pulse 1.4s infinite; }
.dot.st-complete { background: var(--ok); }
.dot.st-failed, .dot.st-cancelled { background: var(--danger); }
.dot.st-awaiting_review, .dot.st-interrupted { background: var(--warn); }
@keyframes pulse { 50% { opacity: 0.35; } }
.side-bottom {
  display: flex; justify-content: space-between; padding: 0.6rem 0.3rem 0;
  border-top: 1px solid var(--border); font-size: 0.85rem;
}

/* ---------------- main ---------------- */
main { flex: 1; overflow-y: auto; padding: 1.4rem 2rem 4rem; }

/* composer */
.composer-wrap { max-width: 760px; margin: 8vh auto 0; }
.composer-title { text-align: center; font-size: 1.7rem; font-weight: 500; margin-bottom: 1.4rem; }
.composer {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  padding: 1rem 1rem 0.7rem; box-shadow: 0 8px 30px #0004;
}
.topic-input {
  width: 100%; background: transparent; border: 0; outline: 0;
  color: var(--text); font: 600 1.15rem/1.4 system-ui, sans-serif; padding: 0.3rem 0.2rem;
}
.topic-input::placeholder, .context-input::placeholder { color: var(--muted); }
.context-input {
  width: 100%; background: transparent; border: 0; outline: 0; resize: vertical;
  color: var(--text); font: 0.88rem/1.5 ui-monospace, monospace; padding: 0.3rem 0.2rem;
}
.composer-bar { display: flex; align-items: center; justify-content: space-between; gap: 0.8rem; margin-top: 0.4rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.45rem; align-items: center; }
.chip {
  display: inline-flex; gap: 0.4rem; align-items: center;
  background: var(--card2); border: 1px solid var(--border); border-radius: 999px;
  padding: 0.22rem 0.7rem; font-size: 0.82rem; color: var(--text); cursor: default;
}
.chip input[type="number"] { width: 64px; }
.chip input, .chip select {
  background: transparent; border: 0; outline: 0; color: var(--text); font: inherit;
}
.chip option { background: var(--card); }
.send-btn {
  background: var(--accent); color: #14100b; border: 0; border-radius: 10px;
  padding: 0.5rem 1.1rem; font: 600 0.95rem system-ui, sans-serif; cursor: pointer;
}
.send-btn:hover { filter: brightness(1.1); }

.advanced { margin-top: 0.8rem; border-top: 1px solid var(--border); }
.advanced summary {
  cursor: pointer; color: var(--muted); padding: 0.6rem 0.2rem; font-size: 0.88rem;
}
.advanced section {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 10px;
  padding: 0.7rem 0.9rem; margin-bottom: 0.7rem;
}
.advanced h3 { margin: 0 0 0.4rem; font-size: 0.8rem; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--muted); }
.advanced label { display: block; margin: 0.4rem 0; font-size: 0.88rem; }
.advanced input:not([type="checkbox"]), .advanced select, .advanced textarea {
  width: 100%; margin-top: 0.15rem; padding: 0.35rem 0.5rem;
  background: var(--card2); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font: inherit;
}
.advanced textarea { font-family: ui-monospace, monospace; font-size: 0.82rem; }
.row { display: flex; gap: 1rem; flex-wrap: wrap; align-items: end; }
.row label { flex: 0 1 auto; min-width: 120px; }
.row label.grow { flex: 1 1 240px; }
.row input[type="number"] { width: 100px; }
.indent { margin-left: 1.4rem; }
.switch { display: flex !important; gap: 0.5rem; align-items: center; }
.switch input { width: auto !important; accent-color: var(--accent); }
.pass-block { border-top: 1px dashed var(--border); padding-top: 0.45rem; margin-top: 0.5rem; }

/* run view */
.run-top { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; max-width: 980px; margin: 0 auto; }
.run-top h2 { margin: 0; margin-right: auto; font-size: 1.15rem; font-weight: 600; }
.badge {
  display: inline-block; padding: 0.12rem 0.6rem; margin-right: 0.3rem;
  border-radius: 999px; border: 1px solid var(--border); font-size: 0.78rem;
  background: var(--card);
}
.st-running, .st-queued { border-color: var(--accent); color: var(--accent); }
.st-complete { border-color: var(--ok); color: var(--ok); }
.st-failed, .st-cancelled { border-color: var(--danger); color: var(--danger); }
.st-awaiting_review, .st-interrupted { border-color: var(--warn); color: var(--warn); }

.btn {
  display: inline-block; padding: 0.3rem 0.8rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  cursor: pointer; font-size: 0.85rem;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #14100b; font-weight: 600; }
.btn.danger { border-color: var(--danger); color: var(--danger); background: transparent; }

.tabs { display: flex; gap: 0.2rem; max-width: 980px; margin: 1rem auto 0;
  border-bottom: 1px solid var(--border); }
.tab {
  background: transparent; border: 0; color: var(--muted); padding: 0.5rem 0.9rem;
  cursor: pointer; font: inherit; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-pane { max-width: 980px; margin: 1rem auto 0; }

/* the reading pane, claude-artifact style. Dark sheet: the document is read for
   an hour at a stretch, so it sits a shade below the app background rather than
   glowing above it. */
.doc {
  background: #1c1b18; color: #ddd9cf; border: 1px solid #34322c; border-radius: 12px;
  padding: 3rem clamp(1.4rem, 6vw, 4.5rem); font: 1.02rem/1.65 Georgia, "Iowan Old Style", serif;
  box-shadow: 0 10px 40px #0008;
}
.doc h1, .doc h2, .doc h3, .doc h4 { color: #f1eee4; }
.doc h1 { font-size: 1.75rem; line-height: 1.25; }
.doc h2 { font-size: 1.35rem; margin-top: 2rem; }
.doc h3 { font-size: 1.12rem; margin-top: 1.5rem; }
.doc p { margin: 0.9rem 0; }
.doc code {
  background: #2b2a24; color: #e0dac9; border-radius: 4px; padding: 0.08rem 0.3rem;
  font-size: 0.85em; font-family: ui-monospace, monospace;
}
.doc pre {
  background: #121210; color: #ddd9cf; border: 1px solid #34322c; border-radius: 8px;
  padding: 1rem; overflow-x: auto; font-size: 0.8rem;
}
.doc pre code { background: transparent; padding: 0; color: inherit; }
.doc blockquote { border-left: 3px solid #4a483f; margin-left: 0; padding-left: 1rem; color: #a8a396; }
.doc ul, .doc ol { margin: 0.7rem 0 0.9rem; padding-left: 1.5rem; }
.doc li { margin: 0.3rem 0; }
.doc strong { font-weight: 700; color: #fbf8f0; }
.doc hr { border: 0; border-top: 1px solid #34322c; margin: 2rem 0; }
.doc h2 { border-bottom: 1px solid #34322c; padding-bottom: 0.3rem; }
.doc .table-wrap { overflow-x: auto; margin: 1rem 0; }
.doc table { border-collapse: collapse; font-size: 0.92em; min-width: 50%; }
.doc th, .doc td { border: 1px solid #3b3931; padding: 0.35rem 0.6rem; text-align: left; vertical-align: top; }
.doc th { background: #2a2924; color: #f1eee4; }
.doc a { color: #e2895f; text-decoration: underline; text-underline-offset: 2px; }
.doc a:hover { color: #f0a077; }
.doc-empty { color: var(--muted); font-style: italic; }

.doc-controls {
  display: flex; gap: 0.7rem; align-items: center; flex-wrap: wrap;
  margin-bottom: 0.6rem; font-size: 0.86rem;
}
.doc-controls .spacer { flex: 1; }
.doc-controls input[type="number"] { width: 62px; }
.doc-controls input, .doc-controls select {
  background: var(--card2); border: 1px solid var(--border); border-radius: 6px;
  color: var(--text); font: inherit; padding: 0.2rem 0.4rem;
}

.log, .files { list-style: none; padding: 0; margin: 0; font-size: 0.87rem; }
.log li, .files li { padding: 0.3rem 0.1rem; border-bottom: 1px solid var(--border); }
.files code { color: var(--text); }

.pipeline {
  max-width: 980px; margin: 0.7rem auto 0; display: flex; flex-wrap: wrap;
  gap: 0.35rem; align-items: center; font-size: 0.82rem;
}
.pipeline .stage {
  padding: 0.15rem 0.65rem; border-radius: 999px; border: 1px solid var(--border);
  color: var(--muted); background: var(--card);
}
.pipeline .stage.done { border-color: var(--ok); color: var(--ok); }
.pipeline .stage.current { border-color: var(--accent); color: var(--accent); animation: pulse 1.4s infinite; }
.pipeline .sep { color: var(--border); }

#live-strip {
  max-width: 980px; margin: 0.8rem auto 0; display: flex; gap: 0.8rem;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 0.7rem 0.9rem; align-items: flex-start;
}
.spinner {
  width: 14px; height: 14px; margin-top: 3px; border-radius: 50%; flex: 0 0 auto;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#live-line { font-weight: 600; }
#live-events { list-style: none; margin: 0.25rem 0 0; padding: 0; font-size: 0.8rem; }
#live-events li { padding: 0.08rem 0; }

#gate-panel {
  max-width: 980px; margin: 1rem auto 0; background: var(--card);
  border: 1px solid var(--warn); border-radius: 12px; padding: 0.9rem 1rem;
}
.gate-head { display: flex; gap: 0.8rem; align-items: center; margin-bottom: 0.6rem; flex-wrap: wrap; }
.gate-head .send-btn { margin-left: auto; }
#gate-editor {
  width: 100%; background: var(--bg2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  font: 0.84rem/1.5 ui-monospace, monospace; padding: 0.7rem;
}

#viewer { margin-top: 1rem; background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 0.8rem; }
.viewer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
#viewer-content { overflow: auto; max-height: 60vh; white-space: pre-wrap;
  background: var(--bg2); padding: 0.9rem; border-radius: 8px; font-size: 0.8rem; }

/* login */
.login-body { display: grid; place-items: center; height: 100vh; background: var(--bg); }
.login-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 2rem; width: 320px; display: flex; flex-direction: column; gap: 0.8rem;
}
.login-card h1 { margin: 0 0 0.5rem; color: var(--accent); font-size: 1.3rem; }
.login-card input {
  padding: 0.5rem 0.7rem; background: var(--bg2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; font: inherit;
}
.login-card button {
  padding: 0.55rem; background: var(--accent); border: 0; border-radius: 8px;
  font: 600 1rem system-ui, sans-serif; cursor: pointer; color: #14100b;
}

@media (max-width: 760px) {
  .sidebar { display: none; }
  main { padding: 1rem; }
}

.side-recents .stop-btn {
  flex: 0 0 auto; border: 1px solid var(--border); background: transparent;
  color: var(--muted); border-radius: 6px; padding: 0.05rem 0.35rem;
  font-size: 0.72rem; cursor: pointer; opacity: 0; transition: opacity 0.12s;
}
.side-recents li a:hover .stop-btn,
.side-recents .stop-btn:disabled { opacity: 1; }
.side-recents .stop-btn:hover { color: var(--danger); border-color: var(--danger); }
.recents-error {
  padding: 0.42rem 0.55rem; font-size: 0.82rem; color: var(--danger);
}


/* table of contents above the reading pane */
.toc { margin: 0 0 0.8rem; border: 1px solid var(--border); border-radius: 10px; background: var(--card); font-size: 0.86rem; }
.toc summary { cursor: pointer; padding: 0.5rem 0.9rem; color: var(--muted); font-weight: 600; letter-spacing: 0.04em; }
.toc nav { padding: 0.2rem 0.9rem 0.7rem; max-height: 40vh; overflow-y: auto; }
.toc ul { list-style: none; margin: 0; padding: 0; columns: 2; column-gap: 1.5rem; }
.toc li { margin: 0.15rem 0; break-inside: avoid; }
.toc li.toc-h3 { padding-left: 1rem; color: var(--muted); }
.toc a { color: var(--text); }
.toc li.toc-h3 a { color: var(--muted); }
.toc a:hover { color: var(--accent); }
@media (max-width: 900px) { .toc ul { columns: 1; } }

/* editor tab */
.editor-pane { display: flex; flex-direction: column; gap: 1rem; }
.editor-card { background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 1rem 1.2rem; }
.editor-card h3 { margin: 0 0 0.6rem; font-size: 0.95rem; color: var(--accent); letter-spacing: 0.03em; }
.editor-card pre { background: #121210; color: #ddd9cf; border: 1px solid #34322c; border-radius: 8px; padding: 0.8rem; font-size: 0.78rem; overflow-x: auto; white-space: pre-wrap; }
.doc.doc-compact { padding: 0.6rem 1rem; box-shadow: none; border: 0; background: transparent; font-size: 0.95rem; }
.doc.doc-compact h1 { font-size: 1.3rem; }
.doc.doc-compact h2 { font-size: 1.1rem; margin-top: 1.2rem; border: 0; }
.doc.doc-compact h3 { font-size: 1rem; margin-top: 0.9rem; }
