:root {
  --bg: #fffbf7;
  --surface: #ffffff;
  --surface-2: #f6ece3;
  --text: #2a1d14;
  --muted: #6b5a4d;
  --primary: #8d4e1f;
  --on-primary: #ffffff;
  --primary-soft: #ffdbc7;
  --add: #2e6b30;
  --add-soft: #d6f0cf;
  --remove: #a12f22;
  --remove-soft: #ffdad3;
  --search: #2f5b8c;
  --search-soft: #d7e6f8;
  --border: #e3d4c8;
  --focus: #1a5fb4;
  --radius: 14px;
  --shadow: 0 2px 10px rgb(60 30 10 / 0.08);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a120d;
    --surface: #251a13;
    --surface-2: #33251b;
    --text: #f4e9e1;
    --muted: #cbb7a8;
    --primary: #ffb68a;
    --on-primary: #3d1b00;
    --primary-soft: #5a2f10;
    --add: #9ed69a;
    --add-soft: #1f3a1f;
    --remove: #ffb4a8;
    --remove-soft: #4a1d17;
    --search: #a9c9f0;
    --search-soft: #1c3049;
    --border: #4a3a2e;
    --focus: #8ab4f8;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: .5rem 1rem; z-index: 99; }
.skip:focus { left: .5rem; }

/* ---------- barra superiore ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: .5rem;
  padding: calc(.5rem + env(safe-area-inset-top)) 1rem .5rem;
  background: var(--primary-soft);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.brand img { border-radius: 7px; }
.topnav { margin-left: auto; display: flex; gap: .25rem; }
.net { font-size: .75rem; font-weight: 600; background: var(--remove); color: var(--bg); padding: .1rem .5rem; border-radius: 99px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: transparent; color: var(--text); cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: rgb(0 0 0 / .06); }
.icon-btn svg, .btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn.listening { background: var(--remove); color: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgb(161 47 34 / .25); } }

main { max-width: 760px; margin: 0 auto; padding: 1rem 1rem 8rem; outline: none; }
h1 { font-size: 1.35rem; margin: .25rem 0 1rem; }
h2 { font-size: 1.05rem; margin: 1.5rem 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* ---------- bottoni ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: .55rem 1.1rem; border-radius: 99px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: transparent; }
.btn.add { background: var(--add); color: var(--bg); border-color: transparent; }
.btn.remove { background: var(--remove); color: var(--bg); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }

/* ---------- elenco ---------- */
.search { width: 100%; padding: .7rem 1rem; border: 1px solid var(--border); border-radius: 99px; background: var(--surface); }
.chips { display: flex; gap: .5rem; margin: .75rem 0; overflow-x: auto; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 99px;
  padding: .35rem .9rem; min-height: 36px; cursor: pointer; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--primary); color: var(--on-primary); border-color: transparent; }
.cat { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--primary); margin: 1.1rem 0 .35rem; font-weight: 700; }
.list { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.item { display: flex; align-items: center; gap: .75rem; padding: .6rem .75rem; border-top: 1px solid var(--border); }
.item:first-child { border-top: 0; }
.item .info { flex: 1; min-width: 0; text-decoration: none; color: inherit; display: flex; align-items: center; gap: .75rem; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: .85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumb {
  flex: none; width: 48px; height: 48px; border-radius: 10px; object-fit: cover;
  background: var(--surface-2); display: grid; place-items: center; font-weight: 700; color: var(--muted);
}
.thumb.big { width: 96px; height: 96px; font-size: 2rem; }
.qty { display: flex; align-items: center; gap: .15rem; }
.qty output { min-width: 2ch; text-align: center; font-weight: 700; font-size: 1.1rem; }
.qty .low { color: var(--remove); }
.stepper { display: flex; align-items: center; gap: .5rem; }
.stepper button { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface-2); font-size: 1.3rem; font-weight: 700; cursor: pointer; }
.stepper output { min-width: 2.5ch; text-align: center; font-size: 1.3rem; font-weight: 700; }
.empty { text-align: center; padding: 3rem 1rem; color: var(--muted); }

.fab-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 9;
  display: flex; justify-content: center; gap: .75rem;
  padding: .75rem 1rem calc(.75rem + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 35%);
}
.fab-bar .btn { box-shadow: 0 4px 14px rgb(0 0 0 / .18); min-height: 52px; padding: 0 1.4rem; font-size: 1.05rem; }

/* ---------- scelta modalità ---------- */
.modes { display: grid; gap: .75rem; }
.mode {
  display: flex; align-items: center; gap: 1rem; text-align: left;
  padding: 1rem; border-radius: var(--radius); border: 2px solid transparent; cursor: pointer;
  font-size: 1.05rem; font-weight: 600;
}
.mode small { display: block; font-weight: 400; font-size: .85rem; color: var(--muted); }
.mode .ico { font-size: 1.6rem; width: 2.2rem; text-align: center; }
.mode.search { background: var(--search-soft); }
.mode.add { background: var(--add-soft); }
.mode.remove { background: var(--remove-soft); }
.mode:hover { border-color: var(--border); }

/* ---------- scanner ---------- */
.scanner { display: grid; gap: .75rem; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--border); border-radius: 99px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: .6rem .25rem; min-height: 44px; font-weight: 600; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"].search { background: var(--search); color: var(--bg); }
.seg button[aria-pressed="true"].add { background: var(--add); color: var(--bg); }
.seg button[aria-pressed="true"].remove { background: var(--remove); color: var(--bg); }
.viewport { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; aspect-ratio: 4 / 3; max-height: 55dvh; width: 100%; }
.viewport video { width: 100%; height: 100%; object-fit: cover; display: block; }
.viewport .frame {
  position: absolute; inset: 22% 12%; border: 3px solid rgb(255 255 255 / .9); border-radius: 14px;
  box-shadow: 0 0 0 999px rgb(0 0 0 / .25); pointer-events: none; transition: border-color .15s;
}
.viewport.hit .frame { border-color: #6be36b; }
.viewport .hint { position: absolute; left: 50%; bottom: .6rem; transform: translateX(-50%); background: rgb(0 0 0 / .6); color: #fff; padding: .25rem .75rem; border-radius: 8px; font-size: .9rem; white-space: nowrap; }
.viewport .torch { position: absolute; top: .5rem; right: .5rem; background: rgb(0 0 0 / .5); color: #fff; }
.viewport .paused { position: absolute; inset: 0; display: grid; place-items: center; background: rgb(0 0 0 / .55); color: #fff; font-weight: 600; }
.manual { display: flex; gap: .5rem; }
.manual input { flex: 1; min-width: 0; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; border: 1px solid var(--border); }
.card.ok { border-color: var(--add); }
.card.warn { border-color: var(--remove); }
.card.info { border-color: var(--search); }
.result { display: flex; gap: 1rem; align-items: center; }
.result .big-qty { font-size: 2rem; font-weight: 800; line-height: 1; }

/* ---------- form ---------- */
.form { display: grid; gap: .9rem; }
label { font-weight: 600; font-size: .9rem; display: block; margin-bottom: .25rem; }
input[type="text"], input[type="password"], input[type="search"], input:not([type]), select {
  width: 100%; padding: .65rem .8rem; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); min-height: 44px;
}
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.suggestions { display: flex; flex-wrap: wrap; gap: .4rem; }
.photo-box { display: flex; gap: 1rem; align-items: center; background: var(--surface-2); padding: .9rem; border-radius: var(--radius); }
.moves { list-style: none; padding: 0; margin: 0; font-size: .875rem; }
.moves li { display: flex; justify-content: space-between; padding: .35rem 0; border-bottom: 1px dashed var(--border); }
.plus { color: var(--add); font-weight: 700; }
.minus { color: var(--remove); font-weight: 700; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .5rem 0; }
.switch input { width: 22px; height: 22px; }
.progress { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; }
.progress::after { content: ""; display: block; height: 100%; width: 40%; background: var(--primary); animation: slide 1s infinite ease-in-out; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(250%); } }

/* ---------- toast e dialoghi ---------- */
.toasts { position: fixed; left: 50%; bottom: calc(5.5rem + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50; display: grid; gap: .5rem; width: min(92vw, 520px); }
.toast {
  display: flex; align-items: center; gap: .75rem;
  background: #2a1d14; color: #fff; padding: .7rem 1rem; border-radius: 12px; box-shadow: 0 6px 20px rgb(0 0 0 / .25);
  animation: rise .2s ease-out;
}
.toast button { margin-left: auto; background: transparent; border: 0; color: #ffb68a; font-weight: 700; cursor: pointer; min-height: 36px; }
@keyframes rise { from { transform: translateY(10px); opacity: 0; } }
.dialog { border: 0; border-radius: 20px; padding: 1.25rem; width: min(92vw, 460px); background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgb(0 0 0 / .3); }
.dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog h2 { margin-top: 0; }
.login { max-width: 360px; margin: 15vh auto 0; }
