/* Camel Backtester — dark TradingView-like theme */
:root {
  --bg: #0f1118;
  --surface: #161a25;
  --surface2: #1d2230;
  --border: #2a3040;
  --ink: #e6e9f0;
  --ink2: #9aa3b5;
  --muted: #6b7387;
  --accent: #4f8cff;
  --green: #26a69a;
  --red: #ef5350;
  --gold: #e8b93e;
  --blue: #5b9cf6;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--ink);
  font: 13px/1.45 "Segoe UI", system-ui, sans-serif; }

/* ── top bar ── */
#topbar {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-size: 15px; white-space: nowrap; }
.brand .sub { color: var(--ink2); font-size: 12px; margin-left: 6px; }
.home-link {
  display: inline-block; color: var(--ink2); text-decoration: none;
  border: 1px solid var(--border); border-radius: 5px; background: var(--surface2);
  padding: 1px 8px; margin-right: 4px; font-size: 14px; vertical-align: 2px;
}
.home-link:hover { color: var(--ink); border-color: var(--accent); }
.controls { display: flex; align-items: flex-end; gap: 10px; flex-wrap: wrap; }
.controls label { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--ink2); }
.controls select, .controls input[type=date] {
  background: var(--surface2); color: var(--ink); border: 1px solid var(--border);
  border-radius: 4px; padding: 5px 8px; font-size: 13px; color-scheme: dark;
}
button {
  background: var(--accent); color: #fff; border: none; border-radius: 4px;
  padding: 7px 14px; font-size: 13px; font-weight: 600; cursor: pointer;
}
button:hover { filter: brightness(1.12); }
button.ghost {
  background: transparent; color: var(--ink2); border: 1px solid var(--border); font-weight: 400;
}
button.small { padding: 2px 8px; font-size: 11px; }
.data-info { margin-left: auto; color: var(--muted); font-size: 11px; text-align: right; }

/* ── code drawer ── */
#codeDrawer {
  background: var(--surface); border-bottom: 1px solid var(--border);
  padding: 10px 16px; display: flex; flex-direction: column; gap: 8px;
}
/* `display:flex` above outranks the browser's default [hidden] rule, so the
   drawer needs an explicit hidden state or it can never be closed. */
#codeDrawer[hidden] { display: none; }
.code-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.code-hint { color: var(--muted); font-size: 11px; }
#codeArea {
  width: 100%; height: 340px; resize: vertical;
  background: #0c0e14; color: #d8dee9; border: 1px solid var(--border); border-radius: 6px;
  font: 12px/1.5 Consolas, "Cascadia Code", monospace; padding: 10px; tab-size: 4;
  white-space: pre; overflow: auto;
}
#codeStatus { font-size: 12px; color: var(--muted); min-height: 16px; }
#codeStatus.ok { color: var(--green); }
#codeStatus.err { color: var(--red); white-space: pre-wrap; }

/* ── layout ── */
#layout { display: flex; min-height: calc(100vh - 55px); }
#sidebar {
  width: 268px; flex: 0 0 268px; background: var(--surface);
  border-right: 1px solid var(--border); padding: 10px 12px 40px;
  overflow-y: auto; max-height: calc(100vh - 55px); position: sticky; top: 55px;
}
.side-title { font-weight: 600; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; }
#main { flex: 1; min-width: 0; padding: 12px 16px 60px; }

/* ── settings form ── */
.set-group { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 10px; }
.set-group > summary {
  cursor: pointer; padding: 7px 10px; font-weight: 600; font-size: 12px;
  color: var(--ink2); text-transform: uppercase; letter-spacing: .4px; list-style: none;
}
.set-group > summary::before { content: "▸ "; }
.set-group[open] > summary::before { content: "▾ "; }
.set-group .rows { padding: 2px 10px 10px; display: flex; flex-direction: column; gap: 7px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.set-row span { color: var(--ink2); font-size: 12px; }
.set-row input[type=number] {
  width: 74px; background: var(--surface2); color: var(--ink);
  border: 1px solid var(--border); border-radius: 4px; padding: 4px 6px; text-align: right;
}
.set-row input[type=checkbox] { accent-color: var(--accent); width: 15px; height: 15px; }
.set-row select {
  max-width: 120px; background: var(--surface2); color: var(--ink);
  border: 1px solid var(--border); border-radius: 4px; padding: 3px 5px;
}
.set-row span[title] { cursor: help; border-bottom: 1px dotted var(--muted); }

/* ── portfolio picker ── */
#portfolioPanel { border: 1px solid var(--accent); border-radius: 6px; padding: 9px 10px 10px; margin-bottom: 12px; }
#portfolioPanel input, #portfolioPanel select {
  background: var(--surface2); color: var(--ink); border: 1px solid var(--border);
  border-radius: 4px; padding: 4px 6px; font: inherit;
}
.pf-hint { color: var(--muted); font-size: 11px; margin-bottom: 8px; }
.pf-list { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.pf-item { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--ink2); cursor: pointer; }
.pf-item input[type=checkbox] { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }
.pf-item .grp { margin-left: auto; color: var(--muted); font-size: 10.5px; }
.pf-item label { display: flex; align-items: center; gap: 7px; flex: 1; min-width: 0; cursor: pointer; }
#portfolioPanel .pf-risk { width: 52px; margin-left: auto; padding: 2px 5px; text-align: right; }
#portfolioPanel .pf-risk:disabled { opacity: .35; }
.pf-item .grp + .pf-risk { margin-left: 0; }
.pf-item .pct { color: var(--muted); font-size: 11px; margin-left: -4px; }
.pf-head { display: flex; justify-content: space-between; font-size: 10.5px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .4px; margin-bottom: 2px; }
.pf-item button { padding: 0 6px; font-size: 11px; }
/* Ticker search in the top bar. The input borrows the select/date styling so
   the row reads as one set of controls. */
.ticker-search { display: flex; gap: 4px; }
.ticker-search input {
  background: var(--surface2); color: var(--ink); border: 1px solid var(--border);
  border-radius: 4px; padding: 5px 8px; font-size: 13px; color-scheme: dark;
  text-transform: uppercase; min-width: 0;
}
.ticker-search input::placeholder { text-transform: none; color: var(--muted); }
.ticker-search input:focus { outline: none; border-color: var(--accent); }

.pf-add { display: flex; gap: 6px; margin-bottom: 8px; }
.pf-add input { flex: 1; min-width: 0; text-transform: uppercase; }
.pf-chart { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  font-size: 12px; color: var(--ink2); margin-bottom: 9px; }
.pf-cap { display: flex; align-items: center; gap: 4px; }
.pf-cap > span { color: var(--muted); font-size: 11px; }
#portfolioPanel #pfCapital { width: 92px; padding: 2px 5px; text-align: right; }
#pfRunBtn { width: 100%; margin-bottom: 6px; }

/* ── charts ── */
/* ticker mode banner — the cycle-strategy state for the ticker on the chart */
#cycleBanner { border: 1px solid var(--border); border-left: 3px solid var(--border); border-radius: 6px;
  background: var(--surface); padding: 9px 12px; margin-bottom: 8px; font-size: 12px; color: var(--ink2); }
#cycleBanner.bull { border-left-color: var(--green); }
#cycleBanner.bear { border-left-color: var(--red); }
#cycleBanner .cb-top { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
#cycleBanner .cb-sym { font-size: 16px; font-weight: 700; color: var(--ink); }
#cycleBanner .cb-tag { font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 4px; letter-spacing: .4px;
  background: var(--surface2); color: var(--ink2); }
#cycleBanner.bull .cb-tag { background: rgba(38,166,154,.15); color: var(--green); }
#cycleBanner.bear .cb-tag { background: rgba(239,83,80,.15); color: var(--red); }
#cycleBanner .cb-head { color: var(--ink); font-weight: 600; }
#cycleBanner .cb-det { margin-top: 3px; }
#cycleBanner .cb-dates { margin-top: 3px; color: var(--muted); font-size: 11.5px; }
#cycleBanner a { color: var(--accent); text-decoration: none; margin-left: auto; font-size: 11.5px; }
#cycleBanner a:hover { text-decoration: underline; }
/* Full chart handoff from The Merchant: what was applied, and whether to keep it */
#handoffBanner { border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 6px;
  background: var(--surface); padding: 9px 12px; margin-bottom: 8px; font-size: 12px; color: var(--ink2);
  display: flex; align-items: center; gap: 10px 14px; flex-wrap: wrap; }
#handoffBanner.warn { border-left-color: var(--gold); }
#handoffBanner .hb-head { color: var(--ink); font-weight: 600; }
#handoffBanner .hb-det { color: var(--muted); font-size: 11.5px; }
#handoffBanner .hb-warn { color: var(--gold); font-size: 11.5px; flex-basis: 100%; }
#handoffBanner .hb-btns { margin-left: auto; display: flex; gap: 6px; }
#chartWrap { position: relative; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface); }
#chart { height: 52vh; min-height: 340px; }
#chartLegend {
  position: absolute; top: 8px; left: 10px; z-index: 10; font-size: 12px;
  color: var(--ink2); background: rgba(15,17,24,.72); padding: 4px 8px; border-radius: 4px;
  pointer-events: none;
}
#equityWrap { position: relative; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface); margin-top: 10px; }
#equityChart { height: 140px; }
/* DSS Bressert oscillator pane — directly under the price chart, time-synced */
#dssWrap { position: relative; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: var(--surface); margin-top: 6px; }
#dssChart { height: 150px; }
#dssWrap .pane-label .k-dss { color: #5b9cf6; }
#dssWrap .pane-label .k-trig { color: #ef9a4a; }
.pane-label { position: absolute; top: 6px; left: 10px; z-index: 10; font-size: 11px; color: var(--muted); pointer-events: none; }

/* Collapse for the two lower panes. The label bar stays put as the handle to
   bring the pane back -- a collapsed pane that vanished entirely would need
   the sidebar checkbox to restore, which is a different control in a different
   place. The chart is display:none rather than zero-height: the charts are
   autoSize, so a 0px box means a 0px canvas and a needless re-layout of a pane
   nobody is looking at. */
.pane-toggle {
  position: absolute; top: 4px; right: 8px; z-index: 11;
  width: 20px; height: 18px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid transparent; border-radius: 4px;
  background: transparent; color: var(--muted);
  font-size: 10px; line-height: 1; cursor: pointer;
  transition: transform .12s ease, color .12s ease, background .12s ease;
}
.pane-toggle:hover { background: var(--surface2); border-color: var(--border); color: var(--ink); }
.pane-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#dssWrap.collapsed, #equityWrap.collapsed { height: 24px; }
#dssWrap.collapsed #dssChart, #equityWrap.collapsed #equityChart { display: none; }
.collapsed > .pane-toggle { transform: rotate(-90deg); }

/* ── tabs & results ── */
#results { margin-top: 14px; }
#tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); }
.tab {
  background: transparent; color: var(--ink2); border: none; border-bottom: 2px solid transparent;
  border-radius: 0; padding: 8px 14px; font-weight: 500;
}
.tab.active { color: var(--ink); border-bottom-color: var(--accent); }
.tab-panel { display: none; padding: 14px 2px; }
.tab-panel.active { display: block; }

/* overview stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; }
.tile .k { color: var(--ink2); font-size: 11px; text-transform: uppercase; letter-spacing: .4px; }
.tile .v { font-size: 20px; font-weight: 600; margin-top: 3px; }
.tile .s { color: var(--muted); font-size: 11px; margin-top: 2px; }
.pos { color: var(--green); }
.neg { color: var(--red); }

/* tables */
table.stats, table.trades { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.stats td, table.trades td, table.trades th {
  padding: 6px 10px; border-bottom: 1px solid var(--border);
}
table.stats td:first-child { color: var(--ink2); width: 260px; }
table.stats td { text-align: left; }
.trades-scroll { overflow-x: auto; max-height: 60vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; }
table.trades th {
  text-align: right; color: var(--ink2); font-weight: 600; background: var(--surface2);
  position: sticky; top: 0; white-space: nowrap;
}
table.trades td { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.trades th:nth-child(-n+3), table.trades td:nth-child(-n+3) { text-align: left; }
table.trades tr.open-row td { color: var(--gold); }
table.trades tr:hover td { background: var(--surface2); cursor: pointer; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border); color: var(--ink);
  border-radius: 6px; padding: 10px 18px; z-index: 100; max-width: 80vw;
}
#toast.err { border-color: var(--red); }

@media (max-width: 900px) {
  #layout { flex-direction: column; }
  #sidebar { width: auto; flex: none; position: static; max-height: none; }
}

/* ── bar-by-bar replay ── */
#replayBar {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 7px 10px; margin-bottom: 6px;
}
#replayBar[hidden] { display: none; }
.rp-group { display: flex; gap: 3px; }
button.rp-btn {
  background: var(--surface2); color: var(--ink); border: 1px solid var(--border);
  padding: 4px 9px; font-size: 13px; line-height: 1.2; min-width: 34px;
}
button.rp-btn:disabled { opacity: .38; cursor: default; filter: none; }
button.rp-play { background: var(--accent); border-color: var(--accent); color: #fff; }
.rp-speed { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--ink2); }
.rp-speed select { font-size: 11px; padding: 3px 4px; }
/* the slider is the length of the bar, so a drag maps to a useful number of bars */
#rpSlider { flex: 1; min-width: 120px; accent-color: var(--accent); cursor: pointer; }
.rp-read { font-size: 12px; color: var(--ink); min-width: 190px; text-align: right;
           font-variant-numeric: tabular-nums; }
.rp-read .rp-pos { color: var(--muted); font-size: 11px; margin-left: 7px; }
/* while picking a start bar, the chart says so rather than looking inert */
#replayHint {
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%); z-index: 11;
  font-size: 12px; color: var(--ink); background: rgba(79,140,255,.9);
  padding: 5px 12px; border-radius: 4px; pointer-events: none; white-space: nowrap;
}
#replayHint[hidden] { display: none; }
#chartWrap.rp-picking { cursor: crosshair; }
/* the replayed part of the run is a live, partial result — say so once */
#results.rp-live #tabs::after {
  content: 'replaying'; align-self: center; margin-left: 10px;
  font-size: 10px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--accent); border: 1px solid var(--accent); border-radius: 3px; padding: 1px 6px;
}
