:root {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #222220;
  --surface-3: #2a2a27;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-1-track: rgba(57, 135, 229, 0.20);
  --reference: #898781;
  --pos: #0ca30c;
  --neg: #e66767;
  --warning: #fab219;
  --critical: #d03b3b;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--page);
  color: var(--text-primary);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--series-1); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select { font: inherit; color: inherit; }
.num, td.n, th.n { font-variant-numeric: tabular-nums; }

/* layout */
.wrap { max-width: 1320px; margin: 0 auto; padding: 16px; }
header.top {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface-1);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; font-size: 16px; letter-spacing: 0.2px; }
.badge {
  display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px;
  font-size: 12px; border: 1px solid var(--border); color: var(--text-secondary); white-space: nowrap;
}
.badge.live { border-color: rgba(208, 59, 59, 0.6); color: var(--text-primary); }
.badge.live::before, .dot::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--critical); display: inline-block;
}
.dot.ok::before { background: var(--pos); }
.dot.bad::before { background: var(--critical); }
.dot.idle::before { background: var(--text-muted); }
.spacer { flex: 1; }
.top-actions { display: flex; flex-wrap: wrap; gap: 8px; }

nav.tabs { display: flex; gap: 4px; margin: 4px 0 16px; border-bottom: 1px solid var(--border); overflow-x: auto; }
nav.tabs button {
  background: none; border: 0; padding: 10px 14px; color: var(--text-muted); cursor: pointer;
  border-bottom: 2px solid transparent; white-space: nowrap;
}
nav.tabs button.on { color: var(--text-primary); border-bottom-color: var(--series-1); }
.tab { display: none; }
.tab.on { display: block; }

.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 16px; min-width: 0;
}
.card h2 { font-size: 15px; font-weight: 600; margin: 0 0 10px; }
.card h3 { font-size: 13px; font-weight: 600; margin: 14px 0 8px; color: var(--text-secondary); }
.card .sub { color: var(--text-muted); font-size: 12px; margin: -6px 0 10px; }
.row-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.muted { color: var(--text-muted); }
.sec { color: var(--text-secondary); }
.small { font-size: 12px; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.warn-text { color: var(--warning); }

/* buttons */
.btn {
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; color: var(--text-primary); white-space: nowrap;
}
.btn:hover { border-color: rgba(255, 255, 255, 0.25); }
.btn.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn.danger { border-color: rgba(208, 59, 59, 0.55); }
.btn.danger:hover { background: rgba(208, 59, 59, 0.15); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.small { padding: 4px 9px; font-size: 12px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; min-width: 0; }
.tile .label { color: var(--text-secondary); font-size: 12px; }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 2px; line-height: 1.2; overflow-wrap: anywhere; }
.tile .value.hero { font-size: 34px; }
.tile .delta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.tile.wide { grid-column: span 2; }
@media (max-width: 560px) { .tile.wide { grid-column: span 1; } .tile .value.hero { font-size: 28px; } }

/* meter */
.meter { height: 6px; border-radius: 3px; background: var(--series-1-track); overflow: hidden; margin-top: 8px; min-width: 60px; }
.meter > i { display: block; height: 100%; background: var(--series-1); border-radius: 3px; }
.meter.over > i { background: var(--pos); }

/* tables */
.tbl-wrap { overflow-x: auto; margin: 0 -4px; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 7px 8px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--text-muted); font-weight: 500; font-size: 12px; white-space: nowrap; }
td.n, th.n { text-align: right; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tr.total td { border-top: 1px solid var(--axis); font-weight: 600; }
.addr { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 12px; color: var(--text-secondary); }
.chip {
  display: inline-block; padding: 1px 7px; border-radius: 6px; background: var(--surface-3);
  font-size: 12px; margin: 1px 4px 1px 0; white-space: nowrap;
}
.chip.long::before { content: "▲ "; color: var(--text-secondary); }
.chip.short::before { content: "▼ "; color: var(--text-secondary); }
.stage { white-space: nowrap; }
.err { color: var(--neg); font-size: 12px; max-width: 280px; overflow-wrap: anywhere; }
.note { color: var(--text-muted); font-size: 12px; max-width: 260px; }

/* chart */
figure.chart { margin: 0; }
.chart-box { position: relative; width: 100%; }
.chart-box svg { display: block; width: 100%; height: auto; overflow: visible; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--text-secondary); margin: 0 0 8px; }
.legend .key { display: inline-block; width: 16px; height: 2px; vertical-align: middle; margin-right: 6px; }
.legend .key.a { background: var(--series-1); }
.legend .key.b { background: var(--reference); }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface-3); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 150px; display: none; z-index: 3;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}
.tooltip .t { color: var(--text-muted); margin-bottom: 4px; }
.tooltip .r { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.tooltip .r b { font-weight: 600; color: var(--text-primary); }
.tooltip .r span { color: var(--text-secondary); display: inline-flex; align-items: center; gap: 6px; }
.tooltip .r span i { display: inline-block; width: 12px; height: 2px; }
.empty { color: var(--text-muted); padding: 22px 0; text-align: center; }

/* forms */
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px 16px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label { color: var(--text-secondary); font-size: 12px; }
.field .hint { color: var(--text-muted); font-size: 11px; }
.field input[type=number], .field input[type=text], .field input[type=password], .field select {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; width: 100%;
}
.field input:focus, .field select:focus { outline: 1px solid var(--series-1); border-color: var(--series-1); }
.field textarea, input.proxy-in {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; width: 100%;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 12px; color: var(--text-primary);
}
input.proxy-in { min-width: 220px; }
.field textarea:focus, input.proxy-in:focus { outline: 1px solid var(--series-1); border-color: var(--series-1); }
.proxy-edit { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.ok-text { color: var(--pos); }
.check { display: flex; align-items: center; gap: 8px; }
.form-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; align-items: center; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px 14px; margin: 0 0 14px; min-width: 0; }
legend { color: var(--text-secondary); font-size: 12px; padding: 0 6px; }

.round { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: var(--surface-2); margin-bottom: 10px; }
.round .head { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: baseline; }
.round .head b { font-size: 14px; }
.round .sides { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 8px; }
@media (max-width: 560px) { .round .sides { grid-template-columns: 1fr; } }
.sched { display: flex; height: 6px; border-radius: 3px; overflow: hidden; margin-top: 8px; background: var(--surface-3); gap: 2px; }
.sched i { display: block; height: 100%; background: var(--series-1-track); }
.sched i.done { background: var(--series-1); }

.log { max-height: 560px; overflow-y: auto; }
.log .e { display: grid; grid-template-columns: 92px 1fr; gap: 10px; padding: 5px 0; border-bottom: 1px solid var(--grid); font-size: 13px; }
.log .e .ts { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.log .e.warn .msg::before { content: "⚠ "; color: var(--warning); }
.log .e.error .msg::before { content: "✖ "; color: var(--critical); }

.banner { border: 1px solid rgba(250, 178, 25, 0.45); background: rgba(250, 178, 25, 0.08); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px; }
.banner.red { border-color: rgba(208, 59, 59, 0.55); background: rgba(208, 59, 59, 0.08); }
.banner b { font-weight: 600; }
.toast {
  position: fixed; right: 16px; bottom: 16px; max-width: min(420px, calc(100vw - 32px)); background: var(--surface-3);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; display: none; z-index: 20;
}
.toast.err { border-color: rgba(208, 59, 59, 0.7); }
.loading { opacity: 0.6; }
body.busy .proxy-edit .btn, body.busy #proxyBulkSave, body.busy #proxyCheckAll { opacity: 0.45; pointer-events: none; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(360px, 100%); background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 22px; }
.login h1 { font-size: 18px; margin: 0 0 4px; }
.login p { color: var(--text-muted); margin: 0 0 16px; font-size: 13px; }
.login .btn { width: 100%; margin-top: 12px; padding: 9px; }
.login .msg { color: var(--neg); font-size: 13px; min-height: 18px; margin-top: 10px; }

@media (max-width: 720px) {
  .wrap { padding: 12px; }
  header.top { padding: 10px 12px; }
  .card { padding: 12px; }
}
