:root {
  --bg: #f3f1ec;
  --surface: #ffffff;
  --surface-2: #ebe8e1;
  --text: #1e1d1a;
  --muted: #6d6a62;
  --line: #ddd9cf;
  --accent: #c8860d;
  --accent-soft: #fbefd5;
  --cool: #2f6f9f;
  --ok: #2f7d4f;
  --warn: #b5481f;
  --warn-soft: #fbe3d9;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141412;
    --surface: #1f1e1b;
    --surface-2: #2a2925;
    --text: #efece5;
    --muted: #a19d93;
    --line: #34322d;
    --accent: #f0b13b;
    --accent-soft: #3a2f17;
    --cool: #6aa9d8;
    --ok: #5cbf86;
    --warn: #f08a62;
    --warn-soft: #3d231a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
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;
}
button { font: inherit; color: inherit; }

/* ---------- barra superiore ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 18px; margin: 0; flex: 1; letter-spacing: -0.01em; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--warn); }
.link { background: none; border: 0; color: var(--muted); font-size: 13px; padding: 6px; cursor: pointer; }

main { max-width: 980px; margin: 0 auto; padding: 12px 16px calc(88px + env(safe-area-inset-bottom)); }

/* ---------- riepilogo ---------- */
.summary { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 18px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line);
  font-size: 13px;
}
.chip.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.chip button { all: unset; cursor: pointer; font-weight: 600; }

/* ---------- ambienti ---------- */
.room { margin-bottom: 22px; }
.room h2 {
  font-size: 13px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); margin: 0 0 8px 2px; font-weight: 600;
}
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }

.tile {
  position: relative;
  display: flex; flex-direction: column; gap: 6px;
  min-height: 84px; padding: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  text-align: left; cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
.tile:active { transform: scale(.98); }
.tile .name { font-weight: 600; font-size: 14px; overflow-wrap: anywhere; }
.tile .state { color: var(--muted); font-size: 13px; }
.tile svg { width: 22px; height: 22px; color: var(--muted); }
.tile.on { background: var(--accent-soft); border-color: transparent; }
.tile.on svg { color: var(--accent); }
.tile.pending { opacity: .6; }
.tile.wide { grid-column: span 2; }
.tile.static { cursor: default; }
.tile.static:active { transform: none; }

input[type=range] { width: 100%; accent-color: var(--accent); margin: 2px 0 0; }

.cover-btns { display: flex; gap: 6px; margin-top: auto; }
.cover-btns button {
  flex: 1; padding: 8px 0; border-radius: 10px; cursor: pointer;
  background: var(--surface-2); border: 0; font-size: 15px;
}

/* ---------- termostato ---------- */
.zone { grid-column: span 2; cursor: default; }
.zone:active { transform: none; }
.zone .row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.zone .temp { font-size: 32px; font-weight: 650; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.zone .set { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.zone .set button {
  width: 40px; height: 40px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--surface-2); font-size: 20px; line-height: 1;
}
.zone .set .target { flex: 1; text-align: center; font-variant-numeric: tabular-nums; }
.zone .set .target b { font-size: 18px; }
.zone .auto { padding: 0 12px; width: auto !important; border-radius: 999px !important; font-size: 13px !important; }
.zone.heating svg { color: var(--accent); }
.note { font-size: 12px; color: var(--warn); }

/* ---------- antifurto ---------- */
.alarm-zones { display: grid; gap: 6px; margin-top: 4px; }
.alarm-zones div { display: flex; justify-content: space-between; font-size: 13px; }
.bad { color: var(--warn); font-weight: 600; }
.good { color: var(--ok); }

/* ---------- vari ---------- */
.readonly button:not(.link), .readonly input { pointer-events: none; opacity: .55; }
.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 7;
  max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form {
  width: 100%; max-width: 340px; display: grid; gap: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px;
}
.login h1 { margin: 0 0 4px; font-size: 20px; }
.login label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login input {
  font: inherit; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
}
.login button {
  padding: 11px; border: 0; border-radius: 10px; cursor: pointer;
  background: var(--accent); color: #1e1d1a; font-weight: 650;
}
.login .err { color: var(--warn); font-size: 13px; margin: 0; }

/* ---------- automazioni ---------- */
.autoview { max-width: 980px; margin: 0 auto; padding: 12px 16px 48px; }
.primary, .secondary {
  border: 0; border-radius: 999px; padding: 8px 14px; cursor: pointer; font-weight: 600; font-size: 13px;
}
.primary { background: var(--accent); color: #1e1d1a; }
.secondary { background: var(--surface-2); justify-self: start; }
.rules { display: grid; gap: 8px; }
@media (min-width: 720px) { .rules { grid-template-columns: 1fr 1fr; } }
.rule .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.rule .acts { font-size: 13px; }
.rule .meta { display: grid; gap: 2px; font-size: 12px; color: var(--muted); }
.rule.disabled { opacity: .6; }
.rule .cover-btns button { font-size: 13px; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--surface-2); transition: background .15s; }
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface); box-shadow: 0 1px 2px rgba(0,0,0,.3); transition: transform .15s;
}
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(18px); }
.editor { display: grid; gap: 12px; max-width: 560px; margin: 0 auto; }
.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.editor input, .editor select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; min-width: 0;
}
.offset { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.days { display: flex; gap: 6px; flex-wrap: wrap; }
.day { position: relative; }
.day input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.day span {
  display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%;
  background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 13px;
}
.day input:checked + span { background: var(--accent); color: #1e1d1a; }
.day input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.label { font-size: 13px; color: var(--muted); }
.actions { display: grid; gap: 8px; }
.action-row {
  display: grid; grid-template-columns: 1fr 90px 36px; gap: 6px; align-items: center;
  padding: 8px; border-radius: 12px; background: var(--surface-2);
}
.action-row select:first-child { grid-column: 1 / -1; }
.action-row input[hidden] ~ .del { grid-column: 3; }
.action-row select:nth-child(2):has(~ input[hidden]) { grid-column: 1 / 3; }
.action-row .del {
  width: 36px; height: 36px; border-radius: 50%; border: 0; cursor: pointer; background: var(--surface); font-size: 18px;
}
.cover-btns .primary { background: var(--accent); color: #1e1d1a; }

/* ---------- navigazione ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: center; gap: 4px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.tabs a {
  flex: 1; max-width: 140px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 4px; border-radius: 12px;
  color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 600;
}
.tabs a svg { width: 22px; height: 22px; }
.tabs a.active { color: var(--accent); background: var(--accent-soft); }

/* ---------- home ---------- */
.rooms .room-card { text-decoration: none; color: inherit; min-height: 96px; }
.room-card .room-temp { font-size: 22px; font-weight: 650; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; min-height: 1.2em; }
.scene-row { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.scene-btn {
  flex: none; display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-radius: 999px; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); font-weight: 600; font-size: 14px;
}
.scene-btn svg, .scene-title svg { width: 20px; height: 20px; color: var(--accent); }
.scene-btn:active { transform: scale(.97); }
.scene-btn.pending, .rule.pending { opacity: .6; }

/* ---------- pagina ambiente ---------- */
.page-head { display: flex; align-items: center; gap: 10px; margin: 4px 0 14px; }
.page-head h2 { flex: 1; margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.back { display: inline-flex; align-items: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: 14px; }
.back svg { width: 20px; height: 20px; }

/* ---------- scenari ---------- */
.scene-title { display: flex; align-items: center; gap: 8px; min-width: 0; }
.rule .row .primary { flex: none; }
.cover-btns .narrow { flex: 0 0 44px; }
.cover-btns button:disabled { opacity: .35; cursor: default; }
.icon-pick { display: flex; gap: 6px; flex-wrap: wrap; }
.icon-pick button {
  width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); color: var(--muted); cursor: pointer; display: grid; place-items: center;
}
.icon-pick button svg { width: 22px; height: 22px; }
.icon-pick button.sel { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.readonly .scene-btn { pointer-events: none; }

/* ---------- clima ---------- */
.hint-box { font-size: 13px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 4px 0 14px; }
.holiday { margin-bottom: 18px; gap: 8px; }
.holiday.on { background: var(--accent-soft); border-color: transparent; }
.inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.inline input, .inline select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-width: 0;
}
.inline select { flex: 1; }
.inline input[type=number] { width: 80px; }
.frost { margin-top: 14px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.section-head h2 { margin: 0; }
.zone-card .zone-now { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; }
.zone-card.managed { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.zone-card .set button, .zone-card .set .auto { width: 40px; height: 40px; border-radius: 50%; border: 0; cursor: pointer; background: var(--surface-2); font-size: 20px; }
.zone-card .set { display: flex; align-items: center; gap: 8px; }
.zone-card .set .target { flex: 1; text-align: center; }
.zone .src { color: var(--accent); }
.week { display: grid; gap: 4px; }
.week-row { display: grid; grid-template-columns: 26px 1fr; align-items: center; gap: 6px; cursor: default; border-radius: 6px; }
.editor .week-row { cursor: pointer; padding: 2px; }
.week-row.sel { outline: 2px solid var(--accent); outline-offset: 1px; }
.week-row .d { font-size: 12px; color: var(--muted); font-weight: 600; }
.daybar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.daybar span { height: 100%; }
.week-scale { display: flex; justify-content: space-between; padding-left: 32px; font-size: 10px; color: var(--muted); }
.daytab {
  width: 38px; height: 38px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: 13px;
}
.daytab.sel { background: var(--accent); color: #1e1d1a; }
.band-row {
  display: grid; grid-template-columns: auto 1fr auto 72px auto 36px; gap: 6px; align-items: center;
  padding: 8px; border-radius: 12px; background: var(--surface-2); font-size: 13px; color: var(--muted);
}
.band-row .del { width: 36px; height: 36px; border-radius: 50%; border: 0; cursor: pointer; background: var(--surface); font-size: 18px; }
.empty.small { padding: 8px 0; font-size: 13px; }
.wrap { flex-wrap: wrap; }

/* ---------- impostazioni ---------- */
.wide-editor { max-width: none; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.progress { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.chip-btn { border: 1px solid var(--accent); background: var(--accent-soft); color: var(--text); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 13px; }
.room-row { display: flex; align-items: center; gap: 6px; }
.room-row input:not([type=checkbox]), .room-row select {
  flex: 1; min-width: 0; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}
.room-row .name { flex: 1; }
.room-row input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.mini { width: 34px; height: 34px; flex: none; border: 0; border-radius: 10px; background: var(--surface-2); color: var(--text); cursor: pointer; }
.mini.wide { width: auto; padding: 0 10px; font-size: 13px; }
.dev-list { display: grid; gap: 8px; margin-bottom: 10px; }
@media (min-width: 720px) { .dev-list { grid-template-columns: 1fr 1fr; } }
.dev-row { display: grid; gap: 6px; padding: 10px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); }
.dev-row.off { opacity: .5; }
.dev-top { display: flex; align-items: center; gap: 8px; }
.dev-top input { width: 18px; height: 18px; accent-color: var(--accent); }
.dev-top .addr { font-weight: 650; font-variant-numeric: tabular-nums; }
.dev-top .mini { margin-left: auto; }
.dev-row > input, .dev-sel select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-width: 0; width: 100%;
}
.dev-sel { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.badge { font-size: 10px; font-weight: 700; letter-spacing: .05em; padding: 2px 6px; border-radius: 6px; }
.badge.new { background: var(--accent); color: #1e1d1a; }
.badge.found { background: color-mix(in srgb, var(--ok) 25%, transparent); color: var(--ok); }
.badge.missing { background: var(--warn-soft); color: var(--warn); }
.badge.conf { display: none; }
.big { width: 100%; padding: 12px; font-size: 15px; margin-top: 12px; }
.overlay { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 16px; }
.qr-box { width: 100%; max-width: 340px; gap: 10px; }
.qr { display: grid; place-items: center; padding: 8px; background: #fff; border-radius: 12px; }
.qr svg { width: 100%; max-width: 260px; height: auto; }
.qr-box input { font: inherit; font-size: 12px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 8px; }
.login-msg { max-width: 340px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; }
.login-msg h1 { margin: 0 0 8px; font-size: 20px; }
.login-msg a { color: var(--accent); }
.editor > *, .room-row > * { min-width: 0; }
.room-row input:not([type=checkbox]) { width: 100%; }

/* ---------- serrature ---------- */
.lock-row { display: grid; gap: 6px; padding: 10px; border-radius: 12px; background: var(--surface-2); }
.lock-row textarea {
  font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; resize: vertical; min-width: 0;
}
.lock-row .room-row input[type=number] { flex: 0 0 90px; }
.frames { font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg); border-radius: 10px; padding: 8px 10px; overflow-x: auto; margin: 4px 0; }
.lock-btn svg { color: var(--warn); }
