:root, [data-theme="light"] {
  --bg: #f4f6f9; --surface: #ffffff; --surface2: #eef1f5; --border: #d5dbe3;
  --text: #17202b; --muted: #55606e; --accent: #0b6bcb; --accent-text: #ffffff;
  --ok: #157f3b; --warn: #a15c00; --err: #c0262d; --info: #0b6bcb;
  --ok-bg: #e3f4e8; --warn-bg: #fdf0dc; --err-bg: #fbe5e6; --info-bg: #e2eefb;
  --rx: #0b6bcb; --tx: #c2410c; --focus: #0b6bcb;
}
[data-theme="dark"] {
  --bg: #0f141a; --surface: #171e26; --surface2: #1f2832; --border: #2e3a47;
  --text: #e6ebf1; --muted: #9aa7b5; --accent: #4ea3f5; --accent-text: #08121c;
  --ok: #4cc777; --warn: #f0b04a; --err: #ff6b70; --info: #4ea3f5;
  --ok-bg: #12301d; --warn-bg: #36280f; --err-bg: #3a1618; --info-bg: #102a44;
  --rx: #4ea3f5; --tx: #fb923c; --focus: #7cbcff;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
code { background: var(--surface2); padding: 0 4px; border-radius: 4px; font-size: .9em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 50; }
.skip:focus { left: 8px; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .9em; }

/* Buttons and inputs */
.btn { min-height: 44px; padding: 0 16px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 8px; font: inherit; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; }
.btn:hover { background: var(--surface2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--err); border-color: var(--err); }
.btn.icon { width: 44px; padding: 0; font-size: 18px; }
.btn.sm { min-height: 36px; padding: 0 12px; }
.btn:disabled { opacity: .6; cursor: progress; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 500; }
input:not([type=checkbox]), select { min-height: 44px; padding: 0 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit; width: 100%; }
label.check { flex-direction: row; align-items: center; gap: 10px; min-height: 44px; font-weight: 400; }
label.check input { width: 20px; height: 20px; accent-color: var(--accent); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; }
.login-card h1 { margin: 0; font-size: 24px; }
.login-card p { margin: 0; }
.form-error { color: var(--err); min-height: 1.5em; }

/* Layout */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px; padding: 8px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; min-height: 44px; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a { padding: 0 12px; min-height: 44px; display: flex; align-items: center; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 500; }
.nav a:hover { background: var(--surface2); color: var(--text); }
.nav a[aria-current="page"] { color: var(--text); background: var(--surface2); box-shadow: inset 0 -3px 0 var(--accent); }
.top-actions { display: flex; gap: 8px; }
main { max-width: 1280px; margin: 0 auto; padding: 20px; outline: none; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 22px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 16px; min-width: 0; }
.card h2 { margin: 0 0 12px; font-size: 16px; }
.spacer { flex: 1; }

/* Summary strip */
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat .v { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: 13px; }

/* Device cards */
.devices { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.device { display: flex; flex-direction: column; gap: 10px; color: inherit; text-decoration: none; border-left: 4px solid var(--border); }
.device:hover { border-color: var(--accent); }
.device.up { border-left-color: var(--ok); }
.device.down { border-left-color: var(--err); }
.device-head { display: flex; align-items: flex-start; gap: 8px; }
.device-head h3 { margin: 0; font-size: 16px; flex: 1; overflow-wrap: anywhere; }
.meters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; }
.meter .row { display: flex; justify-content: space-between; font-size: 13px; }
.meter .row b { font-variant-numeric: tabular-nums; }
.bar { height: 6px; border-radius: 3px; background: var(--surface2); overflow: hidden; margin-top: 3px; }
.bar > span { display: block; height: 100%; background: var(--ok); }
.bar > span.warn { background: var(--warn); }
.bar > span.err { background: var(--err); }
.kv { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--muted); }
.kv b { color: var(--text); font-weight: 600; }

.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.err, .badge.error { color: var(--err); background: var(--err-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
.badge.info, .badge.pending { color: var(--info); background: var(--info-bg); }

/* States */
.state { text-align: center; padding: 40px 16px; color: var(--muted); }
.state h2 { color: var(--text); font-size: 18px; margin: 0 0 6px; }
.state.err h2 { color: var(--err); }
.state .btn { margin-top: 12px; }
.skeleton { background: linear-gradient(90deg, var(--surface2), var(--surface), var(--surface2)); background-size: 200% 100%;
  animation: sk 1.2s infinite linear; border-radius: 10px; min-height: 160px; }
@keyframes sk { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }

/* Detail */
.detail-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 12px; }
.charts { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.chart-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.chart-head h2 { margin: 0; flex: 1; }
.chart-head .now { font-weight: 700; font-variant-numeric: tabular-nums; }
.chart { width: 100%; height: 160px; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 3px; vertical-align: middle; margin-right: 4px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { min-height: 40px; min-width: 48px; border: 0; background: var(--surface); color: var(--muted); font: inherit; cursor: pointer; padding: 0 10px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; overflow-x: auto; }
.tabs button { min-height: 44px; padding: 0 14px; border: 0; background: none; color: var(--muted); font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; border-bottom: 3px solid transparent; }
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td, tr.selected td { background: var(--surface2); }
.filter { max-width: 280px; margin-bottom: 10px; }

/* Events */
.events { list-style: none; margin: 0; padding: 0; }
.events li { display: flex; gap: 10px; align-items: baseline; padding: 8px 0; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.events time { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; min-width: 130px; }
.events .msg { flex: 1; min-width: 200px; overflow-wrap: anywhere; }

/* Dialog */
dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); padding: 20px; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 24px); }
dialog::backdrop { background: rgb(0 0 0 / .5); }
dialog h2 { margin: 0 0 16px; font-size: 18px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.checks { display: flex; flex-direction: column; justify-content: flex-end; }
.hint { font-size: 13px; }
.form-msg { min-height: 1.5em; margin: 8px 0; }
.form-msg.err { color: var(--err); }
.form-msg.ok { color: var(--ok); }
.dialog-actions { display: flex; gap: 8px; flex-wrap: wrap; }
#toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px); z-index: 60; }
#toast.show { opacity: 1; }

@media (max-width: 900px) {
  .summary, .detail-grid { grid-template-columns: 1fr 1fr; }
  .charts { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 6px 12px; gap: 6px; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  .top-actions { margin-left: auto; }
  main { padding: 12px; }
  .grid2 { grid-template-columns: 1fr; }
  .devices { grid-template-columns: 1fr; }
  .stat .v { font-size: 22px; }
}
