:root {
  --primary: #1e5c4f;
  --secondary: #a3e635;
  --bg-app: #f1f5f9;
  --bg-card: #ffffff;
  --bg-hover: #f8fafc;
  --nav-active-bg: #f1f5f9;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --text-faint: #cbd5e1;
  --border: #e2e8f0;
  --success: #10b981;
  --success-bg: #f0fdf4;
  --success-text: #166534;
  --success-strong: #16a34a;
  --danger: #dc2626;
  --danger-light: #ef4444;
  --danger-bg: #fef2f2;
  --danger-strong: #991b1b;
  --warning: #f59e0b;
  --warning-bg: #fef9c3;
  --warning-text: #854d0e;
  --info-bg: #eff6ff;
  --info-text: #1d4ed8;
  --purple-bg: #faf5ff;
  --purple-text: #6d28d9;
  --code-bg: #0f172a;
  --code-text: #e2e8f0;
  --radius-lg: 1.5rem;
  --radius-md: 0.75rem;
  --shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

/* Tema scuro "Sistema": si applica solo se l'utente non ha scelto esplicitamente "Chiaro" */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-app: #0f172a;
    --bg-card: #1e293b;
    --bg-hover: #334155;
    --nav-active-bg: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --text-faint: #475569;
    --border: #334155;
    --success-bg: #052e1c;
    --success-text: #4ade80;
    --danger-bg: #450a0a;
    --warning-bg: #422006;
    --warning-text: #fde047;
    --info-bg: #172554;
    --info-text: #93c5fd;
    --purple-bg: #2e1065;
    --purple-text: #c4b5fd;
  }
}

/* Tema scuro scelto esplicitamente dall'utente, indipendente dal sistema operativo */
:root[data-theme="dark"] {
  --bg-app: #0f172a;
  --bg-card: #1e293b;
  --bg-hover: #334155;
  --nav-active-bg: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --text-faint: #475569;
  --border: #334155;
  --success-bg: #052e1c;
  --success-text: #4ade80;
  --danger-bg: #450a0a;
  --warning-bg: #422006;
  --warning-text: #fde047;
  --info-bg: #172554;
  --info-text: #93c5fd;
  --purple-bg: #2e1065;
  --purple-text: #c4b5fd;
}

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

body {
  font-family: 'Inter', sans-serif;
  background-color: var(--bg-app);
  color: var(--text-primary);
  line-height: 1.5;
}

/* Layout */
.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 260px;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: 2rem;
  display: flex;
  flex-direction: column;
  transition: width 0.2s ease, padding 0.2s ease;
}

/* Concept: sidebar a scomparsa (solo desktop, vedi .sidebar-toggle / toggleSidebarCollapse) */
@media (min-width: 769px) {
  .sidebar.collapsed {
    width: 84px;
    min-width: 84px;
    padding: 2rem 0.9rem;
  }

  .sidebar.collapsed .sidebar-label {
    display: none;
  }

  .sidebar.collapsed #sidebar-header-row {
    flex-direction: column;
    gap: 0.75rem;
  }

  .sidebar.collapsed .flex.items-center.gap-3 {
    justify-content: center;
  }

  .sidebar.collapsed .nav-item {
    justify-content: center;
    gap: 0;
    padding: 0.75rem 0.5rem;
  }
}

.main-content {
  flex: 1;
  padding: 2.5rem;
  overflow-y: auto;
}

/* Components */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.stat-value {
  font-size: 1.85rem;
  font-weight: 700;
  color: var(--primary);
  margin: 0.5rem 0;
}

.stat-trend {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--success-bg);
  color: var(--success-text);
}

.chart-section {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 2rem;
}

.chart-span-2 {
  grid-column: span 2;
}

/* WHY min-width:0 + overflow:hidden: senza questo il grafico Incassi, quando
   ha molte barre (periodo Personalizzato con range ampio), forza la sua
   colonna della grid a crescere oltre lo spazio disponibile invece di
   restare nel proprio track e scorrere internamente (vedi #chart-scroll in
   chart.html). */
#chart-card {
  min-width: 0;
  overflow: hidden;
}

/* Buttons */
.btn {
  padding: 0.75rem 1.25rem;
  border-radius: var(--radius-md);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  border: none;
  font-family: inherit;
  font-size: inherit;
}

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

.btn-primary:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  text-decoration: none;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
  font-weight: 500;
  background: none;
  border: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: inherit;
}

.nav-item:hover {
  background: var(--bg-hover);
}

.nav-item.active {
  background: var(--nav-active-bg);
  color: var(--primary);
}

/* Voce di sottomenu (es. "Prodotti più venduti" sotto Statistiche). */
.nav-item.nav-sub {
  padding: 0.5rem 1rem 0.5rem 2.75rem;
  margin-top: -0.25rem;
  font-size: 0.85rem;
}

/* Table */
.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 1rem;
}

.data-table th {
  text-align: left;
  padding: 1rem;
  color: var(--text-secondary);
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
}

.data-table td {
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.data-table th.sortable {
  cursor: pointer;
  user-select: none;
}

.data-table th.sortable:hover {
  color: var(--primary);
}

/* Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(15, 23, 42, 0.4);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: var(--bg-card);
  border-radius: 1.5rem;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  padding: 2rem;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.modal-body {
  overflow-y: auto;
  flex: 1;
}

/* Search */
.search-container {
  position: relative;
  max-width: 300px;
}

.search-input {
  width: 100%;
  padding: 0.625rem 1rem 0.625rem 2.5rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-hover);
  font-size: 0.875rem;
  outline: none;
  transition: all 0.2s;
  font-family: inherit;
}

.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30, 92, 79, 0.1);
}

.search-icon {
  position: absolute;
  left: 0.875rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
}

/* Login */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top right, var(--border), var(--bg-app));
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-card);
  padding: 2.5rem;
  border-radius: 2rem;
  box-shadow: var(--shadow);
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-primary);
}

.form-input {
  width: 100%;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-hover);
  color: var(--text-primary);
  outline: none;
  font-family: inherit;
  font-size: inherit;
}

.form-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30, 92, 79, 0.1);
}

/* Utilities */
.flex {
  display: flex;
}

.flex-col {
  flex-direction: column;
}

.items-center {
  align-items: center;
}

.justify-center {
  justify-content: center;
}

.justify-between {
  justify-content: space-between;
}

.text-center {
  text-align: center;
}

.gap-2 {
  gap: 0.5rem;
}

.gap-3 {
  gap: 0.75rem;
}

.gap-4 {
  gap: 1rem;
}

.mb-2 {
  margin-bottom: 0.5rem;
}

.mb-4 {
  margin-bottom: 1rem;
}

.mb-8 {
  margin-bottom: 2rem;
}

.text-sm {
  font-size: 0.875rem;
}

.text-xs {
  font-size: 0.75rem;
}

.text-muted {
  color: var(--text-secondary);
}

.font-bold {
  font-weight: 700;
}

.w-full {
  width: 100%;
}

.desktop-only {
  display: flex;
}

.hidden {
  display: none !important;
}

.theme-option {
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
}

.theme-option.active {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--bg-hover);
}

/* HTMX loading indicator */
.htmx-indicator {
  display: none;
}

.htmx-request .htmx-indicator {
  display: inline-block;
}

.htmx-request.htmx-indicator {
  display: inline-block;
}

/* Smooth transitions for HTMX swaps */
.htmx-swapping {
  opacity: 0;
  transition: opacity 0.15s ease-out;
}

.htmx-settling {
  opacity: 1;
  transition: opacity 0.15s ease-in;
}

/* Mobile Responsiveness */
@media (max-width: 1024px) {
  .chart-section {
    grid-template-columns: minmax(0, 1fr);
  }

  /* WHY: con una sola colonna esplicita, uno "span 2" residuo dal layout
     desktop costringe la grid a crearsi una colonna implicita fantasma
     (per soddisfare lo span), che spinge la card larga oltre lo schermo. */
  .chart-span-2 {
    grid-column: auto;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }

  .sidebar {
    position: fixed;
    left: -100%;
    top: 0;
    bottom: 0;
    z-index: 1001;
    transition: left 0.3s ease;
    box-shadow: 20px 0 50px rgba(0, 0, 0, 0.1);
  }

  .sidebar.open {
    left: 0;
  }

  .sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(2px);
    z-index: 1000;
    display: none;
  }

  .sidebar-overlay.visible {
    display: block;
  }

  .main-content {
    padding: 1.5rem;
    padding-top: 5rem;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .desktop-only {
    display: none;
  }

  header {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 1.5rem;
  }

  .mobile-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--bg-card);
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 999;
    border-bottom: 1px solid var(--border);
  }

  .table-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-top: 1rem;
    border-radius: var(--radius-md);
  }

  .data-table {
    min-width: 600px;
  }

  .card {
    padding: 1.25rem;
  }

  .modal-content {
    padding: 1.5rem;
    width: 95%;
  }

  h1 {
    font-size: 1.5rem !important;
  }
}

/* Hidden elements on mobile by default */
.mobile-header {
  display: none;
}

@media (max-width: 768px) {
  .mobile-header {
    display: flex;
  }
}

@media print {
  .sidebar, .mobile-header, .sidebar-overlay, .no-print { display: none !important; }
  .main-content, .app-container { margin: 0 !important; padding: 0 !important; }
  @page { size: A4; margin: 1.5cm; }
  body { font-size: 11pt; color: #000; background: #fff; }
  .card { box-shadow: none !important; border: 1px solid #e2e8f0; }
  table { page-break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
}

/* Grafico Incassi: con molte barre (es. un mese di 31 giorni) il contenuto
   interno e' piu' largo del riquadro visibile e scorre orizzontalmente.
   Scrollbar sempre visibile (non overlay) cosi' e' chiaro che c'e' altro
   contenuto a destra invece di sembrare tagliato. */
.chart-scroll {
  scrollbar-width: thin;
  scrollbar-color: var(--text-faint) transparent;
}
.chart-scroll::-webkit-scrollbar {
  height: 8px;
}
.chart-scroll::-webkit-scrollbar-track {
  background: transparent;
}
.chart-scroll::-webkit-scrollbar-thumb {
  background: var(--text-faint);
  border-radius: 4px;
}
.chart-scroll::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ---------- Statistiche: istogramma per operatore, torta reparti, filtri ---------- */

/* Bottoni sotto le barre (mesi/giorni): posizionati da chart.html leggendo la x
   di ogni barra, così restano allineati anche quando il grafico scorre. */
.stats-pills {
  position: relative;
  height: 30px;
  margin-top: 0.5rem;
}

.stats-pill {
  position: absolute;
  top: 0;
  transform: translateX(-50%);
  min-width: 26px;
  padding: 0.2rem 0.55rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-card);
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
}

.stats-pill:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.stats-pill.on {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--bg-card);
}

.stats-pill.empty:not(.on) {
  border-style: dashed;
  color: var(--text-faint);
}

.stats-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 1rem;
}

.stats-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-card);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.72rem;
  cursor: pointer;
}

.stats-legend-item:hover {
  border-color: var(--primary);
}

.stats-legend-item.on {
  border-color: var(--text-primary);
  font-weight: 700;
}

.stats-legend-item[disabled] {
  cursor: default;
}

.stats-legend-item i,
.pie-row i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex: none;
}

.pie-wrap {
  display: flex;
  justify-content: center;
}

.pie {
  width: 100%;
  max-width: 250px;
  height: auto;
  overflow: visible;
}

/* Anello sottile: ogni segmento è un arco con il bordo colorato (il colore sta
   nell'attributo stroke). Lo spessore va tenuto uguale a _STROKE in charts.py. */
.pie-slice {
  fill: none;
  stroke-width: 14;
  transition: opacity 0.15s, stroke-width 0.15s;
}

.pie-slice[data-stats-reparto] {
  cursor: pointer;
}

.pie-slice[data-stats-reparto]:hover {
  stroke-width: 17;
}

.pie-slice.dim {
  opacity: 0.25;
}

.pie-slice.picked {
  stroke-width: 18;
}

/* Con il clic il browser disegna un rettangolo di focus attorno all'elemento
   (per un segmento grande copre tutta la torta): lo togliamo e, per chi usa la
   tastiera, ingrandiamo e ombreggiamo il segmento invece del rettangolo. */
.pie-slice:focus,
.pie-center:focus {
  outline: none;
}

.pie-slice:focus-visible {
  stroke-width: 18;
  filter: drop-shadow(0 0 3px var(--text-primary));
}

.pie-tip {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  padding: 0.45rem 0.75rem;
  border-radius: 0.75rem;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-primary);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.12);
}

.pie-center {
  cursor: pointer;
}

/* Il cerchio è solo l'area cliccabile del centro. */
.pie-center circle {
  fill: transparent;
  stroke: none;
  transition: fill 0.15s;
}

.pie-center:hover circle,
.pie-center:focus-visible circle {
  fill: var(--bg-app);
}

.pie-center text {
  fill: var(--text-primary);
  text-anchor: middle;
}

.pie-center .amount {
  font-weight: 700;
}

.pie-center .sub {
  fill: var(--text-secondary);
  font-size: 8.5px;
  font-weight: 600;
}

.pie-center .hint {
  fill: var(--text-muted);
  font-size: 7.5px;
}

.pie-list {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  margin-top: 1rem;
}

.pie-row {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto auto;
  gap: 0.6rem;
  align-items: center;
  width: 100%;
  padding: 0.35rem 0.5rem;
  border: 0;
  border-radius: 0.6rem;
  background: none;
  color: var(--text-primary);
  font: inherit;
  font-size: 0.8rem;
  text-align: left;
}

.pie-row[data-stats-reparto] {
  cursor: pointer;
}

.pie-row[data-stats-reparto]:hover {
  background: var(--bg-hover);
}

.pie-row.on {
  background: var(--success-bg);
  font-weight: 700;
}

.pie-row .pct {
  width: 3rem;
  text-align: right;
  color: var(--text-muted);
  font-size: 0.72rem;
}

.pie-row .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pie-more {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  margin-top: 0.75rem;
  padding: 0.6rem 0.5rem 0;
  border: 0;
  border-top: 1px solid var(--border);
  background: none;
  color: var(--primary);
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.pie-more:hover {
  text-decoration: underline;
}

.stats-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  min-height: 1.9rem;
}

.stats-filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.35rem 0.2rem 0.7rem;
  border-radius: 999px;
  background: var(--success-bg);
  color: var(--primary);
  font-size: 0.75rem;
  font-weight: 700;
}

.stats-filter-chip small {
  color: var(--text-muted);
  font-weight: 500;
}

.stats-filter-chip button {
  width: 18px;
  height: 18px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
}

.stats-filter-chip button:hover {
  color: var(--danger);
}

.stats-link-btn {
  padding: 0 0.25rem;
  border: 0;
  background: none;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.75rem;
  text-decoration: underline;
  cursor: pointer;
}

.doc-reparto-cell {
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

/* Colonne numeriche della tabella Documenti: importi allineati a destra. */
.data-table th.num,
.data-table td.num {
  text-align: right;
  white-space: nowrap;
}

/* Con 9-11 colonne il padding standard (1rem) fa scorrere la tabella già a 1000px. */
#documents-card .data-table th,
#documents-card .data-table td {
  padding: 0.75rem 0.6rem;
}

#documents-card .data-table td.nowrap {
  white-space: nowrap;
}

.data-table .doc-detail-col {
  width: 1%;
  text-align: right;
}

.doc-detail-link {
  display: inline-flex;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  text-decoration: none;
}

.doc-detail-link:hover,
.doc-detail-link:focus-visible {
  background: var(--bg-hover);
  color: var(--primary);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Totali dei documenti filtrati, sopra la tabella. */
.doc-totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(105px, 1fr));
  gap: 0.75rem;
}

.doc-total {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-md);
  background: var(--bg-hover);
  border: 1px solid var(--border);
}

.doc-total span {
  color: var(--text-muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.doc-total strong {
  color: var(--text-primary);
  font-size: 1.05rem;
}

.doc-total-reparto {
  background: var(--success-bg);
}

.doc-total-reparto strong {
  color: var(--primary);
}

/* Menu Tipo documento / Pagamento della tabella Documenti. */
.stats-doc-filter {
  padding: 0.45rem 0.75rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-hover);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.stats-doc-filter:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(30, 92, 79, 0.1);
}

/* Periodi rapidi di Vendite e Movimenti: il periodo scelto resta evidenziato. */
.date-presets {
  padding: 0.75rem 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.date-preset {
  padding: 0.3rem 0.6rem;
  font-size: 0.75rem;
  color: var(--primary);
  border: 1px solid var(--border);
  background: var(--bg-card);
}

.date-preset:hover {
  background: var(--bg-hover);
}

.date-preset[aria-pressed="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* Guida online contestuale (templates/help, static/js/help.js) */
.help-dialog {
  border: none;
  padding: 0;
  background: transparent;
  color: var(--text-primary);
  width: min(720px, calc(100vw - 2rem));
  max-height: calc(100vh - 3rem);
  margin: auto;
  overflow: visible;
}

.help-dialog::backdrop {
  background: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
}

.help-panel {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 3rem);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

.help-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border);
}

.help-kicker {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.help-title {
  margin: 0.1rem 0 0;
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--text-primary);
}

.help-close {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
}

.help-close:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.help-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--bg-hover);
}

.help-toc[hidden] {
  display: none;
}

.help-toc a {
  padding: 0.2rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-weight: 600;
  text-decoration: none;
}

.help-toc a:hover {
  color: var(--primary);
  border-color: var(--primary);
}

.help-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 1.5rem;
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-secondary);
  outline: none;
}

.help-body h3 {
  margin: 1.5rem 0 0.4rem;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

.help-body section:first-child h3,
.help-body .help-lead + section h3 {
  margin-top: 1rem;
}

.help-body p {
  margin: 0 0 0.6rem;
}

.help-body .help-lead {
  font-size: 0.95rem;
  color: var(--text-primary);
}

.help-body ul,
.help-body ol {
  margin: 0 0 0.6rem;
  padding-left: 1.25rem;
}

.help-body li {
  margin-bottom: 0.3rem;
}

.help-body dl {
  margin: 0 0 0.6rem;
}

.help-body dt {
  font-weight: 700;
  color: var(--text-primary);
}

.help-body dd {
  margin: 0 0 0.6rem;
}

.help-body strong {
  color: var(--text-primary);
}

.help-body section {
  scroll-margin-top: 0.5rem;
}

.help-tip,
.help-note {
  margin: 0.75rem 0;
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
}

.help-tip {
  background: var(--success-bg);
  color: var(--text-primary);
}

.help-note {
  background: var(--warning-bg);
  color: var(--text-primary);
}

.help-tip::before,
.help-note::before {
  font-weight: 700;
}

.help-tip::before {
  content: "Suggerimento · ";
}

.help-note::before {
  content: "Attenzione · ";
}

.help-message {
  padding: 2rem 0;
  text-align: center;
  color: var(--text-muted);
}

.help-footer {
  padding: 0.6rem 1.5rem;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.help-footer kbd {
  padding: 0.05rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 0.3rem;
  background: var(--bg-hover);
  font-family: inherit;
  font-size: 0.7rem;
}

/* Sotto i 640px la guida occupa lo schermo intero. */
@media (max-width: 640px) {
  .help-dialog {
    width: 100vw;
    max-width: 100vw;
    max-height: 100dvh;
    height: 100dvh;
  }

  .help-panel {
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
    border: none;
  }
}

body:has(.help-dialog[open]) {
  overflow: hidden;
}
