:root {
  --bg: #f3f6f4;
  --card: #ffffff;
  --text: #17211e;
  --muted: #64716d;
  --line: #e4e9e7;
  --field: #f3f6f4;
  --accent: #1f7a63;
  --accent-strong: #176450;
  --accent-soft: #e1f1eb;
  --on-accent: #ffffff;
  --phe: #a35f00;
  --phe-soft: #fbefdc;
  --danger: #c2372d;
  --shadow: 0 1px 2px rgba(20, 40, 34, .05), 0 4px 16px rgba(20, 40, 34, .06);
  --radius: 20px;
  --radius-sm: 12px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1413;
    --card: #182120;
    --text: #e9efed;
    --muted: #93a39e;
    --line: #26302e;
    --field: #111917;
    --accent: #5fcfac;
    --accent-strong: #7fdcbe;
    --accent-soft: #19332c;
    --on-accent: #06221a;
    --phe: #f0b35a;
    --phe-soft: #33261a;
    --danger: #ff8a80;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); }
h1 { font-size: 1.05rem; font-weight: 700; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 0 0 .5rem; letter-spacing: -.01em; }
h3 { font-size: .95rem; margin: 24px 0 0; padding-top: 16px; border-top: 1px solid var(--line); letter-spacing: -.01em; }
p.small { margin: 6px 0 0; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Top bar ---------- */

.topbar {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px); backdrop-filter: saturate(1.4) blur(14px);
  padding: env(safe-area-inset-top) 16px 0;
}
.titlerow {
  display: flex; align-items: center; justify-content: center; position: relative;
  max-width: 640px; margin: 0 auto; height: 48px;
}
.backbtn {
  position: absolute; left: -8px; display: flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; padding: 0; border: 0; border-radius: 50%;
  background: none; color: var(--accent);
}
.backbtn svg { width: 24px; height: 24px; stroke-width: 2.4; }

/* ---------- Bottom tab bar ---------- */

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex; justify-content: center;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(14px); backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tab {
  flex: 1; max-width: 140px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: 0; border-radius: 14px; padding: 6px 4px;
  color: var(--muted); font: inherit; font-size: .72rem; font-weight: 600; cursor: pointer;
}
.tab svg { width: 24px; height: 24px; padding: 0; }
.tab span { line-height: 1.2; }
.tab.active { color: var(--accent); }
.tab.active svg { stroke-width: 2.4; }

/* ---------- Layout ---------- */

main {
  max-width: 640px; margin: 0 auto;
  padding: 4px 16px calc(var(--nav-h) + 28px + env(safe-area-inset-bottom));
}
.view { display: none; }
.view.active { display: block; animation: fadein .18s ease-out; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px); } }
@media (prefers-reduced-motion: reduce) { .view.active { animation: none; } }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
}
@media (prefers-color-scheme: dark) { .card { border: 1px solid var(--line); } }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.row { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.row.between { justify-content: space-between; margin-top: 0; margin-bottom: 4px; }
.row.between h2 { margin: 0; }
.row.wrap { flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }

/* ---------- Forms ---------- */

label { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-top: 12px; }
label.check { display: flex; gap: 10px; align-items: center; color: var(--text); font-size: .95rem; font-weight: 500; }
input, select {
  display: block; width: 100%; margin-top: 6px; min-height: 46px;
  font: inherit; font-weight: 400; color: var(--text); background: var(--field);
  border: 1.5px solid var(--line); border-radius: var(--radius-sm); padding: 10px 12px;
  -webkit-appearance: none; appearance: none;
}
select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2364716d' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; background-size: 18px; padding-right: 34px;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="checkbox"] {
  -webkit-appearance: auto; appearance: auto; min-height: 0;
  width: 20px; height: 20px; accent-color: var(--accent);
}
label.check input { width: 20px; margin: 0; }
.row input { margin-top: 0; flex: 1; }

/* ---------- Buttons ---------- */

button, .filebtn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 0; background: var(--accent-soft); color: var(--accent);
  border-radius: var(--radius-sm); padding: 11px 16px; min-height: 44px; white-space: nowrap;
  transition: transform .08s ease, background-color .15s ease;
}
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .35; cursor: default; }
button:focus-visible, .filebtn:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.filebtn { display: inline-flex; align-items: center; margin: 0; font-size: 1rem; }
button.primary { background: var(--accent); color: var(--on-accent); }
button.big {
  width: 100%; padding: 16px; font-size: 1.05rem; border-radius: 16px;
  display: flex; align-items: center; justify-content: center; gap: 10px;
}
button.big svg { width: 24px; height: 24px; }
button.ghost { background: none; color: var(--accent); }
button.small { padding: 6px 12px; min-height: 34px; font-size: .85rem; border-radius: 10px; }
button.danger { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
button.ghost.danger { background: none; }
button.icon {
  background: none; color: var(--muted); padding: 0; min-height: 0;
  width: 32px; height: 32px; border-radius: 50%; font-size: .9rem; flex: none; opacity: .7;
}

/* ---------- Day / range navigation ---------- */

.daynav { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.daynav button {
  width: 40px; height: 40px; min-height: 0; padding: 0; border-radius: 50%;
  background: var(--card); color: var(--text); font-size: 1.4rem; line-height: 1; box-shadow: var(--shadow);
}
.daynav > div { text-align: center; }
.daylabel { font-weight: 700; font-size: 1.2rem; letter-spacing: -.01em; }
#day-picker {
  border: 0; background: none; padding: 0; margin: 0 auto; width: auto; min-height: 0;
  color: var(--muted); font-size: .85rem; text-align: center; box-shadow: none;
}

/* ---------- Summary ---------- */

.summary { display: flex; gap: 12px 16px; flex-wrap: wrap; }
.stat { flex: 1 1 120px; min-width: 0; }
.stat .num { font-size: 1.75rem; font-weight: 750; line-height: 1.1; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat .num small { font-size: .9rem; font-weight: 600; color: var(--muted); letter-spacing: 0; }
.stat .lbl { font-size: .8rem; color: var(--muted); margin-top: 2px; }
.stat.phe .num { color: var(--phe); }
.stat.over .num { color: var(--phe); }

#range-summary { display: grid; grid-template-columns: 1fr 1fr; }

/* Today with goals: a ring per goal (protein, phe), what is left underneath */
#day-summary { align-items: center; justify-content: space-around; text-align: center; }
#day-summary .stat { flex: 0 1 132px; }
.meter { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.meter-left { font-weight: 700; font-variant-numeric: tabular-nums; }
.meter-left span { font-weight: 500; color: var(--muted); }
.meter.phe .meter-left { color: var(--phe); }
.meter.over .meter-left, .meter.phe.over .meter-left { color: var(--danger); }
.split {
  flex: 1 1 100%; display: flex; justify-content: center; gap: 8px 20px; flex-wrap: wrap;
  padding-top: 12px; border-top: 1px solid var(--line); font-size: .9rem; color: var(--muted);
}
.split b { color: var(--text); font-variant-numeric: tabular-nums; }
.ring { position: relative; width: 132px; height: 132px; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { stroke-width: 10; }
.ring-track { stroke: var(--accent-soft); }
.ring-fill { stroke: var(--accent); transition: stroke-dashoffset .5s ease; }
.ring.over .ring-fill { stroke: var(--phe); }
.ring.phe .ring-track { stroke: var(--phe-soft); }
.ring.phe .ring-fill { stroke: var(--phe); }
.ring.phe .ring-text .num { color: var(--phe); }
.ring.phe.over .ring-fill { stroke: var(--danger); }
.ring.phe.over .ring-text .num { color: var(--danger); }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring-text .num { font-size: 1.9rem; font-weight: 750; line-height: 1; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.ring-text .num small { font-size: .9rem; font-weight: 600; color: var(--muted); }
.ring-text .lbl { font-size: .72rem; color: var(--muted); margin-top: 4px; }

/* ---------- Meals and lists ---------- */

.mealhead { display: flex; align-items: center; gap: 12px; }
.mealhead h2 { margin: 0; }
.mealhead .tot { font-size: .85rem; color: var(--muted); font-weight: 500; }
.meal .list { margin-top: 8px; }
.formula .mealhead button { flex: none; }
.addmeal {
  width: 38px; height: 38px; min-height: 0; padding: 0; border-radius: 50%; flex: none;
  font-size: 1.4rem; font-weight: 500; line-height: 1;
}

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.list .grow { flex: 1; min-width: 0; }
.grow { flex: 1; min-width: 0; }
.list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.list .meta { font-size: .8rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .amt { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.list .amt .phe { display: block; font-size: .78rem; color: var(--phe); font-weight: 600; }
.empty { color: var(--muted); font-size: .9rem; padding: 6px 0; }
.list li.empty { display: block; border: 0; }

/* ---------- Scanner ---------- */

#scanner { position: relative; margin-top: 12px; border-radius: 16px; overflow: hidden; background: #000; }
#scanner video { width: 100%; display: block; max-height: 50vh; object-fit: cover; }
.scanline {
  position: absolute; left: 12%; right: 12%; top: 50%; height: 2px; border-radius: 2px;
  background: #4ff0bd; box-shadow: 0 0 12px #4ff0bd; pointer-events: none;
}
#scan-stop { position: absolute; right: 10px; bottom: 10px; background: rgba(0,0,0,.55); color: #fff; min-height: 38px; padding: 8px 14px; }
#barcode-form button { background: var(--accent-soft); }
.status { min-height: 1.2em; margin: 8px 0 0; font-size: .9rem; color: var(--muted); }
.status:empty { display: none; }
.status.err { color: var(--danger); }

/* ---------- Food / dish forms ---------- */

.preview {
  margin-top: 14px; padding: 12px 14px; background: var(--accent-soft); color: var(--accent-strong);
  border-radius: var(--radius-sm); font-weight: 650;
}
.preview .phe { color: var(--phe); }
#food-form .row .primary, #dish-log .row .primary { flex: 1; }

.seg { display: flex; background: var(--card); border-radius: 14px; padding: 4px; margin-bottom: 8px; box-shadow: var(--shadow); }
.segbtn { flex: 1; background: none; color: var(--muted); border-radius: 10px; min-height: 38px; padding: 6px; }
.segbtn.active { background: var(--accent); color: var(--on-accent); }

/* ---------- History bars ---------- */

.bars { list-style: none; margin: 0; padding: 0; }
.bars li {
  display: grid; grid-template-columns: 5.2em 1fr 4.5em; gap: 10px; align-items: center;
  padding: 6px 4px; margin: 0 -4px; font-size: .88rem; cursor: pointer; border-radius: 8px;
}
.bars li:active { background: var(--accent-soft); }
.bars .lbl { color: var(--muted); }
.bars .track { height: 12px; background: var(--accent-soft); border-radius: 6px; overflow: hidden; position: relative; }
.bars .fill { height: 100%; background: var(--accent); border-radius: 6px; }
.bars .goal { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--phe); }
.bars .val { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; }
.bars .val .phe { display: block; font-size: .72rem; color: var(--phe); }
.bars .val .phe.over { color: var(--danger); font-weight: 750; }
.bars li.today .lbl { font-weight: 700; color: var(--accent); }

/* ---------- Toast and banner ---------- */

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 10px;
  background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 14px;
  font-weight: 600; font-size: .92rem; width: max-content; max-width: calc(100% - 32px); z-index: 10;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
.toast button { padding: 4px 10px; min-height: 30px; background: none; color: inherit; border: 1.5px solid currentColor; }

.banner { background: var(--accent-soft); color: var(--accent-strong); border-radius: var(--radius-sm); padding: 12px 14px; margin-bottom: 12px; font-weight: 600; }
.dishitems { margin: 8px 0 12px; }
label.inline { display: flex; align-items: center; gap: 4px; margin: 0; }
.gramsin { width: 4.8em; margin: 0; padding: 6px 8px; min-height: 36px; text-align: right; }

.brand { display: none; }

/* ---------- Wide screens (laptops, desktops) ----------
   Tabs become a left sidebar, and screens use two columns. */

@media (min-width: 900px) {
  :root { --side-w: 236px; }

  .tabs {
    top: 0; right: auto; width: var(--side-w);
    flex-direction: column; justify-content: flex-start; gap: 4px;
    padding: 24px 14px; border-top: 0; border-right: 1px solid var(--line);
    background: var(--card);
  }
  .brand {
    display: flex; align-items: center; gap: 10px;
    font-weight: 750; font-size: 1.1rem; letter-spacing: -.01em; padding: 0 10px 22px;
  }
  .brand img { border-radius: 9px; }
  .tab {
    flex: none; max-width: none; flex-direction: row; gap: 12px;
    padding: 10px 12px; font-size: .95rem; border-radius: 12px;
  }
  .tab svg { width: 22px; height: 22px; }
  .tab:hover { background: var(--bg); color: var(--text); }
  .tab.active { background: var(--accent-soft); color: var(--accent); }

  .topbar { margin-left: var(--side-w); padding: 0 40px; background: none; -webkit-backdrop-filter: none; backdrop-filter: none; }
  /* The sidebar is always there, so Back only shows inside a dish */
  .topbar { display: none; }
  body:has(#view-dish.active, #dish-banner:not([hidden])) .topbar:has(#back-btn:not([hidden])) { display: block; }
  .topbar h1 { display: none; }
  .titlerow { max-width: 1080px; justify-content: flex-start; height: 56px; }
  .backbtn { position: static; width: auto; height: 40px; padding: 0 14px 0 8px; border-radius: 12px; gap: 2px; }
  .backbtn::after { content: "Back"; font-weight: 600; }
  .backbtn:hover { background: var(--accent-soft); }

  main { margin-left: var(--side-w); max-width: calc(1080px + 80px); padding: 32px 40px 48px; }
  body:has(#view-dish.active) main { padding-top: 8px; }

  .card { padding: 20px 22px; }
  .daylabel { font-size: 1.4rem; }

  /* Today: summary across the top, meals in a 2 x 2 grid */
  #view-today { display: none; }
  #view-today.active { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
  #view-today > .daynav, #view-today > .summary, #view-today > .formula { grid-column: 1 / -1; }
  #view-today > .formula { margin-bottom: 16px; }
  #view-today .daynav { justify-content: center; gap: 24px; margin-bottom: 16px; }
  #view-today > .summary { margin-bottom: 16px; }
  #meals { display: contents; }
  #meals .card { margin-bottom: 16px; }
  #day-summary { justify-content: center; column-gap: 96px; }
  .split { flex: none; border-top: 0; padding-top: 0; flex-direction: column; align-items: flex-start; text-align: left; font-size: 1rem; }

  /* Two-column screens: Add food, a dish, Settings */
  .view.cols.active { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
  .view.cols > .banner, .view.cols > p { grid-column: 1 / -1; margin: 0; }
  .view.cols .card { margin-bottom: 16px; }
  .view.cols > .card { margin-bottom: 0; }
  #view-add > .col:first-of-type { position: sticky; top: 24px; }

  /* History: controls in one row, four numbers across */
  .rangebar { display: flex; align-items: center; gap: 24px; margin-bottom: 16px; }
  .rangebar .seg { margin: 0; flex: 0 0 240px; }
  .rangebar .daynav { flex: 1; margin: 0; justify-content: flex-end; gap: 16px; }
  #range-summary { grid-template-columns: repeat(4, 1fr); }
  .bars li { grid-template-columns: 7em 1fr 6em; }
  .bars li:hover { background: var(--bg); }

  .list li:hover .icon { opacity: 1; }
  button:hover:not(:disabled) { filter: brightness(1.05); }
  button.primary:hover:not(:disabled) { background: var(--accent-strong); filter: none; }
  .daynav button:hover:not(:disabled) { background: var(--accent-soft); color: var(--accent); }

  .toast { left: calc(50% + var(--side-w) / 2); bottom: 28px; }
}
