:root {
  --font-sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, monospace;
  --bg: #0b0e13;
  --surface: #11151c;
  --surface-2: #171c25;
  --surface-3: #1e2430;
  --border: #252c38;
  --grid: #1a2029;
  --grid-strong: #3a4352;
  --text: #e6e9ef;
  --text-muted: #8a93a3;
  --accent: #14b88a;
  --accent-ink: #04140f;
  --up: #22c38e;
  --down: #f0596a;
  --up-soft: rgba(34, 195, 142, 0.35);
  --down-soft: rgba(240, 89, 106, 0.35);
  --warn: #f2b53c;
  --c1: #4c8dff;
  --c2: #f2a33c;
  --c3: #b07cff;
  --c4: #2ec7d6;
  --c5: #e86fb8;
  --radius: 8px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f3f5f8; --surface: #ffffff; --surface-2: #f6f8fb; --surface-3: #eceff4;
    --border: #dde2ea; --grid: #eef1f5; --grid-strong: #c4cbd6;
    --text: #141a23; --text-muted: #5f6878; --accent: #0f9d76; --accent-ink: #ffffff;
    --up: #0f9d76; --down: #d93f52; --up-soft: rgba(15, 157, 118, 0.3); --down-soft: rgba(217, 63, 82, 0.3);
    color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f3f5f8; --surface: #ffffff; --surface-2: #f6f8fb; --surface-3: #eceff4;
  --border: #dde2ea; --grid: #eef1f5; --grid-strong: #c4cbd6;
  --text: #141a23; --text-muted: #5f6878; --accent: #0f9d76; --accent-ink: #ffffff;
  --up: #0f9d76; --down: #d93f52; --up-soft: rgba(15, 157, 118, 0.3); --down-soft: rgba(217, 63, 82, 0.3);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 13px/1.4 var(--font-sans); -webkit-font-smoothing: antialiased;
  display: flex; flex-direction: column; overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
.up { color: var(--up); }
.down { color: var(--down); }
.muted { color: var(--text-muted); }
.num { font-variant-numeric: tabular-nums; }
.spacer { flex: 1; }
[hidden] { display: none !important; }

/* Buttons & inputs */
.btn {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 6px;
  padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-3); }
.btn.icon { padding: 6px 9px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.buy { background: var(--up); border-color: var(--up); color: #fff; font-weight: 600; }
.btn.sell { background: var(--down); border-color: var(--down); color: #fff; font-weight: 600; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input, select, textarea {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-muted); }
label.field > input, label.field > select { color: var(--text); }
label.check { display: flex; align-items: center; gap: 6px; font-size: 12px; cursor: pointer; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px; height: 50px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 15px; margin-right: 8px; }
.symbol-btn {
  display: flex; align-items: baseline; gap: 8px; border: 1px solid var(--border); background: var(--surface-2);
  border-radius: 6px; padding: 6px 12px; cursor: pointer; font-weight: 700; font-size: 14px;
}
.symbol-btn:hover { border-color: var(--accent); }
.symbol-market { font-size: 11px; font-weight: 500; color: var(--text-muted); }
.quote { display: flex; gap: 8px; align-items: baseline; font-variant-numeric: tabular-nums; min-width: 150px; }
.quote b { font-size: 15px; }
.tf-group { display: flex; gap: 2px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 2px; }
.tf-group button { border: 0; background: transparent; padding: 4px 8px; border-radius: 4px; cursor: pointer; color: var(--text-muted); }
.tf-group button.active { background: var(--surface-3); color: var(--text); font-weight: 600; }
.tf-group button:disabled { opacity: .35; cursor: not-allowed; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 20; min-width: 220px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px;
  box-shadow: 0 8px 30px rgba(0,0,0,.35);
}
.menu label { display: flex; gap: 8px; padding: 6px 8px; border-radius: 4px; cursor: pointer; }
.menu label:hover { background: var(--surface-2); }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 260px minmax(0, 1fr) 280px; min-height: 0; }
.watchlist, .ticket { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; min-height: 0; }
.ticket { border-right: 0; border-left: 1px solid var(--border); padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.center { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chart-area { flex: 1; display: flex; min-height: 220px; position: relative; background: var(--surface); }
.chart-col { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }

/* Drawing tools */
.draw-bar { width: 44px; flex-shrink: 0; display: flex; flex-direction: column; align-items: center; gap: 1px; padding: 6px 0; border-right: 1px solid var(--border); overflow-y: auto; background: var(--surface); }
.draw-btn { width: 32px; height: 27px; display: grid; place-items: center; border: 0; border-radius: 7px; background: transparent; color: var(--text-muted); cursor: pointer; flex-shrink: 0; }
.draw-btn:hover { background: var(--surface-3); color: var(--text); }
.draw-btn.on { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.draw-sep { width: 24px; border: 0; border-top: 1px solid var(--border); margin: 3px 0; flex-shrink: 0; }
.draw-layer { position: absolute; left: 0; top: 0; z-index: 2; pointer-events: none; overflow: hidden; }
.draw-layer.placing { pointer-events: all; cursor: crosshair; }
.draw-layer .hit { stroke: transparent; stroke-width: 12; pointer-events: stroke; cursor: move; }
.draw-layer .hit-fill { fill: transparent; pointer-events: all; cursor: move; }
.draw-layer .handle { pointer-events: all; cursor: grab; }
.draw-layer .draw-text { pointer-events: all; cursor: move; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; }
.draw-layer .draw-label { pointer-events: none; paint-order: stroke; stroke: var(--surface); stroke-width: 3px; font-variant-numeric: tabular-nums; }
.draw-layer.locked * { pointer-events: none !important; }
.draw-layer.placing .drawing * { pointer-events: none; }
.chart-main { flex: 1; min-height: 0; position: relative; }
.chart-pane { height: 130px; border-top: 1px solid var(--border); flex-shrink: 0; }
.chart-legend {
  position: absolute; top: 8px; left: 10px; z-index: 3; display: flex; gap: 10px; font-size: 12px;
  font-variant-numeric: tabular-nums; pointer-events: none; color: var(--text-muted);
}
.chart-status {
  position: absolute; inset: 0; z-index: 5; display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 24px; pointer-events: none;
}
.chart-status:empty { display: none; }
.chart-status .card {
  pointer-events: auto; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px 22px; max-width: 420px;
}
.chart-status .card h3 { margin: 0 0 6px; font-size: 15px; }
.chart-status .card p { margin: 0 0 12px; color: var(--text-muted); }

/* Watchlist */
.wl-tabs { display: flex; position: sticky; top: 0; background: var(--surface); z-index: 2; border-bottom: 1px solid var(--border); }
.wl-tabs button { flex: 1; border: 0; background: transparent; padding: 10px 4px; cursor: pointer; color: var(--text-muted); border-bottom: 2px solid transparent; font-weight: 600; }
.wl-tabs button.active { color: var(--text); border-bottom-color: var(--accent); }
.wl-note { padding: 6px 12px; font-size: 11px; color: var(--text-muted); display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.wl-row {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 7px 12px; cursor: pointer;
  border-left: 2px solid transparent; position: relative;
}
.wl-row:hover { background: var(--surface-2); }
.wl-row.active { background: var(--surface-2); border-left-color: var(--accent); }
.wl-row .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wl-row .n { color: var(--text-muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wl-row .p { text-align: right; font-variant-numeric: tabular-nums; }
.wl-row .c { text-align: right; font-size: 11px; font-variant-numeric: tabular-nums; }
.wl-row .rm { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); display: none; border: 0; background: var(--surface-3); border-radius: 4px; cursor: pointer; padding: 2px 6px; color: var(--text-muted); }
.wl-row:hover .rm { display: block; }
.flash-up { animation: fu .6s; }
.flash-down { animation: fd .6s; }
@keyframes fu { from { background: var(--up-soft); } }
@keyframes fd { from { background: var(--down-soft); } }

/* Bottom panel */
.bottom { height: 340px; display: flex; flex-direction: column; border-top: 1px solid var(--border); background: var(--surface); flex-shrink: 0; min-height: 0; }
.bottom.collapsed { height: 38px; }
.bottom.collapsed .tab-body { display: none; }
.tabs { display: flex; align-items: center; gap: 2px; padding: 0 8px; border-bottom: 1px solid var(--border); flex-shrink: 0; overflow-x: auto; }
.tabs button[role="tab"] { border: 0; background: transparent; padding: 10px 12px; cursor: pointer; color: var(--text-muted); border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 600; }
.tabs button[role="tab"].active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow: auto; padding: 12px; min-height: 0; }

/* Strategy tester */
.st-grid { display: grid; grid-template-columns: minmax(280px, 1.1fr) minmax(300px, 1fr); gap: 14px; height: 100%; min-height: 0; }
.st-left { display: flex; flex-direction: column; gap: 8px; min-height: 0; }
.st-row { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.code {
  flex: 1; min-height: 160px; width: 100%; resize: vertical; font: 12px/1.5 var(--font-mono); tab-size: 2;
  background: var(--bg); white-space: pre; overflow: auto;
}
.params { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; }
.params input { width: 100%; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.st-right { display: flex; flex-direction: column; gap: 10px; min-height: 0; min-width: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 6px; }
.stat { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 7px 9px; }
.stat .k { font-size: 11px; color: var(--text-muted); }
.stat .v { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.equity-chart { height: 150px; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; flex-shrink: 0; }
.empty { color: var(--text-muted); padding: 20px; text-align: center; }
.error-box { color: var(--down); background: var(--down-soft); border-radius: 6px; padding: 8px 10px; white-space: pre-wrap; font-family: var(--font-mono); font-size: 12px; }

/* Tables */
table.grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.grid th { text-align: left; font-weight: 500; color: var(--text-muted); font-size: 11px; padding: 6px 8px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
table.grid td { padding: 6px 8px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
table.grid td.r, table.grid th.r { text-align: right; }
table.grid tr.clickable { cursor: pointer; }
table.grid tr.clickable:hover td { background: var(--surface-2); }

/* Paper */
.acct-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px; margin-bottom: 12px; }
.acct { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; }
.acct h4 { margin: 0 0 6px; font-size: 12px; color: var(--text-muted); font-weight: 600; }
.acct .big { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.acct .row { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); }
.section-title { font-size: 12px; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; margin: 14px 0 6px; display: flex; justify-content: space-between; align-items: center; }

/* Ticket */
.ticket h3 { margin: 0; font-size: 14px; }
.side-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: 7px; border: 1px solid var(--border); }
.side-toggle button { border: 0; padding: 7px; border-radius: 5px; cursor: pointer; background: transparent; font-weight: 600; color: var(--text-muted); }
.side-toggle button.on.buy { background: var(--up); color: #fff; }
.side-toggle button.on.sell { background: var(--down); color: #fff; }
.seg { display: flex; gap: 4px; }
.seg button { flex: 1; border: 1px solid var(--border); background: var(--surface-2); border-radius: 5px; padding: 5px; cursor: pointer; color: var(--text-muted); }
.seg button.on { color: var(--text); border-color: var(--accent); }
.kv { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); }
.kv b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }
.trade-toggle { display: none; }
.hint { font-size: 11px; color: var(--text-muted); }

/* Dialogs */
.dialog {
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text);
  padding: 0; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 64px);
}
.dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog .d-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.dialog .d-head h2 { margin: 0; font-size: 16px; }
.dialog .d-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; overflow: auto; }
.dialog .d-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.search-dialog .d-body { padding: 0; gap: 0; }
.search-dialog input.q { border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 14px 16px; font-size: 15px; background: transparent; }
.search-results { max-height: 50vh; overflow: auto; }
.search-results .r { display: flex; justify-content: space-between; padding: 9px 16px; cursor: pointer; gap: 8px; }
.search-results .r:hover, .search-results .r.sel { background: var(--surface-2); }
.search-results .r .m { font-size: 11px; color: var(--text-muted); }
.chips { display: flex; gap: 4px; padding: 8px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.chips button { border: 1px solid var(--border); background: transparent; border-radius: 99px; padding: 3px 10px; cursor: pointer; color: var(--text-muted); }
.chips button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.dialog p { margin: 0; }
.dialog ol { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 4px; color: var(--text-muted); }
.dialog code { font-family: var(--font-mono); font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

/* Toasts */
.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 6px; z-index: 100; }
.toast { background: var(--surface-3); border: 1px solid var(--border); border-left: 3px solid var(--c1); padding: 9px 12px; border-radius: 6px; max-width: 360px; box-shadow: 0 6px 20px rgba(0,0,0,.3); }
.toast.ok { border-left-color: var(--up); }
.toast.error { border-left-color: var(--down); }

/* Bots */
.bot { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; background: var(--surface-2); display: flex; flex-direction: column; gap: 6px; }
.bot-head { display: flex; align-items: center; gap: 8px; }
.bot-head b { flex: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--grid-strong); }
.dot.on { background: var(--up); box-shadow: 0 0 0 3px var(--up-soft); }
.bot-log { font-size: 11px; color: var(--text-muted); font-family: var(--font-mono); max-height: 70px; overflow: auto; }
.bots { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 8px; }

/* Option chain */
.oc-bar { align-items: end; margin-bottom: 8px; }
.oc-summary { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--text-muted); margin-left: auto; }
.oc-summary b { color: var(--text); font-variant-numeric: tabular-nums; }
table.oc { font-size: 12px; }
table.oc th.side { text-align: center; color: var(--text); letter-spacing: .06em; }
table.oc thead tr:first-child th { height: 26px; padding-top: 4px; padding-bottom: 4px; }
table.oc thead tr:nth-child(2) th { top: 26px; }
table.oc thead th { z-index: 3; }
table.oc th.strike, table.oc td.strike { text-align: center; font-weight: 700; background: var(--surface-2); position: sticky; }
table.oc td { padding: 5px 8px; }
table.oc td.itm { background: color-mix(in srgb, var(--c2) 9%, transparent); }
table.oc tr.atm td { border-top: 2px solid var(--accent); }
table.oc tr.atm td.strike { color: var(--accent); }
table.oc td.oi { background-image: linear-gradient(to left, color-mix(in srgb, var(--c1) 28%, transparent) var(--w), transparent var(--w)); }
table.oc td.oi.pe { background-image: linear-gradient(to right, color-mix(in srgb, var(--c1) 28%, transparent) var(--w), transparent var(--w)); }
table.oc td.ltp { position: relative; min-width: 92px; }
table.oc td.ltp a { color: var(--text); text-decoration: none; }
table.oc td.ltp a:hover { text-decoration: underline; }
table.oc td.ltp .chg { font-size: 10px; }
table.oc .trade { position: absolute; left: 4px; top: 50%; transform: translateY(-50%); display: none; gap: 2px; }
table.oc tr:hover .trade, table.oc .trade:focus-within { display: inline-flex; }
table.oc .trade button { border: 0; border-radius: 4px; font-size: 10px; font-weight: 700; padding: 3px 6px; cursor: pointer; color: #fff; }
table.oc .trade .b { background: var(--up); }
table.oc .trade .s { background: var(--down); }

/* Sign-in */
body.locked > header, body.locked > .layout { display: none !important; }
.login-screen { flex: 1; display: grid; place-items: center; padding: 24px 16px; overflow: auto; }
.login-card {
  width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.login-card p { margin: 0; }
.login-brand { display: flex; align-items: center; gap: 10px; }
.login-brand h1 { margin: 0; font-size: 22px; }
.login-logo { display: inline-flex; }
.login-card .field input { padding: 9px 10px; font-size: 14px; }
.login-card .btn.primary { padding: 10px; font-size: 14px; }
.login-msg { font-size: 12px; border-radius: 6px; padding: 8px 10px; }
.login-msg.error { color: var(--down); background: var(--down-soft); }
.login-msg.ok { color: var(--up); background: var(--up-soft); }

/* Responsive */
@media (max-width: 1100px) {
  .layout { grid-template-columns: 220px minmax(0, 1fr); }
  .ticket { display: none; }
  .trade-toggle { display: inline-block; }
  .ticket.show { display: flex; position: fixed; right: 0; top: 50px; bottom: 0; width: 300px; z-index: 30; box-shadow: -8px 0 30px rgba(0,0,0,.3); }
}
@media (max-width: 760px) {
  body { overflow: auto; }
  .topbar { flex-wrap: wrap; height: auto; padding: 8px 16px; }
  .brand span { display: none; }
  .quote { min-width: 0; }
  .layout { display: flex; flex-direction: column; }
  .watchlist { max-height: 220px; border-right: 0; border-bottom: 1px solid var(--border); }
  .chart-area { min-height: 360px; flex-direction: column; }
  .draw-bar { width: auto; flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid var(--border); padding: 4px 8px; }
  .draw-sep { width: 0; height: 24px; border-top: 0; border-left: 1px solid var(--border); margin: 0 4px; }
  .bottom { height: auto; min-height: 300px; }
  .st-grid { grid-template-columns: 1fr; }
  .tab-body { padding: 12px 16px; }
}
