:root {
  --bg: #f6f7f9; --card: #ffffff; --fg: #0f172a; --muted: #64748b; --border: #e2e8f0;
  --grid: #eef2f7; --accent: #2563eb; --bid: #16a34a; --ask: #dc2626; --warn: #d97706; --run: #94a3b8;
  --jump: #7c3aed; --c1: #2563eb; --c2: #ea580c; --c3: #0d9488;
  --c4: #9333ea; --c5: #db2777; --c6: #65a30d; --c7: #ca8a04; --c8: #475569;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1020; --card: #121a2e; --fg: #e2e8f0; --muted: #94a3b8; --border: #1f2a44;
    --grid: #1a2440; --accent: #60a5fa; --bid: #4ade80; --ask: #f87171; --warn: #fbbf24; --run: #64748b;
    --jump: #a78bfa; --c1: #60a5fa; --c2: #fb923c; --c3: #2dd4bf;
    --c4: #c084fc; --c5: #f472b6; --c6: #a3e635; --c7: #facc15; --c8: #cbd5e1;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1020; --card: #121a2e; --fg: #e2e8f0; --muted: #94a3b8; --border: #1f2a44;
  --grid: #1a2440; --accent: #60a5fa; --bid: #4ade80; --ask: #f87171; --warn: #fbbf24; --run: #64748b;
  --jump: #a78bfa; --c1: #60a5fa; --c2: #fb923c; --c3: #2dd4bf;
    --c4: #c084fc; --c5: #f472b6; --c6: #a3e635; --c7: #facc15; --c8: #cbd5e1;
}

* { box-sizing: border-box; }
body {
  margin: 0; padding: 12px 16px 24px; background: var(--bg); color: var(--fg);
  font: 13px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}
.muted { color: var(--muted); }
header { display: flex; flex-wrap: wrap; gap: 10px 24px; align-items: center; justify-content: space-between; }
.title { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
h1 { font-size: 16px; margin: 0; }
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button {
  background: var(--card); color: var(--fg); border: 0; padding: 5px 10px; cursor: pointer; font: inherit;
}
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: #fff; }
.select { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); }
select { font: inherit; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--border); background: var(--card); color: var(--fg); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 10px 0; font-size: 12px; align-items: center; }
.legend details { flex-basis: 100%; }
.legend summary { cursor: pointer; }
.sw { display: inline-block; vertical-align: middle; margin-right: 5px; }
.sw.line { width: 14px; height: 2px; background: var(--accent); }
.sw.bid { width: 14px; height: 1px; background: var(--bid); }
.sw.ask { width: 14px; height: 1px; background: var(--ask); }
.sw.dot { width: 8px; height: 8px; border-radius: 50%; }
.sw.buy { background: var(--bid); } .sw.sell { background: var(--ask); }
.sw.jump { width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 8px solid var(--jump); }
.sw.run { width: 0; height: 12px; border-left: 1px dashed var(--run); }

#grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 1200px) { #grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #grid[data-layout="stock"] > .card { order: var(--pair-order, 0); } }
#grid[data-layout="sector"] .wide { grid-column: 1 / -1; }
body[data-book="off"] .book-only, body[data-trades="off"] .trades-only { display: none; }
@media (max-width: 640px) { #grid { grid-template-columns: 1fr; } }

.card { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 8px 8px 2px; min-width: 0; }
.card .head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; padding: 0 4px; }
.card .sym { font-weight: 700; font-size: 14px; }
.card .sym .role { font-weight: 400; font-size: 11px; color: var(--muted); margin-left: 4px; }
.card .px { font-variant-numeric: tabular-nums; }
.card .chg.up { color: var(--bid); } .card .chg.down { color: var(--ask); }
.card .stats { display: flex; flex-wrap: wrap; gap: 2px 10px; padding: 2px 4px 0; font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.card .stats b { color: var(--fg); font-weight: 600; }
.card .stats .hot b { color: var(--warn); }
.card .plot { height: 300px; }

.err { color: var(--ask); }

body[data-layout="sector"] .stock-only, body:not([data-layout="sector"]) .sector-only { display: none; }
#grid[data-layout="sector"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1000px) { #grid[data-layout="sector"] { grid-template-columns: 1fr; } }
.card.sector .plot { height: 380px; }
.card .members { display: flex; flex-direction: column; gap: 1px; padding: 4px 4px 0; font-size: 11.5px; font-variant-numeric: tabular-nums; }
.card .members .hot b { color: var(--warn); }
.card .members .chg.up { color: var(--bid); } .card .members .chg.down { color: var(--ask); }

/* engineering view */
body[data-layout="ops"] .market-only { display: none; }
#grid[data-layout="ops"] { grid-template-columns: repeat(2, minmax(0, 1fr)); margin-top: 10px; }
#grid[data-layout="ops"] .wide { grid-column: 1 / -1; }
@media (max-width: 900px) { #grid[data-layout="ops"] { grid-template-columns: 1fr; } }
.card .plot.tall { height: 340px; }
.card .head .muted { font-size: 11.5px; text-align: right; }
.ops-summary { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; }
.chip { display: flex; flex-direction: column; gap: 1px; padding: 4px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 11.5px; font-variant-numeric: tabular-nums; }
.chip b { font-size: 13px; }
.chip.ok b, td.ok { color: var(--bid); } .chip.bad b, td.bad { color: var(--ask); } .chip.warn b, td.warn { color: var(--warn); }
.tbl { overflow-x: auto; padding: 4px 4px 8px; }
.tbl.scroll { max-height: 420px; overflow-y: auto; }
.tbl table { border-collapse: collapse; width: 100%; font-size: 11.5px; font-variant-numeric: tabular-nums; }
.tbl th { text-align: left; color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--card); border-bottom: 1px solid var(--border); padding: 3px 8px 3px 0; white-space: nowrap; }
.tbl td { padding: 2px 8px 2px 0; border-bottom: 1px solid var(--grid); white-space: nowrap; vertical-align: top; }
.tbl td.err-cell { white-space: normal; word-break: break-word; min-width: 220px; }
/* engineering view: legends under the card titles */
.keys { display: flex; flex-wrap: wrap; gap: 2px 14px; padding: 0 8px 2px; font-size: 11px; color: var(--muted); }
.key { display: inline-flex; align-items: center; gap: 5px; }
.sw { display: inline-block; width: 12px; height: 10px; flex: none; }
.sw.box { background: var(--c); opacity: 0.75; border-radius: 2px; }
.sw.line { height: 0; border-top: 2px solid var(--c); }
.sw.dash { height: 0; border-top: 2px dashed var(--c); }
.sw.dot { height: 0; border-top: 2px dotted var(--c); }
.sw.vline { width: 0; height: 12px; border-left: 2px solid var(--c); margin: 0 5px; }
.sw.vdot { width: 0; height: 12px; border-left: 2px dotted var(--c); margin: 0 5px; }
.sw.x { width: auto; height: auto; color: var(--c); font-style: normal; font-weight: 700; }
.sw.x::before { content: "✕"; }
/* engineering view: issues sparkline + scorecard help */
.tbl td.spark { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 1px; color: var(--warn); }
.tbl tr.muted td { color: var(--muted); }
.note { font-size: 11px; line-height: 1.45; margin: 4px 4px 8px; }

/* signals panel (below the market grid) */
#sig { margin-top: 18px; display: flex; flex-direction: column; gap: 10px; }
#sig[hidden], #sig-stock[hidden], body[data-layout="ops"] #sig { display: none; }
#sig-stock { display: flex; flex-direction: column; gap: 10px; }
.sig-head { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; margin-top: 6px; }
.sig-head h2 { font-size: 14px; margin: 0; }
.card .plot.heat { height: 250px; }
.sig-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 1100px) { .sig-row { grid-template-columns: 1fr; } }
#sig details.legend { display: block; line-height: 1.5; }
.ll-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.ll-chip { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; padding: 8px 12px; background: var(--card);
  border: 1px solid var(--border); border-left: 3px solid var(--bid); border-radius: 8px; font-size: 12px; font-variant-numeric: tabular-nums; }
.ll-chip.free { border-left-color: var(--muted); }
.ll-chip .ll-pair { font-size: 14px; }
.ll-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr); gap: 10px; }
@media (max-width: 1000px) { .ll-row { grid-template-columns: 1fr; } }
.card .plot.ll { height: 340px; }
a.jump { color: var(--accent); text-decoration: none; font-size: 12.5px; }
a.jump:hover { text-decoration: underline; }
.card.wide .members { flex-direction: row; flex-wrap: wrap; gap: 4px 18px; }

/* competitors view (rivals.js) */
body[data-layout="rivals"] .market-only, body[data-layout="rivals"] #sig { display: none; }
#grid[data-layout="rivals"] { margin-top: 10px; }
#grid[data-layout="rivals"] .wide { grid-column: 1 / -1; }
#grid[data-layout="rivals"] .half { grid-column: span 2; }
@media (max-width: 1200px) { #grid[data-layout="rivals"] .half { grid-column: 1 / -1; } }
@media (max-width: 640px) { #grid[data-layout="rivals"] .half, #grid[data-layout="rivals"] .wide { grid-column: auto; } }
.riv-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; }
.riv-bar .seg { flex-wrap: wrap; }
.riv-sub { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.riv-sub h2 { font-size: 16px; margin: 0; }
.riv-sub h2 .muted { font-size: 12px; font-weight: 400; margin-left: 6px; }
.riv-dist { margin: 0; font-size: 12.5px; line-height: 1.5; max-width: 1200px; }
.riv-kinds { padding: 0; gap: 4px 8px; }
.riv-k { cursor: pointer; padding: 1px 6px; border: 1px solid var(--border); border-radius: 10px; user-select: none; }
.riv-k.off { opacity: 0.35; text-decoration: line-through; }
#riv-matrix .riv-s, #riv-matrix .riv-c { cursor: pointer; }
#riv-matrix .riv-s.on { color: var(--accent); font-weight: 700; }
#riv-matrix td.riv-c.on { outline: 1px solid var(--accent); outline-offset: -1px; }
#riv-matrix td, #riv-cmp td { padding-right: 10px; }
#grid[data-layout="rivals"] tr.pas td:first-child { padding-left: 10px; }
#riv-matrix tr.us td { color: var(--muted); }
#riv-cmp tr.us-row td { background: color-mix(in srgb, var(--c5) 9%, transparent); }
#riv-cmp tr.us-row:first-of-type td, #riv-cmp tr:not(.us-row) + tr.us-row td { border-top: 2px solid var(--border); }
#riv-cmp td.riv-top { font-size: 11px; }
.riv-help { display: block; line-height: 1.5; font-size: 12px; }
.riv-help ul { margin: 4px 0; padding-left: 18px; }

/* our trades view (ticker.js) */
body[data-layout="trades"] .market-only, body[data-layout="trades"] #sig { display: none; }
#grid[data-layout="trades"] { margin-top: 10px; }
#grid[data-layout="trades"] .wide { grid-column: 1 / -1; }
#grid[data-layout="trades"] .half { grid-column: span 2; }
@media (max-width: 1200px) { #grid[data-layout="trades"] .half { grid-column: 1 / -1; } }
@media (max-width: 640px) { #grid[data-layout="trades"] .half, #grid[data-layout="trades"] .wide { grid-column: auto; } }
.tk-tbl.scroll { max-height: 560px; }
.tk-tbl td { padding-right: 12px; }

/* bots view (bots.js) */
body[data-layout="bots"] .market-only, body[data-layout="bots"] #sig { display: none; }
#grid[data-layout="bots"] { margin-top: 10px; }
#grid[data-layout="bots"] .wide { grid-column: 1 / -1; }
#grid[data-layout="bots"] .half { grid-column: span 2; }
@media (max-width: 1200px) { #grid[data-layout="bots"] .half { grid-column: 1 / -1; } }
@media (max-width: 640px) { #grid[data-layout="bots"] .half, #grid[data-layout="bots"] .wide { grid-column: auto; } }
#bot-metric { flex-wrap: wrap; }
#bot-matrix td.bot-c, #bot-matrix td.bot-name { cursor: pointer; padding-right: 10px; }
#bot-matrix td.bot-c.on { outline: 1px solid var(--accent); outline-offset: -1px; }
#bot-matrix tr.on td.bot-name { color: var(--accent); }
#bot-prof td { padding-right: 9px; white-space: nowrap; }
#bot-prof td .b { font-weight: 700; }
#bot-cps td.bot-cps { white-space: normal; line-height: 1.6; }
#bot-cps .bot-us { background: color-mix(in srgb, var(--c5) 18%, transparent); border-radius: 4px; padding: 0 3px; }
