:root {
  --bg: #F4F2EE;
  --card: #FFFFFF;
  --text: #1C1B1A;
  --muted: #6F6B66;
  --line: #E3DFD8;
  --primary: #1C1B1A;
  --on-primary: #FFFFFF;
  --ok: #2F9E6B;
  --warn: #D99A1E;
  --over: #D64545;
  --accent: #5B8DEF;
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151413;
    --card: #211F1E;
    --text: #F2EFEA;
    --muted: #A39E97;
    --line: #34312F;
    --primary: #F2EFEA;
    --on-primary: #151413;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
a { color: inherit; }
h1 { font-size: 1.6rem; margin: .4rem 0 1rem; }
h2 { font-size: 1.05rem; margin: 0; }
code { font-size: .9em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

.top {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .75rem 16px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; text-decoration: none; letter-spacing: -.01em; }
.top nav { display: flex; gap: .25rem; overflow-x: auto; min-width: 0; }
.top nav a, .seg a {
  text-decoration: none; padding: .35rem .7rem; border-radius: 999px; color: var(--muted); white-space: nowrap; font-size: .95rem;
}
.top nav a[aria-current], .seg a[aria-current] { background: var(--card); color: var(--text); box-shadow: 0 0 0 1px var(--line); }

main { max-width: 760px; margin: 0 auto; padding: 16px; }
.card {
  background: var(--card); border-radius: var(--radius); padding: 16px;
  box-shadow: 0 0 0 1px var(--line); margin-bottom: 14px;
}
.alert { background: color-mix(in srgb, var(--over) 15%, var(--card)); color: var(--over); padding: .75rem 1rem; border-radius: 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: .75rem; flex-wrap: wrap; margin-bottom: .75rem; }

button, input, select { font: inherit; color: inherit; }
input, select {
  width: 100%; padding: .65rem .8rem; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); min-height: 44px;
}
input[type=color] { padding: .25rem; }
input[type=checkbox] { width: auto; min-height: 0; }
button {
  border: 0; border-radius: 12px; padding: .6rem 1rem; min-height: 44px; cursor: pointer;
  background: var(--bg); box-shadow: 0 0 0 1px var(--line); touch-action: manipulation;
}
button:disabled { opacity: .35; cursor: default; }
button.primary { background: var(--primary); color: var(--on-primary); box-shadow: none; font-weight: 600; }
button.danger { color: var(--over); }
button.ghost { background: transparent; }
button.small { min-height: 32px; padding: .3rem .6rem; }
form { margin: 0; }
.busy { opacity: .6; pointer-events: none; }

/* counters */
.counters { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-bottom: 14px; }
.counter { margin: 0; display: flex; flex-direction: column; gap: .4rem; border-top: 4px solid var(--accent); }
.counter-head { display: flex; align-items: center; gap: .5rem; }
.icon { font-size: 1.4rem; }
.count { display: flex; align-items: baseline; gap: .35rem; }
.count .n { font-size: 3.2rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -.03em; }
.count .limit { color: var(--muted); font-size: 1.2rem; }
.meter { height: 8px; border-radius: 99px; background: var(--line); overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--ok); border-radius: inherit; transition: width .25s; }
.s-warn .meter span { background: var(--warn); }
.s-at .meter span, .s-over .meter span { background: var(--over); }
.s-over .count .n { color: var(--over); }
.counter-actions { display: grid; grid-template-columns: 1fr 2.2fr; gap: .5rem; margin-top: .25rem; }
.counter-actions button { width: 100%; min-height: 56px; font-size: 1.4rem; }
button.plus { background: var(--accent); color: #fff; box-shadow: none; font-weight: 700; }

/* food */
.food-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: .75rem; }
.kcal { font-weight: 600; font-variant-numeric: tabular-nums; }
.food-form { display: grid; grid-template-columns: 1fr 7.5rem 5.5rem auto; gap: .5rem; }
@media (max-width: 560px) {
  .food-form { grid-template-columns: 1fr 1fr; }
  .food-form input[name=name] { grid-column: 1 / -1; }
  .food-form button { grid-column: 1 / -1; }
}
.chips { display: flex; gap: .4rem; overflow-x: auto; padding: .75rem 0 .25rem; scrollbar-width: none; }
.chip { min-height: 34px; padding: .3rem .75rem; border-radius: 999px; white-space: nowrap; font-size: .9rem; }
.food-list { list-style: none; margin: .5rem 0 0; padding: 0; }
.food-list li { display: grid; grid-template-columns: 1.8rem 1fr auto auto; align-items: center; gap: .5rem; padding: .5rem 0; border-top: 1px solid var(--line); }
.food-list .name { overflow-wrap: anywhere; }

/* history */
.seg { display: flex; gap: .25rem; }
.bars { display: flex; align-items: flex-end; gap: 2px; height: 110px; margin-top: .5rem; }
.bar { flex: 1; min-height: 2px; background: var(--accent); border-radius: 3px 3px 0 0; opacity: .85; }
.bar.over { background: var(--over); }
table.history { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.history th, .history td { padding: .45rem .3rem; text-align: right; border-bottom: 1px solid var(--line); }
.history th:first-child, .history td:first-child { text-align: left; }

/* forms */
.grid-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .6rem; align-items: end; }
.grid-form label:first-child { grid-column: 1 / -1; }
label { display: grid; gap: .25rem; font-size: .85rem; color: var(--muted); }
label input, label select { color: var(--text); font-size: 1rem; }
label.check { display: flex; align-items: center; gap: .5rem; min-height: 44px; }
.actions { display: flex; gap: .5rem; justify-content: flex-end; grid-column: 1 / -1; }
.stack { display: grid; gap: .8rem; }
.tracker-edit { border-left: 4px solid var(--accent); display: grid; gap: .5rem; }
.tracker-edit > form:last-child { justify-self: start; }
.archived { opacity: .6; }
.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login form { display: grid; gap: .6rem; margin-top: 1rem; }
