/* Veta — app stylesheet. Design tokens per conventions/visual-standards (GitHub-dark workbench),
   light overrides on [data-theme="light"], pre-paint script sets the attribute before first paint. */

/* ---------- tokens ---------- */
:root,
[data-theme="dark"] {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #1c2330;
  --border: #30363d;
  --text: #c9d1d9;
  --muted: #8b949e;
  --accent: #58a6ff;
  --good: #3fb950;
  --warn: #d29922;
  --bad: #f85149;
  --accent-soft: color-mix(in srgb, var(--accent) 14%, transparent);
  --good-soft: color-mix(in srgb, var(--good) 14%, transparent);
  --warn-soft: color-mix(in srgb, var(--warn) 16%, transparent);
  --bad-soft: color-mix(in srgb, var(--bad) 14%, transparent);
  color-scheme: dark;
}
[data-theme="light"] {
  --bg: #f6f8fa;
  --surface: #ffffff;
  --surface2: #eef1f5;
  --border: #d0d7de;
  --text: #1f2328;
  --muted: #656d76;
  --accent: #0969da;
  --good: #1a7f37;
  --warn: #9a6700;
  --bad: #cf222e;
  color-scheme: light;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  height: 100vh;
  height: 100dvh;                     /* dvh with vh fallback */
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, "Segoe UI", system-ui, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ---------- shell grid: "chat circuit" / "chat work" ---------- */
main#shell {
  height: 100%;
  display: grid;
  min-height: 0;
  gap: 10px;
  padding: 10px;
  grid-template-columns: clamp(300px, 22%, 420px) minmax(0, 1fr);
  grid-template-rows: minmax(0, 11fr) minmax(0, 9fr);
  grid-template-areas:
    "chat circuit"
    "chat work";
}
#pane-chat    { grid-area: chat; }
#pane-circuit { grid-area: circuit; }
#pane-work    { grid-area: work; }

.pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
.panehead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  min-height: 38px;
}
.panebody { flex: 1 1 auto; min-height: 0; overflow: auto; }

/* ---------- small kit ---------- */
.iconbtn {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 9px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.2;
}
.iconbtn:hover { border-color: var(--accent); }
.iconbtn.rec { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.iconbtn.on { border-color: var(--good); color: var(--good); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 11px;
  color: var(--muted);
  background: var(--surface2);
  white-space: nowrap;
}
.chip.good { color: var(--good); border-color: var(--good); background: var(--good-soft); }
.chip.warn { color: var(--warn); border-color: var(--warn); background: var(--warn-soft); }
.chip.bad  { color: var(--bad);  border-color: var(--bad);  background: var(--bad-soft); }
.chip.acc  { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot.ok  { background: var(--good); }
.dot.bad { background: var(--bad); }

.empty {
  color: var(--muted);
  font-size: 13px;
  padding: 18px 14px;
  text-align: center;
}
.empty-title { font-weight: 600; color: var(--text); margin-bottom: 4px; }

.sec-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--muted);
  padding: 6px 2px;
}

/* ---------- chat pane ---------- */
.chat-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: 0.5px; }
.brand-sub { font-size: 10px; color: var(--muted); margin-top: -2px; }
.chat-head .grow { flex: 1 1 auto; }
.ver { font-size: 10px; color: var(--muted); }

#chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.msg .bubble {
  max-width: 92%;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 11px;
  background: var(--surface2);
  font-size: 13px;
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }
.msg.assistant .bubble { width: 92%; }

.md p { margin: 0 0 6px; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 4px 0; padding-left: 18px; }
.md code { background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; font-size: 12px; }
.md pre { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px; overflow-x: auto; }
.md table { border-collapse: collapse; margin: 6px 0; font-size: 12px; }
.md th, .md td { border: 1px solid var(--border); padding: 3px 7px; text-align: left; }
.md th { background: var(--surface2); }

.meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.steps-chip { font-size: 11px; }
.warnchip { color: var(--warn); border-color: var(--warn); background: var(--warn-soft); }
.errchip  { color: var(--bad);  border-color: var(--bad);  background: var(--bad-soft); }

/* inline scenario card (chat) */
.scn-card {
  display: block;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  background: var(--surface);
  padding: 8px 10px;
  margin-top: 8px;
  color: var(--text);
}
.scn-card:hover { text-decoration: none; border-color: var(--accent); }
.scn-card .scn-title { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 13px; }
.scn-card .scn-kpis { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 6px; }
.scn-card .scn-kpi { font-size: 11px; color: var(--muted); }
.scn-card .scn-kpi b { color: var(--text); font-size: 13px; margin-left: 4px; }
.scn-card .scn-kpi .delta.up { color: var(--good); }
.scn-card .scn-kpi .delta.down { color: var(--bad); }

/* verdict strip */
.verdict {
  margin-top: 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 6px 9px;
  font-size: 12px;
}
.verdict.v-pass    { border-color: var(--good); background: var(--good-soft); }
.verdict.v-caution { border-color: var(--warn); background: var(--warn-soft); }
.verdict.v-block   { border-color: var(--bad);  background: var(--bad-soft); }
.verdict .v-head { font-weight: 600; }
.verdict ul { margin: 4px 0 0; padding-left: 16px; }

/* stage timeline */
.tl {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: 6px 12px;
  max-height: 168px;
  overflow-y: auto;
  font-size: 12px;
  background: var(--surface);
}
.tl-row { display: flex; align-items: baseline; gap: 8px; padding: 2px 0; }
.tl-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted); flex: 0 0 auto; align-self: center;
}
.tl-row.st-started  .tl-dot { background: var(--accent); animation: tlpulse 1.1s ease-in-out infinite; }
.tl-row.st-done     .tl-dot { background: var(--good); }
.tl-row.st-fallback .tl-dot { background: var(--warn); }
.tl-row.st-skipped  .tl-dot { background: var(--border); }
.tl-row.st-error    .tl-dot { background: var(--bad); }
.tl-row.st-skipped .tl-name { color: var(--muted); text-decoration: line-through; }
.tl-name { font-weight: 600; white-space: nowrap; }
.tl-note { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-row.tool .tl-name { color: var(--accent); font-weight: 500; }
@keyframes tlpulse { 0%, 100% { opacity: 0.35; } 50% { opacity: 1; } }

/* input row */
.chat-input-row {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}
#chat-input {
  flex: 1 1 auto;
  resize: none;
  min-height: 36px;
  max-height: 120px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
}
#chat-input:focus { outline: none; border-color: var(--accent); }
#voice-meter {
  flex: 0 0 auto;
  height: 26px;
  display: block;
  margin: 0 12px 6px;
  width: calc(100% - 24px);
  border-radius: 6px;
  background: var(--surface2);
}

/* ---------- circuit pane ---------- */
.strip { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; flex: 1 1 auto; }
.strip-item { display: inline-flex; align-items: baseline; gap: 5px; white-space: nowrap; }
.strip-value { color: var(--text); font-weight: 600; font-size: 12px; text-transform: none; }
.strip-plan {
  color: var(--text); font-size: 11px; text-transform: none; letter-spacing: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 34ch;
}
.strip-upd { white-space: nowrap; margin-left: auto; }

#circuit-host { width: 100%; height: 100%; overflow: hidden; }
#pane-circuit .panebody { overflow: hidden; }
#circuit-host svg { width: 100%; height: 100%; display: block; }

.node rect, .node path.shape { fill: var(--surface2); stroke: var(--border); stroke-width: 1.5; }
.node text { fill: var(--text); font-size: 13px; }
.node .nlabel { font-size: 12px; font-weight: 600; }
.node .nicon { fill: var(--muted); font-size: 13px; }
.node .nv { fill: var(--muted); font-size: 11px; }
.node .ndot { fill: var(--muted); }
.node.st-run .ndot { fill: var(--good); }
.node.st-warn .ndot { fill: var(--warn); }
.node.st-bad .ndot { fill: var(--bad); }
.node.st-run rect, .node.st-run path.shape { stroke: color-mix(in srgb, var(--good) 55%, var(--border)); }
.node.selected rect, .node.selected path.shape { stroke: var(--accent); stroke-width: 2.5; }
.node { cursor: default; }
.node.clickable { cursor: pointer; }
.node.clickable:hover rect, .node.clickable:hover path.shape { stroke: var(--accent); }
.anchor { fill: var(--accent); }
.anchor-tick { stroke: var(--accent); stroke-width: 1.5; }

.edge { fill: none; stroke: var(--border); stroke-width: 2; }
.edge.flow {
  stroke: var(--accent);
  stroke-dasharray: 7 9;
  animation: dashflow var(--flow-dur, 1.4s) linear infinite;
}
.edge.paused { animation-play-state: paused; opacity: 0.45; stroke: var(--muted); }
@keyframes dashflow { to { stroke-dashoffset: -16; } }
.edge-arrow { fill: var(--muted); }
.stage-label { fill: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: 0.4px; }

/* ---------- work pane ---------- */
.work-grid {
  display: grid;
  gap: 10px;
  padding: 10px;
  grid-template-columns: minmax(0, 7fr) minmax(0, 5fr);
  grid-template-rows: minmax(0, 7fr) minmax(0, 3fr);
  grid-template-areas:
    "scenario profiler"
    "scenario recent";
  height: 100%;
}
.work-scenario { grid-area: scenario; overflow-y: auto; min-height: 0; }
.work-profiler { grid-area: profiler; display: flex; flex-direction: column; min-height: 0; }
.work-recent   { grid-area: recent; display: flex; flex-direction: column; min-height: 0; }

.scn-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.scn-head h2 { font-size: 15px; margin: 0; }
.scn-section { margin-bottom: 14px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 8px; }
.kpi {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  min-width: 0;
}
.kpi .kpi-label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); }
.kpi .kpi-value { font-size: 18px; font-weight: 700; margin-top: 2px; }
.kpi .kpi-unit { font-size: 11px; color: var(--muted); font-weight: 400; margin-left: 2px; }
.kpi .kpi-delta { font-size: 11px; margin-top: 2px; }
.kpi .kpi-delta.up { color: var(--good); }
.kpi .kpi-delta.down { color: var(--bad); }

.chartbox { background: var(--surface2); border: 1px solid var(--border); border-radius: 10px; padding: 8px; margin-bottom: 8px; }
.chartbox svg { width: 100%; height: auto; display: block; }
.chart-title { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; margin-bottom: 4px; }

table.plan { width: 100%; border-collapse: collapse; font-size: 12px; }
table.plan th, table.plan td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
table.plan th { background: var(--surface2); color: var(--muted); font-weight: 600; font-size: 11px; }
.share-bar { display: inline-block; height: 8px; background: var(--accent); border-radius: 4px; vertical-align: middle; margin-right: 6px; }

.guards { display: flex; flex-direction: column; gap: 6px; }
.guard {
  display: flex; align-items: baseline; gap: 8px;
  border: 1px solid var(--border); border-left-width: 3px;
  border-radius: 8px; padding: 5px 9px; font-size: 12px;
}
.guard.g-info  { border-left-color: var(--accent); }
.guard.g-warn  { border-left-color: var(--warn); background: var(--warn-soft); }
.guard.g-block { border-left-color: var(--bad); background: var(--bad-soft); }
.guard .g-code { font-weight: 600; white-space: nowrap; }
.guard .g-eta { color: var(--muted); white-space: nowrap; margin-left: auto; }

/* recent scenarios rail */
.rail { flex: 1 1 auto; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.rail-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface2);
  padding: 6px 9px;
  cursor: pointer;
  font-size: 12px;
}
.rail-card:hover { border-color: var(--accent); }
.rail-card .rc-title {
  font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.rail-card .rc-meta { display: flex; gap: 6px; align-items: center; margin-top: 3px; color: var(--muted); font-size: 11px; }

/* ---------- profiler ---------- */
.profiler-bar { display: flex; align-items: center; gap: 8px; padding: 4px 2px; flex: 0 0 auto; }
.profiler-bar input[type="range"] { flex: 1 1 auto; accent-color: var(--accent); min-width: 0; }
.prof-ts { font-size: 11px; color: var(--muted); white-space: nowrap; }
.prof-host { flex: 1 1 auto; min-height: 0; overflow: hidden; }
.prof-host svg { width: 100%; height: 100%; display: block; }
.pcol rect { stroke: var(--bg); stroke-width: 0.5; }
.pcol.sel rect { stroke: var(--accent); stroke-width: 1.2; }
.paxis text, .pcol-label { fill: var(--muted); font-size: 10px; }
.pcol-label.sel { fill: var(--accent); font-weight: 700; }

/* ---------- charts (svg) ---------- */
.cgrid { stroke: var(--border); stroke-width: 0.5; }
.caxis { fill: var(--muted); font-size: 10px; }
.cline { fill: none; stroke-width: 2; }
.cband { stroke: none; }
.ccursor { stroke: var(--text); stroke-width: 1; stroke-dasharray: 3 3; }
.cbar-label { fill: var(--text); font-size: 11px; }
.cbar-value { fill: var(--muted); font-size: 11px; }

/* ---------- feedback (scenario page) ---------- */
.feedback { border: 1px solid var(--border); border-radius: 10px; padding: 12px; background: var(--surface2); }
.stars { display: flex; gap: 4px; }
.star {
  background: none; border: none; cursor: pointer;
  font-size: 22px; color: var(--border); padding: 0 2px; line-height: 1;
}
.star.on { color: var(--warn); }
.aspects { display: flex; gap: 14px; flex-wrap: wrap; margin: 10px 0; font-size: 13px; }
.aspects label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.feedback textarea {
  width: 100%; min-height: 60px; resize: vertical;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); padding: 8px; font: inherit; font-size: 13px;
}
.feedback textarea:focus { outline: none; border-color: var(--accent); }
.btn {
  background: var(--accent-soft); border: 1px solid var(--accent); color: var(--accent);
  border-radius: 8px; padding: 6px 14px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.btn:hover { background: color-mix(in srgb, var(--accent) 24%, transparent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.fb-item { border-top: 1px solid var(--border); padding: 8px 0; font-size: 13px; }
.fb-item .fb-meta { color: var(--muted); font-size: 11px; }

/* ---------- scenario detail page ---------- */
body.page-scenario { overflow: auto; height: auto; min-height: 100dvh; }
.scn-page-head {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 10px 18px;
}
.scn-page-main { max-width: 1100px; margin: 0 auto; padding: 16px; }
.scn-page-main section { margin-bottom: 20px; }
.scn-page-main h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.4px;
  color: var(--muted); margin: 0 0 8px;
}
.evidence-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 10px; }
.ev-list { display: flex; flex-direction: column; gap: 6px; }
.ev-item {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; font-size: 12px; background: var(--surface2);
  display: flex; gap: 8px; align-items: baseline;
}
.ev-item .ev-title { font-weight: 600; }
.ev-item .ev-date { color: var(--muted); margin-left: auto; white-space: nowrap; }
#scn-live { margin-bottom: 14px; }

/* ---------- login ---------- */
body.page-login {
  display: grid; place-items: center; overflow: auto;
}
.login-card {
  width: min(360px, 92vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 26px;
}
.login-card .brand { font-size: 22px; }
.login-card form { display: flex; flex-direction: column; gap: 10px; margin-top: 16px; }
.login-card input {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); padding: 10px 12px; font: inherit;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-err { color: var(--bad); font-size: 13px; min-height: 18px; }
.login-tools { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }

/* ---------- mobile tab bar (<1000px) ---------- */
#tabbar { display: none; }
@media (max-width: 1000px) {
  body { overflow: hidden; }
  main#shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "view";
    padding-bottom: 56px;
  }
  .pane { display: none; grid-area: view; }
  body[data-tab="chat"] #pane-chat { display: flex; }
  body[data-tab="circuit"] #pane-circuit { display: flex; }
  body[data-tab="work"] #pane-work { display: flex; }
  .work-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    grid-template-areas: "profiler" "scenario" "recent";
    height: auto;
    overflow-y: auto;
  }
  #pane-work .panebody { overflow-y: auto; }
  .work-profiler { min-height: 260px; }
  #tabbar {
    display: flex;
    position: fixed; bottom: 0; left: 0; right: 0;
    background: var(--surface); border-top: 1px solid var(--border);
    z-index: 10;
  }
  #tabbar .tab {
    flex: 1 1 0; background: none; border: none; padding: 12px 0;
    color: var(--muted); cursor: pointer; font-size: 13px;
  }
  #tabbar .tab.active { color: var(--accent); font-weight: 600; box-shadow: inset 0 2px 0 var(--accent); }
}

/* ---------- reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .edge.flow { animation: none; }
  .tl-row.st-started .tl-dot { animation: none; opacity: 1; }
  * { scroll-behavior: auto !important; }
}
