:root {
  --bg: #111927;
  --panel: #161F30;
  --panel-2: #1C2739;
  --line: #26334A;
  --line-strong: #34455F;
  --ink: #E4E9F2;
  --ink-2: #A5B0C4;
  --ink-3: #6E7C95;
  --accent: #F4A63A;
  --accent-ink: #1A1206;
  --up: #2FBF8F;
  --down: #F0544F;
  --focus: #7FB3FF;
  --s1: #5B8DEF; --s2: #F4A63A; --s3: #C084FC; --s4: #38BDF8; --s5: #F472B6;
  --radius: 4px;
  --font: "IBM Plex Sans", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.45 var(--font);
  font-variant-numeric: tabular-nums;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }

.num { font-variant-numeric: tabular-nums; }
.up { color: var(--up); }
.down { color: var(--down); }
.muted { color: var(--ink-3); }
.hidden { display: none !important; }

/* ---------- shell ---------- */
.app {
  display: grid;
  grid-template-rows: 44px 1fr;
  grid-template-columns: 280px 1fr;
  grid-template-areas: "top top" "side main";
  height: 100vh;
  height: 100dvh;
}
.topbar {
  grid-area: top;
  display: flex; align-items: center; gap: 16px;
  padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; letter-spacing: -0.01em; }
.brand svg { width: 18px; height: 18px; }
.topbar .spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--panel-2);
  color: var(--ink-2); font-size: 12px;
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); flex: none; }
.dot.on { background: var(--up); box-shadow: 0 0 0 3px rgba(47,191,143,.18); }
.dot.warn { background: var(--accent); }
.dot.off { background: var(--down); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 6px 12px; border-radius: var(--radius);
  border: 1px solid var(--line-strong); background: var(--panel-2); color: var(--ink);
  line-height: 1.2; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-3); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--panel-2); color: var(--ink); }
.btn.sm { padding: 3px 8px; font-size: 12px; }
.btn.danger { color: var(--down); }

/* ---------- sidebar ---------- */
.sidebar {
  grid-area: side;
  display: flex; flex-direction: column; min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--panel);
}
.search { position: relative; padding: 10px 10px 6px; }
.search input {
  width: 100%; padding: 7px 10px 7px 30px;
  background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius);
}
.search input::placeholder { color: var(--ink-3); }
.search svg { position: absolute; left: 19px; top: 19px; width: 14px; height: 14px; color: var(--ink-3); pointer-events: none; }
.search-results {
  position: absolute; left: 10px; right: 10px; top: 42px; z-index: 30;
  max-height: 60vh; overflow: auto;
  background: var(--panel-2); border: 1px solid var(--line-strong); border-radius: var(--radius);
  box-shadow: 0 12px 30px rgba(0,0,0,.45);
}
.search-results .row { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 7px 10px; cursor: pointer; }
.search-results .row:hover, .search-results .row.active { background: var(--line); }
.search-results .sym { font-weight: 600; }
.search-results .name { grid-column: 1 / -1; color: var(--ink-3); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search-results .ex { color: var(--ink-3); font-size: 11px; }
.search-results .empty { padding: 10px; color: var(--ink-3); }

.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 6px; }
.tabs button {
  background: none; border: 0; padding: 8px 8px 7px; color: var(--ink-3);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.tabs button:hover { color: var(--ink); }

.side-body { flex: 1; overflow: auto; min-height: 0; }
.wl-row {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 8px;
  padding: 7px 8px 7px 12px; cursor: pointer; border-bottom: 1px solid var(--line);
}
.wl-row:hover { background: var(--panel-2); }
.wl-row.active { background: var(--panel-2); box-shadow: inset 3px 0 0 var(--accent); }
.wl-row .sym { font-weight: 600; }
.wl-row .name { color: var(--ink-3); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wl-row .px { text-align: right; }
.wl-row .chg { font-size: 11px; }
.wl-row .rm { background: none; border: 0; color: var(--ink-3); padding: 2px 4px; visibility: hidden; }
.wl-row:hover .rm { visibility: visible; }
.wl-row .rm:hover { color: var(--down); }
.flash-up { animation: flashUp .6s; }
.flash-down { animation: flashDown .6s; }
@keyframes flashUp { from { background: rgba(47,191,143,.25); } }
@keyframes flashDown { from { background: rgba(240,84,79,.25); } }

.side-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.side-table th, .side-table td { padding: 6px 8px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.side-table th { color: var(--ink-3); font-weight: 500; position: sticky; top: 0; background: var(--panel); }
.side-table th:first-child, .side-table td:first-child { text-align: left; padding-left: 12px; }
.side-table tfoot td { font-weight: 600; border-top: 1px solid var(--line-strong); }
.side-note { padding: 14px 12px; color: var(--ink-3); }
.side-note a, .side-note button { margin-top: 6px; }

/* ---------- main ---------- */
.main { grid-area: main; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.banner {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; padding: 8px 14px;
  background: #2A2412; border-bottom: 1px solid #4A3D18; color: #F3D9A6;
}
.banner code { background: rgba(0,0,0,.3); padding: 1px 5px; border-radius: 3px; }
.banner .spacer { flex: 1; }

.chart-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px 18px;
  padding: 8px 14px; border-bottom: 1px solid var(--line);
}
.symbol-block { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.symbol-block .sym { font-size: 16px; font-weight: 600; }
.symbol-block .name { color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 240px; }
.price-block { display: flex; align-items: baseline; gap: 8px; }
.price-block .ltp { font-size: 20px; font-weight: 600; }
.ohlc { display: flex; gap: 12px; color: var(--ink-3); font-size: 12px; }
.ohlc b { color: var(--ink-2); font-weight: 500; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius); overflow: hidden; }
.seg button { background: transparent; border: 0; padding: 4px 9px; color: var(--ink-2); font-size: 12px; border-right: 1px solid var(--line-strong); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--line-strong); color: var(--ink); }
.seg button:hover { color: var(--ink); }
.chart-head .spacer { flex: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--ink-2); }
.legend span::before { content: ""; display: inline-block; width: 14px; height: 2px; vertical-align: middle; margin-right: 6px; background: var(--c, var(--ink-3)); }
.legend .dashed::before { background: repeating-linear-gradient(90deg, var(--c) 0 4px, transparent 4px 7px); }

.charts { flex: 1; display: flex; flex-direction: column; min-height: 220px; position: relative; }
#price-chart { flex: 1; min-height: 160px; }
#pane-chart { height: 150px; border-top: 1px solid var(--line); }
.pane-title { position: absolute; left: 12px; z-index: 5; font-size: 12px; color: var(--ink-2); pointer-events: none; }
.chart-empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--ink-3); text-align: center; padding: 20px; pointer-events: none; z-index: 4;
}
.chart-empty.busy { color: var(--ink-2); }

/* ---------- bottom / strategy ---------- */
.bottom {
  position: relative;
  height: 320px; display: grid; grid-template-columns: 300px 1fr;
  border-top: 1px solid var(--line); background: var(--panel); min-height: 0;
}
.resize-handle { position: absolute; left: 0; right: 0; top: -4px; height: 8px; cursor: row-resize; z-index: 6; }
.resize-handle:hover { background: linear-gradient(transparent 3px, var(--line-strong) 3px 5px, transparent 5px); }
.bottom.collapsed { height: 38px !important; }
.bottom.collapsed .strategy-form, .bottom.collapsed .results-body, .bottom.collapsed .results-tabs { display: none; }
.strategy { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); height: 38px; }
.panel-head h2 { font-size: 13px; font-weight: 600; margin: 0; }
.panel-head .spacer { flex: 1; }
.strategy-form { padding: 8px 12px 10px; overflow: auto; display: grid; gap: 7px; align-content: start; }
.field { display: grid; gap: 3px; }
.field label { color: var(--ink-3); font-size: 12px; }
.field input, .field select {
  width: 100%; padding: 5px 8px; background: var(--bg);
  border: 1px solid var(--line-strong); border-radius: var(--radius);
}
.field input[type="number"] { text-align: right; }
.field .help { color: var(--ink-3); font-size: 11px; }
.params { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.check { display: flex; align-items: center; gap: 8px; color: var(--ink-2); }
.check input { accent-color: var(--accent); }
.desc { color: var(--ink-3); font-size: 12px; margin: -2px 0 2px; }
.actions { display: flex; gap: 8px; margin-top: 4px; }
.actions .btn { flex: 1; }

.results { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.results-tabs { display: flex; align-items: center; gap: 4px; padding: 0 6px; border-bottom: 1px solid var(--line); height: 38px; }
.results-tabs .tabs { border: 0; }
.results-body { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
.metrics {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr));
  gap: 0; border-bottom: 1px solid var(--line);
}
.metric { padding: 8px 12px; border-right: 1px solid var(--line); }
.metric:last-child { border-right: 0; }
.metric .k { color: var(--ink-3); font-size: 11px; }
.metric .v { font-size: 15px; font-weight: 600; }
.metric .sub { color: var(--ink-3); font-size: 11px; }
.tab-panel { flex: 1; min-height: 0; overflow: auto; }
.trades { width: 100%; border-collapse: collapse; font-size: 12px; }
.trades th, .trades td { padding: 5px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
.trades th { position: sticky; top: 0; background: var(--panel); color: var(--ink-3); font-weight: 500; z-index: 1; }
.trades th:first-child, .trades td:first-child, .trades .l { text-align: left; }
.trades tbody tr { cursor: pointer; }
.trades tbody tr:hover { background: var(--panel-2); }
.tag { display: inline-block; padding: 1px 6px; border-radius: 3px; font-size: 11px; font-weight: 600; }
.tag.long { background: rgba(47,191,143,.15); color: var(--up); }
.tag.short { background: rgba(240,84,79,.15); color: var(--down); }
.tag.flat { background: var(--line); color: var(--ink-2); }
.tag.fresh { background: rgba(244,166,58,.18); color: var(--accent); }
.equity-wrap { display: flex; flex-direction: column; height: 100%; }
.equity-legend { display: flex; gap: 16px; padding: 6px 12px 0; font-size: 12px; color: var(--ink-2); }
.equity-legend span::before { content: ""; display: inline-block; width: 14px; height: 2px; vertical-align: middle; margin-right: 6px; background: var(--c); }
#equity-chart { flex: 1; min-height: 120px; }
.empty-state { padding: 22px 16px; color: var(--ink-3); max-width: 52ch; }
.empty-state b { color: var(--ink-2); font-weight: 500; }

/* ---------- toasts ---------- */
.toasts { position: fixed; right: 14px; bottom: 14px; display: grid; gap: 8px; z-index: 100; max-width: 380px; }
.toast { padding: 10px 14px; border-radius: var(--radius); background: var(--panel-2); border: 1px solid var(--line-strong); box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.toast.error { border-color: var(--down); }
.toast.ok { border-color: var(--up); }

/* ---------- responsive ---------- */
.menu-btn { display: none; }
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-areas: "top" "main"; }
  .sidebar { position: fixed; top: 44px; bottom: 0; left: 0; width: min(320px, 90vw); z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 40px rgba(0,0,0,.5); }
  .sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .bottom { grid-template-columns: 1fr; height: auto; max-height: 55vh; overflow: auto; }
  .strategy { border-right: 0; border-bottom: 1px solid var(--line); }
  .charts { min-height: 260px; }
  .symbol-block .name { display: none; }
  .ohlc { display: none; }
  #live-pill, #app-logout { display: none; }
  .topbar { gap: 10px; }
  .banner .spacer { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
  .flash-up, .flash-down { animation: none; }
}

/* ---------- index sweep ---------- */
#batch-btn { width: 100%; margin-top: 2px; }
.batch-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; padding: 6px 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 2; }
.batch-bar select { padding: 4px 6px; background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius); max-width: 260px; }
.check.small { font-size: 12px; gap: 5px; }
.check.small input[type="number"] { width: 52px; padding: 2px 4px; background: var(--bg); border: 1px solid var(--line-strong); border-radius: var(--radius); text-align: right; }
.progress { position: relative; height: 26px; margin: 10px 12px; background: var(--panel-2); border-radius: var(--radius); overflow: hidden; }
.progress-fill { position: absolute; inset: 0 auto 0 0; width: 0; background: var(--accent); opacity: .35; transition: width .3s; }
.progress span { position: absolute; inset: 0; display: flex; align-items: center; padding: 0 10px; font-size: 12px; }
.trades th.sortable { cursor: pointer; user-select: none; }
.trades th.sortable:hover { color: var(--ink); }
.trades th.sorted::after { content: " ▾"; }
.bar-cell { position: relative; }
.bar-cell i { position: absolute; left: 0; top: 4px; bottom: 4px; background: rgba(47,191,143,.18); z-index: 0; }
.bar-cell span { position: relative; z-index: 1; }
.matrix { border-collapse: separate; border-spacing: 2px; font-size: 11px; }
.matrix th { position: sticky; top: 0; background: var(--panel); color: var(--ink-3); font-weight: 500; padding: 4px 6px; z-index: 1; white-space: nowrap; }
.matrix th.rowhead { left: 0; z-index: 2; text-align: left; }
.matrix td.rowhead { position: sticky; left: 0; background: var(--panel); font-weight: 600; padding: 3px 8px; white-space: nowrap; }
.matrix td.cell { text-align: right; padding: 3px 6px; cursor: pointer; border-radius: 2px; color: var(--ink); min-width: 52px; }
.matrix td.cell:hover { outline: 1px solid var(--ink-2); }
.matrix td.cell.na { color: var(--ink-3); background: transparent; }
.matrix td.cell.faded { opacity: .35; }
.matrix-legend { display: flex; gap: 14px; align-items: center; padding: 6px 12px 0; font-size: 12px; color: var(--ink-3); }
.matrix-legend i { display: inline-block; width: 16px; height: 10px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.pill-flag { display: inline-block; font-size: 10px; padding: 0 5px; border-radius: 3px; background: var(--line); color: var(--ink-2); margin-left: 4px; }
.pill-flag.ok { background: rgba(47,191,143,.15); color: var(--up); }

dialog { background: var(--panel); color: var(--ink); border: 1px solid var(--line-strong); border-radius: 6px; padding: 18px 20px; width: min(520px, 92vw); box-shadow: 0 30px 80px rgba(0,0,0,.6); }
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h2 { margin: 0 0 6px; font-size: 15px; }
dialog p { margin: 0 0 12px; font-size: 12px; line-height: 1.5; }
dialog form { display: grid; gap: 12px; }
dialog .label { color: var(--ink-3); font-size: 12px; }
.checklist { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.checklist label { display: flex; align-items: center; gap: 7px; font-size: 12px; }
.checklist label.off { color: var(--ink-3); }
.checklist input { accent-color: var(--accent); width: auto; padding: 0; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
