:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #17201b;
  --muted: #6b7570;
  --line: #e4e8e6;
  --accent: #16a34a;
  --accent-soft: #dcf5e4;
  --danger: #dc2626;
  --p: #2563eb;
  --c: #d97706;
  --f: #9333ea;
  --over: #dc2626;
  --input: #f1f3f2;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 4px 14px rgba(0, 0, 0, .04);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1210;
    --card: #181d1a;
    --text: #e8eeea;
    --muted: #93a099;
    --line: #2a322e;
    --accent: #22c55e;
    --accent-soft: #163322;
    --p: #60a5fa;
    --c: #fbbf24;
    --f: #c084fc;
    --over: #f87171;
    --input: #222924;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
.hidden { display: none !important; }

/* Barra superior */
.topbar {
  position: sticky; top: 0; z-index: 10;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px;
}
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; }
.daynav { display: flex; align-items: center; justify-content: space-between; width: 100%; max-width: 520px; }
.daynav .day { font-weight: 650; font-size: 17px; padding: 6px 12px; border-radius: 10px; text-transform: capitalize; }
.daynav .arrow { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; color: var(--accent); }
.daynav .arrow:active, .daynav .day:active { background: var(--line); }

/* Contenido */
.view { max-width: 560px; margin: 0 auto; padding: 4px 16px calc(var(--safe-bottom) + 96px); }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.card h2 { font-size: 17px; margin: 0; font-weight: 650; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.card-head .kcal, .muted { color: var(--muted); }
.small { font-size: 13px; }

/* Resumen */
.summary { display: grid; gap: 16px; }
.summary-top { display: flex; align-items: center; gap: 18px; }
.ring-wrap { position: relative; width: 132px; height: 132px; flex: none; }
.ring-wrap svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { stroke: var(--line); }
.ring-fg { stroke: var(--accent); transition: stroke-dashoffset .5s ease; }
.ring-fg.over { stroke: var(--over); }
.ring-label { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring-label strong { font-size: 28px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.ring-label strong.over { color: var(--over); }
.ring-label span { font-size: 12px; color: var(--muted); margin-top: 4px; }
.equation { display: grid; gap: 8px; flex: 1; min-width: 0; }
.eq-row { display: flex; justify-content: space-between; font-size: 14px; color: var(--muted); }
.eq-row b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 600; }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.macro .label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.macro .val { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; margin: 2px 0 5px; }
.macro .val small { color: var(--muted); font-weight: 400; }
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 3px; transition: width .4s ease; }
.macro.p .bar > i { background: var(--p); }
.macro.c .bar > i { background: var(--c); }
.macro.f .bar > i { background: var(--f); }
.bar > i.over { background: var(--over) !important; }

/* Comidas */
.entries { list-style: none; margin: 0 -16px; padding: 0; }
.entries li {
  display: flex; justify-content: space-between; gap: 12px; align-items: center;
  padding: 10px 16px; border-top: 1px solid var(--line); cursor: pointer;
}
.entries li:active { background: var(--input); }
.entries .name { min-width: 0; }
.entries .name div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entries small { display: block; color: var(--muted); font-size: 12.5px; }
.entries .nums { text-align: right; flex: none; }
.entries .nums b { font-variant-numeric: tabular-nums; font-weight: 600; }
.meal-foot { display: flex; justify-content: space-between; align-items: center; margin: 0 -16px -16px; padding: 4px 8px; border-top: 1px solid var(--line); }
.meal-foot.empty { border-top: 0; margin-top: -4px; }
.link { color: var(--accent); font-weight: 600; padding: 10px 8px; font-size: 15px; }
.link.muted { color: var(--muted); font-weight: 500; font-size: 14px; }
.meal-macros { font-size: 12px; color: var(--muted); padding: 0 8px; }

/* Barra de pestañas */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 8px calc(var(--safe-bottom) + 6px);
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button { display: grid; justify-items: center; gap: 2px; padding: 4px 0; color: var(--muted); font-size: 11px; font-weight: 500; }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.active { color: var(--accent); }

/* Formularios */
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.input, input[type=text], input[type=search], input:not([type]), select {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid transparent; background: var(--input); font-size: 16px; outline: none;
  -webkit-appearance: none; appearance: none;
}
select { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
input:focus, select:focus { border-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stack { display: grid; gap: 12px; }
.btn {
  display: block; width: 100%; min-height: 48px; border-radius: 14px; font-weight: 650; font-size: 16px;
  background: var(--input); color: var(--text);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.danger { background: transparent; color: var(--danger); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn:active { filter: brightness(.95); }
.row-btns { display: flex; gap: 10px; }
.row-btns .btn { flex: 1; }
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 15px; transition: .2s; }
.switch span::after { content: ""; position: absolute; width: 26px; height: 26px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.seg { display: inline-flex; background: var(--input); border-radius: 10px; padding: 3px; }
.seg button { padding: 6px 14px; border-radius: 8px; font-size: 14px; color: var(--muted); font-weight: 500; }
.seg button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* Hoja modal */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 40; animation: fade .2s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  max-width: 600px; margin: 0 auto;
  height: calc(100% - var(--safe-top) - 12px);
  background: var(--bg); border-radius: 18px 18px 0 0;
  display: flex; flex-direction: column; animation: up .25s ease;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.sheet-head { display: grid; grid-template-columns: 56px 1fr 56px; align-items: center; padding: 10px 8px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 17px; text-align: center; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sheet-head .icon { width: 44px; height: 44px; border-radius: 50%; font-size: 20px; color: var(--accent); display: grid; place-items: center; }
.sheet-body { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px calc(var(--safe-bottom) + 24px); }
.searchbar { display: flex; gap: 8px; }
.searchbar input { flex: 1; }
.icon-btn { width: 48px; min-height: 44px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; flex: none; }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.chips { display: flex; gap: 8px; margin: 12px 0 4px; flex-wrap: wrap; }
.chip { padding: 8px 14px; border-radius: 20px; background: var(--card); box-shadow: var(--shadow); border: 1px solid var(--line); font-size: 14px; font-weight: 550; }
.section-title { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 6px; }
.results { list-style: none; margin: 0; padding: 0; background: var(--card); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.results li { display: flex; justify-content: space-between; gap: 12px; align-items: center; padding: 11px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.results li:first-child { border-top: 0; }
.results li:active { background: var(--input); }
.results .name { min-width: 0; }
.results .name div { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.results small { color: var(--muted); font-size: 12.5px; display: block; }
.results .k { text-align: right; flex: none; font-variant-numeric: tabular-nums; }
.results .k small { white-space: nowrap; }
.tag { display: inline-block; font-size: 10.5px; padding: 1px 6px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); font-weight: 600; margin-left: 4px; vertical-align: 1px; }
.empty-state { text-align: center; color: var(--muted); padding: 22px 12px; font-size: 14px; }
.spinner { width: 20px; height: 20px; border: 2.5px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 0 auto 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
.food-title h3 { margin: 4px 0 2px; font-size: 20px; }
.food-title p { margin: 0; color: var(--muted); font-size: 14px; }
.preview { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; background: var(--card); border-radius: 14px; padding: 14px 6px; box-shadow: var(--shadow); }
.preview b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.preview span { font-size: 12px; color: var(--muted); }
.preview .pk b { color: var(--accent); }
.preview .pp b { color: var(--p); }
.preview .pc b { color: var(--c); }
.preview .pf b { color: var(--f); }

/* Escáner */
.scanner { position: relative; border-radius: 16px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-frame { position: absolute; left: 12%; right: 12%; top: 32%; bottom: 32%; border: 3px solid rgba(255,255,255,.9); border-radius: 12px; box-shadow: 0 0 0 999px rgba(0,0,0,.3); }
.scan-frame::after { content: ""; position: absolute; left: 6px; right: 6px; top: 50%; height: 2px; background: #ef4444; animation: scan 1.6s ease-in-out infinite alternate; }
@keyframes scan { from { transform: translateY(-20px); } to { transform: translateY(20px); } }

/* Gráficos */
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart text { fill: var(--muted); font-size: 10px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .goal { stroke: var(--muted); stroke-dasharray: 4 4; stroke-width: 1; }
.chart .barv { fill: var(--accent); }
.chart .barv.over { fill: var(--over); }
.chart .barv.empty { fill: var(--line); }
.chart .wline { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .wdot { fill: var(--card); stroke: var(--accent); stroke-width: 2; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; margin-top: 12px; }
.stats b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.stats span { font-size: 11.5px; color: var(--muted); }
.list-plain { list-style: none; padding: 0; margin: 8px -16px -16px; }
.list-plain li { display: flex; justify-content: space-between; padding: 10px 16px; border-top: 1px solid var(--line); font-size: 15px; }
.list-plain li button { color: var(--danger); font-size: 14px; }
.hint { font-size: 13px; color: var(--muted); margin: 6px 2px 0; line-height: 1.45; }
.result-box { background: var(--accent-soft); border-radius: 12px; padding: 12px 14px; font-size: 14px; }
.result-box b { font-size: 18px; }
.foot-note { text-align: center; font-size: 12px; color: var(--muted); padding: 8px 12px 0; line-height: 1.5; }
.foot-note a { color: inherit; }

/* Aviso */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 84px); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #1f2937; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  transition: .25s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
