:root {
  --bg: #f4f5f7; --card: #fff; --text: #1d2330; --muted: #667085; --line: #e3e6eb;
  --accent: #2f6fde; --accent-text: #fff; --ok: #1f9d55; --off: #b42318; --soft: #eef2f8;
  /* acceso (riquadri "on") e promemoria */
  --amber: #e08a00; --amber-soft: #fff5e3; --amber-line: #f3d199; --amber-text: #8a5300;
  --info-soft: #e8f0fd; --info-line: #c9dafa; --info-text: #1f4fa8;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #11151c; --card: #1a2029; --text: #e6e9ef; --muted: #98a2b3; --line: #2b3340; --accent: #5b8ff0; --soft: #222a36; --ok: #3ccf7d; --off: #f0715f;
    --amber: #f5a524; --amber-soft: #2e2413; --amber-line: #5c4318; --amber-text: #f7c368;
    --info-soft: #18243a; --info-line: #2a3f63; --info-text: #9dbcf5; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
a { color: var(--accent); }
header { display: flex; align-items: center; gap: 12px; padding: 12px 16px; background: var(--card); border-bottom: 1px solid var(--line); }
header .brand { font-weight: 700; font-size: 18px; text-decoration: none; color: var(--text); }
header .sp { flex: 1; }
header .who { color: var(--muted); font-size: 14px; }
main { max-width: 960px; margin: 0 auto; padding: 20px 16px 40px; }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 18px; margin: 28px 0 10px; }
button, .btn { font: inherit; border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; text-decoration: none; display: inline-block; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--off); }
button:disabled { opacity: .5; cursor: default; }
input, select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--text); width: 100%; }
label { display: block; font-size: 14px; color: var(--muted); margin: 12px 0 4px; }
.err { color: var(--off); min-height: 1.4em; margin-top: 10px; font-size: 14px; }
.msg { color: var(--ok); min-height: 1.4em; font-size: 14px; }
.hidden { display: none !important; }

.login { max-width: 360px; margin: 12vh auto 0; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 24px; }
.login h1 { text-align: center; }
.login button { width: 100%; margin-top: 18px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.tile { display: block; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 18px; text-decoration: none; color: var(--text); min-height: 110px; position: relative; }
.tile:hover { border-color: var(--accent); }
.tile .name { font-size: 18px; font-weight: 600; }
.tile .desc { color: var(--muted); font-size: 14px; margin-top: 6px; }
.tile .dot { position: absolute; top: 16px; right: 16px; width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.tile.off { opacity: .55; }
.tile.off .dot { background: var(--off); }
.empty { color: var(--muted); }
/* riquadro cliccabile tutto (link "steso"), i controlli del widget stanno sopra */
.tile .tile-link { display: block; color: var(--text); text-decoration: none; padding-right: 20px; }
.tile .tile-link::after { content: ""; position: absolute; inset: 0; border-radius: 16px; }
.tile .widget { position: relative; z-index: 1; margin-top: 10px; font-size: 14px; }
.w-detail { color: var(--muted); }
.w-detail:empty { display: none; }
.w-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.w-row button { padding: 5px 10px; font-size: 14px; }
.w-bri input { flex: 1; min-width: 0; padding: 0; border: 0; background: none; accent-color: var(--accent); }
.w-lbl { color: var(--muted); }
.w-val { min-width: 3.2em; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.w-msg { color: var(--off); font-size: 13px; }
.w-msg:empty { display: none; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 16px; margin-bottom: 12px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 160px; }
.user-head { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.user-head .name { font-weight: 600; flex: 1; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--soft); color: var(--muted); }
.levels { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; margin-top: 12px; }
.lv-app { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.lv-app .t { font-weight: 600; margin-bottom: 8px; }
.lv-msg { font-weight: 400; font-size: 13px; margin-left: 6px; }
.lv-desc { color: var(--muted); font-size: 13px; margin-top: 6px; min-height: 1.4em; }
/* scelta a 4 (Nessuno / Lettura / Scrittura / Admin): radio nascosti, etichette come bottoni */
.seg { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg label { margin: 0; color: var(--text); font-size: 14px; position: relative; }
.seg label + label { border-left: 1px solid var(--line); }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: block; text-align: center; padding: 8px 2px; cursor: pointer; }
.seg input:checked + span { background: var(--accent); color: var(--accent-text); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -3px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
.scroll { overflow-x: auto; }
dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--text); max-width: 380px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.mt { margin-top: 12px; }

/* telefono: tutto su una riga, scritte accorciate (CascaHome -> Casca, Amministrazione -> Admin) */
header { flex-wrap: nowrap; }
header .btn, header button { white-space: nowrap; }
@media (max-width: 600px) {
  header { padding: 10px 12px; gap: 8px; }
  header .who, header .long { display: none; }
  header .btn, header button { padding: 6px 10px; font-size: 14px; }
  main { padding: 16px 12px 32px; }
}
.muted { color: var(--muted); font-size: 14px; }
td.act { text-align: right; white-space: nowrap; }
td.act button { padding: 4px 10px; font-size: 14px; }

/* home "pannello per zone": orologio grande a sinistra, meteo a destra */
.today { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 14px; }
.clock { min-width: 0; }
.clock .time { font: 600 48px/1.05 var(--mono); letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.clock .date { color: var(--muted); font-size: 15px; margin-top: 4px; }
.clock .date::first-letter { text-transform: uppercase; }
.weather { text-align: right; max-width: 48%; flex-shrink: 0; }
.weather .wnow { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.weather .wicon { font-size: 30px; line-height: 1; }
.weather .wtemp { font-size: 28px; font-weight: 600; font-variant-numeric: tabular-nums; }
.weather .wtext { font-size: 14px; }
.weather .wmore { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.reminder { background: var(--info-soft); border: 1px solid var(--info-line); color: var(--info-text); border-radius: 999px; padding: 8px 16px; font-size: 14px; margin-bottom: 4px; }
.reminder.quiet { background: none; border-color: transparent; color: var(--muted); padding: 4px 4px; }
.reminder .rdot { vertical-align: baseline; }
.rdays label { white-space: nowrap; margin-right: 6px; }
.zone-label { font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 20px 4px 8px; }
.zone-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.zone-link { font-size: 14px; font-weight: 600; color: var(--text); text-decoration: none; margin: 14px 2px 6px; padding: 7px 14px; border: 1px solid var(--line); border-radius: 99px; background: var(--card); white-space: nowrap; }
.zone-link:hover, .zone-link:focus-visible { border-color: var(--accent); color: var(--accent); }
.home .tiles { grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); gap: 10px; }
main.home { max-width: 1100px; }
.home .tile { border-radius: 16px; padding: 14px; min-height: 92px; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.home .tile.wide { grid-column: span 2; }
.home .tile .name { display: block; font-size: 16px; }
.home .tile .state { display: block; color: var(--muted); font-size: 14px; margin-top: 4px; }
.home .tile .dot { top: 14px; right: 14px; width: 8px; height: 8px; }
.home .tile.on { background: var(--amber-soft); border-color: var(--amber-line); }
.home .tile.on .state { color: var(--amber-text); font-weight: 600; }
.home .tile.on .dot { background: var(--amber); box-shadow: 0 0 0 3px var(--amber-line); }
.home .tile .widget { margin-top: 8px; }
@media (max-width: 600px) {
  .clock .time { font-size: 38px; }
  .weather .wicon { font-size: 24px; }
  .weather .wtemp { font-size: 22px; }
  .home .tiles { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .bt-main { padding: 10px 8px; min-height: 104px; }
  .bt-name { font-size: 13px; }
}
@media (max-width: 340px) { .clock .time { font-size: 32px; } }

/* pagina di dettaglio di un servizio (/d/<id>): intestazione con stato, comandi, attivita' per giorno */
.dhead { position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px 18px; }
.dhead h1 { margin: 0; font-size: 24px; padding-right: 20px; }
.dhead .dstate { color: var(--muted); font-size: 15px; margin-top: 2px; }
.dhead .dot { position: absolute; top: 20px; right: 18px; width: 10px; height: 10px; border-radius: 50%; background: var(--ok); }
.dhead.off { opacity: .6; }
.dhead.off .dot { background: var(--off); }
.dhead.on { background: var(--amber-soft); border-color: var(--amber-line); }
.dhead.on .dstate { color: var(--amber-text); font-weight: 600; }
.dhead.on .dot { background: var(--amber); box-shadow: 0 0 0 3px var(--amber-line); }
.dctl { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 4px 18px 14px; margin-top: 10px; font-size: 15px; }
.dctl .w-row button { padding: 9px 16px; font-size: 15px; }
.dopen { display: block; text-align: center; margin-top: 10px; padding: 11px 14px; border-radius: 14px; }
a.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.tl-label { font-size: 14px; font-weight: 600; margin: 14px 4px 6px; }
.timeline { list-style: none; margin: 0; padding: 4px 0 4px 0; background: var(--card); border: 1px solid var(--line); border-radius: 16px; }
.tl-item { display: grid; grid-template-columns: 3.4em 1fr; gap: 10px; padding: 9px 14px 9px 12px; position: relative; }
.tl-item + .tl-item { border-top: 1px solid var(--line); }
.tl-time { color: var(--muted); font: 14px/1.45 var(--mono); font-variant-numeric: tabular-nums; padding-left: 12px; position: relative; }
/* pallino della linea del tempo */
.tl-time::before { content: ""; position: absolute; left: 0; top: .55em; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.tl-item.ko .tl-time::before { background: var(--off); }
.tl-item::before { content: ""; position: absolute; left: 15px; top: 0; bottom: 0; width: 1px; background: var(--line); }
.tl-item:first-child::before { top: 20px; }
.tl-item:last-child::before { bottom: auto; height: 20px; }
.tl-item:only-child::before { display: none; }
.tl-what { font-weight: 500; overflow-wrap: anywhere; }
.tl-who { color: var(--muted); font-size: 13px; }
.tl-res { font-size: 12px; padding: 1px 7px; border-radius: 99px; background: var(--soft); margin-left: 4px; white-space: nowrap; }
.tl-res.ok { color: var(--ok); }
.tl-res.ko { color: var(--off); }

/* Casa: un elemento per dispositivo (widget multiplo). Accessi = riquadri con "Apri" a pressione prolungata, Luci = righe in una scheda */
.home .tile.dev { display: flex; flex-direction: column; gap: 2px; }
.tile.dev .hold { position: relative; overflow: hidden; margin-top: auto; padding: 8px 10px; width: 100%; touch-action: manipulation; -webkit-user-select: none; user-select: none; }
.tile.dev .hold .fill { position: absolute; inset: 0; background: var(--accent); opacity: .3; transform: scaleX(0); transform-origin: left; transition: transform .15s ease-out; }
.tile.dev .hold.holding .fill { transform: scaleX(1); transition: transform 1s linear; }
.tile.dev .hold.done .fill { transform: scaleX(1); transition: none; opacity: .5; }
.tile.dev .hold .lbl { position: relative; }
.tile.dev .w-msg, .devrow .w-msg { display: block; }
.home .tile.dev.off { opacity: .5; }
.devrows { background: var(--card); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; }
.devrow { display: flex; align-items: center; gap: 12px; padding: 12px 14px; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.devrow + .devrow { border-top: 1px solid var(--line); }
.devrow.off { opacity: .5; }
.devtxt { flex: 1; min-width: 0; }
.devtxt .name { display: block; font-weight: 600; }
.devtxt .state, .devtxt .timer { display: block; color: var(--muted); font-size: 14px; }
.devtxt .timer:empty { display: none; }
.devrow.on .state { color: var(--amber-text); font-weight: 600; }
.sw { position: relative; flex: none; width: 50px; height: 30px; padding: 0; border-radius: 99px; background: var(--line); border: 0; transition: background .15s; }
.sw .knob { position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.sw[aria-checked="true"] { background: var(--amber); }
.sw[aria-checked="true"] .knob { transform: translateX(20px); }
.sw:disabled { opacity: .5; }
.dctl.multi { background: none; border: 0; padding: 0; }

/* riquadri-bottone (home e dettaglio): icona, nome, stato; amber se acceso, grigio se offline */
.home .tile.bt { padding: 0; min-height: 0; overflow: hidden; }
.bt-main { position: relative; overflow: hidden; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; width: 100%; height: 100%; min-height: 120px; padding: 12px 10px; margin: 0; border: 0; border-radius: 16px; background: none; color: var(--text); text-align: left; cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }
.bt-main[aria-disabled="true"] { cursor: default; }
.bt-main:active:not([aria-disabled="true"]) { background: var(--soft); }
.bt-main:focus-visible { outline: 2px solid var(--accent); outline-offset: -3px; }
.bt-ic { display: block; color: var(--muted); margin-bottom: 4px; }
.bt-ic .ico { width: 28px; height: 28px; display: block; }
.bt-name { font-size: 14px; font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; position: relative; }
.bt-state { font-size: 12px; color: var(--muted); max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; position: relative; }
.bt-timer { display: flex; align-items: center; gap: 3px; margin-top: auto; padding-top: 4px; max-width: 100%; font-size: 11px; color: var(--muted); position: relative; }
.bt-timer .ico { width: 12px; height: 12px; flex: none; }
.bt-timer-txt { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bt-more { position: absolute; top: 2px; right: 2px; z-index: 2; width: 32px; height: 32px; display: grid; place-items: center; border-radius: 10px; color: var(--muted); text-decoration: none; font-size: 20px; line-height: 1; }
.bt-more:hover, .bt-more:focus-visible { background: var(--soft); color: var(--text); }
.home .tile.bt.on .bt-ic { color: var(--amber); }
.home .tile.bt.on .bt-state { color: var(--amber-text); font-weight: 600; }
.home .tile.bt.off { opacity: .5; filter: grayscale(1); }
.home .tile.bt.off .bt-main { cursor: default; }
.bt-main .w-msg { position: relative; font-size: 11px; }
.bt-main .fill { position: absolute; inset: 0; background: var(--accent); opacity: .3; transform: scaleX(0); transform-origin: left; transition: transform .15s ease-out; pointer-events: none; }
.bt-main.holding .fill { transform: scaleX(1); transition: transform 1s linear; }
.bt-main.done .fill { transform: scaleX(1); transition: none; opacity: .5; }
.detail .bt-more { display: none; }
.detail .tile.bt .bt-main { min-height: 104px; }

/* pulsante "Home" nell'intestazione di dettaglio e amministrazione */
header .homebtn { font-weight: 600; border-color: var(--accent); color: var(--accent); }
