:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #15202b;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #0d9488;
  --accent-ink: #ffffff;
  --heat: #ea580c;
  --solar: #ca8a04;
  --good: #16a34a;
  --bad: #dc2626;
  --chip: #eef2f6;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 2px 8px rgb(0 0 0 / .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1720; --card: #17212c; --text: #e6edf3; --muted: #8b9bb0; --line: #263342;
    --accent: #2dd4bf; --accent-ink: #062a26; --heat: #fb923c; --solar: #facc15;
    --good: #4ade80; --bad: #f87171; --chip: #213040; --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1720; --card: #17212c; --text: #e6edf3; --muted: #8b9bb0; --line: #263342;
  --accent: #2dd4bf; --accent-ink: #062a26; --heat: #fb923c; --solar: #facc15;
  --good: #4ade80; --bad: #f87171; --chip: #213040; --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button { font: inherit; color: inherit; cursor: pointer; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 14px max(16px, calc((100% - 1100px) / 2 + 16px));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.top h1 { font-size: 22px; margin: 0; flex: 1; }
.pill:empty { display: none; }
.pill { font-size: 12px; padding: 4px 10px; border-radius: 99px; background: var(--chip); color: var(--muted); }
.pill[data-status="connected"] { color: var(--good); }
.pill[data-status="connected"]::before { content: "● "; }
.pill[data-status="disconnected"], .pill[data-status="auth_invalid"] { color: var(--bad); }
.icon-btn { border: 0; background: none; font-size: 18px; color: var(--muted); padding: 4px 6px; }

main { max-width: 1100px; margin: 0 auto; padding: 0 16px 40px; }
section { margin-top: 18px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.grid { display: grid; gap: 12px; margin-bottom: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.grid.plugs { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
body.stale main { opacity: .55; transition: opacity .3s; }

.card {
  background: var(--card); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); border: 1px solid var(--line);
}
.card.unavailable { opacity: .5; }
.card header { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.name { font-weight: 600; }
.sub, .action, .state { color: var(--muted); font-size: 13px; }

/* Energy */
.energy { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 640px) { .energy { grid-template-columns: repeat(2, 1fr); } }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.tile-label { font-size: 12px; color: var(--muted); }
.tile-value { font-size: 22px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile-sub { font-size: 12px; color: var(--muted); min-height: 1.4em; }
.tile-pv[data-state="active"] .tile-value { color: var(--solar); }
.tile-grid[data-state="import"] .tile-sub { color: var(--bad); }
.tile-grid[data-state="export"] .tile-sub { color: var(--good); }
.tile-battery[data-state="charge"] .tile-sub { color: var(--good); }
.tile-battery::after { content: ""; position: absolute; left: 0; bottom: 0; height: 4px; width: var(--soc, 0%); background: var(--good); transition: width .6s; }

/* Climate */
.climate[data-action="heating"] .action { color: var(--heat); font-weight: 600; }
.climate[data-action="heating"] { border-color: color-mix(in srgb, var(--heat) 45%, var(--line)); }
.temps { display: flex; align-items: center; justify-content: space-between; margin: 10px 0; }
.current { font-size: 32px; font-weight: 300; font-variant-numeric: tabular-nums; }
.setpoint { display: flex; align-items: center; gap: 8px; }
.target { min-width: 52px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.target.pending { color: var(--accent); }
.round { width: 38px; height: 38px; border-radius: 50%; border: 1px solid var(--line); background: var(--chip); font-size: 20px; line-height: 1; }
.round:active { transform: scale(.94); }
.modes { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 0; background: var(--chip); padding: 5px 12px; border-radius: 99px; font-size: 13px; text-transform: capitalize; color: var(--muted); }
.chip.on { background: var(--accent); color: var(--accent-ink); }

/* Plugs */
.plug { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.switch { width: 50px; height: 30px; border-radius: 99px; border: 0; background: var(--line); position: relative; flex: none; transition: background .2s; }
.switch .knob { position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / .3); transition: transform .2s; }
.plug.on .switch { background: var(--accent); }
.plug.on .switch .knob { transform: translateX(20px); }

/* Lock / gate */
.lock[data-state="unlocked"] .state, .lock[data-state="open"] .state { color: var(--heat); font-weight: 600; }
.lock[data-state="jammed"] .state { color: var(--bad); font-weight: 700; }
.lock[data-state="locked"] .state { color: var(--good); }
.row { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.row > * { flex: 1 1 auto; }
.btn { border: 1px solid var(--line); background: var(--chip); border-radius: 12px; padding: 10px 14px; font-weight: 600; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.hold {
  position: relative; overflow: hidden; border: 1px solid var(--line); background: var(--chip);
  border-radius: 12px; padding: 10px 14px; font-weight: 600; user-select: none; -webkit-user-select: none; touch-action: none;
}
.hold.accent { background: color-mix(in srgb, var(--accent) 16%, var(--chip)); border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }
.hold.big { width: 100%; padding: 18px; font-size: 17px; margin-top: 10px; }
.hold .fill { position: absolute; inset: 0; background: var(--accent); transform: scaleX(0); transform-origin: left; opacity: .35; }
.hold.holding .fill { transform: scaleX(1); transition: transform .7s linear; }
.hold.fired { animation: fired .9s; }
.hold .label { position: relative; }
.hold:disabled { opacity: .6; cursor: not-allowed; }
.hold[hidden] { display: none; }
@keyframes fired { 0% { background: var(--accent); color: var(--accent-ink); } 100% {} }

/* Login */
.login { max-width: 420px; margin: 40px auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; display: grid; gap: 14px; }
.login h2 { font-size: 18px; text-transform: none; letter-spacing: 0; color: var(--text); }
.login form { display: grid; gap: 10px; margin-top: 10px; }
.login label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login input { font: inherit; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.error { color: var(--bad); margin: 0; }
.hint, .empty { color: var(--muted); font-size: 13px; text-align: center; }
a { color: var(--accent); }

#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translate(-50%, 40px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none;
  transition: .25s; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
