:root {
  --bg: #ffffff;
  --bg-soft: #f4f5f7;
  --bg-card: #ffffff;
  --fg: #0f172a;
  --fg-soft: #475569;
  --fg-muted: #94a3b8;
  --line: #e2e8f0;
  --accent: #2563eb;
  --cal: #f59e0b;
  --pro: #ef4444;
  --carb: #3b82f6;
  --fat: #10b981;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(15,23,42,0.06), 0 4px 12px rgba(15,23,42,0.05);
  --radius: 14px;
  --radius-sm: 10px;
  --pad: 16px;
  --tabbar-h: 64px;
  --topbar-h: 56px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro", "Segoe UI", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --bg-soft: #111c33;
    --bg-card: #18243f;
    --fg: #f1f5f9;
    --fg-soft: #cbd5e1;
    --fg-muted: #64748b;
    --line: #1f2c4a;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 4px 12px rgba(0,0,0,0.2);
  }
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  height: 100%;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-top: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

button {
  font: inherit;
  color: inherit;
  background: transparent;
  border: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
input, select, textarea {
  font: inherit;
  color: inherit;
}

/* ---------- top bar ---------- */
.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  align-items: center;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
  z-index: 50;
}
.apptitle {
  margin: 0;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.iconbtn {
  width: 44px; height: 44px;
  margin: 0 auto;
  display: grid; place-items: center;
  border-radius: 12px;
  font-size: 20px;
  color: var(--fg-soft);
}
.iconbtn:hover { background: var(--bg-soft); }
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: grid; place-items: center;
  font-size: 14px; font-weight: 600;
}

/* ---------- main view ---------- */
.view {
  flex: 1;
  padding: var(--pad);
  max-width: 720px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
}

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
  z-index: 50;
}
.tab {
  display: grid;
  place-items: center;
  grid-template-rows: auto auto;
  gap: 2px;
  color: var(--fg-muted);
  font-size: 11px;
  font-weight: 500;
}
.tab.active { color: var(--accent); }
.tab-icon { font-size: 20px; line-height: 1; }
.tab-icon.big { font-size: 28px; }

/* ---------- cards ---------- */
.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.card h2 {
  margin: 0 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-muted);
  font-weight: 600;
}

/* ---------- donuts ---------- */
.donut-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.donut-tile {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: var(--shadow);
}
.donut-tile .label {
  font-size: 12px;
  color: var(--fg-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.donut-svg { width: 110px; height: 110px; }
.donut-center {
  display: flex; flex-direction: column; align-items: center;
  font-size: 14px; font-weight: 600;
}
.donut-center .num { font-size: 20px; font-weight: 700; }
.donut-center .unit { font-size: 11px; color: var(--fg-muted); font-weight: 500; }
.donut-meta { margin-top: 6px; font-size: 11px; color: var(--fg-muted); }

/* ---------- list rows ---------- */
.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--line);
  align-items: center;
  cursor: pointer;
}
.row:last-child { border-bottom: none; }
.row .title { font-weight: 600; font-size: 15px; }
.row .sub { font-size: 12px; color: var(--fg-muted); margin-top: 2px; }
.row .macros { font-size: 12px; color: var(--fg-soft); text-align: right; line-height: 1.35; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: white;
  font-weight: 600;
  font-size: 15px;
}
.btn:active { transform: translateY(1px); }
.btn.secondary {
  background: var(--bg-soft);
  color: var(--fg);
  border: 1px solid var(--line);
}
.btn.danger { background: var(--danger); }
.btn.block { display: flex; width: 100%; }
.btn.small { padding: 8px 12px; font-size: 13px; }

/* ---------- forms ---------- */
.field {
  display: block;
  margin-bottom: 12px;
}
.field label {
  display: block;
  font-size: 12px;
  color: var(--fg-muted);
  margin-bottom: 4px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.field input, .field select, .field textarea {
  width: 100%;
  padding: 12px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--bg-soft);
  box-sizing: border-box;
}
.field-row {
  display: grid;
  grid-template-columns: 1fr 100px;
  gap: 10px;
}
.macro-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

/* ---------- entry method tiles ---------- */
.entry-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}
.entry-tile {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 14px;
  text-align: center;
  font-weight: 600;
  font-size: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow);
}
.entry-tile .em { font-size: 28px; line-height: 1; }
.entry-tile .sub { font-size: 11px; color: var(--fg-muted); font-weight: 500; }

/* ---------- search ---------- */
.search-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--bg-soft);
  margin-bottom: 12px;
  box-sizing: border-box;
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 100;
  padding: 0;
}
@media (min-width: 600px) {
  .modal-backdrop { align-items: center; padding: 20px; }
}
.modal {
  background: var(--bg-card);
  border-radius: 18px 18px 0 0;
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 18px;
  box-sizing: border-box;
}
@media (min-width: 600px) { .modal { border-radius: 18px; } }
.modal h2 { margin: 0 0 12px; font-size: 18px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

/* ---------- camera ---------- */
.camera-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  background: black;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
}
.camera-wrap video, .camera-wrap canvas {
  width: 100%; height: 100%;
  object-fit: cover;
}
.scan-overlay {
  position: absolute;
  inset: 20% 10%;
  border: 2px solid rgba(255,255,255,0.85);
  border-radius: 8px;
  pointer-events: none;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 200;
  box-shadow: var(--shadow);
}

/* ---------- misc ---------- */
.muted { color: var(--fg-muted); }
.empty { text-align: center; color: var(--fg-muted); padding: 40px 20px; }
.empty .em { font-size: 32px; margin-bottom: 8px; }
.hr { height: 1px; background: var(--line); margin: 12px 0; border: 0; }
.tag {
  display: inline-block;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--fg-soft);
}
.spinner {
  width: 22px; height: 22px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.center { display: flex; align-items: center; justify-content: center; padding: 24px; }

.swatch {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 3px;
  margin-right: 6px;
}
.swatch.cal { background: var(--cal); }
.swatch.pro { background: var(--pro); }
.swatch.carb { background: var(--carb); }
.swatch.fat { background: var(--fat); }
