/* Mobile-first. Colori come variabili: tema chiaro di base, scuro se il sistema lo chiede. */
:root {
  --bg: #f4f5f7; --card: #fff; --text: #14171a; --muted: #6b7280; --line: #e4e6ea;
  --accent: #2f6fed; --on-accent: #fff; --danger: #d43d3d;
  --kcal: #2f6fed; --p: #e5484d; --c: #e39b12; --f: #2f9e6b;
  --tabs-h: 54px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --card: #1b2026; --text: #eceef1; --muted: #98a1ad; --line: #2b323a;
    --accent: #5b93ff; --on-accent: #0b1220; --danger: #ff6b6b;
    --kcal: #5b93ff; --p: #ff6b70; --c: #f2b544; --f: #4cc38a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
body.locked { overflow: hidden; }
h1, h2, p { margin: 0; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
button, input, select { font: inherit; color: inherit; }

#view {
  max-width: 640px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px);
}
#view > div { display: flex; flex-direction: column; gap: 12px; }

/* --- barra di navigazione in basso --- */
#tabs {
  position: fixed; inset: auto 0 0 0; z-index: 5; display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
#tabs a {
  flex: 1; max-width: 128px; height: var(--tabs-h); display: grid; place-items: center;
  color: var(--muted); text-decoration: none; font-size: .82rem; font-weight: 600;
}
#tabs a[aria-current] { color: var(--accent); box-shadow: inset 0 2px 0 var(--accent); }

/* --- elementi comuni --- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 44px; }
.card { background: var(--card); border-radius: 14px; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.card.warn { border: 1px solid var(--c); }
.muted { color: var(--muted); }
.small-text { font-size: .85rem; }
.section-label { font-size: .8rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-top: 6px; }
.empty { color: var(--muted); text-align: center; padding: 18px 0; }
.stack { display: flex; flex-direction: column; gap: 8px; }

.btn {
  border: 0; border-radius: 10px; background: var(--accent); color: var(--on-accent);
  padding: 0 16px; min-height: 44px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; }
.btn.ghost { background: transparent; color: var(--accent); border: 1px solid var(--line); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.icon-btn {
  border: 0; background: transparent; width: 44px; height: 44px; border-radius: 10px;
  font-size: 1.3rem; color: var(--muted); cursor: pointer; flex: none;
}

/* font-size 16px: sotto questa soglia iPhone zooma la pagina quando tocchi il campo */
.input {
  width: 100%; min-height: 44px; padding: 8px 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
.input:focus, .btn:focus-visible, .row:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); min-width: 0; }
.field .input { color: var(--text); }
form { display: flex; flex-direction: column; gap: 10px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-2.wide-first { grid-template-columns: 2fr 1fr; }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 6px 12px; font-size: .88rem; color: var(--muted); cursor: pointer;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.chip.center { align-self: center; }

/* --- righe di elenco (alimenti, voci, pesate) --- */
.list { display: flex; flex-direction: column; }
.row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-top: 1px solid var(--line); min-height: 52px; }
.list > .row:first-child { border-top: 0; }
.row[role=button] { cursor: pointer; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub { font-size: .82rem; color: var(--muted); }
.row-kcal { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 3ch; text-align: right; }
.star { border: 0; background: transparent; font-size: 1.25rem; width: 36px; height: 44px; color: var(--muted); cursor: pointer; flex: none; text-align: center; }
.star.on { color: var(--c); }
.input.grams { width: 76px; text-align: right; flex: none; }
.unit { color: var(--muted); }

/* --- diario --- */
.date-pick { position: relative; font-weight: 700; font-size: 1.1rem; padding: 10px 14px; border-radius: 10px; text-align: center; }
.date-pick input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.kcal-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.big { font-size: 1.9rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.bar { height: 8px; background: var(--line); border-radius: 999px; overflow: hidden; }
.fill { height: 100%; border-radius: 999px; transition: width .25s; }
.fill.kcal { background: var(--kcal); } .fill.p { background: var(--p); } .fill.c { background: var(--c); } .fill.f { background: var(--f); }
.macro-grid { display: grid; gap: 10px; }
.macro { display: flex; flex-direction: column; gap: 4px; }
.macro-head { display: flex; justify-content: space-between; font-size: .88rem; font-weight: 600; }
.macro-head .muted { font-weight: 400; font-variant-numeric: tabular-nums; }
@media (min-width: 520px) { .macro-grid { grid-template-columns: repeat(3, 1fr); } }
.meal { gap: 0; }
.meal-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding-bottom: 6px; }
.meal-actions { display: flex; gap: 6px; flex: none; }

/* --- pannelli modali --- */
.overlay {
  position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, .5);
  display: flex; justify-content: center; align-items: flex-start;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px;
}
.sheet {
  background: var(--card); border-radius: 16px; width: 100%; max-width: 560px;
  max-height: calc(100dvh - env(safe-area-inset-top) - 16px); display: flex; flex-direction: column;
}
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 6px 0 16px; }
.sheet-head h2 { overflow-wrap: anywhere; }
.sheet-body { padding: 8px 16px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; -webkit-overflow-scrolling: touch; }
.qty-form { flex-direction: row; align-items: center; }
.big-input { font-size: 1.5rem; font-weight: 700; text-align: right; flex: 1; min-width: 0; }
.preview { background: var(--bg); border-radius: 10px; padding: 10px 12px; font-variant-numeric: tabular-nums; font-size: .95rem; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 20;
  top: calc(env(safe-area-inset-top) + 12px); max-width: calc(100vw - 32px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .92rem; font-weight: 600;
}

/* --- peso --- */
.stats { flex-direction: row; justify-content: space-between; }
.stat-value { font-weight: 700; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.chart { width: 100%; height: auto; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .raw { fill: var(--muted); opacity: .55; }
.chart .trend { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.legend { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.raw { background: var(--muted); opacity: .55; }
.dot.trend { background: var(--accent); margin-left: 10px; }

/* --- Fase 2: allergeni, scanner, ricerca online --- */
.center-text { text-align: center; }
.search-row { display: flex; gap: 8px; }
.search-row .btn { flex: none; font-size: 1.2rem; }
.badge-warn { color: var(--danger); font-weight: 800; margin-right: 6px; }
.allergen-box { border-radius: 10px; padding: 10px 12px; font-size: .92rem; display: flex; flex-direction: column; gap: 4px; border: 1px solid var(--line); }
.allergen-box.danger { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.allergen-box.caution { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, transparent); }
.allergen-box.ok { background: color-mix(in srgb, var(--f) 10%, transparent); }
.allergen-title { color: var(--danger); font-weight: 800; }
.scanner-frame { position: relative; border-radius: 12px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner-video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-line { position: absolute; left: 10%; right: 10%; top: 50%; height: 2px; background: var(--p); box-shadow: 0 0 8px var(--p); }

/* --- Fase 3: check-in --- */
.banner {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left; width: 100%;
  border: 1px solid var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--card));
  border-radius: 14px; padding: 12px 14px; cursor: pointer; font-size: .92rem;
}
.banner strong { color: var(--accent); }
.kv { display: flex; justify-content: space-between; gap: 12px; font-size: .95rem; padding: 2px 0; font-variant-numeric: tabular-nums; }
.kv strong { text-align: right; }
.tag { font-size: .72rem; font-weight: 700; padding: 1px 7px; border-radius: 999px; margin-left: 8px; background: var(--line); color: var(--muted); }
.tag.ok { background: color-mix(in srgb, var(--f) 20%, transparent); color: var(--f); }
.field select.input { color: var(--text); }

/* l'attributo hidden deve vincere sui display impostati dalle classi */
[hidden] { display: none !important; }
