/* ---------------------------------------------------------------------
   Token di design
   - Un solo family di sistema (nessun webfont esterno: sul contatore di
     un negozio, con connessioni a volte lente, ogni richiesta di rete in
     più si sente. La personalità viene da scala/peso, non dal font).
   - Due soli accenti "funzionali": verde per la vendita, blu per il
     ricarico. Tutto il resto resta in scala di grigi/inchiostro.
------------------------------------------------------------------------ */
:root {
  --ink: #14181f;
  --ink-muted: #5b6472;
  --ink-faint: #9098a6;
  --surface: #ffffff;
  --bg: #f5f6f8;
  --border: #e1e4e9;

  --sale: #0f6b4c;
  --sale-dark: #0b5039;
  --restock: #0b4f6c;
  --restock-dark: #083b52;

  --danger: #b3261e;
  --danger-bg: #fbeae9;
  --success-bg: #e7f4ee;
  --success-ink: #0b5039;

  --radius-lg: 22px;
  --radius-md: 12px;
  --radius-sm: 8px;
}

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

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 17px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

button, input {
  font-family: inherit;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------------------------------------------------------------------
   Struttura schermate
------------------------------------------------------------------------ */
.screen {
  display: none;
  position: relative;
  min-height: 100vh;
  min-height: 100dvh;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

.message-banner {
  display: none;
  position: absolute;
  top: max(24px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  max-width: min(92vw, 420px);
  padding: 14px 18px;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.92rem;
  text-align: center;
  box-shadow: 0 8px 24px rgba(20, 24, 31, 0.16);
}
.message-banner.success { display: block; background: var(--success-bg); color: var(--success-ink); }
.message-banner.error { display: block; background: var(--danger-bg); color: var(--danger); }

.screen-scanner .message-banner {
  top: max(140px, calc(env(safe-area-inset-top) + 130px));
}

.batch-toggle-btn {
  position: absolute;
  top: max(96px, calc(env(safe-area-inset-top) + 86px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 9;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  min-width: unset;
}
.batch-toggle-btn.active { background: var(--sale); }
.batch-toggle-btn[hidden] { display: none; }

/* ---------------------------------------------------------------------
   Login
------------------------------------------------------------------------ */
.screen-login {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 32px;
  text-align: center;
}

.brand {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0 0 4px;
}

.subtitle {
  color: var(--ink-muted);
  margin: 0 0 28px;
  font-size: 0.92rem;
}

.field-label {
  display: block;
  text-align: left;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-muted);
  margin-bottom: 6px;
}

input[type="password"],
input[type="number"] {
  font-size: 1.15rem;
  padding: 14px 16px;
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--ink);
  margin-bottom: 20px;
}
input:focus { outline: none; border-color: var(--ink); background: var(--surface); }

.btn-primary {
  width: 100%;
  background: var(--ink);
  color: #fff;
  font-size: 1.05rem;
  font-weight: 600;
  padding: 16px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.btn-primary:active { background: #000; }

/* ---------------------------------------------------------------------
   Scanner: la fotocamera è il vero protagonista, a schermo intero
------------------------------------------------------------------------ */
.screen-scanner {
  background: #05070a;
  color: #fff;
}

.topbar {
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(16px, env(safe-area-inset-top)) 16px 44px;
  background: linear-gradient(to bottom, rgba(5, 7, 10, 0.82), rgba(5, 7, 10, 0));
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.topbar-actions {
  display: flex;
  gap: 8px;
}

.mode-toggle {
  display: flex;
  gap: 4px;
  background: rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  padding: 4px;
}

.mode-btn {
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.78);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 999px;
  min-width: unset;
  cursor: pointer;
}
.mode-btn.active.mode-btn-sale { background: var(--sale); color: #fff; }
.mode-btn.active.mode-btn-restock { background: var(--restock); color: #fff; }

.btn-icon-text {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: none;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  min-width: unset;
  cursor: pointer;
}

#scanner-container {
  position: relative;
  flex: 1;
  min-height: 0; /* bug noto di Safari/iOS: senza questo, un flex-item con
                    dentro un <video> può ricevere un'altezza sbagliata
                    invece di riempire lo spazio disponibile */
  overflow: hidden;
  background: #000;
}

#scanner-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Mirino + testo di aiuto: un unico blocco centrato con flex, così il
   testo resta sempre subito sotto il riquadro qualunque sia l'orientamento
   e l'altezza disponibile, invece di due elementi posizionati a distanza
   fissa dai bordi (che in orizzontale finivano per sovrapporsi). */
.scan-guide {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  pointer-events: none;
}

.scan-frame {
  position: relative;
  width: min(78vw, 55vh, 420px);
  aspect-ratio: 1 / 1;
  flex-shrink: 0;
}
.scan-frame .corner {
  position: absolute;
  width: 34px;
  height: 34px;
  border: 3px solid #fff;
  transition: border-color 0.15s ease, opacity 0.15s ease;
}
.corner-tl { top: 0; left: 0; border-right: none; border-bottom: none; border-top-left-radius: 14px; }
.corner-tr { top: 0; right: 0; border-left: none; border-bottom: none; border-top-right-radius: 14px; }
.corner-bl { bottom: 0; left: 0; border-right: none; border-top: none; border-bottom-left-radius: 14px; }
.corner-br { bottom: 0; right: 0; border-left: none; border-top: none; border-bottom-right-radius: 14px; }

/* Conferma immediata alla lettura riuscita del codice: risponde
   all'azione dell'operatore (ha appena inquadrato bene), non decorazione. */
.scan-frame.detected .corner { border-color: var(--sale); }

/* Durante la ricerca del prodotto, il mirino si attenua: segnala che non
   serve più tenere inquadrato il codice, senza bisogno di leggere il testo. */
.scan-frame.paused { opacity: 0.35; }

/* Icona di conferma lettura: un cerchio con la spunta che compare per un
   secondo e mezzo appena il codice viene letto, PRIMA ancora di sapere se
   il prodotto verrà trovato. Serve a rispondere subito alla domanda
   "è stato letto?", che altrimenti restava senza risposta finché non
   arrivava l'esito della ricerca. Mentre è visibile, la lettura resta in
   pausa: sparisce e la scansione riprende subito dopo. */
.scan-confirm-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.6);
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--sale);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
  z-index: 12;
}
.scan-confirm-icon.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}.scan-hint {
  color: rgba(255, 255, 255, 0.92);
  font-size: 0.92rem;
  font-weight: 500;
  background: rgba(0, 0, 0, 0.4);
  padding: 8px 16px;
  border-radius: 999px;
  margin: 0;
  white-space: nowrap;
}

#scanner-loading {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(136px, calc(env(safe-area-inset-bottom) + 116px));
  display: none;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.88rem;
  margin: 0;
  z-index: 6;
}
#scanner-loading.visible { display: inline-flex; }
/* Piccolo anello animato: conferma visivamente che "sta succedendo qualcosa"
   mentre si attende la risposta, anche quando la rete è un po' lenta. */
#scanner-loading::before {
  content: '';
  width: 13px;
  height: 13px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: nbb-spin 0.7s linear infinite;
}
@keyframes nbb-spin {
  to { transform: rotate(360deg); }
}

.batch-summary-btn {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(78px, calc(env(safe-area-inset-bottom) + 58px));
  z-index: 8;
  background: var(--sale);
  color: #fff;
  border: none;
  padding: 12px 22px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  min-width: unset;
  box-shadow: 0 6px 16px rgba(15, 107, 76, 0.4);
}
.batch-summary-btn[hidden] { display: none; }

.manual-entry-btn {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 8;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  border: none;
  padding: 12px 20px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  min-width: unset;
}
.manual-entry-btn:disabled { opacity: 0.5; cursor: default; }

.manual-entry-overlay {
  position: absolute;
  inset: 0;
  z-index: 30;
  background: rgba(5, 7, 10, 0.6);
  display: flex;
  align-items: flex-end;
}
.manual-entry-overlay[hidden] { display: none; }

.manual-entry-sheet {
  background: var(--surface);
  color: var(--ink);
  width: 100%;
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  padding: 28px 24px max(24px, env(safe-area-inset-bottom));
}
.manual-entry-sheet input[type="text"] {
  font-size: 1.1rem;
  padding: 14px 16px;
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--ink);
  margin-bottom: 16px;
}
.manual-entry-sheet input:focus { outline: none; border-color: var(--ink); background: var(--surface); }
.manual-entry-sheet .btn-primary { margin-bottom: 8px; }

/* ---------------------------------------------------------------------
   Scheda prodotto: un foglio ancorato in basso, azione a portata di
   pollice — pensato per l'uso con una mano sola al banco.
------------------------------------------------------------------------ */
.screen-product {
  justify-content: flex-end;
  background: var(--bg);
}

.product-sheet {
  background: var(--surface);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  box-shadow: 0 -12px 32px rgba(20, 24, 31, 0.12);
  padding: 28px 24px max(24px, env(safe-area-inset-bottom));
  width: 100%;
}

.product-media {
  width: 100%;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--bg);
  margin-bottom: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.product-media img { width: 100%; height: 100%; object-fit: contain; display: none; }
.product-media img[src] { display: block; }

.product-info { margin-bottom: 24px; }
.product-name { font-size: 1.25rem; font-weight: 700; letter-spacing: -0.01em; margin-bottom: 4px; }
.product-sku { color: var(--ink-muted); font-size: 0.88rem; margin-bottom: 10px; font-variant-numeric: tabular-nums; }
.product-stock {
  display: inline-block;
  font-size: 0.92rem;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--bg);
  color: var(--ink-muted);
}
.product-stock.low { background: var(--danger-bg); color: var(--danger); }

.btn-action {
  width: 100%;
  border: none;
  border-radius: var(--radius-md);
  padding: 20px;
  font-size: 1.1rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #fff;
  cursor: pointer;
  margin-bottom: 12px;
}
.btn-action-sale { background: var(--sale); }
.btn-action-sale:active { background: var(--sale-dark); }
.btn-action-restock { background: var(--restock); }
.btn-action-restock:active { background: var(--restock-dark); }
.btn-action:disabled { background: #c7ccd4; cursor: default; }

.btn-text {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--ink-muted);
  font-size: 0.92rem;
  padding: 10px;
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
}
.btn-text:disabled { color: var(--ink-faint); text-decoration: none; cursor: default; }

.restock-form #restock-quantity {
  text-align: center;
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------
   Indicatori di stato (offline / coda)
------------------------------------------------------------------------ */
.offline-badge,
.queue-badge {
  position: fixed;
  top: max(78px, calc(env(safe-area-inset-top) + 68px));
  z-index: 15;
  color: #fff;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  display: none;
}
.offline-badge { right: 16px; background: var(--danger); }
.queue-badge { left: 16px; background: #3a4250; }
.offline-badge.visible, .queue-badge.visible { display: block; }

.issues-badge {
  position: fixed;
  top: max(112px, calc(env(safe-area-inset-top) + 102px));
  left: 16px;
  z-index: 15;
  color: #fff;
  background: #b3261e;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 700;
  display: none;
  cursor: pointer;
  border: none;
}
.issues-badge.visible { display: block; }

/* ---------------------------------------------------------------------
   Riepilogo scansione multipla
------------------------------------------------------------------------ */
.screen-batch {
  background: var(--bg);
}

.batch-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(16px, env(safe-area-inset-top)) 16px 12px;
}
.batch-title { font-size: 1.05rem; font-weight: 700; margin: 0; color: var(--ink); }
.screen-batch .btn-icon-text { background: var(--border); color: var(--ink); }

.screen-batch .message-banner {
  top: max(76px, calc(env(safe-area-inset-top) + 66px));
}

.batch-progress-banner {
  margin: 0 16px 12px;
  padding: 12px 16px;
  background: var(--success-bg);
  color: var(--success-ink);
  border-radius: var(--radius-md);
  font-size: 0.88rem;
  font-weight: 600;
  text-align: center;
}

.batch-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.batch-empty {
  display: none;
  text-align: center;
  color: var(--ink-muted);
  margin-top: 60px;
  padding: 0 24px;
}

.batch-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
}

.batch-row-image {
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  background: var(--bg);
  object-fit: contain;
  flex-shrink: 0;
  display: none;
}
.batch-row-image[src] { display: block; }

.batch-row-info { flex: 1; min-width: 0; }
.batch-row-name {
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.25;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.batch-row-sku { color: var(--ink-muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.batch-row-warning { color: var(--danger); font-size: 0.78rem; font-weight: 600; margin-top: 2px; }

.batch-row-qty {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.qty-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--bg);
  color: var(--ink);
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  min-width: unset;
  padding: 0;
  line-height: 1;
}
.qty-btn:active { background: var(--border); }
.qty-value {
  min-width: 22px;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.batch-row-remove {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--ink-faint);
  font-size: 1.1rem;
  cursor: pointer;
  min-width: unset;
  padding: 0;
}
.batch-row-remove:active { color: var(--danger); }

.batch-footer {
  padding: 14px 16px max(14px, env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.batch-total {
  text-align: center;
  color: var(--ink-muted);
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 10px;
}

/* ---------------------------------------------------------------------
   Storico operazioni
------------------------------------------------------------------------ */
.history-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 12px;
}

.history-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-top: 6px;
  flex-shrink: 0;
}
.history-status-dot.completed { background: var(--sale); }
.history-status-dot.failed { background: var(--danger); }
.history-status-dot.pending { background: #d9a300; }

.history-info { flex: 1; min-width: 0; }
.history-name {
  font-weight: 600;
  font-size: 0.92rem;
  line-height: 1.25;
  margin-bottom: 2px;
}
.history-meta {
  color: var(--ink-muted);
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
}
.history-qty {
  font-size: 0.85rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
  text-align: right;
  white-space: nowrap;
}
.history-qty .arrow { color: var(--ink-faint); font-weight: 400; margin: 0 2px; }
