/* ==========================================================================
   MABA STORE — interfaccia del gestionale
   Regole dal sistema di design (repo ux-design, area amministrativa):
   grigi neutri freddi + UN solo accento, font Manrope, nessun colore
   letterale nei componenti: tutto passa dai token qui sotto.
   Per adattare i colori del cliente basta cambiare --accent-*.
   ========================================================================== */

:root {
  --bg: #f4f6f9;
  --surface: #fcfdfe;
  --surface-2: #f7f9fb;
  --line: #e1e5eb;
  --line-2: #edf0f4;
  --text: #161a20;
  --muted: #5f6774;
  --faint: #959dab;

  /* Accento unico — blu "acqua" della rivendita idraulica */
  --accent: #0b5cad;
  --accent-strong: #08498a;
  --accent-soft: #e7f0fa;
  --accent-ink: #0a4f94;
  --on-accent: #fbfdff;

  --ok: #1f7a4d;
  --ok-soft: #e5f4ec;
  --warn: #9a5b00;
  --warn-soft: #fdf3e1;
  --danger: #b4302b;
  --danger-soft: #fbeaea;

  --sidebar: #12263d;
  --sidebar-text: #c9d4e2;
  --sidebar-muted: #7f90a6;
  --sidebar-active: #1d3a5c;

  /* Tinte dei tasti reparto in cassa: varianti spente, testo scuro */
  --tint-blu: #dce9f7;
  --tint-rosso: #f7e0de;
  --tint-turchese: #d8f0ef;
  --tint-grigio: #e6e9ee;
  --tint-verde: #dff0e4;
  --tint-arancio: #f9e8d4;
  --tint-viola: #ebe2f6;
  --tint-giallo: #f7f0cf;

  --font: 'Manrope', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.16);
  --motion: 1;
}
@media (prefers-reduced-motion: reduce) {
  :root { --motion: 0; }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; font-weight: 800; letter-spacing: -0.01em; }
h2 { font-size: 16px; font-weight: 700; }
h3 { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.hidden { display: none !important; }
.grow { flex: 1; }

/* ---------------------------------------------------------------- shell */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 18px 12px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 9px; background: var(--accent);
  display: grid; place-items: center; color: var(--on-accent); font-weight: 800; font-size: 15px;
}
.brand-name { font-weight: 800; font-size: 16px; color: var(--on-accent); letter-spacing: 0.02em; }
.brand-sub { font-size: 11px; color: var(--sidebar-muted); }
.nav-group { margin-top: 12px; padding: 0 10px 4px; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: var(--sidebar-muted); }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--sidebar-text); font-weight: 600; font-size: 13.5px;
}
.nav a:hover { background: var(--sidebar-active); text-decoration: none; }
.nav a.active { background: var(--accent); color: var(--on-accent); }
.nav svg { width: 18px; height: 18px; flex: none; opacity: 0.9; }
.nav .badge { margin-left: auto; }
.sidebar-foot { margin-top: auto; padding: 12px 10px 0; font-size: 12px; color: var(--sidebar-muted); border-top: 1px solid var(--sidebar-active); }
.sidebar-foot button { background: none; border: 0; color: var(--sidebar-text); font: inherit; cursor: pointer; padding: 0; text-decoration: underline; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 28px; background: var(--surface);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.topbar .menu-btn { display: none; }
.crumbs { font-size: 13px; color: var(--muted); }
.crumbs b { color: var(--text); }
.content { padding: 24px 28px 48px; max-width: 1480px; width: 100%; }
.page-head { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head .sub { color: var(--muted); margin-top: 4px; }
.page-head .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }

/* -------------------------------------------------------------- elementi */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px 20px; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 14px 20px; border-bottom: 1px solid var(--line-2); }
.card-head .actions { margin-left: auto; display: flex; gap: 8px; }
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  height: 36px; padding: 0 14px; border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 700; font-size: 13.5px;
  cursor: pointer; white-space: nowrap; text-decoration: none !important;
  transition: background calc(120ms * var(--motion)), border-color calc(120ms * var(--motion));
}
.btn:hover { background: var(--surface-2); border-color: var(--faint); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; }
.btn-icon { width: 32px; padding: 0; height: 32px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { font-size: 12px; font-weight: 700; color: var(--muted); }
.field > span em { color: var(--danger); font-style: normal; }
.field small { color: var(--faint); font-size: 11.5px; }
.input, select.input, textarea.input {
  height: 36px; width: 100%; padding: 0 11px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit; font-size: 14px;
}
textarea.input { height: auto; min-height: 72px; padding: 9px 11px; resize: vertical; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input.num { text-align: right; font-variant-numeric: tabular-nums; }
.input-lg { height: 46px; font-size: 16px; }
.check { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 14px 16px; }
.c-2 { grid-column: span 2; } .c-3 { grid-column: span 3; } .c-4 { grid-column: span 4; } .c-5 { grid-column: span 5; }
.c-6 { grid-column: span 6; } .c-8 { grid-column: span 8; } .c-9 { grid-column: span 9; } .c-12 { grid-column: span 12; }
.section-title { grid-column: span 12; margin-top: 6px; padding-top: 14px; border-top: 1px solid var(--line-2); }
.section-title:first-child { margin-top: 0; padding-top: 0; border-top: 0; }

.badge {
  display: inline-flex; align-items: center; height: 22px; padding: 0 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 700; background: var(--line-2); color: var(--muted); white-space: nowrap;
}
.badge.accent { background: var(--accent-soft); color: var(--accent-ink); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }

.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th {
  text-align: left; font-size: 11.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em;
  padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface-2); white-space: nowrap;
}
.table td { padding: 10px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr.click { cursor: pointer; }
.table tbody tr.click:hover td { background: var(--surface-2); }
.table th.right, .table td.right { text-align: right; }
.table td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .strike td { color: var(--faint); text-decoration: line-through; }
.table tfoot td { font-weight: 800; background: var(--surface-2); }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.toolbar { display: flex; gap: 10px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line-2); flex-wrap: wrap; }
.toolbar .input { width: auto; }
.search { position: relative; flex: 1; min-width: 220px; }
.search .input { padding-left: 34px; width: 100%; }
.search > svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; color: var(--faint); pointer-events: none; }
.search .scan-btn { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); color: var(--accent-ink); }
.search .scan-btn svg { width: 22px; height: 22px; }
.search:has(.scan-btn) .input { padding-right: 48px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  height: 30px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  font: inherit; font-size: 12.5px; font-weight: 700; color: var(--muted); cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }

.stat { padding: 16px 18px; }
.stat .label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value { font-size: 26px; font-weight: 800; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat .hint { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.stat a.value { color: var(--text); display: block; }

/* ------------------------------------------------------- overlay & toast */
.modal-back {
  position: fixed; inset: 0; background: rgba(15, 23, 36, 0.45); display: grid; place-items: center; z-index: 100; padding: 20px;
}
.modal { width: min(560px, 100%); max-height: 92vh; overflow: auto; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); }
.modal.wide { width: min(920px, 100%); }
.modal-head { display: flex; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line-2); }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line-2); background: var(--surface-2); border-radius: 0 0 14px 14px; }
.toasts { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast {
  background: var(--text); color: var(--surface); padding: 11px 16px; border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  font-weight: 600; max-width: 420px; animation: toast-in calc(180ms * var(--motion)) ease-out;
}
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }
@keyframes toast-in { from { transform: translateY(calc(8px * var(--motion))); opacity: 0; } }

/* ------------------------------------------------- ricerca articoli (pop) */
.ac { position: relative; }
.ac-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); z-index: 50; max-height: 340px; overflow: auto;
}
.ac-item { display: flex; gap: 12px; align-items: center; padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--line-2); }
.ac-item:last-child { border-bottom: 0; }
.ac-item.on, .ac-item:hover { background: var(--accent-soft); }
.ac-item .ac-main { flex: 1; min-width: 0; }
.ac-item .ac-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ac-item .ac-meta { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------ editor documento */
.doc-layout { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.doc-side { position: sticky; top: 76px; display: flex; flex-direction: column; gap: 16px; }
.righe .input { height: 32px; padding: 0 8px; font-size: 13.5px; }
.righe td:nth-last-child(2) { white-space: nowrap; }
.righe td { padding: 6px 6px; }
.righe th { padding: 8px 6px; }
.righe .w-cod { width: 120px; min-width: 104px; }
.righe .w-desc { min-width: 200px; }
.righe .w-um { width: 60px; min-width: 56px; }
.righe .w-qta { width: 78px; min-width: 70px; }
.righe .w-prezzo { width: 100px; min-width: 88px; }
.righe .w-sc { width: 64px; min-width: 56px; }
.righe .w-iva { width: 90px; min-width: 84px; }
.righe tr.from-bolla td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
.totali { display: flex; flex-direction: column; gap: 8px; }
.totali div { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
.totali .big { font-size: 22px; font-weight: 800; padding-top: 8px; border-top: 1px solid var(--line); }
.linked a { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--line-2); font-weight: 600; }
.linked a:last-child { border-bottom: 0; }
.giac { font-size: 11.5px; font-weight: 700; }
.giac.no { color: var(--danger); } .giac.low { color: var(--warn); } .giac.ok { color: var(--ok); }

/* ------------------------------------------------------ scheda articolo */
.scheda-articolo { align-items: start; }
.scheda { margin: 10px 0 0; display: flex; flex-direction: column; }
.scheda > div { display: flex; justify-content: space-between; gap: 16px; padding: 8px 0; border-bottom: 1px solid var(--line-2); }
.scheda > div:last-child { border-bottom: 0; }
.scheda dt { color: var(--muted); }
.scheda dd { margin: 0; font-weight: 700; text-align: right; overflow-wrap: anywhere; }

/* ---------------------------------------------------------------- cassa */
.pos { display: grid; grid-template-columns: minmax(340px, 440px) minmax(0, 1fr); gap: 16px; height: calc(100vh - 110px); min-height: 560px; }
.pos-left { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.pos-display {
  background: var(--sidebar); color: var(--on-accent); border-radius: var(--radius); padding: 14px 18px;
  display: flex; flex-direction: column; align-items: flex-end; gap: 2px;
}
.pos-display .lbl { font-size: 11.5px; color: var(--sidebar-muted); align-self: flex-start; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; }
.pos-display .amt { font-size: 38px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; line-height: 1.1; }
.pos-display .qty { font-size: 13px; color: var(--sidebar-text); min-height: 18px; }
.keypad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.key {
  height: 58px; border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--surface);
  font: inherit; font-size: 22px; font-weight: 700; cursor: pointer; color: var(--text); user-select: none;
}
.key:active { transform: scale(calc(1 - 0.03 * var(--motion))); }
.key:hover { background: var(--surface-2); }
.key.fn { font-size: 15px; background: var(--surface-2); color: var(--muted); }
.key.clear { color: var(--danger); }
.reparti { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; overflow-y: auto; align-content: start; }
.reparto {
  min-height: 62px; border-radius: var(--radius-sm); border: 1px solid transparent; padding: 8px 10px; text-align: left;
  font: inherit; cursor: pointer; display: flex; flex-direction: column; justify-content: space-between; color: var(--text);
}
.reparto b { font-size: 13.5px; line-height: 1.2; }
.reparto small { font-size: 11.5px; font-weight: 700; color: var(--muted); }
.reparto:hover { border-color: var(--faint); }
.tint-blu { background: var(--tint-blu); } .tint-rosso { background: var(--tint-rosso); } .tint-turchese { background: var(--tint-turchese); }
.tint-grigio { background: var(--tint-grigio); } .tint-verde { background: var(--tint-verde); } .tint-arancio { background: var(--tint-arancio); }
.tint-viola { background: var(--tint-viola); } .tint-giallo { background: var(--tint-giallo); }
.pos-right { display: flex; flex-direction: column; min-height: 0; }
.pos-right .card { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.cart { flex: 1; overflow-y: auto; }
.cart-row { display: grid; grid-template-columns: 1fr auto auto auto; gap: 12px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line-2); }
.cart-row.sel { background: var(--accent-soft); }
.cart-row .d { min-width: 0; }
.cart-row .d b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cart-row .d small { color: var(--muted); }
.cart-row .q { display: flex; align-items: center; gap: 4px; }
.cart-row .q span { min-width: 34px; text-align: center; font-weight: 800; font-variant-numeric: tabular-nums; }
.cart-row .t { font-weight: 800; min-width: 86px; text-align: right; font-variant-numeric: tabular-nums; }
.pos-total { padding: 14px 16px; border-top: 1px solid var(--line); display: flex; align-items: baseline; gap: 12px; }
.pos-total .big { margin-left: auto; font-size: 34px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.pay { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 0 16px 16px; }
.pay button {
  height: 72px; border-radius: var(--radius); border: 0; font: inherit; font-size: 20px; font-weight: 800; letter-spacing: 0.04em;
  cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px;
}
.pay button svg { width: 26px; height: 26px; }
.pay .cash { background: var(--ok); color: var(--on-accent); }
.pay .card-pay { background: var(--accent); color: var(--on-accent); }
.pay button:disabled { opacity: 0.4; cursor: not-allowed; }

/* ------------------------------------------------------------- accesso */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: var(--sidebar); }
.login { width: min(400px, 100%); }
.login .brand { padding: 0 0 18px; }
.login .brand-name { color: var(--text); }

/* ------------------------------------------------------------ responsive */
@media (max-width: 1100px) {
  .doc-layout { grid-template-columns: 1fr; }
  .doc-side { position: static; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .pos { grid-template-columns: minmax(0, 1fr); height: auto; }
  .pos-right .card { min-height: 420px; }
}
.nav-backdrop, .bottom-nav, .mobile-bar { display: none; }
.cella { display: contents; }

/* --------------------------------------------------------------- telefono
   Sotto 820px: menu a cassetto + barra in basso, una colonna, campi a 16px
   (niente zoom automatico su iPhone), bersagli tattili da almeno 42px. */
@media (max-width: 820px) {
  :root { --bottom-nav: 60px; }
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: min(280px, 84vw); z-index: 90;
    transform: translateX(-100%); transition: transform calc(180ms * var(--motion));
    padding-top: calc(18px + env(safe-area-inset-top)); padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
  .app.nav-open .sidebar { transform: none; box-shadow: var(--shadow-lg); }
  .nav-backdrop { position: fixed; inset: 0; z-index: 80; background: rgba(15, 23, 36, 0.45); }
  .app.nav-open .nav-backdrop { display: block; }
  .nav a { padding: 11px 10px; }

  .topbar { padding: calc(10px + env(safe-area-inset-top)) 12px 10px; gap: 6px; }
  .topbar .menu-btn { display: inline-flex; }
  .crumbs { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .content { padding: 14px 12px calc(var(--bottom-nav) + 28px + env(safe-area-inset-bottom)); }

  .bottom-nav {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
    height: calc(var(--bottom-nav) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--line);
  }
  .bottom-nav a, .bottom-nav button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    font: inherit; font-size: 11px; font-weight: 700; color: var(--muted); background: none; border: 0; text-decoration: none;
  }
  .bottom-nav svg { width: 22px; height: 22px; }
  .bottom-nav a.active { color: var(--accent-ink); }

  h1 { font-size: 21px; }
  .page-head { gap: 10px; margin-bottom: 14px; }
  .page-head > div:first-child { min-width: 0; }
  .page-head .actions { margin-left: 0; width: 100%; }
  .page-head .actions .btn { flex: 1 1 auto; }

  .input, select.input { height: 44px; font-size: 16px; }
  textarea.input { font-size: 16px; }
  .input-lg { height: 50px; }
  .btn { height: 42px; }
  .btn-sm { height: 36px; }
  .btn-icon { width: 40px; height: 40px; }

  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 12px 14px; }
  .stat .value { font-size: 21px; }
  .form-grid > * { grid-column: span 12; }
  .card-pad { padding: 14px; }
  .card-head { padding: 12px 14px; flex-wrap: wrap; }

  .toolbar { padding: 10px 12px; }
  .toolbar > .input, .toolbar > label.row { flex: 1 1 140px; }
  .toolbar .input { width: 100%; }
  .search { min-width: 100%; }
  .chips { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; max-width: 100%; padding-bottom: 2px; }
  .chips::-webkit-scrollbar { display: none; }
  .chip { flex: none; height: 34px; }

  /* Finestre come pannello dal basso */
  .modal-back { align-items: end; padding: 0; }
  .modal, .modal.wide { width: 100%; max-height: 92vh; border-radius: 16px 16px 0 0; }
  .modal-foot { border-radius: 0; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .modal-foot .btn { flex: 1; }
  .toasts { left: 10px; right: 10px; top: calc(58px + env(safe-area-inset-top)); bottom: auto; z-index: 210; }
  .toast { max-width: none; padding: 9px 12px; font-size: 13px; }
  .ac-list { max-height: 50vh; }

  /* Editor documenti: totale e Salva sempre a portata di pollice */
  .doc-layout { grid-template-columns: 1fr; }
  .doc-side { position: static; }
  .mobile-bar {
    display: flex; align-items: center; gap: 12px; position: sticky; z-index: 30;
    bottom: calc(var(--bottom-nav) + env(safe-area-inset-bottom) + 8px);
    margin-top: 14px; padding: 10px 12px 10px 16px; border-radius: var(--radius);
    background: var(--sidebar); color: var(--on-accent); box-shadow: var(--shadow-lg);
  }
  .mobile-bar .tot { flex: 1; font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
  .content:has(.mobile-bar) .page-head #save { display: none; }
  .hide-sm { display: none; }
  .mobile-bar .tot small { display: block; font-size: 11px; font-weight: 600; color: var(--sidebar-muted); }

  /* Cassa: carrello e pagamento in alto, tastierino e reparti sotto */
  .pos { grid-template-columns: minmax(0, 1fr); height: auto; min-height: 0; gap: 12px; }
  .pos-right { order: -1; }
  .pos-right .card { min-height: 0; }
  .cart { max-height: 42vh; min-height: 120px; }
  .cart .empty { padding: 18px 12px; }
  .cart-row { grid-template-columns: 1fr auto auto; gap: 8px; padding: 10px 12px; }
  .cart-row .t { grid-column: 1; grid-row: 2; text-align: left; min-width: 0; }
  .cart-row .q { grid-column: 2 / 4; grid-row: 2; justify-content: flex-end; }
  .cart-row .q .btn { width: 40px; height: 40px; }
  .pos-total { padding: 10px 14px; }
  .pos-total .big { font-size: 28px; }
  .pay { position: sticky; bottom: calc(var(--bottom-nav) + env(safe-area-inset-bottom)); background: var(--surface); padding: 10px 12px 12px; gap: 10px; border-radius: 0 0 var(--radius) var(--radius); }
  .pay button { height: 58px; font-size: 17px; }
  .pos-display { padding: 10px 14px; }
  .pos-display .amt { font-size: 32px; }
  .keypad { gap: 6px; }
  .key { height: 52px; font-size: 20px; }
  .reparti { grid-template-columns: repeat(2, 1fr); }
}

/* Tabelle come schede su schermi stretti: intestazione di colonna a sinistra,
   valore a destra (etichette impostate da app.js). */
@media (max-width: 640px) {
  .table-wrap { overflow-x: visible; }
  table.table, .table tbody, .table tfoot, .table tr, .table td { display: block; width: 100%; }
  .table thead { display: none; }
  .table tbody tr { padding: 10px 14px; border-bottom: 1px solid var(--line-2); }
  .table tbody tr:last-child { border-bottom: 0; }
  .table td { border: 0; padding: 3px 0; display: flex; justify-content: space-between; align-items: baseline; gap: 12px; text-align: right; min-height: 0; }
  .table td > .cella { display: block; min-width: 0; overflow-wrap: anywhere; }
  .table td.vuota { display: none; }
  .table td[data-label]::before {
    content: attr(data-label); flex: none; max-width: 45%; text-align: left;
    font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted);
  }
  .table td[data-label=""]::before { content: none; }
  .table td[data-label=""] { justify-content: flex-start; text-align: left; }
  .table td:first-child { text-align: left; }
  .table td.empty, .table td[colspan] { display: block; text-align: center; }
  .table td.empty::before, .table td[colspan]::before { content: none; }
  .table td.num { white-space: normal; }
  .table tbody tr.click:hover td { background: none; }
  .table tbody tr.click:active { background: var(--surface-2); }
  .table tfoot tr { padding: 10px 14px; }
  .table tfoot td { background: none; }
  .table tfoot td:empty { display: none; }

  /* Righe dei documenti: ogni riga è una scheda con i campi a griglia */
  .righe tbody tr { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 10px; padding: 12px 14px; position: relative; }
  .righe td { display: flex; flex-direction: column; align-items: stretch; gap: 4px; padding: 0; text-align: left; width: auto; min-width: 0 !important; }
  .righe td[data-label]::before { max-width: none; }
  .righe td > .cella { text-align: left; }
  .righe td.vuota { display: flex; }
  .righe td.w-desc { grid-column: 1 / -1; order: -2; padding-right: 44px; }
  .righe td.w-cod { order: -3; grid-column: 1 / -1; padding-right: 44px; }
  .righe td.w-cod::before { content: none; }
  .righe td:last-child { position: absolute; top: 8px; right: 8px; width: auto; }
  .righe td:last-child::before { content: none; }
  .righe td:nth-last-child(2) { grid-column: 1 / -1; flex-direction: row; justify-content: space-between; align-items: center; border-top: 1px dashed var(--line); padding-top: 8px; }
  .righe .input { height: 44px; font-size: 16px; }
  .righe tr.from-bolla td:first-child { box-shadow: none; }
  .righe tr.from-bolla { box-shadow: inset 3px 0 0 var(--accent); }
}

/* -------------------------------------------------------------- stampa */
@media print {
  .sidebar, .topbar, .page-head .actions, .no-print { display: none !important; }
  .app { display: block; }
  body { background: var(--surface); }
}
