/* Colors are tokens; <html data-theme="dark|light"> picks the set (app.js resolves "auto"). */
:root, :root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1216; --panel: #171b21; --field: #0c0f13; --line: #262c35; --line-soft: #1d2229;
  --text: #e6e9ee; --muted: #8b94a3; --strong: #ffffff;
  --up: #2fbf71; --down: #ef5a5a; --accent: #3b82f6; --on-accent: #ffffff;
  --buy: #1d4ed8; --sell: #b91c1c;
  --bid-bg: rgba(59, 130, 246, .14); --ask-bg: rgba(239, 90, 90, .14);
  --bid-zone: rgba(59, 130, 246, .07); --ask-zone: rgba(239, 90, 90, .07);
  --bid-bar: rgba(59, 130, 246, .38); --ask-bar: rgba(239, 90, 90, .38);
  --bid-solid: #1d4ed8; --ask-solid: #b91c1c;
  --bid-text: #9cc2ff; --ask-text: #ffb0b0;
  --price-bg: #12161b; --last-bg: rgba(250, 204, 21, .22); --last-line: #facc15; --avg-mark: #a78bfa;
  --test-bg: #3a3215; --test-text: #facc15; --live-bg: #3a1515; --live-text: #ff7676;
  --backdrop: rgba(0, 0, 0, .6);
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6f9; --panel: #ffffff; --field: #ffffff; --line: #d8dde5; --line-soft: #e8ebf0;
  --text: #16191d; --muted: #5d6675; --strong: #000000;
  --up: #0f8a4a; --down: #d12f2f; --accent: #2563eb; --on-accent: #ffffff;
  --buy: #2563eb; --sell: #dc2626;
  --bid-bg: rgba(37, 99, 235, .12); --ask-bg: rgba(220, 38, 38, .11);
  --bid-zone: rgba(37, 99, 235, .06); --ask-zone: rgba(220, 38, 38, .06);
  --bid-bar: rgba(37, 99, 235, .28); --ask-bar: rgba(220, 38, 38, .26);
  --bid-solid: #2563eb; --ask-solid: #dc2626;
  --bid-text: #1d4ed8; --ask-text: #b91c1c;
  --price-bg: #f1f3f6; --last-bg: rgba(234, 179, 8, .35); --last-line: #ca8a04; --avg-mark: #7c3aed;
  --test-bg: #fef3c7; --test-text: #92400e; --live-bg: #fee2e2; --live-text: #b91c1c;
  --backdrop: rgba(0, 0, 0, .35);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
header { display: flex; align-items: center; gap: 14px; padding: 10px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; background: var(--panel); }
header h1 { font-size: 18px; margin: 0; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav a { color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 6px; }
nav a.active { color: var(--text); background: var(--bg); }
#conn { flex: 1; text-align: right; }
.badge { font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; }
.badge.test { background: var(--test-bg); color: var(--test-text); }
.badge.live { background: var(--live-bg); color: var(--live-text); }
main { padding: 16px 0; width: 90%; margin: 0 auto; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 18px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px; }
.narrow { max-width: 420px; margin: 40px auto; display: flex; flex-direction: column; gap: 10px; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
@media (max-width: 800px) { .grid { grid-template-columns: 1fr; } }
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.tile .k { color: var(--muted); font-size: 12px; }
.tile .v { font-size: 20px; font-variant-numeric: tabular-nums; }
.scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 8px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th:first-child, td:first-child, th.l, td.l { text-align: left; }
th { color: var(--muted); font-weight: 500; font-size: 12px; }
#quotes td:first-child, td .sym-link { cursor: pointer; color: var(--accent); }
.up { color: var(--up); } .down { color: var(--down); }
input, select, button, .button { font: inherit; color: var(--text); background: var(--field); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; }
button, .button { background: var(--accent); border-color: var(--accent); color: var(--on-accent); cursor: pointer; text-decoration: none; text-align: center; }
button:disabled { opacity: .5; cursor: default; }
.ghost { background: transparent; border-color: var(--line); color: var(--text); }
.small { font-size: 12px; } button.small { padding: 3px 8px; }
.muted { color: var(--muted); }
.error { color: var(--down); min-height: 1em; }
#error:empty { display: none; }
.ok { color: var(--up); }
code { word-break: break-all; }
.x { background: none; border: none; color: var(--muted); padding: 0 4px; }

/* Segmented buttons */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { background: transparent; border: 0; border-radius: 0; color: var(--muted); padding: 6px 12px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--on-accent); }

/* Ladder */
/* Ladder page: [trade panel] [ladder] [bot] on wide screens; below 1400px the
   left column switches between Trade and Bot with tabs. The page fills the window
   height (app.js sets --ladder-h); panels scroll on their own if taller. */
.ladder-wrap { display: grid; grid-template-columns: 270px 1fr 300px; grid-template-rows: minmax(0, 1fr); grid-template-areas: "side ladder bot"; gap: 16px; height: var(--ladder-h, auto); }
.ladder-wrap > .card { min-height: 0; }
.ladder-side { grid-area: side; }
.ladder-wrap > .ladder { grid-area: ladder; }
.bot-card { grid-area: bot; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; }
.panel-tabs { display: none; width: 100%; }
.panel-tabs button { flex: 1; }
@media (max-width: 1399px) {
  .ladder-wrap { grid-template-columns: 270px 1fr; grid-template-areas: "side ladder"; }
  .bot-card { grid-area: side; }
  .panel-tabs { display: inline-flex; }
  .ladder-wrap[data-panel="trade"] .bot-card, .ladder-wrap[data-panel="bot"] .ladder-side { display: none; }
}
@media (max-width: 800px) {
  .ladder-wrap { grid-template-columns: 1fr; grid-template-rows: auto; grid-template-areas: "side" "ladder"; height: auto; }
}
.bot-tab-info { font-size: 11px; }
.bot-tab-info .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--up); margin-right: 3px; vertical-align: middle; }
.ladder { display: flex; flex-direction: column; }
.ladder #ladder { flex: 0 0 auto; }
.ladder .ladder-foot { margin-top: auto; }
.zoom { display: inline-flex; align-items: center; gap: 4px; }
.zoom button { min-width: 26px; }
.ladder-side { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
@media (min-width: 801px) { .ladder-side > * { flex-shrink: 0; } }
.ladder-side .row { margin: 0; }
#ladder-symbol { width: 120px; text-transform: uppercase; }
.quote-box .big { font-size: 28px; font-variant-numeric: tabular-nums; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.field input, .field select { color: var(--text); }
.qty-buttons { display: flex; gap: 4px; flex-wrap: wrap; }
.toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.btn-grid .wide { grid-column: 1 / -1; font-size: 12px; padding: 5px 8px; }
button.buy { background: var(--buy); border-color: var(--buy); }
button.sell { background: var(--sell); border-color: var(--sell); }
.pos-box { border-top: 1px solid var(--line); padding-top: 8px; display: flex; flex-direction: column; gap: 2px; }
.kv { display: flex; justify-content: space-between; gap: 8px; font-variant-numeric: tabular-nums; }
.bot-head { display: flex; align-items: center; gap: 6px; }
.bot-head .spacer { flex: 1; }
.bot-head button { padding: 4px 12px; }
.info { cursor: help; display: inline-flex; align-items: center; justify-content: center; width: 15px; height: 15px; border-radius: 50%; border: 1px solid var(--muted); color: var(--muted); font-size: 10px; font-weight: 700; font-style: normal; line-height: 1; }
.bot-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.bot-grid .field { font-size: 11px; }
.bot-grid input { width: 100%; padding: 5px 6px; }
button.go { background: var(--up); border-color: var(--up); }
.badge.off { background: var(--line); color: var(--muted); }
.badge.run { background: var(--up); color: #fff; }
.bot-log-head { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); padding-top: 6px; }
.bot-log { margin: 0; padding: 0 0 0 16px; max-height: 110px; overflow-y: auto; color: var(--muted); }
.bot-log.more { max-height: none; }
.bot-log li { margin: 1px 0; }
#bot-status div { display: flex; justify-content: space-between; }
.chip.bot { outline: 2px solid var(--test-text); outline-offset: -1px; }
.ladder { padding: 0; overflow: hidden; }
#ladder { user-select: none; table-layout: fixed; }
#ladder th, #ladder td { text-align: center; padding: 2px 6px; height: 22px; border-bottom: 1px solid var(--line-soft); }
#ladder td.price { font-weight: 600; background: var(--price-bg); }
#ladder td.bid, #ladder td.ask { cursor: pointer; font-variant-numeric: tabular-nums; }
/* ToS-style zones: bid column shaded at/below the bid, ask column at/above the ask. */
#ladder td.bid.zone { background-color: var(--bid-zone); }
#ladder td.ask.zone { background-color: var(--ask-zone); }
/* Size bars grow from the price column outward, scaled by --w (0..1). */
#ladder td.bid.has { color: var(--bid-text); font-weight: 600;
  background-image: linear-gradient(to left, var(--bid-bar) calc(var(--w) * 100%), transparent 0); }
#ladder td.ask.has { color: var(--ask-text); font-weight: 600;
  background-image: linear-gradient(to right, var(--ask-bar) calc(var(--w) * 100%), transparent 0); }
#ladder td.bid:hover, #ladder td.ask:hover { outline: 1px solid var(--muted); outline-offset: -1px; }
#ladder tr.at-bid td.price { background: var(--bid-solid); color: #fff; }
#ladder tr.at-ask td.price { background: var(--ask-solid); color: #fff; }
#ladder tr.at-bid td.bid, #ladder tr.at-ask td.ask { font-size: 15px; }
#ladder tr.at-last td.price { box-shadow: inset 0 0 0 2px var(--last-line); }
#ladder tr.at-last:not(.at-bid):not(.at-ask) td.price { background: var(--last-bg); color: var(--strong); }
.spread-bar .sb-sep { width: 1px; align-self: stretch; background: var(--line); }
.spread-bar { flex-wrap: wrap; display: flex; gap: 14px; justify-content: center; padding: 6px 10px; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.spread-bar .b { color: var(--bid-text); font-weight: 600; } .spread-bar .a { color: var(--ask-text); font-weight: 600; }
#ladder tr.at-avg td.price { box-shadow: inset 3px 0 0 var(--avg-mark); }
#ladder td.ord { font-size: 12px; }
.chip { display: inline-flex; align-items: center; gap: 2px; padding: 0 6px; border-radius: 4px; cursor: pointer; color: #fff; }
.chip.buy { background: var(--buy); } .chip.sell { background: var(--sell); }
.chip.test { opacity: .55; }
.chip.pending { opacity: .5; outline: 1px dashed currentColor; outline-offset: -2px; }
.ladder-foot { padding: 6px 10px; margin: 0; }
.ladder-foot select { padding: 2px 6px; margin-left: 4px; }

/* History */
.history-controls { margin-bottom: 16px; }
.nav-arrows { display: inline-flex; align-items: center; gap: 8px; }
#hist-label { min-width: 140px; text-align: center; }
#hist-symbol { width: 150px; text-transform: uppercase; }
#hist-symbol::placeholder, #ladder-symbol::placeholder { text-transform: none; }
#hist-totals .tile .v { font-size: 17px; }
#hist-table th[data-sort] { cursor: pointer; }
#hist-table th[data-sort].sorted::after { content: ' ▾'; }
#hist-table th[data-sort].sorted.asc::after { content: ' ▴'; }
.hist-more { justify-content: center; margin: 10px 0 0; }
.status-FILLED { color: var(--up); } .status-REJECTED, .status-CANCELED, .status-EXPIRED { color: var(--muted); }

/* Stats */
#stats-view h3 { margin-top: 18px; }
.dist { display: grid; grid-template-columns: 60px 1fr 50px; gap: 4px 8px; align-items: center; font-size: 12px; font-variant-numeric: tabular-nums; }
.dist .bar { height: 10px; border-radius: 3px; background: var(--accent); min-width: 2px; }
.dist.sell .bar { background: var(--sell); }
.bot-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 18px; }
@media (max-width: 800px) { .bot-stats { grid-template-columns: 1fr; } }

/* Settings */
.settings { max-width: 640px; margin: 0 auto; display: flex; flex-direction: column; gap: 8px; }
.settings .field input, .settings .field select { max-width: 260px; }
.big-toggle { font-size: 16px; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 18px; max-width: 420px; width: calc(100% - 32px); }
dialog::backdrop { background: var(--backdrop); }
.big-text { font-size: 18px; font-weight: 600; }
