/* Dark is the default -- it is what people stare at all day on a trading desk.
   Light is a full peer, not an afterthought: the same tokens are redefined so
   every panel, chart and table follows without any per-component special cases. */
:root, :root[data-theme="dark"] {
  --bg: #0b0f16;
  --panel: #121822;
  --panel-2: #182130;
  --border: #223046;
  --text: #dbe4f0;
  --muted: #7e8fa8;
  --accent: #4c9aff;
  --accent-ink: #041018;
  --green: #22c55e;
  --red: #ef4444;
  --amber: #f59e0b;
  --purple: #a855f7;
  --grid: #1b2739;
  --shadow: none;
  --note-bg: rgba(245,158,11,.09);
  --note-tx: #e8d5a8;
  --err-bg: rgba(239,68,68,.09);
  --err-tx: #f0b8b8;
  --ok-bg: rgba(34,197,94,.09);
  --ok-tx: #b5e8c8;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

:root[data-theme="light"] {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --panel-2: #eef2f7;
  --border: #d6dee8;
  --text: #16202e;
  --muted: #5b6b80;
  --accent: #1d6fd4;
  --accent-ink: #ffffff;
  --green: #12874a;
  --red: #c62828;
  --amber: #a86407;
  --purple: #7b3fbf;
  --grid: #e3e9f0;
  --shadow: 0 1px 2px rgba(16,32,54,.05);
  --note-bg: #fdf5e3;
  --note-tx: #6d4c07;
  --err-bg: #fdeaea;
  --err-tx: #922020;
  --ok-bg: #e8f6ec;
  --ok-tx: #14653a;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 13px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }

/* ---------- layout ---------- */
#app { display: grid; grid-template-columns: 224px 1fr; height: 100vh; }
#sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
}
.brand {
  padding: 16px 16px 12px; border-bottom: 1px solid var(--border);
}
.brand h1 { margin: 0; font-size: 15px; letter-spacing: .02em; }
.brand small { color: var(--muted); font-size: 11px; }

nav { padding: 8px; flex: 1; overflow-y: auto; }
nav button {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  background: none; border: 0; color: var(--muted); border-radius: 6px;
  font-size: 13px; cursor: pointer;
}
nav button:hover { background: var(--panel-2); color: var(--text); }
nav button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.runitem {
  padding: 7px 9px; border-radius: 6px; cursor: pointer; margin-bottom: 3px;
  border: 1px solid transparent; font-size: 12px;
}
.runitem:hover { background: var(--panel-2); }
.runitem.active { background: var(--panel-2); border-color: var(--accent); }
.runitem .t { display: flex; justify-content: space-between; gap: 6px; }
.runitem .n { color: var(--muted); font-size: 11px; font-family: var(--mono); }

main { overflow-y: auto; padding: 20px 24px 60px; }
.view { display: none; }
.view.active { display: block; }
h2 { margin: 0 0 4px; font-size: 18px; font-weight: 600; }
.sub { color: var(--muted); margin: 0 0 18px; font-size: 12px; max-width: 78ch; }

/* ---------- panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 8px; padding: 14px 16px; margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.panel > h3 {
  margin: 0 0 10px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted); font-weight: 600;
}
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1100px) { .grid2 { grid-template-columns: 1fr; } }

/* ---------- form ---------- */
label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
input, select, textarea {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 5px; padding: 6px 8px; font-size: 12px; font-family: inherit;
  min-width: 0; width: 100%;
}
input:focus, select:focus { outline: 1px solid var(--accent); border-color: var(--accent); }
.field { min-width: 120px; flex: 0 1 auto; }
.field.sm { max-width: 110px; }
button.btn {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 6px;
  padding: 8px 16px; font-size: 12px; font-weight: 600; cursor: pointer;
}
button.btn:hover { filter: brightness(1.1); }
button.btn:disabled { opacity: .5; cursor: not-allowed; }
button.btn.ghost {
  background: transparent; color: var(--text); border: 1px solid var(--border);
}
button.btn.warn { background: var(--amber); color: #fff; }
a.btn {
  display: inline-block; background: var(--accent); color: var(--accent-ink);
  border: 0; border-radius: 6px; padding: 8px 16px; font-size: 12px;
  font-weight: 600; cursor: pointer; text-decoration: none;
}
a.btn:hover { filter: brightness(1.1); }
a.btn.ghost {
  background: transparent; color: var(--text); border: 1px solid var(--border);
}
.btn.sm { padding: 3px 9px; font-size: 11px; }

.paramgrid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 10px; }
.stat {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 6px; padding: 9px 11px;
}
.stat .k { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-size: 17px; font-family: var(--mono); margin-top: 2px; }
.pos { color: var(--green); } .neg { color: var(--red); } .warnc { color: var(--amber); }

/* ---------- tables ---------- */
.tablewrap { overflow: auto; max-height: 460px; border: 1px solid var(--border); border-radius: 6px; }
table { border-collapse: collapse; width: 100%; font-size: 12px; font-family: var(--mono); }
th, td { padding: 5px 9px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th {
  position: sticky; top: 0; background: var(--panel-2); color: var(--muted);
  font-weight: 600; font-size: 10px; text-transform: uppercase; letter-spacing: .04em;
  z-index: 1; cursor: pointer;
}
th:first-child, td:first-child { text-align: left; }
tbody tr:hover { background: var(--panel-2); }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable.selected { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
th.sorted { color: var(--text); }

/* ---------- statistics table ---------- */
/* The Analyze tab's stat block. Two columns, grouped, no sticky header --
   it is a read-down list of facts, not a sortable dataset. */
.statwrap { max-height: none; }
.stattable { width: 100%; }
.stattable td { border-bottom: 1px solid var(--border); vertical-align: baseline; }
.stattable tr:last-child td { border-bottom: 0; }
.stattable td.statkey {
  color: var(--muted); width: 42%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
}
.stattable td.statval { text-align: right; font-size: 14px; }
/* The qualifier that keeps a number honest -- trade counts behind a win rate,
   the date behind a best trade -- kept quiet so it never competes with it. */
.stattable .statsub {
  display: block; font-size: 10px; color: var(--muted);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  text-transform: none; letter-spacing: 0; margin-top: 1px;
}
.stattable tr.statgroup td {
  background: var(--panel-2); color: var(--muted); font-size: 10px;
  text-transform: uppercase; letter-spacing: .07em; padding: 6px 9px;
  text-align: left; font-weight: 600;
}
.stattable tr.statgroup:hover td { background: var(--panel-2); }

.dstoolbar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; align-items: center; }
.dstoolbar input[type="text"] { flex: 1 1 220px; min-width: 160px; }
.dstoolbar select { flex: 0 0 auto; width: auto; }

.chart { width: 100%; height: 300px; }
.chart.tall { height: 420px; }

.note {
  background: var(--note-bg); border-left: 3px solid var(--amber);
  padding: 9px 12px; border-radius: 0 5px 5px 0; font-size: 12px; margin: 10px 0;
  color: var(--note-tx);
}
.note.err { background: var(--err-bg); border-color: var(--red); color: var(--err-tx); }
.note.ok  { background: var(--ok-bg); border-color: var(--green); color: var(--ok-tx); }
.muted { color: var(--muted); }
code { font-family: var(--mono); background: var(--panel-2); padding: 1px 5px; border-radius: 3px; }
pre {
  font-family: var(--mono); font-size: 11px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: 11px;
  overflow: auto; max-height: 320px; margin: 0;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
  padding: 3px 10px; font-size: 11px; cursor: pointer; user-select: none;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.spinner { color: var(--muted); font-size: 12px; }

.schema-row { display: flex; gap: 20px; flex-wrap: wrap; align-items: flex-start; }
.schema-row .chips { flex: 1 1 200px; }
.schema-legend { flex: 1 1 260px; font-size: 11px; color: var(--muted); }
.schema-legend .sl-row { display: flex; gap: 8px; padding: 3px 0; }
.schema-legend .sl-row + .sl-row { border-top: 1px solid var(--border); }
.schema-legend .sl-name {
  font-family: var(--mono); color: var(--text); flex: 0 0 74px; font-size: 11px;
}
.schema-legend .sl-warn { color: var(--amber); }
@media (max-width: 700px) { .schema-row { flex-direction: column; } }

/* ---------- modal ---------- */
#modal {
  position: fixed; inset: 0; background: rgba(4,8,14,.62); display: none;
  align-items: center; justify-content: center; z-index: 50; padding: 24px;
}
#modal.open { display: flex; }
#modalbox {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  width: min(1100px, 96vw); max-height: 92vh; overflow: auto; padding: 18px 20px;
  box-shadow: 0 20px 60px rgba(4,8,14,.45);
}
#modalbox h3 { margin: 0 0 8px; font-size: 15px; }
.closex { float: right; background: none; border: 0; color: var(--muted);
  font-size: 20px; cursor: pointer; line-height: 1; }

/* ---------- theme toggle ---------- */
.themerow {
  display: flex; gap: 4px; padding: 8px; border-top: 1px solid var(--border);
}
.themerow button {
  flex: 1; padding: 5px 8px; font-size: 11px; border-radius: 5px; cursor: pointer;
  background: transparent; border: 1px solid var(--border); color: var(--muted);
}
.themerow button:hover { color: var(--text); }
.themerow button.active {
  background: var(--accent); color: var(--accent-ink);
  border-color: var(--accent); font-weight: 600;
}

/* ---------- upload ---------- */
.drop {
  border: 1.5px dashed var(--border); border-radius: 8px; padding: 18px 16px;
  text-align: center; color: var(--muted); font-size: 12px; cursor: pointer;
  transition: border-color .12s, background .12s;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--panel-2); }
.drop strong { color: var(--text); display: block; font-size: 13px; margin-bottom: 2px; }
.bar { height: 5px; background: var(--panel-2); border-radius: 3px; overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .18s; }

/* ---------- mobile ----------
   The console has to be usable from a phone: a fixed 224px rail and a
   12-column parameter grid are unusable at 390px wide. Below the breakpoint the
   sidebar becomes a drawer, grids collapse, and every control grows to a real
   touch target. Wide tables keep scrolling inside their own container so the
   page body never scrolls sideways. */
#menubtn { display: none; }
#scrim { display: none; }

@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }

  #menubtn {
    display: flex; align-items: center; justify-content: center;
    position: fixed; top: 10px; left: 10px; z-index: 40;
    width: 42px; height: 42px; border-radius: 9px;
    background: var(--panel); border: 1px solid var(--border);
    color: var(--text); font-size: 19px; cursor: pointer;
    box-shadow: 0 2px 10px rgba(0,0,0,.28);
  }

  #sidebar {
    position: fixed; inset: 0 auto 0 0; width: 264px; z-index: 45;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: 0 0 40px rgba(0,0,0,.4);
  }
  #sidebar.open { transform: none; }

  #scrim {
    display: block; position: fixed; inset: 0; z-index: 44;
    background: rgba(4,8,14,.55); opacity: 0; pointer-events: none;
    transition: opacity .2s;
  }
  #scrim.open { opacity: 1; pointer-events: auto; }

  main { padding: 62px 14px 72px; }
  h2 { font-size: 17px; }
  .sub { font-size: 12px; }

  .grid2 { grid-template-columns: 1fr; }
  .paramgrid { grid-template-columns: repeat(auto-fill, minmax(136px, 1fr)); gap: 9px; }
  .stats { grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 8px; }
  .stat .v { font-size: 15px; }

  .row { gap: 9px; }
  .field { flex: 1 1 138px; min-width: 118px; }
  .field.sm { max-width: none; }

  /* 16px prevents iOS Safari from zooming the viewport on focus. */
  input, select { font-size: 16px; padding: 9px 10px; }
  button.btn { padding: 11px 18px; font-size: 14px; }
  .chip { padding: 7px 13px; font-size: 12px; }
  nav button { padding: 11px 12px; font-size: 14px; }

  .tablewrap { max-height: 340px; -webkit-overflow-scrolling: touch; }
  th, td { padding: 7px 9px; }

  #modal { padding: 0; }
  #modalbox {
    width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh;
    border-radius: 0; border: 0; padding: 14px 14px 40px;
  }
  .chart { height: 240px; }
  .chart.tall { height: 300px; }
}

@media (max-width: 420px) {
  .paramgrid { grid-template-columns: 1fr 1fr; }
  .stats { grid-template-columns: 1fr 1fr; }
}

/* Anything carrying a hover tooltip advertises it: dotted underline on the
 * label, help cursor. The bubble itself is the custom .tooltip element in
 * app.js -- instant and reliable, unlike the native title bubble it used to
 * be, which had a ~1s delay and was easy to miss entirely. */
.has-tip { cursor: help; }
.has-tip > label, .has-tip .k, th.has-tip {
  text-decoration: underline dotted var(--muted);
  text-underline-offset: 3px;
}

/* Dataset checklist: grouped by instrument, one readable line per file. */
.dslist { display: flex; flex-wrap: wrap; gap: 12px; }
.dsgroup {
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px;
  min-width: 260px; flex: 1 1 260px; max-width: 420px;
}
.dsg-head { margin-bottom: 4px; }
.dsg-head label { display: flex; align-items: center; gap: 8px; cursor: pointer; }
.dsitem {
  display: flex; align-items: center; gap: 8px; padding: 3px 0 3px 4px;
  font-size: 12px; cursor: pointer; color: var(--text);
}
.dsitem input, .dsg-head input { accent-color: var(--accent); }
.dsitem.selected { color: var(--accent); font-weight: 600; }
.tooltip {
  position: fixed; z-index: 9999; max-width: 300px; pointer-events: none;
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 6px; padding: 7px 10px; font-size: 12px; line-height: 1.45;
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
  opacity: 0; transform: translateY(2px);
  transition: opacity .08s ease, transform .08s ease;
}
.tooltip.show { opacity: 1; transform: translateY(0); }

/* ---------- strategy tags ---------- */
.tagchip {
  display: inline-block; font-size: 10px; line-height: 16px;
  border: 1px solid var(--border); border-radius: 9px; padding: 0 7px;
  margin: 3px 4px 0 0; color: var(--muted); background: var(--panel-2);
  white-space: nowrap;
}

/* ---------- draggable table columns (Live tab) ---------- */
th[draggable="true"] { cursor: grab; }
th[draggable="true"]:active { cursor: grabbing; }
.draghandle { color: var(--muted); opacity: .55; user-select: none; }
.tagchip.clickable { cursor: pointer; }
.tagchip.clickable:hover { border-color: var(--accent); color: var(--text); }
.tagchip.active { border-color: var(--accent); color: var(--accent); }

/* ------------------------------------------------------------ quantstats */

/* The key-performance table reads as a set of short columns rather than one
   70-row list: auto-fill so it is four columns on a monitor and one on a
   phone, with no breakpoint to maintain. */
.qsgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 10px;
  margin-top: 10px;
}
.qsbox {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--bg);
  min-width: 0;
}
.qsbox > h4 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}
.qstable { width: 100%; font-family: var(--mono); font-size: 12px; }
.qstable td, .qstable th { padding: 3px 0; border: 0; }
.qstable th {
  text-align: left;
  color: var(--muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  padding-bottom: 5px;
}
.qstable td.k { color: var(--muted); white-space: nowrap; }
.qstable td.v { text-align: right; color: var(--text); font-variant-numeric: tabular-nums; }
.qstable td.v.pos { color: var(--green); }
.qstable td.v.neg { color: var(--red); }
.qstable tr:nth-child(even) td { background: rgba(255, 255, 255, .012); }
/* The year and drawdown tables are wide, not tall -- let them scroll sideways
   on a phone instead of squeezing the date columns into unreadability. */
.qsbox .qstable.wide { min-width: 380px; }
.qsbox:has(.qstable.wide) { overflow-x: auto; }

/* A tearsheet chart: its own titled block, so the grid of numbers above and
   the plots below read as one document rather than a wall of canvases. */
.qschart { margin-top: 14px; }
.qschart > h4 {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 10px;
}
.btn.small { padding: 2px 8px; font-size: 11px; line-height: 1.6; }

/* The run number leads each row: it is the handle you type back into the jump
   box, so it reads as an identifier rather than part of the label. */
.runno {
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11px;
  margin-right: 7px;
}
