:root {
  --bg: #f6f7f9; --card: #ffffff; --ink: #14181f; --muted: #5d6573; --line: #e2e5ea; --soft: #eef1f5;
  --accent: #b7791f; --accent-ink: #ffffff; --live: #7b8494; --good: #1f8a4c; --bad: #c0392b; --warn: #b7791f;
  --band: rgba(183, 121, 31, 0.16); --changed: #fff6e5; --focus: #2f6fdb;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --card: #171b21; --ink: #e8ebf0; --muted: #9aa3b2; --line: #2a3039; --soft: #1e232b;
    --accent: #e0a63c; --accent-ink: #16120a; --live: #8d97a8; --good: #43b77a; --bad: #e46a5c; --warn: #e0a63c;
    --band: rgba(224, 166, 60, 0.18); --changed: #2a2416; --focus: #6b9cff; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1216; --card: #171b21; --ink: #e8ebf0; --muted: #9aa3b2; --line: #2a3039; --soft: #1e232b;
  --accent: #e0a63c; --accent-ink: #16120a; --live: #8d97a8; --good: #43b77a; --bad: #e46a5c; --warn: #e0a63c;
  --band: rgba(224, 166, 60, 0.18); --changed: #2a2416; --focus: #6b9cff; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: hidden; background: var(--bg); color: var(--ink); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
@supports (overflow: clip) { html, body { overflow-x: clip; } }   /* hidden would break the sticky save bar */
.boot { padding: 40px 16px; color: var(--muted); text-align: center; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 8px 14px; }
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { background: var(--bad); color: #fff; border-color: var(--bad); font-weight: 600; }
button:disabled { opacity: 0.5; cursor: default; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
input[type="number"], input[type="text"], input[type="password"], select, textarea {
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; width: 100%;
}
.wrap { max-width: 1320px; margin: 0 auto; padding: 0 16px 48px; }
header.top { display: flex; align-items: center; gap: 12px; padding: 14px 0; flex-wrap: wrap; }
header.top h1 { font-size: 17px; margin: 0; letter-spacing: 0.01em; }
header.top .sp { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 10px 14px; color: var(--muted); white-space: nowrap; }
.tag-halt { font-style: normal; font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--bad); border: 1px solid var(--bad); border-radius: 999px; padding: 1px 6px; margin-left: 4px; white-space: nowrap; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.stats { word-break: break-word; }
.card { min-width: 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px; }
.card h2 { font-size: 15px; margin: 0 0 10px; }
.card h3 { font-size: 13px; margin: 16px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.status { display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: center; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-weight: 600; font-size: 12px; background: var(--soft); }
.pill.run { color: var(--good); } .pill.halt { color: var(--bad); } .pill.warn { color: var(--warn); }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.grid { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 16px; align-items: start; }
@media (max-width: 980px) { .grid { grid-template-columns: minmax(0, 1fr); } .status { grid-template-columns: minmax(0, 1fr); } }
/* Desktop with room: settings (left) and backtest (right) are two panes that scroll on their own, depending on where
   the mouse is. The header, tabs and bot status stay on screen. Phones, tablets and short windows keep one normal
   page scroll (both columns move together). */
@media (min-width: 981px) and (min-height: 640px) {
  .wrap.split { height: 100vh; height: 100dvh; display: flex; flex-direction: column; padding-bottom: 12px; }
  .wrap.split > header.top, .wrap.split > .tabs, .wrap.split > .card { flex: none; }
  .wrap.split > header.top { padding: 8px 0; }                 /* a little tighter, so the panes get the height */
  .wrap.split > .tabs { margin-bottom: 10px; }
  .wrap.split > .card { margin-bottom: 12px; padding: 12px 16px; }
  .wrap.split > .grid { flex: 1 1 auto; min-height: 0; grid-template-rows: minmax(0, 1fr); align-items: stretch; }
  .pane { min-height: 0; overflow-y: auto; overflow-x: auto; overscroll-behavior: contain; scrollbar-width: thin; padding: 2px 8px 2px 2px; }
  .pane:focus-visible { outline-offset: -2px; }
  .pane > .card:last-child { margin-bottom: 4px; }
}
fieldset { border: 0; margin: 0 0 6px; padding: 0; }
legend { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; padding: 12px 0 6px; }
.field { display: grid; grid-template-columns: minmax(0, 1fr) 128px; gap: 4px 12px; align-items: center; padding: 8px; border-radius: 8px; }
.field.changed { background: var(--changed); }
.field label { font-weight: 500; }
.field .help { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.field .help b { color: var(--ink); font-weight: 600; }
.field .ctl { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }
.field .ctl .unit { color: var(--muted); font-size: 12px; min-width: 26px; }
.switch { position: relative; width: 40px; height: 22px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 22px; background: var(--line); transition: background .15s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(18px); }
.switch input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }
.bar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.bar .sp { flex: 1; }
.opts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
.opts label { font-size: 12px; color: var(--muted); display: grid; gap: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: -4px 0 10px; }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 12px; }
.chip.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
label.compare { display: grid; gap: 4px; margin-bottom: 12px; }
/* Quick setups (presets): a grid of tappable cards that goes from several columns to one as the screen narrows */
.presets-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 14px 0 6px; }
.presets { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; margin-bottom: 4px; }
button.preset { display: grid; gap: 2px; align-content: center; text-align: left; width: 100%; min-height: 54px; padding: 10px 12px; border-radius: 10px; }
button.preset b { font-size: 14px; }
button.preset span { font-size: 12px; font-weight: 400; color: var(--muted); line-height: 1.35; }
button.preset.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--changed); }
button.preset.on b::after { content: " \2713"; color: var(--accent); }
.defline { margin: 0 0 2px; }

/* Undo / reset / save. Sticks to the bottom of the screen while there are unsaved edits. */
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; padding: 10px 0 2px; border-top: 1px solid var(--line); background: var(--card); }
.actions .sp { flex: 1; }
.actions.dirty { position: sticky; bottom: 0; z-index: 2; padding-bottom: calc(8px + env(safe-area-inset-bottom)); box-shadow: 0 -6px 12px -8px rgba(16, 24, 40, 0.25); }

@media (max-width: 560px) {
  .opts { grid-template-columns: minmax(0, 1fr); } .field { grid-template-columns: minmax(0, 1fr); }
  .field .ctl { justify-content: flex-start; } .field .ctl input { max-width: 160px; }
  .kv { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .presets { grid-template-columns: minmax(0, 1fr); }
  .actions .sp { display: none; }
  .actions button { flex: 1 1 calc(50% - 4px); min-height: 44px; }
  .actions .save { flex-basis: 100%; order: -1; }   /* Save on its own row, the two undo buttons side by side below */
  .tabs { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 0; overflow: visible; }
  .tabs button { white-space: normal; text-align: center; line-height: 1.25; padding: 8px 4px; min-height: 48px; font-size: 13px; }
  .tag-halt { display: inline-block; margin: 2px 0 0; }
}
.stats { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stats th, .stats td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: right; }
.stats th:first-child, .stats td:first-child { text-align: left; color: var(--muted); font-weight: 500; }
.stats thead th { font-size: 12px; color: var(--muted); font-weight: 600; }
.stats .edit { color: var(--accent); font-weight: 600; }
.better, .stats td.better { color: var(--good); } .worse, .stats td.worse { color: var(--bad); }
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .grid line { stroke: var(--line); }
.chart .live { stroke: var(--live); fill: none; stroke-width: 1.5; }
.chart .edit { stroke: var(--accent); fill: none; stroke-width: 2; }
.chart .band { fill: var(--band); stroke: none; }
.chart .med { stroke: var(--accent); fill: none; stroke-width: 2; }
.chart .med-live { stroke: var(--live); fill: none; stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart .start { stroke: var(--muted); stroke-dasharray: 2 3; }
.legend { display: flex; gap: 6px 16px; font-size: 12px; color: var(--muted); margin: 4px 0 8px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 18px; height: 0; flex: none; }
.legend .k-live { border-top: 2px solid var(--live); }
.legend .k-edit { border-top: 3px solid var(--accent); }
.legend .k-live-dash { border-top: 2px dashed var(--live); }
.legend .k-start { border-top: 2px dotted var(--muted); }
.legend .k-band { height: 12px; background: var(--band); border-radius: 3px; }
.legend .k-dd-live, .legend .k-dd-edit { width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--bad); }
.legend .k-dd-live { background: var(--card); } .legend .k-dd-edit { background: var(--bad); }
.chart .dd-live { fill: var(--card); stroke: var(--bad); stroke-width: 2; }
.chart .dd-edit { fill: var(--bad); stroke: var(--card); stroke-width: 1.5; }
.chart .hline { stroke: var(--muted); stroke-dasharray: 3 3; }
.chart .latest { stroke: var(--ink); fill: none; stroke-width: 1.6; }
.chart .join { stroke: var(--focus); stroke-width: 1.5; stroke-dasharray: 6 4; opacity: 0.8; }
.legend .k-latest { border-top: 2px solid var(--ink); }
.legend .k-join { width: 0; height: 12px; border-left: 2px dashed var(--focus); }
.chart .hdot-live { fill: var(--live); } .chart .hdot-edit { fill: var(--accent); }
.chart .hbox { fill: var(--card); stroke: var(--line); }
.chart .htext { fill: var(--ink); font-size: 12px; } .chart .htext .hdate { font-weight: 600; }
.chart .hit { cursor: crosshair; }
.note { font-size: 12px; color: var(--muted); background: var(--soft); border-radius: 8px; padding: 8px 10px; margin-top: 10px; }
.warnbox { font-size: 13px; background: var(--changed); border: 1px solid var(--warn); border-radius: 8px; padding: 8px 10px; margin: 10px 0; }
.err { color: var(--bad); font-size: 13px; margin-top: 8px; }
.log { list-style: none; margin: 0; padding: 0; max-height: 360px; overflow: auto; }
.log.tall { max-height: none; }
.log li { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.log li .when { color: var(--muted); font-size: 12px; }
.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: r 0.8s linear infinite; vertical-align: -2px; }
@keyframes r { to { transform: rotate(360deg); } }
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10; }
.modal[hidden] { display: none; }
.dialog { background: var(--card); border-radius: 12px; max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; padding: 18px; border: 1px solid var(--line); }
.dialog h2 { margin: 0 0 10px; font-size: 16px; }
.dialog ul { padding-left: 18px; }
.dialog .bar { margin-top: 14px; justify-content: flex-end; }
.login { max-width: 360px; margin: 10vh auto; }
.login label { display: grid; gap: 4px; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px 16px; font-size: 13px; }
.kv div span { color: var(--muted); display: block; font-size: 12px; }
