/* Home Dashboard styles: light/dark via custom properties, mobile first. */
:root {
  --bg: #f3f5f8;
  --card: #ffffff;
  --text: #17202a;
  --muted: #566372;
  --border: #d5dbe3;
  --accent: #1560d4;
  --accent-text: #ffffff;
  --seg-bg: #e8ecf1;
  --good: #0b6b3a;  --good-bg: #d8f3e3;
  --fair: #7a4a00;  --fair-bg: #fdebc4;
  --poor: #9b1c1c;  --poor-bg: #fcdcdc;
  --err-bg: #fcdcdc; --err-text: #7f1414;
  --shadow: 0 1px 3px rgba(20, 30, 50, .1);
  --tint-lg: #b4234f; --tint-st: #0b7a8a; --tint-kasa: #7c4dbd;
  color-scheme: light;
}
/* Dark palette: follows the system unless the toggle saved a choice (data-theme on <html>).
   The two blocks below must stay identical. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1419;
    --card: #1a2129;
    --text: #e8edf2;
    --muted: #9aa8b6;
    --border: #2f3a46;
    --accent: #5b9dff;
    --accent-text: #08111f;
    --seg-bg: #26303b;
    --good: #7be0a6;  --good-bg: #123a26;
    --fair: #f6cf74;  --fair-bg: #3d2f0c;
    --poor: #ff9a9a;  --poor-bg: #471a1a;
    --err-bg: #471a1a; --err-text: #ffc4c4;
    --shadow: none;
    --tint-lg: #ff8fb0; --tint-st: #5fd3e3; --tint-kasa: #c4a2ff;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1419;
  --card: #1a2129;
  --text: #e8edf2;
  --muted: #9aa8b6;
  --border: #2f3a46;
  --accent: #5b9dff;
  --accent-text: #08111f;
  --seg-bg: #26303b;
  --good: #7be0a6;  --good-bg: #123a26;
  --fair: #f6cf74;  --fair-bg: #3d2f0c;
  --poor: #ff9a9a;  --poor-bg: #471a1a;
  --err-bg: #471a1a; --err-text: #ffc4c4;
  --shadow: none;
  --tint-lg: #ff8fb0; --tint-st: #5fd3e3; --tint-kasa: #c4a2ff;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3, p { margin: 0; }
.muted { color: var(--muted); font-size: .875rem; }

.top, main { max-width: 1200px; margin: 0 auto; padding: 16px; }
.top { padding-bottom: 0; }
.top-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; justify-content: space-between; }
h1 { font-size: 1.5rem; }
.top-actions { display: flex; align-items: center; gap: 12px; }
.providers { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.providers li { font-size: .875rem; color: var(--muted); }
.providers li.bad { color: var(--err-text); }
.badge {
  display: inline-block; padding: 1px 8px; margin-left: 6px; border-radius: 999px;
  background: var(--fair-bg); color: var(--fair); font-size: .75rem; font-weight: 600;
}

/* Buttons and focus */
button, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, select:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
button:disabled, select:disabled { cursor: progress; opacity: .6; }
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--card);
}
/* Theme switch: three icon buttons, selected one highlighted */
.theme-switch { display: flex; gap: 2px; padding: 3px; border: 1px solid var(--border); border-radius: 12px; background: var(--seg-bg); }
.theme-switch button {
  display: grid; place-items: center; width: 40px; height: 38px; padding: 0;
  border: 0; border-radius: 9px; background: transparent; color: var(--muted);
}
.theme-switch button:hover { color: var(--text); }
.theme-switch button[aria-pressed="true"] { background: var(--card); color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.theme-switch svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Sections and grid */
.section h2 { font-size: 1rem; margin: 8px 0; }
.section-toggle {
  display: flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 8px 0 4px; margin-left: -4px;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  font-weight: 700; letter-spacing: .02em;
}
.section-toggle:hover { color: var(--text); }
.chevron { display: grid; place-items: center; transition: transform .15s ease; }
.section-toggle[aria-expanded="false"] .chevron { transform: rotate(-90deg); }
.chevron svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
@media (prefers-reduced-motion: reduce) { .chevron { transition: none; } }
.grid { display: grid; grid-template-columns: 1fr; gap: 16px; }
.grid[hidden] { display: none; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); } }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  padding: 16px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px;
}
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card h3 { font-size: 1.15rem; }
.status { font-size: .875rem; color: var(--muted); display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--good); }
.dot.off { background: transparent; border: 2px solid var(--muted); }

.power {
  min-height: 64px; width: 100%; border-radius: 14px; font-size: 1.2rem; font-weight: 700;
  border: 2px solid var(--border); background: var(--seg-bg);
}
.power[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.air { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }
.q { padding: 4px 12px; border-radius: 999px; font-weight: 600; font-size: .9rem; }
.q-good { background: var(--good-bg); color: var(--good); }
.q-fair { background: var(--fair-bg); color: var(--fair); }
.q-poor { background: var(--poor-bg); color: var(--poor); }
.q-none { background: var(--seg-bg); color: var(--muted); }

/* Controls dim when the device is off, but stay tappable */
.controls { display: flex; flex-direction: column; gap: 12px; }
.card.is-off .controls { opacity: .55; }
.field > label, .field > .label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 4px; }
.seg { display: flex; gap: 4px; padding: 4px; background: var(--seg-bg); border-radius: 12px; }
.seg button {
  flex: 1; min-height: 44px; min-width: 0; padding: 0 6px; border: 0; border-radius: 9px;
  background: transparent; font-size: .9rem;
}
.seg button[aria-pressed="true"] { background: var(--card); box-shadow: 0 0 0 2px var(--accent) inset; font-weight: 700; }
.seg.dim button[aria-pressed="true"] { box-shadow: 0 0 0 1px var(--border) inset; font-weight: 500; opacity: .7; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.toggle {
  min-height: 44px; border-radius: 10px; border: 1px solid var(--border); background: var(--seg-bg);
  padding: 0 12px; text-align: left;
}
.toggle[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
select {
  width: 100%; min-height: 44px; padding: 0 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--seg-bg);
}

.card-error { background: var(--err-bg); color: var(--err-text); border-radius: 10px; padding: 8px 12px; font-size: .9rem; }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

/* Brand tag: the text is the cue, the tint is secondary */
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; margin-top: 2px; }
.brand {
  display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: .75rem; font-weight: 600;
  color: var(--text); background: var(--seg-bg); border: 1px solid var(--border);
}
.brand-winix {
  background: color-mix(in srgb, var(--accent) 16%, var(--card));
  border-color: color-mix(in srgb, var(--accent) 45%, var(--card));
}
.brand-vesync {
  background: color-mix(in srgb, var(--good) 16%, var(--card));
  border-color: color-mix(in srgb, var(--good) 45%, var(--card));
}

/* Rename */
.head-main { min-width: 0; flex: 1; }
.name-row { display: flex; align-items: center; gap: 2px; }
.name-row h3 { overflow-wrap: anywhere; }
.icon-btn {
  display: grid; place-items: center; flex: none; width: 44px; height: 44px; margin: -8px 0;
  padding: 0; border: 0; border-radius: 10px; background: transparent; color: var(--muted);
}
.icon-btn:hover { color: var(--text); background: var(--seg-bg); }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.name-edit { display: flex; flex-direction: column; gap: 8px; }
.name-input {
  width: 100%; min-height: 44px; padding: 0 10px; border-radius: 10px; font: inherit; font-size: 1.05rem;
  color: var(--text); border: 1px solid var(--border); background: var(--seg-bg);
}
.name-input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.name-input:disabled { opacity: .6; }
.name-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.name-actions .btn { min-height: 44px; padding: 0 14px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.original { margin-top: 4px; overflow-wrap: anywhere; }

/* Drag to reorder */
.drag-handle {
  display: grid; place-items: center; flex: none; width: 40px; height: 44px; margin: -8px 0 -8px -8px; padding: 0;
  border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  cursor: grab; touch-action: none; -webkit-user-select: none; user-select: none;
}
.drag-handle:hover { color: var(--text); background: var(--seg-bg); }
.drag-handle:disabled { cursor: not-allowed; opacity: .35; }
.drag-handle svg { width: 20px; height: 20px; fill: currentColor; }
.card.dragging {
  box-shadow: 0 14px 34px rgba(0, 0, 0, .3); opacity: .92; transform: scale(1.02);
  cursor: grabbing;
}
.card-placeholder { border: 2px dashed var(--accent); border-radius: 16px; background: var(--seg-bg); opacity: .7; }
html.drag-active, html.drag-active * { cursor: grabbing !important; -webkit-user-select: none; user-select: none; }

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Provider status lines: optional setup action next to the text */
.providers li { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 8px; }
.providers .badge { margin-left: 0; }
.btn-link {
  display: inline-flex; align-items: center; text-decoration: none; color: var(--accent-text);
  background: var(--accent); border-color: var(--accent); font-weight: 600; font-size: .9rem;
}
.btn-link:hover { filter: brightness(1.08); }
.btn-link:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* One-off notice banner (?notice=...) */
.notice {
  max-width: 1168px; margin: 12px auto 0; padding: 8px 8px 8px 14px; border-radius: 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--seg-bg); border: 1px solid var(--accent); color: var(--text);
}
@media (max-width: 1200px) { .notice { margin-left: 16px; margin-right: 16px; } }

/* More brand tints */
.brand-lgthinq {
  background: color-mix(in srgb, var(--tint-lg) 16%, var(--card));
  border-color: color-mix(in srgb, var(--tint-lg) 45%, var(--card));
}
.brand-smartthings {
  background: color-mix(in srgb, var(--tint-st) 16%, var(--card));
  border-color: color-mix(in srgb, var(--tint-st) 45%, var(--card));
}
.brand-kasa {
  background: color-mix(in srgb, var(--tint-kasa) 16%, var(--card));
  border-color: color-mix(in srgb, var(--tint-kasa) 45%, var(--card));
}

/* Segmented controls with icons (AC modes) */
.seg.with-icons { flex-wrap: wrap; }
.seg button.has-icon {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  flex: 1 1 64px; padding: 4px 6px; font-size: .75rem;
}
.seg button svg, .step-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.seg button { overflow-wrap: anywhere; }

/* Steppers (AC target, appliance numbers) */
.step { display: flex; align-items: center; gap: 8px; }
.step-btn {
  flex: none; min-width: 44px; min-height: 44px; padding: 0; border-radius: 10px; font-size: 1.5rem; line-height: 1;
  border: 1px solid var(--border); background: var(--seg-bg);
}
.step-btn[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
.step-value { min-width: 4.5ch; text-align: center; font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Air conditioner */
.ac-temps { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px 20px; }
.ac-temps .label { display: block; font-size: .8rem; color: var(--muted); margin-bottom: 2px; }
.temp-now .num { font-size: 2.75rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.card.is-off .temp-now, .card.is-off .setpoint { opacity: .6; }

/* Appliances */
.appl-top { display: flex; align-items: center; gap: 10px; }
.appl-icon { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; background: var(--seg-bg); color: var(--muted); }
.appl-icon svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.readings, .c-group { display: flex; flex-direction: column; gap: 6px; }
.readings { gap: 12px; }
.r-group { display: flex; flex-direction: column; gap: 4px; }
.card h4 { margin: 0; font-size: .8rem; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.reading { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 10px; background: var(--seg-bg); }
.r-label { color: var(--muted); font-size: .9rem; }
.r-value { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.reading.alert { background: var(--fair-bg); color: var(--fair); border: 1px solid currentColor; }
.reading.alert .r-label { color: inherit; }
.r-icon svg { display: block; width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.r-flag { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; padding: 1px 6px; border-radius: 999px; border: 1px solid currentColor; }
.c-list { display: flex; flex-direction: column; gap: 10px; }
.card-appliance .controls { gap: 16px; }
.appl-group { display: flex; flex-direction: column; gap: 8px; }
.appl-group + .appl-group { padding-top: 4px; }

/* Plugs: compact cards */
.grid-compact { gap: 12px; }
@media (min-width: 640px) { .grid.grid-compact { grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); } }
.card-plug { padding: 12px; gap: 10px; }
.card-plug h3 { font-size: 1.05rem; }
.parent { margin-top: 2px; }
.plug-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.energies { display: flex; flex-wrap: wrap; gap: 2px 12px; color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; }
.energy:first-child:not(:only-child) { color: var(--text); font-weight: 600; }
.power.power-sm { width: auto; min-width: 88px; min-height: 44px; margin-left: auto; font-size: 1rem; border-radius: 12px; }

/* Lights */
.slider-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.slider-head label { font-size: .8rem; color: var(--muted); }
.slider-value { font-weight: 700; font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; min-height: 44px; margin: 0; accent-color: var(--accent); }
input[type="range"]:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
input[type="range"]:disabled { opacity: .6; }
