/* =============================================================
   DESIGN SYSTEM — Dashboard RSA (+CARE)
   =============================================================
   Colori istituzionali: verde (#388865) + giallo (#FFDE00)
   Font: Inter (Google Fonts)
   ============================================================= */

/* ===== CSS VARIABLES ===== */
:root {
  /* --- Brand: Verde (primary) --- */
  --primary:        #388865;
  --primary-dark:   #2c6e50;
  --primary-deeper: #1e4d38;
  --primary-light:  #B8E0D2;
  --primary-subtle: #e8f5f0;

  /* --- Brand: Giallo (accent) --- */
  --accent:       #FFDE00;
  --accent-dark:  #e6c800;
  --accent-light: #fff9db;

  /* --- Surfaces --- */
  --bg:           #F2F4F7;
  --surface:      #ffffff;
  --surface-glass: rgba(255, 255, 255, 0.72);
  --border:       #DAE0E8;
  --border-light: #EDF0F4;

  /* --- Text --- */
  --text:         #1C2A3A;
  --text-secondary: #4A6070;
  --text-muted:   #8A9BAA;
  --text-on-primary: #ffffff;
  --text-on-accent: #3A3000;

  /* --- Status --- */
  --success:      #22c55e;
  --success-bg:   #dcfce7;
  --warning:      #f59e0b;
  --warning-bg:   #fef3c7;
  --danger:       #ef4444;
  --danger-bg:    #fee2e2;
  --info:         #3b82f6;
  --info-bg:      #dbeafe;
  --neutral:      #94a3b8;
  --neutral-bg:   #f1f5f9;

  /* --- Dati ambientali (sensori ambientali): mai verde/giallo/rosso (riservati
     alla gravità clinica), mai --info (riservato a interazione/occupazione).
     Violetto tenue: letto come "misura passiva", non allarme né azione. --- */
  --ambient:      #a78bfa;
  --ambient-bg:   #f1edfe;
  --ambient-border: #ddd3fb;

  /* --- Layout --- */
  --sidebar-w:    256px;
  --topbar-h:     64px;

  /* --- Spacing scale --- */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 20px;
  --sp-6: 24px;
  --sp-8: 32px;
  --sp-10: 40px;
  --sp-12: 48px;

  /* --- Typography --- */
  --font:         'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  --fs-xs:        12px;
  --fs-sm:        13px;
  --fs-base:      14.5px;
  --fs-md:        16px;
  --fs-lg:        18px;
  --fs-xl:        22px;
  --fs-2xl:       28px;
  --fs-3xl:       34px;

  /* --- Radius --- */
  --r-sm:  6px;
  --r-md:  10px;
  --r-lg:  14px;
  --r-xl:  20px;
  --r-full: 9999px;

  /* --- Shadows --- */
  --shadow-sm:   0 1px 3px rgba(26, 46, 38, 0.06);
  --shadow-md:   0 4px 12px rgba(26, 46, 38, 0.08);
  --shadow-lg:   0 8px 28px rgba(26, 46, 38, 0.10);
  --shadow-xl:   0 16px 48px rgba(26, 46, 38, 0.12);
  --shadow-glow: 0 0 20px rgba(56, 136, 101, 0.15);

  /* --- Transitions --- */
  --ease:     cubic-bezier(0.4, 0, 0.2, 1);
  --duration: 0.2s;
  --duration-slow: 0.35s;
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}
a { color: inherit; text-decoration: none; }
img { max-width: 100%; display: block; }
button { font-family: var(--font); }

/* ===== LAYOUT ===== */
.app-wrapper {
  display: flex;
  min-height: 100vh;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  background: linear-gradient(180deg, var(--primary-deeper) 0%, var(--primary-dark) 60%, var(--primary) 100%);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0;
  height: 100vh;
  z-index: 100;
  transition: transform var(--duration-slow) var(--ease);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-6);
  height: var(--topbar-h);
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}
.sidebar-logo-img {
  height: 34px;
  width: auto;
  object-fit: contain;
  filter: brightness(0) invert(1);
  opacity: 0.95;
}
.sidebar-logo-text {
  font-size: var(--fs-xl);
  font-weight: 800;
  color: #fff;
  letter-spacing: 1.5px;
  line-height: 1;
}
.sidebar-logo-text .accent {
  color: var(--accent);
}

.sidebar-nav {
  flex: 1;
  padding: var(--sp-5) var(--sp-3);
  overflow-y: auto;
}
.sidebar-nav ul { list-style: none; }
.sidebar-nav li { margin-bottom: 2px; }

.nav-label {
  font-size: var(--fs-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.4px;
  color: rgba(255,255,255,0.35);
  padding: var(--sp-5) var(--sp-3) var(--sp-2);
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r-md);
  color: rgba(255,255,255,0.72);
  font-size: var(--fs-base);
  font-weight: 500;
  transition: all var(--duration) var(--ease);
  position: relative;
}
.sidebar-nav a:hover {
  background: rgba(255,255,255,0.1);
  color: #fff;
  transform: translateX(2px);
}
.sidebar-nav a.active {
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--accent);
}
.sidebar-nav a .nav-icon {
  font-size: var(--fs-lg);
  width: 24px;
  text-align: center;
  flex-shrink: 0;
  line-height: 1;
}
.sidebar-nav a .nav-badge {
  margin-left: auto;
  background: var(--danger);
  color: #fff;
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: 1;
  padding: 3px 7px;
  border-radius: var(--r-full);
  flex-shrink: 0;
}

.sidebar-footer {
  padding: var(--sp-4) var(--sp-6);
  border-top: 1px solid rgba(255,255,255,0.1);
  font-size: var(--fs-xs);
  color: rgba(255,255,255,0.3);
}

/* ===== TOPBAR ===== */
.topbar {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: var(--topbar-h);
  background: var(--surface-glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--sp-8);
  z-index: 90;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
}
.topbar-title {
  font-size: var(--fs-lg);
  font-weight: 600;
  color: var(--text);
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: var(--sp-4);
}

.topbar-avatar {
  width: 38px; height: 38px;
  border-radius: var(--r-full);
  background: linear-gradient(135deg, var(--primary-light), var(--primary-subtle));
  border: 2px solid var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--fs-base);
  font-weight: 700;
  color: var(--primary-dark);
  cursor: pointer;
  transition: transform var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.topbar-avatar:hover {
  transform: scale(1.08);
  box-shadow: var(--shadow-glow);
}

.topbar-badge {
  background: var(--accent-light);
  color: var(--text-on-accent);
  font-size: var(--fs-xs);
  font-weight: 700;
  padding: 3px 10px;
  border-radius: var(--r-full);
  border: 1px solid rgba(255,222,0,0.3);
}

/* ===== MAIN CONTENT ===== */
.content-area {
  margin-left: var(--sidebar-w);
  margin-top: var(--topbar-h);
  flex: 1;
  min-width: 0;
  padding: var(--sp-8);
  min-height: calc(100vh - var(--topbar-h));
}

/* ===== FASCE TAB SCORREVOLI (frecce, vedi main.js initScrollableTabs) ===== */
.tabs-scroll-wrap {
  position: relative;
}
.tabs-scroll-wrap.has-scroll {
  padding: 0 26px;
}
.tabs-scroll-arrow {
  position: absolute;
  top: -4px;
  bottom: 8px;
  width: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  background: var(--primary);
  color: var(--text-on-primary);
  box-shadow: 0 2px 8px rgba(56,136,101,0.35);
  border-radius: var(--r-full);
  z-index: 2;
  transition: all var(--duration) var(--ease);
}
.tabs-scroll-arrow:hover {
  background: var(--primary-dark);
  transform: translateY(-1px);
}
.tabs-scroll-arrow-left  { left: -6px; }
.tabs-scroll-arrow-right { right: -6px; }
.tabs-scroll-arrow.is-hidden,
.tabs-scroll-wrap:not(.has-scroll) .tabs-scroll-arrow {
  display: none;
}

/* ===== CARDS ===== */
.card {
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-light);
  padding: var(--sp-6);
  margin-bottom: var(--sp-6);
  transition: box-shadow var(--duration-slow) var(--ease);
}
.card:hover {
  box-shadow: var(--shadow-md);
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--sp-5);
  padding-bottom: var(--sp-4);
  border-bottom: 1px solid var(--border-light);
}
.card-title {
  font-size: var(--fs-md);
  font-weight: 700;
  color: var(--text);
}

/* ===== STAT CARDS ===== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--sp-5);
  margin-bottom: var(--sp-8);
}
.stat-card {
  background: var(--surface);
  border-radius: var(--r-lg);
  padding: var(--sp-6);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  transition: all var(--duration-slow) var(--ease);
  cursor: default;
  position: relative;
  overflow: hidden;
}
.stat-card::after {
  content: '';
  position: absolute;
  top: 0; right: 0;
  width: 80px; height: 80px;
  border-radius: 50%;
  background: var(--primary-subtle);
  opacity: 0.5;
  transform: translate(30px, -30px);
  transition: transform var(--duration-slow) var(--ease);
}
.stat-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-lg);
}
.stat-card:hover::after {
  transform: translate(20px, -20px) scale(1.2);
}
.stat-icon {
  width: 52px; height: 52px;
  border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
  font-size: 24px;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}
.stat-icon-green  { background: var(--primary-subtle); }
.stat-icon-yellow { background: var(--accent-light); }
.stat-icon-red    { background: var(--danger-bg); }
.stat-icon-blue   { background: var(--info-bg); }
.stat-icon-gray   { background: var(--neutral-bg); }

.stat-info { position: relative; z-index: 1; min-width: 0; flex: 1 1 auto; }
.stat-value {
  font-size: var(--fs-2xl);
  font-weight: 800;
  color: var(--text);
  line-height: 1.15;
  letter-spacing: -0.5px;
  overflow-wrap: break-word;
  word-break: break-word;
}
.stat-label {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: var(--sp-1);
}
.stat-card-total {
  border-left: 4px solid var(--primary);
}
.stat-card-total .stat-value {
  font-size: calc(var(--fs-2xl) + 4px);
}
.stat-trend {
  font-size: var(--fs-xs);
  font-weight: 600;
  margin-top: var(--sp-1);
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 6px;
  border-radius: var(--r-sm);
}
.stat-trend-up   { color: var(--success); background: var(--success-bg); }
.stat-trend-down { color: var(--danger);  background: var(--danger-bg); }

/* ===== WELCOME BANNER ===== */
.welcome-banner {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 50%, var(--primary-deeper) 100%);
  border-radius: var(--r-xl);
  padding: var(--sp-8) var(--sp-10);
  margin-bottom: var(--sp-8);
  color: white;
  position: relative;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
}
.welcome-banner::before {
  content: '';
  position: absolute;
  top: -20%; right: -5%;
  width: 280px; height: 280px;
  border-radius: 50%;
  background: rgba(255,222,0,0.08);
}
.welcome-banner::after {
  content: '';
  position: absolute;
  bottom: -30%; right: 10%;
  width: 200px; height: 200px;
  border-radius: 50%;
  background: rgba(255,255,255,0.04);
}
.welcome-title {
  font-size: var(--fs-xl);
  font-weight: 700;
  margin-bottom: var(--sp-2);
  position: relative;
  z-index: 1;
}
.welcome-subtitle {
  font-size: var(--fs-base);
  color: rgba(255,255,255,0.75);
  position: relative;
  z-index: 1;
}
.welcome-date {
  font-size: var(--fs-sm);
  color: rgba(255,255,255,0.5);
  margin-top: var(--sp-3);
  position: relative;
  z-index: 1;
}

/* ===== GRID LAYOUTS ===== */
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--sp-6);
}
.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--sp-6);
}

/* ===== ACTIVITY LIST ===== */
.activity-list {
  list-style: none;
}
.activity-item {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-4);
  padding: var(--sp-4) 0;
  border-bottom: 1px solid var(--border-light);
  transition: background var(--duration) var(--ease);
}
.activity-item:last-child { border-bottom: none; }
.activity-item:hover { background: var(--primary-subtle); margin: 0 calc(-1 * var(--sp-4)); padding-left: var(--sp-4); padding-right: var(--sp-4); border-radius: var(--r-md); }

.activity-icon {
  width: 40px; height: 40px;
  border-radius: var(--r-md);
  display: flex; align-items: center; justify-content: center;
  font-size: var(--fs-lg);
  flex-shrink: 0;
  background: var(--primary-subtle);
}
.activity-content { flex: 1; min-width: 0; }
.activity-text {
  font-size: var(--fs-base);
  color: var(--text);
  font-weight: 500;
}
.activity-meta {
  font-size: var(--fs-xs);
  color: var(--text-muted);
  margin-top: 2px;
}

/* ===== QUICK ACTIONS ===== */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: var(--sp-3);
}
.quick-action {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-5) var(--sp-4);
  border-radius: var(--r-lg);
  border: 1.5px dashed var(--border);
  background: var(--surface);
  color: var(--text-secondary);
  font-size: var(--fs-sm);
  font-weight: 600;
  cursor: pointer;
  transition: all var(--duration) var(--ease);
  text-align: center;
}
.quick-action:hover {
  border-color: var(--primary);
  border-style: solid;
  color: var(--primary-dark);
  background: var(--primary-subtle);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}
.quick-action-icon {
  font-size: 28px;
  line-height: 1;
}

/* ===== EMPTY STATE ===== */
.empty-state {
  text-align: center;
  padding: var(--sp-10) var(--sp-6);
  color: var(--text-muted);
}
.empty-state-icon {
  font-size: 48px;
  margin-bottom: var(--sp-4);
  opacity: 0.5;
}
.empty-state-title {
  font-size: var(--fs-md);
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: var(--sp-2);
}
.empty-state-text {
  font-size: var(--fs-sm);
  max-width: 320px;
  margin: 0 auto;
  line-height: 1.5;
}

/* ===== PROGRESS BAR ===== */
.progress-wrap {
  width: 100%;
  height: 6px;
  background: var(--border-light);
  border-radius: var(--r-full);
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--primary-dark));
  border-radius: var(--r-full);
  transition: width 0.6s var(--ease);
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  padding: 10px 20px;
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--duration) var(--ease);
  line-height: 1;
  font-family: var(--font);
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--primary);
  color: var(--text-on-primary);
  box-shadow: 0 2px 8px rgba(56,136,101,0.25);
}
.btn-primary:hover {
  background: var(--primary-dark);
  box-shadow: 0 4px 16px rgba(56,136,101,0.35);
  transform: translateY(-1px);
}

.btn-accent {
  background: var(--accent);
  color: var(--text-on-accent);
  box-shadow: 0 2px 8px rgba(255,222,0,0.25);
}
.btn-accent:hover {
  background: var(--accent-dark);
  box-shadow: 0 4px 16px rgba(255,222,0,0.4);
  transform: translateY(-1px);
}

.btn-ghost {
  background: transparent;
  color: var(--primary-dark);
  border: 1.5px solid var(--border);
}
.btn-ghost:hover {
  border-color: var(--primary);
  background: var(--primary-subtle);
}

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
}
.btn-danger:hover {
  background: #fecaca;
}

.btn-sm { padding: 6px 14px; font-size: var(--fs-sm); }
.btn-lg { padding: 14px 28px; font-size: var(--fs-md); }

/* ===== FORMS ===== */
.form-group {
  margin-bottom: var(--sp-5);
}
.form-label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--text);
  margin-bottom: var(--sp-2);
}
.form-control {
  width: 100%;
  padding: 11px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
  outline: none;
}
.form-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 136, 101, 0.12);
}
.form-control::placeholder { color: var(--text-muted); }

.form-row {
  display: flex;
  gap: var(--sp-4);
}
.form-row .form-group { flex: 1; }

/* ===== TABLE ===== */
.table-wrap { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-base);
}
thead th {
  text-align: left;
  padding: var(--sp-3) var(--sp-4);
  font-size: var(--fs-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: var(--text-muted);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
tbody td {
  padding: var(--sp-4);
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr {
  transition: background var(--duration) var(--ease);
}
tbody tr:hover td { background: var(--primary-subtle); }

/* ===== BADGE ===== */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: 1.4;
}
.badge-green  { background: var(--success-bg); color: #16a34a; }
.badge-yellow { background: var(--warning-bg); color: #b45309; }
.badge-red    { background: var(--danger-bg);  color: var(--danger); }
.badge-gray   { background: #f1f5f9; color: #64748b; box-shadow: inset 0 0 0 1px #cbd5e1; }
.badge-blue   { background: var(--info-bg);   color: var(--info); }

/* ===== MARKDOWN LITE (testo generato dall'AI: Diario Sociale, Analisi Documenti) ===== */
.md-lite p { margin: 0 0 var(--sp-2); }
.md-lite p:last-child { margin-bottom: 0; }
.md-lite h4, .md-lite h5, .md-lite h6 { margin: var(--sp-3) 0 var(--sp-2); font-weight: 700; color: var(--text); }
.md-lite h4 { font-size: var(--fs-lg); }
.md-lite h5 { font-size: var(--fs-md); }
.md-lite h6 { font-size: var(--fs-base); }
.md-lite ul, .md-lite ol { margin: 0 0 var(--sp-2); padding-left: 1.4em; }
.md-lite li { margin-bottom: 2px; }
.md-lite code { background: var(--bg); border-radius: 4px; padding: 1px 5px; font-size: 0.9em; }
.md-lite strong { font-weight: 700; }

/* ===== FLASH MESSAGES ===== */
.flash-container { margin-bottom: var(--sp-5); }
.flash {
  padding: var(--sp-4) var(--sp-5);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-weight: 500;
  margin-bottom: var(--sp-3);
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  animation: flashSlideIn 0.35s var(--ease);
  border-left: 4px solid transparent;
}
@keyframes flashSlideIn {
  from { opacity: 0; transform: translateY(-10px); }
  to   { opacity: 1; transform: translateY(0); }
}
.flash-success { background: var(--success-bg); color: #16a34a; border-left-color: var(--success); }
.flash-error   { background: var(--danger-bg);  color: var(--danger); border-left-color: var(--danger); }
.flash-warning { background: var(--warning-bg); color: #b45309; border-left-color: var(--warning); }
.flash-info    { background: var(--info-bg);    color: var(--info); border-left-color: var(--info); }

/* ===== TOAST GENERICO (successo/errore azioni, qualsiasi pagina) =====
   Globale in base.html (funzione mostraToast(msg, ok=true)) — prima
   duplicato localmente in alcune pagine (es. pazienti/anagrafica.html),
   altre lo chiamavano senza mai definirlo (bug: nessun popup mostrato in
   caso di errore, solo silenzio in console). Un solo elemento riusato per
   tutte le chiamate sulla stessa pagina (id fisso). */
.app-toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--primary-dark);
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--r-lg);
  font-size: var(--fs-sm);
  font-weight: 600;
  box-shadow: var(--shadow-xl);
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: min(420px, calc(100vw - 48px));
  transform: translateY(80px);
  opacity: 0;
  transition: all 0.35s cubic-bezier(.4, 0, .2, 1);
}
.app-toast.show {
  transform: translateY(0);
  opacity: 1;
}

/* ===== TOAST NOTIFICHE IoT (allarmi/warning, qualsiasi pagina) ===== */
.iot-toast-stack {
  position: fixed; top: var(--sp-5); right: var(--sp-5); z-index: 2000;
  display: flex; flex-direction: column; gap: var(--sp-3);
  max-width: 360px;
}
.iot-toast {
  display: flex; align-items: flex-start; gap: var(--sp-3);
  padding: var(--sp-4); border-radius: var(--r-md); box-shadow: var(--shadow-lg);
  border-left: 4px solid transparent;
  animation: iotToastIn 0.3s var(--ease);
  background: var(--surface);
}
.iot-toast-out { animation: iotToastOut 0.3s var(--ease) forwards; }
@keyframes iotToastIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes iotToastOut { to { opacity: 0; transform: translateX(20px); } }
.iot-toast-urgente { background: var(--danger-bg); border-left-color: var(--danger); color: var(--danger); }
.iot-toast-alta { background: var(--warning-bg); border-left-color: var(--warning); color: #b45309; }
.iot-toast-successo { background: var(--success-bg); border-left-color: var(--success); color: #16a34a; }
.iot-toast-body { flex: 1; min-width: 0; }
.iot-toast-titolo { font-weight: 700; font-size: var(--fs-sm); }
.iot-toast-luogo { font-size: var(--fs-sm); margin-top: 2px; color: inherit; opacity: 0.9; }
.iot-toast-luogo a { color: inherit; text-decoration: underline; font-weight: 600; }
.iot-toast-close {
  background: none; border: none; cursor: pointer; color: inherit; opacity: 0.6;
  font-size: 18px; line-height: 1; padding: 0 2px; flex-shrink: 0;
}
.iot-toast-close:hover { opacity: 1; }

/* ===== SEARCHABLE SELECT (combobox cercabile, vedi searchable-select.js) ===== */
.searchable-select { position: relative; width: 100%; }
.searchable-select-native { display: none; }
/* position:fixed, non absolute: il widget vive spesso dentro una tabella
   con overflow:auto (.table-wrap) — con absolute il dropdown verrebbe
   tagliato da quell'overflow invece di comparire sopra al resto della
   pagina. Coordinate (left/top/width) impostate via JS in apri()/
   posizionaDropdown(), agganciato a <body> non al wrapper. */
.searchable-select-dropdown {
  display: none; position: fixed; z-index: 1000;
  max-height: 220px; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
}
.searchable-select-item {
  padding: var(--sp-2) var(--sp-3); font-size: var(--fs-sm); line-height: 1.45;
  cursor: pointer; white-space: normal; /* le opzioni con dettaglio occupanti vanno a capo, non tagliate */
  border-bottom: 1px solid var(--border-light);
}
.searchable-select-item:last-child { border-bottom: none; }
.searchable-select-item:hover { background: var(--bg); }
.searchable-select-empty { padding: var(--sp-2) var(--sp-3); font-size: var(--fs-sm); color: var(--text-muted); }

/* ===== PAGE HEADER ===== */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--sp-8);
}
.page-header h1 {
  font-size: var(--fs-xl);
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.3px;
}
.page-header p {
  font-size: var(--fs-base);
  color: var(--text-muted);
  margin-top: var(--sp-1);
}

/* ===== DIVIDER ===== */
.divider {
  border: none;
  border-top: 1px solid var(--border-light);
  margin: var(--sp-5) 0;
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--primary-light); border-radius: var(--r-full); }
::-webkit-scrollbar-thumb:hover { background: var(--primary); }

/* ===== HAMBURGER MENU ===== */
.menu-toggle {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  flex-direction: column;
  gap: 5px;
  padding: var(--sp-1);
}
.menu-toggle span {
  display: block;
  width: 22px; height: 2px;
  background: var(--text);
  border-radius: 2px;
  transition: var(--duration) var(--ease);
}

/* ===== LOGIN PAGE ===== */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--primary-subtle) 0%, #e8eef3 40%, var(--bg) 100%);
  padding: var(--sp-6);
  position: relative;
  overflow: hidden;
}
.login-page::before {
  content: '';
  position: absolute;
  top: -10%; left: -10%;
  width: 500px; height: 500px;
  border-radius: 50%;
  background: rgba(56, 136, 101, 0.05);
}
.login-page::after {
  content: '';
  position: absolute;
  bottom: -15%; right: -5%;
  width: 400px; height: 400px;
  border-radius: 50%;
  background: rgba(255, 222, 0, 0.05);
}
.login-card {
  background: var(--surface);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-xl);
  padding: 48px 44px;
  width: 100%;
  max-width: 420px;
  position: relative;
  z-index: 1;
  border: 1px solid var(--border-light);
}
.login-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  margin-bottom: var(--sp-8);
}
.login-logo-img {
  height: 72px;
  width: auto;
  object-fit: contain;
}
.login-title {
  font-size: var(--fs-xl);
  font-weight: 700;
  color: var(--text);
  margin-bottom: var(--sp-2);
}
.login-subtitle {
  font-size: var(--fs-base);
  color: var(--text-muted);
  margin-bottom: var(--sp-8);
}
.login-card .btn-primary {
  width: 100%;
  padding: 13px;
  font-size: 15px;
  border-radius: var(--r-md);
  margin-top: var(--sp-1);
}
.login-footer {
  text-align: center;
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: var(--sp-6);
}
.login-footer a { color: var(--primary-dark); font-weight: 600; }
.login-footer a:hover { text-decoration: underline; }

/* ===== CONTENT ANIMATIONS ===== */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}
.animate-in {
  animation: fadeInUp 0.4s var(--ease) both;
}
.animate-in:nth-child(1) { animation-delay: 0.05s; }
.animate-in:nth-child(2) { animation-delay: 0.1s; }
.animate-in:nth-child(3) { animation-delay: 0.15s; }
.animate-in:nth-child(4) { animation-delay: 0.2s; }

/* ===== PAGE HEADER ===== */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--sp-6);
  gap: var(--sp-4);
}
.page-header h1 {
  font-size: var(--fs-2xl);
  font-weight: 800;
  letter-spacing: -0.5px;
  line-height: 1.2;
}
.page-header p {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  margin-top: var(--sp-1);
}

/* ===== BADGES ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: var(--r-full);
  font-size: var(--fs-xs);
  font-weight: 700;
  line-height: 1.6;
  letter-spacing: 0.3px;
  white-space: nowrap;
}
.badge-green  { background: var(--success-bg); color: #16a34a; }
.badge-red    { background: var(--danger-bg);  color: var(--danger); }
.badge-yellow { background: var(--warning-bg); color: #b45309; }
.badge-blue   { background: var(--info-bg);    color: var(--info); }
.badge-gray   { background: #f1f5f9;           color: var(--text-secondary); box-shadow: inset 0 0 0 1px #cbd5e1; }

/* ===== FORMS ===== */
.form-group {
  margin-bottom: var(--sp-4);
  flex: 1;
}
.form-label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: var(--sp-2);
}
.form-control {
  width: 100%;
  padding: 11px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
  outline: none;
}
.form-control:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 136, 101, 0.12);
}
.form-control::placeholder {
  color: var(--text-muted);
  font-weight: 400;
}
.form-row {
  display: flex;
  gap: var(--sp-4);
}
.form-row .form-group {
  flex: 1;
}

/* ===== DIVIDER ===== */
.divider, hr.divider {
  border: none;
  height: 1px;
  background: var(--border-light);
  margin: var(--sp-6) 0;
}

/* ===== BUTTON VARIANTS ===== */
.btn-accent {
  background: var(--accent);
  color: var(--text-on-accent);
  border: none;
  font-weight: 700;
}
.btn-accent:hover {
  background: var(--accent-dark);
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
}
.btn-danger {
  background: var(--danger);
  color: white;
  border: none;
}
.btn-danger:hover {
  background: #dc2626;
  transform: translateY(-1px);
}
.btn-lg {
  padding: 13px 28px;
  font-size: var(--fs-md);
}

/* ===== CONFIRM MODAL (global) ===== */
.confirm-overlay {
  display: none; position: fixed; inset: 0; z-index: 9999;
  background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
  align-items: center; justify-content: center;
}
.confirm-overlay.active { display: flex; }
.confirm-box {
  background: var(--surface); border-radius: var(--r-xl);
  box-shadow: var(--shadow-xl); padding: var(--sp-8);
  width: 100%; max-width: 420px; text-align: center;
  animation: confirmIn .2s var(--ease);
}
@keyframes confirmIn { from { opacity:0; transform: scale(.92) translateY(10px); } to { opacity:1; transform: scale(1) translateY(0); } }
.confirm-icon {
  width: 56px; height: 56px; border-radius: var(--r-full);
  background: var(--danger-bg); color: var(--danger);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto var(--sp-4);
}
.confirm-title {
  font-size: var(--fs-lg); font-weight: 700;
  color: var(--text-primary); margin-bottom: var(--sp-2);
}
.confirm-message {
  font-size: var(--fs-sm); color: var(--text-muted);
  line-height: 1.5; margin-bottom: var(--sp-6);
}
.confirm-actions {
  display: flex; gap: var(--sp-3); justify-content: center;
}
.confirm-actions .btn { min-width: 120px; }

/* ===== IMPORT MODAL ===== */
.import-overlay {
  display: none; position: fixed; inset: 0; z-index: 600;
  background: rgba(0,0,0,0.5); backdrop-filter: blur(4px);
  align-items: center; justify-content: center;
}
.import-overlay.active { display: flex; }
.import-box {
  background: var(--surface); border-radius: var(--r-xl);
  box-shadow: var(--shadow-xl); padding: var(--sp-8);
  width: 100%; max-width: 900px; max-height: 85vh;
  display: flex; flex-direction: column;
  animation: confirmIn .25s var(--ease);
  overflow: hidden;
}
.import-title {
  font-size: var(--fs-lg); font-weight: 700; margin-bottom: var(--sp-6);
  display: flex; align-items: center; gap: var(--sp-3);
}
/* Step indicator */
.import-steps {
  display: flex; align-items: center; gap: var(--sp-2);
  margin-bottom: var(--sp-6);
}
.import-step {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-4); border-radius: var(--r-full);
  font-size: var(--fs-sm); font-weight: 600;
  background: var(--bg); color: var(--text-muted);
  border: 1.5px solid var(--border-light);
  transition: all var(--duration) var(--ease);
}
.import-step.active {
  background: var(--primary-subtle); color: var(--primary-dark);
  border-color: var(--primary);
}
.import-step.done {
  background: var(--success-bg); color: #16a34a;
  border-color: var(--success);
}
.import-step-num {
  width: 22px; height: 22px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  background: var(--border); color: white;
}
.import-step.active .import-step-num { background: var(--primary); }
.import-step.done .import-step-num { background: var(--success); }
.import-step-sep {
  width: 24px; height: 2px; background: var(--border-light);
  border-radius: 1px;
}
/* Drop zone */
.drop-zone {
  border: 2.5px dashed var(--border);
  border-radius: var(--r-lg);
  padding: var(--sp-10) var(--sp-6);
  text-align: center; cursor: pointer;
  transition: all var(--duration) var(--ease);
  background: var(--bg);
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--primary); background: var(--primary-subtle);
}
.drop-zone-icon {
  width: 56px; height: 56px; border-radius: var(--r-full);
  background: var(--primary-subtle); color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto var(--sp-4);
}
.drop-zone-text { font-weight: 600; color: var(--text-primary); margin-bottom: var(--sp-1); }
.drop-zone-hint { font-size: var(--fs-sm); color: var(--text-muted); }
.drop-zone-link { color: var(--primary); font-weight: 600; cursor: pointer; text-decoration: underline; }
.drop-zone input[type="file"] { display: none; }
/* Preview */
.import-step-content { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.import-body { flex: 1; overflow: auto; margin-bottom: var(--sp-5); min-height: 0; }
.preview-stats {
  display: flex; gap: var(--sp-4); margin-bottom: var(--sp-4);
  flex-wrap: wrap;
}
.preview-stat {
  padding: var(--sp-2) var(--sp-4); border-radius: var(--r-md);
  font-size: var(--fs-sm); font-weight: 600;
}
.preview-stat.ok { background: var(--success-bg); color: #16a34a; }
.preview-stat.err { background: var(--danger-bg); color: var(--danger); }
.preview-stat.dup { background: var(--warning-bg); color: #b45309; }
.preview-table {
  width: 100%; font-size: var(--fs-sm);
  border-collapse: collapse;
}
.preview-table th {
  text-align: left; padding: var(--sp-2) var(--sp-3);
  background: var(--bg); font-weight: 600; font-size: var(--fs-xs);
  text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted);
  border-bottom: 1.5px solid var(--border);
  position: sticky; top: 0;
}
.preview-table td {
  padding: var(--sp-2) var(--sp-3);
  border-bottom: 1px solid var(--border-light);
  max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.preview-table td:last-child {
  max-width: none; white-space: normal; word-break: break-word;
  overflow: visible; text-overflow: unset;
}
.preview-table .row-error { background: #fef2f2; }
.preview-table .row-error td { color: var(--danger); }
.preview-table .row-dup { background: #fffbeb; }
.preview-table .row-dup td { color: #b45309; }
.error-msg {
  font-size: var(--fs-xs); color: var(--danger);
  font-style: italic; max-width: 200px;
  white-space: normal;
}
/* Result */
.import-result {
  text-align: center; padding: var(--sp-8) var(--sp-4);
}
.import-result-icon {
  width: 64px; height: 64px; border-radius: var(--r-full);
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto var(--sp-4);
}
.import-result-icon.success { background: var(--success-bg); color: #16a34a; }
.import-result-icon.partial { background: var(--warning-bg); color: #b45309; }
.import-result-title { font-size: var(--fs-lg); font-weight: 700; margin-bottom: var(--sp-2); }
.import-result-detail { font-size: var(--fs-sm); color: var(--text-muted); }
.import-result-reminder {
  display: flex; align-items: center; gap: var(--sp-2); justify-content: center;
  margin-top: var(--sp-4); padding: var(--sp-3) var(--sp-4);
  background: var(--info-bg); color: var(--text-secondary);
  border-radius: var(--r-md); font-size: var(--fs-sm); text-align: left;
}
/* Actions */
.import-actions {
  display: flex; justify-content: flex-end; gap: var(--sp-3);
  padding-top: var(--sp-4); border-top: 1px solid var(--border-light);
  flex-shrink: 0;
}
/* Bulk Action Bar */
.bulk-bar {
  display: none; align-items: center; gap: var(--sp-4);
  padding: var(--sp-3) var(--sp-5);
  background: var(--surface); border: 1.5px solid var(--primary);
  border-radius: var(--r-lg); margin: 0 var(--sp-5) var(--sp-4);
  box-shadow: var(--shadow-md);
  animation: confirmIn .2s var(--ease);
}
.bulk-bar.active { display: flex; }
.bulk-bar span {
  font-weight: 600; font-size: var(--fs-sm); color: var(--primary-dark);
}
/* Button group */
.btn-group {
  display: flex; gap: var(--sp-2); align-items: center;
}

/* ===== END DRAWER ===== */
.drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 600;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity var(--duration-slow) var(--ease);
}
.drawer-overlay.active {
  display: block;
  opacity: 1;
}
.end-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: 540px;
  max-width: 92vw;
  height: 100vh;
  background: var(--surface);
  z-index: 650;
  box-shadow: -8px 0 40px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--duration-slow) var(--ease);
}
.end-drawer.active {
  transform: translateX(0);
}
.end-drawer form {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--sp-5) var(--sp-6);
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--primary-subtle), var(--bg));
}
.drawer-title {
  font-size: var(--fs-lg);
  font-weight: 800;
  letter-spacing: -0.3px;
}
.drawer-close {
  width: 36px; height: 36px;
  border-radius: var(--r-md);
  border: 1.5px solid var(--border);
  background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all var(--duration) var(--ease);
}
.drawer-close:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-bg);
}
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-6);
}
.drawer-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-3);
  padding: var(--sp-4) var(--sp-6);
  border-top: 1px solid var(--border-light);
  flex-shrink: 0;
  background: var(--bg);
}

/* Drawer section titles */
.drawer-section-title {
  font-size: var(--fs-md);
  font-weight: 700;
  color: var(--primary-dark);
  margin-bottom: var(--sp-4);
  margin-top: var(--sp-6);
  display: flex;
  align-items: center;
  gap: var(--sp-2);
}
.drawer-section-title:first-child {
  margin-top: 0;
}

/* ===== FORM SELECT (global) ===== */
.form-select {
  width: 100%;
  padding: 11px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
  outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%234A6070' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}
.form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(56, 136, 101, 0.12);
}
textarea.form-control {
  resize: vertical;
  min-height: 80px;
}


/* ===== RESPONSIVE ===== */
@media (max-width: 1024px) {
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  :root { --sidebar-w: 0px; }
  .sidebar { transform: translateX(-260px); width: 260px; }
  .sidebar.open { transform: translateX(0); }
  .topbar { left: 0; }
  .content-area { margin-left: 0; padding: var(--sp-5) var(--sp-4); }
  .form-row { flex-direction: column; gap: 0; }
  .menu-toggle { display: flex !important; }
  .welcome-banner { padding: var(--sp-6); }
  .welcome-title { font-size: var(--fs-lg); }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .quick-actions { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .login-card { padding: 32px 24px; }
}

/* ===== MODAL OVERLAY (globale — riusato da attivita, dashboard, ecc.) ===== */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.6);
  backdrop-filter: blur(4px);
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: opacity .3s ease, visibility .3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay.active { opacity: 1; visibility: visible; }
.modal-box {
  background: var(--surface);
  width: 90%;
  max-width: 480px;
  border-radius: var(--r-xl);
  padding: var(--sp-6);
  box-shadow: var(--shadow-xl);
}
.modal-title {
  font-size: var(--fs-lg);
  font-weight: 700;
  margin-bottom: var(--sp-5);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--sp-3);
  margin-top: var(--sp-5);
}
