:root {
  --bg: #ffffff; --fg: #1a1a1a; --muted: #6b7280; --border: #e5e7eb;
  --panel: #f9fafb; --accent: #2563eb; --accent-fg: #ffffff;
  --ok: #16a34a; --err: #dc2626; --buy: #16a34a; --sell: #dc2626;
}
[data-theme="dark"] {
  --bg: #0f1115; --fg: #e6e6e6; --muted: #9ca3af; --border: #2a2f3a;
  --panel: #171a21; --accent: #3b82f6; --accent-fg: #ffffff;
  --ok: #22c55e; --err: #ef4444; --buy: #22c55e; --sell: #f87171;
}

* { box-sizing: border-box; }
body {
  margin: 0; font-family: system-ui, sans-serif; background: var(--bg); color: var(--fg);
}
header {
  display: flex; align-items: center; gap: 1rem; padding: 0.8rem 1.2rem;
  border-bottom: 1px solid var(--border); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
header h1 { font-size: 1.1rem; margin: 0; }
header .spacer { flex: 1; }
main { padding: 1.2rem; max-width: 1100px; margin: 0 auto; }

.toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-bottom: 1rem; }
button, select, input, textarea {
  font: inherit; color: var(--fg); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: 0.45rem 0.7rem;
}
button { cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.ghost { background: transparent; }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.pill { padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.75rem; border: 1px solid var(--border); }
.pill.on { color: var(--ok); border-color: var(--ok); }
.pill.off { color: var(--muted); }

.panel { border: 1px solid var(--border); border-radius: 10px; padding: 1rem; background: var(--panel); margin-bottom: 1rem; }
.muted { color: var(--muted); }

table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { border-bottom: 1px solid var(--border); padding: 0.4rem 0.55rem; text-align: left; }
.side-buy { color: var(--buy); font-weight: 600; }
.side-sell { color: var(--sell); font-weight: 600; }
.side-signal { color: var(--accent); font-weight: 600; }

/* live RSI cell highlighting */
.rsi-ob { color: var(--sell); font-weight: 700; }
.rsi-os { color: var(--buy); font-weight: 700; }
.rsi-cell { text-align: center; }
.rsi-na { color: var(--muted); }
/* closed (signal-basis) vs live (forming) values shown together in one cell */
.rsi-closed { display: block; font-weight: 700; }
.rsi-live { display: block; font-size: 0.75em; font-weight: 400; color: var(--muted); }
.rsi-legend { font-size: 0.8em; margin-top: 0.35rem; }

/* inline help / tooltips */
.hint { cursor: help; border-bottom: 1px dotted var(--muted); }
details.help > summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; }
details.help table { margin-top: 0.5rem; }
.field-help { font-size: 0.75rem; color: var(--muted); margin-top: -0.1rem; }

/* modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: none;
  align-items: center; justify-content: center; z-index: 50;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  width: min(640px, 92vw); max-height: 88vh; overflow: auto; padding: 1.2rem;
}
.modal h2 { margin-top: 0; font-size: 1.05rem; }
.modal .row { display: flex; flex-direction: column; gap: 0.25rem; margin-bottom: 0.7rem; }
.modal label { font-size: 0.8rem; color: var(--muted); }
.modal .actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; }
.status-line { font-size: 0.85rem; margin: 0.4rem 0; min-height: 1.2em; }
.status-ok { color: var(--ok); }
.status-err { color: var(--err); }

/* asset tree */
.group { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 0.5rem; }
.group > summary { cursor: pointer; padding: 0.4rem 0.6rem; font-weight: 600; }
.group .assets { padding: 0.3rem 0.8rem; display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 0.2rem; }
.asset { font-size: 0.82rem; display: flex; gap: 0.35rem; align-items: center; }
.spinner { display: inline-block; width: 1em; height: 1em; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== redesign: shell, drawer, tabs, KPIs, heatmap, toasts ===== */
.iconbtn { background: transparent; border: 1px solid transparent; font-size: 1.05rem; line-height: 1; padding: 0.35rem 0.5rem; }
.iconbtn:hover { border-color: var(--border); }
.iconbtn.active { color: var(--accent); border-color: var(--accent); }
.small { font-size: 0.8rem; padding: 0.25rem 0.45rem; }
button.small { padding: 0.25rem 0.55rem; }
header h1 { white-space: nowrap; }

.sse-dot { color: var(--muted); font-size: 0.7rem; transition: color 0.3s; }
.sse-dot.live { color: var(--ok); }
.sse-dot.down { color: var(--err); }

/* tab bar */
.tabbar { display: flex; align-items: center; gap: 0.25rem; padding: 0 1.2rem; border-bottom: 1px solid var(--border);
  background: var(--panel); position: sticky; top: 3.2rem; z-index: 9; overflow-x: auto; }
.tabbar .spacer { flex: 1; }
.tab { background: transparent; border: none; border-bottom: 2px solid transparent; border-radius: 0;
  padding: 0.6rem 0.9rem; color: var(--muted); white-space: nowrap; }
.tab:hover { color: var(--fg); }
.tab.active { color: var(--fg); border-bottom-color: var(--accent); font-weight: 600; }

.view { animation: fade 0.15s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
/* symmetric equal-width columns; each .col stacks its panels (top-aligned) */
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start; }
.cols2 .col > .panel:last-child { margin-bottom: 0; }
.panel-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.6rem; flex-wrap: wrap; }
.panel-head .spacer { flex: 1; }

/* KPI stat cards */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.8rem; margin-bottom: 1rem; }
.kpi { border: 1px solid var(--border); border-radius: 10px; padding: 0.8rem 1rem; background: var(--panel); }
.kpi .k-label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.kpi .k-value { font-size: 1.4rem; font-weight: 700; margin-top: 0.15rem; }
.kpi .k-sub { font-size: 0.75rem; color: var(--muted); }
.kpi.state-running { border-left: 3px solid var(--ok); }
.kpi.state-error { border-left: 3px solid var(--err); }
.kpi.state-idle { border-left: 3px solid var(--muted); }

/* capped, scrollable sub-window (e.g. dashboard Live RSI) so it never exceeds
   the screen; only this box scrolls, not the whole page */
.scrollcap { max-height: 52vh; overflow: auto; }
.scrollcap table thead th { position: sticky; top: 0; z-index: 1; background: var(--panel); }

/* heatmap + sparkline */
table.heatmap td.heat { transition: background 0.3s; border-radius: 4px; }
.spark { display: block; margin: 0.15rem auto 0; opacity: 0.7; }
.spark polyline { fill: none; stroke: currentColor; stroke-width: 1; }
a.sym { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--muted); cursor: pointer; }

/* signal cards */
.sigcard { border: 1px solid var(--border); border-left-width: 3px; border-radius: 8px; padding: 0.5rem 0.7rem; margin-bottom: 0.5rem; background: var(--bg); }
.sigcard.side-buy { border-left-color: var(--buy); }
.sigcard.side-sell { border-left-color: var(--sell); }
.sigcard.side-signal { border-left-color: var(--accent); }
.sigcard-head { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.sigsym { font-size: 0.95rem; }
.sigprice { color: var(--muted); }
.sigtime { margin-left: auto; font-size: 0.78rem; }
.badge { font-size: 0.72rem; font-weight: 700; padding: 0.1rem 0.4rem; border-radius: 999px; border: 1px solid var(--border); }
.badge-buy { color: var(--buy); border-color: var(--buy); }
.badge-sell { color: var(--sell); border-color: var(--sell); }
.badge-signal { color: var(--accent); border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 0.3rem; margin-top: 0.35rem; }
.sigstamp { font-size: 0.74rem; margin-top: 0.35rem; }
.sigstamp .rel { opacity: 0.8; }
.chip { font-size: 0.72rem; padding: 0.05rem 0.4rem; border: 1px solid var(--border); border-radius: 6px; color: var(--muted); }

/* config drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: none; z-index: 40; }
.drawer-backdrop.open { display: block; }
.drawer { position: fixed; top: 0; left: 0; height: 100vh; width: min(320px, 86vw); background: var(--panel);
  border-right: 1px solid var(--border); z-index: 41; padding: 1rem; transform: translateX(-100%);
  transition: transform 0.22s ease; overflow-y: auto; }
.drawer.open { transform: translateX(0); }
.drawer-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.3rem; }
.drawer hr { border: none; border-top: 1px solid var(--border); margin: 0.6rem 0; }
.drawer-item { display: block; width: 100%; text-align: left; background: var(--bg); margin-bottom: 0.4rem; }
.drawer-item:hover { border-color: var(--accent); }

/* toasts */
.toast-root { position: fixed; right: 1rem; bottom: 1rem; z-index: 60; display: flex; flex-direction: column; gap: 0.5rem; }
.toast { border: 1px solid var(--border); border-left-width: 3px; border-radius: 8px; background: var(--panel);
  padding: 0.6rem 0.8rem; min-width: 220px; max-width: 360px; box-shadow: 0 4px 18px rgba(0,0,0,0.18);
  animation: toastin 0.2s ease; }
.toast.t-ok { border-left-color: var(--ok); }
.toast.t-err { border-left-color: var(--err); }
.toast.t-buy { border-left-color: var(--buy); }
.toast.t-sell { border-left-color: var(--sell); }
.toast.t-info { border-left-color: var(--accent); }
@keyframes toastin { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

@media (max-width: 760px) {
  .grid2, .cols2 { grid-template-columns: 1fr; }
  .vis-md { display: none; }
  header { gap: 0.5rem; padding: 0.6rem 0.8rem; }
  header h1 { font-size: 1rem; }
  main { padding: 0.8rem; }
  .tabbar { top: 3rem; }
}
@media (prefers-reduced-motion: reduce) {
  .view, .drawer, .toast { animation: none; transition: none; }
}
