:root {
  --bg: #0b0e11;
  --panel: #181a20;
  --panel-2: #1e2329;
  --line: #2b3139;
  --text: #eaecef;
  --muted: #848e9c;
  --dim: #5e6673;
  --green: #0ecb81;
  --red: #f6465d;
  --green-bg: rgba(14, 203, 129, 0.12);
  --red-bg: rgba(246, 70, 93, 0.12);
  --yellow: #f0b90b;
  --accent: #fcd535;
  --mark: #d8a90b;
  --liq: #b36bff;
  --entry: #6ea8fe;
  --font: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "SF Mono", "JetBrains Mono", "Fira Code", ui-monospace, Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 12px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
.mono, .v, td, .book-row, .trade-row { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.green { color: var(--green); }
.red { color: var(--red); }
.yellow { color: var(--yellow); }
.hidden { display: none !important; }
kbd {
  font: 10px var(--mono); padding: 0 4px; border-radius: 3px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--muted);
}

#app { display: flex; flex-direction: column; height: 100vh; gap: 4px; background: var(--bg); }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  height: 56px; padding: 0 16px; background: var(--panel); flex-shrink: 0;
  overflow: hidden;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; color: var(--accent); white-space: nowrap; }
.symbol { white-space: nowrap; }
.sym-name { font-size: 18px; font-weight: 600; }
.sym-sub { color: var(--muted); font-size: 11px; }
.last-price { font: 600 20px var(--mono); min-width: 110px; font-variant-numeric: tabular-nums; }
.stats { display: flex; gap: 22px; flex: 1; min-width: 0; overflow: hidden; }
.stat { display: flex; flex-direction: column; white-space: nowrap; }
.stat .k { color: var(--muted); font-size: 11px; }
.stat .v { font-size: 12px; }
.conn { display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); }
.conn.on .dot { background: var(--green); box-shadow: 0 0 6px var(--green); }
.conn.off .dot { background: var(--red); }

/* ---------- Sim bar ---------- */
.simbar {
  display: flex; align-items: center; gap: 10px; padding: 6px 16px;
  background: var(--panel); flex-shrink: 0; flex-wrap: wrap;
}
.icon-btn {
  width: 30px; height: 26px; border-radius: 4px; background: var(--accent); color: #181a20;
  font-weight: 700; font-size: 12px;
}
.ghost-btn {
  height: 26px; padding: 0 10px; border-radius: 4px; background: var(--panel-2);
  border: 1px solid var(--line); color: var(--text);
}
.ghost-btn:hover { border-color: var(--muted); }
.seg { display: flex; background: var(--panel-2); border-radius: 4px; padding: 2px; }
.seg button { padding: 2px 8px; border-radius: 3px; color: var(--muted); font-family: var(--mono); }
.seg button.active { background: var(--line); color: var(--text); }
.progress-wrap { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 200px; }
.progress { flex: 1; height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.15s linear; }
.status-pill {
  padding: 2px 8px; border-radius: 10px; font-size: 11px; text-transform: capitalize;
  background: var(--panel-2); color: var(--muted); border: 1px solid var(--line);
}
.status-pill.running { color: var(--green); border-color: rgba(14,203,129,.4); }
.status-pill.paused { color: var(--yellow); border-color: rgba(240,185,11,.4); }
.status-pill.live { color: var(--red); border-color: rgba(246,70,93,.5); }
.status-pill.live::before { content: "● "; animation: blink 1.6s infinite; }
@keyframes blink { 50% { opacity: .3; } }
body.is-live #btnPlay, body.is-live #btnStep, body.is-live #btnStep60, body.is-live #speedSeg,
body.is-live .progress, body.is-live #simEnd { display: none; }
.status-pill.liquidated { color: var(--red); border-color: rgba(246,70,93,.5); }
#windowSel {
  height: 26px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px;
  padding: 0 6px; max-width: 220px;
}

/* ---------- Grid ---------- */
.grid {
  flex: 1; min-height: 0; display: grid; gap: 4px;
  grid-template-columns: minmax(0, 1fr) 300px 320px;
  grid-template-rows: minmax(0, 1fr) 260px;
  grid-template-areas: "chart book order" "bottom bottom order";
}
.panel { background: var(--panel); min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.chart-panel { grid-area: chart; }
.book-panel { grid-area: book; }
.order-panel { grid-area: order; overflow-y: auto; padding: 12px 16px; gap: 10px; }
.bottom-panel { grid-area: bottom; }

.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  height: 38px; padding: 0 12px; border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.tabs { display: flex; gap: 14px; align-items: center; height: 100%; }
.tab {
  height: 100%; color: var(--muted); font-weight: 500; position: relative; white-space: nowrap;
}
.tab:hover:not(:disabled) { color: var(--text); }
.tab.active { color: var(--text); }
.tab.active::after {
  content: ""; position: absolute; left: 50%; bottom: 0; transform: translateX(-50%);
  width: 16px; height: 2px; background: var(--accent); border-radius: 1px;
}
#intervalTabs .tab { font-family: var(--mono); }
.chart-legend { font: 11px var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; }
.chart-legend b { font-weight: 400; color: var(--text); }
.chart-wrap { flex: 1; min-height: 0; position: relative; cursor: crosshair; user-select: none; }
#chartOverlay { pointer-events: none; }
.render-badge {
  font: 10px var(--mono); color: var(--muted); border: 1px solid var(--line); border-radius: 3px;
  padding: 1px 5px; white-space: nowrap; flex-shrink: 0;
}
.render-badge.gpu { color: var(--green); border-color: rgba(14,203,129,.4); }
.chart-legend { flex: 1; text-align: right; }
.chart-wrap canvas, .equity-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; cursor: inherit; }
.equity-wrap canvas { cursor: crosshair; }

/* ---------- Order book ---------- */
.book-view, .trades-view { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 4px 0; }
.book-cols {
  display: grid; grid-template-columns: 1fr 1fr 1fr; padding: 4px 12px; color: var(--muted); font-size: 11px;
}
.book-cols span:not(:first-child) { text-align: right; }
.book-side { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
.book-side.asks { justify-content: flex-end; }
.book-row, .trade-row {
  display: grid; grid-template-columns: 1fr 1fr 1fr; padding: 0 12px; height: 19px; align-items: center;
  position: relative; font-size: 12px; flex-shrink: 0;
}
.book-row span:not(:first-child), .trade-row span:not(:first-child) { text-align: right; position: relative; }
.book-row span { position: relative; z-index: 1; }
.book-row .bar { position: absolute; top: 1px; bottom: 1px; right: 0; z-index: 0; }
.asks .book-row .px { color: var(--red); }
.asks .bar { background: var(--red-bg); }
.bids .book-row .px { color: var(--green); }
.bids .bar { background: var(--green-bg); }
.book-row:hover { background: var(--panel-2); }
.book-mid {
  display: flex; align-items: baseline; gap: 10px; padding: 6px 12px; border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line); margin: 2px 0;
}
.mid-last { font: 600 18px var(--mono); }
.mid-mark { font: 12px var(--mono); color: var(--muted); }
.mid-spread { margin-left: auto; font: 11px var(--mono); }
.book-ratio { display: flex; align-items: center; gap: 8px; padding: 6px 12px 2px; font: 11px var(--mono); }
.ratio-bar { flex: 1; height: 4px; background: var(--red); border-radius: 2px; overflow: hidden; }
.ratio-bar div { height: 100%; background: var(--green); width: 50%; transition: width .3s; }
.trade-list { flex: 1; overflow: hidden; }
.trade-row .t { color: var(--muted); }

/* ---------- Order panel ---------- */
.margin-row { display: flex; gap: 8px; }
.pill {
  flex: 1; height: 30px; border-radius: 4px; background: var(--panel-2); font-weight: 500;
}
.pill:not(:disabled):hover { background: var(--line); }
.pill:disabled { opacity: 1; cursor: default; }
.lev-picker { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.lev-title { font-weight: 600; }
.lev-opts { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.lev-opts button { height: 28px; border-radius: 4px; background: var(--bg); border: 1px solid var(--line); font-family: var(--mono); }
.lev-opts button.active { border-color: var(--accent); color: var(--accent); }
.lev-note { font-size: 11px; }
.order-tabs { height: 30px; border-bottom: 1px solid var(--line); }
.row-kv { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.row-kv.small { font-size: 11px; }
.size-box { display: flex; flex-direction: column; gap: 6px; background: var(--panel-2); border-radius: 6px; padding: 10px; }
.size-head { display: flex; justify-content: space-between; }
.slider { position: relative; height: 26px; margin: 2px 6px; cursor: pointer; }
.slider .track { position: absolute; left: 0; right: 0; top: 12px; height: 3px; background: var(--line); border-radius: 2px; }
.slider .fill { position: absolute; left: 0; top: 12px; height: 3px; background: var(--muted); border-radius: 2px; }
.slider .stop {
  position: absolute; top: 8px; width: 11px; height: 11px; margin-left: -5.5px; transform: rotate(45deg);
  background: var(--panel-2); border: 2px solid var(--line);
}
.slider .stop.on { border-color: var(--text); background: var(--muted); }
.slider .stop.sel { border-color: var(--accent); background: var(--accent); }
.slider .lbl { position: absolute; top: 22px; font: 10px var(--mono); color: var(--muted); transform: translateX(-50%); }
.size-box .slider { margin-bottom: 12px; }
.order-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn-buy, .btn-sell { height: 40px; border-radius: 4px; font-weight: 600; font-size: 13px; color: #fff; }
.btn-buy { background: var(--green); }
.btn-sell { background: var(--red); }
.btn-buy:hover { filter: brightness(1.1); }
.btn-sell:hover { filter: brightness(1.1); }
.btn-buy kbd, .btn-sell kbd { background: rgba(0,0,0,.18); border-color: transparent; color: rgba(255,255,255,.8); margin-left: 4px; }
.lev-warn {
  font-size: 11px; line-height: 1.45; color: var(--yellow); background: rgba(240,185,11,.08);
  border: 1px solid rgba(240,185,11,.3); border-radius: 4px; padding: 6px 8px;
}
.hint { color: var(--dim); font-size: 11px; line-height: 1.45; }
.account { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.acc-title { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.mr { margin-top: 6px; display: flex; flex-direction: column; gap: 4px; }
.mr-bar { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.mr-bar div { height: 100%; width: 0; background: var(--green); transition: width .2s, background .2s; }
.shortcuts { margin-top: auto; font-size: 11px; line-height: 1.9; padding-top: 8px; }

/* ---------- Bottom ---------- */
.bottom-body { flex: 1; min-height: 0; position: relative; }
.table-wrap { position: absolute; inset: 0; overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  position: sticky; top: 0; background: var(--panel); text-align: left; font-weight: 400; color: var(--muted);
  padding: 8px 12px; white-space: nowrap; font-size: 11px;
}
td { padding: 7px 12px; white-space: nowrap; border-top: 1px solid rgba(43,49,57,.5); }
tbody tr:hover { background: var(--panel-2); }
td.empty { text-align: center; color: var(--dim); padding: 36px; font-family: var(--font); border: 0; }
.sym-cell { display: flex; align-items: center; gap: 8px; font-family: var(--font); }
.sym-cell .bar { width: 3px; height: 26px; border-radius: 2px; }
.sym-cell small { color: var(--muted); display: block; font-size: 10px; }
.badge { font-size: 10px; padding: 0 4px; border-radius: 2px; background: var(--panel-2); color: var(--yellow); margin-left: 4px; }
.close-btn { padding: 3px 10px; border-radius: 3px; background: var(--panel-2); border: 1px solid var(--line); font-family: var(--font); }
.close-btn:hover { border-color: var(--accent); color: var(--accent); }
.equity-wrap { position: absolute; inset: 0; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: 340px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; pointer-events: none; }
.toast {
  background: var(--panel-2); border: 1px solid var(--line); border-left: 3px solid var(--muted);
  padding: 10px 14px; border-radius: 4px; min-width: 240px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  animation: toastIn .18s ease-out;
}
.toast.buy { border-left-color: var(--green); }
.toast.sell { border-left-color: var(--red); }
.toast.warn { border-left-color: var(--yellow); }
.toast.bad { border-left-color: var(--red); background: #2a1519; }
.toast b { display: block; margin-bottom: 2px; }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Responsive ---------- */
@media (max-width: 1280px) {
  .stats .stat:nth-child(n+6) { display: none; }
}
@media (max-width: 1050px) {
  body { overflow: auto; }
  #app { height: auto; min-height: 100vh; }
  .grid {
    grid-template-columns: minmax(0, 1fr) 280px;
    grid-template-rows: 460px auto 300px;
    grid-template-areas: "chart chart" "book order" "bottom bottom";
  }
  .book-panel { height: 560px; }
  .stats { display: none; }
  .toasts { right: 16px; }
}
@media (max-width: 640px) {
  .grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 380px auto 520px 300px;
    grid-template-areas: "chart" "order" "book" "bottom";
  }
  .topbar { gap: 12px; padding: 0 12px; }
  .brand span, .sym-sub { display: none; }
  .simbar { padding: 6px 12px; }
  .progress-wrap { min-width: 100%; order: 10; }
}

/* ---------- FPS meter + benchmark ---------- */
.fps-meter {
  font-size: 10px; color: var(--muted); white-space: pre; flex-shrink: 0;
  padding: 1px 5px; border-radius: 3px; background: var(--panel-2);
}
.fps-meter.warn { color: var(--yellow); }
.fps-meter.bad { color: var(--red); }
.bench-btn {
  font: 10px var(--mono); color: var(--muted); border: 1px solid var(--line); border-radius: 3px;
  padding: 1px 6px; flex-shrink: 0;
}
.bench-btn:hover { color: var(--accent); border-color: var(--accent); }
.bench-modal {
  position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.55);
  display: flex; align-items: flex-start; justify-content: center; padding: 60px 16px; overflow: auto;
}
.bench-modal.running { background: rgba(0,0,0,.15); pointer-events: none; }
.bench-modal.running .bench-card { pointer-events: auto; opacity: .92; }
.bench-card {
  width: min(1100px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5); display: flex; flex-direction: column;
}
.bench-head { display: flex; align-items: baseline; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.bench-head b { font-size: 14px; }
.bench-head .muted { font: 11px var(--mono); flex: 1; }
.bench-x { color: var(--muted); font-size: 14px; }
.bench-x:hover { color: var(--text); }
.bench-opts { display: flex; gap: 16px; padding: 12px 16px; align-items: flex-start; flex-wrap: wrap; }
.bench-scen { display: flex; flex-wrap: wrap; gap: 6px 16px; flex: 1; }
.bench-scen label { display: flex; gap: 6px; align-items: center; color: var(--muted); cursor: pointer; }
.bench-opts select { margin-left: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px; height: 24px; }
.bench-actions { display: flex; gap: 8px; align-items: center; padding: 0 16px 12px; flex-wrap: wrap; }
.btn-run { height: 26px; padding: 0 16px; border-radius: 4px; background: var(--accent); color: #181a20; font-weight: 600; }
.bench-modal.running .bench-actions button { pointer-events: none; opacity: .5; }
.bench-progress { height: 2px; background: var(--line); }
.bench-progress div { height: 100%; width: 0; background: var(--accent); transition: width .1s linear; }
.bench-body { padding: 12px 16px; overflow-x: auto; }
.bench-body:empty { display: none; }
.bench-body h4 { font-size: 12px; margin-bottom: 8px; }
.bench-body p { line-height: 1.5; }
.bench-body .small { font-size: 11px; margin-top: 8px; }
.bench-table td, .bench-table th { padding: 5px 10px; text-align: right; font-size: 11px; }
.bench-table td:first-child, .bench-table th:first-child { text-align: left; font-family: var(--font); }
.bench-table th { position: static; }
.bench-table small { color: var(--muted); }

/* ---------- Connection banner ---------- */
.conn-banner {
  padding: 8px 16px; background: #2a1519; color: var(--text); border-left: 3px solid var(--red);
  font-size: 12px; flex-shrink: 0;
}
.conn-banner code { font: 11px var(--mono); background: var(--panel-2); padding: 1px 4px; border-radius: 3px; }
body.disconnected .order-btns button { opacity: .4; cursor: not-allowed; }

/* ---------- Limit orders ---------- */
.price-field {
  display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 10px;
  background: var(--panel-2); border: 1px solid transparent; border-radius: 6px;
}
.price-field:focus-within { border-color: var(--accent); }
.price-field input {
  flex: 1; min-width: 0; background: none; border: 0; outline: 0; color: var(--text);
  font: 13px var(--mono); text-align: right;
}
.price-field input::-webkit-inner-spin-button { -webkit-appearance: none; }
.last-btn { font: 11px var(--font); color: var(--accent); }
.limit-flags { display: flex; gap: 14px; align-items: center; font-size: 12px; }
.limit-flags label { display: flex; gap: 6px; align-items: center; cursor: pointer; }
.limit-flags .muted { margin-left: auto; font-size: 11px; }
.book-row { cursor: pointer; }
#btnCancelAll { font-size: 11px; padding: 1px 8px; }

/* ---------- Stop orders ---------- */
.stop-box { display: flex; flex-direction: column; gap: 8px; }
.stop-box select {
  background: var(--bg); border: 1px solid var(--line); border-radius: 4px; height: 24px; font-size: 11px;
}
.stop-kind { width: 100%; }
.stop-kind button { flex: 1; padding: 4px 8px; }
.stop-dir { font-size: 11px; min-height: 14px; }

/* header account chip */
.acct-chip { display: flex; gap: 14px; align-items: center; padding: 4px 10px; border: 1px solid var(--line);
  border-radius: 6px; background: none; color: var(--text); font: inherit; cursor: pointer; white-space: nowrap; }
.acct-chip:hover { border-color: var(--muted); }
.acct-chip .stat .v { font-family: var(--mono); }
.assets-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px 24px; padding: 12px 16px; }
.assets-grid .k { color: var(--muted); font-size: 11px; display: block; }
.assets-grid .v { font-family: var(--mono); font-size: 14px; }

/* page navigation */
.top-nav { display: flex; gap: 2px; }
.top-nav a { color: var(--muted); text-decoration: none; padding: 5px 10px; border-radius: 4px; font-weight: 500; }
.top-nav a:hover { color: var(--text); background: var(--panel-2); }
.top-nav a.active { color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.acc-title { display: flex; justify-content: space-between; align-items: baseline; }
.acc-link { color: var(--muted); font-weight: 400; font-size: 11px; text-decoration: none; }
.acc-link:hover { color: var(--accent); }
#accountSel { max-width: 180px; }

/* shared form controls, dialogs and user menu (all pages) */
select.inp, input.inp {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 4px;
  padding: 6px 8px; font: inherit; min-width: 0;
}
input.inp:focus, select.inp:focus { outline: none; border-color: var(--accent); }
.btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 4px;
  padding: 6px 12px; font: inherit; cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); color: #181a20; border-color: var(--accent); font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }
dialog.modal { margin: auto; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 0; width: min(380px, calc(100vw - 32px)); }
dialog.modal::backdrop { background: rgba(0,0,0,.6); }
dialog.modal form { display: flex; flex-direction: column; gap: 10px; padding: 16px; }
dialog.modal h3 { font-size: 15px; }
dialog.modal label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 11px; }
dialog.modal .row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 4px; }
/* user menu (nav.js) */
.user-menu { position: relative; }
.user-btn { display: flex; align-items: center; gap: 6px; background: none; border: 1px solid var(--line); color: var(--text); border-radius: 4px; padding: 4px 10px; font: inherit; cursor: pointer; }
.user-btn:hover { border-color: var(--muted); }
.user-pop { position: fixed; min-width: 200px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 4px; z-index: 60; box-shadow: 0 8px 24px rgba(0,0,0,.5); }
.user-pop button { display: block; width: 100%; text-align: left; background: none; border: 0; color: var(--text); padding: 8px 10px; border-radius: 4px; font: inherit; cursor: pointer; }
.user-pop button:hover { background: var(--panel); }
.user-pop .who { color: var(--muted); padding: 6px 10px; font-size: 11px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
