:root {
  color-scheme: light;
  --surface-0: #f4f4f2;
  --surface-1: #fcfcfb;
  --border: #e3e2de;
  --grid: #ecebe7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --series-elec-import: #2a78d6;  /* slot 1 blue */
  --series-gas: #eb6834;          /* slot 2 orange */
  --series-elec-export: #1baf7a;  /* slot 3 aqua */
  --series-ev: #eda100;           /* slot 4 yellow */
  --series-ev-peak: #4a3aa7;      /* slot 7 violet */
  --series-garage: #e87ba4;       /* slot 5 magenta */
  /* Status colours: fixed in both themes, never used for a series; always with icon + text */
  --status-warning: #fab219;
  --status-critical: #d03b3b;
  --warn-bg: #fff4d6;
  --warn-text: #6b4b00;
  --ok-bg: #e3f4ea;
  --seq-1: #cde2fb; --seq-2: #9ec5f4; --seq-3: #6da7ec; --seq-4: #3987e5; --seq-5: #256abf; --seq-6: #184f95;
  --ok-text: #0f5132;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --border: #2e2e2c;
    --grid: #262624;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent: #3987e5;
    --series-elec-import: #3987e5;
    --series-gas: #d95926;
    --series-elec-export: #199e70;
    --series-ev: #c98500;
    --series-ev-peak: #9085e9;
    --series-garage: #d55181;
    --warn-bg: #3a2f10;
    --warn-text: #f3d68a;
    --ok-bg: #12301f;
    --seq-1: #104281; --seq-2: #1c5cab; --seq-3: #2a78d6; --seq-4: #5598e7; --seq-5: #86b6ef; --seq-6: #b7d3f6;
    --ok-text: #9ad8b2;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --border: #2e2e2c;
  --grid: #262624;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --accent: #3987e5;
  --series-elec-import: #3987e5;
  --series-gas: #d95926;
  --series-elec-export: #199e70;
  --series-ev: #c98500;
  --series-ev-peak: #9085e9;
  --series-garage: #d55181;
  --warn-bg: #3a2f10;
  --warn-text: #f3d68a;
  --ok-bg: #12301f;
  --ok-text: #9ad8b2;
  --seq-1: #104281; --seq-2: #1c5cab; --seq-3: #2a78d6; --seq-4: #5598e7; --seq-5: #86b6ef; --seq-6: #b7d3f6;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.muted { color: var(--text-muted); font-weight: 400; }
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 14px; margin: 0; font-weight: 600; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px 24px;
  padding: 10px 16px; background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.topbar h1 { white-space: nowrap; }
.sync { display: flex; justify-content: flex-end; align-items: center; gap: 10px; font-size: 13px; }
@media (max-width: 900px) {
  .topbar { grid-template-columns: 1fr auto; }
  .topbar .controls { grid-column: 1 / -1; grid-row: 2; justify-content: center; }
}

main {
  max-width: 2200px; margin: 0 auto; padding: 16px;
  display: grid; gap: 16px; grid-template-columns: repeat(12, minmax(0, 1fr));
}
main > * { grid-column: 1 / -1; min-width: 0; }

/* Wide screens: cards side by side. Hidden partners free their space. */
@media (min-width: 1200px) {
  #live-card { grid-column: span 5; }
  #session-card { grid-column: span 7; }
  #energy-card { grid-column: span 8; }
  #rates-card { grid-column: span 4; }
  #ev-card, #charger-card { grid-column: span 6; }
  main:has(#session-card[hidden]) #live-card,
  main:has(#live-card[hidden]) #session-card,
  main:has(#rates-card[hidden]) #energy-card,
  main:has(#charger-card[hidden]) #ev-card,
  main:has(#ev-card[hidden]) #charger-card { grid-column: 1 / -1; }
  #live-card { grid-template-columns: minmax(180px, 220px) 1fr; }
  /* With the Shelly's circuits the live card needs the full width; the charging session
     then takes the next row. */
  main:has(#circuits-now:not([hidden])) #live-card,
  main:has(#circuits-now:not([hidden])) #session-card { grid-column: 1 / -1; }
  #live-card:has(#circuits-now:not([hidden])) {
    grid-template-columns: minmax(220px, 260px) minmax(320px, 440px) minmax(0, 1fr);
  }
}
@media (min-width: 1800px) {
  #live-card { grid-column: span 4; }
  #session-card { grid-column: span 8; }
  #energy-card { grid-column: span 9; }
  #rates-card { grid-column: span 3; }
}
[hidden] { display: none !important; }

button {
  font: inherit; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 6px; padding: 5px 12px; cursor: pointer;
}
button:hover { border-color: var(--text-muted); }
button:disabled { opacity: .5; cursor: default; }

.banner { background: var(--warn-bg); color: var(--warn-text); padding: 10px 14px; border-radius: 8px; }

.controls { display: flex; flex-wrap: wrap; gap: 12px 28px; justify-content: center; align-items: center; }
.seg button { min-width: 64px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--surface-1); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.active { background: var(--accent); color: #fff; }
.nav { display: flex; align-items: center; gap: 8px; }
#period-label { min-width: 170px; text-align: center; font-weight: 600; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.tile, .card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 16px;
}
.tile .name { display: flex; align-items: center; gap: 8px; color: var(--text-secondary); font-size: 13px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.tile .big { font-size: 28px; font-weight: 600; margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.tile .big small { font-size: 14px; font-weight: 400; color: var(--text-secondary); }
.tile .sub { color: var(--text-secondary); font-size: 13px; font-variant-numeric: tabular-nums; }

.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; color: var(--text-secondary); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-box { position: relative; height: 300px; }
.chart-box.short { height: 200px; }

details summary { cursor: pointer; font-weight: 600; }
.table-wrap { overflow-x: auto; margin-top: 12px; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; font-size: 13px; }
th, td { text-align: right; padding: 6px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-secondary); font-weight: 600; }

@media (max-width: 600px) {
  #period-label { min-width: 0; }
  .chart-box { height: 240px; }
}

/* Live card */
.live { display: grid; grid-template-columns: minmax(200px, 260px) 1fr; gap: 16px; align-items: center; }
.live .name { color: var(--text-secondary); font-size: 13px; }
.live .big { font-size: 34px; font-weight: 600; margin: 4px 0; font-variant-numeric: tabular-nums; }
.live .big small { font-size: 15px; font-weight: 400; color: var(--text-secondary); }
.live .sub { font-size: 13px; color: var(--text-secondary); }
.chart-box.live-chart { height: 100%; min-height: 160px; }
.live { align-items: stretch; }
.live-now { align-self: center; }
/* Electricity and gas charts side by side; electricity alone gets the full width */
.live-main { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; min-width: 0; }
.live-main:has(#gas-panel[hidden]) { grid-template-columns: minmax(0, 1fr); }
.live-panel { display: flex; flex-direction: column; min-width: 0; gap: 6px; }
.live-panel .chart-box { flex: 1; }
.live .sub-head { margin: 0; gap: 12px; }
.live .sub-head h3 { margin-right: auto; }
.stat-grid.circuits { grid-template-columns: repeat(2, minmax(0, 1fr)); align-content: center; margin: 0; }
.stat-grid.circuits .v small + small { margin-left: 6px; }
.seg.small button { min-width: 0; padding: 2px 10px; font-size: 12px; }
/* Main fuse gauge: fill = current as a share of the fuse's rating (bar = 125% of it) */
.fuse { margin: 8px 0 4px; }
.fuse-bar { position: relative; height: 8px; border-radius: 4px; background: var(--grid); overflow: hidden; }
.fuse-fill { position: absolute; inset: 0 auto 0 0; border-radius: 4px; background: var(--accent); }
.fuse-mark { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text-primary); }
.fuse.near .fuse-fill { background: var(--status-warning); }
.fuse.over .fuse-fill { background: var(--status-critical); }
.fuse .sub { margin-top: 4px; }
.note { font-size: 12px; margin-top: 8px; line-height: 1.5; }

#amps-card .tile .big { font-size: 22px; }
.fuse.near .sub, .fuse.over .sub { color: var(--text-primary); font-weight: 600; }
@media (max-width: 1100px) {
  #live-card:has(#circuits-now:not([hidden])) { grid-template-columns: minmax(200px, 260px) 1fr; }
  #live-card .live-main { grid-column: 1 / -1; min-height: 200px; }
}
@media (max-width: 700px) {
  .live, #live-card:has(#circuits-now:not([hidden])) { grid-template-columns: 1fr; }
  .live-main { grid-template-columns: minmax(0, 1fr); }
}

/* EV card */
#ev-card h3 { font-size: 13px; font-weight: 600; margin: 18px 0 0; color: var(--text-secondary); }
#ev-card .tiles { margin-bottom: 4px; }
#ev-card .tile { padding: 12px 14px; background: var(--surface-0); border-color: var(--grid); }
.callout { margin-top: 14px; padding: 10px 14px; border-radius: 8px; font-size: 13px; }
.callout.ok { background: var(--ok-bg); color: var(--ok-text); }
.callout.warn { background: var(--warn-bg); color: var(--warn-text); }
.callout.info { background: var(--surface-0); color: var(--text-secondary); border: 1px solid var(--grid); }
.callout ul { margin: 6px 0 0; padding-left: 18px; }
.pill { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 12px; background: var(--grid); color: var(--text-secondary); }

/* ---------- Car charging ---------- */
.sub-head { margin: 18px 0 8px; }
.sub-head h3 { margin: 0; font-size: 13px; font-weight: 600; color: var(--text-secondary); }

.battery { margin: 4px 0 16px; }
.battery .track {
  position: relative; height: 22px; border-radius: 6px; background: var(--grid); overflow: hidden;
}
.battery .before, .battery .gained { position: absolute; top: 0; bottom: 0; }
.battery .before { left: 0; background: var(--series-ev); opacity: .45; }
.battery .gained { background: var(--series-ev); border-left: 2px solid var(--surface-1); }
.battery .target {
  position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text-primary);
}
.battery .labels {
  display: flex; justify-content: space-between; gap: 8px; margin-top: 6px;
  font-size: 13px; color: var(--text-secondary); font-variant-numeric: tabular-nums;
}
.battery .labels strong { color: var(--text-primary); }

.stat-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px;
}
.stat { background: var(--surface-0); border: 1px solid var(--grid); border-radius: 8px; padding: 10px 12px; }
.stat .k { font-size: 12px; color: var(--text-secondary); }
.stat .v { font-size: 20px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .v small { font-size: 13px; font-weight: 400; color: var(--text-secondary); }
.stat .s { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

#charger-card .tile { padding: 12px 14px; background: var(--surface-0); border-color: var(--grid); }
#charger-card .tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
#charger-card .tile .big { font-size: 22px; }

.heatmap {
  display: grid; grid-template-columns: 34px repeat(24, minmax(0, 1fr)); gap: 2px;
  font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums;
}
.heatmap .cell { aspect-ratio: 1.6; border-radius: 3px; background: var(--grid); min-height: 12px; }
.heatmap .cell:hover { outline: 2px solid var(--text-primary); outline-offset: -1px; }
.heatmap .rowlab { align-self: center; }
.heatmap .collab { text-align: center; }
.heat-scale { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-muted); }
.heat-scale .ramp { display: flex; gap: 2px; }
.heat-scale .ramp span { width: 16px; height: 10px; border-radius: 2px; }

.tooltip {
  position: fixed; pointer-events: none; z-index: 10; padding: 8px 10px; border-radius: 6px;
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--border);
  font-size: 12px; box-shadow: 0 4px 12px rgba(0,0,0,.12); white-space: nowrap;
}

.details {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px 24px;
  margin: 12px 0 0; font-size: 13px;
}
.details > div { min-width: 0; }
.details dt { color: var(--text-secondary); font-size: 12px; }
.details dd { margin: 2px 0 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.theme-pick select {
  font: inherit; font-size: 13px; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px;
}
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}
