:root {
  color-scheme: light;
  --surface-0: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #efeeea;
  --border: #dddcd6;
  --grid: #e6e5e0;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --accent: #2a78d6;
  --pos: #2a78d6;      /* diverging pole: profit */
  --neg: #e34948;      /* diverging pole: loss */
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --good-text: #006300;
  --critical-text: #b42323;
  --warning-text: #8a5a00;
  --focus: #2a78d6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border: #353532;
    --grid: #2c2c2a;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #9a998f;
    --accent: #3987e5;
    --pos: #3987e5;
    --neg: #e66767;
    --good-text: #4ccf4c;
    --critical-text: #f08a8a;
    --warning-text: #fab219;
    --focus: #6da7ec;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #242422;
  --border: #353532;
  --grid: #2c2c2a;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #9a998f;
  --accent: #3987e5;
  --pos: #3987e5;
  --neg: #e66767;
  --good-text: #4ccf4c;
  --critical-text: #f08a8a;
  --warning-text: #fab219;
  --focus: #6da7ec;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--surface-0);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.pos { color: var(--good-text); }
.neg { color: var(--critical-text); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.nowrap { white-space: nowrap; }

header.top {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface-1); border-bottom: 1px solid var(--border);
}
/* the same width as main, so the menu starts where the page content starts */
header.top .top-inner {
  max-width: 1400px; margin: 0 auto; padding: 10px 16px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px;
}
header.top .brand { font-weight: 650; margin-right: 8px; }
nav.tabs { display: flex; flex-wrap: wrap; gap: 2px; }
nav.tabs a {
  padding: 6px 10px; border-radius: 6px; color: var(--text-secondary);
}
nav.tabs a.active { background: var(--surface-2); color: var(--text-primary); font-weight: 600; text-decoration: none; }
header.top .right { margin-left: auto; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

main { max-width: 1400px; margin: 0 auto; padding: 16px; }
main > * + * { margin-top: 12px; }
main > .row + *, main > h1 + * { margin-top: 0; }
h1 { font-size: 20px; margin: 4px 0 12px; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 14px; margin: 0; }
.card > .section { border-top: 1px solid var(--border); padding-top: 10px; margin-top: 12px; }
.card > .section > .row { margin-bottom: 6px; }
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.grid { display: grid; gap: 12px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; box-shadow: var(--shadow); min-width: 0;
}
.card + .card, .section + .section { margin-top: 12px; }
.grid > .card + .card { margin-top: 0; }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; margin-bottom: 12px; }
.toolbar label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--text-secondary); }

input, select, textarea, button {
  font: inherit; color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 9px;
}
input[type=checkbox] { width: 16px; height: 16px; padding: 0; accent-color: var(--accent); }
textarea { width: 100%; min-height: 60px; }
button { cursor: pointer; background: var(--surface-2); }
button:hover { border-color: var(--text-muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { background: var(--critical); border-color: var(--critical); color: #fff; }
button:disabled { opacity: .5; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--surface-1); padding: 5px 10px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--surface-2); font-weight: 600; }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.tile .label { font-size: 12px; color: var(--text-secondary); }
.tile .value { font-size: 24px; font-weight: 650; margin-top: 2px; }
.tile .sub { font-size: 12px; color: var(--text-muted); }

/* status pills: icon + label, never colour alone */
.pill { display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px; border-radius: 999px; font-size: 12px; border: 1px solid var(--border); white-space: nowrap; }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); }
.pill.good::before { background: var(--good); }
.pill.warning::before { background: var(--warning); }
.pill.critical::before { background: var(--critical); }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--text-secondary); position: sticky; top: 0; background: var(--surface-1); cursor: default; }
th.sortable { cursor: pointer; }
td.num, th.num { text-align: right; }
td.wrap-any { overflow-wrap: anywhere; min-width: 180px; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }

/* charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text { fill: var(--text-muted); font-size: 11px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .zero { stroke: var(--text-muted); stroke-width: 1; }
.chart .bar-pos { fill: var(--pos); }
.chart .bar-neg { fill: var(--neg); }
.chart .line { fill: none; stroke: var(--pos); stroke-width: 2; stroke-linejoin: round; }
.chart .line-thin { fill: none; stroke: var(--text-muted); stroke-width: 1; stroke-linejoin: round; opacity: .7; }
.chart .bar-attempts { fill: var(--text-muted); opacity: .35; }
.chart .bar-entered { fill: var(--pos); }
.chart .area { fill: var(--pos); opacity: .08; }
.chart .cross { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .dot { fill: var(--pos); stroke: var(--surface-1); stroke-width: 2; }
.chart .threshold { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 4 3; }
.chart .hit { fill: transparent; }
.chart .bar-hover { fill: var(--text-primary); opacity: .06; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-secondary); margin: 2px 0 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.legend i.pos { background: var(--pos); }
.legend i.neg { background: var(--neg); }
.legend i.line-main { background: var(--pos); height: 2px; vertical-align: 3px; border-radius: 0; }
.legend i.line-thin { background: var(--text-muted); height: 1px; vertical-align: 3px; border-radius: 0; }
.legend i.attempts { background: var(--text-muted); opacity: .35; }
.legend i.entered { background: var(--pos); }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 9px; font-size: 12px; box-shadow: 0 2px 8px rgba(0,0,0,.12); white-space: nowrap;
}
.tooltip b { display: block; margin-bottom: 2px; }

/* settings form */
.settings-group { margin-bottom: 14px; }
.field { display: grid; grid-template-columns: minmax(200px, 320px) minmax(160px, 1fr); gap: 4px 16px; padding: 7px 0; border-bottom: 1px solid var(--grid); align-items: start; }
.field .help { grid-column: 2; font-size: 12px; color: var(--text-muted); }
.field .control { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.field .unit { color: var(--text-muted); font-size: 12px; }
.field.changed > label { font-weight: 600; }
.field.changed > label::after { content: " •"; color: var(--accent); }
.windows { display: flex; flex-direction: column; gap: 6px; }
.windows .wrow { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.windows .wrow input { width: 90px; }
.windows .head { font-size: 12px; color: var(--text-muted); }
.windows .head span { display: inline-block; width: 90px; margin-right: 6px; }
.checks { display: flex; flex-wrap: wrap; gap: 10px; }
.checks label { display: inline-flex; gap: 5px; align-items: center; }

.banner { padding: 9px 12px; border-radius: 8px; margin-bottom: 12px; border: 1px solid var(--border); background: var(--surface-1); }
.banner.error { border-color: var(--critical); }
.banner.ok { border-color: var(--good); }
.banner.warning { border-color: var(--warning); }
.banner b.title { display: block; margin-bottom: 2px; }
/* bot is down: shown under the header on every page */
.page-alert { max-width: 1400px; margin: 12px auto 0; padding: 0 16px; }
.page-alert .banner { margin: 0; border-width: 1px 1px 1px 4px; }
a.pill { color: var(--text-primary); text-decoration: none; }
a.pill:empty { display: none; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 3px 14px; }
.kv dt { color: var(--text-secondary); }
.kv dd { margin: 0; }

.login { max-width: 340px; margin: 12vh auto; }
.login form { display: flex; flex-direction: column; gap: 10px; }
.login input { width: 100%; }

#toast { position: fixed; bottom: 16px; right: 16px; z-index: 20; max-width: 420px; }

@media (max-width: 640px) {
  main { padding: 12px 16px; }
  .grid.cols-2 { grid-template-columns: 1fr; }
  .field { grid-template-columns: 1fr; }
  .field .help { grid-column: 1; }
  header.top .right { margin-left: 0; }
}
@media (forced-colors: active) {
  .chart .bar-pos, .chart .bar-neg { fill: CanvasText; }
  .chart .bar-neg { fill: GrayText; }
}
