:root {
  color-scheme: light dark;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1b2330;
  --muted: #5b6676;
  --border: #d5dae2;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --series: #2f6fdd;
  --series-err: #d4432f;
  --grid: #e3e7ed;
  --ok: #1a7f37;
  --warn: #9a6700;
  --bad: #c62828;
  --focus: #0a58ca;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --surface: #1a212c;
    --text: #e6eaf0;
    --muted: #9aa5b6;
    --border: #2f3a4a;
    --accent: #4c8dff;
    --accent-text: #08111f;
    --series: #5b97ff;
    --series-err: #ff6b57;
    --grid: #2a3442;
    --ok: #4ac26b;
    --warn: #e3b341;
    --bad: #ff7b72;
    --focus: #79b8ff;
  }
}

* { box-sizing: border-box; }
html { font-family: var(--font); font-size: 16px; }
body { margin: 0; background: var(--bg); color: var(--text); line-height: 1.45; min-width: 320px; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 0.5rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: 0.5rem 1rem; z-index: 10; }
.skip:focus { left: 0; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* layout */
.topbar { display: flex; flex-wrap: wrap; gap: 0.75rem 1.5rem; align-items: center; padding: 0.75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.site-pick { display: flex; align-items: center; gap: 0.5rem; }
.userbar { margin-left: auto; display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.75rem; }
main { max-width: 1200px; margin: 0 auto; padding: 1rem; }
.footer { text-align: center; padding: 1.5rem 1rem; font-size: 0.85rem; color: var(--muted); }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem 1rem; margin-bottom: 0.75rem; }
.banner { padding: 0.6rem 0.8rem; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); margin: 0.5rem 0; }
.banner.error { border-color: var(--bad); color: var(--bad); }

/* controls */
button, input, select { font: inherit; color: inherit; }
button { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.8rem; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { border-color: var(--bad); color: var(--bad); }
button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.small { padding: 0.15rem 0.5rem; font-size: 0.85rem; }
input, select { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.6rem; max-width: 100%; }
input[type="checkbox"] { width: auto; }
.btn-group { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.inline-form { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 0.75rem; margin-bottom: 0.75rem; }
.stack { display: flex; flex-direction: column; gap: 0.4rem; max-width: 28rem; }
.stack label { font-weight: 600; margin-top: 0.4rem; }
.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
.form-error, .field-error { color: var(--bad); margin: 0.25rem 0 0; font-size: 0.9rem; }
.form-error[hidden], .field-error[hidden], [hidden] { display: none !important; }

.login-page { display: grid; place-items: center; min-height: 100vh; padding: 1rem; }
.login-box { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 1.5rem; width: min(24rem, 100%); }
.login-box h1 { margin-bottom: 1rem; }
.login-box input { width: 100%; }

/* status pill */
.pill { display: inline-block; padding: 0.1rem 0.6rem; border-radius: 999px; border: 1px solid var(--border); font-size: 0.85rem; }
.pill[data-state="live"] { color: var(--ok); border-color: var(--ok); }
.pill[data-state="reconnecting"], .pill[data-state="connecting"] { color: var(--warn); border-color: var(--warn); }
.pill[data-state="closed"] { color: var(--bad); border-color: var(--bad); }
.pill[data-state="live"]::before { content: "\25CF\00a0"; }

/* tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr)); gap: 0.6rem; margin-bottom: 0.75rem; }
.tile { border: 1px solid var(--border); border-radius: 6px; padding: 0.55rem 0.7rem; background: var(--bg); }
.tile .label { font-size: 0.8rem; color: var(--muted); }
.tile .value { font-size: 1.35rem; font-weight: 600; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.tile .sub { font-size: 0.8rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* charts */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; }
.chart .axis-text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-line { stroke: var(--border); stroke-width: 1; }
.chart .bar { fill: var(--series); }
.chart .bar-err { fill: var(--series-err); }
.chart .hit { fill: transparent; }
.chart .hit:hover, .chart .hit:focus-visible { fill: var(--grid); fill-opacity: 0.6; }
.chart .hit:focus { outline: none; }
.chart .hit:focus-visible { stroke: var(--focus); stroke-width: 2; }
.legend { display: flex; gap: 1rem; font-size: 0.85rem; color: var(--muted); margin-top: 0.25rem; }
.swatch { display: inline-block; width: 0.8rem; height: 0.8rem; border-radius: 2px; margin-right: 0.3rem; vertical-align: -1px; background: var(--series); }
.swatch.err { background: var(--series-err); }
.tooltip { position: absolute; pointer-events: none; z-index: 5; background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.6rem; font-size: 0.85rem; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25); white-space: nowrap; transform: translate(-50%, -100%); }
.tooltip div { font-variant-numeric: tabular-nums; }

/* tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.92rem; }
th, td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.key { overflow-wrap: anywhere; max-width: 32rem; font-family: var(--mono); font-size: 0.85rem; }
td.actions-cell { display: table-cell; }
td.actions-cell button { margin: 0 0.25rem 0.25rem 0; }

.badge { display: inline-block; font-size: 0.72rem; padding: 0 0.4rem; border-radius: 4px; border: 1px solid var(--border); color: var(--muted); margin-left: 0.4rem; font-family: var(--font); vertical-align: 1px; }
.badge.stale { color: var(--bad); border-color: var(--bad); }
.badge.ok { color: var(--ok); border-color: var(--ok); }

/* dialogs and one-time secrets */
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 1.25rem; width: min(30rem, calc(100% - 2rem)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
.secret-box { border: 2px solid var(--warn); border-radius: 8px; padding: 0.75rem 1rem; margin: 1rem 0; background: var(--surface); }
.secret-box .warning { color: var(--warn); font-weight: 600; }
.secret-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin: 0.4rem 0; }
.secret-row code { font-family: var(--mono); background: var(--bg); border: 1px solid var(--border); padding: 0.25rem 0.5rem; border-radius: 4px; overflow-wrap: anywhere; user-select: all; }
.secret-box h4 { margin: 1rem 0 0.25rem; }
.integration-block { border: 1px solid var(--border); border-radius: 6px; padding: 0.4rem 0.75rem; margin: 0.5rem 0; }
.integration-block summary { cursor: pointer; font-weight: 600; }
.integration-block pre { font-family: var(--mono); font-size: 0.85rem; background: var(--bg); border: 1px solid var(--border); border-radius: 4px; padding: 0.5rem; margin: 0.4rem 0; max-height: 24rem; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; tab-size: 4; }
.check-list { display: grid; gap: 0.3rem; margin: 0.5rem 0; max-height: 14rem; overflow-y: auto; }
.check-list label { display: flex; gap: 0.5rem; align-items: center; font-weight: normal; margin: 0; }
.site-form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 0.4rem 1rem; }
.site-form-grid label { display: block; font-weight: 600; margin-top: 0.4rem; }
.site-form-grid input { width: 100%; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0; }

@media (max-width: 480px) {
  main { padding: 0.5rem; }
  .panel { padding: 0.7rem; }
  .userbar { margin-left: 0; }
  .tiles { grid-template-columns: repeat(2, 1fr); }
}

@media (prefers-reduced-motion: no-preference) {
  button { transition: border-color 0.15s; }
}

section > h2 { margin: 0.5rem 0; }
.table-wrap { margin-bottom: 1rem; }
.tabs { margin-bottom: 0.75rem; }
