:root {
  color-scheme: light dark;
  --accent: #2563eb;
  --ok: #16a34a;
  --ko: #dc2626;
  --line: color-mix(in srgb, currentColor 16%, transparent);
  --soft: color-mix(in srgb, currentColor 7%, transparent);
}
* { box-sizing: border-box; }
body { font-family: system-ui, -apple-system, sans-serif; margin: 0; line-height: 1.4; }
.wrap { max-width: 860px; margin: 0 auto; padding: 0 1.2rem; }

/* --- icone --- */
.ic { width: 1.2em; height: 1.2em; flex: 0 0 auto; display: inline-block; vertical-align: -.18em; }

/* --- layout: header / footer --- */
header.app { border-bottom: 1px solid var(--line); position: sticky; top: 0; backdrop-filter: blur(8px); z-index: 5; }
header.app .bar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; height: 60px; }
.brand { display: flex; align-items: center; gap: .5rem; text-decoration: none; color: inherit; }
.brand-ic { width: 26px; height: 26px; color: #f97316; }
.brand-tx { display: flex; flex-direction: column; font-weight: 700; font-size: 1.05rem; line-height: 1.05; }
.brand-tx small { font-weight: 500; font-size: .64rem; opacity: .5; letter-spacing: .02em; }
header.app nav { display: flex; gap: .3rem; }
header.app nav a { display: flex; align-items: center; gap: .35rem; text-decoration: none; color: inherit; opacity: .6; font-size: .9rem; padding: .4rem .7rem; border-radius: 10px; }
header.app nav a .ic { width: 17px; height: 17px; }
header.app nav a.on { opacity: 1; background: var(--soft); font-weight: 600; }
@media (max-width: 560px) { header.app nav a span { display: none; } header.app nav a { padding: .45rem .6rem; } }
main.wrap { padding-top: 1.2rem; padding-bottom: 2rem; }
footer.app { border-top: 1px solid var(--line); opacity: .5; font-size: .8rem; }
footer.app .wrap { padding-top: .8rem; padding-bottom: .8rem; }

h2 { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; opacity: .55; margin: 1.4rem 0 .6rem; }

/* --- card generiche --- */
.card { border-radius: 18px; padding: 1.1rem; background: var(--soft); }
.card .val { font-size: 2rem; font-weight: 700; }
.card .val.sm { font-size: 1.3rem; }
.card .lbl { opacity: .65; font-size: .82rem; margin-top: .15rem; }

.row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.luce-lbl { display: flex; align-items: center; gap: .5rem; }
.luce-lbl .ic { width: 20px; height: 20px; color: #f59e0b; }
.ct-head { display: flex; align-items: center; gap: .6rem; }
.ct-head .ic { width: 22px; height: 22px; color: #0ea5e9; }
.clima-state { font-weight: 700; font-size: 1.05rem; text-align: right; white-space: nowrap; }
.clima-state.on { color: var(--ok); }
.clima-state.off { opacity: .5; }

button { font: inherit; cursor: pointer; }
.btn { padding: .55rem 1.1rem; border-radius: 11px; border: 1px solid currentColor; background: transparent; color: inherit; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.on { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.del { border: none; font-size: 1.1rem; opacity: .6; padding: .2rem; }
.rg-days { display: flex; gap: .3rem; flex-wrap: wrap; margin: .3rem 0; }
.btn.day { padding: .25rem .5rem; font-size: .78rem; min-width: 2.6rem; }
#routList input[type=time] { width: 7.5rem; flex: 0 0 auto; }
#routList .tnum { width: 5.5rem; flex: 0 0 auto; }

/* --- modale di conferma (sostituisce il confirm() nativo) --- */
.modal-ov {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 1.2rem;
  background: rgba(0, 0, 0, .45); backdrop-filter: blur(3px);
  opacity: 0; transition: opacity .15s ease;
}
.modal-ov.show { opacity: 1; }
.modal {
  width: 100%; max-width: 360px; background: Canvas; color: CanvasText;
  border: 1px solid var(--line); border-radius: 20px; padding: 1.4rem 1.3rem;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .35);
  transform: translateY(10px) scale(.97); transition: transform .15s ease;
}
.modal-ov.show .modal { transform: none; }
.modal-tt { font-weight: 700; font-size: 1.15rem; margin-bottom: .35rem; }
.modal-msg { font-size: 1rem; opacity: .8; }
.modal-btns { display: flex; gap: .8rem; margin-top: 1.5rem; }
.modal-btns .btn { flex: 1; min-height: 52px; padding: .8rem 1rem; font-size: 1.02rem; border-radius: 14px; }
.modal-btns .modal-yes.danger { background: var(--ko); border-color: var(--ko); }

/* --- bilancio energetico (TABELLA: colonne allineate per costruzione) --- */
/* 5 colonne: nodo | tubo | nodo | tubo | nodo. Icone (riga 1), valori (riga 2) ed
   etichette (riga 3) stanno nelle STESSE colonne della tabella → allineate sempre.
   I tubi sono in celle proprie, separate dalle icone: non le toccano mai. */
.flow-card { padding: 1.4rem 1.1rem; }
.flow { width: 100%; table-layout: fixed; border-collapse: collapse; }
.flow col.c-node { width: 22%; }
.flow col.c-pipe { width: 17%; }
.flow td { text-align: center; padding: 0; vertical-align: middle; }
.flow .r-ic td  { padding-bottom: .55rem; line-height: 0; }
.flow .r-val td { padding-bottom: .2rem; }
.fl-ic { width: 46px; height: 46px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--soft); margin: 0 auto; }
.fl-ic .ic { width: 24px; height: 24px; }
.fl-ic.fv   { background: color-mix(in srgb, #f59e0b 16%, transparent); color: #f59e0b; }
.fl-ic.home { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }
.fl-ic.rete  { opacity: .75; }
.flow .r-val td { font-size: 1.4rem; font-weight: 800; white-space: nowrap; }
.flow .r-val td.big { font-size: 1.7rem; }
.flow .r-lbl td { font-size: .66rem; opacity: .55; text-transform: uppercase; letter-spacing: .04em; }
#flow.is-feed .fl-ic.rete { background: color-mix(in srgb, var(--ok) 16%, transparent); color: var(--ok); opacity: 1; }
#flow.is-feed #fGrid { color: var(--ok); }
#flow.is-draw .fl-ic.rete { background: color-mix(in srgb, var(--ko) 16%, transparent); color: var(--ko); opacity: 1; }
#flow.is-draw #fGrid { color: var(--ko); }
/* connettori animati: la riga si illumina e scorre nella direzione del flusso */
/* connettori a pallini: scorrono nella direzione del flusso. Verde = var(--ok)
   (identico al verde delle scritte), rosso = var(--ko). A riposo pallini grigi. */
.pipe-h { display: block; width: 100%; height: 8px;
  background-image: radial-gradient(circle, var(--soft) 0 2.6px, transparent 3.1px);
  background-size: 13px 100%; background-position: 0 50%; background-repeat: repeat-x; }
.pipe-h.on { background-image: radial-gradient(circle, var(--flux) 0 2.6px, transparent 3.1px); animation: dots .8s linear infinite; }
.pipe-h.left.on { animation-direction: reverse; }
.pipe-h.green { --flux: var(--ok); }
.pipe-h.red   { --flux: var(--ko); }
@keyframes dots { to { background-position: 13px 50%; } }
@media (max-width: 480px) {
  .flow col.c-node { width: 25%; }
  .flow col.c-pipe { width: 12.5%; }
  .flow-card { padding: 1.2rem .7rem; }
  .fl-ic { width: 38px; height: 38px; }
  .fl-ic .ic { width: 20px; height: 20px; }
  .flow .r-val td { font-size: .95rem; }
  .flow .r-val td.big { font-size: 1.15rem; }
  .flow .r-lbl td { font-size: .55rem; }
}

/* --- comfort interno (in riga) --- */
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 1rem; gap: .8rem; }
.card.mini { display: flex; align-items: center; gap: .75rem; padding: .9rem 1rem; }
.card.mini .node-ic .ic { width: 24px; height: 24px; }
.card.mini.clima .node-ic { color: #0ea5e9; }
.card.mini.temp  .node-ic { color: #f97316; }
.card.mini.umid  .node-ic { color: #3b82f6; }
@media (max-width: 440px) {
  /* su schermi stretti: Clima a tutta riga, Temp e Umidità appaiate sotto */
  .g3 { grid-template-columns: 1fr 1fr; }
  .g3 .clima { grid-column: 1 / -1; }
}

/* --- meteo (riga intera, in fondo) --- */
.meteo-full {
  display: flex; align-items: center; gap: 1.3rem; flex-wrap: wrap;
  background: linear-gradient(135deg, #3b82f6, #1e3a8a); color: #fff; margin-top: 1.4rem;
}
.mf-now { display: flex; align-items: center; gap: .9rem; flex: 0 0 auto; }
.mf-ic { display: inline-flex; }
.mf-ic .ic { width: 50px; height: 50px; }
.mf-temp { font-size: 2.2rem; font-weight: 800; line-height: 1; }
.mf-desc { opacity: .95; margin-top: .15rem; }
.mf-place { opacity: .6; font-size: .78rem; margin-top: .12rem; }
.mf-days { display: flex; gap: .5rem; overflow-x: auto; flex: 1 1 300px; padding-bottom: .2rem; }
.fc-day { flex: 0 0 auto; min-width: 56px; text-align: center; padding: .55rem .35rem; border-radius: 14px; background: rgba(255,255,255,.16); }
.fc-day .d { font-size: .7rem; opacity: .9; }
.fc-ic { margin: .25rem 0; }
.fc-ic .ic { width: 24px; height: 24px; }
.fc-day .t .mx { font-weight: 700; }
.fc-day .t .mn { opacity: .65; }

/* --- form / config --- */
label { display: block; font-size: .8rem; opacity: .75; margin-bottom: .25rem; }
/* TUTTI i campi con lo stesso vestito: testo, password, numeri, orari e select */
input[type=number], input[type=time], input[type=text], input[type=password], select {
  font: inherit; padding: .4rem .5rem; border-radius: 10px; border: 1px solid var(--line);
  background: transparent; color: inherit; width: 100%;
}
select { appearance: none; -webkit-appearance: none; padding-right: 1.6rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23888' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right .55rem center; }
select option { background: var(--card, #fff); color: inherit; }
/* parametri: testo a sinistra, input piccolo a destra, una riga per voce */
.params { display: flex; flex-direction: column; gap: .55rem; }
.params > div { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.params label { margin: 0; font-size: .85rem; opacity: .9; flex: 1 1 auto; }
.params .play { flex: 0 0 auto; padding: .35rem .7rem; }
.params input[type=number] { width: 5.5rem; flex: 0 0 auto; text-align: right; }
.params input[type=text], .params input[type=password] { flex: 0 1 14rem; min-width: 0; text-align: right; }
.params select { flex: 0 1 14rem; width: auto; min-width: 0; }
table { width: 100%; border-collapse: collapse; }
th { font-size: .72rem; text-transform: uppercase; opacity: .6; text-align: left; padding: .3rem .4rem; }
td { padding: .35rem .4rem; vertical-align: middle; }
.fascia { padding: .85rem 0; border-bottom: 1px solid var(--line); }
.fascia:first-child { padding-top: .2rem; }
.fascia:last-child { border-bottom: none; padding-bottom: .2rem; }
.giorni { display: flex; gap: .25rem; }
.giorni button { flex: 1 1 0; min-width: 0; height: 2.1rem; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: inherit; font-size: .82rem; }
.giorni button.on { background: var(--ok); color: #fff; border-color: var(--ok); }
.fascia-orari { display: flex; align-items: center; gap: .7rem; margin-top: .7rem; flex-wrap: wrap; }
.fascia-orari label { display: flex; align-items: center; gap: .35rem; margin: 0; font-size: .85rem; opacity: .9; }
.fascia-orari input[type=time] { width: auto; }
.fascia-orari .tnum { width: 4rem; }
.fascia-orari .del { margin-left: auto; }
.bar-actions { display: flex; gap: .6rem; align-items: center; margin-top: 1rem; }
.net { margin-top: 1.2rem; font-size: .85rem; }
.ok { color: var(--ok); } .ko { color: var(--ko); }

/* --- pannello fotovoltaico --- */
.fv-now { display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap; }
.fv-pac { font-size: 2.4rem; font-weight: 800; line-height: 1; color: #f59e0b; }
.fv-now-grid { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 1rem; flex: 1 1 240px; }
.fv-now-grid > div { text-align: center; }
.fv-kpi { display: flex; flex-direction: column; gap: .15rem; margin-top: .6rem; font-size: .82rem; opacity: .75; }
.fv-kpi b { font-weight: 700; opacity: 1; }
.ac-bar { display: flex; height: 16px; border-radius: 8px; overflow: hidden; background: var(--soft); margin: .2rem 0 .7rem; }
.ac-bar .self { background: var(--ok); }
.ac-bar .exp  { background: color-mix(in srgb, var(--accent) 70%, transparent); }
.ac-legend { display: flex; gap: 1.2rem; font-size: .78rem; opacity: .8; flex-wrap: wrap; }
.ac-legend span::before { content: ''; display: inline-block; width: .7rem; height: .7rem; border-radius: 3px; margin-right: .35rem; vertical-align: -1px; }
.ac-legend .l-self::before { background: var(--ok); }
.ac-legend .l-exp::before  { background: color-mix(in srgb, var(--accent) 70%, transparent); }
.bars { display: flex; align-items: flex-end; gap: .3rem; height: 168px; margin-top: .6rem; overflow-x: auto; }
/* min-width fissa + niente shrink: su schermi stretti le barre non collassano sotto
   l'etichetta (che altrimenti farebbe sforare la fila), si scorre invece in orizzontale. */
.bars .bar { flex: 1 0 12px; min-width: 12px; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; }
.bars .bar .seg { width: 100%; }
.bars .bar .seg.exp  { background: color-mix(in srgb, var(--accent) 70%, transparent); border-radius: 3px 3px 0 0; }
.bars .bar .seg.self { background: var(--ok); }
.bars .bar .cap { text-align: center; font-size: .58rem; opacity: .5; margin-top: .25rem; white-space: nowrap; }
/* grafico tensione inverter (linea) */
.volt-wrap { display: flex; gap: .5rem; }
.volt-axis { display: flex; flex-direction: column; justify-content: space-between; font-size: .62rem; opacity: .5; padding: 3px 0; min-width: 2.5rem; text-align: right; }
.volt-plot { flex: 1 1 auto; height: 150px; }
.volt-svg { width: 100%; height: 150px; display: block; }
.volt-svg polyline { fill: none; stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
/* selettore vista energia (giorni/settimane/mesi) nelle card Consumi */
.seg-btns { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg-btns button { background: none; border: 0; color: inherit; font: inherit; font-size: .72rem; padding: .25rem .6rem; cursor: pointer; opacity: .65; }
.seg-btns button.on { background: color-mix(in srgb, var(--accent) 18%, transparent); opacity: 1; font-weight: 600; }
.seg-btns button + button { border-left: 1px solid var(--line); }
/* righe del registro dispositivi (Impostazioni) */
.dev-row { display: flex; gap: .4rem; align-items: center; flex-wrap: wrap; padding: .45rem 0; border-bottom: 1px solid var(--line); }
.dev-row:last-of-type { border-bottom: 0; }
.dev-row input[type=text].dr-nome { width: 8.5rem; }
.dev-row input[type=text].dr-host { width: 8rem; }
.dev-row select { width: auto; }
.dev-row .dr-ch input { width: 3rem; }
.dev-row .dr-ch, .dev-row .dr-en { display: inline-flex; align-items: center; gap: .25rem; font-size: .78rem; opacity: .85; }
/* l'attributo hidden vince sempre, anche su elementi con display esplicito */
[hidden] { display: none !important; }
/* tab della pagina Impostazioni (scorrono in orizzontale sugli schermi stretti) */
.tabs { display: flex; gap: .3rem; margin: .2rem 0 1.1rem; overflow-x: auto; padding-bottom: .15rem; }
.tabs button { background: none; border: 1px solid var(--line); border-radius: 999px; color: inherit;
  font: inherit; font-size: .82rem; padding: .35rem .85rem; cursor: pointer; opacity: .7;
  white-space: nowrap; flex: 0 0 auto; }
.tabs button.on { background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); opacity: 1; font-weight: 600; }

/* --- mini-grafici (charts.js): tooltip, tacche e crosshair --- */
#ctip { position: fixed; z-index: 50; background: color-mix(in srgb, CanvasText 88%, transparent);
  color: Canvas; padding: .4rem .6rem; border-radius: 9px; font-size: .74rem; line-height: 1.4;
  white-space: pre-line; pointer-events: none; max-width: 250px; }
.gwrap { position: relative; }
.glayer { position: absolute; inset: 0; pointer-events: none; }
.gline { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--line); }
.gline span { position: absolute; right: 0; top: -1.15em; font-size: .6rem; opacity: .8; z-index: 2;
  background: color-mix(in srgb, Canvas 72%, transparent); border-radius: 4px; padding: 0 .25rem; }
.bars { position: relative; overscroll-behavior-x: contain; }   /* scroll-x: il default lascia scorrere */
.bars .bar { position: relative; z-index: 1; }      /* barre davanti alle gridline */
/* su mobile barre più larghe: ~14 giorni leggibili a colpo d'occhio, gli altri si
   scorrono in orizzontale (il grafico parte già dai giorni recenti) */
@media (max-width: 560px) { .bars .bar { min-width: 20px; } }
.volt-plot { position: relative; touch-action: pan-y; }
.volt-svg { overflow: visible; }                    /* lo stroke ai bordi non si tronca */
.volt-svg line.g { stroke: var(--line); stroke-dasharray: 4 4; vector-effect: non-scaling-stroke; }
.xlab { position: absolute; bottom: 2px; transform: translateX(-50%); font-size: .6rem;
  opacity: .5; pointer-events: none; }
.ch-line { position: absolute; top: 0; bottom: 0; pointer-events: none;
  border-left: 1px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.ch-dot { position: absolute; width: 9px; height: 9px; border-radius: 50%; background: var(--accent);
  transform: translate(-50%, -50%); pointer-events: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }

/* --- telecomando clima per-unità (dashboard) --- */
#climaUnits { margin-top: .2rem; }
.clima-unit { border-top: 1px solid var(--line); padding-top: .75rem; margin-top: .75rem; }
.clima-unit:first-child { border-top: 0; padding-top: 0; margin-top: .35rem; }
.cu-ctl { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; margin-top: .7rem; }
.cu-pwr { min-width: 5.6rem; }
.cu-temp { display: inline-flex; align-items: center; gap: .3rem; }
.cu-step { min-width: 2.5rem; font-size: 1.1rem; line-height: 1; padding: .4rem .6rem; }
.cu-tval { min-width: 2.6rem; text-align: center; font-weight: 700; font-size: 1.05rem; }
.cu-sel { width: auto; min-width: 0; flex: 0 1 auto; }
.cu-ctrl { margin-left: auto; }     /* Auto/Manuale/Off allineati a destra */
/* Home: ogni clima è un accordion, comandi nascosti finché non si espande */
details.clima-unit > summary { cursor: pointer; list-style: none; gap: .6rem; }
details.clima-unit > summary::-webkit-details-marker { display: none; }
details.clima-unit > summary .ct-head { flex: 1; }
details.clima-unit > summary::before { content: '\25B8'; color: #888; flex: 0 0 auto; transition: transform .15s; }
details.clima-unit[open] > summary::before { transform: rotate(90deg); }
details.clima-unit:not([open]) > .cu-ctl { display: none; }   /* regola autore: batte il display:flex quando chiuso */
.cu-empty { margin: .8rem 0 .2rem; }
.cu-empty a { color: var(--accent); }
@media (max-width: 560px) { .cu-ctrl { margin-left: 0; } }

/* --- Clima Fase 2: anteprima settimanale --- */
.wk { margin:.4rem 0 .6rem; }
.wk-row { display:grid; grid-template-columns:1.5rem repeat(24,1fr); align-items:center; gap:1px; height:.8rem; }
.wk-row + .wk-row { margin-top:1px; }
.wk-day { font-size:.6rem; color:#888; text-align:right; padding-right:.25rem; }
.wk-row > span { height:100%; border-radius:1px; }
.wk-head { height:auto; margin-bottom:1px; }
.wk-head i { font-style:normal; font-size:.58rem; color:#999; text-align:left; }
.fascia.reg-comfort { border-left:3px solid #378add; padding-left:.5rem; }
.fascia.reg-eco { border-left:3px solid #1d9e75; padding-left:.5rem; }
.fascia.reg-off { border-left:3px solid #bbb; padding-left:.5rem; }
