/* ======================================================================
   Virtual Trading Plus — counterfactual UI.
   Single flat stylesheet, no framework. Light/dark via data-theme auto.
   ====================================================================== */

*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg-0: #fafbfc; --bg-1: #ffffff; --bg-2: #f4f5f7; --bg-3: #e8eaed;
  --fg-0: #1a1d23; --fg-1: #3d4250; --fg-2: #6b7080; --fg-3: #9aa0ad;
  --muted: #9aa0ad;
  --border: #dfe1e6;
  --accent: #2563eb; --accent-light: #dbeafe;
  --red: #dc2626; --green: #16a34a; --amber: #d97706; --purple: #7c3aed;
  --frame: #c6cad1;
  --grid: rgba(0,0,0,0.06);
  --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,.07);
  --radius: 10px; --radius-sm: 6px;
  --font-ui: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --font-mono: 'SF Mono', ui-monospace, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg-0:#0d1117; --bg-1:#161b22; --bg-2:#1c2129; --bg-3:#272d38;
    --fg-0:#e6edf3; --fg-1:#c9d1d9; --fg-2:#8b949e; --fg-3:#6e7681;
    --muted:#8b949e;
    --border:#30363d;
    --frame:#3a4150;
    --grid:rgba(255,255,255,0.08);
  }
}

body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--fg-0);
  background: var(--bg-0);
  -webkit-font-smoothing: antialiased;
}

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-1);
  border-bottom: 1px solid var(--border);
}
.app-header h1 { margin: 0; font-size: 16px; font-weight: 600; }
.app-header .subtitle { display: block; font-size: 11px; color: var(--fg-2); }
.controls { display: flex; gap: 8px; }
.controls button {
  border: 1px solid var(--border);
  background: var(--bg-1);
  color: var(--fg-0);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font: inherit;
  cursor: pointer;
}
.controls button:hover { background: var(--bg-2); }
.controls button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }

.grid {
  display: grid;
  grid-template-columns: 280px 1fr 1fr 1fr;
  grid-template-areas:
    "params stats   stats   stats"
    "params price   price   bubble"
    "params fan     fan     reg"
    "params volume  trust   trust"
    "params agents  agents  agents"
    "params msg     msg     events";
  gap: 10px; padding: 10px;
}
.panel-params   { grid-area: params; max-height: 92vh; overflow-y: auto; }
.panel-stats    { grid-area: stats;  display: grid; grid-template-columns: repeat(9, 1fr); gap: 0; }
.fig-price      { grid-area: price;  }
.fig-bubble     { grid-area: bubble; }
.fig-fan        { grid-area: fan;    }
.fig-volume     { grid-area: volume; }
.fig-reg        { grid-area: reg;    }
.fig-trust      { grid-area: trust;  }
.panel-agents   { grid-area: agents; }
.panel-messages { grid-area: msg;    }
.panel-events   { grid-area: events; }

.panel {
  background: var(--bg-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  box-shadow: var(--shadow-sm);
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 6px;
}
.panel-header h2 { margin: 0; font-size: 13px; font-weight: 600; color: var(--fg-1); }
.panel-header .muted { font-size: 11px; color: var(--fg-2); }

.panel-chart { min-height: 220px; display: flex; flex-direction: column; }
.panel-chart canvas { flex: 1; width: 100%; height: 220px; }
.fig-head { margin-bottom: 4px; }
.fig-head h2 { margin: 0; font-size: 12px; font-weight: 600; color: var(--fg-1); }

/* ---------- Stats row ---------- */
.panel-stats .stat {
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 4px 12px;
  border-right: 1px solid var(--border);
}
.panel-stats .stat:last-child { border-right: 0; }
.panel-stats .label { font-size: 10px; color: var(--fg-2); text-transform: uppercase; letter-spacing: 0.04em; }
.panel-stats .value { font-family: var(--font-mono); font-weight: 600; font-size: 14px; color: var(--fg-0); }
.panel-stats .value.warn { color: var(--red); }
.panel-stats .value.ok   { color: var(--green); }
.panel-stats .value.price { color: var(--accent); }
.panel-stats .value.fv    { color: var(--amber); }

/* ---------- Parameters ---------- */
.psec { border-bottom: 1px dashed var(--border); margin-bottom: 6px; padding-bottom: 6px; }
.psec[open] { }
.psec summary { cursor: pointer; font-weight: 600; font-size: 12px; color: var(--fg-1); padding: 4px 0; }
.psec summary:hover { color: var(--accent); }
.psec-body { padding: 4px 0 6px; }
.param-row { margin-bottom: 8px; }
.param-head { display: flex; justify-content: space-between; font-size: 11px; color: var(--fg-1); margin-bottom: 2px; }
.param-head label { font-size: 11px; }
.pval { font-family: var(--font-mono); color: var(--fg-0); }
input[type=range] { width: 100%; accent-color: var(--accent); }
.input-select {
  width: 100%; padding: 4px 6px; font: inherit; font-size: 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg-1); color: var(--fg-0);
}

/* Toggle switch */
.slide-toggle { display: inline-block; position: relative; width: 32px; height: 18px; }
.slide-toggle input { opacity: 0; width: 0; height: 0; }
.slide-track { position: absolute; inset: 0; background: var(--bg-3); border-radius: 9px; transition: background .15s; }
.slide-thumb { position: absolute; left: 2px; top: 2px; width: 14px; height: 14px; background: #fff; border-radius: 50%; transition: transform .15s; }
.slide-toggle input:checked + .slide-track { background: var(--accent); }
.slide-toggle input:checked + .slide-track .slide-thumb { transform: translateX(14px); }
.dlm-treatment-row { display: flex; gap: 10px; }
.dlm-treatment-opt { display: flex; align-items: center; gap: 6px; font-size: 11px; }

/* ---------- Agent table ---------- */
.agent-table-wrap { max-height: 440px; overflow-y: auto; }
#agent-table {
  width: 100%; border-collapse: collapse; font-size: 12px;
  font-variant-numeric: tabular-nums;
}
#agent-table thead th {
  position: sticky; top: 0; background: var(--bg-2);
  border-bottom: 1px solid var(--border);
  padding: 6px 8px; text-align: left;
  font-size: 11px; font-weight: 600;
  color: var(--fg-2); text-transform: uppercase; letter-spacing: 0.03em;
  cursor: pointer;
}
#agent-table thead th:hover { color: var(--accent); }
#agent-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); }
#agent-table tr:hover { background: var(--bg-2); }
#agent-table tr.traced { background: var(--accent-light); }
#agent-table tr.expanded { background: var(--bg-2); }
.col-name { font-weight: 600; }
.col-cash, .col-inv, .col-wealth { text-align: right; font-family: var(--font-mono); }
.badge.llm { font-size: 9px; padding: 1px 5px; background: var(--purple); color: #fff; border-radius: 3px; }
.tag { font-size: 10px; padding: 2px 6px; border-radius: 4px; background: var(--bg-3); font-family: var(--font-mono); }
.tag.action-bid  { background: rgba(22,163,74,0.12);  color: var(--green); }
.tag.action-ask  { background: rgba(220,38,38,0.12);  color: var(--red); }
.tag.action-hold { background: var(--bg-3); color: var(--fg-2); }
.expand-row td { padding: 0; background: var(--bg-0); }
.expand-pad { padding: 8px 12px; }
.expand-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; font-size: 12px; }
.expand-head .export-trace {
  margin-left: auto; padding: 2px 8px; font-size: 11px;
  border: 1px solid var(--border); background: var(--bg-1); border-radius: var(--radius-sm); cursor: pointer;
}
.trace-table { width: 100%; border-collapse: collapse; font-size: 11px; font-family: var(--font-mono); }
.trace-table th, .trace-table td { border-bottom: 1px solid var(--border); padding: 3px 6px; text-align: left; }
.mono { font-family: var(--font-mono); font-size: 10px; color: var(--fg-2); }

/* ---------- Messages / Events ---------- */
#message-log, #event-log { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; font-size: 11px; font-family: var(--font-mono); }
.msg, .event { display: grid; grid-template-columns: 50px 90px 100px 140px 1fr; gap: 6px; padding: 3px 6px; border-bottom: 1px dashed var(--border); }
.msg .warn { color: var(--red); font-weight: 600; }
.msg.kind-regulator-warning { background: rgba(220,38,38,0.06); }
.msg.kind-valuation-report  { color: var(--fg-1); }
.msg-tick { color: var(--fg-3); }
.msg-from { color: var(--fg-0); font-weight: 600; }
.msg-to   { color: var(--fg-2); }
.msg-kind { color: var(--purple); }
.msg-body { color: var(--fg-1); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.event { grid-template-columns: 50px 140px 1fr; }
.ev-type { color: var(--accent); }
.ev-tick { color: var(--fg-3); }

/* ---------- Scrollbars (subtle) ---------- */
.agent-table-wrap::-webkit-scrollbar,
.panel-params::-webkit-scrollbar,
#message-log::-webkit-scrollbar,
#event-log::-webkit-scrollbar { width: 8px; }
.agent-table-wrap::-webkit-scrollbar-thumb,
.panel-params::-webkit-scrollbar-thumb,
#message-log::-webkit-scrollbar-thumb,
#event-log::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 4px; }

/* ---------- Responsive collapse ---------- */
@media (max-width: 1100px) {
  .grid {
    grid-template-columns: 1fr;
    grid-template-areas:
      "stats"
      "params"
      "price"
      "bubble"
      "fan"
      "volume"
      "reg"
      "trust"
      "agents"
      "msg"
      "events";
  }
  .panel-stats { grid-template-columns: repeat(3, 1fr); }
}
