/* Scale Sync — stile v14
   Tema chiaro/scuro (automatico + pulsante), animazioni leggere, responsive:
   PC (>1100px) · tablet (641–1100px, sidebar compatta o a cassetto) · smartphone (≤640px).
   Le animazioni si disattivano con prefers-reduced-motion. */

:root {
  color-scheme: light;
  --bg: #f3f5fa;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --text: #0f1b2d;
  --muted: #5a6a80;
  --border: #e2e8f1;
  --accent: #2563eb;
  --accent-2: #6d5dfc;
  --accent-soft: rgba(37, 99, 235, .12);
  --ok: #16a34a;
  --warn: #d97706;
  --crit: #dc2626;
  --ok-bg: #dcfce7;   --ok-fg: #166534;
  --warn-bg: #fef3c7; --warn-fg: #92400e;
  --crit-bg: #fee2e2; --crit-fg: #991b1b;
  --neu-bg: #e8edf4;  --neu-fg: #475569;
  --info-bg: #dbeafe; --info-fg: #1e40af;
  --glow-1: rgba(37, 99, 235, .10);
  --glow-2: rgba(109, 93, 252, .08);
  --sidebar-1: #0b1226;
  --sidebar-2: #12204a;
  --sh-1: 0 1px 2px rgba(15, 27, 45, .06), 0 1px 1px rgba(15, 27, 45, .04);
  --sh-2: 0 10px 26px rgba(15, 27, 45, .10);
  --sh-3: 0 28px 80px rgba(8, 13, 28, .38);
  --radius: 14px;
  --ease: cubic-bezier(.2, .8, .2, 1);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a1120; --surface: #111b2e; --surface-2: #16223a;
  --text: #e7eefb; --muted: #94a6c2; --border: #24324d;
  --accent: #5b8def; --accent-2: #8b7bff; --accent-soft: rgba(91, 141, 239, .18);
  --ok: #34d399; --warn: #fbbf24; --crit: #f87171;
  --ok-bg: rgba(52, 211, 153, .16);  --ok-fg: #86efac;
  --warn-bg: rgba(251, 191, 36, .16); --warn-fg: #fcd34d;
  --crit-bg: rgba(248, 113, 113, .16); --crit-fg: #fca5a5;
  --neu-bg: rgba(148, 163, 184, .18); --neu-fg: #cbd5e1;
  --info-bg: rgba(96, 165, 250, .18); --info-fg: #93c5fd;
  --glow-1: rgba(91, 141, 239, .14); --glow-2: rgba(139, 123, 255, .10);
  --sh-1: 0 1px 2px rgba(0, 0, 0, .35);
  --sh-2: 0 10px 26px rgba(0, 0, 0, .45);
  --sh-3: 0 28px 80px rgba(0, 0, 0, .6);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0a1120; --surface: #111b2e; --surface-2: #16223a;
    --text: #e7eefb; --muted: #94a6c2; --border: #24324d;
    --accent: #5b8def; --accent-2: #8b7bff; --accent-soft: rgba(91, 141, 239, .18);
    --ok: #34d399; --warn: #fbbf24; --crit: #f87171;
    --ok-bg: rgba(52, 211, 153, .16);  --ok-fg: #86efac;
    --warn-bg: rgba(251, 191, 36, .16); --warn-fg: #fcd34d;
    --crit-bg: rgba(248, 113, 113, .16); --crit-fg: #fca5a5;
    --neu-bg: rgba(148, 163, 184, .18); --neu-fg: #cbd5e1;
    --info-bg: rgba(96, 165, 250, .18); --info-fg: #93c5fd;
    --glow-1: rgba(91, 141, 239, .14); --glow-2: rgba(139, 123, 255, .10);
    --sh-1: 0 1px 2px rgba(0, 0, 0, .35);
    --sh-2: 0 10px 26px rgba(0, 0, 0, .45);
    --sh-3: 0 28px 80px rgba(0, 0, 0, .6);
  }
}

/* ---------- Base ---------- */

* { box-sizing: border-box; margin: 0; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
  font-family: -apple-system, "Segoe UI Variable", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  background-color: var(--bg);
  background-image:
    radial-gradient(900px 520px at 100% -10%, var(--glow-1), transparent),
    radial-gradient(700px 420px at -10% 110%, var(--glow-2), transparent);
  background-attachment: fixed;
  color: var(--text);
  transition: background-color .3s, color .3s;
}

body:has(.modal-overlay) { overflow: hidden; }

h1, h2, h3 { font-weight: 700; letter-spacing: -.01em; }
.muted { color: var(--muted); }
.attivazione-bloccata {
  padding: 1rem; margin-bottom: 1rem; border: 1px solid var(--warn);
  border-radius: var(--radius); background: var(--warn-bg); color: var(--warn-fg);
}
.attivazione-bloccata ul { margin-top: 6px; }
.full { width: 100%; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
::selection { background: var(--accent-soft); }

/* ---------- Animazioni ---------- */

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeOut { to { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
@keyframes pop { from { opacity: 0; transform: translateY(18px) scale(.96); } to { opacity: 1; transform: none; } }
@keyframes popOut { to { opacity: 0; transform: translateY(10px) scale(.97); } }
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheetDown { to { transform: translateY(100%); } }
@keyframes toastIn { from { opacity: 0; transform: translateY(16px) scale(.96); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateX(24px); } }
@keyframes progress { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes shake { 0%, 100% { transform: none; } 20% { transform: translateX(-9px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(4px); } }
@keyframes ripple { to { transform: scale(2.6); opacity: 0; } }
@keyframes gradShift { 0%, 100% { background-position: 0% 50%; } 50% { background-position: 100% 50%; } }
@keyframes float { 0%, 100% { transform: translate(0, 0); } 50% { transform: translate(5vmax, 4vmax); } }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-6px); } }
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes grow { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes pulseDot { 0% { box-shadow: 0 0 0 0 currentColor; } 70%, 100% { box-shadow: 0 0 0 7px transparent; } }

/* ---------- Spinner e caricamenti ---------- */

.spinner {
  display: inline-block;
  width: 20px; height: 20px;
  border: 2.5px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .75s linear infinite;
  vertical-align: middle;
}
.spinner.lg { width: 46px; height: 46px; border-width: 4px; }

.loader-page {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; min-height: 45vh; color: var(--muted);
  animation: fade .3s .15s both; /* il ritardo evita lampeggi sulle risposte rapide */
}

.skeleton {
  display: block; min-height: 14px; border-radius: 8px;
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 37%, var(--surface-2) 63%);
  background-size: 200% 100%; animation: shimmer 1.4s linear infinite;
}

/* ---------- Login ---------- */

.login-wrap {
  min-height: 100vh; min-height: 100dvh;
  display: flex; align-items: center; justify-content: center;
  padding: calc(24px + var(--sat)) 20px calc(24px + var(--sab));
  position: relative; overflow: hidden;
  background: linear-gradient(135deg, #0b1226, #17306e 50%, #4a2fb3);
  background-size: 240% 240%;
  animation: gradShift 18s ease infinite;
}
.login-wrap::before, .login-wrap::after {
  content: ""; position: absolute; width: 46vmax; height: 46vmax; border-radius: 50%;
  filter: blur(80px); opacity: .45; animation: float 16s ease-in-out infinite; pointer-events: none;
}
.login-wrap::before { background: #2563eb; top: -18vmax; left: -14vmax; }
.login-wrap::after { background: #7c3aed; bottom: -20vmax; right: -14vmax; animation-delay: -8s; }

.login-card {
  position: relative; z-index: 1;
  background: var(--surface);
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 22px;
  padding: 36px 32px;
  width: min(420px, 100%);
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--sh-3);
  animation: pop .6s var(--ease) both;
}
.login-card.shake { animation: shake .45s; }
.login-card .logo { text-align: center; }
.login-card .logo img { animation: bob 5s ease-in-out infinite; }
.login-card h1 { text-align: center; font-size: 24px; }
.login-card .muted { text-align: center; margin-bottom: 6px; }
.errore { color: var(--crit); font-size: 13px; min-height: 18px; }
.errore:not(:empty) { animation: fade .25s both; }

.pw-wrap { position: relative; display: block; }
.pw-wrap input { width: 100%; padding-right: 46px; }
.pw-toggle {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: 0; cursor: pointer; color: var(--muted);
  padding: 8px; border-radius: 8px; font-size: 16px; line-height: 1;
  transition: background .15s, color .15s;
}
.pw-toggle:hover { background: var(--accent-soft); color: var(--accent); }

/* ---------- Shell e sidebar ---------- */

.shell { display: flex; min-height: 100vh; min-height: 100dvh; }

.sidebar {
  width: 248px; flex-shrink: 0;
  background: linear-gradient(180deg, var(--sidebar-1), var(--sidebar-2));
  color: #cbd5e1;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  overflow-y: auto; overscroll-behavior: contain;
  scrollbar-width: thin; scrollbar-color: rgba(255, 255, 255, .18) transparent;
  padding-left: var(--sal);
}

.brand {
  padding: 22px 20px 18px; font-size: 18px; font-weight: 800; color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, .08); letter-spacing: -.01em;
}
.brand span { display: block; font-size: 12px; font-weight: 500; color: #94a3b8; letter-spacing: .12em; }

.sidebar nav { flex: 1; padding: 14px 12px; display: flex; flex-direction: column; gap: 4px; }
.sidebar nav a, .sidebar nav .nav-toggle {
  position: relative; display: flex; align-items: center; justify-content: space-between;
  width: 100%; text-align: left; color: #cbd5e1; text-decoration: none;
  background: none; border: 0; font: inherit; font-size: 14.5px;
  padding: 11px 14px; border-radius: 11px; cursor: pointer;
  transition: background .2s, color .2s, transform .2s var(--ease);
}
.sidebar nav a:hover, .sidebar nav .nav-toggle:hover { background: rgba(255, 255, 255, .08); color: #fff; transform: translateX(3px); }
.sidebar nav a.active, .sidebar nav .nav-toggle.active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; font-weight: 600;
  box-shadow: 0 8px 20px -8px rgba(37, 99, 235, .8);
}
.sidebar nav a.active::before {
  content: ""; position: absolute; left: -12px; top: 22%; bottom: 22%; width: 4px;
  border-radius: 0 4px 4px 0; background: #fff;
}
.sidebar .submenu { padding-left: 10px; margin-top: 2px; animation: rise .25s var(--ease) both; }
.sidebar .submenu[hidden], [data-opzionale][hidden] { display: none !important; }
.noscript-avviso { padding: 24px; font-family: sans-serif; }
.dashboard-negozi, .punti-vendita-nota { margin-bottom: 14px; }
.bilance-nota { margin-top: 12px; }
.blocco-tecnico { max-width: 460px; margin: 48px auto; text-align: center; }
.blocco-tecnico .muted { margin: 10px 0 16px; }
.reparto-codici-etichetta { font-weight: 600; }
.codici-nota { margin-top: 6px; }
.pesato-secondario { margin-top: 14px; }
.pesato-grafico { display: flex; align-items: flex-end; gap: 10px; height: 130px; padding: 10px 4px 0; border-bottom: 2px solid var(--border); }
.pesato-valore { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; font-size: 10px; }
.pesato-etichetta { text-align: center; }
.pesato-barra, .pesato-barra-vuota { border-radius: 6px 6px 0 0; }
.pesato-barra { width: 100%; background: linear-gradient(180deg, var(--accent-2), var(--accent)); }
.pesato-barra.negativo { background: var(--crit); }
.pesato-barra-vuota { flex: 1; background: var(--border); }
.pesato-motivo { font-size: 12px; margin-top: 8px; }
.rapporti-scontrino { margin-top: 10px; }
.config-negozi-nota { margin: 6px 0 12px; }
.config-negozi-lista { display: flex; flex-direction: column; gap: 8px; max-width: 600px; }
.config-negozio { display: block; padding: 12px 16px; border: 1px solid var(--border); border-radius: 10px; text-decoration: none; color: var(--text); }
.config-negozio .muted { margin-left: 8px; font-size: 12px; }
.form-reparto-nota { font-size: 12px; }
.sidebar .submenu a { font-size: 13.5px; padding: 9px 14px; }
.sidebar .submenu .muted-nav { color: #64748b; font-size: 11px; }

.sidebar-footer { padding: 16px; padding-bottom: calc(16px + var(--sab)); border-top: 1px solid rgba(255, 255, 255, .08); display: flex; flex-direction: column; gap: 12px; }
.user-chip strong { display: block; color: #fff; font-size: 14.5px; }
.user-chip span { font-size: 12px; color: #94a3b8; overflow-wrap: anywhere; }
.footer-azioni { display: flex; gap: 8px; }
.footer-azioni .full { flex: 1; width: auto; }
.sidebar .btn.ghost { border-color: rgba(255, 255, 255, .16); color: #cbd5e1; }
.sidebar .btn.ghost:hover { background: rgba(255, 255, 255, .1); color: #fff; border-color: rgba(255, 255, 255, .3); }

.sidebar-backdrop { display: none; }

.content {
  flex: 1; min-width: 0; max-width: 1600px;
  padding: 32px clamp(16px, 3vw, 44px) calc(48px + var(--sab));
  padding-right: calc(clamp(16px, 3vw, 44px) + var(--sar));
}
.content h2 { margin-bottom: 20px; font-size: clamp(21px, 2.4vw, 27px); }

/* entrata delle pagine (solo al cambio vista, non nei refresh in tempo reale) */
.content.anim-in > * { animation: rise .5s var(--ease) both; }
.content.anim-in > *:nth-child(2) { animation-delay: .05s; }
.content.anim-in > *:nth-child(3) { animation-delay: .1s; }
.content.anim-in > *:nth-child(4) { animation-delay: .15s; }
.content.anim-in > *:nth-child(n+5) { animation-delay: .2s; }
.content.anim-in > .loader-page { animation: fade .3s .15s both; }
.content.anim-in .cards > *, .content.anim-in .pv-cards > * { animation: rise .5s var(--ease) both; }
.content.anim-in .cards > *:nth-child(2), .content.anim-in .pv-cards > *:nth-child(2) { animation-delay: .06s; }
.content.anim-in .cards > *:nth-child(3), .content.anim-in .pv-cards > *:nth-child(3) { animation-delay: .12s; }
.content.anim-in .cards > *:nth-child(4), .content.anim-in .pv-cards > *:nth-child(4) { animation-delay: .18s; }
.content.anim-in .cards > *:nth-child(n+5), .content.anim-in .pv-cards > *:nth-child(n+5) { animation-delay: .24s; }

.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h2 { margin-bottom: 0; flex: 0 0 auto; }
.page-head .spacer { flex: 1; }

.negozio-tabs {
  display: flex; align-items: center; gap: 4px; max-width: 100%; margin-bottom: 18px; padding: 5px;
  overflow-x: auto; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px;
  scrollbar-width: thin;
}
.negozio-tab {
  display: inline-flex; flex: 0 0 auto; align-items: center; justify-content: center;
  min-height: 38px; padding: 8px 14px; border: 1px solid transparent; border-radius: 8px;
  color: var(--muted); font-size: 13.5px; font-weight: 600; text-decoration: none; white-space: nowrap;
  transition: color .18s, background-color .18s, border-color .18s, box-shadow .18s;
}
.negozio-tab:hover { color: var(--text); background: var(--surface); }
.negozio-tab[aria-current="page"] {
  color: var(--accent); background: var(--surface); border-color: var(--border);
  box-shadow: 0 1px 2px rgba(15, 27, 45, .08);
}
.negozio-tab:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* Dashboard negozio: sidebar a sinistra (menu voci modificabili/inviabili al
   POS) + contenuto a destra. */
.negozio-layout {
  display: grid; grid-template-columns: 252px minmax(0, 1fr); gap: 20px; align-items: start;
}
.negozio-menu {
  position: sticky; top: 16px; display: flex; flex-direction: column; gap: 2px;
  padding: 12px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 14px; max-height: calc(100vh - 32px); overflow-y: auto; scrollbar-width: thin;
}
.negozio-menu details { border: 0; margin: 0; }
.negozio-menu summary {
  cursor: pointer; list-style: none; user-select: none;
  padding: 10px 10px 4px; margin-top: 4px;
  font-size: 11px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--muted);
}
.negozio-menu summary::-webkit-details-marker { display: none; }
.negozio-menu summary::after { content: "▾"; float: right; opacity: .6; }
.negozio-menu details:not([open]) summary::after { content: "▸"; }
.negozio-sottomenu { display: flex; flex-direction: column; gap: 2px; padding-left: 0; }
.negozio-menu .negozio-tab {
  display: flex; align-items: center; justify-content: flex-start; text-align: left;
  gap: 8px; white-space: normal;
  min-height: 36px; padding: 7px 12px; border: 1px solid transparent; border-radius: 9px;
  color: var(--muted); font-size: 13.5px; font-weight: 600; text-decoration: none;
  transition: color .18s, background-color .18s, border-color .18s;
}
.negozio-menu .negozio-tab:hover { color: var(--text); background: var(--surface); }
.negozio-menu .negozio-tab[aria-current="page"] {
  color: var(--accent); background: var(--surface); border-color: var(--border);
  box-shadow: 0 1px 2px rgba(15, 27, 45, .08);
}
#negozio-body { min-width: 0; }
/* Testata della vista attiva: titolo sopra, controlli (ricerca/pulsanti/dropdown)
   in una riga sotto, allineati e non disallineati. */
.negozio-vista-titolo { margin: 0 0 12px; }
.negozio-vista-controlli {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px;
}
.negozio-vista-controlli .search { width: 260px; }
.negozio-vista-controlli .search:focus { width: 320px; }
/* Form PLU: testi e date su 3 colonne (confezionamento / vendita / scadenza). */
.plu-date-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
.plu-data-col {
  display: flex; flex-direction: column; gap: 8px; min-width: 0;
  padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
}
.plu-data-titolo { font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.plu-data-col label { display: flex; flex-direction: column; gap: 4px; }
.plu-data-col label.chk { flex-direction: row; align-items: center; gap: 8px; }
.plu-data-col input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
@media (max-width: 760px) { .plu-date-grid { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 900px) {
  .negozio-layout { grid-template-columns: 1fr; }
  .negozio-menu { position: static; max-height: none; }
}

.search {
  padding: 10px 14px 10px 38px; width: 260px; max-width: 100%;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' fill='none' stroke='%238a98ad' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='7' cy='7' r='5.5'/%3E%3Cpath d='M11.5 11.5L15 15'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: 13px center;
  transition: width .3s var(--ease), border-color .2s, box-shadow .2s;
}
.search:focus { width: 340px; }

/* ---------- Card e statistiche ---------- */

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; margin-bottom: 26px; }

.stat-card {
  position: relative; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px 20px 18px 24px; box-shadow: var(--sh-1);
  transition: transform .25s var(--ease), box-shadow .25s;
}
.stat-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 5px; background: linear-gradient(180deg, var(--accent), var(--accent-2)); }
.stat-card:hover { transform: translateY(-3px); box-shadow: var(--sh-2); }
.stat-card .valore { font-size: 32px; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.stat-card .etichetta { color: var(--muted); font-size: 13.5px; margin-top: 4px; }
.stat-card.warn::before { background: var(--warn); } .stat-card.warn .valore { color: var(--warn); }
.stat-card.crit::before { background: var(--crit); } .stat-card.crit .valore { color: var(--crit); }
.stat-card.ok::before { background: var(--ok); }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 22px; margin-bottom: 22px; box-shadow: var(--sh-1);
  transition: box-shadow .25s, border-color .25s;
}
.panel:hover { box-shadow: var(--sh-2); }
.panel h3 { margin-bottom: 14px; font-size: 16px; }

.grid-2-panels { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; align-items: start; }

/* grafico a barre: le barre crescono dal basso */
div[title$=" kg"] { transform-origin: bottom; animation: grow .7s var(--ease) both; transition: filter .2s; }
div[title$=" kg"]:hover { filter: brightness(1.15); }

/* ---------- Tabelle ---------- */

table { width: 100%; border-collapse: separate; border-spacing: 0; background: transparent; }

.card-table {
  background:
    linear-gradient(to right, var(--surface) 30%, transparent) 0 0 / 40px 100% no-repeat local,
    linear-gradient(to left, var(--surface) 30%, transparent) 100% 0 / 40px 100% no-repeat local,
    radial-gradient(farthest-side at 0 50%, rgba(0, 0, 0, .18), transparent) 0 0 / 14px 100% no-repeat scroll,
    radial-gradient(farthest-side at 100% 50%, rgba(0, 0, 0, .18), transparent) 100% 0 / 14px 100% no-repeat scroll,
    var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--sh-1);
  overflow: auto; -webkit-overflow-scrolling: touch; margin-bottom: 22px;
}
th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); padding: 9px 12px; background: var(--surface-2);
  border-bottom: 1px solid var(--border); white-space: nowrap; font-weight: 700;
}
/* ordinamento colonne: clic su intestazione = crescente → decrescente → naturale */
#content th[tabindex] { cursor: pointer; }
#content th[tabindex]:hover { color: var(--text); }
#content th[tabindex][aria-sort="ascending"]::after { content: " ▲"; color: var(--accent); }
#content th[tabindex][aria-sort="descending"]::after { content: " ▼"; color: var(--accent); }
#content th button.th-sort {
  all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
  font: inherit; color: inherit; text-transform: inherit; letter-spacing: inherit;
}
#content th button.th-sort:hover { color: var(--text); }
.sort-arrows { display: inline-flex; flex-direction: column; gap: 2px; }
.sort-arrows::before, .sort-arrows::after {
  content: ""; border-left: 4px solid transparent; border-right: 4px solid transparent; opacity: .35;
}
.sort-arrows::before { border-bottom: 5px solid currentColor; }
.sort-arrows::after { border-top: 5px solid currentColor; }
th[aria-sort="ascending"] .sort-arrows::before,
th[aria-sort="descending"] .sort-arrows::after { opacity: 1; color: var(--accent); }
td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr { transition: background .15s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: none; }
tbody tr[hidden] { display: none !important; }
.content.anim-in tbody tr { animation: fade .45s both; }

.empty { color: var(--muted); text-align: center; padding: 34px !important; }
.cell-valori { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; min-width: 0; }

/* ---------- Badge ---------- */

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .85; }
.badge.ok { background: var(--ok-bg); color: var(--ok-fg); }
.badge.warn { background: var(--warn-bg); color: var(--warn-fg); }
.badge.crit { background: var(--crit-bg); color: var(--crit-fg); }
.badge.crit::before { animation: pulseDot 1.8s infinite; }
.badge.neutro { background: var(--neu-bg); color: var(--neu-fg); }
.badge.info { background: var(--info-bg); color: var(--info-fg); }

/* ---------- Pulsanti ---------- */

.btn {
  position: relative; overflow: hidden;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid transparent; border-radius: 11px; padding: 10px 18px;
  font: inherit; font-size: 14px; font-weight: 600; line-height: 1.2; cursor: pointer;
  text-align: center; text-decoration: none;
  color: var(--text); background: var(--surface-2);
  transition: transform .18s var(--ease), box-shadow .25s, background-color .2s, border-color .2s, color .2s, background-position .5s;
  -webkit-tap-highlight-color: transparent; user-select: none;
}
.btn:hover { transform: translateY(-1px); box-shadow: var(--sh-2); }
.btn:active { transform: translateY(0) scale(.96); box-shadow: none; }
.btn::before {  /* riflesso che attraversa il pulsante */
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(110deg, transparent 30%, rgba(255, 255, 255, .35) 50%, transparent 70%);
  transform: translateX(-120%); transition: transform .65s var(--ease);
}
.btn.primary:hover::before, .btn.danger:hover::before { transform: translateX(120%); }

.btn.primary {
  color: #fff; background: linear-gradient(135deg, var(--accent), var(--accent-2)); background-size: 150% 150%; background-position: 0 50%;
  box-shadow: 0 8px 18px -8px rgba(37, 99, 235, .8);
}
.btn.primary:hover { background-position: 100% 50%; box-shadow: 0 12px 24px -8px rgba(37, 99, 235, .9); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.ghost:hover { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); box-shadow: none; }
.btn.danger { color: #fff; background: linear-gradient(135deg, #ef4444, #dc2626); box-shadow: 0 8px 18px -8px rgba(220, 38, 38, .8); }
.btn.sm { padding: 6px 12px; font-size: 12.5px; border-radius: 9px; }
.btn:disabled, .btn[disabled] { opacity: .55; cursor: not-allowed; transform: none; box-shadow: none; }

.btn.loading { color: transparent !important; pointer-events: none; }
.btn.loading::after {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, .4); border-top-color: #fff; animation: spin .7s linear infinite;
}
.btn.ghost.loading::after { border-color: var(--accent-soft); border-top-color: var(--accent); }

.ripple { position: absolute; border-radius: 50%; background: currentColor; opacity: .28; transform: scale(0); animation: ripple .6s ease-out forwards; pointer-events: none; }

.azioni { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-group { display: inline-flex; vertical-align: middle; }
.azioni.btn-group { flex-wrap: nowrap; gap: 0; white-space: nowrap; }
.btn-group > .btn { border-radius: 0; margin-left: -1px; }
.btn-group > .btn:first-child { margin-left: 0; border-radius: 7px 0 0 7px; }
.btn-group > .btn:last-child { border-radius: 0 7px 7px 0; }
.btn-group > .btn:only-child { border-radius: 7px; }
.btn-group > .btn:hover { position: relative; z-index: 1; transform: none; }

/* ---------- Form ---------- */

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
input, select, textarea {
  padding: 10px 12px; min-height: 42px;
  border: 1px solid var(--border); border-radius: 10px;
  font: inherit; font-size: 14px; background: var(--surface); color: var(--text);
  transition: border-color .2s, box-shadow .2s, background-color .2s;
}
textarea { min-height: 84px; resize: vertical; }
input:hover, select:hover, textarea:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .7; }
select {
  appearance: none; -webkit-appearance: none; padding-right: 36px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none' stroke='%238a98ad' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M1 1.5l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 13px center;
}
input[type="checkbox"], input[type="radio"] { width: 18px; height: 18px; min-height: 0; padding: 0; accent-color: var(--accent); cursor: pointer; }
.chk { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.chk input[type="checkbox"] { width: auto; }
.allergeni-grid .chk input[type="checkbox"] { width: 18px; }

.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.grid2 > label, .grid2 select { min-width: 0; }
.config-colonne {
  display: grid;
  grid-template-columns: minmax(250px, 1fr) minmax(0, 2fr);
  align-items: start;
  gap: 16px;
}
.config-colonne > * { min-width: 0; }
.config-form { display: flex; flex-direction: column; gap: 12px; }
.config-form button[hidden], #plu-tara-manuale[hidden], #plu-config-stato:empty { display: none; }
.testo-multiriga { white-space: pre-wrap; overflow-wrap: anywhere; }
.plu-dati-aggiuntivi summary { list-style: none; }
.plu-dati-aggiuntivi fieldset { margin-top: 12px; }
@media (max-width: 900px) {
  .config-colonne { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- Modale ---------- */

.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 20px;
  background: rgba(8, 13, 28, .55); -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  animation: fade .2s both;
}
.modal {
  display: flex; flex-direction: column;
  width: min(640px, 100%); max-height: 90vh; max-height: min(90dvh, 860px);
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  box-shadow: var(--sh-3); overflow: hidden; animation: pop .32s var(--ease) both;
}
.modal-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 18px 24px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 18px; }
.modal-head .btn { padding: 6px 11px; }
.modal-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 22px 24px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { flex: none; padding: 14px 24px; border-top: 1px solid var(--border); background: var(--surface-2); display: flex; justify-content: flex-end; gap: 10px; }
.modal-overlay.chiude { animation: fadeOut .18s both; }
.modal-overlay.chiude .modal { animation: popOut .18s both; }

/* ---------- Toast ---------- */

#toast-container {
  position: fixed; z-index: 200; display: flex; flex-direction: column; gap: 10px;
  right: calc(24px + var(--sar)); bottom: calc(24px + var(--sab));
}
.toast {
  position: relative; overflow: hidden; cursor: pointer;
  display: flex; align-items: center; gap: 12px;
  min-width: 240px; max-width: min(420px, 92vw); padding: 13px 18px 15px;
  background: #111c33; color: #fff; border-radius: 13px; box-shadow: var(--sh-3);
  animation: toastIn .35s var(--ease) both;
}
.toast::before {
  content: "✓"; flex: none; display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  background: rgba(52, 211, 153, .22); color: #34d399; font-weight: 800; font-size: 13px;
}
.toast.errore { background: #b91c1c; }
.toast.errore::before { content: "!"; background: rgba(255, 255, 255, .22); color: #fff; }
.toast::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: rgba(255, 255, 255, .55); transform-origin: left; animation: progress 3.5s linear forwards; }
.toast.esce { animation: toastOut .25s ease forwards; }

/* ---------- Dettaglio lotto / tracciabilità ---------- */

.dettaglio-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 16px; margin-bottom: 22px; }
.info-voce { margin-bottom: 10px; }
.info-voce .k { font-size: 11.5px; text-transform: uppercase; color: var(--muted); letter-spacing: .05em; font-weight: 600; }
.info-voce .v { font-weight: 600; overflow-wrap: anywhere; }

.back-link {
  background: none; border: none; color: var(--accent); font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer; padding: 6px 0; margin-bottom: 14px; display: inline-flex; gap: 6px; align-items: center;
  transition: transform .2s var(--ease);
}
.back-link:hover { transform: translateX(-4px); }

/* ---------- Filtri, normative, etichetta, report ---------- */

.filtri { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.filtri button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-size: 13.5px;
  border-radius: 999px; padding: 7px 16px; cursor: pointer;
  transition: background .2s, color .2s, border-color .2s, transform .15s var(--ease);
}
.filtri button:hover { border-color: var(--accent); transform: translateY(-1px); }
.filtri button:active { transform: scale(.95); }
.filtri button.active { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; border-color: transparent; }

.norm-box { border: 1px dashed var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.norm-box legend { font-size: 12px; font-weight: 700; color: var(--muted); padding: 0 8px; }

.allergeni-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; }
.allergeni-grid .chk { flex-direction: row; align-items: center; gap: 10px; font-weight: 400; font-size: 13.5px; cursor: pointer; }
.allergene { background: var(--warn-bg); color: var(--warn-fg); font-weight: 700; padding: 2px 7px; border-radius: 5px; }

.etichetta-preview { padding: 6px 2px; }
.etichetta-preview h3 { margin-bottom: 2px; }
.etichetta-preview .riga { font-size: 14px; margin: 5px 0; }
.prezzo-etichetta { font-size: 20px; font-weight: 800; margin-top: 10px; }
.norm-rif { font-size: 11px; color: var(--muted); margin-top: 12px; line-height: 1.5; border-top: 1px solid var(--border); padding-top: 8px; }

.filtri-report { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; }
.filtri-report label { min-width: 160px; }
.totale-report { margin-top: 14px; font-weight: 700; }

/* ---------- Punti vendita ---------- */

.pv-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; margin-top: 12px; }
.pv-card {
  display: block; border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px;
  text-decoration: none; color: inherit; background: var(--surface);
  transition: box-shadow .25s, transform .25s var(--ease), border-color .25s;
}
.pv-card:hover { border-color: var(--accent); box-shadow: 0 12px 28px -10px var(--accent-soft), var(--sh-2); transform: translateY(-3px); }
.pv-card:active { transform: scale(.98); }
.pv-card .pv-nome { font-size: 16px; font-weight: 700; margin-bottom: 4px; }
.pv-card .pv-info { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }
.pv-card .pv-badge { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Paginazione ---------- */

.paginazione { display: flex; align-items: center; gap: .7rem; flex-wrap: wrap; padding: .8rem 1rem; border-top: 1px solid var(--border); background: var(--surface-2); font-size: .9rem; }
.paginazione .spacer { flex: 1 1 auto; }
.paginazione .pag-stato { font-variant-numeric: tabular-nums; color: var(--muted); }
.paginazione .pag-per { display: inline-flex; flex-direction: row; align-items: center; gap: .4rem; margin: 0; font-weight: 500; color: var(--muted); }
.paginazione .pag-per select { padding: .3rem 1.8rem .3rem .6rem; min-height: 0; border-radius: 8px; background-position: right 9px center; }
.table-pagination {
  display: flex; align-items: center; justify-content: flex-end; gap: .6rem; flex-wrap: wrap;
  margin: -12px 0 18px; padding: 8px 12px; color: var(--muted); font-size: 12.5px;
}
.table-pagination[hidden] { display: none !important; }
.table-pagination .pag-info { margin-right: auto; font-variant-numeric: tabular-nums; }
.table-pagination .pag-per { display: inline-flex; flex-direction: row; align-items: center; gap: .4rem; margin: 0; font-weight: 500; }
.table-pagination .pag-per select { padding: .3rem 1.8rem .3rem .6rem; min-height: 32px; border-radius: 8px; background-position: right 9px center; font-size: 12px; }
.table-pagination .pag-stato { min-width: 4.5rem; text-align: center; font-variant-numeric: tabular-nums; }

/* ---------- Mobile / PWA ---------- */

.hamburger {
  display: none; position: fixed; z-index: 80;
  top: calc(10px + var(--sat)); left: calc(10px + var(--sal));
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--sh-2);
  padding: 9px 14px;
}

/* tablet orizzontale e piccoli portatili: sidebar compatta */
@media (max-width: 1100px) {
  .sidebar { width: 212px; }
  .brand { padding: 18px 14px 14px; }
  .sidebar nav a, .sidebar nav .nav-toggle { padding: 10px 12px; font-size: 14px; }
  .grid-2-panels { grid-template-columns: 1fr; }
  .search:focus { width: 300px; }
}

/* tablet verticale e telefoni: sidebar a cassetto */
@media (max-width: 900px) {
  .hamburger { display: inline-flex; }
  .sidebar {
    position: fixed; top: 0; left: 0; z-index: 90; width: min(300px, 86vw);
    transform: translateX(-102%); visibility: hidden;
    transition: transform .3s var(--ease), visibility 0s .3s;
  }
  .sidebar.aperta { transform: none; visibility: visible; box-shadow: 0 0 60px rgba(0, 0, 0, .55); transition: transform .3s var(--ease), visibility 0s; }
  .sidebar-backdrop {
    display: block; position: fixed; inset: 0; z-index: 85; background: rgba(8, 13, 28, .55);
    -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
    opacity: 0; pointer-events: none; transition: opacity .3s;
  }
  .sidebar.aperta + .sidebar-backdrop { opacity: 1; pointer-events: auto; }
  .content { padding-top: calc(66px + var(--sat)); padding-left: calc(clamp(14px, 3vw, 28px) + var(--sal)); }
  .cards { grid-template-columns: repeat(2, 1fr); }
  .search, .search:focus { width: 100%; }
  .page-head .search, .page-head select.search { flex: 1 1 180px; }
}

/* telefoni */
@media (max-width: 640px) {
  body { font-size: 15px; }
  .content { padding-left: calc(14px + var(--sal)); padding-right: calc(14px + var(--sar)); }
  .panel { padding: 16px; border-radius: 12px; }
  .grid2, .allergeni-grid { grid-template-columns: minmax(0, 1fr); }
  input, select, textarea { font-size: 16px; } /* evita lo zoom automatico di Safari su iPhone */

  .page-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-head .spacer { display: none; }
  .page-head .btn, .page-head .search, .page-head select { width: 100%; }
  .filtri { flex-wrap: nowrap; overflow-x: auto; margin-inline: -14px; padding: 2px 14px 8px; scrollbar-width: none; }
  .filtri::-webkit-scrollbar { display: none; }
  .filtri button { flex: none; }
  .filtri-report label { min-width: 100%; }
  .filtri-report .btn { width: 100%; }

  /* le tabelle diventano elenchi di schede (il JS aggiunge data-label alle celle) */
  .card-table { background: none; border: 0; box-shadow: none; overflow: visible; }
  .content table, .content tbody, .content tr, .content td { display: block; width: 100%; }
  .content thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .content tbody tr {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    padding: 6px 14px; margin-bottom: 12px; box-shadow: var(--sh-1);
  }
  .content tbody tr:hover { background: var(--surface); }
  .content td { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 0; text-align: right; border-bottom: 1px dashed var(--border); overflow-wrap: anywhere; }
  .content td:last-child { border-bottom: 0; }
  .content td::before { content: attr(data-label); flex: 0 0 38%; text-align: left; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
  .content td[data-label=""]::before, .content td:not([data-label])::before { display: none; }
  .content td.empty { display: block; text-align: center; }
  .content td .cell-valori { flex: 1 1 0; justify-content: flex-end; }
  .content td .cell-valori .badge { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
  .content td .azioni { justify-content: flex-end; width: 100%; }
  .content td .azioni.btn-group { flex-wrap: wrap; white-space: normal; }
  .paginazione { border: 1px solid var(--border); border-radius: 14px; }
  .paginazione .spacer { flex-basis: 100%; height: 0; }
  .table-pagination { justify-content: center; margin-top: -8px; }
  .table-pagination .pag-info { flex-basis: 100%; text-align: center; }

  /* modale come "foglio" dal basso */
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal { width: 100%; max-height: 92vh; max-height: 92dvh; border-radius: 22px 22px 0 0; border-bottom: 0; animation: sheetUp .35s var(--ease) both; }
  .modal-overlay.chiude .modal { animation: sheetDown .22s ease both; }
  .modal-head, .modal-body, .modal-foot { padding-left: 18px; padding-right: 18px; }
  .modal-foot { padding-bottom: calc(14px + var(--sab)); }
  .modal-foot .btn { flex: 1; }

  #toast-container { left: calc(14px + var(--sal)); right: calc(14px + var(--sar)); bottom: calc(14px + var(--sab)); align-items: stretch; }
  .toast { max-width: none; }
  .login-card { padding: 28px 22px; }
}

@media (max-width: 380px) {
  .cards { grid-template-columns: 1fr; }
  .stat-card .valore { font-size: 28px; }
}

/* touch: bersagli almeno 44px */
@media (pointer: coarse) {
  .btn { min-height: 44px; }
  .btn.sm { min-height: 38px; }
  .filtri button { min-height: 40px; }
  .sidebar nav a, .sidebar nav .nav-toggle { min-height: 46px; }
  input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; }
  .allergeni-grid .chk input[type="checkbox"] { width: 22px; }
}

@media print {
  .sidebar, .hamburger, .sidebar-backdrop, #toast-container, .btn { display: none !important; }
  .content { padding: 0; max-width: none; }
  body { background: #fff; color: #000; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
[data-campi-stile] [hidden], [data-campi-storici][hidden] { display: none !important; }
