:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --chip: #f3f4f7;
  --sk-hi: #fafbfc;
  --text: #2b313c;
  --muted: #9aa2b1;
  --temp: #5b6573; /* softer gray for plain temperature readings */
  --line: #e8eaee;
  --ok: #2bb673;
  --warn: #e6a23c;
  --bad: #e5604d;
  --radius: 16px;
  --history-avg: #ff9500;
  --hum-lo: #2b313c; /* dry → near-black (light theme) */
  --hum-hi: #1f3a93; /* humid → navy */
}

:root[data-theme="dark"] {
  --bg: #0e1116;
  --panel: #181d26;
  --chip: #232a36;
  --sk-hi: #2e3644;
  --text: #eef2f7;
  --muted: #828c9c;
  --temp: #c3cbd7; /* softer gray for plain temperature readings */
  --line: #2a313d;
  --hum-lo: #eef2f7; /* dry → near-white (dark theme) */
  --hum-hi: #5b9bd5; /* humid → blue */
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI Light", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  font-weight: 300;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
}

/* dvh keeps the footer on-screen on mobile, where 100vh exceeds the visible
   viewport (browser chrome) and would push the footer below the fold. */
body { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ------------------------------------------------------------------ top bar */
#topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;  /* keeps the number centered */
  align-items: center;
  padding: 1.6vh 2.4vw;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  gap: clamp(0.6rem, 1.6vw, 1.5rem);
}
.topbar-right {
  justify-self: stretch;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: clamp(0.6rem, 1.5vw, 1.4rem);
}

.topbar-left {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  min-width: 0;
}
.air-glance {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 0.35rem 0.55rem;
  font-size: clamp(0.68rem, 1.45vh, 0.78rem);
  color: var(--muted);
  line-height: 1.25;
}
/* The `hidden` attribute must win over display:flex above, otherwise the
   header air-quality glance can never be hidden (it would show its `--`
   placeholder forever). */
.air-glance[hidden] {
  display: none;
}
.air-iaq-val {
  font-weight: 500;
  font-size: clamp(0.82rem, 1.7vh, 0.95rem);
  color: var(--text);
}
.air-iaq--good { color: var(--ok); }
.air-iaq--fair { color: var(--warn); }
.air-iaq--poor { color: var(--bad); }
.air-iaq-label {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-size: 0.92em;
}
.air-detail {
  opacity: 0.85;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-weight: 300;
  font-size: clamp(1rem, 2.4vh, 1.5rem);
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--text);
}

.dot {
  width: 0.7em; height: 0.7em;
  border-radius: 50%;
  background: var(--muted);
  transition: background .3s;
}
.dot.ok   { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad  { background: var(--bad); }
.dot.loading { background: var(--muted); animation: pulse 1.1s ease-in-out infinite; }
/* Cached (not-yet-live) data: amber, gently pulsing to signal "refreshing". */
.dot.cached { background: var(--warn); animation: pulse 1.6s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.3; transform: scale(0.82); }
}

/* ---------------------------------------------------- cold-start loading screen */
.boot-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.8rem;
  min-height: 50vh;
  text-align: center;
  color: var(--muted);
}
.boot-spinner {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--ok);
  animation: boot-spin 0.9s linear infinite;
}
.boot-msg {
  font-size: clamp(0.95rem, 2.2vh, 1.25rem);
  font-weight: 400;
  color: var(--text);
}
.boot-sub {
  font-size: 0.8rem;
  opacity: 0.8;
}
@keyframes boot-spin { to { transform: rotate(360deg); } }

/* ----------------------------------------------------------- setup wizard */
/* During the wizard the rest of the chrome (top bar, footer, rooms) is hidden
   so it reads as a clean full-screen flow. */
html[data-wizard="1"] #topbar,
html[data-wizard="1"] #app-footer,
html[data-wizard="1"] #rooms { display: none; }

.wizard {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(1rem, 4vh, 3rem);
}
.wizard.hidden { display: none; }
.wizard-card {
  width: min(560px, 94vw);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: clamp(1.4rem, 4vh, 2.6rem);
}
.wizard-progress {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  margin-bottom: 1.4rem;
}
.wizard-progress span {
  width: 26px;
  height: 4px;
  border-radius: 999px;
  background: var(--line);
  transition: background 0.2s;
}
.wizard-progress span.on { background: var(--ok); }
.wizard-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.9rem;
  text-align: center;
}
.wizard-step[hidden] { display: none; }
.wizard-title {
  font-size: clamp(1.4rem, 4vh, 2rem);
  font-weight: 300;
  letter-spacing: -0.5px;
}
.wizard-sub { color: var(--muted); line-height: 1.4; }
.wizard-hint {
  font-size: 0.78rem;
  color: var(--muted);
  opacity: 0.85;
  line-height: 1.4;
}
.wizard-step .text-input { width: 100%; max-width: 360px; }
.wizard-actions {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  width: 100%;
  margin-top: 0.8rem;
}
.wizard-spacer { flex: 1; }
.wizard-actions .btn:only-child { margin-left: auto; }

/* theme picker — both looks shown side by side */
.wizard-themes {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
}
.theme-choice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: transparent;
  border: 2px solid var(--line);
  border-radius: 14px;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.15s;
}
.theme-choice:hover { transform: translateY(-2px); }
.theme-choice.selected { border-color: var(--ok); }
.theme-choice-label { font-size: 0.85rem; color: var(--text); }
.theme-preview {
  width: 150px;
  height: 96px;
  border-radius: 10px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: hidden;
}
.theme-preview--light { background: #f5f6f8; }
.theme-preview--dark { background: #0e1116; }
.tp-bar { display: flex; align-items: center; gap: 5px; }
.tp-dot { width: 7px; height: 7px; border-radius: 50%; background: #2bb673; }
.tp-title { flex: 1; height: 7px; border-radius: 3px; background: currentColor; opacity: 0.45; }
.theme-preview--light { color: #2b313c; }
.theme-preview--dark { color: #eef2f7; }
.tp-cards { display: flex; gap: 6px; flex: 1; }
.tp-card {
  flex: 1;
  border-radius: 7px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.theme-preview--light .tp-card { background: #ffffff; border: 1px solid #e8eaee; }
.theme-preview--dark .tp-card { background: #181d26; border: 1px solid #2a313d; }
.tp-room { height: 5px; width: 60%; border-radius: 3px; background: currentColor; opacity: 0.4; }
.tp-temp { font-size: 0.95rem; font-weight: 600; align-self: flex-end; }

.wizard-govee-device {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  width: 100%;
  max-width: 360px;
  text-align: left;
}
.wizard-govee-device.hidden { display: none; }
.wizard-govee-device select {
  width: 100%;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.55em 0.65em;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip);
  color: var(--text);
}

.overall-main {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: clamp(0.6rem, 1.5vw, 1.3rem);
  line-height: 1;
}
.overall-num {
  text-align: center;
}
.overall-main--tappable {
  cursor: pointer;
  border-radius: 12px;
  transition: background 0.15s;
}
.overall-main--tappable:hover {
  background: color-mix(in srgb, var(--chip) 45%, transparent);
}
.overall-val {
  font-size: clamp(2rem, 6vh, 4rem);
  font-weight: 200;
  letter-spacing: -1px;
  display: block;
}
.overall-label {
  font-size: clamp(0.55rem, 1.3vh, 0.8rem);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 2px;
  font-weight: 400;
}
/* Govee humidity + air quality, to the right of the whole-home average.
   Two stat cells side by side: colored number on top, muted label below. */
.overall-air {
  display: flex;
  align-items: flex-start;
  gap: clamp(0.7rem, 1.6vw, 1.4rem);
  font-size: clamp(0.62rem, 1.4vh, 0.82rem);
  font-weight: 400;
  line-height: 1.2;
}
.overall-air[hidden] { display: none; }
.oa-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  cursor: pointer;
  border-radius: 10px;
  padding: 0.1rem 0.35rem;
  transition: background 0.15s;
}
.oa-cell:hover {
  background: color-mix(in srgb, var(--chip) 45%, transparent);
}
.oa-cell[hidden] { display: none; }
.oa-num {
  font-size: 1.5em;
  font-weight: 600;
  line-height: 1;
}
.oa-label {
  color: var(--muted);
  white-space: nowrap;
}
.meta { flex: 0 0 auto; margin-left: auto; }

.meta {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  color: var(--muted);
  font-size: clamp(0.6rem, 1.5vh, 0.85rem);
  font-weight: 400;
}

#settings-btn {
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 1.7rem;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  transition: transform .25s, color .2s;
}
#settings-btn:hover { color: var(--text); transform: rotate(40deg); }

/* ---------------------------------------------------------------- app footer */
.app-footer {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  height: 2.125rem;
  max-height: 2.125rem;
  padding: 0 clamp(0.6rem, 2.4vw, 2rem);
  box-sizing: border-box;
  border-top: 1px solid var(--line);
  background: var(--panel);
  font-size: 0.76rem;
  line-height: 1;
  color: var(--muted);
  overflow: hidden;
}
#footer-status {
  line-height: 1;
  display: block;
  transform: translateY(-1px);
  white-space: nowrap;
  flex-shrink: 0;
}
#footer-hint {
  line-height: 1;
  transform: translateY(-1px);
  text-align: right;
  min-width: 0;
  flex: 1 1 auto;
}
.footer-options {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem 0.75rem;
  flex: 0 1 auto;
  min-width: 0;
  flex-wrap: nowrap;
  transform: translateY(-1px);
  line-height: 1;
}
.footer-opt {
  display: inline-flex;
  align-items: center;
  gap: 0.34rem;
  font-size: 0.68rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}
.footer-opt input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 0.8rem;
  height: 0.8rem;
  margin: 0;
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--panel);
  cursor: pointer;
  position: relative;
  vertical-align: middle;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.footer-opt input[type="checkbox"]:checked {
  background: var(--chip);
  border-color: color-mix(in srgb, var(--line) 55%, var(--muted));
}
.footer-opt input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0.34rem;
  height: 0.17rem;
  border-left: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: translate(-50%, -60%) rotate(-45deg);
}
.footer-opt input[type="checkbox"]:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}
.footer-opt:has(input:disabled) {
  cursor: not-allowed;
  opacity: 0.55;
}
.ft-sep {
  color: var(--muted);
  opacity: 0.55;
  padding: 0 0.15em;
  user-select: none;
}
.ft-stat {
  font-weight: 400;
}
.ft-rooms { color: #9a60b4; }
.ft-sensors { color: #5470c6; }
.ft-online { color: var(--ok); }
.ft-offline { color: var(--muted); }
.ft-hint {
  color: var(--muted);
  opacity: 0.75;
}

html[data-hide-offline="1"] .device.offline {
  display: none;
}

html[data-compact="1"] .rooms {
  gap: clamp(0.35rem, 1vh, 0.65rem);
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 210px), 1fr));
  padding-top: clamp(0.3rem, 0.8vh, 0.55rem);
  padding-bottom: clamp(0.4rem, 1vh, 0.75rem);
}
html[data-compact="1"] .room {
  --room-pad-top: 0.32rem;
  --room-pad-bottom: 0.45rem;
  --room-pad-x: 0.72rem;
}
html[data-compact="1"] .room-head {
  min-height: auto;
  padding-bottom: 0.32rem;
  margin-bottom: 0.32rem;
}
html[data-compact="1"] .room-name {
  font-size: clamp(0.72rem, 1.45vh, 0.86rem);
}
html[data-compact="1"] .room-daily {
  font-size: 0.58rem;
}
html[data-compact="1"] .room-avg {
  font-size: clamp(1.1rem, 2.8vh, 1.65rem);
}
html[data-compact="1"] .devices {
  gap: 0.12rem;
}
html[data-compact="1"] .device {
  font-size: clamp(0.64rem, 1.35vh, 0.8rem);
  gap: 0.35rem;
}
html[data-compact="1"] .room-badge {
  font-size: 0.52rem;
  padding: 0.2rem 0.42rem 0.16rem;
}

/* --------------------------------------------------------------- room grid */
.rooms {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
  grid-auto-rows: min-content;
  align-items: start;
  gap: clamp(0.6rem, 1.6vh, 1.1rem);
  padding: clamp(0.45rem, 1.2vh, 0.9rem) clamp(0.6rem, 2.4vw, 2rem) clamp(0.6rem, 1.8vh, 1.4rem);
  overflow: auto;
}

.room {
  --room-pad-x: clamp(1rem, 1.6vw, 1.5rem);
  --room-pad-top: clamp(0.45rem, 1vh, 0.65rem);
  --room-pad-bottom: clamp(0.7rem, 1.5vh, 1rem);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--room-pad-top) var(--room-pad-x) var(--room-pad-bottom);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.room--tappable {
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.room--tappable.room--hold:not(.room-sort-chosen) {
  cursor: grab;
}
.room--tappable:active:not(.room-sort-chosen) {
  border-color: var(--ok);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--ok) 15%, transparent);
}
.room-sort-ghost {
  opacity: 0.35;
  border: 2px dashed var(--ok);
  background: color-mix(in srgb, var(--ok) 7%, transparent);
}
.room-sort-chosen {
  cursor: grabbing;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.22);
  border-color: var(--ok);
}
.room-sort-drag {
  opacity: 0.96;
  cursor: grabbing;
}
.rooms--dragging {
  cursor: grabbing;
}
.rooms--dragging .rooms-divider {
  opacity: 0.45;
}

/* full-width divider before rooms not counted in the whole-home average */
.rooms-divider {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 0.5rem 0.2rem 0;
  color: var(--muted);
  font-size: clamp(0.6rem, 1.4vh, 0.75rem);
  font-weight: 400;
  letter-spacing: 1.5px;
  text-transform: uppercase;
}
.rooms-divider::before,
.rooms-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

.room-head {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  min-height: clamp(2.1rem, 5vh, 2.75rem);
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 0.5rem;
}
.room-name-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.12rem;
}
.room-name {
  flex: none;
  min-width: 0;
  font-size: clamp(0.8rem, 1.75vh, 0.95rem);
  font-weight: 400;
  letter-spacing: 0.2px;
  color: var(--text);
  line-height: 1.35;
  padding-bottom: 0.12em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.room-daily {
  font-size: clamp(0.62rem, 1.35vh, 0.72rem);
  color: var(--muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.room-daily .daily-min,
.room-daily .daily-max {
  font-weight: 600;
}
.room-daily .daily-min {
  margin-right: 0.18em;
}
.room-daily .daily-max {
  margin-left: 0.08em;
}
.room-badge-bar {
  display: flex;
  justify-content: flex-start;
  margin-top: auto;
  margin-left: calc(-1 * var(--room-pad-x));
  margin-bottom: calc(-1 * var(--room-pad-bottom) + 1px);
  padding-top: 0.35rem;
  flex-shrink: 0;
}
.room-badge {
  font-size: 0.58rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.26rem 0.55rem 0.22rem;
  border-radius: 0 6px 0 0;
  line-height: 1.2;
  border: 1px solid var(--line);
  border-left: none;
  border-bottom: none;
}
.room-badge--hot {
  color: color-mix(in srgb, var(--muted) 55%, #c45a4a);
  background: color-mix(in srgb, var(--panel) 88%, #e5604d);
}
.room-badge--cold {
  color: color-mix(in srgb, var(--muted) 55%, #3d7ab8);
  background: color-mix(in srgb, var(--panel) 88%, #4a90e2);
}
.room-avg {
  flex-shrink: 0;
  font-size: clamp(1.45rem, 4vh, 2.35rem);
  font-weight: 200;
  letter-spacing: -1px;
  line-height: 1;
  white-space: nowrap;
}
.room-avg .unit {
  font-size: 0.36em;
  color: var(--muted);
  font-weight: 400;
  letter-spacing: 0;
  vertical-align: super;
}
.room-avg.empty { color: var(--muted); }

/* device readings as compact rows inside the tile */
.devices {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}
.device {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.6rem;
  font-size: clamp(0.72rem, 1.6vh, 0.95rem);
}
.device .name {
  color: var(--muted);
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.device .val {
  font-weight: 400;
  white-space: nowrap;
  letter-spacing: -0.3px;
  color: var(--temp);
}
.device.offline {
  opacity: 0.28;
}
.device.offline .val {
  font-weight: 300;
  font-style: italic;
}

/* ----------------------------------------------------------------- banners */
.banner {
  position: fixed;
  left: 50%; bottom: 2vh;
  transform: translateX(-50%);
  background: var(--bad);
  color: #fff;
  padding: 0.7em 1.4em;
  border-radius: 12px;
  font-size: 0.9rem;
  font-weight: 400;
  box-shadow: 0 8px 30px #0002;
  max-width: 90vw;
}
.banner.warn { background: var(--warn); }
.hidden { display: none; }

/* During boot, the theme is already correct (set inline in <head>) but we have
   no data yet. Hide the value areas (keeping layout) so a refresh reveals real
   or cached values instead of flashing empty "--" placeholders. Cleared by
   revealApp() on the first render(). */
html[data-booting] #overall-main,
html[data-booting] #rooms {
  visibility: hidden;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  margin-top: 12vh;
  font-size: 1.1rem;
  font-weight: 300;
  line-height: 1.6;
}

/* --------------------------------------------------------- loading states */
/* fade real cards in once, when they first replace the skeleton */
.rooms.appear .room { animation: cardIn 0.5s ease both; }
@keyframes cardIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

/* shimmering skeleton placeholders */
.room.skeleton { pointer-events: none; }
.sk {
  border-radius: 7px;
  background: linear-gradient(
    100deg,
    var(--chip) 30%,
    var(--sk-hi) 50%,
    var(--chip) 70%
  );
  background-size: 400% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
}
@keyframes shimmer {
  0%   { background-position: 100% 0; }
  100% { background-position: 0 0; }
}
.sk-name { height: 1rem; width: 50%; }
.sk-avg  { height: 2rem; width: 32%; }
.sk-row  { height: 0.85rem; width: 100%; margin-bottom: 0.15rem; }
.sk-row:nth-child(odd) { width: 88%; }

/* inline value placeholders shown when structure is cached but values pending */
.sk-val {
  display: inline-block;
  width: clamp(2.6rem, 5vw, 3.4rem);
  height: 0.85em;
  vertical-align: middle;
}
.sk-avg-inline {
  display: inline-block;
  width: clamp(3rem, 7vw, 4.2rem);
  height: clamp(1.6rem, 4.5vh, 2.6rem);
  vertical-align: middle;
  border-radius: 9px;
}
.sk-overall {
  display: inline-block;
  width: clamp(4.5rem, 12vw, 7rem);
  height: clamp(1.8rem, 5vh, 3rem);
  border-radius: 9px;
}

/* --------------------------------------------------------- settings dialog */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 11, 16, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 1rem;
}
.overlay.hidden { display: none; }
.dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 18px;
  width: min(30rem, 100%);
  max-height: 90vh;
  overflow: auto;
  padding: 1.4rem 1.5rem 1.6rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.2rem;
}
.dialog-head h2 {
  font-size: 1.2rem;
  font-weight: 300;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.dialog-foot {
  display: flex;
  justify-content: flex-end;
  gap: 0.7rem;
  margin-top: 0.4rem;
}
.btn {
  font: inherit;
  font-size: 0.9rem;
  padding: 0.55em 1.5em;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid var(--line);
  transition: filter .15s, background .15s, color .15s;
}
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); border-color: var(--muted); }
.btn.primary {
  background: var(--ok);
  border-color: var(--ok);
  color: #fff;
  font-weight: 500;
}
.btn.primary:hover { filter: brightness(1.06); }

/* settings dialog */
.overlay:has(.settings-dialog) {
  padding: 0.5rem;
  align-items: center;
}
.dialog.settings-dialog {
  width: min(96vw, 56rem);
  max-width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  padding: 0.35rem 0.85rem 0.6rem;
  overflow: hidden;
  box-sizing: border-box;
}
.zone-slider--compact {
  padding: 0;
  max-width: 100%;
}
.settings-dialog .dialog-head,
.settings-head {
  margin: 0 0 0.5rem;
  flex-shrink: 0;
}
.settings-head h2 {
  margin: 0;
  min-width: 0;
}
.settings-dialog .dialog-head h2 {
  font-size: 1rem;
  letter-spacing: 0.12em;
}
.settings-dialog .dialog-foot {
  margin-top: 0.65rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}
.settings-body {
  flex: 1;
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.settings-card {
  background: color-mix(in srgb, var(--chip) 55%, transparent);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.35rem 1rem 0.85rem;
}
.settings-label,
.settings-field > label {
  display: block;
  font-size: 0.75rem;
  font-weight: 400;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.settings-section-title {
  margin: 0 0 0.15rem;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--text);
}
.setting-hint {
  margin: 0 0 0.55rem;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.35;
}
.govee-key-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}
.govee-key-row .text-input {
  flex: 1 1 auto;
  min-width: 0;
}
.govee-status {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--muted);
}
.amazon-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.info-dot {
  display: inline-block;
  margin-left: 0.35rem;
  font-size: 0.85em;
  color: var(--muted);
  cursor: help;
  opacity: 0.7;
}
.info-dot:hover,
.info-dot:focus { opacity: 1; }
.govee-status--ok { color: var(--ok); }
.govee-status--bad { color: var(--bad); }
.govee-device-row {
  margin-top: 0.6rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.govee-device-row select {
  flex: 1 1 auto;
  min-width: 0;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.55em 0.65em;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip);
  color: var(--text);
}
.govee-device-auto {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--text);
}
.govee-device-auto::before {
  content: "✓";
  color: var(--ok);
  font-weight: 700;
}
.govee-reading {
  margin-top: 0.4rem;
  font-size: 0.82rem;
  color: var(--text);
}
.settings-row--general {
  display: grid;
  grid-template-columns: minmax(0, 1.6fr) auto auto auto;
  gap: 0.75rem 1rem;
  align-items: end;
}
.settings-field {
  min-width: 0;
}
.settings-field--grow {
  min-width: 0;
}
.settings-field--refresh .text-input {
  width: 4.25rem;
}
.settings-num-wrap {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}
.settings-num-suffix {
  font-size: 0.78rem;
  color: var(--muted);
}
.seg--compact .seg-btn {
  padding: 0.48em 0.9em;
  font-size: 0.82rem;
  min-width: 3.1rem;
}
.settings-colors-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 1.25rem;
  align-items: end;
}
.settings-colors-swatches {
  display: flex;
  gap: 0.75rem;
}
.settings-colors-swatches .color-field {
  width: 3.75rem;
}
.settings-sync-light-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 1rem;
  align-items: center;
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--line);
}
.settings-sync-light-row .check-label {
  margin: 0;
  white-space: nowrap;
}
.settings-sync-light-row .notifier-pick {
  margin: 0;
  min-width: 0;
}
.settings-sync-light-row .notifier-pick select {
  width: 100%;
}
.settings-colors-swatches .color-field label {
  font-size: 0.75rem;
  margin-bottom: 0.3rem;
  color: var(--muted);
}
.settings-colors-swatches input[type="color"] {
  width: 100%;
  height: 2.65rem;
  padding: 0.2rem;
  border-radius: 8px;
  box-sizing: border-box;
}
.settings-colors-slider {
  min-width: 0;
}
.zone-slider--compact .zs-track {
  margin-top: 0.45rem;
  margin-bottom: 0.2rem;
}
.settings-card--renames .rename-row input {
  background: var(--bg);
}
.settings-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  align-items: start;
}
.settings-link-btn {
  margin-top: 0.45rem;
  padding: 0;
  border: none;
  background: none;
  color: var(--ok);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.settings-link-btn:hover {
  text-decoration: underline;
}
.hidden-sensors-panel {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.6rem 0.65rem;
}
.exclude-add-select {
  width: 100%;
  font: inherit;
  font-size: 0.88rem;
  padding: 0.48em 0.6em;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}
.hidden-sensors-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  list-style: none;
  margin: 0.5rem 0 0;
  padding: 0;
}
.hidden-sensors-empty {
  margin: 0.45rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}
.hidden-sensors-empty[hidden] {
  display: none;
}
.hidden-sensor-tag {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.35rem 0.45rem 0.35rem 0.6rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 0.84rem;
  line-height: 1.25;
}
.hidden-sensor-tag span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hidden-sensor-remove {
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.1rem 0.25rem;
  border-radius: 4px;
}
.hidden-sensor-remove:hover {
  color: var(--bad);
}

.setting { margin-bottom: 1.5rem; }
.setting.compact { margin-bottom: 1.1rem; }
.setting > label {
  display: block;
  font-size: 0.8rem;
  font-weight: 400;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.7rem;
}
.setting .hint { text-transform: none; letter-spacing: 0; opacity: 0.8; }
.setting.row { display: flex; gap: 1rem; }

/* segmented theme toggle */
.seg {
  display: flex;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.seg-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  padding: 0.6em 1.4em;
  cursor: pointer;
}
.seg-btn.on { background: var(--chip); color: var(--text); }

/* color pickers */
.color-field { flex: 1; }
.color-field label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.4rem;
}
.color-field input[type="color"] {
  width: 100%;
  height: 2.4rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip);
  cursor: pointer;
  padding: 0.2rem;
}

.wide-btn {
  width: 100%;
  background: var(--chip);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7em;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
}
.wide-btn:hover { border-color: var(--muted); }

.rename-list {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}
.text-input {
  width: 100%;
  box-sizing: border-box;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.setting-hint {
  margin: 0 0 0.45rem;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.35;
}
.rename-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr) auto;
  gap: 0.35rem;
  align-items: center;
}
.rename-row input {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.45em 0.55em;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--chip);
  color: var(--text);
  min-width: 0;
}
.rename-arrow { color: var(--muted); font-size: 0.8rem; text-align: center; }
.rename-del {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 1.1rem;
  line-height: 1;
  padding: 0.2rem 0.35rem;
}
.rename-del:hover { color: var(--bad); }

.check-label {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  cursor: pointer;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.95rem;
  color: var(--text);
  margin-bottom: 0;
}
.check-label input { width: 1rem; height: 1rem; cursor: pointer; accent-color: var(--ok); }
.notifier-pick { margin-top: 0.65rem; }
.notifier-pick select {
  width: 100%;
  font: inherit;
  font-size: 0.9rem;
  padding: 0.55em 0.65em;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--chip);
  color: var(--text);
}

/* dual-thumb temperature-zone slider */
.zone-slider { padding: 0 0.6rem; }
.zs-track {
  position: relative;
  height: 10px;
  border-radius: 999px;
  margin: 1.8rem 0 0.4rem;
  /* background gradient set live from JS */
}
.zs-handle {
  position: absolute;
  top: 50%;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--panel);
  border: 3px solid var(--muted);
  transform: translate(-50%, -50%);
  cursor: grab;
  touch-action: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}
.zs-handle:active { cursor: grabbing; }
.zs-val {
  position: absolute;
  bottom: 150%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
}
.zs-scale {
  display: flex;
  justify-content: space-between;
  font-size: 0.65rem;
  color: var(--muted);
}

@media (max-width: 720px) {
  .settings-row--general {
    grid-template-columns: 1fr 1fr;
  }
  .settings-field--grow {
    grid-column: 1 / -1;
  }
  .settings-colors-layout {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }
  .settings-colors-swatches {
    justify-content: flex-start;
  }
  .settings-sync-light-row {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }
  .settings-columns {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 480px) {
  .settings-row--general {
    grid-template-columns: 1fr;
  }
}

/* Mobile / single-column layout tweaks. */
@media (max-width: 600px) {
  /* Title spans the full width on top; avg + math + settings sit below it. */
  #topbar {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "brand brand"
      "main  right";
    row-gap: 0.55rem;
  }
  .topbar-left { grid-area: brand; }
  .overall-main { grid-area: main; justify-self: start; text-align: left; }
  .topbar-right { grid-area: right; justify-self: end; }

  /* Footer: drop the colored totals/hint; keep last-update + the two options,
     bigger so they're legible. */
  #footer-hint { display: none; }
  /* Compact mode isn't useful in single-column; hide its toggle on mobile. */
  .footer-opt[for="opt-compact"] { display: none; }
  .app-footer {
    height: auto;
    min-height: 2.9rem;
    max-height: none;
    padding-top: 0.4rem;
    padding-bottom: 0.4rem;
    font-size: 0.95rem;
    justify-content: space-between;
  }
  #footer-status { transform: none; font-size: 0.95rem; }
  .footer-options { gap: 1.1rem; transform: none; }
  .footer-opt { font-size: 0.92rem; gap: 0.5rem; transform: none; }
  .footer-opt input[type="checkbox"] {
    width: 1.15rem;
    height: 1.15rem;
    border-radius: 4px;
  }
  .footer-opt input[type="checkbox"]:checked::after {
    width: 0.48rem;
    height: 0.24rem;
    border-left-width: 2px;
    border-bottom-width: 2px;
  }
}

/* --------------------------------------------------------- history dialog */
.overlay:has(.history-dialog) {
  padding: 0.5rem;
  align-items: center;
}
.history-dialog {
  width: min(96vw, 78rem);
  max-height: 94vh;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 0.9rem 1.15rem 1.35rem;
  overflow: hidden;
}
.history-dialog .dialog-head,
.history-head,
.history-ranges,
.history-meta-bar {
  margin: 0;
}
.history-head h2 {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 0.5rem;
  text-transform: none;
  letter-spacing: 0.2px;
  font-weight: 400;
  font-size: 1.15rem;
}
.history-head-actions {
  display: flex;
  align-items: center;
  gap: 0.1rem;
  flex-shrink: 0;
}
.history-icon-btn {
  flex-shrink: 0;
  font-family: system-ui, sans-serif;
  width: 2rem;
  height: 2rem;
  border: none;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: color 0.15s, background 0.15s;
}
.history-icon-btn span {
  display: block;
  line-height: 1;
  pointer-events: none;
}
.history-icon-btn:hover {
  color: var(--text);
  background: var(--chip);
}
.history-close span {
  font-size: 1.45rem;
  transform: translateY(-4px);
}
.history-ranges {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.28rem;
  overflow-x: auto;
  padding-bottom: 0.05rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.history-ranges::-webkit-scrollbar {
  display: none;
}
.history-range {
  flex-shrink: 0;
  font: inherit;
  font-size: 0.74rem;
  padding: 0.32rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--chip);
  color: var(--text);
  cursor: pointer;
}
.history-range.on {
  border-color: var(--ok);
  background: color-mix(in srgb, var(--ok) 14%, transparent);
  color: var(--ok);
}
.history-chart-wrap {
  flex: 1;
  min-height: 0;
  background: var(--chip);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.55rem 0.45rem 0.5rem;
}
.history-chart {
  width: 100%;
  height: min(50vh, 400px);
  min-height: 200px;
  touch-action: pan-y pinch-zoom;
}
.history-meta-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1.25rem;
  padding-top: 0.15rem;
  flex-shrink: 0;
  min-height: 1.5rem;
}
.history-legend {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.2rem 0.35rem;
}
.history-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font: inherit;
  font-size: 0.72rem;
  color: var(--muted);
  max-width: 100%;
  padding: 0.18rem 0.4rem;
  border: none;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, opacity 0.12s;
}
.history-legend-item:hover,
.history-legend-item.is-active {
  background: var(--chip);
  color: var(--text);
}
.history-legend-item.is-dim {
  opacity: 0.4;
}
.history-legend-item i {
  flex-shrink: 0;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  display: block;
}
.history-legend-item > span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.history-legend-item--avg {
  color: var(--text);
  font-weight: 400;
}
.history-legend-item--avg i {
  width: 0.65rem;
  height: 0.65rem;
  box-shadow: 0 0 0 1px var(--history-avg);
}
.history-footer-meta {
  flex-shrink: 0;
  max-width: 42%;
  text-align: right;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.35;
  padding-top: 0.2rem;
}
