/* ---------- temi: scuro (predefinito), chiaro, automatico (segue il telefono) ---------- */
:root { color-scheme: dark; --bg:#0f172a; --card:#1e293b; --line:#334155; --text:#f1f5f9; --muted:#94a3b8; --ok:#22c55e; --bad:#ef4444;
  --accent:#2563eb; --input:#0b1220; --okbg:#14532d; --badbg:#7f1d1d; --waitbg:#78350f; --shadow: rgba(0,0,0,.4); }
:root[data-theme="light"] { color-scheme: light; --bg:#f1f5f9; --card:#ffffff; --line:#cbd5e1; --text:#0f172a; --muted:#475569;
  --input:#ffffff; --okbg:#dcfce7; --badbg:#fee2e2; --waitbg:#fef3c7; --shadow: rgba(15,23,42,.15); }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) { color-scheme: light; --bg:#f1f5f9; --card:#ffffff; --line:#cbd5e1; --text:#0f172a; --muted:#475569;
    --input:#ffffff; --okbg:#dcfce7; --badbg:#fee2e2; --waitbg:#fef3c7; --shadow: rgba(15,23,42,.15); }
}

* { box-sizing: border-box; }
body { margin:0; background:var(--bg); color:var(--text); font:16px/1.4 system-ui, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
main { max-width: 560px; margin: 0 auto; padding: 0 16px 16px; }
/* niente effetto elastico di Android ai bordi (allungava anche la barra in alto) */
html, body { overscroll-behavior: none; }
h1 { font-size: 1.5rem; margin: 0; } h2 { font-size: 1.05rem; margin: 20px 0 8px; color: var(--muted); font-weight: 600; }
header { display:flex; align-items:center; justify-content:space-between; margin-bottom: 12px; }
.pill { padding: 4px 10px; border-radius: 99px; background: var(--card); font-size: .85rem; }
.pill.ok { background: var(--okbg); } .pill.off { background: var(--badbg); }
.muted { color: var(--muted); font-size: .9rem; } .error { color: var(--bad); min-height: 1.2em; }
button, .btn { font: inherit; border-radius: 12px; border: 1px solid var(--line); padding: 12px 16px; background: var(--card); color: var(--text); cursor: pointer; display:inline-block; text-decoration:none; }
button.primary { background: var(--accent); border-color: var(--accent); color:#fff; }
button.ghost { background: transparent; } button:disabled { opacity:.5; }
button.danger { color: var(--bad); border-color: var(--bad); }
.list { list-style:none; margin:0 0 12px; padding:0; display:flex; flex-direction:column; gap:8px; }
.list li { display:flex; align-items:center; gap:10px; background: var(--card); border-radius: 12px; padding: 10px 12px; flex-wrap: wrap; }
.list .grow { flex:1; min-width:0; } .list .dot { width:12px; height:12px; border-radius:50%; background: var(--muted); flex:none; }
.list .dot.on { background: var(--bad); } .list .dot.free { background: var(--ok); }
.list button { padding: 8px 12px; }
.list .detail { flex-basis: 100%; margin: 0; padding-left: 22px; color: var(--muted); font-size: .9rem; }
.list button.info { width: 40px; padding: 8px 0; font-weight: 700; font-style: italic; }
form { display:flex; flex-direction:column; gap: 12px; margin-top: 12px; }
label { display:flex; flex-direction:column; gap: 4px; font-size: .9rem; color: var(--muted); }
input, select { font: inherit; padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--input); color: var(--text); }
input[type=color] { padding: 2px; height: 44px; width: 100%; }
input[type=range] { padding: 0; accent-color: var(--accent); }
fieldset { border:1px solid var(--line); border-radius: 12px; padding: 10px; } legend { color: var(--muted); font-size:.9rem; }
fieldset label { flex-direction:row; align-items:center; gap:10px; color: var(--text); padding: 6px 0; }
fieldset input { width: 22px; height: 22px; }
.row { display:flex; gap:10px; }
#toast { position: sticky; top: 64px; margin: 8px 0; padding: 10px 12px; border-radius: 10px; background: var(--okbg); z-index: 2; }
#toast.bad { background: var(--badbg); }
#toast.wait { background: var(--waitbg); }
[hidden] { display: none !important; }
dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--card); color: var(--text); width: min(88vw, 360px); padding: 22px; text-align: center; }
dialog::backdrop { background: rgba(0,0,0,.65); }
dialog p { margin: 12px 0 16px; font-size: 1.05rem; }
.keyreq-dlg .kr-item { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 0 0 12px; }
.keyreq-dlg .kr-name { font-weight: 600; }
.keyreq-dlg .kr-code { font-size: 2rem; font-weight: 700; letter-spacing: 6px; margin: 6px 0 10px; }
.keyreq-dlg .kr-acts { display: flex; gap: 8px; } .keyreq-dlg .kr-acts button { flex: 1; }
.keyreq-dlg #keyreq-dlg-close { width: 100%; }
dialog.bad { border-color: var(--bad); } dialog.good { border-color: var(--ok); }
#icon-dlg, #color-dlg { width: min(92vw, 420px); max-height: 80vh; overflow-y: auto; }
#icon-dlg h2, #color-dlg h2 { margin-top: 0; color: var(--text); }
#icon-dlg .ghost, #color-dlg .ghost { margin-top: 12px; width: 100%; }

/* ---------- pulsanti piccoli con icona (dettagli, modifica, elimina) ---------- */
.list button.ui-btn, .ui-btn { width: 40px; height: 40px; padding: 0; display: grid; place-items: center; border-radius: 10px; flex: none; }
.ui-btn svg { width: 22px; height: 22px; fill: currentColor; }
.ui-btn.danger { color: var(--bad); border-color: var(--line); }

/* ---------- tavolozza colori ---------- */
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 8px; }
.palette .sw { width: 100%; aspect-ratio: 1; padding: 0; border-radius: 50%; border: 2px solid var(--line); }
.palette .sw.sel { outline: 3px solid var(--accent); outline-offset: 2px; }
.palette .sw-custom { grid-column: 1 / -1; display: flex; align-items: center; justify-content: center; gap: 8px; }
.palette .sw-custom svg { width: 22px; height: 22px; fill: currentColor; }
.palette .sw-custom.sel { outline: 3px solid var(--accent); outline-offset: 2px; color: #fff; text-shadow: 0 0 3px #000; }
/* selettore personalizzato: tinta, intensità, luminosità */
.cp { grid-column: 1 / -1; display: flex; flex-direction: column; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; text-align: left; }
.cp-top { display: flex; align-items: center; gap: 12px; }
.cp-preview { width: 52px; height: 52px; border-radius: 50%; border: 2px solid var(--line); flex: none; }
.cp-hex { flex: 1; min-width: 0; font-family: monospace; }
.cp-row { gap: 6px; }
input.cp-range { -webkit-appearance: none; appearance: none; width: 100%; height: 26px; border-radius: 13px; border: 1px solid var(--line); padding: 0; }
input.cp-range::-webkit-slider-thumb { -webkit-appearance: none; width: 26px; height: 26px; border-radius: 50%; background: #fff; border: 3px solid #0f172a; box-shadow: 0 0 0 1px #fff; }
.swatch-btn { width: 44px; height: 44px; padding: 0; border-radius: 50%; border: 2px solid var(--line); flex: none; }
.color-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: .9rem; color: var(--muted); }
.spin { width: 40px; height: 40px; margin: 4px auto; border: 4px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: giro 1s linear infinite; }
@keyframes giro { to { transform: rotate(360deg); } }
label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); padding: 6px 0; }
label.check input { width: 22px; height: 22px; flex: none; }

/* ---------- riquadri delle pagine Impostazioni / Widget / Sicurezza ---------- */
.card { background: var(--card); border-radius: 16px; padding: 4px 14px 14px; margin: 12px 0; display: flex; flex-direction: column; gap: 10px; }
.card h2 { color: var(--text); margin: 12px 0 0; font-size: 1.1rem; }
.card .list li { background: var(--bg); }
.card form { margin-top: 0; }
.seg { display: flex; gap: 6px; }
.seg button { flex: 1; padding: 10px 4px; }
.seg button.sel { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- intestazione e menu ---------- */
header { gap: 10px; position: sticky; top: 0; z-index: 3; background: var(--bg); padding: 16px 0 8px; }
header h1 { flex: 1; font-size: 1.3rem; }
.icon-btn { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 12px; }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
#backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 4; }
#drawer { position: fixed; top: 0; bottom: 0; left: 0; width: min(78vw, 300px); z-index: 5; background: var(--card);
  padding: calc(env(safe-area-inset-top) + 16px) 12px 16px; display: flex; flex-direction: column; gap: 4px;
  transform: translateX(-105%); transition: transform .2s ease; box-shadow: 4px 0 24px var(--shadow); }
body.menu-open #drawer { transform: none; }
.drawer-title { margin: 0 8px 12px; font-size: 1.3rem; font-weight: 700; }
#drawer a { color: var(--text); text-decoration: none; padding: 14px 12px; border-radius: 12px; font-size: 1.05rem; }
#drawer a.active { background: var(--accent); color: #fff; }

/* ---------- scenari a pillola (come il widget) ---------- */
.pills { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 8px; }
.pill-btn { display: flex; align-items: center; gap: 10px; min-height: 64px; padding: 8px 14px 8px 8px; border: 0;
  border-radius: 999px; color: #fff; font-weight: 700; font-size: 1.05rem; text-align: left; }
.pill-btn.light { color: #000; }
.pill-btn .ic { flex: none; width: 48px; height: 48px; border-radius: 50%; background: rgba(255,255,255,.35);
  display: grid; place-items: center; }
.pill-btn .ic svg { width: 28px; height: 28px; fill: currentColor; }
.pill-btn .lbl { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pill-btn.active { box-shadow: 0 0 0 2px rgba(255,255,255,.55), 0 0 18px 2px var(--glow); }
.sc-ic { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; flex: none; border: 0; padding: 0; }
.sc-ic svg { width: 20px; height: 20px; fill: currentColor; }
button.sc-ic.big { width: 44px; height: 44px; } button.sc-ic.big svg { width: 26px; height: 26px; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; }
.icon-opt { height: 48px; padding: 0; display: grid; place-items: center; border-radius: 12px; }
.icon-opt svg { width: 26px; height: 26px; fill: currentColor; }
.icon-opt.sel { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-opt .auto { font-size: .75rem; font-weight: 700; }
input.swatch { width: 44px; height: 44px; padding: 2px; flex: none; }

/* ---------- anteprima del widget ---------- */
/* anteprima fissa in alto (sotto il titolo) mentre si scorrono le regolazioni */
.w-preview { border-radius: 24px; padding: 6px 10px 12px; margin-top: 4px;
  background: linear-gradient(135deg, #64748b, #334155); position: sticky; top: 64px; z-index: 2; overflow: hidden;
  box-shadow: 0 6px 16px var(--shadow); }
.w-preview .wcard { position: absolute; inset: 0; }
.w-preview .wbody { position: relative; }
.w-preview .whead { display: flex; align-items: center; gap: 8px; height: 40px; padding-left: 8px; font-weight: 700; }
.w-preview .whead .grow { flex: 1; }
.w-preview .wrows { display: grid; grid-template-columns: 1fr 1fr; }
.w-preview .wbtn { margin: 5px; height: 48px; border-radius: 999px; display: flex; align-items: center; gap: 10px; padding: 0 10px 0 5px; font-weight: 700; }
.w-preview .wbtn.active { box-shadow: 0 0 0 2px rgba(255,255,255,.7), 0 0 10px 3px var(--glow); }
.w-preview .wbtn .ic { width: 38px; height: 38px; border-radius: 50%; background: rgba(255,255,255,.35); display: grid; place-items: center; flex: none; }
.w-preview .wbtn .ic svg { width: 22px; height: 22px; fill: currentColor; }
.w-preview .wbtn span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.w-preview .wareas { display: grid; grid-template-columns: 1fr 1fr; gap: 5px 0; padding-top: 6px; }
.w-preview .wareas div { padding-left: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-preview .wareas b { font-size: 11px; margin-right: 8px; }

/* ---------- Log (archivio eventi) ---------- */
/* filtro date a fisarmonica */
.log-filters details { border-top: 1px solid var(--line); padding-top: 10px; }
.log-filters summary { cursor: pointer; font-size: .95rem; color: var(--text); padding: 4px 0; }
.date-row { display: flex; gap: 8px; align-items: flex-end; margin-top: 10px; }
.date-row label { flex: 1; min-width: 0; }
.date-btn { width: 100%; text-align: left; padding: 12px; background: var(--input); }
.date-row #log-reset { flex: none; }
/* barra fissa in basso della pagina Log */
#page-log, #page-home, #page-scenari, #page-programmi { padding-bottom: 84px; }
.bottom-bar[hidden] { display: none; }
.bottom-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 3; display: flex; gap: 10px;
  max-width: 560px; margin: 0 auto; padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--bg); box-shadow: 0 -6px 16px var(--shadow); }
.bottom-bar button { flex: 1; }
/* calendario */
#cal-dlg { width: min(92vw, 360px); }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-head h2 { margin: 0; color: var(--text); font-size: 1.1rem; }
.cal-head button { width: 44px; height: 44px; padding: 0; font-size: 1.4rem; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 12px; }
.cal-wd { font-size: .8rem; color: var(--muted); padding: 4px 0; }
.cal-day { padding: 0; height: 40px; border-radius: 10px; border-color: transparent; background: transparent; color: var(--muted); }
.cal-day.has { color: var(--text); font-weight: 700; background: var(--bg); }
.cal-day.sel { background: var(--accent); color: #fff; }
#cal-dlg .row button { flex: 1; }
h2.log-day { margin: 18px 0 6px; color: var(--text); }
.list.log { gap: 4px; }
.list.log li { padding: 8px 12px; flex-wrap: nowrap; align-items: flex-start; font-size: .95rem; }
.list.log .dot { margin-top: 5px; }
.log-time { color: var(--muted); font-variant-numeric: tabular-nums; flex: none; }
.dot.k-alarm { background: #ef4444 !important; } .dot.k-arm { background: #3b82f6 !important; } .dot.k-disarm { background: #22c55e !important; }
.dot.k-fault { background: #f97316 !important; } .dot.k-access, .dot.k-other { background: #94a3b8 !important; }

/* ---------- cosa mostrare a chi ---------- */
.events-only { display: none; }
body.can-events .events-only { display: block; }
body.can-events label.check.events-only { display: flex; }
.app-only, .no-pin-only, .no-edit-only { display: none; }
body.in-app .app-only { display: block; }
body.in-app .card.app-only { display: flex; }
body.in-app .web-only { display: none !important; }
body.no-pin .no-pin-only { display: block; }
body.no-edit .no-edit-only { display: block; }
body.no-pin .owner-only { display: none !important; }
body.no-edit .edit-only { display: none !important; }
body.no-local .local-only { display: none !important; }

/* ---------- programmi orario ---------- */
.t-name { display:block; font-weight:600; }
.t-state { display:block; font-size:.9rem; } .t-state.on { color: var(--ok); } .t-state.off { color: var(--muted); }
.list button.switch { position:relative; width:56px; height:32px; padding:0; border-radius:99px; border:none; background: var(--line); flex:none; transition: background .2s; }
.list button.switch .knob { position:absolute; top:3px; left:3px; width:26px; height:26px; border-radius:50%; background:#fff; box-shadow: 0 1px 3px var(--shadow); transition: left .2s; }
.list button.switch.on { background: var(--ok); } .list button.switch.on .knob { left:27px; }

/* ---------- Log: barra di avanzamento della lettura e "Mostra altri" ---------- */
.progress { position: sticky; top: 8px; z-index: 2; background: var(--card); border-radius: 12px; padding: 10px 12px; margin: 8px 0; box-shadow: 0 2px 8px var(--shadow); }
.progress-track { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
#log-progress-fill { height: 100%; width: 3%; background: var(--accent); border-radius: 99px; transition: width .5s ease; }
.progress p { margin: 6px 0 0; }
button.log-more { width: 100%; margin: 12px 0 4px; }

/* ---------- Home: aree in colonne ---------- */
.list li.area-row { display: grid; grid-template-columns: 12px minmax(0, 1fr) 78px 48px; align-items: center; gap: 10px; }
.area-name { overflow-wrap: anywhere; }
.area-state { font-size: .9rem; font-weight: 600; } .area-state.on { color: var(--bad); } .area-state.free { color: var(--ok); }
.list button.icon-act { width: 48px; height: 44px; padding: 0; display: inline-flex; align-items: center; justify-content: center; }
.list button.icon-act svg { width: 24px; height: 24px; fill: currentColor; }
.list button.icon-act.to-arm { color: var(--bad); } .list button.icon-act.to-free { color: var(--ok); }

/* ---------- Home: anomalie della centrale ---------- */
.anomalies { background: var(--waitbg); border: 1px solid #f59e0b; border-radius: 12px; padding: 10px 14px; margin-bottom: 12px; }
.anomalies .an-title { margin: 0; font-weight: 700; }
.anomalies ul { margin: 6px 0 0; padding-left: 22px; }
.anomalies li { margin: 2px 0; }

/* la schermata di accesso tiene il suo margine in alto (quello di main ora sta dentro la barra) */
#login { padding-top: 16px; }

/* ---------- caricamento senza percentuale (Programmi): barra che scorre ---------- */
.list li.loading-row { flex-direction: column; align-items: stretch; gap: 8px; }
.progress-track.indeterminate { position: relative; }
.progress-track.indeterminate .bar { position: absolute; top: 0; bottom: 0; width: 35%; background: var(--accent); border-radius: 99px;
  animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { 0% { left: -35%; } 100% { left: 100%; } }

/* ---------- chiavi: richieste, elenco, chiave generata ---------- */
.keys-only { display: none; }
body.can-keys .card.keys-only { display: flex; }
body.can-keys .card.keys-only[hidden] { display: none; }
.keyreq-banner { display: block; width: 100%; margin: 0 0 12px; padding: 12px; border-radius: 12px; text-align: left;
  background: #fef3c7; color: #78350f; border: 1px solid #f59e0b; font-weight: 600; }
.keyreq-banner[hidden] { display: none; }
.list li .code { font-size: 1.3em; font-weight: 700; letter-spacing: 3px; }
.list li .acts { display: flex; gap: 8px; }
.key-out { display: flex; flex-direction: column; gap: 8px; }
.key-out[hidden] { display: none; }
.key-out input { font-family: monospace; font-size: 13px; }
#key-gen-form { display: flex; gap: 8px; }
#key-gen-form input { flex: 1; }
