:root {
  --bg: #eef0f3; --fg: #15191e; --muted: #5b6672; --card: #fff; --card-2: #f6f7f9; --line: #d4d9df;
  --accent: #1f6feb; --accent-fg: #fff; --ok: #1a7f37; --ok-soft: #dcf3e2; --err: #c4232f; --err-soft: #fbe1e3;
  --shadow: 0 1px 2px rgb(0 0 0 / .08), 0 2px 6px rgb(0 0 0 / .06);
  --r: 16px; --gap: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1318; --fg: #e8edf2; --muted: #95a1ad; --card: #1b2129; --card-2: #232a34; --line: #313a45;
    --accent: #4c8dff; --accent-fg: #0b1220; --ok: #3fb950; --ok-soft: #12301b; --err: #ff7b72; --err-soft: #3a1618;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1318; --fg: #e8edf2; --muted: #95a1ad; --card: #1b2129; --card-2: #232a34; --line: #313a45;
  --accent: #4c8dff; --accent-fg: #0b1220; --ok: #3fb950; --ok-soft: #12301b; --err: #ff7b72; --err-soft: #3a1618;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; display: flex; flex-direction: column; height: 100dvh; overflow: hidden;
  font: 20px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--fg);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation; overscroll-behavior: none;
}
button, a { touch-action: manipulation; }
svg { display: block; }

/* --- barra superiore ------------------------------------------------------ */
.barra {
  flex: none; display: flex; align-items: center; gap: 12px;
  min-height: 76px; padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--card); border-bottom: 1px solid var(--line);
}
.logo { font-weight: 700; font-size: 1.25rem; color: inherit; text-decoration: none; white-space: nowrap; }
.indietro {
  display: flex; align-items: center; gap: 6px; height: 56px; padding: 0 20px 0 12px;
  border: 0; border-radius: 12px; background: var(--card-2); color: inherit; font: inherit; font-weight: 600; cursor: pointer;
}
.indietro svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.indietro[hidden] { display: none; }
.indietro:not([hidden]) + .logo { display: none; }
.percorso { flex: 1; min-width: 0; display: flex; gap: 8px; align-items: center; overflow: hidden; font-size: 1.1rem; color: var(--muted); }
.percorso b { color: var(--fg); white-space: nowrap; }
.percorso i { font-style: normal; opacity: .5; }
.admin-link { color: var(--muted); text-decoration: none; font-size: .9rem; padding: 12px 8px; }
.schermo {
  width: 56px; height: 56px; border: 0; border-radius: 12px; background: var(--card-2); color: inherit; cursor: pointer;
  display: grid; place-items: center;
}
.schermo svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* --- area contenuti ------------------------------------------------------- */
main {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 20px max(16px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
h1 { font-size: 1.6rem; margin: 0 0 18px; }

/* tile grandi per materiale / forma / misura */
.griglia { display: grid; gap: var(--gap); grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); }
.griglia.larga { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.griglia.stretta { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.tile {
  min-height: 112px; padding: 14px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  border: 2px solid transparent; border-radius: var(--r); background: var(--card); box-shadow: var(--shadow);
  color: inherit; text-decoration: none; font: inherit; font-size: 1.45rem; font-weight: 650; text-align: center;
  overflow-wrap: anywhere; cursor: pointer; transition: transform .08s, border-color .08s;
}
.tile:active { transform: scale(.97); border-color: var(--accent); }
.tile small { font-size: .95rem; font-weight: 500; color: var(--muted); }
.tile.vuota { opacity: .55; }
.tile.forma { min-height: 200px; font-size: 1.7rem; }
.tile.forma svg { width: 84px; height: 84px; fill: none; stroke: var(--accent); stroke-width: 5; }
.tile .num { font-size: 2.1rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile .sp { font-size: 1.1rem; font-weight: 600; color: var(--muted); }

/* --- schermata quantità --------------------------------------------------- */
.quantita { display: grid; gap: 20px; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; max-width: 1200px; margin: 0 auto; }
@media (orientation: portrait), (max-width: 820px) { .quantita { grid-template-columns: 1fr; } }
.pannello { display: flex; flex-direction: column; gap: 16px; }
.articolo { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow); padding: 18px 20px; }
.articolo .nome { font-size: 1.5rem; font-weight: 700; }
.articolo .giacenza { display: flex; gap: 28px; margin-top: 10px; flex-wrap: wrap; }
.articolo .giacenza div { display: flex; flex-direction: column; }
.articolo .giacenza span { color: var(--muted); font-size: .9rem; }
.articolo .giacenza strong { font-size: 2rem; font-variant-numeric: tabular-nums; }
.articolo .nota { margin-top: 10px; padding: 8px 12px; border-radius: 10px; background: var(--card-2); font-size: 1rem; }

.segmenti { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.segmenti button {
  height: 84px; border: 3px solid var(--line); border-radius: var(--r); background: var(--card);
  color: inherit; font: inherit; font-size: 1.45rem; font-weight: 700; cursor: pointer;
}
.segmenti button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: var(--accent-fg); }
.segmenti.movimento button[data-v="-1"][aria-pressed="true"] { border-color: var(--err); background: var(--err); color: #fff; }
.segmenti.movimento button[data-v="1"][aria-pressed="true"] { border-color: var(--ok); background: var(--ok); color: #fff; }

.display {
  display: flex; align-items: center; gap: 12px; height: 100px; padding: 0 12px 0 22px;
  border-radius: var(--r); background: var(--card); box-shadow: var(--shadow); border: 3px solid var(--line);
}
.display .valore { flex: 1; text-align: right; font-size: 3.2rem; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; }
.display .valore.vuoto { color: var(--muted); font-weight: 500; }
.display .unita { font-size: 1.3rem; color: var(--muted); min-width: 64px; }
.anteprima { min-height: 1.6em; font-size: 1.15rem; color: var(--muted); text-align: right; padding-right: 6px; }
.anteprima strong { color: var(--fg); }
.anteprima.negativa, .anteprima.negativa strong { color: var(--err); }

.tastierino { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.tastierino button {
  height: clamp(72px, 13dvh, 120px); border: 0; border-radius: var(--r); background: var(--card); box-shadow: var(--shadow);
  color: inherit; font: inherit; font-size: 2.3rem; font-weight: 600; cursor: pointer; font-variant-numeric: tabular-nums;
}
.tastierino button:active, .segmenti button:active, .grande:active { transform: scale(.97); }
.tastierino .svuota { font-size: 1.3rem; color: var(--err); }
.grande {
  width: 100%; height: 96px; border: 0; border-radius: var(--r); cursor: pointer;
  font: inherit; font-size: 1.6rem; font-weight: 700; background: var(--accent); color: var(--accent-fg);
}
.grande:disabled { opacity: .4; cursor: default; transform: none; }
.grande.secondario { background: var(--card-2); color: var(--fg); }

/* in verticale tutto deve stare in una schermata, senza scorrere */
@media (orientation: portrait) {
  .quantita, .quantita .pannello { gap: 12px; }
  .articolo { padding: 14px 18px; }
  .articolo .giacenza { margin-top: 6px; }
  .segmenti button { height: 68px; }
  .display { height: 84px; }
  .display .valore { font-size: 2.8rem; }
  .anteprima { min-height: 1.4em; }
  .tastierino { gap: 10px; }
  .tastierino button { height: clamp(56px, 7dvh, 90px); }
  .quantita .grande { height: 80px; }
}

/* --- conferma e esito ----------------------------------------------------- */
.velo { position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 24px; background: rgb(0 0 0 / .55); }
.scheda { width: min(640px, 100%); background: var(--card); border-radius: 22px; padding: 28px; display: flex; flex-direction: column; gap: 20px; }
.scheda h2 { margin: 0; font-size: 1.6rem; }
.riepilogo { display: grid; grid-template-columns: auto 1fr; gap: 10px 24px; font-size: 1.25rem; }
.riepilogo dt { color: var(--muted); }
.riepilogo dd { margin: 0; font-weight: 650; }
.movimento-grande { font-size: 2.6rem; font-weight: 800; text-align: center; padding: 14px; border-radius: var(--r); }
.movimento-grande.carico { background: var(--ok-soft); color: var(--ok); }
.movimento-grande.scarico { background: var(--err-soft); color: var(--err); }
.scheda .azioni { display: grid; grid-template-columns: 1fr 1.4fr; gap: 14px; }

.esito { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; text-align: center; cursor: pointer; }
.esito .spunta { width: 150px; height: 150px; border-radius: 50%; background: var(--ok); display: grid; place-items: center; }
.esito .spunta svg { width: 90px; height: 90px; fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.esito h1 { margin: 0; font-size: 2.2rem; }
.esito p { margin: 0; font-size: 1.3rem; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 30;
  max-width: calc(100% - 32px); padding: 16px 24px; border-radius: 14px; background: var(--err); color: #fff;
  font-size: 1.2rem; font-weight: 600; box-shadow: 0 6px 20px rgb(0 0 0 / .25);
}
.toast[hidden] { display: none; }
.messaggio { text-align: center; color: var(--muted); font-size: 1.3rem; padding: 40px 0; }

/* --- amministrazione (resta un'interfaccia da tavolo) ---------------------- */
body.admin { -webkit-user-select: text; user-select: text; font-size: 18px; }
body.admin .schermo { display: none; }
body.admin main > * { max-width: 1200px; margin-left: auto; margin-right: auto; }
.back { display: none; }
.row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; } .row > * { flex: 1; }
body.admin main button, .btn {
  min-height: 52px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  color: inherit; font: inherit; cursor: pointer;
}
body.admin button.primary, dialog .primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.danger { color: var(--err); }
.err { color: var(--err); font-weight: 600; min-height: 1.4em; }
table { width: 100%; border-collapse: collapse; background: var(--card); font-size: .95rem; }
th, td { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; }
.scroll { overflow-x: auto; }
input, textarea, select { font: inherit; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: inherit; width: 100%; -webkit-user-select: text; user-select: text; }
label { display: block; margin-bottom: 10px; font-size: .9rem; }
dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--fg); width: min(520px, 92vw); }
dialog button { min-height: 56px; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card-2); color: inherit; font: inherit; cursor: pointer; }
body.admin td button { min-height: 40px; font-size: .9rem; padding: 4px 10px; }
