:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --border-strong: rgba(11, 11, 11, 0.18);
  --series-1: #2a78d6;
  --accent: #256abf;
  --accent-ink: #ffffff;
  --accent-wash: rgba(42, 120, 214, 0.1);
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --critical-ink: #b42f2f;
  --off: #a8a7a1;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 8px 24px rgba(11, 11, 11, 0.06);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.18);
    --series-1: #3987e5;
    --accent: #3987e5;
    --accent-ink: #0b0b0b;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --good-ink: #0ca30c;
    --critical-ink: #e66767;
    --off: #6b6a65;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --border-strong: rgba(255, 255, 255, 0.18);
  --series-1: #3987e5;
  --accent: #3987e5;
  --accent-ink: #0b0b0b;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --good-ink: #0ca30c;
  --critical-ink: #e66767;
  --off: #6b6a65;
  --shadow: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 15px; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.secondary { color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

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

/* ---------- Shell ---------- */

.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 4px;
  padding: 20px 14px;
  border-right: 1px solid var(--border);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 20px; font-weight: 650; font-size: 15px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
}
.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-link.active { background: var(--accent-wash); color: var(--text); }
.nav-link svg, .btn svg { width: 16px; height: 16px; flex: none; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 8px; padding: 12px 10px 0; border-top: 1px solid var(--border); }
.who { display: flex; flex-direction: column; line-height: 1.3; }
.who strong { font-weight: 600; }
.sidebar-actions { display: flex; gap: 6px; }

.main { padding: 28px 32px 48px; min-width: 0; max-width: 1360px; width: 100%; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .sub { margin-top: 4px; color: var(--text-2); }
.crumb { display: inline-flex; align-items: center; gap: 4px; margin-bottom: 8px; color: var(--text-2); font-size: 13px; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Controls ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: 500 13px var(--font); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical-ink); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.icon { width: 34px; padding: 0; }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12.5px; }

input, select, textarea {
  height: 34px; padding: 0 10px;
  border: 1px solid var(--border-strong); border-radius: 8px;
  background: var(--surface); color: var(--text);
  font: 14px var(--font);
}
textarea { height: auto; padding: 8px 10px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-weight: 500; font-size: 13px; }
.field .hint { font-size: 12px; color: var(--muted); }

.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .grow { flex: 1; }
.toolbar input[type="search"] { min-width: 220px; }

.segmented { display: inline-flex; padding: 3px; gap: 2px; border-radius: 9px; background: var(--surface-2); border: 1px solid var(--border); }
.segmented button {
  height: 28px; padding: 0 12px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text-2); font: 500 13px var(--font); cursor: pointer;
}
.segmented button:hover { color: var(--text); }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgba(0, 0, 0, 0.06); }

.check { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: 13px; cursor: pointer; user-select: none; }
.check input { height: auto; accent-color: var(--accent); }

/* ---------- Cards, tiles ---------- */

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 0; }
.card-body { padding: 14px 16px 16px; }
.section { margin-top: 28px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.tile { padding: 14px 16px; }
.tile-label { color: var(--text-2); font-size: 13px; }
.tile-value { margin-top: 4px; font-size: 26px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.2; }
.tile-value .unit { margin-left: 4px; font-size: 14px; font-weight: 500; color: var(--text-2); }
.tile-foot { margin-top: 4px; font-size: 12.5px; color: var(--muted); }

.info-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px 24px; }
.info-grid dt { color: var(--muted); font-size: 12.5px; }
.info-grid dd { margin: 2px 0 0; font-weight: 500; overflow-wrap: anywhere; }

/* ---------- Status ---------- */

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  height: 22px; padding: 0 8px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
  font-size: 12px; font-weight: 500; color: var(--text); white-space: nowrap;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 2px; background: var(--off); }
.badge.active::before { border-radius: 50%; background: var(--good); }
.badge.disabled::before { background: var(--warning); }
.badge.revoked::before { width: 8px; height: 2px; border-radius: 1px; background: var(--critical); }

.conn { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; box-sizing: border-box; border: 2px solid var(--off); }
.conn.online::before { border-color: var(--good); background: var(--good); }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; vertical-align: middle; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; background: var(--surface); color: var(--muted); font-weight: 500; font-size: 12.5px; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
td.r, th.r { text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.cell-title { font-weight: 550; }
.cell-sub { font-size: 12.5px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.chip { white-space: nowrap; }
.chip .k { color: var(--muted); margin-right: 4px; }
.empty { padding: 36px 16px; text-align: center; color: var(--text-2); }
.empty strong { display: block; color: var(--text); margin-bottom: 4px; }
.table-foot { display: flex; justify-content: center; padding: 12px; border-top: 1px solid var(--border); }

/* ---------- Charts ---------- */

.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 460px), 1fr)); gap: 12px; }
.chart-card .card-head h3 { font-size: 14px; font-weight: 600; }
.chart-card .card-head .now { font-size: 13px; color: var(--text-2); }
.chart-card .card-head .now strong { color: var(--text); font-size: 15px; }
.chart-sub { padding: 2px 16px 0; font-size: 12.5px; color: var(--muted); }
.chart { position: relative; height: 220px; margin: 8px 8px 10px 4px; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; touch-action: pan-y; }
.chart:focus-visible { outline-offset: 0; border-radius: 6px; }
.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .band { fill: var(--series-1); opacity: 0.12; }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .crosshair { stroke: var(--text-2); stroke-width: 1; shape-rendering: crispEdges; opacity: 0.6; }
.chart .gap-mark { fill: var(--surface-2); }
.chart.loading { opacity: 0.45; transition: opacity 0.15s; }
.chart-empty { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: 13px; text-align: center; }

.tooltip {
  position: absolute; z-index: 5; pointer-events: none;
  min-width: 150px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.14);
  font-size: 12.5px; line-height: 1.45;
}
.tooltip .t { color: var(--muted); margin-bottom: 4px; }
.tooltip .v { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; color: var(--text); }
.tooltip .v::before { content: ""; width: 12px; height: 2px; border-radius: 1px; background: var(--series-1); }
.tooltip .r { color: var(--text-2); font-variant-numeric: tabular-nums; }

/* ---------- Login ---------- */

.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; padding: 28px; }
.login-card .brand { padding: 0 0 18px; }
.login-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.login-card .btn { height: 38px; margin-top: 4px; }
.form-error { padding: 8px 10px; border-radius: 8px; background: rgba(208, 59, 59, 0.1); color: var(--critical-ink); font-size: 13px; }
.notice { padding: 8px 10px; border-radius: 8px; background: var(--surface-2); color: var(--text-2); font-size: 13px; }

/* ---------- Dialog ---------- */

dialog {
  width: min(520px, calc(100vw - 32px)); padding: 0;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.4); }
.dialog-head { padding: 18px 20px 0; }
.dialog-head p { margin: 6px 0 0; color: var(--text-2); }
.dialog-body { display: flex; flex-direction: column; gap: 14px; padding: 18px 20px; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.secret-box {
  position: relative; padding: 12px 14px; border-radius: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.warn { display: flex; gap: 8px; padding: 10px 12px; border-radius: 8px; background: rgba(250, 178, 25, 0.14); font-size: 13px; }
.warn::before { content: "!"; flex: none; width: 18px; height: 18px; border-radius: 50%; display: grid; place-items: center; background: var(--warning); color: #0b0b0b; font-weight: 700; font-size: 12px; }

.toast {
  position: fixed; right: 16px; bottom: 16px; z-index: 50;
  max-width: calc(100vw - 32px); padding: 10px 14px; border-radius: 8px;
  background: var(--text); color: var(--page); font-size: 13px; box-shadow: var(--shadow);
}
.toast.error { background: var(--critical); color: #fff; }

.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky; z-index: 10; height: auto;
    flex-direction: row; align-items: center; gap: 2px;
    padding: 8px 12px; border-right: 0; border-bottom: 1px solid var(--border);
    overflow-x: auto;
  }
  .brand { padding: 0 8px 0 0; }
  .brand span { display: none; }
  .nav-link { padding: 6px 10px; white-space: nowrap; }
  .sidebar-foot { margin: 0 0 0 auto; padding: 0; border: 0; flex-direction: row; align-items: center; }
  .who { display: none; }
  .main { padding: 18px 16px 40px; }
  .toolbar input[type="search"] { min-width: 0; flex: 1 1 160px; }
  .tile-value { font-size: 22px; }
}

.dialog-form { display: flex; flex-direction: column; gap: 14px; }
.btn.primary.danger-fill { background: var(--critical); border-color: var(--critical); color: #fff; }
.head-meta { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.table-wrap.scroll { max-height: 480px; overflow-y: auto; }
.crumb { white-space: nowrap; }
.crumb svg { width: 14px; height: 14px; }

/* ---------- Thresholds ---------- */
.chart .zone-alert { fill: var(--critical); opacity: 0.06; }
.chart .threshold { stroke: var(--critical); stroke-width: 1; opacity: 0.75; shape-rendering: crispEdges; }
.chart .threshold-label { fill: var(--text-2); font-size: 11px; font-weight: 500; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }
.chart .stable { stroke: var(--good); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart .stable-label { fill: var(--text-2); font-size: 11px; font-weight: 500; paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }
.legend-key.stable-key::before { height: 0; border-top: 1.5px dashed var(--good); background: none; border-radius: 0; }
.chart .line.alert { stroke: var(--critical); }
.chart .dot.alert { fill: var(--critical); }

.lvl { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; font-weight: 500; color: var(--text); white-space: nowrap; }
.lvl::before { font-size: 10px; line-height: 1; }
.lvl-high::before { content: "▲"; color: var(--critical); }
.lvl-low::before { content: "▼"; color: var(--critical); }
.lvl-ok::before { content: "●"; color: var(--good); }
.lvl.icon-only { gap: 0; margin-left: 4px; }
.tooltip .lvl { margin-top: 4px; }
.tile.alert { border-color: var(--critical); box-shadow: inset 3px 0 0 var(--critical), var(--shadow); }
.tile-foot { display: flex; align-items: center; gap: 4px 8px; flex-wrap: wrap; }
.th-range { color: var(--text-2); }
.head-right { display: flex; align-items: center; gap: 8px; }
.dialog-foot .grow { flex: 1; }

/* ---------- Actuators (pump) ---------- */
:root { --pump: #1baf7a; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --pump: #199e70; } }
:root[data-theme="dark"] { --pump: #199e70; }

.chart .span-on { fill: var(--pump); }
.legend-key { display: inline-flex; align-items: center; gap: 6px; }
.legend-key::before { content: ""; width: 14px; height: 5px; border-radius: 1.5px; background: var(--pump); }
.tooltip .span-row { margin-top: 4px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.tooltip .span-row::before { content: ""; width: 10px; height: 5px; border-radius: 1.5px; border: 1px solid var(--pump); }
.tooltip .span-row.on::before { background: var(--pump); }

.actuators { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 460px), 1fr)); gap: 12px; }
.act-card .card-head h3 { font-size: 15px; }
.act-state { display: inline-flex; align-items: center; gap: 8px; height: 28px; padding: 0 12px; border-radius: 999px; font-weight: 600; font-size: 13px; border: 1px solid var(--border); background: var(--surface-2); }
.act-state::before { content: ""; width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--off); box-sizing: border-box; }
.act-state.on { background: color-mix(in srgb, var(--pump) 14%, var(--surface)); border-color: color-mix(in srgb, var(--pump) 45%, transparent); }
.act-state.on::before { border-color: var(--pump); background: var(--pump); animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .act-state.on::before { animation: none; } }
.mode-tag { font-size: 12px; color: var(--text-2); padding: 2px 8px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); white-space: nowrap; }
.rule { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; font-size: 13.5px; }
.rule dt { color: var(--muted); }
.rule dd { margin: 0; }
.streak { display: flex; align-items: center; gap: 10px; margin-top: 14px; font-size: 13px; color: var(--text-2); flex-wrap: wrap; }
.streak-dots { display: inline-flex; gap: 4px; }
.streak-dots i { width: 14px; height: 6px; border-radius: 2px; background: var(--grid); }
.streak-dots i.on { background: var(--accent); }
.act-meta { display: flex; flex-direction: column; gap: 4px; margin-top: 12px; font-size: 13px; color: var(--text-2); }
.act-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }
.act-actions .grow { flex: 1; }
.act-card .warn { margin-top: 12px; }
.events { border-top: 1px solid var(--border); }
.events summary { padding: 10px 16px; cursor: pointer; color: var(--text-2); font-size: 13px; }
.events ul { list-style: none; margin: 0; padding: 0 16px 12px; display: flex; flex-direction: column; gap: 8px; }
.events li { display: grid; grid-template-columns: 110px 64px 1fr; gap: 10px; font-size: 13px; align-items: baseline; }
.events .ev-on { font-weight: 600; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 520px) { .form-grid { grid-template-columns: 1fr; } .events li { grid-template-columns: 1fr; gap: 0; } }
