:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #14181f;
  --muted: #5f6673;
  --border: #dde0e6;
  --primary: #1f6feb;
  --ok: #138a43;
  --ok-bg: #dff5e7;
  --err: #c62828;
  --err-bg: #fde4e4;
  --warn: #9a6200;
  --warn-bg: #fff1d6;
  --busy-bg: #e7eefc;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1217;
    --surface: #191d24;
    --text: #eef1f5;
    --muted: #9aa3b2;
    --border: #2b313b;
    --primary: #4c8dff;
    --ok: #4fd18a;
    --ok-bg: #12321f;
    --err: #ff7b7b;
    --err-bg: #3a1717;
    --warn: #ffc65c;
    --warn-bg: #3a2c0f;
    --busy-bg: #18243a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
}

/* ---------- Login ---------- */
#login { min-height: 100vh; display: grid; place-items: center; }
.login-box {
  width: 100%; max-width: 320px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 28px 24px;
  display: grid; gap: 12px; text-align: center;
}
.login-box h1 { margin: 0 0 8px; font-size: 22px; }
.login-box label { text-align: left; color: var(--muted); font-size: 14px; }
.logo { display: block; width: 64px; height: 64px; margin: 0 auto 4px; border-radius: 14px; }
#pin { font-size: 28px; text-align: center; letter-spacing: 8px; }
.msg { min-height: 1.4em; margin: 0; color: var(--err); font-size: 14px; }

/* ---------- Comuni ---------- */
input[type=text], input[type=password] {
  width: 100%; min-width: 0;
  padding: 12px 14px; font-size: 18px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 12px 16px; border-radius: 10px; font-size: 16px; font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-small { padding: 8px 12px; font-size: 14px; }
.btn-big { width: 100%; padding: 18px; font-size: 20px; border-radius: 12px; }
.btn-icon {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  width: 40px; height: 40px; border-radius: 10px; font-size: 18px; cursor: pointer;
}

/* ---------- Testata ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.topbar h1 { margin: 0; font-size: 20px; }
.chip {
  display: inline-block; margin-top: 4px; padding: 2px 10px;
  border-radius: 999px; background: var(--busy-bg); color: var(--muted); font-size: 13px;
}

/* ---------- Interruttore ---------- */
.toggle {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 12px; cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  flex: none; width: 50px; height: 30px; border-radius: 999px;
  background: var(--border); position: relative; transition: background .15s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 24px; height: 24px; border-radius: 50%; background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s;
}
.toggle input:checked + .switch { background: var(--ok); }
.toggle input:checked + .switch::after { transform: translateX(20px); }
.toggle input:focus-visible + .switch { outline: 2px solid var(--primary); outline-offset: 2px; }
.toggle-text { display: grid; }
.toggle-text small { color: var(--muted); font-size: 13px; }

/* ---------- Scansione ---------- */
.scan-row { display: flex; gap: 8px; margin-bottom: 12px; }
.cam {
  background: #000; border-radius: var(--radius); overflow: hidden;
  margin-bottom: 12px; display: grid; gap: 8px; padding-bottom: 8px; justify-items: center;
}
#camReader { width: 100%; }

/* ---------- Stato ---------- */
.status {
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
  font-size: 18px; font-weight: 600; text-align: center; min-height: 64px;
  display: grid; place-items: center; transition: background .2s;
}
.status.idle { background: var(--surface); color: var(--muted); border: 1px dashed var(--border); font-weight: 500; }
.status.busy { background: var(--busy-bg); color: var(--primary); }
.status.ok { background: var(--ok-bg); color: var(--ok); }
.status.err { background: var(--err-bg); color: var(--err); }
.status.warn { background: var(--warn-bg); color: var(--warn); }
.status.flash { animation: flash .5s ease-out; }
@keyframes flash { from { transform: scale(1.03); } to { transform: scale(1); } }

/* ---------- Scelta tra più varianti ---------- */
.choices { display: grid; gap: 8px; margin-bottom: 12px; }
.choice {
  display: flex; align-items: center; gap: 12px; text-align: left; width: 100%;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 10px; color: var(--text); font-size: 15px; cursor: pointer;
}
.choice img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; background: var(--bg); }

/* ---------- Scheda articolo ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
  display: grid; grid-template-columns: 96px 1fr; gap: 14px; align-items: start;
}
.card img { width: 96px; height: 96px; object-fit: cover; border-radius: 10px; background: var(--bg); }
.card img[src=""], .card img:not([src]) { visibility: hidden; }
.card h2 { margin: 0; font-size: 17px; line-height: 1.25; }
.card p { margin: 4px 0 0; }
.variant { font-size: 20px; font-weight: 700; }
.codes { color: var(--muted); font-size: 13px; word-break: break-all; }
.stock { font-size: 16px; }
.stock strong { font-size: 20px; }
.warn { color: var(--warn); font-weight: 600; font-size: 14px; }
.card .btn-big { grid-column: 1 / -1; }

/* ---------- Carrello ---------- */
.basket {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-bottom: 12px;
}
.basket-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.basket-head h3 { margin: 0; font-size: 16px; }
.basket-head .chip { margin: 0; font-weight: 600; }
.basket-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.basket-empty { margin: 4px 0 12px; color: var(--muted); font-size: 14px; text-align: center; }
.b-row {
  display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; gap: 10px; align-items: center;
  border: 1px solid var(--border); border-radius: 12px; padding: 8px;
}
.b-row.flash-row { animation: rowflash .9s ease-out; }
@keyframes rowflash { from { background: var(--ok-bg); } to { background: transparent; } }
.b-row img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; background: var(--bg); }
.b-row img[src=""] { visibility: hidden; }
.b-info { min-width: 0; }
.b-title {
  font-size: 13px; line-height: 1.25; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.b-variant { font-size: 16px; font-weight: 700; line-height: 1.25; }
.b-stock { font-size: 13px; color: var(--muted); }
.b-row.over .b-stock { color: var(--warn); font-weight: 600; }
.b-ctrl { display: flex; align-items: center; gap: 4px; }
.b-ctrl button {
  width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 18px; font-weight: 700; cursor: pointer;
}
.b-ctrl button:disabled { opacity: .35; cursor: default; }
.b-ctrl .b-del { color: var(--err); margin-left: 4px; }
.b-qty { min-width: 22px; text-align: center; font-size: 18px; font-weight: 700; }
.basket-actions { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px; margin-top: 12px; }
.basket-actions .btn-big { width: 100%; white-space: normal; }

.btn-undo { width: 100%; margin-bottom: 18px; color: var(--err); border-color: var(--err); }

/* ---------- Storico ---------- */
.history h3 { margin: 0 0 8px; font-size: 14px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.history ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.history li {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; font-size: 14px; display: flex; justify-content: space-between; gap: 10px;
}
.history li.empty { color: var(--muted); justify-content: center; }
.history li.undone { opacity: .55; text-decoration: line-through; }
.history .time { color: var(--muted); flex: none; }

@media (max-width: 380px) {
  .card { grid-template-columns: 72px 1fr; }
  .card img { width: 72px; height: 72px; }
}
