:root {
  --bg: #f4f2ef;
  --surface: #ffffff;
  --surface-2: #efece8;
  --text: #1c1d20;
  --muted: #6b6d73;
  --line: #e2ded8;
  --heat: #f2652d;
  --heat-soft: #fde4d8;
  --cool: #2f7de1;
  --cool-soft: #dbe9fb;
  --ok: #2c9a5a;
  --warn: #c98a12;
  --bad: #d23c3c;
  --accent: var(--heat);
  --radius: 18px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 20px rgba(0,0,0,.05);
  color-scheme: light;
}
/* Dark theme: follows the system unless the Appearance setting forces one (data-theme on <html>).
   Forcing matters for privacy browsers like Mullvad/Tor, which always report "light". */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #101114;
    --surface: #1a1c21;
    --surface-2: #23262c;
    --text: #eceae6;
    --muted: #9a9ca3;
    --line: #2c2f36;
    --heat: #ff7a3a;
    --heat-soft: #3a2218;
    --cool: #4c9aff;
    --cool-soft: #172638;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #101114;
  --surface: #1a1c21;
  --surface-2: #23262c;
  --text: #eceae6;
  --muted: #9a9ca3;
  --line: #2c2f36;
  --heat: #ff7a3a;
  --heat-soft: #3a2218;
  --cool: #4c9aff;
  --cool-soft: #172638;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; margin-right: auto; }
.brand img { width: 26px; height: 26px; }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill.ok { color: var(--ok); }
.pill.bad { color: var(--bad); }
.pill.mock { color: var(--warn); }
#device-picker { max-width: 40vw; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 4px 8px; }

main { max-width: 720px; margin: 0 auto; padding: 4px 16px 24px; }
.view > h2 { font-size: 22px; margin: 8px 0 14px; }
h3 { font-size: 15px; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px; border: 1px solid var(--line);
}
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.row.between { justify-content: space-between; flex-wrap: nowrap; }
.row.between > :first-child { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stack > * + * { margin-top: 10px; }

/* alert banner */
#alert-banner { max-width: 720px; margin: 0 auto; padding: 0 16px; }
#alert-banner .alert { margin-bottom: 8px; }
.alert { display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-radius: 12px; background: var(--heat-soft); color: var(--text); border-left: 4px solid var(--bad); }
.alert.medium { border-left-color: var(--warn); }
.alert.low { border-left-color: var(--muted); }
.alert b { display: block; }

/* home: dial */
.dial-wrap { display: grid; place-items: center; padding: 6px 0 4px; }
.dial { position: relative; width: min(300px, 78vw); aspect-ratio: 1; }
.dial svg { width: 100%; height: 100%; transform: rotate(135deg); }
.dial .track { fill: none; stroke: var(--surface-2); stroke-width: 14; stroke-linecap: round; }
.dial .arc { fill: none; stroke: var(--muted); stroke-width: 14; stroke-linecap: round; transition: stroke-dasharray .5s, stroke .3s; }
.dial.heating .arc { stroke: var(--heat); }
.dial.cooling .arc { stroke: var(--cool); }
.dial .center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.dial .now { font-size: clamp(56px, 18vw, 76px); font-weight: 300; letter-spacing: -2px; line-height: 1; }
.dial .now sup { font-size: .35em; letter-spacing: 0; vertical-align: top; position: relative; top: .35em; margin-left: 2px; }
.dial .action { margin-top: 8px; font-weight: 600; font-size: 14px; }
.dial.heating .action { color: var(--heat); }
.dial.cooling .action { color: var(--cool); }
.dial .humid { color: var(--muted); font-size: 18px; font-weight: 550; margin-top: 4px; }
.dial .outside { margin: 10px auto 0; padding: 4px 12px; border-radius: 99px; background: var(--surface-2); font-size: 14px; }
.dial .outside span { color: var(--muted); }
.dial .outside b { color: var(--cool); font-weight: 650; }
.flame { display: inline-block; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }

.setpoints { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; margin: 4px 0 12px; }
.stepper { display: flex; align-items: center; gap: 4px; background: var(--surface-2); border-radius: 99px; padding: 4px; }
.stepper button { width: 52px; height: 52px; border-radius: 50%; border: 0; background: var(--surface); font-size: 26px; cursor: pointer; box-shadow: var(--shadow); touch-action: manipulation; }
.stepper button:active { transform: scale(.94); }
.stepper .val { min-width: 92px; text-align: center; }
.stepper .val b { display: block; font-size: 28px; font-weight: 600; line-height: 1.1; }
.stepper .val span { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.stepper.heat .val b { color: var(--heat); }
.stepper.cool .val b { color: var(--cool); }
.stepper.pending .val b { opacity: .55; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px 6px; border-radius: 9px; cursor: pointer; font-weight: 550; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg button.on[data-v="heat"] { color: var(--heat); }
.seg button.on[data-v="cool"] { color: var(--cool); }
.label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 12px 0 6px; }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.fact { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.fact .k { font-size: 12px; color: var(--muted); }
.fact .v { font-weight: 600; margin-top: 2px; }

/* buttons & inputs */
.btn { border: 1px solid var(--line); background: var(--surface); padding: 9px 14px; border-radius: 11px; cursor: pointer; font-weight: 550; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.ghost { background: none; border-color: transparent; color: var(--muted); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=number], input[type=time], input[type=datetime-local], input[type=password], select, textarea {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-width: 0;
}
input[type=number] { width: 84px; }
textarea { width: 100%; min-height: 80px; font: 12px/1.4 ui-monospace, Consolas, monospace; }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.field > input, label.field > select { color: var(--text); }
.switch { position: relative; width: 46px; height: 28px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: .2s; cursor: pointer; }
.switch span::after { content: ""; position: absolute; width: 22px; height: 22px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(18px); }

/* schedule */
.entry { border: 1px solid var(--line); border-radius: 14px; padding: 12px; margin-bottom: 10px; background: var(--surface); }
.days { display: flex; gap: 4px; flex-wrap: wrap; }
.days button { width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-size: 12px; font-weight: 600; color: var(--muted); }
.days button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.timeline { position: relative; height: 34px; border-radius: 10px; overflow: hidden; background: var(--surface-2); margin: 6px 0 2px; }
.timeline div { position: absolute; top: 0; bottom: 0; display: grid; place-items: center; font-size: 11px; font-weight: 600; color: #fff; border-right: 1px solid var(--surface); overflow: hidden; white-space: nowrap; }
.timeline .now-mark { width: 2px; background: var(--text); border: 0; z-index: 2; }
.tl-row { display: grid; grid-template-columns: 34px 1fr; gap: 8px; align-items: center; }
.tl-row > span { font-size: 12px; color: var(--muted); }

/* history */
.chart { width: 100%; min-height: 330px; } /* keep its height while redrawing */
.uplot { font-family: inherit; }
.u-legend { font-size: 12px; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 140px; padding-top: 8px; }
.bars .b { flex: 1; background: var(--heat); border-radius: 4px 4px 0 0; min-height: 2px; position: relative; opacity: .85; }
.bars .b.cool { background: var(--cool); }
.bars .b:hover { opacity: 1; }
.bars-x { display: flex; gap: 3px; font-size: 10px; color: var(--muted); }
.bars-x span { flex: 1; text-align: center; overflow: hidden; }

/* lists */
.log { list-style: none; padding: 0; margin: 0; font-size: 13px; }
.log li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; gap: 10px; }
.log li time { color: var(--muted); white-space: nowrap; min-width: 92px; }
.empty { text-align: center; color: var(--muted); padding: 18px 0; }

/* bottom tabs */
.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 5;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; max-width: 120px; border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 10px; font-size: 11px; color: var(--muted); position: relative;
}
.tabs button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.active { color: var(--accent); }
.badge { position: absolute; top: 4px; left: calc(50% + 6px); background: var(--bad); color: #fff; font-size: 10px; min-width: 16px; height: 16px; border-radius: 8px; display: grid; place-items: center; padding: 0 4px; }

dialog { border: 0; border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 18px; width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.45); }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; max-width: calc(100vw - 32px); z-index: 10;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
#toast.error { background: var(--bad); color: #fff; }

ol.steps { padding-left: 20px; margin: 8px 0; }
ol.steps li { margin-bottom: 4px; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; font-size: 13px; }
.grid2 label.field > input, .grid2 label.field > select { width: 100%; }
.boost-row { display: flex; justify-content: center; margin: -2px 0 6px; }
.boost-on { display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap; margin: -2px 0 6px; padding: 6px 10px; border-radius: 12px; background: var(--heat-soft); }
.alert .btn { flex: none; }
.meter { height: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 12px; }
.meter > div { height: 100%; border-radius: 99px; }
/* Equipment status under the dial */
.status-strip { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; margin: 2px 0 12px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-size: 14px; }
.chip b { font-weight: 600; }
.chip.on.heat { background: var(--heat-soft); color: var(--heat); }
.chip.on.cool { background: var(--cool-soft); color: var(--cool); }
.chip.on.fan { background: var(--cool-soft); color: var(--cool); }
.chip:not(.on) > span:first-child { filter: grayscale(1); opacity: .55; }
.fan-icon { display: inline-flex; line-height: 0; }
.fan-icon.spin { animation: spin 1.2s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .fan-icon.spin, .flame { animation: none; } }
fieldset.plain { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset.plain[disabled] .entry { opacity: .75; }
