:root {
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef2ee;
  --text: #17211b;
  --muted: #5f6d64;
  --border: #dce3dd;
  --accent: #2f8f5b;
  --accent-strong: #226e45;
  --accent-text: #ffffff;
  --danger: #c0392b;
  --warn: #d98324;
  --protein: #3b7dd8;
  --carbs: #d98324;
  --fat: #a05bc4;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 40, 28, 0.06), 0 4px 14px rgba(20, 40, 28, 0.05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1411;
    --surface: #171f1a;
    --surface-2: #1f2a23;
    --text: #e8eee9;
    --muted: #93a398;
    --border: #2b3830;
    --accent: #43b077;
    --accent-strong: #5cc78e;
    --accent-text: #08170e;
    --danger: #ea6a5c;
    --warn: #e8a04c;
    --protein: #6aa3f0;
    --carbs: #e8a04c;
    --fat: #c68ae6;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
.loading { padding: 3rem 1rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.shell { max-width: 720px; margin: 0 auto; padding: 0 16px 96px; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 0;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.25rem; letter-spacing: -0.02em; }
.logo {
  width: 32px; height: 32px; border-radius: 9px; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-weight: 800;
}
.whoami { display: flex; align-items: center; gap: 10px; font-size: 0.9rem; color: var(--muted); }
.nav { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 16px; }
.nav button {
  flex: 1; border: 0; background: transparent; padding: 9px 8px; border-radius: 9px; cursor: pointer;
  color: var(--muted); font-weight: 600;
}
.nav button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

@media (max-width: 640px) {
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; margin: 0; border-radius: 0; z-index: 5;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .nav button.active { background: var(--surface-2); box-shadow: none; color: var(--accent-strong); }
  .whoami .who-name { display: none; }
}

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.card > h2 { margin-bottom: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.stack { display: grid; gap: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.mt { margin-top: 14px; }
.mb { margin-bottom: 14px; }

/* ---------- forms & buttons ---------- */
label.field { display: grid; gap: 5px; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
input, select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); min-height: 44px;
}
input:focus, select:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent);
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 9px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn.danger.solid { background: var(--danger); color: #fff; }
.btn.small { min-height: 36px; padding: 5px 12px; font-size: 0.9rem; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn[disabled] { opacity: 0.6; cursor: default; }
.icon-btn {
  width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent; cursor: pointer;
  color: var(--muted); font-size: 1rem; display: grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.error { color: var(--danger); font-size: 0.9rem; min-height: 1.2em; }
.notice {
  background: var(--surface-2); border-left: 4px solid var(--accent); padding: 10px 12px;
  border-radius: 8px; font-size: 0.9rem;
}

/* ---------- auth screens ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: 100%; max-width: 380px; padding: 24px; }
.auth .brand { justify-content: center; margin-bottom: 6px; font-size: 1.6rem; }
.auth .sub { text-align: center; color: var(--muted); margin-bottom: 18px; }

/* ---------- log view ---------- */
.datebar { display: flex; align-items: center; gap: 6px; margin-bottom: 14px; }
.datebar .date-label { flex: 1; text-align: center; font-weight: 700; font-size: 1.1rem; }
.datebar .date-label small { display: block; font-weight: 500; color: var(--muted); font-size: 0.8rem; }
.datebar input[type="date"] { width: 0; height: 0; min-height: 0; padding: 0; border: 0; opacity: 0; position: absolute; }

.cal-total { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.cal-total .big { font-size: 2.4rem; font-weight: 800; letter-spacing: -0.03em; }
.bar { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.3s; }
.bar.over > span { background: var(--warn); }
.macro { display: grid; gap: 5px; font-size: 0.85rem; }
.macro .m-head { display: flex; flex-wrap: wrap; justify-content: space-between; column-gap: 6px; color: var(--muted); }
.macro .m-head b { color: var(--text); }
.macro .bar { height: 7px; }
.macro.protein .bar > span { background: var(--protein); }
.macro.carbs .bar > span { background: var(--carbs); }
.macro.fat .bar > span { background: var(--fat); }

.meal { margin-bottom: 14px; }
.meal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.meal-head .meal-name { font-weight: 700; text-transform: capitalize; }
.meal-head .meal-cal { color: var(--muted); font-size: 0.9rem; margin-left: 8px; font-weight: 500; }
.entry {
  display: flex; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--border);
}
.entry:first-child { border-top: 0; }
.entry .e-main { flex: 1; min-width: 0; }
.entry .e-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .e-macros { font-size: 0.8rem; color: var(--muted); }
.entry .e-cal { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); font-size: 0.9rem; padding: 6px 0; }

/* ---------- history ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; }
.stat .v { font-size: 1.4rem; font-weight: 800; letter-spacing: -0.02em; }
.stat .l { font-size: 0.8rem; color: var(--muted); }
.day-row {
  display: grid; grid-template-columns: 92px 1fr 64px; align-items: center; gap: 10px;
  width: 100%; padding: 9px 4px; border: 0; border-top: 1px solid var(--border); background: transparent;
  cursor: pointer; text-align: left; border-radius: 0;
}
.day-row:first-child { border-top: 0; }
.day-row:hover { background: var(--surface-2); }
.day-row .d-label { font-size: 0.9rem; font-weight: 600; }
.day-row .d-cal { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.day-row.none .d-cal, .day-row.none .d-label { color: var(--muted); font-weight: 500; }

/* ---------- admin ---------- */
.user-row { display: flex; align-items: center; gap: 12px; padding: 12px 0; border-top: 1px solid var(--border); flex-wrap: wrap; }
.user-row:first-child { border-top: 0; }
.user-row .u-main { flex: 1; min-width: 160px; }
.user-row .u-name { font-weight: 700; }
.user-row .u-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  display: inline-block; font-size: 0.72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); margin-left: 6px; vertical-align: middle; text-transform: uppercase; letter-spacing: 0.04em;
}
.badge.admin { background: var(--accent); color: var(--accent-text); }
.badge.warn { background: var(--warn); color: #201200; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 18px; background: var(--surface); color: var(--text);
  padding: 0; width: min(440px, calc(100vw - 24px)); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
dialog form { padding: 20px; display: grid; gap: 14px; max-height: calc(100vh - 40px); overflow-y: auto; }
dialog .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
.chip {
  flex: none; border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px;
  padding: 5px 12px; font-size: 0.85rem; cursor: pointer; white-space: nowrap; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
}
.chip:hover { border-color: var(--accent); }
.pw-row { display: flex; gap: 8px; }
.pw-row input { flex: 1; }

#toast {
  position: fixed; left: 50%; bottom: 84px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 0.9rem; z-index: 20;
  transition: opacity 0.2s, transform 0.2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (min-width: 641px) { #toast { bottom: 28px; } }

.datebar > .btn.ghost:not(.date-label) { font-size: 1.6rem; padding: 0 12px; }
