/* ---------- Token ---------- */
:root {
  --bg:        #f6f7f8;
  --surface:   #ffffff;
  --surface-2: #eef0f2;
  --text:      #16191d;
  --muted:     #6b7280;
  --border:    #dfe3e8;
  --accent:    #0f766e;
  --accent-ink:#ffffff;

  --danger:  #b42318;  --danger-bg:  #fee4e2;
  --warn:    #b54708;  --warn-bg:    #fef0c7;
  --soon:    #854d0e;  --soon-bg:    #fef9c3;
  --ok:      #14602c;  --ok-bg:      #dcfce7;

  --radius: 14px;
  --tabbar-h: 62px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #101214;
    --surface:   #191c1f;
    --surface-2: #23272b;
    --text:      #e9ecef;
    --muted:     #98a1ab;
    --border:    #2c3136;
    --accent:    #2dd4bf;
    --accent-ink:#06211f;

    --danger:  #ff9a91;  --danger-bg:  #3d1512;
    --warn:    #fdb022;  --warn-bg:    #3a2408;
    --soon:    #fde047;  --soon-bg:    #392f08;
    --ok:      #6ee7a0;  --ok-bg:      #0c2a17;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* L'attributo hidden vale "display: none" solo come valore predefinito:
   qualsiasi regola che imposti display su una classe lo batterebbe, e
   l'elemento resterebbe visibile. Qui si rende hidden davvero definitivo,
   una volta per tutte invece che caso per caso. */
[hidden] { display: none !important; }

body {
  margin: 0;
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b));
  overscroll-behavior-y: contain;
}

h1, h2 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 20px; }
h2 { font-size: 16px; margin-bottom: 4px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- Topbar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  padding: calc(var(--safe-t) + 12px) 16px 12px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
}
.icon-btn {
  border: none; background: none; color: var(--muted);
  font-size: 22px; line-height: 1; padding: 6px; cursor: pointer;
}

/* ---------- Viste ---------- */
.view { display: none; padding: 14px 16px 24px; }
.view.active { display: block; }

/* ---------- Riepilogo ---------- */
.summary { display: flex; gap: 8px; margin-bottom: 14px; }
.sum-card {
  flex: 1; text-align: center; padding: 10px 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.sum-card b { display: block; font-size: 22px; line-height: 1.1; }
.sum-card span { font-size: 11px; color: var(--muted); }
.sum-card.is-danger b { color: var(--danger); }
.sum-card.is-warn b   { color: var(--warn); }

/* ---------- Filtri ---------- */
.filterbar { margin-bottom: 12px; }
input[type="search"] {
  width: 100%; padding: 11px 14px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--surface); color: var(--text);
}

/* ---------- Liste ---------- */
.gruppo-head {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted);
  margin: 18px 0 8px;
}
.gruppo-head:first-child { margin-top: 0; }

.riga {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px;
}
.prod-img {
  width: 46px; height: 46px; flex: none; border-radius: 9px;
  object-fit: contain; background: var(--surface-2);
}
.prod-img.lg { width: 60px; height: 60px; }
.riga-body { flex: 1; min-width: 0; }
.riga-nome {
  font-weight: 600; font-size: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.riga-sub {
  font-size: 13px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.pill {
  display: inline-block; padding: 4px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 650; white-space: nowrap;
  flex: none;                 /* non si comprime: il nome cede spazio, non la pastiglia */
}
.pill.scaduto { background: var(--danger-bg); color: var(--danger); }
.pill.critico { background: var(--warn-bg);   color: var(--warn); }
.pill.vicino  { background: var(--soon-bg);   color: var(--soon); }
.pill.ok      { background: var(--ok-bg);     color: var(--ok); }

.qta-badge {
  display: inline-block; margin-top: 4px;
  font-size: 12px; color: var(--muted);
}
.riga-side { text-align: right; flex: none; }
.riga-del {
  border: none; background: none; color: var(--muted);
  font-size: 20px; padding: 4px 2px 4px 8px; cursor: pointer; flex: none;
}

.empty { text-align: center; color: var(--muted); padding: 48px 20px; }
.empty .big-ic { font-size: 40px; display: block; margin-bottom: 10px; opacity: .5; }

/* ---------- Scanner ---------- */
.scanner-wrap {
  position: relative; aspect-ratio: 4/3; width: 100%;
  background: #000; border-radius: var(--radius); overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
#video { width: 100%; height: 100%; object-fit: cover; }
.scanner-overlay {
  position: absolute; inset: 14% 8%;
  border: 2px solid rgba(255,255,255,.7); border-radius: 10px;
  pointer-events: none;
}
.scanner-line {
  position: absolute; left: 0; right: 0; height: 2px;
  background: #2dd4bf; box-shadow: 0 0 8px #2dd4bf;
  animation: sweep 2s ease-in-out infinite;
}
@keyframes sweep { 0%,100% { top: 4%; } 50% { top: 96%; } }
@media (prefers-reduced-motion: reduce) { .scanner-line { animation: none; top: 50%; } }

.scanner-hint {
  position: absolute; bottom: 10px; left: 0; right: 0;
  text-align: center; color: #fff; font-size: 14px; margin: 0;
  text-shadow: 0 1px 4px rgba(0,0,0,.8);
}
.scanner-actions { margin-top: 16px; display: grid; gap: 10px; }

/* ---------- Bottoni ---------- */
.btn {
  padding: 11px 16px; font-size: 15px; font-weight: 600;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--surface); color: var(--text); cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost   { background: transparent; }
.btn.big     { padding: 15px; font-size: 17px; }
.btn.wide    { width: 100%; margin-top: 10px; }
.btn:active  { opacity: .75; }

/* ---------- Card ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
}
.row { display: flex; gap: 8px; align-items: center; margin-top: 10px; flex-wrap: wrap; }
.row input[type="number"] {
  width: 90px; padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); color: var(--text);
}
.unit { color: var(--muted); font-size: 14px; }
.check { display: flex; align-items: center; gap: 9px; margin-top: 12px; font-size: 15px; }
.check input { width: 19px; height: 19px; accent-color: var(--accent); }

/* ---------- Soglie ---------- */
.soglie { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.soglia-chip {
  display: flex; align-items: center; gap: 7px;
  background: var(--surface-2); border-radius: 999px;
  padding: 6px 8px 6px 13px; font-size: 14px; font-weight: 600;
}
.soglia-chip button {
  border: none; background: none; color: var(--muted);
  font-size: 17px; line-height: 1; cursor: pointer; padding: 0 2px;
}

/* ---------- Sheet ---------- */
.sheet {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-panel {
  background: var(--bg); width: 100%; max-width: 520px;
  border-radius: 20px 20px 0 0;
  padding: 20px 18px calc(20px + var(--safe-b));
  max-height: 92vh; overflow-y: auto;
  animation: rise .22s ease-out;
}
@keyframes rise { from { transform: translateY(24px); opacity: .6; } }
@media (prefers-reduced-motion: reduce) { .sheet-panel { animation: none; } }

.sheet-head { display: flex; gap: 13px; align-items: center; margin-bottom: 18px; }
.sheet-head-txt { flex: 1; min-width: 0; }
.nome-input {
  width: 100%; font-size: 17px; font-weight: 650;
  border: none; border-bottom: 1px dashed var(--border);
  background: none; color: var(--text); padding: 3px 0;
}

.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input[type="date"],
.field input[type="text"] {
  width: 100%; padding: 12px 14px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--surface); color: var(--text);
}

.quick-dates { display: flex; flex-wrap: wrap; gap: 7px; margin: -8px 0 16px; }
.quick-dates button {
  padding: 7px 12px; font-size: 13px; font-weight: 600;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text); cursor: pointer;
}

.stepper { display: flex; align-items: center; gap: 0; }
.stepper button {
  width: 48px; height: 48px; font-size: 22px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); cursor: pointer;
}
.stepper button:first-child { border-radius: 11px 0 0 11px; }
.stepper button:last-child  { border-radius: 0 11px 11px 0; }
.stepper input {
  width: 74px; height: 48px; text-align: center; font-size: 17px; font-weight: 600;
  border: 1px solid var(--border); border-left: none; border-right: none;
  background: var(--surface); color: var(--text);
}

.esistenti { margin-bottom: 16px; }
.esistenti .gruppo-head { margin-top: 0; }

.sheet-actions { display: flex; gap: 9px; justify-content: flex-end; margin-top: 4px; }
.sheet-actions .btn { flex: 1; }

textarea {
  width: 100%; padding: 13px; font-size: 14px; line-height: 1.5;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--surface); color: var(--text); resize: vertical;
  margin: 10px 0;
}

/* ---------- Tabbar ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 2px;
  border: none; background: none; cursor: pointer;
  color: var(--muted); font-size: 11px; font-weight: 600;
}
.tab .ic { font-size: 20px; line-height: 1; }
.tab.active { color: var(--accent); }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 80;
  background: var(--text); color: var(--bg);
  padding: 11px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
}

/* ---------- Codice inserito a mano / lettore Bluetooth ---------- */
.codice-manuale { margin-top: 16px; }
.codice-manuale .row { flex-wrap: nowrap; }
.codice-manuale input[type="text"] {
  flex: 1; min-width: 0;
  padding: 11px 14px; font-size: 16px;
  letter-spacing: .04em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--bg); color: var(--text);
}

/* ================= Account, liste e permessi ================= */

/* ---------- Selettore della lista nella barra in alto ---------- */
.topbar-txt { min-width: 0; }
.lista-scelta {
  display: flex; align-items: center; gap: 5px;
  border: none; background: none; padding: 1px 0 0;
  color: var(--muted); font-size: 13px; font-weight: 600;
  cursor: pointer; max-width: 60vw;
}
.lista-scelta #listaNome {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lista-scelta .freccia { font-size: 10px; flex: none; }

/* ---------- Schermata di accesso ---------- */
.accesso {
  position: fixed; inset: 0; z-index: 90;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 24px 20px calc(24px + var(--safe-b));
  overflow-y: auto;
}

.accesso-box { width: 100%; max-width: 380px; }
.accesso-logo {
  width: 62px; height: 62px; border-radius: 15px;
  display: block; margin: 0 auto 14px;
}
.accesso-titolo { text-align: center; font-size: 24px; margin-bottom: 4px; }
.accesso-sotto { text-align: center; margin: 0 0 22px; font-size: 15px; }
.accesso-form .field { margin-bottom: 13px; }
.accesso-form input {
  width: 100%; padding: 12px 14px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 11px;
  background: var(--surface); color: var(--text);
}

.accesso-errore {
  background: var(--danger-bg); color: var(--danger);
  padding: 10px 13px; border-radius: 10px;
  font-size: 14px; margin: 0 0 13px;
}

.google-btn {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  margin-top: 10px;
}

.accesso-piede {
  display: flex; flex-direction: column; align-items: center; gap: 11px;
  margin-top: 22px;
}
.link-btn {
  border: none; background: none; color: var(--accent);
  font-size: 14px; font-weight: 600; cursor: pointer; padding: 2px;
}

.invito-banner {
  background: var(--ok-bg); color: var(--ok);
  padding: 12px 14px; border-radius: 11px;
  font-size: 14px; margin-bottom: 18px; text-align: center;
}

/* ---------- Liste e membri ---------- */
.riga.selezionata { border-color: var(--accent); border-width: 2px; }

.ruolo-tag {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 650;
  background: var(--surface-2); color: var(--muted);
}
.ruolo-tag.proprietario { background: var(--ok-bg); color: var(--ok); }

.select {
  flex: 1; min-width: 0;
  padding: 10px 12px; font-size: 15px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
}

.invito-creato {
  margin-top: 12px; padding: 14px;
  background: var(--surface-2); border-radius: 11px;
}
.invito-codice {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 23px; font-weight: 700; letter-spacing: .14em;
  text-align: center; margin: 6px 0 12px; color: var(--accent);
}

.btn.pericolo { color: var(--danger); }

/* Chi può solo guardare non vede comandi che non potrebbe usare. */
body.sola-lettura .riga-del,
body.sola-lettura .tab[data-view="scansiona"] { display: none; }

/* ---------- Foto del prodotto ---------- */
.foto-btn {
  display: inline-block; margin-top: 5px; padding: 0;
  font-size: 13px; text-align: left;
}
.foto-btn:disabled { color: var(--muted); }

/* L'immagine nel pannello e' anche il bersaglio piu' grande su cui
   toccare per sostituire la foto. */
#lottoImg { cursor: pointer; }

/* ---------- Conferma della registrazione ---------- */
.conferma {
  margin-top: 16px; padding: 16px;
  background: var(--ok-bg); border-radius: 12px;
  font-size: 14px; line-height: 1.5;
}
.conferma p { margin: 0 0 14px; color: var(--ok); }
.conferma .field > span { color: var(--ok); opacity: .85; }
.conferma input[type="text"] {
  flex: 1; min-width: 0;
  padding: 11px 14px; font-size: 20px; letter-spacing: .28em;
  text-align: center; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
}
.conferma .link-btn { margin-top: 4px; }
