/* Mobile-first styles. Colours are tokens, redefined for dark mode. */

:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-2: #f1f5f9;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #0f766e;
  --accent-contrast: #ffffff;
  --accent-soft: #ccfbf1;
  --over: #b45309;
  --danger: #b91c1c;
  --warning-bg: #fef3c7;
  --warning-text: #78350f;
  --protein: #2563eb;
  --carbs: #ca8a04;
  --fat: #db2777;
  --radius: 14px;
  --gap: 12px;
  --tabbar-h: 56px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1f2937;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #1f2937;
    --accent: #2dd4bf;
    --accent-contrast: #042f2e;
    --accent-soft: #134e4a;
    --over: #f59e0b;
    --danger: #f87171;
    --warning-bg: #422006;
    --warning-text: #fde68a;
    --protein: #60a5fa;
    --carbs: #facc15;
    --fat: #f472b6;
  }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }
/* 16px minimum prevents iOS from zooming on focus. */
input, select, textarea {
  width: 100%;
  font-size: 16px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
textarea { resize: vertical; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* Layout */
.app { min-height: 100dvh; }
.view {
  max-width: 640px;
  margin: 0 auto;
  padding: var(--gap) 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  display: grid;
  gap: var(--gap);
}

.topbar {
  position: sticky; top: 0; z-index: 10;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { margin: 6px 0; font-size: 18px; text-align: center; }
.day-nav { display: flex; align-items: center; justify-content: space-between; max-width: 640px; margin: 0 auto; }
.day-label {
  position: relative; flex: 1; text-align: center;
  font-weight: 600; font-size: 17px; padding: 6px 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.day-label span { display: block; overflow: hidden; text-overflow: ellipsis; }
.day-label span::first-letter { text-transform: uppercase; }
/* Native date picker stretched invisibly over the title: tapping the title opens it. */
.day-picker { position: absolute; inset: 0; opacity: 0; padding: 0; border: 0; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tab { flex: 1; border: 0; background: none; color: var(--muted); font-size: 14px; font-weight: 500; }
.tab[aria-current="page"] { color: var(--accent); font-weight: 700; }

/* Components */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.card-title { margin: 0 0 12px; font-size: 17px; }
.row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 12px; }
.field { display: grid; gap: 4px; margin-bottom: 10px; font-size: 14px; color: var(--muted); }
.field > input, .field > select, .field > textarea { color: var(--text); }
.muted { color: var(--muted); font-size: 14px; }

.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-danger { color: var(--danger); margin-right: auto; }
.btn-block { width: 100%; }
.icon-btn { border: 0; background: none; font-size: 30px; line-height: 1; padding: 4px 14px; color: var(--accent); cursor: pointer; }
.icon-btn:disabled { opacity: .3; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 8px 0; cursor: pointer; }

.error { color: var(--danger); font-size: 14px; margin: 8px 0 0; }
.warning { background: var(--warning-bg); color: var(--warning-text); padding: 10px 12px; border-radius: 10px; font-size: 14px; margin: 0 0 12px; }

/* Summary */
.kcal-line { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.kcal-value { font-size: 30px; font-weight: 700; }
.kcal-goal { color: var(--muted); }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 6px 0 12px; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: inherit; }
.bar.is-over > span { background: var(--over); }
.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.macro-name { font-size: 13px; color: var(--muted); }
.macro-value { font-weight: 600; font-size: 15px; }
.macro-value small { color: var(--muted); font-weight: 400; }
.macros .bar { height: 6px; margin-bottom: 0; }
.macro-protein .bar > span { background: var(--protein); }
.macro-carbs .bar > span { background: var(--carbs); }
.macro-fat .bar > span { background: var(--fat); }

/* Preview rows */
.edit-item { border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.edit-item-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.edit-item-remove { border: 0; background: none; color: var(--danger); font-size: 22px; padding: 0 6px; cursor: pointer; }
.edit-grid { display: grid; grid-template-columns: 1fr 88px; gap: 8px; margin-bottom: 8px; }
.edit-grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.mini { display: grid; gap: 2px; font-size: 12px; color: var(--muted); }
.mini input, .mini select { padding: 8px; color: var(--text); }
.edit-note { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.totals-line { font-size: 14px; color: var(--muted); margin-top: 4px; }

/* Meals list */
.meal-group { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: var(--gap); overflow: hidden; }
.meal-group-head { display: flex; justify-content: space-between; align-items: baseline; padding: 12px 16px; background: var(--surface-2); font-weight: 700; }
.meal-group-head small { font-weight: 500; color: var(--muted); }
.entry { border-top: 1px solid var(--border); }
.entry:first-of-type { border-top: 0; }
.entry-head {
  display: block; width: 100%; text-align: left;
  border: 0; background: none; padding: 8px 16px 0;
  font-size: 13px; color: var(--muted); font-style: italic; cursor: pointer;
}
.item {
  display: flex; justify-content: space-between; gap: 12px; width: 100%;
  border: 0; background: none; text-align: left; padding: 10px 16px; cursor: pointer;
}
.item + .item { border-top: 1px dashed var(--border); }
.item-name { font-weight: 500; }
.item-sub { font-size: 13px; color: var(--muted); }
.item-kcal { font-weight: 600; white-space: nowrap; }
.badge { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 6px; padding: 0 4px; margin-left: 4px; font-style: normal; }
.empty { text-align: center; color: var(--muted); padding: 24px 0; }

/* History */
.history-day {
  display: block; width: 100%; text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 16px; margin-bottom: 10px;
}
.history-top { display: flex; justify-content: space-between; align-items: baseline; }
.history-date { font-weight: 600; text-transform: capitalize; }
.history-kcal { font-weight: 700; }
.history-macros { font-size: 13px; color: var(--muted); }
.history-day .bar { margin: 6px 0; }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-title { text-align: center; margin: 0 0 16px; color: var(--accent); }
.login-card .btn { width: 100%; margin-top: 8px; }

/* Dialogs */
.dialog {
  width: min(560px, calc(100vw - 24px));
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 16px;
}
.dialog::backdrop { background: rgb(0 0 0 / .45); }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 20;
  max-width: calc(100vw - 32px);
}
.toast.is-error { background: var(--danger); color: #fff; }
