/* ============================================================
   TAVOLA — diario calorie
   Token di design
   ============================================================ */
:root {
  /* colori */
  --carta: #F2F4EF;        /* fondo, salvia chiarissima */
  --bianco: #FFFFFF;       /* superfici card */
  --inchiostro: #19211B;   /* testo primario, verde-nero */
  --inchiostro-2: #5A655C; /* testo secondario */
  --inchiostro-3: #8A948C; /* testo attenuato */
  --linea: #E1E5DD;        /* bordi e divisori */
  --verde: #1F8A44;        /* azione primaria (validato) */
  --verde-scuro: #166A33;
  --verde-velo: #E4F2E8;
  --proteine: #B04A1E;     /* paprika (validato) */
  --carboidrati: #A87C11;  /* miele (validato) */
  --grassi: #4E6BD6;       /* ardesia (validato) */
  --rosso: #C03B2D;        /* elimina / errori */

  /* tipografia */
  --font-display: "Bricolage Grotesque", "Inter", system-ui, sans-serif;
  --font-ui: "Inter", system-ui, -apple-system, sans-serif;

  /* forma */
  --raggio: 16px;
  --raggio-s: 10px;
  --ombra: 0 1px 2px rgba(25,33,27,.05), 0 4px 16px rgba(25,33,27,.06);

  /* spazi sicuri (iPhone) */
  --safe-basso: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.45;
  color: var(--inchiostro);
  background: var(--carta);
  min-height: 100dvh;
  padding-bottom: calc(76px + var(--safe-basso));
}
button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; font-size: 16px; color: var(--inchiostro); }
h1, h2, h3 { margin: 0; font-family: var(--font-display); }
:focus-visible { outline: 3px solid var(--verde); outline-offset: 2px; border-radius: 4px; }

/* ============ barra superiore ============ */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center; gap: 4px;
  padding: 14px 16px 10px;
  background: color-mix(in srgb, var(--carta) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.topbar h1 {
  font-size: 20px; font-weight: 800; letter-spacing: -0.02em;
}
.data-nav { display: flex; align-items: center; gap: 2px; width: 100%; justify-content: space-between; }
.data-nav .freccia {
  border: 0; background: var(--bianco); color: var(--inchiostro);
  width: 40px; height: 40px; border-radius: 50%;
  font-size: 18px; line-height: 1; box-shadow: var(--ombra);
}
.data-centro { text-align: center; }
.data-centro .giorno {
  display: block; font-family: var(--font-display);
  font-size: 19px; font-weight: 800; letter-spacing: -0.02em;
}
.data-centro .torna-oggi {
  border: 0; background: none; color: var(--verde);
  font-size: 13px; font-weight: 600; padding: 0;
}

/* ============ viste ============ */
.view { padding: 8px 16px 24px; max-width: 560px; margin: 0 auto; }

/* ============================================================
   ETICHETTA DEL GIORNO — l'elemento firma.
   Riprende la grafica delle etichette nutrizionali:
   barra piena in alto, righe sottili, numeri grandi e stretti.
   ============================================================ */
.etichetta {
  background: var(--bianco);
  border: 1.5px solid var(--inchiostro);
  border-radius: var(--raggio-s);
  padding: 0 16px 14px;
  box-shadow: var(--ombra);
  overflow: hidden;
}
.etichetta .barra-titolo {
  margin: 0 -16px;
  background: var(--inchiostro);
  color: var(--carta);
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 12px;
  letter-spacing: .14em;
  text-transform: uppercase;
  padding: 7px 16px 6px;
}
.etichetta .kcal-riga {
  display: flex; align-items: baseline; gap: 10px;
  padding: 12px 0 4px;
}
.etichetta .kcal-numero {
  font-family: var(--font-display);
  font-size: 54px; font-weight: 800;
  font-stretch: 87%;
  letter-spacing: -0.03em; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.etichetta .kcal-unita { font-size: 16px; font-weight: 700; color: var(--inchiostro-2); }
.etichetta .kcal-resto { font-size: 14px; color: var(--inchiostro-2); padding-bottom: 10px; }
.etichetta .kcal-resto strong { color: var(--inchiostro); font-variant-numeric: tabular-nums; }
.etichetta .kcal-resto.oltre strong { color: var(--proteine); }

.etichetta .kcal-barra {
  height: 10px; border-radius: 5px; background: var(--carta);
  border: 1px solid var(--linea);
  overflow: hidden; margin-bottom: 12px;
}
.etichetta .kcal-barra > i {
  display: block; height: 100%;
  background: var(--verde);
  border-radius: 5px 0 0 5px;
  transition: width .5s cubic-bezier(.2,.8,.2,1);
}
.etichetta .kcal-barra > i.oltre { background: var(--proteine); }

.etichetta .macro-riga {
  display: grid;
  grid-template-columns: 12px 92px 64px 1fr;
  align-items: center; gap: 10px;
  padding: 9px 0;
  border-top: 1px solid var(--inchiostro);
  font-size: 14px;
}
.etichetta .macro-riga:first-of-type { border-top: 3px solid var(--inchiostro); }
.etichetta .macro-punto { width: 12px; height: 12px; border-radius: 3px; }
.etichetta .macro-nome { font-weight: 600; }
.etichetta .macro-valore {
  font-variant-numeric: tabular-nums; text-align: right;
  font-weight: 600; white-space: nowrap;
}
.etichetta .macro-valore small { color: var(--inchiostro-3); font-weight: 500; }
.etichetta .macro-barra {
  height: 6px; border-radius: 3px; background: var(--carta);
  border: 1px solid var(--linea); overflow: hidden;
}
.etichetta .macro-barra > i { display: block; height: 100%; border-radius: 3px 0 0 3px; }

/* ============ elenco pasti ============ */
.pasti { margin-top: 18px; display: grid; gap: 14px; }
.gruppo-pasto { background: var(--bianco); border-radius: var(--raggio); box-shadow: var(--ombra); overflow: hidden; }
.gruppo-head {
  display: flex; justify-content: space-between; align-items: baseline;
  padding: 12px 16px 8px;
}
.gruppo-head h2 { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.gruppo-head .gruppo-kcal { font-size: 13px; color: var(--inchiostro-2); font-variant-numeric: tabular-nums; }
.pasto-riga {
  display: flex; align-items: center; gap: 12px;
  width: 100%; text-align: left;
  padding: 12px 16px; border: 0; background: none;
  border-top: 1px solid var(--linea);
  font-size: 15px;
}
.pasto-riga:active { background: var(--verde-velo); }
.pasto-mini { width: 44px; height: 44px; border-radius: var(--raggio-s); object-fit: cover; flex: none; background: var(--carta); }
.pasto-info { flex: 1; min-width: 0; }
.pasto-nome { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pasto-sub { font-size: 12.5px; color: var(--inchiostro-3); }
.pasto-kcal { font-variant-numeric: tabular-nums; font-weight: 700; white-space: nowrap; }
.pasto-kcal small { font-weight: 500; color: var(--inchiostro-3); }

.vuoto {
  text-align: center; padding: 36px 20px; color: var(--inchiostro-2);
  background: var(--bianco); border-radius: var(--raggio); box-shadow: var(--ombra);
}
.vuoto .vuoto-icona { font-size: 34px; display: block; margin-bottom: 8px; }
.vuoto strong { display: block; color: var(--inchiostro); font-size: 16px; margin-bottom: 4px; }

/* ============ card generiche ============ */
.card {
  background: var(--bianco); border-radius: var(--raggio);
  box-shadow: var(--ombra); padding: 16px; margin-bottom: 14px;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 12px; }
.card-head h2 { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.legenda-obiettivo { font-size: 12px; color: var(--inchiostro-2); }

/* ============ tiles storico ============ */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.tile {
  background: var(--bianco); border-radius: var(--raggio);
  box-shadow: var(--ombra); padding: 12px 12px 10px; text-align: left;
}
.tile .tile-valore {
  font-family: var(--font-display); font-weight: 800;
  font-size: 24px; letter-spacing: -0.02em; line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.tile .tile-valore small { font-size: 13px; font-weight: 600; color: var(--inchiostro-2); }
.tile .tile-nome { font-size: 11.5px; color: var(--inchiostro-2); margin-top: 3px; line-height: 1.3; }

/* ============ grafico ============ */
.grafico-wrap { position: relative; }
.grafico svg { display: block; width: 100%; height: auto; }
.grafico .barra { fill: var(--verde); opacity: .55; }
.grafico .barra.oggi { opacity: 1; }
.grafico .barra-hit { fill: transparent; }
.grafico .linea-obiettivo { stroke: var(--inchiostro); stroke-width: 2; stroke-dasharray: 5 4; }
.grafico .asse-testo { font: 500 10px var(--font-ui); fill: var(--inchiostro-3); }
.grafico .griglia { stroke: var(--linea); stroke-width: 1; }
.grafico-tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--inchiostro); color: var(--carta);
  font-size: 12.5px; line-height: 1.4;
  padding: 7px 10px; border-radius: 8px;
  transform: translate(-50%, -110%);
  white-space: nowrap;
}
.grafico-tooltip strong { font-variant-numeric: tabular-nums; }
.dati-tabella { margin-top: 10px; }
.dati-tabella summary { font-size: 13.5px; font-weight: 600; color: var(--verde); cursor: pointer; }
.dati-tabella table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 13.5px; }
.dati-tabella th { text-align: left; font-weight: 600; color: var(--inchiostro-2); padding: 6px 4px; border-bottom: 1px solid var(--linea); }
.dati-tabella td { padding: 6px 4px; border-bottom: 1px solid var(--linea); font-variant-numeric: tabular-nums; }

/* macro medie storico */
.macro-storico-riga {
  display: grid; grid-template-columns: 12px 100px 1fr 70px;
  gap: 10px; align-items: center; padding: 8px 0; font-size: 14px;
}
.macro-storico-riga .macro-punto { width: 12px; height: 12px; border-radius: 3px; }
.macro-storico-riga .barra { height: 8px; background: var(--carta); border: 1px solid var(--linea); border-radius: 4px; overflow: hidden; }
.macro-storico-riga .barra i { display: block; height: 100%; border-radius: 4px 0 0 4px; }
.macro-storico-riga .valore { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

/* ============ impostazioni ============ */
.campo-riga {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 0; border-bottom: 1px solid var(--linea); gap: 12px;
}
.campo-riga:last-child { border-bottom: 0; }
.campo-riga label { font-size: 14.5px; font-weight: 500; }
.campo-riga input {
  width: 110px; text-align: right;
  border: 1px solid var(--linea); border-radius: var(--raggio-s);
  padding: 8px 10px; background: var(--carta);
}
.campo-colonna { margin-bottom: 12px; }
.campo-colonna label { display: block; font-size: 13.5px; font-weight: 600; margin-bottom: 5px; }
.campo-colonna input {
  width: 100%; border: 1px solid var(--linea); border-radius: var(--raggio-s);
  padding: 10px 12px; background: var(--carta);
}
.nota { font-size: 13px; color: var(--inchiostro-2); margin: 6px 0 12px; }
.nota.errore { color: var(--rosso); font-weight: 500; }

/* ============ bottoni ============ */
.btn {
  border: 0; border-radius: 12px; font-size: 15.5px; font-weight: 700;
  padding: 13px 18px; letter-spacing: -0.01em;
}
.btn-primario { background: var(--verde); color: #fff; }
.btn-primario:active { background: var(--verde-scuro); }
.btn-primario:disabled { background: var(--linea); color: var(--inchiostro-3); cursor: default; }
.btn-secondario { background: var(--verde-velo); color: var(--verde-scuro); }
.btn-pericolo { background: #F7E7E4; color: var(--rosso); }
.btn-largo { width: 100%; }
.btn-testo { border: 0; background: none; color: var(--verde); font-weight: 600; font-size: 14px; padding: 10px 0; }
.btn-icona {
  border: 0; background: var(--carta); color: var(--inchiostro);
  width: 34px; height: 34px; border-radius: 50%; font-size: 15px; flex: none;
}

/* ============ tab bar + FAB ============ */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  background: color-mix(in srgb, var(--bianco) 92%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--linea);
  padding: 6px 8px calc(8px + var(--safe-basso));
}
.tabbar button {
  border: 0; background: none;
  display: grid; justify-items: center; gap: 2px;
  font-size: 11px; font-weight: 600; color: var(--inchiostro-3);
  padding: 6px 0; border-radius: 10px;
}
.tabbar button svg { width: 22px; height: 22px; fill: currentColor; }
.tabbar button.attivo { color: var(--verde-scuro); }
.fab {
  position: fixed; right: 18px; bottom: calc(78px + var(--safe-basso)); z-index: 31;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--verde); color: #fff;
  font-size: 30px; font-weight: 500; line-height: 1;
  box-shadow: 0 6px 20px rgba(31,138,68,.4);
  transition: transform .15s ease;
}
.fab:active { transform: scale(.92); }

/* ============ sheet ============ */
.sheet {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(25,33,27,.35);
  display: flex; align-items: flex-end;
}
.sheet[hidden] { display: none; }
.sheet-inner {
  background: var(--carta); width: 100%; max-height: 94dvh;
  border-radius: 20px 20px 0 0;
  display: flex; flex-direction: column;
  animation: sale .28s cubic-bezier(.2,.8,.2,1);
}
@keyframes sale { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px 10px;
}
.sheet-head h2 { font-size: 19px; font-weight: 800; letter-spacing: -0.02em; flex: 1; }
.sheet-data { font-size: 13px; color: var(--inchiostro-2); }
.sheet-corpo { overflow-y: auto; padding: 4px 16px 16px; flex: 1; }
.sheet-piede {
  padding: 12px 16px calc(14px + var(--safe-basso));
  background: var(--carta); border-top: 1px solid var(--linea);
}
.piede-doppio { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* segmenti tipo pasto */
.segmenti {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px;
  background: var(--bianco); border-radius: 12px; padding: 5px;
  box-shadow: var(--ombra); margin-bottom: 16px;
}
.segmenti button {
  border: 0; background: none; border-radius: 9px;
  font-size: 13px; font-weight: 600; color: var(--inchiostro-2);
  padding: 9px 2px;
}
.segmenti button.attivo { background: var(--inchiostro); color: var(--carta); }

.etich-campo { display: block; font-size: 13.5px; font-weight: 600; margin: 4px 0 6px; }
#pasto-testo {
  width: 100%; border: 1px solid var(--linea); border-radius: var(--raggio-s);
  background: var(--bianco); padding: 12px; resize: vertical; min-height: 76px;
}

/* foto */
.foto-riga { margin: 12px 0; }
.foto-bottone {
  display: flex; align-items: center; gap: 10px;
  background: var(--bianco); border: 1.5px dashed var(--linea);
  border-radius: var(--raggio-s); padding: 12px 14px;
  font-size: 14px; font-weight: 600; color: var(--inchiostro-2); cursor: pointer;
}
.foto-icona { font-size: 20px; }
.foto-anteprima { position: relative; margin-top: 10px; }
.foto-anteprima img { width: 100%; max-height: 220px; object-fit: cover; border-radius: var(--raggio-s); display: block; }
.foto-rimuovi { position: absolute; top: 8px; right: 8px; background: rgba(25,33,27,.75); color: #fff; }

#btn-analizza { width: 100%; margin-top: 4px; }
#btn-analizza.caricamento { position: relative; color: transparent; }
#btn-analizza.caricamento::after {
  content: ""; position: absolute; inset: 0; margin: auto;
  width: 20px; height: 20px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.35); border-top-color: #fff;
  animation: gira .7s linear infinite;
}
@keyframes gira { to { transform: rotate(360deg); } }

/* editor ingredienti */
.ingredienti { margin-top: 18px; }
.ingredienti-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.ingredienti-head h3 { font-size: 15px; }
.ingredienti-head .nota { margin: 0; }
.ingredienti-intestazione, .ingrediente-riga {
  display: grid;
  grid-template-columns: 1fr 52px 40px 40px 40px 28px;
  gap: 6px; align-items: center;
}
.ingredienti-intestazione {
  font-size: 11px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--inchiostro-3);
  padding: 0 2px 6px;
}
.ingredienti-intestazione span:not(:first-child) { text-align: center; }
.ingrediente-riga {
  background: var(--bianco); border-radius: var(--raggio-s);
  padding: 8px 6px; margin-bottom: 6px;
  animation: appare .3s ease both;
}
@keyframes appare { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.ingrediente-riga input {
  border: 0; background: none; padding: 4px 2px; width: 100%;
  font-size: 13.5px; font-variant-numeric: tabular-nums;
}
.ingrediente-riga input.i-nome { font-weight: 600; font-size: 14px; }
.ingrediente-riga input.i-num { text-align: center; }
.ingrediente-riga .i-qta { font-size: 11.5px; color: var(--inchiostro-3); grid-column: 1; }
.ingrediente-riga .i-x {
  border: 0; background: none; color: var(--inchiostro-3);
  font-size: 15px; padding: 4px;
}
.ingredienti-totale {
  display: flex; justify-content: space-between; align-items: baseline;
  border-top: 2px solid var(--inchiostro);
  padding-top: 10px; margin-top: 6px;
  font-weight: 700; font-size: 15px;
}
.ingredienti-totale .tot-macro { font-size: 12.5px; color: var(--inchiostro-2); font-weight: 500; font-variant-numeric: tabular-nums; }
.ingredienti-totale .tot-kcal { font-variant-numeric: tabular-nums; }

/* dettaglio pasto */
.dettaglio-tab { width: 100%; border-collapse: collapse; font-size: 14px; }
.dettaglio-tab td { padding: 8px 2px; border-bottom: 1px solid var(--linea); }
.dettaglio-tab td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.dettaglio-tab .d-qta { color: var(--inchiostro-3); font-size: 12.5px; display: block; }
.dettaglio-foto { width: 100%; max-height: 240px; object-fit: cover; border-radius: var(--raggio-s); margin-bottom: 12px; }
.dettaglio-macro { display: flex; gap: 14px; margin: 12px 0 4px; font-size: 13px; color: var(--inchiostro-2); }
.dettaglio-macro b { color: var(--inchiostro); font-variant-numeric: tabular-nums; }

/* ============ toast ============ */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-basso)); z-index: 50;
  transform: translateX(-50%);
  background: var(--inchiostro); color: var(--carta);
  font-size: 14px; font-weight: 600;
  padding: 11px 18px; border-radius: 999px;
  box-shadow: 0 8px 24px rgba(25,33,27,.25);
  animation: appare .25s ease;
  max-width: calc(100vw - 40px); text-align: center;
}

/* ============ riduzione movimento ============ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* schermi larghi: l'app resta una colonna da telefono, centrata */
@media (min-width: 720px) {
  .tabbar { max-width: 560px; left: 50%; transform: translateX(-50%); border-radius: 18px 18px 0 0; border: 1px solid var(--linea); border-bottom: 0; }
  .fab { right: calc(50% - 268px); }
  .sheet-inner { max-width: 560px; margin: 0 auto; }
}
