:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --border: rgba(11, 11, 11, 0.1);
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-text: #1c5cab;
  --accent-wash: rgba(42, 120, 214, 0.1);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-other: #c3c2b7;
  --good: #0ca30c;
  --good-text: #006300;
  --warn: #fab219;
  --warn-text: #8a5a00;
  --critical: #d03b3b;
  --critical-text: #b02a2a;
  --critical-wash: rgba(208, 59, 59, 0.08);
  --side-bg: #161615;
  --side-text: #c3c2b7;
  --side-active: #f0efec;
  --side-hover: rgba(255, 255, 255, 0.06);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #898781;
    --border: rgba(255, 255, 255, 0.1);
    --grid: #2c2c2a;
    --axis: #383835;
    --accent: #3987e5;
    --accent-text: #86b6ef;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-other: #5a5a56;
    --good-text: #0ca30c;
    --warn-text: #fab219;
    --critical-text: #e66767;
    --critical-wash: rgba(230, 103, 103, 0.1);
    --side-bg: #111110;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --grid: #2c2c2a;
  --axis: #383835;
  --accent: #3987e5;
  --accent-text: #86b6ef;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-other: #5a5a56;
  --good-text: #0ca30c;
  --warn-text: #fab219;
  --critical-text: #e66767;
  --critical-wash: rgba(230, 103, 103, 0.1);
  --side-bg: #111110;
  --border: rgba(255, 255, 255, 0.1);
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 15px; }
h3 { font-size: 13px; color: var(--text-2); }
p { margin: 0; }
a { color: var(--accent-text); }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.hint { color: var(--text-2); font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- buttons & inputs ---------- */

button, .btn {
  font: inherit;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  text-decoration: none;
}
button:hover { background: var(--surface-2); }
button:disabled { opacity: 0.5; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--critical-text); }
button.danger.solid { background: var(--critical); border-color: var(--critical); color: #fff; }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--surface-2); }
button.small { padding: 4px 9px; font-size: 13px; }
button.link { border: 0; background: none; padding: 0; color: var(--accent-text); font-weight: 400; }
button.link:hover { text-decoration: underline; background: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--axis);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); }
input.mono { font-family: var(--mono); font-size: 13px; }
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }
label.field { display: grid; gap: 5px; font-size: 13px; color: var(--text-2); font-weight: 500; }
label.field input, label.field select { width: 100%; }
.field-row { display: flex; gap: 8px; align-items: center; }
.field-row input { flex: 1; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px; }
@media (min-width: 1100px) { .form-grid .wide { grid-column: span 2; } }

/* ---------- login ---------- */

#login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 32px 28px;
  display: grid;
  gap: 18px;
}
.login-card .brand { color: var(--text); }
.login-card button.primary { justify-content: center; padding: 9px; }
.form-error {
  color: var(--critical-text);
  background: var(--critical-wash);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
}

/* ---------- shell ---------- */

#app-view { display: flex; min-height: 100vh; }
.sidebar {
  width: 236px;
  flex: none;
  background: var(--side-bg);
  color: var(--side-text);
  display: flex;
  flex-direction: column;
  padding: 18px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; color: var(--side-active); }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent);
  display: grid; place-items: center; color: #fff; flex: none;
}
.brand-name { font-weight: 650; font-size: 14px; line-height: 1.2; }
.brand-sub { font-size: 12px; color: var(--muted); }
.nav { display: grid; gap: 2px; margin-top: 26px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  color: var(--side-text); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--side-hover); color: var(--side-active); }
.nav a[aria-current="page"] { background: rgba(255, 255, 255, 0.1); color: var(--side-active); }
.nav svg { width: 18px; height: 18px; flex: none; opacity: 0.9; }
.side-foot { margin-top: auto; border-top: 1px solid rgba(255, 255, 255, 0.08); padding: 14px 8px 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.side-foot .who { font-size: 13px; color: var(--side-active); overflow: hidden; text-overflow: ellipsis; }
.side-foot button { background: transparent; color: var(--side-text); border-color: rgba(255, 255, 255, 0.14); }
.side-foot button:hover { background: var(--side-hover); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 19px; flex: 1; }
#menu-btn { display: none; }
#page { padding: 24px 28px 96px; display: grid; gap: 20px; max-width: 1280px; width: 100%; }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; font-weight: 600;
  padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-2); white-space: nowrap;
}
.badge.demo { background: var(--warn); border-color: var(--warn); color: #0b0b0b; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.up, .dot.good { background: var(--good); }
.dot.down, .dot.bad { background: var(--critical); }
.dot.trying, .dot.warn { background: var(--warn); }
.dot.disabled, .dot.unknown { background: var(--axis); }

/* ---------- cards ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: grid;
  gap: 14px;
  min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card-head > div:not(.toolbar) { display: grid; gap: 4px; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 20px; }
.grid-2.even { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }

.notice {
  border-radius: 8px; padding: 10px 12px; font-size: 13px;
  background: var(--surface-2); color: var(--text-2);
  display: flex; gap: 8px; align-items: flex-start;
}
.notice.warn { background: rgba(250, 178, 25, 0.14); color: var(--text); }
.notice.bad { background: var(--critical-wash); color: var(--critical-text); }
.notice svg { flex: none; width: 16px; height: 16px; margin-top: 2px; }

/* ---------- toolbar ---------- */

.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.toolbar .spacer { flex: 1; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 11px; border-radius: 7px; font-size: 13px; color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }

/* ---------- stat tiles ---------- */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px 18px; display: grid; gap: 4px;
}
.tile .label { font-size: 12.5px; color: var(--text-2); font-weight: 500; display: flex; align-items: center; gap: 6px; }
.tile .value { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.tile .sub { font-size: 12px; color: var(--muted); }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.swatch.s1 { background: var(--series-1); }
.swatch.s2 { background: var(--series-2); }
.swatch.so { background: var(--series-other); }

/* ---------- chart ---------- */

.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; overflow: visible; }
.chart-wrap text { font: 11px system-ui, -apple-system, "Segoe UI", sans-serif; fill: var(--muted); font-variant-numeric: tabular-nums; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 3;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
  padding: 8px 10px; font-size: 12.5px; min-width: 140px;
}
.tooltip .t-title { font-weight: 600; margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .t-row span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }
.hit:hover { fill: var(--text); fill-opacity: 0.05; }
.empty { color: var(--muted); padding: 28px 0; text-align: center; font-size: 13px; }
.gw-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.gw-list li { display: grid; grid-template-columns: 1fr auto auto; gap: 12px; align-items: center; font-size: 13px; }
details.data-table summary { cursor: pointer; color: var(--text-2); font-size: 13px; }

/* ---------- tables ---------- */

.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 0 10px 8px; white-space: nowrap; border-bottom: 1px solid var(--grid); }
td { padding: 9px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
th:first-child, td:first-child { padding-left: 0; }
th:last-child, td:last-child { padding-right: 0; }
td.actions { text-align: right; white-space: nowrap; }
td.actions button + button { margin-left: 4px; }
td input, td select { width: 100%; padding: 5px 8px; }
td input.w-port { width: 84px; }
td input.w-weight { width: 72px; }
.status { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }

/* gateway editor */
.gw-rows { display: grid; gap: 10px; }
.gw-row { border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; display: grid; gap: 10px; }
.gw-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; font-size: 13px; }
.gw-head .danger { margin-left: auto; }
.gw-toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }
.gw-fields { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.gw-fields .field { flex: 0 0 auto; }
.gw-fields .gw-ip { flex: 1 1 200px; }
.gw-fields .gw-port input { width: 96px; }
.gw-fields .gw-tr select { width: 92px; }
.gw-fields .gw-w input { width: 84px; }
.gw-add { border-top: 1px solid var(--grid); padding-top: 14px; display: grid; gap: 10px; }

/* ---------- lists ---------- */

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 16px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-family: var(--mono); font-size: 12.5px;
  background: var(--surface-2); border-radius: 7px; padding: 4px 6px 4px 10px;
}
.chip.plain { padding: 4px 10px; }
.chip button { border: 0; background: transparent; padding: 2px; color: var(--muted); border-radius: 5px; }
.chip button:hover { color: var(--critical-text); background: var(--critical-wash); }
.chip-btn { font-family: var(--mono); font-size: 12px; padding: 3px 9px; border-radius: 7px; }

.event-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.event { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--surface-2); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.event strong { font-variant-numeric: tabular-nums; font-size: 15px; }

.ani-hero { display: flex; align-items: center; gap: 28px; flex-wrap: wrap; }
.ani-number { font-family: var(--mono); font-size: 30px; font-weight: 600; letter-spacing: 0.01em; }
.ani-meta { display: flex; gap: 28px; flex-wrap: wrap; }
.ani-meta div { display: grid; }
.ani-meta strong { font-size: 18px; font-variant-numeric: tabular-nums; }
.ani-meta span { font-size: 12px; color: var(--muted); }
.pill-active { font-size: 11px; font-weight: 600; color: var(--good-text); display: inline-flex; align-items: center; gap: 5px; }

/* ---------- save bar ---------- */

.savebar {
  position: fixed; left: 236px; right: 0; bottom: 0; z-index: 6;
  display: flex; align-items: center; justify-content: flex-end; gap: 10px;
  padding: 12px 28px;
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.06);
}
.savebar .msg { margin-right: auto; color: var(--text-2); font-size: 13px; }

/* ---------- editor ---------- */

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--grid); }
.tabs button { border: 0; border-radius: 0; background: none; padding: 8px 12px; color: var(--text-2); border-bottom: 2px solid transparent; margin-bottom: -1px; font-family: var(--mono); font-size: 13px; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
textarea.code {
  width: 100%; min-height: 460px; resize: vertical;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.55;
  tab-size: 4; white-space: pre; overflow: auto;
  background: var(--surface-2); border-color: var(--border);
}
pre.output {
  margin: 0; padding: 12px; border-radius: 8px; max-height: 260px; overflow: auto;
  background: var(--surface-2); font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------- logs ---------- */

.logview {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  font-family: var(--mono); font-size: 12px; line-height: 1.6;
  height: calc(100vh - 250px); min-height: 360px; overflow: auto; padding: 8px 0;
}
.log-row { display: grid; grid-template-columns: 150px 52px 1fr; gap: 10px; padding: 1px 14px; }
.log-row:hover { background: var(--surface-2); }
.log-row time { color: var(--muted); white-space: nowrap; }
.log-row .lvl { font-weight: 600; font-size: 10.5px; letter-spacing: 0.04em; padding-top: 1px; color: var(--muted); }
.log-row.error { background: var(--critical-wash); }
.log-row.error .lvl { color: var(--critical-text); }
.log-row.warn .lvl { color: var(--warn-text); }
.log-row .txt { white-space: pre-wrap; overflow-wrap: anywhere; min-width: 0; }
.cid { font: inherit; border: 0; background: var(--accent-wash); color: var(--accent-text); border-radius: 4px; padding: 0 4px; }
.cid:hover { text-decoration: underline; background: var(--accent-wash); }

/* ---------- modal & toast ---------- */

.modal-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0, 0, 0, 0.45);
  display: grid; place-items: center; padding: 16px;
}
.modal {
  width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto;
  background: var(--surface); border-radius: 14px; border: 1px solid var(--border);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.3);
  padding: 22px; display: grid; gap: 14px;
}
.modal h2 { font-size: 17px; }
.modal .body { display: grid; gap: 10px; color: var(--text-2); font-size: 13.5px; }
.modal .body ul { margin: 0; padding-left: 18px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

#toasts { position: fixed; right: 16px; top: 16px; z-index: 30; display: grid; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast {
  background: var(--text); color: var(--bg);
  border-radius: 10px; padding: 10px 14px; font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  display: flex; gap: 8px; align-items: flex-start;
}
.toast.bad { background: var(--critical); color: #fff; }
.toast svg { width: 16px; height: 16px; flex: none; margin-top: 2px; }

.loading { color: var(--muted); padding: 40px 0; text-align: center; }

/* ---------- responsive ---------- */

@media (max-width: 960px) {
  .grid-2, .grid-2.even { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 760px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 15;
    transform: translateX(-100%); transition: transform 0.18s ease;
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open::after { content: ""; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 14; }
  #menu-btn { display: inline-flex; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; row-gap: 8px; }
  .topbar h1 { font-size: 17px; }
  #page { padding: 16px 16px 96px; }
  .savebar { left: 0; padding: 12px 16px; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .log-row { grid-template-columns: 1fr; gap: 0; padding: 4px 12px; }
  .logview { height: calc(100vh - 330px); }
  .tile .value { font-size: 24px; }
}
