:root {
  --ink: #151815;
  --paper: #f1f0e9;
  --panel: #faf9f3;
  --muted: #6d706a;
  --line: #cbc9be;
  --teal: #117a6d;
  --teal-soft: #dcebe6;
  --red: #b84635;
  --amber: #c58a22;
  --violet: #6657a5;
  --blue: #326b9b;
  --shadow: 0 18px 50px rgba(27, 35, 31, .08);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; color: var(--ink); background: var(--paper); font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input { font: inherit; }
.mono { font-family: "SFMono-Regular", Consolas, monospace; }
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 310px minmax(0, 1fr); }

.history-rail { position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; padding: 34px 24px 20px; background: #171c19; color: #f4f1e8; overflow: hidden; }
.brand { padding: 0 8px 24px; border-bottom: 1px solid rgba(255,255,255,.13); }
.brand h1 { margin: 8px 0 10px; font-family: Georgia, serif; font-weight: 500; font-size: 40px; line-height: .96; letter-spacing: -.04em; }
.brand p, .trace-head p { margin: 0; color: #9ca49e; font-size: 13px; }
.eyebrow, .kicker { font-size: 10px; letter-spacing: .18em; text-transform: uppercase; color: #88c5b8; }
.search-box { display: grid; gap: 7px; padding: 22px 8px 14px; color: #9ca49e; font-size: 10px; letter-spacing: .15em; text-transform: uppercase; }
.search-box input { width: 100%; padding: 11px 0; border: 0; border-bottom: 1px solid rgba(255,255,255,.2); outline: 0; color: white; background: transparent; }
.search-box input:focus { border-color: #88c5b8; }
.run-list { flex: 1; overflow-y: auto; padding: 0 4px 20px; scrollbar-width: thin; }
.run-item { width: 100%; display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: start; padding: 15px 5px; border: 0; border-bottom: 1px solid rgba(255,255,255,.1); color: inherit; background: transparent; text-align: left; cursor: pointer; }
.run-item:hover, .run-item.active { background: rgba(255,255,255,.06); }
.run-index { font-family: Georgia, serif; color: #738078; font-size: 18px; }
.run-name { display: block; margin-bottom: 5px; font-size: 14px; line-height: 1.35; }
.run-sub { color: #78837c; font-size: 10px; }
.run-dot { width: 8px; height: 8px; margin-top: 5px; border-radius: 50%; background: var(--teal); box-shadow: 0 0 0 4px rgba(17,122,109,.15); }
.run-dot.warn { background: var(--amber); }
.rail-footer { display: flex; justify-content: space-between; padding: 16px 8px 0; border-top: 1px solid rgba(255,255,255,.13); color: #727c75; font-size: 9px; letter-spacing: .14em; }

.workspace { min-width: 0; padding: 38px 42px 80px; }
.topbar { display: flex; justify-content: space-between; gap: 30px; align-items: flex-end; margin-bottom: 28px; }
.topbar h2 { margin: 7px 0 0; font-family: Georgia, "Songti SC", serif; font-weight: 500; font-size: clamp(30px, 4vw, 55px); letter-spacing: -.045em; }
.status-row { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.status-chip, .kind-chip { padding: 7px 10px; border: 1px solid var(--line); border-radius: 999px; font-size: 10px; letter-spacing: .08em; text-transform: uppercase; background: rgba(255,255,255,.45); }
.status-chip.good { border-color: #9bc4b8; color: #0a695c; background: var(--teal-soft); }
.status-chip.warn { border-color: #d8bb7d; color: #825807; background: #f8efd9; }
.panel { background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow); }
.hero-grid { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(320px, .65fr); gap: 20px; }
.video-card, .story-card { padding: 18px; }
.panel-heading { display: flex; justify-content: space-between; align-items: center; min-height: 28px; margin-bottom: 14px; font-size: 11px; letter-spacing: .15em; text-transform: uppercase; }
.panel-heading.compact { margin: 0 0 7px; }
.section-no { margin-right: 12px; color: var(--teal); font-family: Georgia, serif; font-size: 16px; }
.text-action { color: var(--teal); text-decoration: none; font-size: 10px; }
#finalVideo { width: 100%; display: block; aspect-ratio: 16/9; background: #0b0e0c; object-fit: contain; }
.video-empty { display: grid; min-height: 280px; place-items: center; padding: 40px; border: 1px dashed #bc8b7f; color: #7c3d31; background: #f5e8e3; text-align: center; line-height: 1.7; }
.video-meta { display: flex; gap: 18px; flex-wrap: wrap; padding-top: 12px; color: var(--muted); font-size: 11px; }
.story-card { display: flex; flex-direction: column; }
.story-text { flex: 1; white-space: pre-wrap; max-height: 360px; overflow: auto; padding: 8px 8px 20px 0; font-family: Georgia, "Songti SC", serif; font-size: 16px; line-height: 1.85; }
.metric-grid { display: grid; grid-template-columns: repeat(2, 1fr); border-top: 1px solid var(--line); }
.metric { padding: 14px 6px; border-bottom: 1px solid var(--line); }
.metric:nth-child(odd) { border-right: 1px solid var(--line); }
.metric strong { display: block; font-family: Georgia, serif; font-size: 22px; font-weight: 500; }
.metric span { color: var(--muted); font-size: 9px; letter-spacing: .12em; text-transform: uppercase; }

.trace-section { margin-top: 20px; padding: 18px; }
.trace-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 24px; }
.tree-viewport { min-height: 350px; margin-top: 16px; overflow: auto; padding: 28px 18px 48px; background-image: linear-gradient(rgba(20,30,25,.035) 1px, transparent 1px), linear-gradient(90deg, rgba(20,30,25,.035) 1px, transparent 1px); background-size: 28px 28px; }
.tree { width: max-content; min-width: 100%; }
.tree-actions { display: flex; align-items: center; gap: 8px; }
.tree-action { border: 1px solid var(--teal); background: var(--teal); color: #fff; padding: 9px 12px; font: inherit; font-size: 10px; letter-spacing: .04em; cursor: pointer; }
.tree-action.secondary { background: transparent; color: var(--teal); }
.tree-action:hover { filter: brightness(.95); }
.tree ul { position: relative; display: flex; justify-content: center; gap: 18px; margin: 0; padding: 30px 0 0; list-style: none; }
.tree > ul { padding-top: 0; }
.tree li { position: relative; padding: 30px 5px 0; text-align: center; }
.tree > ul > li { padding-top: 0; }
.tree li::before, .tree li::after { content: ""; position: absolute; top: 0; width: 50%; height: 30px; border-top: 1px solid #aaa99f; }
.tree li::before { right: 50%; }
.tree li::after { left: 50%; border-left: 1px solid #aaa99f; }
.tree li:only-child::before, .tree li:only-child::after { display: none; }
.tree li:first-child::before, .tree li:last-child::after { border: 0; }
.tree li:last-child::before { border-right: 1px solid #aaa99f; border-radius: 0 7px 0 0; }
.tree li:first-child::after { border-radius: 7px 0 0 0; }
.tree ul ul::before { content: ""; position: absolute; top: 0; left: 50%; height: 30px; border-left: 1px solid #aaa99f; }
.node-card { position: relative; width: 168px; min-height: 86px; padding: 12px; border: 1px solid #b9bab2; background: #fffefa; text-align: left; cursor: pointer; box-shadow: 0 7px 18px rgba(20,30,25,.06); transition: transform .15s, border .15s, box-shadow .15s; }
.node-wrap { position: relative; display: inline-block; padding-bottom: 13px; }
.branch-toggle { position: absolute; left: 50%; bottom: 0; z-index: 2; display: inline-flex; align-items: center; gap: 4px; transform: translateX(-50%); min-width: 34px; height: 22px; padding: 0 7px; border: 1px solid #aaa99f; border-radius: 11px; background: #fffefa; color: var(--ink); font: 600 12px/1 monospace; cursor: pointer; }
.branch-toggle span { color: var(--muted); font-size: 8px; }
.branch-toggle:hover { border-color: var(--teal); color: var(--teal); }
.node-card:hover, .node-card.selected { transform: translateY(-3px); border-color: var(--teal); box-shadow: 0 9px 22px rgba(17,122,109,.15); }
.node-card .node-agent { color: var(--muted); font-size: 8px; letter-spacing: .12em; text-transform: uppercase; }
.node-state { float: right; margin-right: 10px; color: var(--muted); font-size: 8px; letter-spacing: .08em; }
.node-state.complete { color: var(--teal); }
.node-state.failed { color: var(--red); }
.node-card strong { display: block; margin: 8px 0 6px; font-family: Georgia, "Songti SC", serif; font-size: 13px; font-weight: 600; line-height: 1.3; }
.node-card p { display: -webkit-box; margin: 0; overflow: hidden; color: var(--muted); font-size: 9px; line-height: 1.4; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.node-card::after { content: ""; position: absolute; top: 10px; right: 10px; width: 6px; height: 6px; border-radius: 50%; background: var(--teal); }
.node-card[data-kind="validator"]::after { background: var(--amber); }
.node-card[data-kind="repair"]::after { background: var(--red); }
.node-card[data-kind="planner"]::after { background: var(--violet); }
.node-card[data-kind="segment"]::after { background: var(--blue); }
.node-card[data-kind="segment_attempt"]::after { background: var(--red); box-shadow: 0 0 0 3px #f3ded9; }
.node-card[data-kind="failure"] { border-color: #c9897d; background: #fff5f1; }
.node-card[data-kind="failure"]::after { background: var(--red); box-shadow: 0 0 0 3px #f3ded9; }
.node-card[data-kind="final"]::after { background: var(--teal); box-shadow: 0 0 0 4px var(--teal-soft); }
.node-media { margin: 0 0 18px; border: 1px solid #c8c9c1; background: #111; }
.node-media[hidden] { display: none; }
.node-media-head { display: flex; justify-content: space-between; gap: 12px; padding: 9px 12px; background: #f2f0e9; color: var(--muted); font-size: 9px; letter-spacing: .05em; }
.node-media-head span:first-child { color: var(--ink); font-weight: 700; }
.node-media video { display: block; width: 100%; max-height: 520px; background: #000; }
.hidden-node { display: none !important; }

.inspector { margin-top: 20px; padding: 22px; }
.inspector-title { display: flex; justify-content: space-between; align-items: flex-start; gap: 20px; }
.inspector-title h3 { margin: 5px 0 16px; font-family: Georgia, "Songti SC", serif; font-size: 28px; font-weight: 500; }
.tabs { display: flex; gap: 22px; border-bottom: 1px solid var(--line); }
.tab { padding: 10px 0; border: 0; border-bottom: 2px solid transparent; color: var(--muted); background: transparent; cursor: pointer; font-size: 11px; }
.tab.active { color: var(--ink); border-color: var(--teal); }
.tab-panel { display: none; padding-top: 18px; }
.tab-panel.active { display: block; }
.prompt-block { margin-bottom: 16px; }
.prompt-label { margin-bottom: 7px; color: var(--teal); font-size: 9px; letter-spacing: .15em; text-transform: uppercase; }
pre, .advice-card { margin: 0; padding: 16px; border: 1px solid #d7d5ca; background: #f5f4ee; white-space: pre-wrap; overflow-wrap: anywhere; font: 12px/1.7 "SFMono-Regular", Consolas, monospace; }
.advice-card { margin-bottom: 10px; border-left: 3px solid var(--amber); font-family: inherit; }
.empty-state { padding: 32px; color: var(--muted); border: 1px dashed var(--line); text-align: center; font-size: 12px; }
.evidence-list { margin: 0; padding-left: 20px; font: 12px/1.8 "SFMono-Regular", Consolas, monospace; }

@media (max-width: 1000px) {
  .app-shell { grid-template-columns: 240px minmax(0, 1fr); }
  .workspace { padding: 28px 24px 60px; }
  .hero-grid { grid-template-columns: 1fr; }
  .story-text { max-height: 260px; }
}
@media (max-width: 720px) {
  .app-shell { display: block; }
  .history-rail { position: relative; width: 100%; height: auto; max-height: 440px; }
  .workspace { padding: 22px 14px 50px; }
  .topbar, .trace-head { align-items: flex-start; flex-direction: column; }
  .status-row { justify-content: flex-start; }
}
