:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #efeee9;
  --line: #e2e1da;
  --text: #1a1a19;
  --text-2: #55554f;
  --muted: #7a7970;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c4302f;
  --over: #e34948;
  --protein: #2a78d6;
  --carbs: #eb6834;
  --fat: #1baf7a;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #252523;
    --line: #2f2f2c;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #9a998f;
    --accent: #3987e5;
    --over: #e66767;
    --danger: #e66767;
    --protein: #3987e5;
    --carbs: #d95926;
    --fat: #199e70;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #252523; --line: #2f2f2c;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #9a998f; --accent: #3987e5;
  --over: #e66767; --danger: #e66767; --protein: #3987e5; --carbs: #d95926; --fat: #199e70; --shadow: none;
}

* { 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, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) 0 calc(76px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.date-label { background: none; border: 0; font-weight: 650; font-size: 17px; padding: 6px 12px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: var(--surface-2); font-size: 22px; line-height: 1;
  display: grid; place-items: center; cursor: pointer;
}
.topbar.hide-nav .icon-btn { visibility: hidden; }

main { max-width: 560px; margin: 0 auto; padding: 0 16px 72px; }
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 12px;
}
.card-title { font-size: 15px; font-weight: 650; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

/* riepilogo */
.summary { display: flex; align-items: center; gap: 20px; }
.ring-wrap { position: relative; width: 132px; height: 132px; flex: none; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--surface-2); stroke-width: 10; }
.ring-fg { fill: none; stroke: var(--accent); stroke-width: 10; stroke-linecap: round; transition: stroke-dashoffset .4s ease; }
.ring-fg.over { stroke: var(--over); }
.ring-text { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring-num { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; }
.ring-sub { font-size: 12px; color: var(--muted); }
.summary-side { flex: 1; display: grid; gap: 10px; }
.kv { display: flex; justify-content: space-between; border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.kv:last-child { border: 0; padding: 0; }
.kv span { color: var(--text-2); }
.kv b { font-variant-numeric: tabular-nums; }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.macro-name { font-size: 13px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.macro-val { font-size: 14px; font-weight: 600; margin: 2px 0 6px; font-variant-numeric: tabular-nums; }
.macro-val small { font-weight: 400; color: var(--muted); }
.bar { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 3px; }

/* pasti */
.meal-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.meal-head h3 { margin: 0; font-size: 15px; }
.meal-head span { font-size: 14px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.entry {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.entry-main { flex: 1; min-width: 0; }
.entry-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry-sub { font-size: 12px; color: var(--muted); }
.entry-kcal { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.entry-del { background: none; border: 0; color: var(--muted); font-size: 18px; width: 32px; height: 32px; cursor: pointer; }
.add-inline { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 8px 0 0; cursor: pointer; }

/* bottoni / form */
.btn {
  border: 0; border-radius: 12px; padding: 12px 16px; min-height: 44px;
  background: var(--surface-2); font-weight: 600; cursor: pointer; text-align: center;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn:disabled { opacity: .5; }
.grow { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
.form { display: grid; gap: 12px; }
.form label { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }
.form label.check { display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--text); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
input, select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--surface);
  border-radius: 10px; padding: 10px 12px; font-size: 16px; min-height: 44px;
}
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
textarea { resize: vertical; }
.calc { background: var(--surface-2); border-radius: 12px; padding: 12px; font-size: 14px; display: grid; gap: 4px; }
.calc b { font-variant-numeric: tabular-nums; }
.file-btn { flex: 1; }
.row > .btn { flex: 1; }

/* tab bar e fab */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab { flex: 1; background: none; border: 0; color: var(--muted); font-size: 11px; display: grid; justify-items: center; gap: 2px; padding: 4px; cursor: pointer; }
.tab-ico { font-size: 20px; line-height: 1.1; }
.tab.active { color: var(--accent); }
.fab {
  position: fixed; right: 18px; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 7;
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); font-size: 32px; line-height: 1;
  box-shadow: 0 6px 20px rgba(42,120,214,.35); cursor: pointer;
}
.fab[hidden] { display: none; }

/* segmented */
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.seg-btn { flex: 1; border: 0; background: none; padding: 8px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--text-2); cursor: pointer; }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.4);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; max-height: 92dvh; overflow-y: auto;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom));
  animation: up .22s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: .6; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 8px; }
.sheet-head h2 { font-size: 18px; margin: 0; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.sheet-head select { width: auto; min-height: 36px; padding: 4px 8px; font-weight: 600; }
.add-pane { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }

.list { display: grid; grid-template-columns: minmax(0, 1fr); }
.item {
  display: flex; align-items: center; gap: 10px; text-align: left; width: 100%;
  background: none; border: 0; border-bottom: 1px solid var(--line); padding: 12px 2px; cursor: pointer;
}
.item-main { flex: 1; min-width: 0; }
.item-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-kcal { font-size: 13px; color: var(--text-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.list-label { font-size: 12px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 10px 0 0; }

.scanner { position: relative; border-radius: 14px; overflow: hidden; background: #000; aspect-ratio: 4/3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame { position: absolute; inset: 30% 12%; border: 2px solid rgba(255,255,255,.85); border-radius: 10px; }

.photo-btn { width: 100%; }
.ai-preview { width: 100%; max-height: 220px; object-fit: cover; border-radius: 12px; }
.ai-item { background: var(--surface); border-radius: 12px; padding: 10px 12px; display: grid; gap: 6px; }
.ai-item-head { display: flex; gap: 8px; align-items: center; }
.ai-item-head input[type="text"] { flex: 1; }
.ai-item .grams { width: 90px; flex: none; }
.ai-note { font-size: 13px; color: var(--text-2); background: var(--surface-2); padding: 10px 12px; border-radius: 10px; }

.portion-row input { font-size: 22px; font-weight: 600; }
.unit { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 14px; cursor: pointer; min-height: 36px; }
.portion-calc { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 6px 0 14px; text-align: center; }
.portion-calc div { background: var(--surface); border-radius: 10px; padding: 8px 4px; }
.portion-calc b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.portion-calc span { font-size: 11px; color: var(--muted); }

/* storico */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .goal-line { stroke: var(--text-2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .axis-text { fill: var(--muted); font-size: 11px; }
.chart .bar-mark { fill: var(--accent); }
.chart .bar-mark.over { fill: var(--over); }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover + .bar-mark, .chart .bar-mark.hl { opacity: .8; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--text); color: var(--surface);
  font-size: 12px; padding: 6px 8px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, -100%);
}
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); margin-top: 8px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 14px; height: 0; border-top: 1.5px dashed var(--text-2); }
.legend .sw { width: 10px; height: 10px; border-radius: 2px; border: 0; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 8px 4px; border-bottom: 1px solid var(--line); text-align: right; }
.table th:first-child, .table td:first-child { text-align: left; }
.table th { font-weight: 600; font-size: 12px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; bottom: calc(150px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 12px; z-index: 30;
  font-size: 14px; max-width: calc(100% - 32px);
}
.spinner { display: inline-block; width: 16px; height: 16px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { color: var(--muted); font-size: 14px; padding: 16px 0; text-align: center; }
