:root {
  --void: #07090d;
  --elevated: #0c1018;
  --subtle: #121826;
  --hairline: rgb(255 255 255 / 0.08);
  --text: #f3f5f9;
  --muted: #9aa3b5;
  --faint: #7a8499;
  --accent: #5b8cff;
  --accent-soft: rgb(91 140 255 / 0.14);
  --accent-glow: rgb(91 140 255 / 0.35);
  --warm: #e8b86d;
  --floor: #dde6f0;
  --floor-deep: #c5d0de;
  --working: #3ddc97;
  --thinking: #f0b429;
  --queued: #5b8cff;
  --idle: #7a8499;
  --error: #ff5c6c;
  --font: "Outfit", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--void);
  overflow: hidden;
}

.shell {
  height: 100%;
  display: grid;
  grid-template-rows: 56px 1fr;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem 0 1.1rem;
  border-bottom: 1px solid var(--hairline);
  background: rgba(12, 16, 24, 0.92);
  backdrop-filter: blur(12px);
  z-index: 5;
}
.brand { display: flex; align-items: center; gap: 0.7rem; min-width: 210px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), #7aa4ff);
  color: white; font-weight: 800; font-size: 0.85rem;
  box-shadow: 0 0 24px var(--accent-glow);
}
.brand-title { margin: 0; font-size: 0.95rem; font-weight: 700; letter-spacing: -0.02em; }
.brand-sub { margin: 0.1rem 0 0; font-size: 0.72rem; color: var(--muted); }

.kpis { display: flex; gap: 1.4rem; overflow-x: auto; justify-content: center; }
.kpi { min-width: 64px; }
.kpi label {
  display: block;
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--faint);
}
.kpi strong {
  display: block;
  margin-top: 0.1rem;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.top-actions { display: flex; gap: 0.5rem; align-items: center; }
.sync, .chip {
  display: inline-flex; align-items: center; gap: 0.4rem;
  height: 28px; padding: 0 0.7rem; border-radius: 8px;
  border: 1px solid var(--hairline); background: var(--subtle);
  font-size: 0.72rem; color: var(--muted); white-space: nowrap;
}
.sync { color: #86efac; border-color: rgba(61,220,151,0.25); background: rgba(61,220,151,0.08); }
.sync i {
  width: 6px; height: 6px; border-radius: 50%; background: #3ddc97;
  box-shadow: 0 0 0 0 rgba(61,220,151,0.6); animation: pulse 1.5s infinite;
}
.sync.stale { color: var(--warm); border-color: rgba(232,184,109,0.3); background: rgba(232,184,109,0.08); }
.sync.stale i { background: var(--warm); animation: none; }
.chip strong { color: var(--text); margin-left: 0.2rem; }

.btn-live {
  display: inline-flex; align-items: center; gap: 0.4rem;
  height: 28px; padding: 0 0.75rem; border-radius: 8px;
  border: 1px solid rgba(240,180,41,0.35);
  background: rgba(240,180,41,0.12);
  color: #f0d48a; font: inherit; font-size: 0.72rem; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.btn-live:hover { background: rgba(240,180,41,0.2); border-color: rgba(240,180,41,0.55); }
.btn-live i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--thinking);
  box-shadow: 0 0 0 0 rgba(240,180,41,0.55); animation: pulse 1.5s infinite;
}
.btn-live-count {
  min-width: 1.2rem; height: 1.2rem; padding: 0 0.3rem;
  border-radius: 999px; background: rgba(0,0,0,0.35);
  display: inline-grid; place-items: center;
  font-family: var(--mono); font-size: 0.62rem; color: var(--text);
}

.modal-root {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  padding: 1.25rem;
}
.modal-root[hidden] { display: none !important; }
.modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(3,5,10,0.72);
  backdrop-filter: blur(6px);
}
.modal-panel {
  position: relative; z-index: 1;
  width: min(980px, 100%);
  height: min(82vh, 820px);
  display: grid;
  grid-template-rows: auto auto auto auto auto 1fr;
  gap: 0.55rem;
  padding: 0.9rem 1rem 1rem;
  border-radius: 18px;
  border: 1px solid var(--hairline);
  background: linear-gradient(180deg, #121826 0%, #0c1018 100%);
  box-shadow: 0 30px 80px rgba(0,0,0,0.55);
}
.modal-head {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem;
}
.modal-head h2 { margin: 0; font-size: 1.15rem; }
.modal-sub { margin: 0.2rem 0 0; color: var(--muted); font-size: 0.78rem; }
.modal-head-actions { display: flex; align-items: center; gap: 0.55rem; }
.modal-close { font-size: 1.1rem; padding: 0.2rem 0.4rem; }
.modal-meta {
  display: flex; flex-wrap: wrap; gap: 0.4rem;
}
.modal-chip {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.25rem 0.55rem; border-radius: 999px;
  border: 1px solid var(--hairline); background: rgba(255,255,255,0.03);
  font-family: var(--mono); font-size: 0.65rem; color: var(--muted);
}
.modal-chip strong { color: var(--text); font-weight: 600; }
.modal-tabs {
  display: flex; gap: 0.35rem; overflow-x: auto; padding-bottom: 0.15rem;
}
.modal-tab {
  border: 1px solid var(--hairline);
  background: rgba(255,255,255,0.03);
  color: var(--muted);
  border-radius: 999px;
  padding: 0.35rem 0.7rem;
  font: inherit; font-size: 0.72rem; font-weight: 600;
  cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 0.4rem;
}
.modal-tab .pip {
  width: 7px; height: 7px; border-radius: 50%; background: var(--idle);
}
.modal-tab.working .pip { background: var(--working); }
.modal-tab.thinking .pip { background: var(--thinking); }
.modal-tab.queued .pip { background: var(--queued); }
.modal-tab.active {
  color: var(--text);
  border-color: rgba(91,140,255,0.55);
  background: rgba(91,140,255,0.14);
}
.modal-tab.dim { opacity: 0.45; }
.modal-agent-bar {
  display: flex; justify-content: space-between; gap: 1rem; align-items: center;
  padding: 0.45rem 0.15rem;
}
.modal-agent-bar strong { font-size: 0.95rem; margin-right: 0.5rem; }
.modal-agent-meta { color: var(--faint); font-size: 0.68rem; text-align: right; }
.modal-thought {
  font-size: 0.84rem; line-height: 1.45; color: #f0d48a;
  background: rgba(240,180,41,0.08); border: 1px solid rgba(240,180,41,0.22);
  border-radius: 12px; padding: 0.7rem 0.8rem;
  min-height: 3.2rem; white-space: pre-wrap; word-break: break-word;
}
.modal-thought.idle { color: var(--faint); border-color: var(--hairline); background: rgba(255,255,255,0.03); }
.modal-stream {
  min-height: 0; overflow: auto;
  font-family: var(--mono); font-size: 0.72rem;
  display: flex; flex-direction: column; gap: 0.3rem;
  padding: 0.55rem; border-radius: 12px;
  border: 1px solid var(--hairline); background: rgba(7,9,13,0.45);
}
#modal-stream-status.live { color: var(--working); }
#modal-stream-status.stale { color: #f87171; }

.stage-row {
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 360px;
}

.stage {
  position: relative;
  min-width: 0;
  background: #1a2332;
  overflow: hidden;
}
.floor {
  position: absolute;
  inset: 18px 18px 58px;
  border-radius: 18px;
  background:
    radial-gradient(circle at 20% 15%, rgba(255,255,255,0.55), transparent 28%),
    linear-gradient(180deg, #e7eef7 0%, var(--floor) 45%, var(--floor-deep) 100%);
  border: 1px solid rgba(255,255,255,0.35);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.55), 0 30px 80px rgba(0,0,0,0.35);
  overflow: hidden;
}
.floor-grid {
  position: absolute; inset: 0; opacity: 0.28; pointer-events: none;
  background-image:
    linear-gradient(rgba(30,40,60,0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(30,40,60,0.08) 1px, transparent 1px);
  background-size: 28px 28px;
  transform: perspective(900px) rotateX(48deg) scale(1.25);
  transform-origin: center 20%;
}

.zone {
  position: absolute;
  border-radius: 14px;
  border: 1.5px dashed color-mix(in srgb, var(--z) 55%, transparent);
  background: color-mix(in srgb, var(--z) 12%, transparent);
  pointer-events: none;
}
.zone span {
  position: absolute;
  top: 6px; left: 8px;
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: color-mix(in srgb, var(--z) 75%, #0f172a);
  font-family: var(--mono);
}

.beams {
  position: absolute; inset: 0; width: 100%; height: 100%;
  pointer-events: none; z-index: 2;
}
.beam {
  fill: none;
  stroke-width: 0.35;
  stroke-linecap: round;
  opacity: 0.75;
  stroke-dasharray: 2 1.2;
  animation: dash 1.2s linear infinite;
}

.desk {
  position: absolute;
  width: 54px; height: 54px;
  margin: -27px 0 0 -27px;
  z-index: 3;
  cursor: pointer;
  border: 0;
  background: transparent;
  padding: 0;
}
.desk-body {
  width: 100%; height: 100%;
  border-radius: 16px;
  background: rgba(255,255,255,0.88);
  border: 1px solid rgba(15,23,42,0.08);
  box-shadow: 0 8px 18px rgba(15,23,42,0.12);
  display: grid;
  place-items: center;
  position: relative;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.desk:hover .desk-body, .desk.selected .desk-body {
  transform: translateY(-3px) scale(1.05);
  box-shadow: 0 14px 28px rgba(15,23,42,0.18);
}
.desk.selected .desk-body { outline: 2px solid var(--accent); }
.avatar {
  width: 30px; height: 30px; border-radius: 10px;
  display: grid; place-items: center;
  font-size: 0.62rem; font-weight: 800; color: white;
  background: linear-gradient(145deg, #64748b, #334155);
}
.desk.working .avatar { background: linear-gradient(145deg, #34d399, #059669); }
.desk.thinking .avatar { background: linear-gradient(145deg, #fbbf24, #d97706); }
.desk.queued .avatar { background: linear-gradient(145deg, #60a5fa, #2563eb); }
.status-pip {
  position: absolute; right: 5px; top: 5px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--idle); border: 1.5px solid white;
}
.desk.working .status-pip { background: var(--working); animation: pulse 1.2s infinite; }
.desk.thinking .status-pip { background: var(--thinking); animation: pulse 1.6s infinite; }
.desk.queued .status-pip { background: var(--queued); }
.desk-name {
  position: absolute; left: 50%; top: calc(100% + 4px);
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 0.58rem; font-weight: 700;
  color: #334155; background: rgba(255,255,255,0.85);
  border-radius: 999px; padding: 0.1rem 0.35rem;
  border: 1px solid rgba(15,23,42,0.06);
  pointer-events: none;
}

.stage-hud {
  position: absolute; left: 18px; right: 18px; bottom: 14px;
  display: flex; justify-content: space-between; align-items: center;
  gap: 1rem; color: #cbd5e1; font-size: 0.75rem;
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 0.55rem; }
.hud-label { color: #94a3b8; text-transform: uppercase; letter-spacing: 0.08em; font-size: 0.62rem; }
.hud-sub { color: #64748b; font-size: 0.68rem; white-space: nowrap; }
.mini-bar {
  width: 110px; height: 6px; border-radius: 999px; background: rgba(255,255,255,0.08); overflow: hidden;
}
.mini-bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--warm), var(--accent)); transition: width .5s ease; }
.legend {
  position: absolute; top: 28px; right: 28px;
  display: flex; gap: 0.55rem; flex-wrap: wrap; justify-content: end;
  font-size: 0.62rem; color: #e2e8f0; font-family: var(--mono);
}
.legend span { display: inline-flex; align-items: center; gap: 0.3rem; background: rgba(0,0,0,0.35); padding: 0.2rem 0.45rem; border-radius: 999px; }
.legend i { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.legend i.working { background: var(--working); }
.legend i.thinking { background: var(--thinking); }
.legend i.queued { background: var(--queued); }
.legend i.idle { background: var(--idle); }

.rail {
  border-left: 1px solid var(--hairline);
  background: var(--elevated);
  min-height: 0;
  display: flex; flex-direction: column; gap: 0.7rem;
  padding: 0.75rem;
  overflow: auto;
}
.rail-card {
  border: 1px solid var(--hairline);
  background: var(--subtle);
  border-radius: 14px;
  padding: 0.75rem;
}
.rail-card.grow { flex: 1; min-height: 180px; display: flex; flex-direction: column; }
.rail-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 0.55rem;
}
.rail-head h2 { margin: 0; font-size: 0.82rem; }
.hint { color: var(--faint); font-family: var(--mono); font-size: 0.68rem; }
.ghost {
  border: 0; background: transparent; color: var(--faint); cursor: pointer; font-size: 0.9rem;
}
.agent-hud { font-size: 0.85rem; }
.agent-hud.empty-hud { color: var(--faint); font-family: var(--mono); font-size: 0.75rem; padding: 0.4rem 0; }
.hud-name { font-size: 1.05rem; font-weight: 700; margin: 0.15rem 0; }
.hud-role { color: var(--muted); font-size: 0.78rem; margin-bottom: 0.55rem; }
.badge {
  display: inline-flex; align-items: center; gap: 0.35rem;
  border-radius: 999px; padding: 0.18rem 0.55rem;
  font-size: 0.68rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em;
}
.badge i { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.working { color: var(--working); background: rgba(61,220,151,0.12); }
.badge.thinking { color: var(--thinking); background: rgba(240,180,41,0.12); }
.badge.queued { color: var(--queued); background: var(--accent-soft); }
.badge.idle { color: var(--idle); background: rgba(122,132,153,0.12); }
.meter {
  margin-top: 0.7rem;
}
.meter label { display: flex; justify-content: space-between; font-size: 0.68rem; color: var(--faint); margin-bottom: 0.25rem; }
.meter .track { height: 6px; border-radius: 999px; background: rgba(255,255,255,0.06); overflow: hidden; }
.meter .track i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), #8eb6ff); }
.activity { margin-top: 0.7rem; color: var(--muted); font-size: 0.78rem; line-height: 1.35; }
.evidence { margin-top: 0.45rem; font-family: var(--mono); font-size: 0.65rem; color: var(--faint); word-break: break-all; }

.task-list { display: grid; gap: 0.5rem; overflow: auto; }
.task {
  border: 1px solid var(--hairline); border-radius: 12px; padding: 0.6rem 0.65rem; background: rgba(7,9,13,0.35);
}
.task-top { display: flex; justify-content: space-between; gap: 0.4rem; margin-bottom: 0.25rem; }
.task-agent { font-weight: 600; font-size: 0.8rem; }
.task-prompt { margin: 0; color: var(--muted); font-size: 0.72rem; line-height: 1.3; }
.bar { margin-top: 0.45rem; height: 4px; border-radius: 999px; background: rgba(255,255,255,0.06); overflow: hidden; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--warm), var(--working)); }
.task-meta { margin-top: 0.3rem; display: flex; justify-content: space-between; font-family: var(--mono); font-size: 0.62rem; color: var(--faint); }

.logs { max-height: 110px; overflow: auto; font-family: var(--mono); font-size: 0.65rem; display: grid; gap: 0.25rem; }
.log-line { color: var(--muted); border-left: 2px solid rgba(91,140,255,0.35); padding-left: 0.4rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log-line strong { color: var(--text); }
.empty { color: var(--faint); font-family: var(--mono); font-size: 0.72rem; padding: 0.4rem 0; }

.context-card { padding-bottom: 0.65rem; }
.ctx-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem 0.6rem;
  margin: 0 0 0.65rem;
}
.ctx-grid dt {
  margin: 0;
  color: var(--faint);
  font-size: 0.58rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.ctx-grid dd {
  margin: 0.1rem 0 0;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ctx-grid dd.mono, .mono { font-family: var(--mono); font-weight: 500; font-size: 0.72rem; color: #d7deea; }
.last-stream {
  border: 1px solid rgba(240,180,41,0.22);
  background: rgba(240,180,41,0.06);
  border-radius: 10px;
  padding: 0.45rem 0.55rem;
  margin-bottom: 0.65rem;
}
.last-stream p {
  margin: 0.2rem 0 0;
  font-family: var(--mono);
  font-size: 0.68rem;
  color: #f0d48a;
  line-height: 1.35;
  max-height: 3.6em;
  overflow: hidden;
}
.rail-head.tight { margin-top: 0.15rem; }
.working-list {
  display: grid;
  gap: 0.35rem;
  max-height: 180px;
  overflow: auto;
}
.working-item {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.15rem 0.5rem;
  padding: 0.4rem 0.45rem;
  border-radius: 10px;
  border: 1px solid var(--hairline);
  background: rgba(7,9,13,0.35);
  cursor: pointer;
  text-align: left;
  color: inherit;
  width: 100%;
  font: inherit;
}
.working-item:hover { border-color: rgba(91,140,255,0.45); }
.working-item .wi-name { font-weight: 600; font-size: 0.75rem; }
.working-item .wi-meta {
  grid-column: 2;
  font-family: var(--mono);
  font-size: 0.62rem;
  color: var(--faint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.working-item .wi-stream {
  grid-column: 1 / -1;
  font-size: 0.66rem;
  color: var(--muted);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.working-item .pip {
  width: 8px; height: 8px; border-radius: 50%; margin-top: 0.35rem;
  background: var(--idle);
}
.working-item.working .pip { background: var(--working); box-shadow: 0 0 0 3px rgba(61,220,151,0.18); }
.working-item.thinking .pip { background: var(--thinking); }
.working-item.queued .pip { background: var(--queued); }

.stream-card { min-height: 220px; }
.thought-now {
  font-size: 0.78rem; line-height: 1.4; color: var(--thinking);
  background: rgba(240,180,41,0.08); border: 1px solid rgba(240,180,41,0.22);
  border-radius: 10px; padding: 0.55rem 0.65rem; margin-bottom: 0.55rem;
  min-height: 2.6rem; white-space: pre-wrap; word-break: break-word;
}
.thought-now.idle { color: var(--faint); border-color: var(--hairline); background: rgba(255,255,255,0.03); }
.stream {
  flex: 1; min-height: 160px; max-height: 42vh; overflow: auto;
  font-family: var(--mono); font-size: 0.68rem; display: flex; flex-direction: column; gap: 0.28rem;
  padding-right: 0.15rem;
}
.stream-line {
  border-left: 2px solid rgba(148,163,184,0.35); padding: 0.15rem 0.45rem;
  color: var(--muted); white-space: pre-wrap; word-break: break-word;
}
.stream-line .k {
  display: inline-block; min-width: 4.2rem; font-weight: 600; text-transform: uppercase;
  font-size: 0.58rem; letter-spacing: 0.04em; margin-right: 0.35rem; opacity: 0.9;
}
.stream-line.thinking { border-color: rgba(240,180,41,0.7); color: #f0d48a; }
.stream-line.thinking .k { color: var(--thinking); }
.stream-line.text { border-color: rgba(61,220,151,0.55); color: #c6f6e0; }
.stream-line.text .k { color: var(--working); }
.stream-line.tool { border-color: rgba(91,140,255,0.7); color: #b9ceff; }
.stream-line.tool .k { color: var(--accent); }
.stream-line.system, .stream-line.raw { border-color: rgba(148,163,184,0.4); }
.stream-line.result { border-color: rgba(255,255,255,0.35); color: var(--text); }
.stream-line.result.err { border-color: #f87171; color: #fecaca; }
#stream-status.live { color: var(--working); }
#stream-status.stale { color: #f87171; }

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(61,220,151,0.55); }
  70% { box-shadow: 0 0 0 8px rgba(61,220,151,0); }
  100% { box-shadow: 0 0 0 0 rgba(61,220,151,0); }
}
@keyframes dash { to { stroke-dashoffset: -6; } }

@media (max-width: 1100px) {
  .stage-row { grid-template-columns: 1fr; }
  .rail { border-left: 0; border-top: 1px solid var(--hairline); max-height: 42vh; }
  .stage { min-height: 52vh; }
  body { overflow: auto; }
  .shell { height: auto; min-height: 100%; }
}
@media (max-width: 760px) {
  .topbar { grid-template-columns: 1fr; padding: 0.55rem 0.75rem; height: auto; gap: 0.55rem; }
  .kpis { justify-content: flex-start; }
}
