/* Casa IoT — pensato prima per iPhone (webapp a schermo intero), poi esteso al PC */

:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e5e9;
  --accent: #e8590c;
  --accent-text: #ffffff;
  --hot: #e8590c;
  --hot-soft: #fff1e8;
  --pause: #2f6fdb;
  --pause-soft: #e9f0fc;
  --ok: #2b8a3e;
  --ok-soft: #e9f7ec;
  --danger: #c92a2a;
  --danger-soft: #fdeceb;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .05);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --surface: #1b1e24;
    --surface-2: #23272e;
    --text: #e9eaec;
    --muted: #9aa0aa;
    --border: #2d313a;
    --accent: #ff7a33;
    --accent-text: #1b1e24;
    --hot: #ff7a33;
    --hot-soft: #3a2418;
    --pause: #6ea0ff;
    --pause-soft: #1b2740;
    --ok: #69db7c;
    --ok-soft: #1c3322;
    --danger: #ff6b6b;
    --danger-soft: #3a1d1d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; }
[hidden] { display: none !important; }

/* ---------- controlli (target minimo 44 px, testo 16 px per evitare lo zoom di Safari) ---------- */
button, select, input {
  font: inherit;
  font-size: 16px;
  color: var(--text);
}

button {
  min-height: 44px;
  cursor: pointer;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: .6rem 1rem;
  touch-action: manipulation;
  transition: background .15s, border-color .15s, opacity .15s, transform .1s;
}
button:active:not(:disabled) { transform: scale(.98); }
button:disabled { opacity: .55; cursor: default; }
button.small { min-height: 36px; padding: .35rem .8rem; font-size: .9rem; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }

select, input[type="number"], input[type="password"] {
  width: 100%;
  min-height: 44px;
  padding: .55rem .7rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 2rem;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 12px) 50%;
  background-size: 6px 6px;
  background-repeat: no-repeat;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.label { display: block; font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

/* ---------- barra superiore (sotto la tacca dell'iPhone) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: .5rem;
  padding: calc(.6rem + var(--safe-top)) calc(16px + var(--safe-right)) .6rem calc(16px + var(--safe-left));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.1rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back {
  display: grid; place-items: center;
  width: 40px; height: 40px; margin-left: -8px; border-radius: 10px; text-decoration: none; color: var(--accent);
}
.topbar form { margin: 0; }

main {
  max-width: 1000px; margin: 0 auto;
  padding: 1rem calc(16px + var(--safe-right)) calc(2rem + var(--safe-bottom)) calc(16px + var(--safe-left));
}

.subtitle { color: var(--muted); margin: 0; font-size: .9rem; }

.msg-error, .msg-ok { border-radius: 12px; padding: .65rem .85rem; font-size: .9rem; margin-bottom: 1rem; }
.msg-error { background: var(--danger-soft); color: var(--danger); }
.msg-error ul { margin: 0; padding-left: 1.1rem; }
.msg-ok { background: var(--ok-soft); color: var(--ok); }
.validation-summary-valid { display: none; }

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh; display: grid; place-items: center;
  padding: calc(16px + var(--safe-top)) 16px calc(16px + var(--safe-bottom));
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 2rem 1.5rem;
}
.login-card h1 { margin: 0 0 .25rem; font-size: 1.5rem; }
.login-card p { margin: 0 0 1.5rem; color: var(--muted); }
.login-card label { display: block; font-size: .9rem; margin-bottom: .35rem; }
.login-card input { margin-bottom: 1rem; }
.login-card button { width: 100%; }

/* ---------- dashboard ---------- */
.tiles { display: grid; grid-template-columns: 1fr; gap: .75rem; }
.tile {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 1rem; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem 1.1rem; text-decoration: none;
}
.tile:active { transform: scale(.99); }
.tile .icon {
  grid-row: span 2;
  width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center;
  background: var(--hot-soft); color: var(--hot);
}
.tile .icon.neutro { background: var(--surface-2); color: var(--muted); }
.tile h2 { margin: 0; font-size: 1.05rem; }
.tile p { margin: 0; color: var(--muted); font-size: .88rem; }

/* ---------- caloriferi ---------- */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .75rem; }

.cards { display: grid; grid-template-columns: 1fr; gap: .9rem; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1rem 1.1rem 1.1rem;
  display: flex; flex-direction: column; gap: .8rem;
  transition: border-color .2s;
}
.card.attivo { border-color: var(--pause); }
.card.caldo { border-color: var(--hot); }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.card-head h2 { margin: 0; font-size: 1.15rem; }

.badge-fiamma {
  display: inline-flex; align-items: center; gap: .3rem;
  background: var(--hot-soft); color: var(--hot);
  border-radius: 999px; padding: .2rem .6rem; font-size: .8rem; font-weight: 600;
}

.temps { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.temps > div { background: var(--surface-2); border-radius: 12px; padding: .55rem .75rem; }
.temps .big { display: block; font-size: 1.9rem; font-weight: 600; line-height: 1.15; font-variant-numeric: tabular-nums; }

.fase { margin: 0; font-size: .95rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.attivo .fase { color: var(--pause); font-weight: 600; }
.caldo .fase { color: var(--hot); }

.btn-ciclo { width: 100%; min-height: 52px; font-size: 1.05rem; font-weight: 600; background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.attivo .btn-ciclo { background: var(--surface-2); border-color: var(--border); color: var(--text); }

.scelte { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem; }
.scelte label { display: flex; flex-direction: column; gap: .3rem; }

.card .errore, .setup .errore { margin: 0; color: var(--danger); font-size: .85rem; }

/* ---------- impostazioni ---------- */
.impostazioni { display: grid; grid-template-columns: 1fr; gap: .9rem; padding-bottom: 4.5rem; /* spazio per il pulsante Salva fisso */ }
.impostazioni fieldset { margin: 0; min-width: 0; }
.impostazioni legend { float: left; width: 100%; padding: 0; }
.impostazioni legend h2 { margin: 0; font-size: 1.15rem; }
.impostazioni .gruppo { display: flex; flex-direction: column; gap: .35rem; clear: both; }
.impostazioni .tre { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; }
.impostazioni .tre label { position: relative; }
.impostazioni .tre span {
  position: absolute; left: .6rem; top: 50%; transform: translateY(-50%);
  font-size: .75rem; color: var(--muted); pointer-events: none;
}
.impostazioni .tre input { padding-left: 1.6rem; text-align: center; }
.impostazioni .riga { display: flex; flex-direction: column; gap: .35rem; }
.impostazioni .riga input { max-width: 8rem; }
.btn-salva { width: 100%; min-height: 52px; font-size: 1.05rem; position: sticky; bottom: calc(12px + var(--safe-bottom)); }

/* ---------- sicurezza ---------- */
.sezioni { display: grid; grid-template-columns: 1fr; gap: .9rem; }
.sezioni h2 { margin: 0; font-size: 1.15rem; }
.stato-sic { margin: 0; color: var(--muted); font-size: .92rem; }
.stato-sic.attivo { color: var(--ok); font-weight: 600; }
.modulo { display: flex; flex-direction: column; gap: .7rem; }
.modulo label { display: flex; flex-direction: column; gap: .3rem; }
.modulo input[type="text"], .modulo input:not([type]), input.codice {
  width: 100%; min-height: 44px; padding: .55rem .7rem; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface-2); font-size: 16px; color: var(--text);
}
input.codice { font-variant-numeric: tabular-nums; letter-spacing: .15em; font-size: 1.2rem; }
.passi { margin: 0; padding-left: 1.2rem; font-size: .92rem; display: grid; gap: .35rem; }
.qr { background: #fff; border-radius: 12px; padding: .5rem; width: min(240px, 100%); align-self: center; }
.qr svg { display: block; width: 100%; height: auto; }
.chiave { margin: 0; }
.chiave code { font-size: 1rem; word-break: break-all; user-select: all; -webkit-user-select: all; }
details summary { cursor: pointer; color: var(--muted); font-size: .92rem; min-height: 44px; display: flex; align-items: center; }
details .modulo { margin-top: .5rem; }
.accessi ul { list-style: none; margin: 0; padding: 0; font-size: .85rem; }
.accessi li {
  display: grid; grid-template-columns: auto 1fr; gap: 0 .6rem; padding: .45rem 0; border-top: 1px solid var(--border);
}
.accessi li:first-child { border-top: 0; }
.accessi .quando { color: var(--muted); font-variant-numeric: tabular-nums; }
.accessi .ip { grid-column: 2; color: var(--muted); font-size: .8rem; word-break: break-all; }
.accessi .ok .cosa { color: var(--ok); }
.accessi .ko .cosa { color: var(--danger); }
.login-card input.codice { margin-bottom: 1rem; }

/* ---------- configurazione iniziale ---------- */
.setup {
  margin-top: 1.5rem; background: var(--surface); border: 1px dashed var(--border);
  border-radius: var(--radius); padding: 1.1rem;
}
.setup h2 { margin: 0 0 .25rem; font-size: 1rem; }
.setup p { margin: 0 0 1rem; color: var(--muted); font-size: .9rem; }
.setup table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.setup th, .setup td { text-align: left; padding: .45rem .4rem; border-top: 1px solid var(--border); vertical-align: top; }
.setup td code { word-break: break-all; }
.table-wrap { overflow-x: auto; }

/* ---------- schermi larghi (tablet, PC) ---------- */
@media (min-width: 720px) {
  main { padding-top: 1.5rem; }
  .tiles { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
  .cards, .impostazioni { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
  .impostazioni { padding-bottom: 0; }
  .sezioni { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); align-items: start; }
  .btn-salva { grid-column: 1 / -1; justify-self: start; width: auto; min-width: 200px; position: static; }
}
