:root {
  color-scheme: dark;
  --bg: #0f1114;
  --surface: #171a1f;
  --surface-2: #1f232a;
  --border: #2b3039;
  --text: #eceef1;
  --text-2: #a8aeb8;
  --muted: #737a86;
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, .16);
  --up: #2fb344;
  --down: #e66767;
  --good: #0ca30c;
  --critical: #d03b3b;
  --warning: #fab219;
  --itm: rgba(250, 178, 25, .07);
  --atm: rgba(57, 135, 229, .18);
  --oi-bar: rgba(57, 135, 229, .22);
  --profit-fill: rgba(12, 163, 12, .20);
  --loss-fill: rgba(208, 59, 59, .20);
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Consolas, "Roboto Mono", monospace;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --border: #dde1e6;
    --text: #111418;
    --text-2: #4b525c;
    --muted: #767d88;
    --accent: #2a78d6;
    --accent-soft: rgba(42, 120, 214, .12);
    --up: #0f7a24;
    --down: #b42323;
    --itm: rgba(237, 161, 0, .09);
    --atm: rgba(42, 120, 214, .14);
    --oi-bar: rgba(42, 120, 214, .16);
    --profit-fill: rgba(12, 163, 12, .16);
    --loss-fill: rgba(208, 59, 59, .14);
  }
}
/* Theme picker: data-theme="dark" keeps the base (dark) tokens above; "light" forces the light set
   even when the OS is dark. No attribute = follow the OS. */
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e6;
  --text: #111418;
  --text-2: #4b525c;
  --muted: #767d88;
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, .12);
  --up: #0f7a24;
  --down: #b42323;
  --itm: rgba(237, 161, 0, .09);
  --atm: rgba(42, 120, 214, .14);
  --oi-bar: rgba(42, 120, 214, .16);
  --profit-fill: rgba(12, 163, 12, .16);
  --loss-fill: rgba(208, 59, 59, .14);
}

* { box-sizing: border-box; }
.theme-btn { display: inline-flex; align-items: center; gap: 6px; }
.theme-btn svg { width: 14px; height: 14px; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); margin: 0 0 10px; }
button, select, input { font: inherit; color: inherit; }
.num, td.n, th.n { font-family: var(--mono); font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.up { color: var(--up); }
.down { color: var(--down); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.brand { font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.ticker { display: flex; gap: 18px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.tick { display: flex; gap: 8px; align-items: baseline; white-space: nowrap; font-size: 13px; }
.tick b { font-weight: 600; color: var(--text-2); }
.session-pill { display: flex; align-items: center; gap: 8px; padding: 5px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; white-space: nowrap; font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--good); }
.dot.bad { background: var(--critical); }
.dot.wait { background: var(--warning); }
#liveBadge { cursor: default; }

/* streamed price change: brief tint, then back to the cell's own background */
@keyframes flashUp { from { background-color: rgba(47, 179, 68, .38); } to { background-color: transparent; } }
@keyframes flashDown { from { background-color: rgba(230, 103, 103, .38); } to { background-color: transparent; } }
.flash-up { animation: flashUp .8s ease-out; }
.flash-down { animation: flashDown .8s ease-out; }
@media (prefers-reduced-motion: reduce) { .flash-up, .flash-down { animation: none; } }

/* tabs */
.tabs { display: flex; gap: 2px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; align-items: center; }
.tabs a { padding: 10px 14px; color: var(--text-2); text-decoration: none; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a:hover { color: var(--text); }
.tabs a.on { color: var(--text); border-bottom-color: var(--accent); }
.tabs-spacer { flex: 1; }
.updated { color: var(--muted); font-size: 12px; white-space: nowrap; }

main { padding: 16px 20px 40px; max-width: 1600px; margin: 0 auto; }
.grid.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; margin-bottom: 12px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.grid.cards .card { margin-bottom: 0; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; }
.big { font-size: 22px; font-weight: 600; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.status-line { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 8px; }
.notice { background: var(--accent-soft); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; color: var(--text-2); }
.error-box { border: 1px solid var(--critical); border-radius: var(--radius); padding: 10px 14px; color: var(--text); background: rgba(208, 59, 59, .1); margin-bottom: 12px; }

.btn { border: 1px solid var(--border); background: var(--surface-2); padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger:hover { border-color: var(--critical); color: var(--down); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.sm { padding: 2px 8px; font-size: 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 3px 10px; font-size: 12px; white-space: nowrap; }
.chip b { font-family: var(--mono); font-weight: 600; margin-left: 4px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; letter-spacing: .03em; border: 1px solid var(--border); white-space: nowrap; }
.badge.buy { color: var(--up); border-color: currentColor; }
.badge.sell { color: var(--down); border-color: currentColor; }
.badge.ok { color: var(--up); }
.badge.bad { color: var(--down); }
.badge.wait { color: var(--warning); }

/* tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
td { padding: 7px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody tr:hover td { background: var(--surface-2); }
td.empty { text-align: center; color: var(--muted); padding: 28px; white-space: normal; }

/* toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; margin-bottom: 12px; }
.toolbar label { display: flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 13px; }
.toolbar select, .basket input, .basket select { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; }
.toolbar .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 4px 12px; cursor: pointer; color: var(--text-2); }
.seg button.on { background: var(--accent-soft); color: var(--text); }

/* option chain */
.chain-layout { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 12px; align-items: start; }
.chain-wrap { max-height: calc(100vh - 200px); position: relative; }
table.chain th, table.chain td { padding: 5px 8px; text-align: right; }
table.chain th.side { text-align: center; color: var(--text-2); }
table.chain td.strike, table.chain th.strike { text-align: center; font-weight: 700; background: var(--surface-2); font-family: var(--mono); }
table.chain tr.atm td { background: var(--atm); }
table.chain tr.atm td.strike { background: var(--accent); color: #fff; }
table.chain td.itm { background: var(--itm); }
table.chain td.oi { background-image: linear-gradient(var(--oi-bar), var(--oi-bar)); background-repeat: no-repeat; background-size: var(--w, 0%) 100%; }
table.chain td.oi.ce { background-position: right; }
table.chain td.oi.pe { background-position: left; }
table.chain td.act { padding: 3px 4px; }
.bs { display: inline-flex; gap: 3px; }
.bs button { width: 22px; height: 20px; padding: 0; border-radius: 4px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; font-size: 11px; font-weight: 700; opacity: .35; }
tr:hover .bs button, .bs button:focus-visible { opacity: 1; }
.bs button.b { color: var(--up); }
.bs button.s { color: var(--down); }
.ltp { font-weight: 600; }

/* basket */
.basket { position: sticky; top: 100px; }
.basket .legs { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.leg { display: grid; grid-template-columns: auto 1fr auto auto auto; gap: 6px; align-items: center; font-size: 12px; }
.leg input { width: 64px; padding: 3px 6px !important; text-align: right; }
.leg .x { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; }
.templates { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.basket .row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.basket .row input { flex: 1; }

/* strategy cards */
.strat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; }
.strat-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.strat-head .title { font-weight: 600; font-size: 15px; }
.strat-head .spacer { flex: 1; }
.strat-body { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 12px; padding: 12px 16px; }
.stat { display: flex; flex-direction: column; }
.stat > span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* payoff chart */
.payoff { position: relative; }
.payoff svg { display: block; width: 100%; height: auto; overflow: visible; }
.payoff .axis text { fill: var(--muted); font-size: 11px; font-family: var(--mono); }
.payoff .grid line { stroke: var(--border); stroke-width: 1; }
.payoff .zero { stroke: var(--text-2); stroke-width: 1; }
.payoff .line { fill: none; stroke: var(--text); stroke-width: 2; stroke-linejoin: round; }
.payoff .area-profit { fill: var(--profit-fill); }
.payoff .area-loss { fill: var(--loss-fill); }
.payoff .spot { stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 4 3; }
.payoff .spot-label { fill: var(--accent); font-size: 11px; font-weight: 600; }
.payoff .be { fill: var(--text); stroke: var(--surface); stroke-width: 2; }
.payoff .be-label { fill: var(--text-2); font-size: 11px; font-family: var(--mono); }
.payoff .cross { stroke: var(--text-2); stroke-width: 1; }
.payoff .cross-dot { fill: var(--text); stroke: var(--surface); stroke-width: 2; }
.payoff .tip { position: absolute; pointer-events: none; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; font-size: 12px; white-space: nowrap; box-shadow: 0 4px 14px rgba(0,0,0,.25); }
.payoff .tip b { font-family: var(--mono); }
.payoff-caption { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--text-2); margin-top: 6px; }
.payoff-caption b { font-family: var(--mono); color: var(--text); }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: -1px; margin-right: 4px; }

/* automated strategies + notes */
.section-title { margin: 18px 0 10px; }
.auto-card .card-head { flex-wrap: wrap; }
.auto-card .auto-name { font-size: 16px; font-weight: 700; text-transform: none; letter-spacing: 0; color: var(--text); margin: 0; }
.auto-card .auto-sub { color: var(--text-2); margin: 2px 0 10px; }
.auto-card .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.auto-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px 28px; margin: 12px 0; }
.auto-grid h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }
.auto-grid .kv dd { text-align: left; }
details.fold { border-top: 1px solid var(--border); padding: 8px 0 0; margin-top: 10px; }
details.fold > summary { cursor: pointer; color: var(--text-2); font-weight: 600; font-size: 13px; }
details.fold > summary:hover { color: var(--text); }
pre.note { white-space: pre-wrap; font: 13px/1.55 var(--font); color: var(--text-2); margin: 10px 0 4px; max-width: 900px; }
.events { list-style: none; padding: 0; margin: 8px 0 0; font-size: 12px; }
.events li { padding: 4px 0; border-bottom: 1px dashed var(--border); display: grid; grid-template-columns: 130px 1fr; gap: 10px; }
.events li .t { color: var(--muted); font-family: var(--mono); }
.events li.warn .m { color: var(--warning); }
.events li.error .m { color: var(--down); }
.preview-box { margin-top: 12px; padding: 12px; border: 1px dashed var(--accent); border-radius: var(--radius); background: var(--accent-soft); }
.strat .note-fold { padding: 0 16px 12px; }

/* live (real money) */
.btn.live { background: var(--critical); border-color: var(--critical); color: #fff; font-weight: 600; }
.btn.live:hover { filter: brightness(1.1); border-color: var(--critical); }
.btn.live.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px rgba(208, 59, 59, .35); } }
@media (prefers-reduced-motion: reduce) { .btn.live.pulse { animation: none; } }
.badge.live { color: #fff; background: var(--critical); border-color: var(--critical); }
.session-pill.live-orders { background: var(--critical); border-color: var(--critical); color: #fff; font-weight: 700; font-size: 11px; letter-spacing: .04em; }
.live-dlg { width: min(760px, calc(100vw - 32px)); max-height: calc(100vh - 40px); border: 1px solid var(--critical); border-radius: 10px;
  background: var(--surface); color: var(--text); padding: 0; }
.live-dlg::backdrop { background: rgba(0, 0, 0, .6); }
.live-dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.live-dlg-head h2 { margin: 0; color: var(--text); font-size: 14px; }
.live-dlg-head .x { border: 0; background: none; color: var(--muted); font-size: 22px; cursor: pointer; }
#liveDlgBody { padding: 14px 16px 16px; overflow: auto; max-height: calc(100vh - 110px); }
.warn-box { border: 1px solid var(--critical); background: rgba(208, 59, 59, .12); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }
.warn-box.amber { border-color: var(--warning); background: rgba(250, 178, 25, .1); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.leg-state { font-weight: 600; font-size: 11px; letter-spacing: .03em; }
.leg-state.FILLED { color: var(--up); }
.leg-state.FAILED, .leg-state.PARTIAL { color: var(--down); }
.leg-state.WORKING { color: var(--warning); }
.leg-state.QUEUED, .leg-state.NOT { color: var(--muted); }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--surface-2); border: 1px solid var(--border); padding: 9px 16px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.3); z-index: 10; }
.toast.err { border-color: var(--critical); }

@media (max-width: 1100px) {
  .chain-layout { grid-template-columns: 1fr; }
  .basket { position: static; }
  .strat-body { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 8px 16px; }
  .ticker { order: 3; flex-basis: 100%; }
  .tabs, main { padding-left: 16px; padding-right: 16px; }
  .updated { display: none; }
}
