:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --text: #0f172a;
  --text-muted: #475569;
  --border: #cbd5e1;
  --primary: #1e3a8a;
  --primary-light: #2563eb;
  --primary-soft: #dbeafe;
  --success: #15803d;
  --success-soft: #dcfce7;
  --warning: #b45309;
  --warning-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --purple: #6d28d9;
  --purple-soft: #ede9fe;
  --radius: 10px;
  --shadow: 0 2px 8px rgba(15, 23, 42, 0.07);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background-color: var(--bg);
  color: var(--text);
  line-height: 1.45;
  min-height: 100vh;
}

/* Header Istituzionale */
.topbar {
  background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
  color: #fff;
  padding: 0.85rem 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  position: sticky;
  top: 0;
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-icon {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.14);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.2rem;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.brand h1 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.brand p {
  font-size: 0.78rem;
  color: #cbd5e1;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  flex-wrap: wrap;
}

.lan-pill {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.22);
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  font-size: 0.76rem;
  color: #e2e8f0;
}

.user-switcher {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(255, 255, 255, 0.15);
  padding: 0.35rem 0.65rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.user-switcher label {
  font-size: 0.75rem;
  color: #e2e8f0;
  font-weight: 600;
}

.user-switcher select {
  background: #ffffff;
  color: #0f172a;
  border: none;
  border-radius: 5px;
  padding: 0.3rem 0.55rem;
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
}

/* Navigazione principale */
.main-nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.nav-tabs {
  display: flex;
  gap: 0.25rem;
  overflow-x: auto;
}

.nav-tab {
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  padding: 0.85rem 1.1rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  transition: all 0.15s;
}

.nav-tab:hover {
  color: var(--primary);
  background: var(--surface-alt);
}

.nav-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary-light);
  background: #f8fafc;
}

.badge-count {
  background: var(--danger);
  color: #fff;
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  font-weight: 700;
}

/* Container */
.container {
  max-width: 1440px;
  margin: 1.25rem auto;
  padding: 0 1.25rem 3rem;
}

/* KPI Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 0.9rem;
  margin-bottom: 1.25rem;
}

.kpi-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-left: 5px solid var(--primary-light);
}

.kpi-card.alert-kpi {
  border-left-color: var(--danger);
  background: #fffafb;
}

.kpi-card.warn-kpi {
  border-left-color: #f59e0b;
}

.kpi-card.purple-kpi {
  border-left-color: var(--purple);
}

.kpi-card.success-kpi {
  border-left-color: var(--success);
}

.kpi-title {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-muted);
  font-weight: 700;
}

.kpi-value {
  font-size: 1.75rem;
  font-weight: 800;
  margin: 0.2rem 0;
  color: var(--text);
}

.kpi-sub {
  font-size: 0.76rem;
  color: var(--text-muted);
}

/* Alert Box 5-7 Giorni */
.alerts-panel {
  background: var(--surface);
  border: 1px solid #fca5a5;
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.alerts-header {
  background: linear-gradient(90deg, #991b1b, #dc2626);
  color: #fff;
  padding: 0.7rem 1.15rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 700;
  font-size: 0.92rem;
}

.alerts-list {
  padding: 0.85rem 1.15rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 0.75rem;
}

.alert-item {
  display: flex;
   flex-direction: column;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.75rem 0.95rem;
  border-radius: 8px;
  border: 1px solid #fecaca;
  background: #fef2f2;
  cursor: pointer;
  transition: transform 0.12s;
}

.alert-item:hover {
  transform: translateY(-1px);
  box-shadow: 0 3px 8px rgba(185, 28, 28, 0.12);
}

.alert-item.prealert {
  background: #fffbeb;
  border-color: #fde68a;
}

.alert-item-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.alert-tag {
  font-size: 0.72rem;
  font-weight: 800;
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  text-transform: uppercase;
}

.alert-tag.critical {
  background: #dc2626;
  color: #fff;
}

.alert-tag.warning {
  background: #d97706;
  color: #fff;
}

.modality-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
  background: #e0e7ff;
  color: #1e40af;
  border: 1px solid #bfdbfe;
}

/* Card & Form Layout */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.2rem;
  margin-bottom: 1.2rem;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding-bottom: 0.85rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--primary);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.step-pill {
  background: var(--primary);
  color: #fff;
  font-size: 0.73rem;
  font-weight: 700;
  padding: 0.15rem 0.5rem;
  border-radius: 5px;
}

/* Filters bar */
.filters-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  margin-bottom: 1rem;
  align-items: center;
}

.filters-bar input,
.filters-bar select {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.86rem;
  background: #fff;
}

/* Table */
.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th, td {
  padding: 0.7rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid #e2e8f0;
  vertical-align: top;
}

th {
  background: #f8fafc;
  color: #334155;
  font-weight: 700;
  font-size: 0.77rem;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

tr:hover td {
  background: #f8fafc;
}

/* Badges */
.chip {
  display: inline-block;
  padding: 0.18rem 0.5rem;
  border-radius: 5px;
  font-size: 0.73rem;
  font-weight: 700;
  margin: 0.1rem 0.15rem 0.1rem 0;
}

.chip-ente {
  background: #1e3a8a;
  color: #fff;
}

.chip-ufficio {
  background: #f1f5f9;
  color: #334155;
  border: 1px solid #cbd5e1;
}

.chip-ufficio.highlight {
  background: #dbeafe;
  color: #1e40af;
  border-color: #93c5fd;
}

.status-badge {
  display: inline-block;
  padding: 0.22rem 0.55rem;
  border-radius: 6px;
  font-size: 0.74rem;
  font-weight: 700;
}

.status-ok {
  background: var(--success-soft);
  color: var(--success);
}

.status-warn {
  background: var(--warning-soft);
  color: var(--warning);
}

.status-danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.status-purple {
  background: var(--purple-soft);
  color: var(--purple);
}

.status-info {
  background: var(--primary-soft);
  color: var(--primary);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-primary {
  background: var(--primary-light);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary);
}

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-outline {
  background: #fff;
  color: var(--text);
  border-color: var(--border);
}

.btn-outline:hover {
  background: var(--surface-alt);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-sm {
  padding: 0.32rem 0.65rem;
  font-size: 0.78rem;
}

/* Forms */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 0.9rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.form-group.full-width {
  grid-column: 1 / -1;
}

.form-group label {
  font-size: 0.8rem;
  font-weight: 700;
  color: #334155;
}

.form-group input,
.form-group select,
.form-group textarea {
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.88rem;
  font-family: inherit;
  background: #fff;
}

.form-group textarea {
  min-height: 72px;
  resize: vertical;
}

.checkbox-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding: 0.5rem 0;
}

.checkbox-pill {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
}

.checkbox-pill input {
  width: 16px;
  height: 16px;
}

/* Modal / Scheda Istruttoria Completa (Punti 1-10B) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.6);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 1.5rem 1rem;
  overflow-y: auto;
  z-index: 200;
}

.modal-backdrop.open {
  display: flex;
}

.modal-sheet {
  background: var(--bg);
  width: 100%;
  max-width: 1220px;
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  overflow: hidden;
  border: 1px solid var(--border);
}

.modal-topbar {
  background: #0f172a;
  color: #fff;
  padding: 1rem 1.4rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.modal-body {
  padding: 1.25rem;
}

/* Stepper Barra 1 -> 10B */
.workflow-stepper {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-bottom: 1.2rem;
  background: #fff;
  padding: 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.wf-step {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 0.35rem 0.6rem;
  border-radius: 6px;
  background: #f1f5f9;
  color: #475569;
  border: 1px solid #e2e8f0;
  cursor: pointer;
}

.wf-step.done {
  background: #dcfce7;
  color: #166534;
  border-color: #86efac;
}

.wf-step.active-step {
  background: #dbeafe;
  color: #1e40af;
  border-color: #93c5fd;
}

.sublist-box {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.85rem;
  margin-top: 0.75rem;
}

.sublist-item {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 0.7rem;
  margin-bottom: 0.55rem;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.two-col {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 1rem;
}

/* Punto 3: Selezione fascicolo e creazione nuovo mutuamente esclusivi */
.radio-group-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-bottom: 0.85rem;
}

.radio-pill {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.radio-pill:hover {
  background: #e2e8f0;
}

.radio-pill.active,
.radio-pill:has(input:checked) {
  background: #dbeafe;
  border-color: #3b82f6;
  color: #1e40af;
}

.radio-pill input[type="radio"] {
  accent-color: var(--primary-light);
  width: 16px;
  height: 16px;
}

/* Punto 4: Destinatari per Area (Gervino: Area Tecnica, Maradei: Area Agraria, Cricenti: Area Legale) con nota associata */
.destinatari-aree-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1rem;
  margin: 0.85rem 0;
}

.area-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.04);
  transition: all 0.15s ease;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.area-card.active-tecnica {
  border-color: #ea580c;
  background: #fffbf7;
  border-left: 5px solid #ea580c;
}

.area-card.active-agraria {
  border-color: #16a34a;
  background: #f7fdf9;
  border-left: 5px solid #16a34a;
}

.area-card.active-legale {
  border-color: #7c3aed;
  background: #faf7ff;
  border-left: 5px solid #7c3aed;
}

.area-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  padding-bottom: 0.6rem;
  margin-bottom: 0.6rem;
  border-bottom: 1px solid #e2e8f0;
}

.area-card-title {
  font-size: 0.92rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.area-note-box {
  margin-top: 0.5rem;
}

.area-note-box label {
  font-size: 0.77rem;
  font-weight: 700;
  color: var(--text-muted);
  display: block;
  margin-bottom: 0.25rem;
}

.area-note-box input {
  width: 100%;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.84rem;
}

.area-note-chip {
  display: block;
  font-size: 0.73rem;
  background: #f8fafc;
  border-left: 3px solid var(--primary-light);
  padding: 0.2rem 0.45rem;
  border-radius: 3px;
  margin-top: 0.25rem;
  color: #334155;
  line-height: 1.3;
}
.area-note-chip.tecnica {
  border-left-color: #ea580c;
  background: #fff7ed;
}
.area-note-chip.agraria {
  border-left-color: #16a34a;
  background: #f0fdf4;
}
.area-note-chip.legale {
  border-left-color: #7c3aed;
  background: #faf5ff;
}

/* Menu a 3 puntini (10A e 10B) */
.dots-menu {
  position: relative;
  display: inline-block;
}

.dots-btn {
  background: #fff;
  border: 1px solid #cbd5e1;
  color: #334155;
  border-radius: 6px;
  padding: 0.15rem 0.5rem;
  font-size: 1rem;
  font-weight: 800;
  line-height: 1.1;
  cursor: pointer;
  transition: all 0.15s ease;
}

.dots-btn:hover {
  background: #f1f5f9;
  border-color: #94a3b8;
  color: #0f172a;
}

.dots-dropdown {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  min-width: 220px;
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.18);
  z-index: 200;
  padding: 0.35rem;
}

.dots-menu.open .dots-dropdown {
  display: block;
}

.dots-dropdown-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  padding: 0.48rem 0.65rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: #1e293b;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  white-space: nowrap;
}

.dots-dropdown-item:hover {
  background: #f1f5f9;
}

.dots-dropdown-item.item-primary {
  color: #1d4ed8;
}
.dots-dropdown-item.item-primary:hover {
  background: #eff6ff;
}

.dots-dropdown-item.item-success {
  color: #15803d;
}
.dots-dropdown-item.item-success:hover {
  background: #f0fdf4;
}

.dots-dropdown-item.item-danger {
  color: #dc2626;
}
.dots-dropdown-item.item-danger:hover {
  background: #fef2f2;
}

.dots-divider {
  height: 1px;
  background: #e2e8f0;
  margin: 0.25rem 0;
}

@media print {
  .topbar, .main-nav, .no-print {
    display: none !important;
  }
  .modal-backdrop {
    position: static;
    background: #fff;
    padding: 0;
  }
  .modal-sheet {
    box-shadow: none;
    border: none;
  }
}
