/* Nexo Bridge — design system unificato, mobile-first. */

/* Niente font web: usiamo lo stack di font di sistema (vedi --font/--font-mono).
   Scelta deliberata: il WebServer sincrono dell'ESP32 serializza le richieste, e
   servire i woff2 dal firmware peggiorava il primo caricamento (3 connessioni
   pesanti). Lo stack di sistema rende subito, zero richieste, zero CDN. */

:root {
  /* Palette allineata a nexoip.it: tema scuro + accent verde menta. */
  --bg: #080c10;
  --bg-elev: #0c1118;
  --surface: rgba(16, 21, 29, 0.95);
  --surface-2: #10151d;
  --surface-3: #141a24;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(61, 255, 160, 0.3);
  --text: #cdd6e0;
  --text-muted: #7a8a99;
  --text-dim: #4a5a6a;
  --text-strong: #f0f4f8;

  --primary: #3dffa0;
  --primary-h: #6fffc0;
  --primary-ink: #04140c;
  /* Alias convenzione nexoip.it */
  --accent: #3dffa0;
  --accent-hover: #6fffc0;
  --accent-dim: rgba(61, 255, 160, 0.12);
  --accent-glow: rgba(61, 255, 160, 0.3);
  --success: #3dffa0;
  --success-h: #6fffc0;
  --warning: #f5b53d;
  --warning-h: #ffcb6f;
  --danger: #ff7b72;
  --danger-h: #ff9a93;
  --info: #6fb4ff;
  --info-h: #9ccbff;
  --neutral: rgba(74, 90, 106, 0.7);
  --neutral-h: rgba(122, 138, 153, 0.85);

  --font: "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    system-ui, sans-serif;
  --font-mono: "Space Mono", ui-monospace, "JetBrains Mono", Consolas, monospace;

  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-base: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.375rem;
  --fs-2xl: 1.75rem;
  --lh-tight: 1.25;
  --lh-normal: 1.5;

  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-pill: 9999px;

  --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.5);
  --shadow-md: 0 8px 32px rgba(0, 0, 0, 0.6);
  --shadow-glow: 0 0 40px rgba(61, 255, 160, 0.15);
  --ring: 0 0 0 3px rgba(61, 255, 160, 0.35);

  --nav-h: 56px;
}

/* Reset minimale */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { background: var(--bg); }
body {
  min-height: 100vh;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-elev) 100%);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  -webkit-font-smoothing: antialiased;
}
img, svg { display: block; max-width: 100%; }
a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-h); }
hr, .hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: var(--sp-4) 0;
}

/* Topbar + nav */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  min-height: var(--nav-h);
  background: rgba(8, 12, 16, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title {
  font-size: var(--fs-lg);
  font-weight: 700;
  color: var(--text-strong);
  letter-spacing: -0.01em;
  flex: 1 1 auto;
  min-width: 0;
}
.topbar-title small {
  display: block;
  font-size: var(--fs-xs);
  font-weight: 400;
  color: var(--text-muted);
  margin-top: 2px;
}
/* Logo "NEXO BRIDGE" stile nexoip.it: mono, maiuscolo, accent menta. */
.brand {
  font-family: var(--font-mono);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  line-height: 1;
}
.brand-nexo   { color: var(--text-strong); }
.brand-bridge { color: var(--accent, var(--primary)); }
.drawer-header .brand { font-size: var(--fs-lg); margin: 0; }
.hamburger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
}
.hamburger:hover { background: var(--surface); }
.hamburger svg { width: 22px; height: 22px; }

.nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
/* Su mobile il nav nella topbar non si vede: si usa il drawer */
#navDesktop { display: none; }
.nav a {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-4);
  min-height: 44px;
  border-radius: var(--radius-md);
  color: var(--text);
  font-size: var(--fs-sm);
  font-weight: 500;
}
.nav a:hover { background: var(--surface); color: var(--text-strong); }
.nav a.active {
  background: linear-gradient(180deg, rgba(56, 189, 248, 0.18), rgba(56, 189, 248, 0.08));
  color: var(--primary-h);
  border: 1px solid rgba(56, 189, 248, 0.35);
}

/* Drawer mobile */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.6);
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(280px, 80vw);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  padding: var(--sp-4);
  z-index: 61;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
}
.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: var(--sp-3);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--sp-2);
}
.drawer-header h2 {
  margin: 0;
  font-size: var(--fs-base);
  font-weight: 700;
  color: var(--text-strong);
}
body.drawer-open .drawer { transform: translateX(0); }
body.drawer-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

/* Layout */
.main { padding: var(--sp-4); }
.container { max-width: 1200px; margin: 0 auto; }
.section + .section { margin-top: var(--sp-4); }
.section-title {
  color: var(--primary-h);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
  margin: 0 0 var(--sp-3) 0;
}
.section-sub {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  margin: 0 0 var(--sp-3) 0;
}

.row { display: flex; flex-wrap: wrap; gap: var(--sp-3); }
.row.between { justify-content: space-between; align-items: center; }
.row.end { justify-content: flex-end; }
.row.center { justify-content: center; align-items: center; }
.col { flex: 1 1 auto; min-width: 0; }
.grid { display: grid; gap: var(--sp-3); }
.grid-2, .grid-3, .grid-4, .grid-6, .grid-8, .grid-12 {
  display: grid;
  gap: var(--sp-2);
  /* Mobile-first: una colonna sola. Le colonne multiple partono dai breakpoint. */
  grid-template-columns: 1fr;
}

/* Card */
.card {
  background: var(--surface);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: var(--sp-4);
}
.card + .card { margin-top: var(--sp-3); }
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  flex-wrap: wrap;
  margin-bottom: var(--sp-3);
}
.card-footer {
  margin-top: var(--sp-4);
  padding-top: var(--sp-3);
  border-top: 1px solid var(--border);
}

/* Pulsanti — semantica fissa per azione */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: 0 var(--sp-4);
  min-height: 44px;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: var(--fs-sm);
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.1s ease, box-shadow 0.15s ease, background 0.15s ease;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.btn:active { transform: translateY(0); }
.btn:focus-visible { outline: none; box-shadow: var(--ring); }
.btn:disabled, .btn.is-loading {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { background: var(--primary-h); color: var(--primary-ink); }
.btn-success { background: var(--success); color: #052e1a; }
.btn-success:hover { background: var(--success-h); }
.btn-warning { background: var(--warning); color: #3a2304; }
.btn-warning:hover { background: var(--warning-h); }
.btn-danger { background: var(--danger); color: #3a0a0a; }
.btn-danger:hover { background: var(--danger-h); }
.btn-info { background: var(--info); color: #1c1640; }
.btn-info:hover { background: var(--info-h); }
.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border-strong);
}
.btn-ghost:hover { background: var(--surface); color: var(--text-strong); }
.btn-sm { min-height: 36px; padding: 0 var(--sp-3); font-size: var(--fs-xs); }
.btn-lg { min-height: 52px; padding: 0 var(--sp-5); font-size: var(--fs-base); }
.btn-block { width: 100%; }
.btn-icon { padding: 0; width: 44px; }

/* Badge */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px var(--sp-2);
  border-radius: var(--radius-pill);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.4;
  background: var(--neutral);
  color: var(--text);
}
.badge-ok, .badge-armed, .badge-active { background: var(--success); color: #052e1a; }
.badge-alarm, .badge-violated { background: var(--danger); color: #fff; }
.badge-fire { background: #ea580c; color: #fff; }
.badge-tamper, .badge-tamper_alarm { background: #f43f5e; color: #fff; }
.badge-alarm_memory { background: var(--warning); color: #3a2304; }
.badge-bypass { background: var(--primary); color: var(--primary-ink); }
.badge-isolate { background: #9333ea; color: #fff; }
.badge-masked { background: var(--neutral); color: var(--text); }
.badge-entry { background: #facc15; color: #1a1505; }
.badge-exit { background: #fde047; color: #1a1505; }
.badge-off { background: rgba(71, 85, 105, 0.5); color: var(--text-muted); }

/* Tile (zone/uscite/partizioni) */
.tile-grid {
  display: grid;
  gap: var(--sp-2);
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
}
.tile {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-2);
  min-height: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  text-align: center;
}
.tile-num {
  font-weight: 700;
  color: var(--primary-h);
  font-size: var(--fs-sm);
}
.tile-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  max-width: 100%;
  line-height: 1.15;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.tile-badges {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
  margin-top: 2px;
}

/* Form */
label { display: block; font-size: var(--fs-xs); color: var(--text-muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: var(--sp-2) var(--sp-3);
  font-family: inherit;
  font-size: 16px; /* >= 16 evita zoom autofocus iOS */
  min-height: 44px;
  line-height: var(--lh-normal);
}
textarea { min-height: 96px; font-family: var(--font-mono); font-size: var(--fs-sm); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--ring);
}
input[readonly], input:disabled, select:disabled, textarea:disabled {
  background: rgba(30, 41, 59, 0.5);
  color: var(--text-muted);
  font-family: var(--font-mono);
}
.field { margin-bottom: var(--sp-3); }
.help { font-size: var(--fs-xs); color: var(--text-muted); margin-top: 4px; }

/* Checkbox / radio inline */
.check {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  font-size: var(--fs-sm);
  color: var(--text);
  cursor: pointer;
}
.check input[type="checkbox"], .check input[type="radio"] {
  width: 18px;
  height: 18px;
  min-height: 0;
  margin: 0;
  accent-color: var(--primary);
}

/* Switch toggle */
.sw {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex: 0 0 auto;
}
.sw input { opacity: 0; width: 0; height: 0; }
.sw-slider {
  position: absolute;
  inset: 0;
  background: rgba(71, 85, 105, 0.6);
  border-radius: var(--radius-pill);
  transition: background 0.15s ease;
  cursor: pointer;
}
.sw-slider::before {
  content: "";
  position: absolute;
  left: 3px;
  top: 3px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s ease;
}
.sw input:checked + .sw-slider { background: var(--success); }
.sw input:checked + .sw-slider::before { transform: translateX(20px); }
.sw input:focus-visible + .sw-slider { box-shadow: var(--ring); }

/* Pill / chip */
.pill, .chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px var(--sp-2);
  border-radius: var(--radius-pill);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: var(--fs-xs);
  color: var(--text-muted);
}
.chip.on { background: rgba(34, 197, 94, 0.15); border-color: rgba(34, 197, 94, 0.4); color: var(--success-h); }
.chip.off { background: rgba(239, 68, 68, 0.15); border-color: rgba(239, 68, 68, 0.4); color: var(--danger-h); }

/* Banner stato sistema (dashboard) — connesso / push attive / non connesso */
.status-banner {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  margin-bottom: var(--sp-4);
}
.status-banner .status-dot {
  flex: 0 0 auto;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text-muted);
}
.status-text { min-width: 0; }
.status-title { color: var(--text-strong); font-weight: 700; font-size: var(--fs-base); }
.status-sub { color: var(--text-muted); font-size: var(--fs-sm); margin-top: 2px; }
.status-banner.on {
  border-color: rgba(61, 255, 160, 0.35);
  background: var(--accent-dim);
}
.status-banner.on .status-dot {
  background: var(--success);
  box-shadow: 0 0 0 0 var(--accent-glow);
  animation: pulseDot 2s infinite;
}
.status-banner.on .status-title { color: var(--success-h); }
.status-banner.warn { border-color: rgba(245, 181, 61, 0.4); background: rgba(245, 181, 61, 0.1); }
.status-banner.warn .status-dot { background: var(--warning); }
.status-banner.warn .status-title { color: var(--warning-h); }
.status-banner.off { border-color: rgba(255, 123, 114, 0.4); background: rgba(255, 123, 114, 0.1); }
.status-banner.off .status-dot { background: var(--danger); }
.status-banner.off .status-title { color: var(--danger-h); }
@keyframes pulseDot {
  0%   { box-shadow: 0 0 0 0 var(--accent-glow); }
  70%  { box-shadow: 0 0 0 7px rgba(61, 255, 160, 0); }
  100% { box-shadow: 0 0 0 0 rgba(61, 255, 160, 0); }
}

/* Lista info chiave/valore (Centrale, Modulo di rete, Cloud) */
.info-list { display: flex; flex-direction: column; }
.info-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-3) 0;
  border-bottom: 1px solid var(--border);
}
.info-row:last-child { border-bottom: none; }
.info-label { color: var(--text-muted); font-size: var(--fs-sm); flex: 0 0 auto; }
.info-value { color: var(--text-strong); text-align: right; min-width: 0; overflow-wrap: anywhere; }

/* Progress bar */
.bar {
  width: 100%;
  height: 8px;
  background: var(--surface-2);
  border-radius: var(--radius-pill);
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary-h));
  transition: width 0.3s ease;
}

/* Stat con barra di occupazione (Info HW: RAM, PSRAM, Flash…) */
.stat { padding: var(--sp-3) 0; border-bottom: 1px solid var(--border); }
.stat:last-child { border-bottom: none; }
.stat-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-2);
  margin-bottom: 6px;
}
.stat-label { color: var(--text); font-size: var(--fs-sm); font-weight: 500; }
.stat-pct {
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: var(--fs-sm);
  color: var(--text-strong);
}
.stat .bar { height: 10px; }
.stat-bar-track {
  height: 10px;
  background: var(--surface-3);
  border-radius: var(--radius-pill);
  overflow: hidden;
}
.stat-bar-fill {
  height: 100%;
  border-radius: var(--radius-pill);
  transition: width 0.4s ease;
  background: linear-gradient(90deg, var(--success), var(--success-h));
}
.stat-bar-fill.mid  { background: linear-gradient(90deg, var(--warning), var(--warning-h)); }
.stat-bar-fill.high { background: linear-gradient(90deg, var(--danger), var(--danger-h)); }
.stat-sub {
  margin-top: 6px;
  font-size: var(--fs-xs);
  color: var(--text-muted);
}
.stat-sub .mono { color: var(--text); }

/* Indicatore segnale Wi-Fi a barrette */
.sig {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
  margin-right: 6px;
  vertical-align: middle;
}
.sig i {
  display: block;
  width: 3px;
  border-radius: 1px;
  background: var(--surface-3);
}
.sig i:nth-child(1) { height: 35%; }
.sig i:nth-child(2) { height: 57%; }
.sig i:nth-child(3) { height: 78%; }
.sig i:nth-child(4) { height: 100%; }
.sig.lvl-1 i:nth-child(-n+1) { background: var(--danger); }
.sig.lvl-2 i:nth-child(-n+2) { background: var(--warning); }
.sig.lvl-3 i:nth-child(-n+3) { background: var(--accent); }
.sig.lvl-4 i:nth-child(-n+4) { background: var(--accent); }

/* Sotto-tab (es. Matter) */
.tab-bar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-1);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--sp-3);
}
.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: var(--sp-2) var(--sp-3);
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  min-height: 44px;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--primary-h); border-bottom-color: var(--primary); }

/* Pair code (Matter setup) */
.pair-code {
  font-family: var(--font-mono);
  font-size: var(--fs-2xl);
  letter-spacing: 0.08em;
  color: var(--primary-h);
  background: var(--surface-3);
  padding: var(--sp-3);
  border-radius: var(--radius-md);
  text-align: center;
  border: 1px solid var(--border);
}

/* Mono / pre / code */
.mono, code { font-family: var(--font-mono); font-size: 0.9em; }
pre {
  background: var(--surface-3);
  color: #cbd5e1;
  padding: var(--sp-3);
  border-radius: var(--radius-md);
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  border: 1px solid var(--border);
  margin: 0;
}
.muted { color: var(--text-muted); }
.dim { color: var(--text-dim); }
.strong { color: var(--text-strong); font-weight: 600; }

/* Stati helper */
.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: var(--fs-sm);
  padding: var(--sp-4);
  font-style: italic;
}
.empty-state.ok { color: var(--success-h); font-style: normal; }

/* Guasti (dashboard) */
.trouble-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.trouble-row {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  background: rgba(255, 123, 114, 0.08);
  border: 1px solid rgba(255, 123, 114, 0.3);
  border-left: 3px solid var(--danger);
  border-radius: var(--radius-sm);
}
.trouble-cat {
  flex: 0 0 auto;
  min-width: 92px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--danger-h);
}
.trouble-label { flex: 1 1 auto; font-size: var(--fs-sm); color: var(--text-strong); }
.skeleton {
  background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2));
  background-size: 200% 100%;
  animation: skel 1.2s infinite;
  border-radius: var(--radius-sm);
}
@keyframes skel { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* Toast (avvisi temporanei) */
.toast-host {
  position: fixed;
  bottom: var(--sp-4);
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  pointer-events: none;
  width: min(420px, calc(100vw - 32px));
}
.toast {
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-sm);
  color: var(--text-strong);
  box-shadow: var(--shadow-md);
  pointer-events: auto;
}
.toast-success { border-left: 4px solid var(--success); }
.toast-warning { border-left: 4px solid var(--warning); }
.toast-danger { border-left: 4px solid var(--danger); }

/* Tabella responsive: card-list su mobile, tabella vera da 768px */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}
table th, table td {
  text-align: left;
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table th { color: var(--text-muted); font-weight: 600; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: 0.04em; }

/* Mobile: tabella → card-list */
@media (max-width: 767px) {
  table thead { display: none; }
  table, table tbody, table tr, table td { display: block; width: 100%; }
  table tr {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: var(--sp-2);
    padding: var(--sp-2);
  }
  table td {
    border: none;
    padding: var(--sp-1) var(--sp-2);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--sp-2);
  }
  table td::before {
    content: attr(data-label);
    color: var(--text-muted);
    font-size: var(--fs-xs);
    font-weight: 600;
    flex-shrink: 0;
  }
}

/* Breakpoint da 640px in su: due colonne nelle griglie generiche */
@media (min-width: 640px) {
  .main { padding: var(--sp-5); }
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* Griglia Informazioni 2×2: righe di pari altezza così tutte e quattro le
     card si allineano in basso (la riga più alta detta l'altezza comune). */
  #infoGrid { grid-auto-rows: 1fr; }
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .grid-6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  .btn-block-sm { width: auto; }
}

@media (min-width: 768px) {
  .topbar { padding: var(--sp-3) var(--sp-5); }
  .hamburger { display: none; }
  .drawer, .drawer-backdrop { display: none; }
  #navDesktop { display: flex; }
  .nav { flex-direction: row; flex-wrap: wrap; gap: var(--sp-1); flex: 1 1 auto; }
  .nav a {
    min-height: 36px;
    padding: var(--sp-2) var(--sp-3);
    font-size: var(--fs-xs);
  }
  .grid-8 { grid-template-columns: repeat(8, minmax(0, 1fr)); }
  .grid-12 { grid-template-columns: repeat(12, minmax(0, 1fr)); }
}

@media (min-width: 1024px) {
  .main { padding: var(--sp-5) var(--sp-6); }
  .nav a { font-size: var(--fs-sm); }
}

/* Pulsanti d'azione dei form: staccati dalle card, mai appiccicati. */
.form-actions { margin: var(--sp-5) 0 var(--sp-4); }
/* Mobile: form principali a tutta larghezza */
@media (max-width: 639px) {
  .form-actions .btn { width: 100%; }
  .form-actions { display: flex; flex-direction: column; gap: var(--sp-3); }
}
@media (min-width: 640px) {
  .form-actions {
    display: flex;
    gap: var(--sp-3);
    justify-content: flex-end;
    flex-wrap: wrap;
  }
}

/* Sheet (slide-over modale per form e editor pesanti) */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.6);
  z-index: 70;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.sheet {
  position: fixed;
  z-index: 71;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.2s ease;
}
.sheet.open { transform: translateY(0); }
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
/* Mobile: bottom sheet a (quasi) tutta altezza */
@media (max-width: 767px) {
  .sheet {
    left: 0;
    right: 0;
    bottom: 0;
    top: 5vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }
}
/* Desktop: side sheet da destra */
@media (min-width: 768px) {
  .sheet {
    top: 0;
    bottom: 0;
    right: 0;
    width: min(560px, 92vw);
    border-radius: 0;
    transform: translateX(100%);
  }
  .sheet.open { transform: translateX(0); }
}
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.sheet-header h2 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 700;
  color: var(--text-strong);
}
.sheet-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--sp-4);
}
.sheet-footer {
  flex: 0 0 auto;
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  display: flex;
  gap: var(--sp-2);
  justify-content: flex-end;
  flex-wrap: wrap;
}
.sheet-footer .btn { min-height: 48px; }
@media (max-width: 639px) {
  .sheet-footer { flex-direction: column-reverse; }
  .sheet-footer .btn { width: 100%; }
}

/* Gruppi collassabili (details/summary) con stile coerente */
.group {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  margin-bottom: var(--sp-2);
  overflow: hidden;
}
.group > summary {
  list-style: none;
  cursor: pointer;
  padding: var(--sp-3);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  color: var(--text-strong);
  font-weight: 600;
  font-size: var(--fs-sm);
  min-height: 48px;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after {
  content: "▾";
  color: var(--text-muted);
  transition: transform 0.15s ease;
}
.group[open] > summary::after { transform: rotate(180deg); }
.group-body { padding: var(--sp-3); border-top: 1px solid var(--border); }

/* Toolbar lista utenti */
.users-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  align-items: center;
  margin-bottom: var(--sp-3);
}
.users-toolbar .search { flex: 1 1 200px; min-width: 0; }
.users-toolbar input[type="search"] { min-height: 40px; }

/* Lista compatta utenti (sostituisce la tabella) */
.user-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.user-item {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-3);
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: var(--sp-3);
  align-items: center;
}
.user-item .num {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--primary-h);
  background: var(--surface-3);
  border-radius: var(--radius-sm);
  padding: 2px var(--sp-2);
  font-size: var(--fs-sm);
  min-width: 40px;
  text-align: center;
}
.user-item .body { min-width: 0; }
.user-item .body .name {
  color: var(--text-strong);
  font-weight: 600;
  font-size: var(--fs-sm);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-item .body .meta {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  margin-top: 2px;
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
}
.user-item .actions {
  display: flex;
  gap: var(--sp-1);
  align-items: center;
}
.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 18px;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text-strong); }
.icon-btn.danger { color: var(--danger-h); border-color: rgba(239, 68, 68, 0.4); }
.icon-btn.danger:hover { background: rgba(239, 68, 68, 0.15); }
.icon-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.user-detail {
  grid-column: 1 / -1;
  border-top: 1px dashed var(--border);
  padding-top: var(--sp-3);
  margin-top: var(--sp-2);
  display: none;
}
.user-item.expanded .user-detail { display: block; }

/* Mass-action bar per chip groups */
.chip-actions {
  display: flex;
  gap: var(--sp-1);
  margin-bottom: var(--sp-2);
}
.chip-actions .btn { min-height: 32px; padding: 0 var(--sp-3); font-size: var(--fs-xs); }
.chip-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-grid .chip { min-width: 44px; justify-content: center; padding: 6px var(--sp-2); cursor: pointer; }

/* Indicatore pillola con conteggio sulla tab-bar */
.tab-btn .pill.alert { background: var(--warning); color: #3a2304; }

/* ============================================================
   Tab Comandi — card per soggetto (partizioni/zone/uscite)
   ============================================================ */
/* Cards compatte, allineate al look del tab Automazioni. */
.cmd-intro { padding: var(--sp-3); }
.cmd-intro .section-sub { margin: 0; }
.cmd-card { padding: var(--sp-3); }
.cmd-card + .cmd-card { margin-top: var(--sp-2); }
.cmd-card .section-title { margin: 0; }
.cmd-head {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin-bottom: var(--sp-2);
}
.cmd-head.between { justify-content: space-between; }
.cmd-icon {
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
  border-radius: var(--radius-md);
  background: var(--surface-3);
  color: var(--primary-h);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
}
.cmd-icon svg { width: 17px; height: 17px; }
.cmd-group { margin: var(--sp-2) 0; }
.cmd-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  margin-top: var(--sp-2);
}
.cmd-actions .btn { flex: 1 1 120px; }
.cmd-hint { color: var(--text-muted); font-size: var(--fs-xs); margin: 0; }
.cmd-warn {
  margin-top: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius-md);
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.4);
  color: var(--warning-h);
  font-size: var(--fs-sm);
}
.cmd-warn a { color: var(--warning-h); text-decoration: underline; }

/* Tapparelle: controllo diretto con slider posizione + pulsanti. */
.cover-cmd-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.cover-cmd {
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
}
.cover-cmd + .cover-cmd { margin-top: 0; }
.cover-cmd-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-2);
}
.cover-cmd-name { font-weight: 600; font-size: var(--fs-sm); }
.cover-cmd-pct {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--primary-h);
  font-size: var(--fs-sm);
}
.cover-cmd-range { width: 100%; margin: var(--sp-2) 0 2px; accent-color: var(--primary-h); }
.cover-cmd-range.moving { accent-color: var(--success-h); }
.cover-cmd-ends {
  display: flex;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: var(--fs-xs);
}
.cover-cmd-btns { display: flex; gap: var(--sp-2); margin-top: var(--sp-2); }
.cover-cmd-btns .btn { flex: 1 1 0; }
.cover-cmd-btns .btn svg { width: 16px; height: 16px; }
.cover-cmd-btns .btn span { margin-left: 4px; }

/* ---- Sottotab tab Comandi (Partizioni / Zone / Uscite / …) ---- */
.cmd-subtabs {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-1, 4px);
  margin-bottom: var(--sp-3);
  border-bottom: 1px solid var(--border);
}
.cmd-subtab {
  appearance: none;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
  margin-bottom: -1px;
}
.cmd-subtab:hover { color: var(--text); }
.cmd-subtab.active {
  color: var(--primary-h, var(--text));
  border-bottom-color: var(--primary-h, var(--text));
}

/* ---- Lista comandi: una riga per soggetto, stato a sinistra, azioni a destra ---- */
.cmd-list { display: flex; flex-direction: column; gap: var(--sp-2); }
.cmd-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
}
.cmd-item-main { min-width: 0; flex: 1 1 auto; }
.cmd-item-top { display: flex; align-items: baseline; gap: var(--sp-2); }
.cmd-item-id {
  font-weight: 700;
  color: var(--primary-h);
  font-size: var(--fs-sm);
  flex: 0 0 auto;
}
.cmd-item-name {
  font-size: var(--fs-sm);
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cmd-item-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin-top: 3px;
  min-height: 0;
}
.cmd-item-badges:empty { display: none; }
.cmd-item-ctl {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex: 0 0 auto;
}
.cmd-item-ctl .btn { white-space: nowrap; }

/* ---- Foglio modale che sale dal basso (opzioni inserimento partizione) ---- */
.cmd-sheet {
  position: fixed;
  inset: 0;
  z-index: 120;
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}
.cmd-sheet.open { visibility: visible; opacity: 1; transition: opacity 0.2s ease; }
.cmd-sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5); }
.cmd-sheet-panel {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 100%;
  max-width: 560px;
  transform: translate(-50%, 100%);
  transition: transform 0.25s ease;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius-lg, 16px) var(--radius-lg, 16px) 0 0;
  padding: var(--sp-3) var(--sp-4) calc(var(--sp-4) + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.35);
}
.cmd-sheet.open .cmd-sheet-panel { transform: translate(-50%, 0); }
.cmd-sheet-grip {
  width: 40px;
  height: 4px;
  border-radius: var(--radius-pill);
  background: var(--border);
  margin: 0 auto var(--sp-2);
}
.cmd-sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  margin-bottom: var(--sp-3);
}
.cmd-sheet-x {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  padding: 4px;
  border-radius: var(--radius-md);
}
.cmd-sheet-x:hover { color: var(--text); background: var(--surface-2); }
.cmd-sheet-actions {
  display: flex;
  gap: var(--sp-2);
  margin-top: var(--sp-4);
}
.cmd-sheet-actions .btn { flex: 1 1 0; }

/* ============================================================
   Tab Automazioni — editor regole stile Apple Home / HA
   ============================================================ */

.rule-list { display: flex; flex-direction: column; gap: var(--sp-3); margin-top: var(--sp-3); }

.rule-card {
  background: var(--surface);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}
.rule-card.expanded { box-shadow: var(--shadow-md); border-color: var(--border-strong); }

/* Flash real-time quando la regola scatta (evento SSE "rule"). */
.rule-card.fired { animation: ruleFired 1.5s ease-out; }
@keyframes ruleFired {
  0%   { border-color: var(--success-h); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.55); }
  15%  { border-color: var(--success-h); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0.35); background: rgba(34, 197, 94, 0.12); }
  100% { border-color: var(--border); box-shadow: var(--shadow-sm); background: transparent; }
}

.rule-card.disabled .rule-card-header { opacity: 0.55; }
.rule-card.disabled .rule-card-header .sw,
.rule-card.disabled .rule-card-header .rule-card-chev { opacity: 1; }

.rule-card-header {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  cursor: pointer;
}
.rule-card-header:hover { background: rgba(56, 189, 248, 0.04); }

.rule-icon-box {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: var(--radius-md);
  background: var(--surface-3);
  color: var(--text-strong);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
}
.rule-icon-box.rule-card-icon-lg { width: 48px; height: 48px; border-radius: var(--radius-md); }

.rule-card-info { min-width: 0; }
.rule-card-title {
  color: var(--text-strong);
  font-weight: 600;
  font-size: var(--fs-base);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rule-card-subtitle {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rule-card-lastexec {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 1px 6px;
  margin-top: 3px;
  font-size: var(--fs-xs);
  line-height: 1.25;
}
.rule-card-lastexec-lbl { color: var(--text-muted); opacity: 0.7; }
.rule-card-lastexec-never { color: var(--text-muted); opacity: 0.7; }
.rule-card-lastexec-val {
  color: var(--text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Avviso "avvio in corso": le automazioni non scattano durante la grace. */
.rule-boot-grace {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  margin: var(--sp-3) 0 0;
  padding: var(--sp-2) var(--sp-3);
  border: 1px solid var(--warning);
  border-radius: var(--radius-md);
  background: rgba(234, 179, 8, 0.1);
  color: var(--warning);
  font-size: var(--fs-xs);
}
/* display:flex ha più specificità di [hidden] {display:none} dello user-agent:
   senza questo l'attributo hidden non nasconde mai il banner. */
.rule-boot-grace[hidden] { display: none; }
.rule-boot-grace svg { flex: 0 0 auto; }

/* Card oscurate finché la grace post-boot è attiva (regole non operative). */
.rule-list.boot-grace .rule-card { opacity: 0.5; }
.rule-list.boot-grace .rule-card.expanded { opacity: 1; }

.rule-card-chev {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: var(--sp-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease;
}
.rule-card.expanded .rule-card-chev,
.counter-card.expanded .rule-card-chev { transform: rotate(90deg); color: var(--primary-h); }
.rule-card-chev:hover { color: var(--text-strong); }

/* Editor inline (espanso) */
.rule-editor {
  padding: var(--sp-3) var(--sp-4) var(--sp-4);
  border-top: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.25);
  display: flex;
  flex-direction: column;
  gap: var(--sp-4);
}

.rule-name-row { margin-bottom: 0; }
.rule-name-input {
  width: 100%;
  background: var(--surface-2);
  color: var(--text-strong);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-2) var(--sp-3);
  font-size: var(--fs-lg);
  font-weight: 600;
  min-height: 48px;
}
.rule-name-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--ring);
}

.rule-section { display: flex; flex-direction: column; gap: var(--sp-2); }
.rule-section-title {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  letter-spacing: 0.1em;
  font-weight: 700;
  text-transform: uppercase;
  padding: 0 var(--sp-1);
}
.rule-section-hint {
  color: var(--text-dim);
  font-size: 0.7rem;
  padding: 0 var(--sp-1);
  margin-top: -2px;
  margin-bottom: var(--sp-1);
}

/* Singola voce (trigger/condition/action) */
.rule-item {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.rule-item-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--sp-3);
}
.rule-item-title-wrap { min-width: 0; }
.rule-item-title {
  background: transparent;
  border: none;
  color: var(--text-strong);
  font-weight: 600;
  font-size: var(--fs-sm);
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
}
.rule-item-title svg { color: var(--text-muted); transition: transform 0.15s ease; }
.rule-item-title:hover { color: var(--primary-h); }
.rule-item-title:hover svg { color: var(--primary-h); }
.rule-item-summary {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rule-item-del {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.rule-item-del:hover {
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger-h);
  border-color: rgba(239, 68, 68, 0.4);
}

/* ---- Toolbar compatta (automazioni + contatori) ---- */
.rule-toolbar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-wrap: wrap;
  margin-bottom: var(--sp-3);
}
.rule-toolbar-status { font-size: var(--fs-xs); margin-left: auto; min-height: 1em; }

/* ---- Sottotab Logiche (Automazioni / Contatori / Presenza) ---- */
.logic-subtabs {
  display: flex;
  gap: var(--sp-1, 4px);
  margin-bottom: var(--sp-3);
  border-bottom: 1px solid var(--border);
}
.logic-subtab {
  appearance: none;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
  margin-bottom: -1px;
}
.logic-subtab:hover { color: var(--text); }
.logic-subtab.active {
  color: var(--primary-h, var(--text));
  border-bottom-color: var(--primary-h, var(--text));
}
.logic-pane-hint {
  margin: 0 0 var(--sp-3);
  font-size: var(--fs-xs);
  color: var(--text-muted);
  line-height: 1.4;
}

/* ---- Contatori ---- */
/* Sezione staccata dalle automazioni: separatore + spazio sopra. */
.counters-section {
  margin-top: var(--sp-6, 32px);
  padding-top: var(--sp-4, 20px);
  border-top: 1px solid var(--border);
}
.counters-title { margin: 0; font-size: var(--fs-lg, 1.1rem); }
.counter-list { display: flex; flex-direction: column; gap: var(--sp-2); }

.counter-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2, rgba(255, 255, 255, 0.02));
  overflow: hidden;
}
.counter-card-header {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
}
.counter-card-info { flex: 1 1 auto; min-width: 0; }
.counter-card-name { font-weight: 600; }
.counter-card-lastchg {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 1px 6px;
  margin-top: 2px;
  font-size: var(--fs-xs);
  line-height: 1.25;
}
.counter-val-num {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: var(--fs-lg, 1.1rem);
  min-width: 3ch;
  text-align: right;
}
/* Badge presenza Sì/No nel sottotab Presenza. */
.presence-badge {
  flex: 0 0 auto;
  font-size: var(--fs-xs);
  font-weight: 700;
  padding: 2px 10px;
  border-radius: 999px;
  white-space: nowrap;
  color: var(--text-muted);
  background: var(--surface-3, rgba(255, 255, 255, 0.05));
  border: 1px solid var(--border);
}
.presence-badge.on {
  color: #0a2a12;
  background: var(--success, #38c172);
  border-color: var(--success, #38c172);
}
.counter-card-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding: 0 var(--sp-3) var(--sp-3);
  border-top: 1px solid var(--border);
}
.counter-card-footer { display: flex; justify-content: flex-end; margin-top: var(--sp-1, 4px); }
.counter-set { width: 120px; }

.rule-item-body {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  padding-left: 56px; /* allinea sotto la riga del titolo, oltre l'icona */
}
@media (max-width: 519px) {
  .rule-item-body { padding-left: 0; }
}

.rule-field { display: flex; flex-direction: column; gap: 4px; }
.rule-field-label {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  margin: 0;
}
.rule-field-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--sp-2);
  align-items: center;
}
.tiny-label { color: var(--text-muted); font-size: var(--fs-xs); }

.rule-input {
  background: var(--surface-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px var(--sp-2);
  font-size: 15px;
  min-height: 40px;
  width: 100%;
  max-width: 320px;
}
.rule-input.rule-input-num { max-width: 100px; }
.rule-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(56, 189, 248, 0.25);
}

/* Bottone "+ Aggiungi …" */
.rule-add-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px dashed var(--border-strong);
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-md);
  padding: 10px var(--sp-3);
  cursor: pointer;
  font-size: var(--fs-sm);
  font-weight: 500;
  min-height: 44px;
  align-self: stretch;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.rule-add-btn:hover {
  border-color: var(--primary);
  color: var(--primary-h);
  background: rgba(56, 189, 248, 0.06);
}
.rule-add-btn svg { width: 18px; height: 18px; }

/* Operatore AND/OR (mostrato solo con >=2 condizioni) */
.rule-operator-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--sp-2);
  padding: 6px 8px;
  background: var(--surface-3);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.rule-operator-label { color: var(--text-muted); font-size: var(--fs-xs); }
.rule-operator-pills { display: inline-flex; gap: 4px; }
.rule-operator-pill {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: var(--radius-pill);
  padding: 4px 12px;
  font-size: var(--fs-xs);
  cursor: pointer;
  font-weight: 600;
}
.rule-operator-pill.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* Weekday pills */
.weekday-pills { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.weekday-pill {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--surface-3);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 700;
  font-size: var(--fs-xs);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.weekday-pill.on {
  background: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}
.weekday-pill:hover { border-color: var(--primary); }

/* Eccezioni calendario (trigger "Calendario") */
.rule-exceptions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
}
.rule-exception {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.rule-exception-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.rule-exc-dates {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}
/* Larghezze compatte: hanno priorità sulla width:100% globale degli input
   perché definite più avanti nel foglio di stile. */
.rule-input.rule-exc-day { width: 52px; max-width: 52px; text-align: center; padding-left: 6px; padding-right: 6px; }
.rule-input.rule-exc-month { width: 84px; max-width: 84px; }
.rule-input.rule-exc-year { width: 84px; max-width: 84px; }
.rule-exc-sep { color: var(--text-muted); font-size: var(--fs-sm); padding: 0 2px; }
.rule-exc-opts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
}
.rule-exc-opts .check { font-size: var(--fs-sm); color: var(--text); }

/* Id chips (zone/uscite/partizioni) */
.id-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  padding: 6px;
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 44px;
}
.id-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px 4px 10px;
  background: var(--primary);
  color: var(--primary-ink);
  border-radius: var(--radius-pill);
  font-size: var(--fs-xs);
  font-weight: 700;
  font-family: var(--font-mono);
}
.id-chip-x {
  background: transparent;
  border: none;
  color: var(--primary-ink);
  cursor: pointer;
  padding: 0;
  display: inline-flex;
  align-items: center;
  opacity: 0.7;
}
.id-chip-x:hover { opacity: 1; }
.id-chip-x svg { width: 14px; height: 14px; }
.id-chip-add { display: inline-flex; align-items: center; gap: 4px; }
.id-chip-add-input {
  width: 60px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 6px;
  font-size: var(--fs-xs);
  min-height: 28px;
}
.id-chip-add-input:focus {
  outline: none;
  border-color: var(--primary);
}
.id-chip-add-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  width: 28px;
  height: 28px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.id-chip-add-btn:hover { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.id-chip-add-btn svg { width: 14px; height: 14px; }
.id-chip-num { font-weight: 700; }
.id-chip-name {
  background: rgba(0, 0, 0, 0.18);
  padding: 1px 8px;
  margin-left: 2px;
  border-radius: var(--radius-pill);
  font-weight: 600;
  font-family: var(--font-sans);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Select "aggiungi" coi soli elementi dell'inventario (zone/uscite/partizioni) */
.id-chip-add-sel {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
  font-size: var(--fs-sm);
  max-width: 220px;
}
.id-chip-add-sel:focus { outline: none; border-color: var(--primary); }
.id-chip-add-sel:disabled { opacity: 0.5; }

/* Pulsante "Verifica adesso" + esito sotto a una condizione */
.rule-cond-test {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: var(--sp-1);
}
.rule-cond-test-res {
  font-size: var(--fs-sm);
  font-weight: 700;
}
.rule-cond-test-res.ok  { color: var(--success-h); }
.rule-cond-test-res.no  { color: var(--text-muted); }
.rule-cond-test-res.err { color: var(--danger-h); }

/* Numero + nome inventario (zone/uscita/partizione) */
.rule-num-name {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.rule-num-name-label {
  color: var(--text-muted);
  font-size: var(--fs-sm);
  font-style: italic;
}

/* Footer editor automazione: azioni sempre visibili (Test + Elimina) */
.rule-editor-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--sp-2);
  padding-top: var(--sp-2);
  margin-top: var(--sp-1);
  border-top: 1px solid var(--border);
}
.rule-editor-delete {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.rule-editor-delete svg { width: 16px; height: 16px; }

/* Opzioni avanzate */
.rule-advanced {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-3);
  overflow: hidden;
}
.rule-advanced-summary {
  list-style: none;
  cursor: pointer;
  padding: 10px var(--sp-3);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 40px;
}
.rule-advanced-summary::-webkit-details-marker { display: none; }
.rule-advanced-summary::after {
  content: "▾";
  color: var(--text-muted);
  font-size: var(--fs-sm);
  transition: transform 0.15s ease;
}
.rule-advanced[open] .rule-advanced-summary::after { transform: rotate(180deg); }
.rule-advanced-body {
  padding: var(--sp-3);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.rule-advanced-ctrls { display: flex; flex-wrap: wrap; gap: var(--sp-2); }

/* Popover scelta tipo */
.rule-popover {
  position: absolute;
  z-index: 80;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 6px;
  max-height: 70vh;
  overflow-y: auto;
  min-width: 220px;
}
.rule-popover-cat {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  padding: 8px var(--sp-2) 4px;
}
.rule-popover-item {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  width: 100%;
  padding: 8px var(--sp-2);
  background: transparent;
  border: none;
  color: var(--text);
  font-size: var(--fs-sm);
  text-align: left;
  cursor: pointer;
  border-radius: var(--radius-sm);
}
.rule-popover-item:hover { background: var(--surface); color: var(--text-strong); }
.rule-popover-ic {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--surface-3);
  color: var(--primary-h);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}
.rule-popover-ic svg { width: 16px; height: 16px; }

/* Mobile tweaks */
@media (max-width: 519px) {
  .rule-card-header { grid-template-columns: auto 1fr auto auto; padding: var(--sp-3); }
  .rule-icon-box.rule-card-icon-lg { width: 40px; height: 40px; }
  .rule-input { max-width: 100%; }
  .rule-input.rule-input-num { max-width: 80px; }
}

/* ---- Lightbox telecamere (anteprima scatto/diretta + archivio) ---- */
.cam-modal-back {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--sp-4);
  background: rgba(2, 6, 23, 0.72);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease;
}
.cam-modal-back.open { opacity: 1; pointer-events: auto; }
.cam-modal {
  display: flex;
  flex-direction: column;
  width: min(960px, 100%);
  max-height: 100%;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  transform: scale(0.96);
  transition: transform 0.18s ease;
}
.cam-modal-back.open .cam-modal { transform: scale(1); }
.cam-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.cam-modal-head h3 {
  margin: 0;
  font-size: var(--fs-md);
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.cam-modal-body {
  flex: 1 1 auto;
  min-height: 240px;          /* riserva spazio: niente collasso mentre carica */
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  padding: var(--sp-2);
}
.cam-modal-body img {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}
/* Placeholder di caricamento (scatto/clip/diretta in corso).
   Overlay assoluto centrato: non sposta l'immagine (niente "decentrato") e
   sparisce davvero quando hidden (display:flex batterebbe l'attributo hidden). */
.cam-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: var(--fs-sm);
}
.cam-loading[hidden] { display: none; }
.cam-loading::before {
  content: "";
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .25);
  border-top-color: #fff;
  animation: cam-spin .8s linear infinite;
}
@keyframes cam-spin { to { transform: rotate(360deg); } }

.cam-modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  flex-wrap: wrap;
  padding: var(--sp-3) var(--sp-4);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
  font-size: var(--fs-xs);
}
@media (max-width: 519px) {
  .cam-modal-back { padding: 0; }
  .cam-modal { width: 100%; height: 100%; max-height: 100%; border-radius: 0; border: 0; }
  .cam-modal-body img { max-height: none; }
}

/* Riga intestazione giorno nell'archivio telecamere */
.tbl tr.arc-day td {
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  font-weight: 700;
  font-size: var(--fs-xs);
  padding: 8px;
}

/* Tag sorgente dello scatto (nome automazione / "webui") nell'archivio */
.arc-tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  font-size: var(--fs-xs);
  color: var(--text-muted);
  vertical-align: middle;
}

/* Sotto-tab bloccato (es. Archivio senza microSD inserita) */
.logic-subtab.sub-locked { opacity: 0.45; cursor: not-allowed; }
.logic-subtab.sub-locked::after { content: " 🔒"; }
