:root {
  --brand: #0b5cab;
  --brand-dark: #08457f;
  --brand-soft: #e6f0fb;
  --bg: #f3f6fa;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --text: #16202b;
  --muted: #5b6979;
  --border: #d8e0ea;
  --ok: #157a3a;
  --ok-soft: #e3f4e9;
  --warn: #a85a00;
  --warn-soft: #fdf1e1;
  --danger: #bd2630;
  --danger-soft: #fce8e9;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 32, 48, 0.06), 0 2px 8px rgba(16, 32, 48, 0.06);
  --tap: 48px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --brand: #3d8fe0;
    --brand-dark: #2a6fb5;
    --brand-soft: #14304e;
    --bg: #0e151e;
    --surface: #17212c;
    --surface-2: #1c2835;
    --text: #e6edf5;
    --muted: #9aa9ba;
    --border: #2a3745;
    --ok: #46b36b;
    --ok-soft: #173424;
    --warn: #e0a042;
    --warn-soft: #3a2a12;
    --danger: #ef6b72;
    --danger-soft: #3d1a1d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
svg.i { width: 22px; height: 22px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------------------------------------------------------------- barra in alto */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 6px;
  padding: calc(env(safe-area-inset-top) + 8px) 10px 8px;
  background: var(--brand); color: #fff;
  min-height: 56px;
}
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; padding: 0 6px; }
.topbar-title strong { font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-title small { font-size: 12px; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 10px; background: transparent; color: inherit;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.18); }
.status-pill {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px;
  border-radius: 15px; border: 0; background: rgba(0, 0, 0, 0.18); color: #fff; font-size: 13px; white-space: nowrap;
}
.status-pill .dot { width: 9px; height: 9px; border-radius: 50%; background: #9aa9ba; }
.status-pill.online .dot { background: #4cd07a; }
.status-pill.offline { background: var(--danger); }
.status-pill.offline .dot { background: #fff; }
.status-pill.pending .dot { background: #ffc24d; }

/* ---------------------------------------------------------------- pagina */
main {
  max-width: 720px; margin: 0 auto;
  padding: 14px 14px calc(env(safe-area-inset-bottom) + 90px);
  outline: none;
}
.boot { padding: 60px 0; text-align: center; color: var(--muted); }
h2 { font-size: 20px; margin: 4px 0 12px; }
h3 { font-size: 15px; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
p { margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; }
.stack { display: grid; gap: 10px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .grow { flex: 1; min-width: 0; }
.spacer { height: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 12px; }
.card.flat { box-shadow: none; }
.card.notice { border-left: 4px solid var(--brand); }
.card.warn { border-left: 4px solid var(--warn); background: var(--warn-soft); }
.card.error { border-left: 4px solid var(--danger); background: var(--danger-soft); }
.card.ok { border-left: 4px solid var(--ok); background: var(--ok-soft); }

/* ---------------------------------------------------------------- pulsanti e campi */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-weight: 600; text-decoration: none; user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; pointer-events: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:active { background: var(--brand-dark); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn.big { min-height: 58px; font-size: 18px; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btns { display: flex; gap: 10px; flex-wrap: wrap; }
.btns > .btn { flex: 1 1 140px; }

label.field { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.input, select.input, textarea.input {
  width: 100%; min-height: var(--tap); padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text);
  font-size: 17px;
}
.input:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
.input.code { font-family: ui-monospace, Consolas, monospace; letter-spacing: 2px; text-transform: uppercase; font-size: 22px; text-align: center; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: var(--tap); }
.switch input { width: 22px; height: 22px; accent-color: var(--brand); }

/* ---------------------------------------------------------------- home */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.tile {
  display: flex; flex-direction: column; justify-content: space-between; gap: 18px;
  min-height: 132px; padding: 16px; border-radius: 16px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); text-decoration: none; box-shadow: var(--shadow);
}
.tile:active { transform: scale(0.99); }
.tile .ico { width: 44px; height: 44px; border-radius: 12px; display: flex; align-items: center; justify-content: center; background: var(--brand-soft); color: var(--brand); }
.tile .ico svg.i { width: 26px; height: 26px; }
.tile strong { font-size: 17px; line-height: 1.2; }
.tile small { color: var(--muted); font-size: 13px; }
.tile.wide { grid-column: 1 / -1; min-height: 0; flex-direction: row; align-items: center; justify-content: flex-start; }

/* ---------------------------------------------------------------- barra di lettura */
.scanbar { display: flex; gap: 8px; }
.scanbar .input { flex: 1; min-width: 0; font-size: 18px; }
.scanbar .btn { padding: 0 14px; }
.scanbar .cam { background: var(--brand); color: #fff; border-color: var(--brand); min-width: 58px; }

/* ---------------------------------------------------------------- articolo */
.art-code { font: 700 22px/1.2 ui-monospace, Consolas, monospace; color: var(--brand); word-break: break-all; }
.art-desc { font-size: 18px; font-weight: 600; margin: 4px 0 10px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0; }
.stat { background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 8px; text-align: center; }
.stat b { display: block; font-size: 24px; line-height: 1.1; }
.stat span { font-size: 12px; color: var(--muted); }
.stat.main { background: var(--brand-soft); border-color: transparent; }
.stat.main b { color: var(--brand); font-size: 30px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 8px 0 0; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font: 13px ui-monospace, Consolas, monospace; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 2px 8px; }
.badge { display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 10px; background: var(--brand-soft); color: var(--brand); }
.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); }
.badge.muted { background: var(--surface-2); color: var(--muted); }

/* ---------------------------------------------------------------- elenchi */
.list { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  text-decoration: none; box-shadow: var(--shadow);
}
.item:active { background: var(--surface-2); }
.item .main { flex: 1; min-width: 0; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; overflow-wrap: anywhere; }
.item > .i { color: var(--brand); }
.item .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .qty { font-size: 22px; font-weight: 700; min-width: 56px; text-align: right; }
.item .qty small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
.item.pending { border-style: dashed; }
.item.flash { animation: flash 1s ease-out; }
@keyframes flash { from { background: var(--ok-soft); } to { background: var(--surface); } }
.diff-pos { color: var(--ok); }
.diff-neg { color: var(--danger); }
.empty { padding: 26px 12px; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: 12px; }

/* ---------------------------------------------------------------- editor quantita' */
.editor {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.18);
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 16px);
  max-width: 720px; margin: 0 auto;
  animation: up 0.18s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.stepper { display: grid; grid-template-columns: 64px 1fr 64px; gap: 10px; align-items: stretch; margin: 12px 0; }
.stepper .btn { min-height: 64px; font-size: 30px; padding: 0; }
.stepper .input { min-height: 64px; font-size: 30px; font-weight: 700; text-align: center; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; border-radius: 8px; min-height: 38px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--brand); box-shadow: var(--shadow); }

/* ---------------------------------------------------------------- fotocamera */
.camera {
  position: fixed; inset: 0; z-index: 60; background: #000;
  display: flex; flex-direction: column;
}
.camera video { flex: 1; width: 100%; min-height: 0; object-fit: cover; }
.camera .frame {
  position: absolute; left: 8%; right: 8%; top: 32%; height: 22%;
  border: 3px solid rgba(255, 255, 255, 0.9); border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.35);
  pointer-events: none; transition: border-color 0.15s;
}
.camera .frame.hit { border-color: #4cd07a; }
.camera .frame.qr { top: 25%; height: auto; aspect-ratio: 1; left: 18%; right: 18%; }
.camera .bar {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 12px; justify-content: center;
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 20px);
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.7));
}
.camera .bar .btn { background: rgba(255, 255, 255, 0.95); color: #111; border: 0; min-width: 120px; }
.camera .hint {
  position: absolute; left: 0; right: 0; top: calc(env(safe-area-inset-top) + 18px); text-align: center;
  color: #fff; font-weight: 600; text-shadow: 0 1px 3px #000; padding: 0 16px;
}
.camera .last {
  position: absolute; left: 16px; right: 16px; top: 58%; text-align: center; color: #fff;
  font: 600 16px ui-monospace, Consolas, monospace; text-shadow: 0 1px 3px #000;
}

/* ---------------------------------------------------------------- PIN */
.pin-dots { display: flex; justify-content: center; gap: 14px; margin: 18px 0; min-height: 20px; }
.pin-dots i { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--brand); }
.pin-dots i.on { background: var(--brand); }
.pinpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; max-width: 330px; margin: 0 auto; }
.pinpad button {
  min-height: 64px; font-size: 26px; font-weight: 600; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.pinpad button:active { background: var(--brand-soft); }
.pinpad button.fn { font-size: 16px; color: var(--muted); }
.operators { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.operators .btn { min-height: 64px; font-size: 17px; }
.operators .btn.last { border-color: var(--brand); color: var(--brand); }

/* ---------------------------------------------------------------- finestre, menu, messaggi */
.overlay {
  position: fixed; inset: 0; z-index: 50; background: rgba(8, 16, 26, 0.5);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade 0.15s ease-out;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  width: 100%; max-width: 720px; max-height: 92dvh; overflow: auto;
  background: var(--surface); border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(env(safe-area-inset-bottom) + 18px);
  animation: up 0.18s ease-out;
}
.sheet h2 { margin-top: 0; }
@media (min-width: 720px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 18px; max-width: 520px; }
}
.menu { display: grid; gap: 4px; }
.menu button, .menu a {
  display: flex; align-items: center; gap: 14px; min-height: 52px; padding: 0 12px; border: 0; border-radius: 10px;
  background: transparent; color: var(--text); text-decoration: none; font-size: 17px; text-align: left;
}
.menu button:active, .menu a:active { background: var(--surface-2); }
.menu .danger { color: var(--danger); }

#toasts {
  position: fixed; left: 0; right: 0; bottom: calc(env(safe-area-inset-bottom) + 16px); z-index: 70;
  display: grid; justify-items: center; gap: 8px; pointer-events: none; padding: 0 16px;
}
.toast {
  max-width: 560px; padding: 12px 16px; border-radius: 12px; background: #1f2b38; color: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); font-weight: 500; animation: up 0.18s ease-out; pointer-events: auto;
}
.toast.ok { background: var(--ok); }
.toast.error { background: var(--danger); }
.toast.warn { background: #8a4b00; }
.toast button { margin-left: 12px; border: 0; background: rgba(255, 255, 255, 0.2); color: #fff; border-radius: 6px; padding: 4px 10px; font-weight: 700; }

.qrbox { max-width: 260px; margin: 8px auto; background: #fff; border-radius: 12px; padding: 6px; }
.qrbox svg { display: block; width: 100%; height: auto; }
.regcode { font: 700 32px ui-monospace, Consolas, monospace; letter-spacing: 4px; text-align: center; color: var(--brand); }
.update-bar { position: fixed; left: 12px; right: 12px; bottom: calc(env(safe-area-inset-bottom) + 12px); z-index: 65; }
