:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f0f3f6;
  --border: #dde3e9;
  --text: #17202a;
  --muted: #5f6d7a;
  --accent: #146c94;
  --accent-text: #ffffff;
  --accent-soft: #e3f0f6;
  --ok: #1f8a4c;
  --ok-soft: #e2f4ea;
  --warn: #a86400;
  --warn-soft: #fdf1dc;
  --danger: #c0352b;
  --danger-soft: #fbe6e4;
  --debug: #7a3fc2;
  --debug-soft: #f0e8fb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 8px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1419;
    --surface: #171e26;
    --surface-2: #1d2630;
    --border: #2b3642;
    --text: #e4e9ee;
    --muted: #93a1ae;
    --accent: #3aa0d0;
    --accent-text: #08131a;
    --accent-soft: #15303f;
    --ok: #4cc27f;
    --ok-soft: #133022;
    --warn: #e8a640;
    --warn-soft: #36290f;
    --danger: #ef6b60;
    --danger-soft: #3a1a18;
    --debug: #b48af0;
    --debug-soft: #2b1f40;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 16px 0 8px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 54px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; color: var(--text); font-size: 15px; }
.brand:hover { text-decoration: none; }
#nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
#nav a {
  padding: 7px 12px; border-radius: 6px; color: var(--muted); font-weight: 550; white-space: nowrap;
}
#nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
#nav a.active { background: var(--accent-soft); color: var(--accent); }
.count {
  display: inline-block; min-width: 18px; padding: 0 6px; margin-left: 4px;
  border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.user { display: flex; align-items: center; gap: 8px; color: var(--muted); white-space: nowrap; }

main { max-width: 1320px; margin: 0 auto; padding: 22px 20px 60px; }

/* ---------- layout pieces ---------- */
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.crumbs { color: var(--muted); font-size: 13px; margin-bottom: 6px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-head h2 { margin: 0; }
.card-head .spacer { flex: 1; }
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
dl.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { white-space: nowrap; text-align: right; }

/* ---------- badges & dots ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px; border-radius: 10px; font-size: 11.5px; font-weight: 650; letter-spacing: .02em; white-space: nowrap;
  background: var(--surface-2); color: var(--muted);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.debug { background: var(--debug-soft); color: var(--debug); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.dot.off { background: #9aa5b1; }
.status { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; }
.sev-critical, .sev-error { color: var(--danger); font-weight: 600; }
.sev-warning { color: var(--warn); font-weight: 600; }
.sev-info, .sev-debug { color: var(--muted); }

/* ---------- buttons & forms ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  padding: 7px 14px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.debug { background: var(--debug); border-color: var(--debug); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 4px 9px; font-size: 12.5px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; min-width: 0;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); }
input.mono, textarea.mono { font-family: var(--mono); font-size: 12.5px; }
textarea { width: 100%; resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--muted); font-weight: 550; }
label.inline { flex-direction: row; align-items: center; gap: 7px; color: var(--text); font-weight: 450; font-size: 13.5px; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.form-row > label { flex: 1 1 160px; }
.form-row > label.narrow { flex: 0 1 110px; }
.hint { font-size: 12px; color: var(--muted); margin: 4px 0 0; }
.error { color: var(--danger); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button {
  padding: 9px 14px; border: 0; border-bottom: 2px solid transparent; background: none;
  font: inherit; font-weight: 550; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- config editor ---------- */
.item {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; background: var(--surface);
}
.item-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.item-head strong { flex: 1; }
.sub { margin-top: 10px; padding: 10px; background: var(--surface-2); border-radius: 6px; }
.sub table th { background: transparent; }
.sub table td, .sub table th { padding: 5px 6px; border-bottom: 0; }
.sub table input, .sub table select { width: 100%; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.problems { background: var(--danger-soft); color: var(--danger); border-radius: 6px; padding: 10px 14px; margin-bottom: 12px; }
.problems ul { margin: 6px 0 0; padding-left: 18px; }
.debug-note { background: var(--debug-soft); color: var(--debug); border-radius: 6px; padding: 8px 12px; font-size: 12.5px; }

/* ---------- mode panel ---------- */
.mode-panel { border-left: 4px solid var(--ok); }
.mode-panel.debug { border-left-color: var(--debug); }
.mode-big { font-size: 18px; font-weight: 700; }
.warnbox { background: var(--warn-soft); color: var(--warn); border-radius: 6px; padding: 10px 12px; margin: 10px 0; }

/* ---------- charts ---------- */
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 12px; }
.chart { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px 6px; background: var(--surface); position: relative; }
.chart-title { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; font-weight: 600; margin-bottom: 4px; }
.chart-title .val { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 500; }
.chart canvas { width: 100%; height: 130px; display: block; }
.metric-picker { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.metric-picker button {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted); border-radius: 14px;
  padding: 3px 10px; font: inherit; font-size: 12px; cursor: pointer;
}
.metric-picker button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
pre.context {
  margin: 6px 0 0; padding: 8px 10px; background: var(--surface-2); border-radius: 6px;
  font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 360px; overflow: auto;
}
pre.context .hit { background: var(--warn-soft); color: var(--warn); font-weight: 600; }

/* ---------- dialogs & toasts ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25); width: min(520px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(10, 16, 22, .45); }
dialog form { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
dialog h2 { margin: 0; font-size: 17px; }
dialog .btn-row { justify-content: flex-end; }
dialog.login form { gap: 12px; }
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow);
  max-width: 420px; font-size: 13px;
}
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 720px) {
  .topbar { gap: 10px; padding: 0 12px; }
  .brand span, .user span { display: none; }
  main { padding: 16px 16px 40px; }
  .grid.cols-2 { grid-template-columns: 1fr; }
  .charts { grid-template-columns: 1fr; }
}

pre.diff .add { background: var(--ok-soft); color: var(--ok); }
pre.diff .del { background: var(--danger-soft); color: var(--danger); }
pre.diff .ctx { color: var(--muted); }

/* ---------- consoles & timeline ---------- */
.console-bar { width: 100%; height: 16px; display: block; }
.console-bar .bg { fill: var(--ok-soft); }
.console-bar .ep { fill: var(--warn); }
.console-bar .ep.usb { fill: var(--accent); }
.console-bar .ep.intentional { fill: var(--muted); opacity: .5; }
.console-bar .ep.stuck { fill: var(--danger); }
table.sub { width: 100%; background: var(--surface-2); }
.timeline-charts { display: grid; grid-template-columns: 1fr; gap: 8px; }
.timeline-charts .chart canvas { height: 92px; }
tr.focus-row td { background: var(--danger-soft); }
