:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11,11,11,0.10);
  --good: #0ca30c;
  --critical: #d03b3b;
  --warning: #fab219;
  --slot-1: #2a78d6;
  --slot-2: #eb6834;
  --slot-3: #1baf7a;
  --slot-4: #eda100;
  --slot-5: #e87ba4;
  --slot-6: #008300;
  --slot-1-tint: rgba(42,120,214,0.14);
  --critical-tint: rgba(208,59,59,0.12);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --baseline: #5a5a55;
    --border: rgba(255,255,255,0.10);
    --good: #0ca30c;
    --critical: #e66767;
    --warning: #fab219;
    --slot-1: #3987e5;
    --slot-2: #d95926;
    --slot-3: #199e70;
    --slot-4: #c98500;
    --slot-5: #d55181;
    --slot-6: #008300;
    --slot-1-tint: rgba(57,135,229,0.18);
    --critical-tint: rgba(230,103,103,0.16);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --baseline: #5a5a55;
  --border: rgba(255,255,255,0.10);
  --slot-1: #3987e5;
  --slot-2: #d95926;
  --slot-3: #199e70;
  --slot-4: #c98500;
  --slot-5: #d55181;
  --slot-6: #008300;
  --slot-1-tint: rgba(57,135,229,0.18);
  --critical-tint: rgba(230,103,103,0.16);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
.viz-root { max-width: 1180px; margin: 0 auto; padding: 24px 20px 60px; }

.topbar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 14px; font-weight: 600; color: var(--text-secondary); margin: 0 0 14px; }

.filters { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-secondary); }
.filters input[type=date] {
  font: inherit; color: var(--text-primary); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px;
}
.filters button {
  font: inherit; border: none; border-radius: 6px; padding: 6px 14px; cursor: pointer;
  background: var(--slot-1); color: white;
}
.filters button.ghost { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 24px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.tile-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.tile-value { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile-sub { font-size: 11px; color: var(--muted); margin-top: 3px; }

.tile-clickable { cursor: pointer; transition: border-color .15s, background-color .15s; }
.tile-clickable:hover { border-color: var(--slot-1); }
.tile-clickable.tile-active { border-color: var(--slot-1); background: var(--slot-1-tint); }

.chart-block { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; margin-bottom: 20px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.chart-wrap { position: relative; height: 220px; }
.chart-wrap.tall { height: 320px; }

.legend-note { width: 100%; font-size: 12px; color: var(--muted); margin-top: 10px; }
.legend-note td { padding: 0; }

.table-wrap { overflow-x: auto; }
#momentumShadowPanel { min-width: 0; font-size: 13px; }
#momentumShadowPanel summary { cursor: pointer; font-weight: 600; }
#momentumShadowReport p { color: var(--text-secondary); line-height: 1.5; overflow-wrap: anywhere; }
.momentum-shadow-table th, .momentum-shadow-table td {
  text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--grid);
  vertical-align: top; font-variant-numeric: tabular-nums; min-width: 105px;
}
.momentum-shadow-table th { color: var(--muted); font-weight: 500; }
.momentum-shadow-table td:first-child { max-width: 230px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
#recentTable th, #tradesTable th { text-align: left; color: var(--muted); font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
#recentTable td, #tradesTable td { padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; font-variant-numeric: tabular-nums; }
/* This column's text length varies a lot row to row, and these tables
   re-render periodically, so without a clamp every row's height (and
   everything below the table) would jitter. The clamp lives on an inner
   .clamp-2 span, not the <td> itself — overriding a table-cell's own
   `display` to -webkit-box breaks the browser's row-height calculation
   (a td that's no longer really a table-cell throws off its row's height),
   which was making things jitter worse, not better. */
#recentTable td.wrap, #tradesTable td.wrap, #symbolsTable td.wrap { white-space: normal; color: var(--text-secondary); }
#recentTable td.wrap, #tradesTable td.wrap { min-width: 220px; }
.clamp-2 {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; line-height: 1.4; min-height: calc(1.4em * 2);
}

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-primary); }
th.sortable .sort-arrow { display: inline-block; margin-left: 4px; opacity: 0.35; }
th.sortable.sort-active .sort-arrow { opacity: 1; color: var(--slot-1); }
.result-good { color: var(--good); font-weight: 600; }
.result-bad { color: var(--critical); font-weight: 600; }
.result-muted { color: var(--muted); }
.result-open { color: var(--slot-1); font-weight: 600; }
/* binance_testnet row whose R/$ has no real fill behind it (never-filled
   entry, execution skipped, or still resting) — overrides the good/bad
   coloring above so a phantom profit never reads as a real green result.
   Color itself is shared with .status-skipped below (same "didn't really
   happen" semantic) so there's one place to update the warning color, not two. */
.result-phantom { font-weight: 600; }

.close-trade-btn {
  font-size: 11px; color: var(--critical); background: none;
  border: 1px solid var(--critical); border-radius: 4px; cursor: pointer;
  padding: 3px 8px; white-space: nowrap;
}
.close-trade-btn:hover { background: var(--critical-tint); }
.close-trade-btn:disabled { opacity: 0.5; cursor: default; }

/* Filled (not outline) on purpose -- unlike .close-trade-btn (paper-only,
   reversible), this one places a real MARKET order on Binance Testnet, so
   it should read as more consequential at a glance. */
.close-real-btn {
  font-size: 11px; color: #fff; background: var(--critical);
  border: 1px solid var(--critical); border-radius: 4px; cursor: pointer;
  padding: 3px 8px; white-space: nowrap;
}
.close-real-btn:hover { opacity: 0.85; }
.close-real-btn:disabled { opacity: 0.5; cursor: default; }
.c-close { display: flex; flex-direction: column; gap: 4px; align-items: flex-start; }

/* --- glossary panel: collapsed accordion by default (title-attribute tooltips don't work on touch, but the block itself doesn't need to eat screen space until asked for) --- */
.glossary-grid { display: none; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px 18px; margin-top: 10px; }
.glossary-grid.open { display: grid; }
.glossary-item { font-size: 12px; line-height: 1.5; padding: 8px 0; border-top: 1px solid var(--grid); }
.glossary-item:nth-child(-n+2) { border-top: none; }
.glossary-item strong { display: block; color: var(--text-primary); font-size: 13px; margin-bottom: 2px; }
.glossary-item span { color: var(--text-secondary); }

/* --- nav / settings page -------------------------------------------- */
.nav-links { font-size: 13px; }
.nav-links a { color: var(--slot-1); text-decoration: none; }
.nav-links a:hover { text-decoration: underline; }

.banner {
  border-radius: 8px; padding: 10px 14px; font-size: 13px; margin-bottom: 18px;
  background: var(--critical-tint); color: var(--critical); border: 1px solid var(--border);
}
.banner.ok { background: rgba(12,163,12,0.12); color: var(--good); }

.group-block { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; margin-bottom: 16px; }
.group-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.group-header h2 { margin: 0; font-size: 14px; color: var(--text-primary); font-weight: 600; }
.test-btn {
  font: inherit; font-size: 12px; border: 1px solid var(--border); background: transparent;
  color: var(--text-secondary); border-radius: 6px; padding: 5px 10px; cursor: pointer;
}
.test-btn:hover { background: var(--page); }
.test-result { font-size: 12px; margin-left: 8px; }
.test-result.ok { color: var(--good); }
.test-result.err { color: var(--critical); }

.field-row { display: grid; grid-template-columns: 260px 1fr; gap: 14px; align-items: start; padding: 8px 0; border-top: 1px solid var(--grid); }
.field-row:first-child { border-top: none; }
.field-label { font-size: 13px; color: var(--text-primary); font-weight: 500; }
.field-help { font-size: 11px; color: var(--muted); margin-top: 3px; max-width: 420px; }
.field-input-wrap { display: flex; align-items: center; gap: 8px; }
.field-input-wrap input[type=text], .field-input-wrap input[type=password], .field-input-wrap input[type=number] {
  font: inherit; font-size: 13px; color: var(--text-primary); background: var(--page);
  border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; width: 100%; max-width: 340px;
  font-variant-numeric: tabular-nums;
}
.field-input-wrap input:focus { outline: 2px solid var(--slot-1); outline-offset: 1px; }
.toggle-secret { font-size: 11px; color: var(--muted); background: none; border: none; cursor: pointer; padding: 4px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; }
.checkbox-row input { width: 16px; height: 16px; }
.secret-hint { font-size: 11px; color: var(--muted); }

.save-bar { position: sticky; bottom: 0; background: var(--page); padding: 16px 0 4px; display: flex; align-items: center; gap: 12px; }
.save-bar button {
  font: inherit; border: none; border-radius: 8px; padding: 10px 22px; cursor: pointer;
  background: var(--slot-1); color: white; font-weight: 600;
}
.save-status { font-size: 13px; color: var(--good); }

/* --- chain / health widget ------------------------------------------- */
.chain-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: stretch; }
.chain-step {
  flex: 1 1 100px; min-width: 100px; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; font-size: 11px; color: var(--text-secondary); position: relative;
}
.chain-step .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px; }
.dot-ok { background: var(--good); }
.dot-error { background: var(--critical); }
.dot-skipped { background: var(--warning); }
.dot-idle { background: var(--baseline); }
.dot-never { background: var(--baseline); }
.chain-step .step-name { font-weight: 600; color: var(--text-primary); }
.chain-meta { font-size: 12px; color: var(--muted); margin-top: 10px; display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
.run-now-btn {
  font: inherit; font-size: 12px; border: 1px solid var(--border); background: var(--slot-1);
  color: white; border-radius: 6px; padding: 6px 12px; cursor: pointer;
}

/* Clamped to a fixed 2-line height for the same reason as #symbolsTable
   td.wrap above — this banner's text changes on every ~15s health refresh,
   and without a reserved height the page jumps as it grows/shrinks. */
.run-result-banner {
  margin: 10px 0; padding: 10px 14px; border-radius: 8px; font-size: 13px; font-weight: 600;
  line-height: 1.4; min-height: calc(1.4em * 2 + 20px);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; box-sizing: border-box;
}
.run-result-banner.banner-neutral { background: var(--page); color: var(--text-secondary); border: 1px solid var(--border); }
.run-result-banner.banner-good { background: rgba(12,163,12,0.12); color: var(--good); }
.run-result-banner.banner-bad { background: var(--critical-tint); color: var(--critical); }
.run-result-banner.banner-info { background: var(--slot-1-tint); color: var(--slot-1); }

/* --- logs panel -------------------------------------------------------- */
.logs-toggle-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.logs-toggle-btn {
  font: inherit; font-size: 13px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text-primary); border-radius: 8px; padding: 8px 14px; cursor: pointer;
}
.public-log-link { font-size: 12px; color: var(--slot-1); text-decoration: none; }
.public-log-link:hover { text-decoration: underline; }
#logsPanel { display: none; }
#logsPanel.open { display: block; }
.run-row { cursor: pointer; }
.run-row:hover td { background: var(--page); }
.stage-detail { font-size: 12px; color: var(--text-secondary); padding: 6px 10px 12px 30px; }
.stage-detail .stage-line { display: flex; gap: 8px; padding: 2px 0; }
.status-ok { color: var(--good); }
.status-error { color: var(--critical); }
.status-skipped, .result-phantom { color: var(--warning); }
.status-running { color: var(--slot-1); }

/* --- trades filter bar -------------------------------------------------- */
.filter-block { padding: 16px 20px; }
.filter-grid { display: flex; flex-wrap: wrap; align-items: end; gap: 12px 16px; font-size: 12px; color: var(--text-secondary); }
.filter-grid label { display: flex; flex-direction: column; gap: 4px; }
.filter-grid select, .filter-grid input[type=number] {
  font: inherit; font-size: 13px; color: var(--text-primary); background: var(--page);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px;
}
.filter-grid button {
  font: inherit; border: none; border-radius: 6px; padding: 7px 14px; cursor: pointer;
  background: var(--slot-1); color: white; align-self: end; height: 32px;
}
.filter-grid button.ghost { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
.row-expandable { cursor: pointer; }
.row-expandable:hover td { background: var(--page); }
.trade-detail { font-size: 12px; color: var(--text-secondary); padding: 10px 10px 14px 30px; line-height: 1.7; }
.trade-detail b { color: var(--text-primary); }
