:root {
  --bg: #09090b;
  --surface: #101015;
  --surface-elevated: #16161f;
  --surface-bubble: #1a1a24;
  --border: #22222e;
  --border-focus: #3f3f50;
  --text: #f4f4f6;
  --text-muted: #737384;
  --text-dim: #4d4d5c;
  --accent: #10b981;
  --accent-glow: rgba(16, 185, 129, 0.15);
  --hazard: #ef4444;
  --hazard-bg: rgba(239, 68, 68, 0.12);
  --tool: #38bdf8;
  --tool-bg: rgba(56, 189, 248, 0.08);
  --font-sans: 'Plus Jakarta Sans', -apple-system, system-ui, sans-serif;
  --font-mono: 'Geist Mono', monospace;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* Header */
header.top-bar {
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  flex-shrink: 0;
}
.brand-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.brand-badge {
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 11px;
  color: var(--accent);
  letter-spacing: 0.06em;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.tab-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 6px 14px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.tab-btn:hover {
  color: var(--text);
  background: rgba(255,255,255,0.03);
}
.tab-btn.active {
  color: #fff;
  background: var(--surface-elevated);
  border-color: var(--border);
}

.auth-status {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: var(--font-mono);
  font-size: 11px;
}
.user-tag {
  color: var(--text-muted);
}
.btn-quit {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--hazard);
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
}
.btn-quit:hover {
  background: var(--hazard-bg);
  border-color: var(--hazard);
}

/* Main Layout */
main {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}
.tab-view {
  flex: 1;
  display: none;
  height: 100%;
  overflow: hidden;
}
.tab-view.active {
  display: flex;
}

/* Conversations Split View */
.col-list {
  width: 360px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.filter-box {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.input-search {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 8px 12px;
  border-radius: 4px;
  outline: none;
}
.input-search:focus {
  border-color: var(--border-focus);
}

.session-stream {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}
.session-item {
  padding: 10px 12px;
  border-radius: 4px;
  border: 1px solid transparent;
  cursor: pointer;
  margin-bottom: 4px;
  transition: all 0.15s ease;
}
.session-item:hover {
  background: rgba(255,255,255,0.02);
  border-color: var(--border);
}
.session-item.selected {
  background: var(--surface-elevated);
  border-color: var(--border);
}
.si-row-1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.si-user {
  font-weight: 600;
  font-size: 13px;
  color: #fff;
}
.si-time {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
}
.si-row-2 {
  display: flex;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-muted);
  align-items: center;
}
.tag-data {
  background: rgba(255,255,255,0.04);
  padding: 2px 5px;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.tag-accent {
  color: var(--accent);
  background: rgba(16, 185, 129, 0.08);
  border-color: rgba(16, 185, 129, 0.2);
}
.tag-hazard {
  color: var(--hazard);
  background: var(--hazard-bg);
  border-color: rgba(239, 68, 68, 0.3);
}

/* Detail Column */
.col-detail {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.detail-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--font-mono);
  font-size: 12px;
}
.dh-specs {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
}
.dh-item {
  display: flex;
  gap: 5px;
}
.dh-k { color: var(--text-muted); }
.dh-v { color: #fff; font-weight: 500; }

.transcript-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Chat Messages */
.msg-unit {
  max-width: 820px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.msg-unit.user {
  align-self: flex-end;
}
.msg-unit.assistant {
  align-self: flex-start;
}
.msg-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.msg-unit.user .msg-label {
  justify-content: flex-end;
}
.msg-body {
  padding: 12px 16px;
  border-radius: 6px;
  font-size: 13.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-unit.user .msg-body {
  background: var(--surface-bubble);
  border: 1px solid var(--border);
  color: #fff;
}
.msg-unit.assistant .msg-body {
  background: var(--surface);
  border: 1px solid var(--border);
  color: #e4e4e7;
}

/* Tools */
.tool-tile {
  background: rgba(0,0,0,0.5);
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: 11.5px;
  margin-top: 4px;
}
.tool-bar {
  padding: 6px 10px;
  background: rgba(255,255,255,0.02);
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.tool-bar:hover {
  background: rgba(255,255,255,0.04);
}
.tool-tag {
  color: var(--tool);
  font-weight: 500;
}
.tool-content {
  padding: 10px;
  border-top: 1px solid var(--border);
  background: #050507;
  max-height: 240px;
  overflow-y: auto;
  white-space: pre-wrap;
  color: #9494a0;
  font-size: 11px;
}

.turn-metric {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 2px;
}

/* Telemetry Grid */
.telemetry-board {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
}
.board-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 24px;
}
.cell-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px;
}
.stat-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat-num {
  font-family: var(--font-mono);
  font-size: 24px;
  font-weight: 700;
  color: #fff;
  margin: 8px 0 2px;
}
.stat-foot {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--accent);
}

/* Tables */
.data-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}
.dc-header {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
table.compact-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 11.5px;
}
th {
  text-align: left;
  padding: 10px 18px;
  color: var(--text-muted);
  font-weight: 500;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,0.01);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
td {
  padding: 11px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
tr:hover td {
  background: rgba(255,255,255,0.02);
}

/* Login Dialog */
#auth-dialog {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dialog-frame {
  width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 24px;
}
.df-title {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.05em;
  margin-bottom: 16px;
  color: #fff;
}
.field-row {
  margin-bottom: 12px;
}
.field-row label {
  display: block;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 4px;
}
.field-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 8px 10px;
  color: #fff;
  font-family: var(--font-mono);
  font-size: 12px;
  border-radius: 4px;
  outline: none;
}
.field-input:focus {
  border-color: var(--border-focus);
}
.btn-exec {
  width: 100%;
  background: #fff;
  color: #000;
  border: none;
  padding: 10px;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 6px;
}
.btn-exec:hover {
  background: #e4e4e7;
}
.btn-exec:active {
  transform: scale(0.98);
}
.err-hint {
  color: var(--hazard);
  font-family: var(--font-mono);
  font-size: 11px;
  margin-top: 8px;
  display: none;
}

/* Scrollbars */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.12); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255, 255, 255, 0.22); }

/* System Prompt Container */
.system-prompt-box {
  padding: 12px 16px;
  background: #111116;
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.5;
  color: #a1a1aa;
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Sub-branch / Base chat navigation buttons */
.btn-subbranch {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  color: var(--tool);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 3px;
  cursor: pointer;
  text-decoration: none;
}
.btn-subbranch:hover {
  background: rgba(56, 189, 248, 0.12);
  border-color: var(--tool);
}

/* Thinking / Reasoning block */
.thinking-tile {
  background: rgba(139, 92, 246, 0.05);
  border: 1px solid rgba(139, 92, 246, 0.25);
  border-radius: 4px;
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: 11.5px;
  margin-top: 4px;
}
.thinking-bar {
  padding: 6px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  background: rgba(139, 92, 246, 0.08);
}
.thinking-bar:hover {
  background: rgba(139, 92, 246, 0.14);
}
.thinking-tag {
  color: #c4b5fd;
  font-weight: 500;
  font-size: 11px;
}
.thinking-arrow {
  color: #a78bfa;
  font-size: 10px;
}
.thinking-content {
  padding: 10px;
  border-top: 1px solid rgba(139, 92, 246, 0.2);
  background: #09090d;
  max-height: 260px;
  overflow-y: auto;
  white-space: pre-wrap;
  color: #d4d4d8;
  font-size: 11px;
  line-height: 1.55;
}

/* Attachment preview tile */
.att-tile {
  background: rgba(16, 185, 129, 0.05);
  border: 1px solid rgba(16, 185, 129, 0.25);
  border-radius: 4px;
  overflow: hidden;
  margin-top: 6px;
  font-family: var(--font-mono);
}
.att-header {
  padding: 6px 10px;
  background: rgba(16, 185, 129, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
}
.att-header:hover {
  background: rgba(16, 185, 129, 0.14);
}
.att-tag {
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}
.att-content {
  padding: 10px;
  border-top: 1px solid rgba(16, 185, 129, 0.2);
  background: #090c0a;
  max-height: 240px;
  overflow-y: auto;
  white-space: pre-wrap;
  color: #d1fae5;
  font-size: 11px;
  line-height: 1.5;
}

/* Tool detail JSON formatting */
.tool-sec-title {
  color: #71717a;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.05em;
  margin-top: 6px;
  margin-bottom: 3px;
}
.tool-sec-title:first-child {
  margin-top: 0;
}
.json-block {
  background: #000;
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 3px;
  padding: 8px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 10.5px;
  line-height: 1.4;
  color: #e4e4e7;
  max-height: 200px;
  overflow-y: auto;
}

/* Sidebar Sub-chat items & Title */
.tag-subchat {
  background: rgba(56, 189, 248, 0.1);
  color: var(--tool);
  border: 1px solid rgba(56, 189, 248, 0.3);
  font-size: 9.5px;
  font-weight: 600;
  padding: 1px 5px;
}
.si-title {
  font-size: 11px;
  color: #d4d4d8;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 170px;
}

/* Tool Delegated Sub-Agent Bar */
.subchat-delegate-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(56, 189, 248, 0.06);
  border-top: 1px solid rgba(56, 189, 248, 0.18);
  padding: 6px 10px;
  font-family: var(--font-mono);
  font-size: 11px;
}
.subchat-delegate-text {
  color: #bae6fd;
}
.subchat-delegate-text b {
  color: #fff;
}

/* Delegated Sub-chats Overview Panel */
.delegated-subchats-panel {
  margin-top: 12px;
  padding: 12px;
  background: rgba(0, 0, 0, 0.4);
  border: 1px dashed rgba(56, 189, 248, 0.35);
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.dsp-title {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--tool);
  letter-spacing: 0.04em;
}
.dsp-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.dsp-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: 11px;
}
.dsp-name {
  font-weight: 600;
  color: #fff;
}

/* Top Banner when viewing a sub-agent session */
.parent-chat-banner {
  padding: 8px 12px;
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.25);
  border-radius: 4px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: #bae6fd;
}
