/* ===== VARIABILI ===== */
:root {
  --primary:      #2d7dc8;
  --primary-dark: #0a2a4a;
  --primary-light:#dbeafe;
  --bg:           #f1f5f9;
  --surface:      #ffffff;
  --border:       #e2e8f0;
  --text:         #1e293b;
  --text-muted:   #64748b;
  --text-faint:   #94a3b8;
  --secco:        #16a34a;
  --secco-bg:     #dcfce7;
  --allerta:      #d97706;
  --allerta-bg:   #fef3c7;
  --piena:        #dc2626;
  --piena-bg:     #fee2e2;
  --shadow-sm:    0 1px 3px rgba(0,0,0,0.06);
  --shadow-md:    0 4px 12px rgba(0,0,0,0.08);
  --radius:       10px;
}

/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { font-family: 'Segoe UI', system-ui, sans-serif;
             background: var(--bg); color: var(--text); min-height: 100vh; }
a { text-decoration: none; color: inherit; }
button { font-family: inherit; }

/* ===== NAVBAR ===== */
.navbar {
  height: 54px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  padding: 0 20px;
  gap: 8px;
  position: sticky; top: 0; z-index: 100;
  box-shadow: var(--shadow-sm);
}

.nav-logo { display: flex; align-items: center; gap: 9px; }
.nav-logo svg { width: 28px; height: 28px; flex-shrink: 0; }
.nav-logo-text { font-size: 0.95rem; font-weight: 700;
                 color: var(--primary-dark); letter-spacing: 0.04em;
                 white-space: nowrap; }
.nav-logo-text em { color: var(--primary); font-style: normal; }

.nav-center { display: flex; align-items: center; justify-content: center; }
.online-pill { display: flex; align-items: center; gap: 5px;
               font-size: 0.78rem; color: var(--text-muted); }
.online-pill i { font-size: 0.65rem; }
.dot-on  { color: var(--secco); }
.dot-off { color: var(--piena); }

.nav-right { display: flex; align-items: center; gap: 4px; justify-content: flex-end; }
.nav-link { display: flex; align-items: center; gap: 6px;
            color: var(--text-muted); font-size: 0.83rem; font-weight: 500;
            padding: 6px 11px; border-radius: 7px;
            transition: background 0.15s, color 0.15s;
            white-space: nowrap; }
.nav-link i { font-size: 0.8rem; }
.nav-link:hover  { background: var(--bg); color: var(--primary); }
.nav-link.active { background: var(--primary-light); color: var(--primary); font-weight: 600; }

/* Hamburger (mobile) */
.nav-toggle { display: none; background: none; border: none;
              cursor: pointer; font-size: 1.1rem; color: var(--text-muted);
              padding: 6px; border-radius: 6px; }
.nav-toggle:hover { background: var(--bg); }

/* ===== PAGINA ===== */
.page { padding: 24px 28px; }
.page-title { font-size: 1.05rem; font-weight: 700; color: var(--primary-dark);
              margin-bottom: 20px; display: flex; align-items: center; gap: 8px; }
.page-title i { color: var(--primary); }

/* ===== STAT CARDS ===== */
.stats-grid { display: grid;
              grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
              gap: 12px; margin-bottom: 24px; }
.sc { background: var(--surface); border-radius: var(--radius);
      border: 1px solid var(--border); padding: 16px 18px;
      box-shadow: var(--shadow-sm); }
.sc .sl { font-size: 0.68rem; color: var(--text-faint); text-transform: uppercase;
          letter-spacing: 0.08em; margin-bottom: 6px;
          display: flex; align-items: center; gap: 5px; }
.sc .sv { font-size: 1.7rem; font-weight: 800; color: var(--text); line-height: 1; }
.sc .sv small { font-size: 0.8rem; color: var(--text-faint); }
.sc .ss { font-size: 0.72rem; color: var(--text-faint); margin-top: 3px; }

/* ===== BADGES ===== */
.badge { display: inline-flex; align-items: center;
         padding: 2px 10px; border-radius: 20px;
         font-size: 0.72rem; font-weight: 700; }
.b-secco   { background: var(--secco-bg);   color: #166534; }
.b-allerta { background: var(--allerta-bg); color: #92400e; }
.b-piena   { background: var(--piena-bg);   color: #991b1b; }
.b-nd      { background: var(--bg);         color: var(--text-muted); }
.b-ok      { background: var(--secco-bg);   color: #166534; }
.b-err     { background: var(--piena-bg);   color: #991b1b; }

/* ===== CARD GENERICA ===== */
.card-box { background: var(--surface); border-radius: var(--radius);
            border: 1px solid var(--border); box-shadow: var(--shadow-sm); }

/* ===== TABELLA ===== */
.table-wrap { background: var(--surface); border-radius: var(--radius);
              border: 1px solid var(--border); overflow: hidden;
              box-shadow: var(--shadow-sm); }
.table-head { padding: 14px 20px; border-bottom: 1px solid var(--bg);
              display: flex; align-items: center; justify-content: space-between; }
.table-head h2 { font-size: 0.85rem; font-weight: 600; color: var(--text);
                 display: flex; align-items: center; gap: 6px; }
.table-head h2 i { color: var(--primary); }
.table-count { font-size: 0.75rem; color: var(--text-faint); }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 600px; }
th { background: #f8fafc; padding: 10px 16px; text-align: left;
     font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase;
     letter-spacing: 0.05em; border-bottom: 1px solid var(--bg);
     white-space: nowrap; }
td { padding: 9px 16px; font-size: 0.83rem;
     border-bottom: 1px solid #f8fafc; color: var(--text); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--bg); }

/* ===== PAGINAZIONE ===== */
.pagination { display: flex; align-items: center; justify-content: space-between;
              padding: 12px 20px; border-top: 1px solid var(--bg); flex-wrap: wrap; gap: 8px; }
.pagination-info { font-size: 0.78rem; color: var(--text-faint); }
.pagination-btns { display: flex; gap: 4px; flex-wrap: wrap; }
.pg-btn { padding: 5px 10px; border: 1px solid var(--border);
          background: var(--surface); border-radius: 6px; cursor: pointer;
          font-size: 0.78rem; color: var(--text); transition: background 0.15s; }
.pg-btn:hover { background: var(--bg); }
.pg-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.pg-btn:disabled { opacity: 0.4; cursor: default; }

/* ===== DISTRIBUZIONE ===== */
.dist-row { display: flex; gap: 14px; margin-bottom: 24px; }
.dist-card { background: var(--surface); border-radius: var(--radius);
             border: 1px solid var(--border); padding: 18px 20px;
             box-shadow: var(--shadow-sm); }
.dist-title { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;
              letter-spacing: 0.06em; margin-bottom: 12px;
              display: flex; align-items: center; gap: 6px; }
.dist-title i { color: var(--primary); }
.dist-bars { display: flex; flex-direction: column; gap: 10px; }
.dist-item { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; }
.dist-label { width: 64px; font-weight: 600; font-size: 0.78rem; }
.dist-bar-wrap { flex: 1; background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.dist-bar-fill { height: 100%; border-radius: 4px; transition: width 0.5s; }
.dist-pct { width: 38px; text-align: right; color: var(--text-muted); font-size: 0.78rem; }
.c-secco  { color: var(--secco); }  .bg-secco  { background: var(--secco); }
.c-allerta{ color: var(--allerta); }.bg-allerta{ background: var(--allerta); }
.c-piena  { color: var(--piena); }  .bg-piena  { background: var(--piena); }

/* ===== GRAFICO ===== */
.chart-wrap { background: var(--surface); border-radius: var(--radius);
              border: 1px solid var(--border); padding: 20px;
              margin-bottom: 24px; box-shadow: var(--shadow-sm); flex: 1; }
.chart-title { font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;
               letter-spacing: 0.06em; margin-bottom: 14px;
               display: flex; align-items: center; gap: 6px; }
.chart-title i { color: var(--primary); }
.chart-svg { width: 100%; height: 180px; }

/* ===== NOTIFICHE — CANALI ===== */
.channels-grid { display: grid;
                 grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
                 gap: 14px; margin-bottom: 24px; }
.ch-card { background: var(--surface); border-radius: var(--radius);
           border: 1px solid var(--border); padding: 18px 20px;
           box-shadow: var(--shadow-sm); }
.ch-header { display: flex; align-items: center; margin-bottom: 12px; }
.ch-name { font-size: 0.85rem; font-weight: 700; color: var(--text);
           display: flex; align-items: center; gap: 8px; }
.ch-icon { width: 32px; height: 32px; border-radius: 8px;
           display: flex; align-items: center; justify-content: center; font-size: 0.9rem; }
.ic-tg  { background: #e0f2fe; color: #0369a1; }
.ic-sms { background: var(--secco-bg); color: #166534; }
.ic-em  { background: var(--allerta-bg); color: #92400e; }
.ic-vp  { background: #ede9fe; color: #6d28d9; }
.ch-stats { display: flex; gap: 14px; margin-bottom: 10px; }
.ch-stat .n { font-size: 1.4rem; font-weight: 800; color: var(--text); }
.ch-stat .l { font-size: 0.68rem; color: var(--text-faint); text-transform: uppercase; }
.ch-config { font-size: 0.75rem; color: var(--text-faint); margin-bottom: 12px; }
.btn-test { width: 100%; padding: 8px; border: 1px solid var(--border);
            background: var(--bg); border-radius: 7px; cursor: pointer;
            font-size: 0.8rem; color: var(--text); font-weight: 500;
            transition: background 0.15s; display: flex;
            align-items: center; justify-content: center; gap: 6px; }
.btn-test:hover { background: var(--border); }
.filters { display: flex; align-items: center; gap: 8px;
           margin-bottom: 14px; flex-wrap: wrap; }
.filter-label { font-size: 0.8rem; color: var(--text-muted); font-weight: 600; }
.filter-btn { padding: 5px 14px; border: 1px solid var(--border);
              background: var(--surface); border-radius: 20px;
              font-size: 0.78rem; color: var(--text); transition: all 0.15s; }
.filter-btn:hover { background: var(--bg); }
.filter-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.ch-badge { padding: 3px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; }
.cb-telegram { background: #e0f2fe; color: #0369a1; }
.cb-sms      { background: var(--secco-bg); color: #166534; }
.cb-email    { background: var(--allerta-bg); color: #92400e; }
.cb-voip     { background: #ede9fe; color: #6d28d9; }
.cb-default  { background: var(--bg); color: var(--text-muted); }
.empty { text-align: center; padding: 40px; color: var(--text-faint); font-size: 0.9rem; }
.empty i { display: block; font-size: 2rem; margin-bottom: 10px; }

/* ===== FORM / SETTINGS ===== */
.settings-grid { display: grid;
                 grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
                 gap: 16px; margin-bottom: 24px; }
.form-card { background: var(--surface); border-radius: var(--radius);
             border: 1px solid var(--border); padding: 22px 24px;
             box-shadow: var(--shadow-sm); }
.form-card h2 { font-size: 0.78rem; color: var(--primary); text-transform: uppercase;
                letter-spacing: 0.08em; margin-bottom: 16px;
                padding-bottom: 10px; border-bottom: 1px solid var(--border);
                display: flex; align-items: center; gap: 7px; }
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: 0.8rem; color: var(--text-muted);
               margin-bottom: 5px; font-weight: 500; }
.field input { width: 100%; padding: 9px 12px; background: var(--bg);
               border: 1px solid var(--border); border-radius: 7px;
               color: var(--text); font-size: 0.88rem;
               transition: border-color 0.15s; }
.field input:focus { outline: none; border-color: var(--primary);
                     background: var(--surface); }
.field .hint { font-size: 0.73rem; color: var(--text-faint); margin-top: 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.btn-save { background: var(--primary); color: #fff; border: none;
            padding: 11px 28px; border-radius: 8px; cursor: pointer;
            font-weight: 600; font-size: 0.92rem;
            transition: background 0.15s; display: flex; align-items: center; gap: 8px; }
.btn-save:hover { background: #1d6aaa; }
.btn-bar { padding: 0 0 28px; }

/* ===== ALERTS ===== */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 20px;
         font-size: 0.85rem; display: flex; align-items: center; gap: 8px; }
.alert-ok  { background: var(--secco-bg); color: #166534; border: 1px solid #bbf7d0; }
.alert-err { background: var(--piena-bg); color: #991b1b; border: 1px solid #fecaca; }

/* ===== BARRE PICCOLE ===== */
.bar-sm { height: 4px; background: var(--bg); border-radius: 2px;
          margin-top: 5px; width: 60px; overflow: hidden; }
.bar-sm-f { height: 100%; border-radius: 2px; }

/* ===== MOBILE RESPONSIVE ===== */

/* Tablet */
@media (max-width: 900px) {
  .dist-row { flex-direction: column; }
  .dist-card { max-width: 100% !important; }
  .stats-grid { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
  .settings-grid { grid-template-columns: 1fr; }
}

/* Mobile */
@media (max-width: 768px) {
  /* Navbar mobile */
  .navbar { grid-template-columns: 1fr auto; height: auto;
            padding: 10px 16px; flex-wrap: wrap; position: relative; }
  .nav-center { display: none; }
  .nav-right { display: none; flex-direction: column; align-items: stretch;
               position: absolute; top: 54px; left: 0; right: 0;
               background: var(--surface); border-bottom: 1px solid var(--border);
               padding: 8px 12px; gap: 2px; box-shadow: var(--shadow-md); z-index: 200; }
  .nav-right.open { display: flex; }
  .nav-link { padding: 10px 12px; border-radius: 8px; font-size: 0.9rem; }
  .nav-toggle { display: flex; align-items: center; justify-content: center; }

  /* Pagina */
  .page { padding: 16px; }
  .page-title { font-size: 0.95rem; }

  /* Stats */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .sc { padding: 14px; }
  .sc .sv { font-size: 1.4rem; }

  /* Tabella */
  .table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Channels */
  .channels-grid { grid-template-columns: 1fr; }

  /* Row2 forms */
  .row2 { grid-template-columns: 1fr; }

  /* Pagination */
  .pagination { flex-direction: column; align-items: flex-start; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .nav-logo-text { font-size: 0.82rem; }
}

/* ===== SEZIONI IMPOSTAZIONI ===== */
.section-header { grid-column: 1 / -1; margin: 8px 0 4px;
                  padding-bottom: 10px; border-bottom: 2px solid var(--primary-light); }
.section-header h3 { font-size: 0.9rem; font-weight: 700; color: var(--primary-dark);
                     display: flex; align-items: center; gap: 8px; }
.section-header h3 i { color: var(--primary); }
.section-header p { font-size: 0.78rem; color: var(--text-faint); margin-top: 3px; }

/* ===== CRONOLOGIA ===== */
.history-toolbar { display: flex; align-items: center; justify-content: space-between;
                   flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.history-toolbar-left { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.btn-danger { background: var(--piena); color: #fff; border: none;
              padding: 7px 14px; border-radius: 7px; cursor: pointer;
              font-size: 0.82rem; font-weight: 600;
              display: flex; align-items: center; gap: 6px;
              transition: background 0.15s; }
.btn-danger:hover { background: #b91c1c; }
.btn-danger:disabled { opacity: 0.4; cursor: default; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border);
                 padding: 7px 14px; border-radius: 7px; cursor: pointer;
                 font-size: 0.82rem; font-weight: 500;
                 display: flex; align-items: center; gap: 6px;
                 transition: background 0.15s; text-decoration: none; }
.btn-secondary:hover { background: var(--border); }
.btn-secondary:disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.history-count { font-size: 0.78rem; color: var(--text-faint); }

.history-grid { display: grid;
                grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
                gap: 14px; }
.h-card { background: var(--surface); border-radius: var(--radius);
          border: 1px solid var(--border); overflow: hidden;
          box-shadow: var(--shadow-sm); transition: box-shadow 0.15s; }
.h-card:hover { box-shadow: var(--shadow-md); }
.h-card.selected { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }
.h-card-img { position: relative; cursor: pointer; }
.h-card-img img { width: 100%; height: 160px; object-fit: cover; display: block; }
.h-card-check { position: absolute; top: 8px; left: 8px;
                width: 20px; height: 20px; cursor: pointer; accent-color: var(--primary); }
.h-card-stato { position: absolute; top: 8px; right: 8px;
                padding: 3px 10px; border-radius: 20px;
                font-size: 0.7rem; font-weight: 700; }
.h-card-body { padding: 10px 14px; }
.h-card-time { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 6px;
               display: flex; align-items: center; gap: 5px; }
.h-card-data { display: flex; gap: 10px; margin-bottom: 10px; }
.h-card-data span { font-size: 0.78rem; color: var(--text-muted);
                    display: flex; align-items: center; gap: 4px; }
.h-card-data strong { color: var(--text); }
.h-card-actions { display: flex; gap: 6px; }
.h-card-actions a, .h-card-actions button {
  flex: 1; padding: 6px; border-radius: 6px; font-size: 0.78rem;
  font-weight: 500; text-align: center; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 5px;
  text-decoration: none; transition: background 0.15s; }
.btn-dl { background: var(--primary-light); color: var(--primary);
          border: 1px solid #bfdbfe; }
.btn-dl:hover { background: #bfdbfe; }
.btn-del { background: var(--piena-bg); color: var(--piena);
           border: 1px solid #fecaca; }
.btn-del:hover { background: #fecaca; }

.history-empty { text-align: center; padding: 60px 20px; color: var(--text-faint); }
.history-empty i { font-size: 3rem; display: block; margin-bottom: 12px; }

/* Lightbox */
.lightbox { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.85);
            z-index: 1000; align-items: center; justify-content: center; }
.lightbox.open { display: flex; }
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }
.lightbox-close { position: absolute; top: 16px; right: 20px; color: #fff;
                  font-size: 1.8rem; cursor: pointer; background: none; border: none; }

@media (max-width: 768px) {
  .history-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
  .h-card-img img { height: 110px; }
}

/* ===== REGOLE NOTIFICHE ===== */
.rules-grid { display: flex; flex-direction: column; gap: 10px; margin-bottom: 24px; }
.rule-row { background: var(--surface); border: 1px solid var(--border);
            border-radius: var(--radius); padding: 14px 18px;
            display: flex; align-items: center; gap: 14px;
            box-shadow: var(--shadow-sm); }
.rule-row.disabled { opacity: 0.55; }
.rule-toggle { position: relative; width: 40px; height: 22px; flex-shrink: 0; }
.rule-toggle input { opacity: 0; width: 0; height: 0; }
.rule-slider { position: absolute; inset: 0; background: var(--border);
               border-radius: 22px; cursor: pointer; transition: background 0.2s; }
.rule-slider:before { content: ''; position: absolute; width: 16px; height: 16px;
                      left: 3px; top: 3px; background: #fff; border-radius: 50%;
                      transition: transform 0.2s; }
.rule-toggle input:checked + .rule-slider { background: var(--primary); }
.rule-toggle input:checked + .rule-slider:before { transform: translateX(18px); }
.rule-info { flex: 1; min-width: 0; }
.rule-name { font-size: 0.88rem; font-weight: 600; color: var(--text); }
.rule-desc { font-size: 0.75rem; color: var(--text-faint); margin-top: 2px; }
.rule-channels { display: flex; gap: 5px; flex-shrink: 0; }
.rule-ch { padding: 2px 8px; border-radius: 10px; font-size: 0.7rem; font-weight: 600; }
.rch-tg  { background: #e0f2fe; color: #0369a1; }
.rch-sms { background: var(--secco-bg); color: #166534; }
.rch-em  { background: var(--allerta-bg); color: #92400e; }
.rch-vp  { background: #ede9fe; color: #6d28d9; }
.rule-actions { display: flex; gap: 6px; flex-shrink: 0; }
.btn-icon { width: 30px; height: 30px; border-radius: 7px; border: 1px solid var(--border);
            background: var(--bg); cursor: pointer; display: flex;
            align-items: center; justify-content: center;
            font-size: 0.8rem; color: var(--text-muted);
            transition: background 0.15s; text-decoration: none; }
.btn-icon:hover { background: var(--border); }
.btn-icon.danger:hover { background: var(--piena-bg); color: var(--piena);
                         border-color: #fecaca; }

.rule-form { background: var(--surface); border: 1px solid var(--border);
             border-radius: var(--radius); padding: 22px 24px;
             box-shadow: var(--shadow-sm); margin-bottom: 24px; }
.rule-form h2 { font-size: 0.8rem; color: var(--primary); text-transform: uppercase;
                letter-spacing: 0.08em; margin-bottom: 16px;
                padding-bottom: 10px; border-bottom: 1px solid var(--border);
                display: flex; align-items: center; gap: 7px; }
.channels-check { display: flex; gap: 12px; flex-wrap: wrap; }
.ch-check { display: flex; align-items: center; gap: 6px;
            font-size: 0.83rem; cursor: pointer; }
.ch-check input { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; }
.event-types { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px,1fr)); gap: 8px; }
.event-type-btn { padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px;
                  background: var(--bg); cursor: pointer; font-size: 0.82rem;
                  color: var(--text); text-align: center; transition: all 0.15s; }
.event-type-btn:hover,
.event-type-btn.selected { background: var(--primary-light); border-color: var(--primary);
                           color: var(--primary); font-weight: 600; }

@media (max-width: 768px) {
  .rule-row { flex-wrap: wrap; }
  .rule-channels { order: 3; }
}