:root {
  --bg: #f6f2ec; --surface: #ffffff; --surface-2: #f0ebe3; --line: #e3dcd2;
  --text: #221f1c; --muted: #7a7169; --accent: #b4532a; --accent-soft: #f6e3d9;
  --heat: #c9602f; --cool: #2f6fb4; --cool-soft: #dfeaf7; --ok: #3a8f5c; --warn: #c48a12; --bad: #b8322f;
  --on: #2f7d4f; --on-soft: #dff1e6; --off: #9a928a; --shadow: 0 1px 2px rgba(40,30,20,.06), 0 6px 20px rgba(40,30,20,.06);
  --radius: 16px; --tap: 44px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17161a; --surface: #211f24; --surface-2: #2a272d; --line: #363239;
    --text: #f1ece6; --muted: #a39b92; --accent: #e07a4b; --accent-soft: #3b2a22;
    --heat: #e37c48; --cool: #6aa3e6; --cool-soft: #1f2d3f; --ok: #5cbf83; --warn: #e0a93a; --bad: #e0574f;
    --on: #5cbf83; --on-soft: #1f3328; --off: #6f6861; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.4 -apple-system, "SF Pro Text", Helvetica, Arial, sans-serif; }
body { padding-bottom: calc(68px + env(safe-area-inset-bottom)); min-height: 100vh; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: var(--tap); }
input[type=checkbox] { min-height: 0; width: 20px; height: 20px; accent-color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.wide { width: 100%; }

/* top */
.top { position: sticky; top: 0; z-index: 10; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); padding: calc(10px + env(safe-area-inset-top)) 16px 8px; border-bottom: 1px solid var(--line); }
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 14px; height: 14px; border-radius: 50%; background: linear-gradient(135deg, var(--heat), var(--cool)); box-shadow: 0 0 0 3px var(--accent-soft); }
.brand-name { font-weight: 700; letter-spacing: -.01em; }
.top-right { display: flex; gap: 8px; align-items: center; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 6px 11px; border-radius: 999px; background: var(--surface-2); font-size: .9rem; font-weight: 600; min-height: 34px; white-space: nowrap; }
.chip-mode.heat { background: var(--accent-soft); color: var(--heat); }
.chip-mode.cool { background: var(--cool-soft); color: var(--cool); }
.status-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--off); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.dot.bad { background: var(--bad); }
.pending { color: var(--accent); font-weight: 600; }

/* layout */
main { padding: 14px 16px 24px; max-width: 920px; margin: 0 auto; }
.section { margin: 18px 0 8px; display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.section h2 { font-size: 1rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; }
.section .actions { display: flex; gap: 6px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: var(--tap); padding: 8px 14px; border-radius: 12px; background: var(--surface-2); font-weight: 600; }
.btn.small { min-height: 34px; padding: 4px 10px; font-size: .85rem; border-radius: 10px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: color-mix(in srgb, var(--bad) 14%, var(--surface)); color: var(--bad); }
.btn.ghost { background: transparent; color: var(--accent); }
.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 4px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.stat .v { font-size: 1.5rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; }
.stat .l { color: var(--muted); font-size: .8rem; margin-top: 2px; }
.quick { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 2px; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.quick .btn { flex: none; background: var(--surface); box-shadow: var(--shadow); }

/* room cards */
.grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
@media (min-width: 640px) { .grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.room { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; border-left: 4px solid var(--off); }
.room.on { border-left-color: var(--on); }
.room.on.heat { border-left-color: var(--heat); }
.room.on.cool { border-left-color: var(--cool); }
.room.err { border-left-color: var(--bad); }
.room-head { display: flex; align-items: center; gap: 10px; }
.room-name { flex: 1; min-width: 0; }
.room-name .n { font-weight: 650; display: flex; align-items: center; gap: 6px; }
.room-name .n button { color: var(--muted); font-size: .9rem; }
.room-name .s { color: var(--muted); font-size: .82rem; margin-top: 1px; overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; }
.temp { font-size: 1.65rem; font-weight: 700; letter-spacing: -.03em; min-width: 64px; text-align: right; line-height: 1; flex: none; }
.temp small { font-size: .9rem; font-weight: 500; color: var(--muted); margin-left: 1px; }
.switch { width: 52px; height: 32px; border-radius: 999px; background: var(--off); position: relative; flex: none; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s; }
.switch.on { background: var(--on); }
.switch.on::after { transform: translateX(20px); }
.room-ctl { display: flex; align-items: center; justify-content: space-between; gap: 8px 12px; margin-top: 10px; flex-wrap: wrap; }
.stepper { display: inline-flex; align-items: center; background: var(--surface-2); border-radius: 12px; flex: none; }
.stepper button { width: 42px; height: 40px; font-size: 1.3rem; font-weight: 600; }
.stepper .val { min-width: 54px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.fans { display: flex; gap: 4px; flex: 1 1 200px; justify-content: flex-end; }
.fans button { flex: 0 1 36px; min-width: 28px; height: 36px; border-radius: 9px; background: var(--surface-2); font-size: .82rem; font-weight: 600; color: var(--muted); }
.fans .fl { font-size: .72rem; color: var(--muted); align-self: center; margin-right: 2px; }
.fans button.sel { background: var(--text); color: var(--bg); }
.lock { font-size: .8rem; color: var(--warn); }
.st { display: inline-block; font-size: .72rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; margin-right: 5px; white-space: nowrap; }
.st.heat { background: var(--accent-soft); color: var(--heat); }
.st.cool { background: var(--cool-soft); color: var(--cool); }
.st.ok { background: var(--on-soft); color: var(--on); }
.st.off { background: var(--surface-2); color: var(--muted); }
.st.bad { background: var(--bad); color: #fff; }
.lab { font-size: .56rem; text-transform: uppercase; letter-spacing: .04em; opacity: .6; font-weight: 700; white-space: nowrap; }
.sub { font-size: .74rem; font-weight: 500; letter-spacing: 0; color: var(--muted); white-space: nowrap; }
.sub b { color: var(--text); font-weight: 700; }
.tb { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.05; }
.tb .lab { margin-top: 3px; } .tb .sub { margin-top: 3px; }
.sb { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.room-sub { color: var(--muted); font-size: .82rem; margin-top: 7px; line-height: 1.55; }
.room-sub .wifi-n { white-space: nowrap; }   /* i due numeri dei collegati al Wi-Fi restano insieme sulla stessa riga */

/* scenes */
.scene { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; display: flex; align-items: center; gap: 14px; }
.scene .ico { font-size: 1.7rem; }
.scene .body { flex: 1; }
.scene .d { color: var(--muted); font-size: .85rem; }

/* schedules */
.sched { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; display: flex; gap: 14px; align-items: center; }
.sched.off { opacity: .55; }
.sched .t { font-size: 1.5rem; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; min-width: 72px; }
.sched .body { flex: 1; min-width: 0; overflow: hidden; }
.sched .days { display: flex; gap: 3px; margin-top: 4px; flex-wrap: wrap; }
.days span { font-size: .72rem; width: 22px; height: 22px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; background: var(--surface-2); color: var(--muted); }
.days span.on { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.daypick { display: flex; gap: 6px; }
.daypick button { flex: 1; height: 40px; border-radius: 10px; background: var(--surface-2); font-weight: 600; color: var(--muted); }
.daypick button.on { background: var(--accent); color: #fff; }

/* forms, sheet */
.form { display: grid; gap: 12px; }
.form label { display: grid; gap: 6px; font-size: .9rem; color: var(--muted); }
.form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.targets { display: flex; flex-wrap: wrap; gap: 6px; }
.targets button { min-height: 36px; padding: 6px 12px; border-radius: 999px; background: var(--surface-2); font-size: .88rem; font-weight: 600; color: var(--muted); }
.targets button.on { background: var(--text); color: var(--bg); }
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-panel { position: absolute; left: 0; right: 0; bottom: 0; background: var(--bg); border-radius: 20px 20px 0 0; padding: 8px 16px calc(20px + env(safe-area-inset-bottom)); max-height: 90vh; overflow: auto; box-shadow: 0 -8px 30px rgba(0,0,0,.2); animation: up .22s ease-out; }
@media (min-width: 640px) { .sheet-panel { left: 50%; right: auto; width: 560px; transform: translateX(-50%); border-radius: 20px; bottom: 5vh; } }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 12px; }
.sheet h2 { margin-bottom: 10px; }
.sheet-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: .92rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; font-weight: 600; text-align: right; }
.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.toggle-row:last-child { border-bottom: 0; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; }
.list .card { margin-bottom: 10px; }
.event { display: grid; grid-template-columns: 54px 1fr; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: .88rem; }
.event .ts { color: var(--muted); font-variant-numeric: tabular-nums; }
.event.bad .a { color: var(--bad); }

/* chart */
.chart { width: 100%; height: 220px; display: block; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .room { fill: none; stroke: var(--heat); stroke-width: 2; stroke-linejoin: round; }
.chart .set { fill: none; stroke: var(--text); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .onband { fill: var(--on); opacity: .12; }
.legend { display: flex; gap: 14px; font-size: .82rem; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 18px; height: 0; border-top: 2px solid var(--heat); vertical-align: middle; margin-right: 5px; }
.legend i.set { border-top: 2px dashed var(--text); }
.legend i.band { height: 10px; background: var(--on); opacity: .25; border: 0; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { padding: 6px 12px; border-radius: 8px; font-weight: 600; color: var(--muted); min-height: 34px; }
.seg button.sel { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* tabs */
.tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
.tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: .7rem; color: var(--muted); font-weight: 600; }
.tabs button .ico { font-size: 1.25rem; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); }
.tabs button.active .ico { filter: none; opacity: 1; }

/* toast, empty, login */
.toast { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600; z-index: 60; box-shadow: var(--shadow); max-width: 90vw; }
.toast.bad { background: var(--bad); color: #fff; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }
.banner { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); color: var(--text); border-radius: 12px; padding: 10px 14px; margin: 8px 0; font-size: .9rem; }
.banner.bad { background: color-mix(in srgb, var(--bad) 14%, var(--surface)); }
.banner.agent { background: color-mix(in srgb, var(--cool) 13%, var(--surface)); }
.fails-h { font-weight: 650; margin-bottom: 4px; }
.fail { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 7px 0; border-top: 1px solid color-mix(in srgb, var(--bad) 22%, transparent); }
.fail-t { min-width: 0; overflow-wrap: anywhere; }
.fail .btn { flex: none; background: var(--surface); }
.fails-a { display: flex; justify-content: flex-end; border-top: 1px solid color-mix(in srgb, var(--bad) 22%, transparent); padding-top: 4px; }
body.login { display: grid; place-items: center; padding: 20px; }
.login-box { background: var(--surface); border-radius: 20px; padding: 28px 24px; width: min(380px, 100%); box-shadow: var(--shadow); }
.login-box h1 { font-size: 1.3rem; }
.login-box form { display: grid; gap: 10px; margin-top: 18px; }
.login-box label { font-size: .9rem; color: var(--muted); }
.pw-row { display: flex; gap: 6px; } .pw-row input { flex: 1; }
.login-box .primary { background: var(--accent); color: #fff; border-radius: 12px; min-height: 48px; font-weight: 700; }
.login-box .ghost { width: 48px; }
.error { color: var(--bad); margin: 0; font-size: .9rem; }

/* mappa */
.plan { position: relative; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; user-select: none; -webkit-user-select: none; }
.plan img { display: block; width: 100%; height: auto; }
@media (prefers-color-scheme: dark) { .plan img { filter: invert(.88) hue-rotate(180deg) brightness(1.05) contrast(.9); } }
.plan.edit { outline: 2px dashed var(--accent); cursor: crosshair; }
.markers { position: absolute; inset: 0; }
.marker { position: absolute; transform: translate(-50%, -50%); width: 40px; height: 40px; border-radius: 50%; background: var(--surface); border: 3px solid var(--off); box-shadow: 0 2px 8px rgba(0,0,0,.25); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: .78rem; color: var(--text); padding: 0; }
.marker.heat { border-color: var(--heat); background: var(--heat); color: #fff; }
.marker.cool { border-color: var(--cool); background: var(--cool); color: #fff; }
.marker.off { color: var(--muted); }
.marker.err { box-shadow: 0 0 0 4px color-mix(in srgb, var(--bad) 45%, transparent); }
.marker.sel { transform: translate(-50%, -50%) scale(1.25); box-shadow: 0 0 0 5px var(--accent-soft), 0 2px 10px rgba(0,0,0,.3); }
.marker .ml { position: absolute; top: 100%; left: 50%; transform: translateX(-50%); margin-top: 3px; font-size: .62rem; font-weight: 600; color: var(--text); background: color-mix(in srgb, var(--surface) 85%, transparent); padding: 1px 5px; border-radius: 6px; white-space: nowrap; pointer-events: none; }
@media (max-width: 520px) { .marker { width: 30px; height: 30px; font-size: .62rem; border-width: 2px; } .marker .ml { display: none; } .marker.sel .ml { display: block; } }
.legend .dotl { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 5px; vertical-align: middle; border: 2px solid var(--off); background: var(--surface); }
.legend .dotl.heat { background: var(--heat); border-color: var(--heat); } .legend .dotl.cool { background: var(--cool); border-color: var(--cool); }
