/* ── Tokens ───────────────────────────────────────────────────────────────── */
:root,
[data-theme='dark'] {
  color-scheme: dark;
  --bg: #1a1a2e;
  --card: #252540;
  --card-hover: #2d2d4a;
  --border: rgba(255, 255, 255, 0.07);
  --shadow: rgba(0, 0, 0, 0.3);
  --text: #e8e8f0;
  --text-2: #b0b0c8;
  --text-3: #8a8aa8;
  --primary: #5b95f5;
  --wash: rgba(255, 255, 255, 0.06);
  --grid: rgba(255, 255, 255, 0.08);
  --axis: rgba(255, 255, 255, 0.18);
  /* categorical series (validated for the dark card surface) */
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767;
  --s-other: #6e6e8a;
  /* status */
  --good: #0ca30c; --warn: #fab219; --serious: #ec835a; --critical: #e05252;
  /* sensor accents (rooms page) */
  --c-temp: #ff9800; --c-hum: #42a5f5; --c-lux: #ffca28; --c-value: #66bb6a;
}
[data-theme='light'] {
  color-scheme: light;
  --bg: #f5f5fa;
  --card: #ffffff;
  --card-hover: #f0f0f5;
  --border: rgba(0, 0, 0, 0.08);
  --shadow: rgba(0, 0, 0, 0.07);
  --text: #1a1a2e;
  --text-2: #4a4a6a;
  --text-3: #6e6e8c;
  --primary: #2a78d6;
  --wash: rgba(0, 0, 0, 0.04);
  --grid: rgba(0, 0, 0, 0.07);
  --axis: rgba(0, 0, 0, 0.2);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --s-other: #a3a3b8;
  --good: #0a8a0a; --warn: #c98500; --serious: #d0602e; --critical: #d03b3b;
  --c-temp: #fb8c00; --c-hum: #1976d2; --c-lux: #f9a825; --c-value: #388e3c;
}

/* ── Base ─────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-font-smoothing: antialiased; }
body {
  min-height: 100vh;
  font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: inherit; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--text-3); border-radius: 3px; }

/* ── Top bar ──────────────────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  max-width: 1280px; margin: 0 auto; padding: 12px 16px 8px;
  background: var(--bg);
}
.nav {
  display: flex; gap: 2px; flex: 1; min-width: 0; overflow-x: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 4px;
  scrollbar-width: none;
}
.nav a {
  padding: 8px 14px; border-radius: 8px; text-decoration: none; white-space: nowrap;
  font-weight: 500; color: var(--text-2);
}
.nav a:hover { background: var(--wash); color: var(--text); }
.nav a[aria-current='page'] { background: var(--primary); color: #fff; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-3); white-space: nowrap; }
.status::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.status[data-state='stale']::before { background: var(--warn); }
.status[data-state='error']::before { background: var(--critical); }
.status[data-state='loading']::before { background: var(--text-3); animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: 0.3; } }
.icon-btn {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border: 1px solid var(--border); border-radius: 10px; background: var(--card); cursor: pointer; color: var(--text-2);
}
.icon-btn:hover { color: var(--text); background: var(--card-hover); }

/* ── Page layout ──────────────────────────────────────────────────────────── */
.page { max-width: 1280px; margin: 0 auto; padding: 8px 16px 40px; outline: none; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0 14px; }
.page-head h1 { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.page-head .spacer { flex: 1; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(12, 1fr); }
.grid > * { grid-column: span 12; min-width: 0; }
@media (min-width: 900px) {
  .span-3 { grid-column: span 3; } .span-4 { grid-column: span 4; } .span-5 { grid-column: span 5; }
  .span-6 { grid-column: span 6; } .span-7 { grid-column: span 7; } .span-8 { grid-column: span 8; }
}
@media (min-width: 600px) and (max-width: 899px) {
  .span-3, .span-4 { grid-column: span 6; }
}

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px; box-shadow: 0 1px 3px var(--shadow);
}
.card h2 { font-size: 14px; font-weight: 600; margin-bottom: 2px; }
.card .sub { color: var(--text-3); font-size: 12px; margin-bottom: 12px; }
.card-head { display: flex; align-items: flex-start; gap: 12px; }
.card-head > div:first-child { flex: 1; min-width: 0; }
.muted { color: var(--text-3); }
.empty { color: var(--text-3); padding: 24px 0; text-align: center; }

/* segmented control (range picker) */
.seg { display: inline-flex; background: var(--wash); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 5px 11px; border-radius: 7px; cursor: pointer;
  font-size: 12px; font-weight: 500; color: var(--text-2);
}
.seg button[aria-pressed='true'] { background: var(--card); color: var(--text); box-shadow: 0 1px 2px var(--shadow); }

/* ── Stat tiles ───────────────────────────────────────────────────────────── */
.tiles { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.tile { padding: 14px 16px; }
.tile .label { color: var(--text-2); font-size: 12px; font-weight: 500; }
.tile .value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; margin-top: 2px; }
.tile .value small { font-size: 13px; font-weight: 500; color: var(--text-2); margin-left: 3px; }
.tile .note { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.hero .value { font-size: 52px; line-height: 1.05; }
.card .tiles { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.card .tiles .tile { background: var(--wash); border: 0; box-shadow: none; padding: 10px 12px; }
.card .tiles .tile .value { font-size: 22px; }
.tiles.kpis { grid-template-columns: repeat(auto-fit, minmax(108px, 1fr)); gap: 10px; }
.tiles.kpis .tile { padding: 9px 12px; }
.tiles.kpis .tile .value { font-size: 20px; }
.tiles.kpis .tile .note { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* meter (battery level) */
.meter { height: 8px; border-radius: 4px; background: var(--wash); overflow: hidden; margin-top: 8px; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--s3); }
.meter.low > span { background: var(--critical); }

/* ── Horizontal bar list ──────────────────────────────────────────────────── */
.bars { display: grid; gap: 8px; }
.bars .row { display: grid; grid-template-columns: minmax(90px, 140px) 1fr auto; gap: 10px; align-items: center; font-size: 13px; }
.bars .name { color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.bars .track { height: 10px; background: var(--wash); border-radius: 5px; overflow: hidden; }
.bars .track, .bars .fill { display: block; }
.bars .fill { height: 100%; border-radius: 5px; background: var(--s1); min-width: 2px; }
.bars .val { font-variant-numeric: tabular-nums; text-align: right; min-width: 64px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }

/* ── Tables ───────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--text-3); font-weight: 500; font-size: 12px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--wash); }

/* status chips: always icon + label */
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 20px; font-size: 12px; font-weight: 500; background: var(--wash); }
.chip::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.chip.good { color: var(--good); } .chip.warn { color: var(--warn); }
.chip.serious { color: var(--serious); } .chip.critical { color: var(--critical); }
.chip span { color: var(--text); }

/* ── Charts ───────────────────────────────────────────────────────────────── */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; -webkit-user-select: none; user-select: none; }
.chart text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { opacity: 0.1; }
.chart .cross { stroke: var(--text-3); stroke-width: 1; }
.chart .hover-band { fill: var(--wash); }
.chart .dot { stroke: var(--card); stroke-width: 2; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.legend i.box { height: 10px; width: 10px; border-radius: 3px; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5; min-width: 140px;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 20px var(--shadow); padding: 8px 10px; font-size: 12px;
}
.tooltip .t { color: var(--text-3); margin-bottom: 4px; }
.tooltip .r { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .r > span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); }
.tooltip .r b { font-weight: 600; font-variant-numeric: tabular-nums; }
.tooltip .sum { border-top: 1px solid var(--grid); margin-top: 4px; padding-top: 4px; }
details.data { margin-top: 8px; font-size: 12px; }
details.data summary { cursor: pointer; color: var(--text-3); width: max-content; }
details.data .table-wrap { max-height: 280px; overflow: auto; margin-top: 6px; }

/* ── Energy flow ──────────────────────────────────────────────────────────── */
.flow svg { width: 100%; max-width: 560px; height: auto; display: block; margin: 0 auto; }
.flow .node rect { fill: var(--wash); stroke: var(--border); }
.flow .node .big { fill: var(--text); font-size: 20px; font-weight: 600; }
.flow .node .lbl { fill: var(--text-2); font-size: 12px; }
.flow .node .small { fill: var(--text-3); font-size: 11px; }
.flow .node.stale rect { stroke-dasharray: 4 3; }
.flow .node.stale .big { fill: var(--text-3); }
.flow .node.stale .small { fill: var(--warn); }
.flow .edge { fill: none; stroke: var(--grid); stroke-linecap: round; }
.flow .edge.on { stroke-dasharray: 2 9; animation: flow 0.9s linear infinite; }
.flow .edge-lbl { fill: var(--text-2); font-size: 12px; font-weight: 500; }
@keyframes flow { to { stroke-dashoffset: -11; } }
@media (prefers-reduced-motion: reduce) { .flow .edge.on { animation: none; stroke-dasharray: none; } }

/* ── Rooms (MQTT) – mirrors the old Angular dashboard's sensor cards ─────── */
.floor-nav {
  display: flex; align-items: center; gap: 4px; margin: 8px 0 20px; padding: 4px; overflow-x: auto;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px; scrollbar-width: none;
}
.floor-nav .seg { background: none; padding: 0; gap: 4px; }
.floor-nav .seg button { padding: 10px 20px; border-radius: 8px; font-size: 14px; white-space: nowrap; transition: background 0.2s, color 0.2s; }
.floor-nav .seg button:hover { background: var(--wash); color: var(--text); }
.floor-nav .seg button[aria-pressed='true'] { background: var(--primary); color: #fff; box-shadow: 0 2px 8px color-mix(in srgb, var(--primary) 30%, transparent); }
.conn {
  display: inline-flex; align-items: center; gap: 6px; flex: none; padding: 4px 12px; border-radius: 20px;
  font-size: 12px; font-weight: 500; background: var(--wash); color: var(--text-2);
}
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); animation: blink 1.5s ease-in-out infinite; }
.conn.on i { background: #66bb6a; animation: none; }
.conn.err i { background: var(--critical); }

.room-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); animation: fade-in 0.3s ease; }
@keyframes fade-in { from { opacity: 0; transform: translateY(8px); } }
.sensor {
  position: relative; height: 160px; display: flex; flex-direction: column; overflow: hidden; cursor: pointer; text-align: left;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px 6px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.sensor:hover { transform: translateY(-2px); box-shadow: 0 6px 20px var(--shadow); }
.sensor:focus-visible { outline-color: var(--accent); }
.sensor .top { display: flex; align-items: center; gap: 6px; min-height: 20px; margin-bottom: 2px; }
.sensor .top > svg { flex: none; }
.sensor .title { flex: 1; min-width: 0; font-size: 14px; font-weight: 500; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sensor .ind { display: flex; gap: 4px; align-items: center; flex: none; color: var(--text-3); }
.sensor .ind-ico { display: block; }
.sensor .bat-critical { color: var(--critical); animation: blink 1s ease-in-out infinite; }
.sensor .val { text-align: center; line-height: 1; white-space: nowrap; }
.sensor .val b { font-size: 34px; font-weight: 700; letter-spacing: -0.5px; color: var(--accent); font-variant-numeric: tabular-nums; }
.sensor .val small { font-size: 14px; color: var(--text-3); margin-left: 1px; vertical-align: super; }
.sensor .mm { display: flex; justify-content: center; gap: 8px; min-height: 21px; padding: 2px 4px 0; font-size: 14px; }
.sensor .mm span { display: inline-flex; align-items: center; gap: 2px; padding: 1px 6px; border-radius: 4px; background: var(--wash); color: var(--text-3); font-variant-numeric: tabular-nums; }
.sensor .mm i { font-size: 9px; font-style: normal; }
.sensor .skel { display: flex; flex-direction: column; align-items: center; gap: 8px; padding-top: 4px; }
.sensor .skel span { width: 50%; height: 14px; border-radius: 4px; background: var(--wash); animation: pulse 1.5s ease-in-out infinite; }
.sensor .skel .wide { width: 80%; height: 28px; }
.sensor.loading .title { color: transparent; background: var(--wash); border-radius: 4px; flex: 0 1 60%; animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 0.4; } 50% { opacity: 0.7; } }
.sensor .spark-box { flex: 1; min-height: 0; display: flex; flex-direction: column; margin-top: 4px; }
.sensor svg.spark { flex: 1; width: 100%; min-height: 0; display: block; }
.sensor .spark-bar { height: 5px; margin-top: -4px; background: var(--accent); opacity: 0.2; border-radius: 0 0 10px 10px; }
.sensor .ruler { display: block; width: 100%; height: 5px; margin-top: -2px; color: var(--text-2); opacity: 0.6; }
@media (prefers-reduced-motion: reduce) { .room-grid { animation: none; } .sensor, .sensor:hover { transition: none; transform: none; } }
@media (max-width: 600px) {
  .room-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
  .floor-nav .seg button { padding: 8px 14px; }
  .status { font-size: 0; gap: 0; } /* dot only */
  .topbar { padding: 8px 10px 6px; }
  .page { padding: 6px 10px 32px; }
  .hero .value { font-size: 44px; }
}
.back { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text-2); }

/* live values older than STALE_MS: still shown, greyed, with their age as text */
.stale-tag { font-size: 11px; font-weight: 400; color: var(--text-3); white-space: nowrap; }
.tile.stale .value, .bars .row.stale .val, td.stale { color: var(--text-3); }
.bars .row.stale .fill { opacity: 0.35; }
.stale-note { font-size: 12px; color: var(--text-3); margin-top: 6px; }
