/*
 * The backtest launcher.
 *
 * Two pickers, one button, one bar. Everything the block shows before a run is a *choice*;
 * everything after it is a number — so the pickers sit on one dense row and the stats read
 * as a strip, in the same tabular mono as the ladder and the HUD.
 *
 * The bar is percent, never a spinner. A run over fifteen minutes of tape takes a couple of
 * seconds, and the only question during those seconds is "is it moving" — which a spinner
 * cannot answer and a percentage answers by existing.
 */
.backtest {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-block-size: 0;
  overflow-y: auto;
}

.backtest__pickers {
  display: grid;
  grid-template-columns: 1.4fr 1fr auto;
  gap: var(--sp-1);
  align-items: center;
}

.backtest__pickers select {
  min-inline-size: 0;
}

.chip--go {
  border-color: var(--green);
  color: var(--green);
}

.backtest__bar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-1);
  padding: 0 var(--sp-1);
  block-size: 1.25rem;
  border: 1px solid var(--line);
  background: var(--bg-inset);
  overflow: hidden;
}

/*
 * The fill is behind the text rather than beside it: a bar with the numbers outside it
 * costs a second row of height, and this block has to sit in the same cell as every other.
 */
.backtest__fill {
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  background: color-mix(in srgb, var(--green) 22%, transparent);
  transition: inline-size 80ms linear;
}

.backtest__pct,
.backtest__ticks {
  position: relative;
  font-variant-numeric: tabular-nums;
}

.backtest__error {
  color: var(--orange);
}

.backtest__stats {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-1) var(--sp-2);
  font-variant-numeric: tabular-nums;
}

.backtest__stat {
  color: var(--ink-muted);
  font-size: var(--fs-0);
}

.backtest__stat b {
  color: var(--green);
  font-weight: 600;
}

/*
 * The headline tiles. Equal cells on one row, so the eye compares magnitudes rather than
 * reading labels — the same reason every block in the grid is the same size.
 */
.bt-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(4.5rem, 1fr));
  gap: var(--sp-1);
}

.bt-tile {
  display: flex;
  flex-direction: column;
  padding: var(--sp-1);
  border: 1px solid var(--line);
  background: var(--bg-inset);
  font-variant-numeric: tabular-nums;
}

.bt-tile__value {
  color: var(--ink-strong);
  font-weight: 600;
}

.bt-tile__label {
  color: var(--ink-muted);
  font-size: var(--fs-0);
}

.bt-tile--up .bt-tile__value { color: var(--green); }
.bt-tile--down .bt-tile__value { color: var(--orange); }

.bt-chart {
  inline-size: 100%;
  block-size: 5rem;
  min-block-size: 0;
}

.backtest__out {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--sp-1);
}

/*
 * The sweep table.
 *
 * One row per combo, coloured by where its P&L sits between the worst and the best. The
 * point is that a winner should be visible without reading a number — a strategy that
 * earns at exactly one setting and nowhere near it is a coincidence with a parameter
 * attached, and a gradient shows that instantly where a sorted column does not.
 */
.sweep {
  display: flex;
  flex-direction: column;
  min-block-size: 0;
  font-variant-numeric: tabular-nums;
  font-size: var(--fs-0);
}

.sweep__head,
.sweep__row {
  display: grid;
  grid-template-columns: 1fr 1fr 0.5fr 0.6fr 1fr 1.6fr;
  gap: var(--sp-1);
  align-items: baseline;
}

.sweep__col {
  padding: 0;
  border: 0;
  background: none;
  color: var(--ink-muted);
  font: inherit;
  text-align: start;
  cursor: pointer;
}

.sweep__col:hover {
  color: var(--green);
}

.sweep__rows {
  overflow-y: auto;
  min-block-size: 0;
}

.sweep__row {
  padding-inline: var(--sp-1);
  /* The gradient is mixed here rather than computed in JS: the tokens already know what
     green and orange are in each theme, and a JS colour would be wrong in one of them. */
  background: linear-gradient(
    to right,
    color-mix(in srgb, var(--orange) 14%, transparent),
    color-mix(in srgb, var(--green) 18%, transparent) var(--heat, 50%),
    transparent var(--heat, 50%)
  );
  cursor: pointer;
}

.sweep__row.is-best {
  outline: 1px solid var(--green);
}

.sweep__cell {
  min-inline-size: 0;
}

/*
 * The run archive and the comparison.
 *
 * Rows are buttons because every one of them is a pin, and a pinned row is outlined rather
 * than filled — a fill would fight the sweep table's heat gradient two blocks up, and two
 * different meanings for "coloured background" in one block is one too many.
 */
.runs-archive {
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  min-block-size: 0;
  font-size: var(--fs-0);
}

.runs-archive__rows {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  max-block-size: 6rem;
  min-block-size: 0;
}

.runs-archive__row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: var(--sp-1);
  padding: 2px var(--sp-1);
  border: 1px solid transparent;
  background: none;
  color: var(--ink);
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: start;
  cursor: pointer;
}

.runs-archive__row:hover {
  border-color: var(--line);
}

.runs-archive__row.is-pinned {
  border-color: var(--green);
  color: var(--green);
}

.cmp {
  display: flex;
  flex-direction: column;
  font-variant-numeric: tabular-nums;
}

.cmp__row {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr 1fr;
  gap: var(--sp-1);
  align-items: baseline;
}

.cmp__delta--up { color: var(--green); }
.cmp__delta--down { color: var(--orange); }
.cmp__delta--flat { color: var(--ink-muted); }
