/* ── Reset & base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #f6f7f9;
  --surface:      #ffffff;
  --surface-2:    #eef0f3;
  --text:         #111827;
  --text-muted:   #6b7280;
  --accent:       #10B981;
  --accent-dark:  #059669;
  --danger:       #EF4444;
  --protein:      #3B82F6;
  --carbs:        #F59E0B;
  --fat:          #F43F5E;
  --border:       #e5e7eb;
  --radius:       12px;
  --radius-sm:    8px;
  --shadow:       0 1px 3px rgba(0,0,0,.07), 0 4px 16px rgba(0,0,0,.05);
  --nav-h:        68px;
  --header-h:     64px;
  --font:         -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

html {
  font-family: var(--font);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}
body {
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

/* ── App shell ────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  max-width: 430px;
  margin: 0 auto;
  position: relative;
}

/* ── Header ───────────────────────────────────────────────── */
#app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--header-h);
}
#app-header h1 {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
}
#header-date {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 3px;
}
#header-remaining { text-align: right; }
#remaining-val {
  display: block;
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.03em;
  line-height: 1;
}
#remaining-val.over { color: var(--danger); }
.remaining-label {
  font-size: 0.68rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ── Main content ─────────────────────────────────────────── */
#main-content {
  flex: 1;
  overflow-y: auto;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 8px);
}

/* ── Views ────────────────────────────────────────────────── */
.view { display: none; padding: 20px; }
.view.active { display: block; animation: fadeIn .18s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; } }

/* ── Calorie ring ─────────────────────────────────────────── */
.ring-wrapper {
  position: relative;
  width: 200px;
  height: 200px;
  margin: 4px auto 24px;
}
.calorie-ring { width: 200px; height: 200px; }
.ring-bg { stroke: var(--surface-2); }
.ring-fill {
  stroke: var(--accent);
  transition: stroke-dashoffset .65s cubic-bezier(.4, 0, .2, 1), stroke .3s ease;
}
.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  gap: 1px;
}
.ring-consumed {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
}
.ring-unit {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 600;
}
.ring-goal-text {
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ── Macro grid ───────────────────────────────────────────── */
.macro-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  margin-bottom: 24px;
}
.macro-card {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 12px 10px;
  box-shadow: var(--shadow);
}
.macro-label {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
}
.progress-bar {
  height: 5px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 6px;
}
.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .55s cubic-bezier(.4, 0, .2, 1);
}
.progress-fill.protein { background: var(--protein); }
.progress-fill.carbs   { background: var(--carbs); }
.progress-fill.fat     { background: var(--fat); }
.progress-fill.accent  { background: var(--accent); }
.macro-val {
  font-size: 0.68rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Section header ───────────────────────────────────────── */
.section-header { margin-bottom: 12px; }
.section-header h2 { font-size: 0.95rem; font-weight: 600; }

/* ── Meals list ───────────────────────────────────────────── */
#meals-list { display: flex; flex-direction: column; gap: 8px; }
.meal-entry {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.meal-info { flex: 1; min-width: 0; }
.meal-name {
  font-weight: 600;
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.meal-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
}
.meal-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.meal-cals { font-weight: 700; font-size: 0.88rem; }
.delete-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 5px 7px;
  border-radius: 4px;
  transition: background .15s, color .15s;
  line-height: 1;
}
.delete-btn:hover { background: #fee2e2; color: var(--danger); }

/* ── Empty state ──────────────────────────────────────────── */
.empty-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.88rem;
  padding: 36px 16px;
  line-height: 1.7;
}

/* ── Search ───────────────────────────────────────────────── */
.search-wrap {
  position: relative;
  margin-bottom: 12px;
}
.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 17px;
  height: 17px;
  color: var(--text-muted);
  pointer-events: none;
}
#food-search {
  width: 100%;
  padding: 12px 12px 12px 38px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
  font-family: var(--font);
}
#food-search:focus { border-color: var(--accent); }

/* ── Search results ───────────────────────────────────────── */
#search-results { margin-bottom: 4px; }
.search-result {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  transition: transform .1s, box-shadow .1s;
  -webkit-tap-highlight-color: transparent;
  gap: 12px;
}
.search-result:active { transform: scale(.98); box-shadow: none; }
.result-info { flex: 1; min-width: 0; }
.result-name {
  font-weight: 600;
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-brand {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-cals {
  font-weight: 700;
  font-size: 0.88rem;
  flex-shrink: 0;
  text-align: right;
}
.result-per {
  font-size: 0.68rem;
  font-weight: 400;
  color: var(--text-muted);
  display: block;
}

.loading, .no-results { text-align: center; padding: 20px; color: var(--text-muted); font-size: 0.88rem; }
.error { text-align: center; padding: 20px; color: var(--danger); font-size: 0.88rem; }

/* ── Divider ──────────────────────────────────────────────── */
.divider-text {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
  font-size: 0.78rem;
  margin: 18px 0 16px;
}
.divider-text::before, .divider-text::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ── Forms ────────────────────────────────────────────────── */
.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
}
.form-group input {
  width: 100%;
  padding: 11px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
  font-family: var(--font);
}
.form-group input:focus { border-color: var(--accent); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ── Buttons ──────────────────────────────────────────────── */
.btn-primary {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 14px 20px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, transform .1s;
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}
.btn-primary:hover  { background: var(--accent-dark); }
.btn-primary:active { transform: scale(.97); }
.btn-primary.saved  { background: #6b7280; }
.btn-primary.full-width { width: 100%; }

.btn-danger {
  background: none;
  color: var(--danger);
  border: 1.5px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: 10px 18px;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  font-family: var(--font);
  transition: background .15s;
}
.btn-danger:hover { background: #fee2e2; }

/* ── History ──────────────────────────────────────────────── */
#history-list { display: flex; flex-direction: column; gap: 10px; }
.history-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.history-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.history-date { font-weight: 600; font-size: 0.88rem; }
.history-date em { font-style: normal; color: var(--accent); }
.history-cals { font-weight: 700; font-size: 0.95rem; }
.history-macros {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* ── Settings ─────────────────────────────────────────────── */
.settings-footer {
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  text-align: center;
}
.settings-note {
  margin-top: 10px;
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* ── Bottom nav ───────────────────────────────────────────── */
#bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 430px;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  padding-top: 8px;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  z-index: 100;
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
  color: var(--text-muted);
  font-size: 0.6rem;
  font-family: var(--font);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  transition: color .15s;
  -webkit-tap-highlight-color: transparent;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--accent); }

/* ── Modal (bottom sheet) ─────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  backdrop-filter: blur(2px);
  animation: overlayIn .2s ease;
}
.modal-overlay.hidden { display: none; }
@keyframes overlayIn { from { opacity: 0; } to { opacity: 1; } }

.modal-sheet {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 14px 20px calc(24px + env(safe-area-inset-bottom, 0px));
  width: 100%;
  max-width: 430px;
  animation: sheetIn .25s cubic-bezier(.4, 0, .2, 1);
}
@keyframes sheetIn { from { transform: translateY(60px); opacity: 0; } to { transform: none; opacity: 1; } }

.modal-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto 16px;
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.modal-title-wrap { flex: 1; min-width: 0; }
.modal-header h3 {
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.3;
}
.text-muted { color: var(--text-muted); font-size: 0.78rem; margin-top: 2px; }
.modal-close {
  background: var(--surface-2);
  border: none;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-muted);
}
.modal-nutrition { margin-bottom: 18px; }
.nutrition-big {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
  margin-bottom: 8px;
}
.nutrition-unit {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-muted);
}
.nutrition-row {
  display: flex;
  gap: 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
  align-items: center;
  flex-wrap: wrap;
}
.macro-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
}
.macro-dot.protein { background: var(--protein); }
.macro-dot.carbs   { background: var(--carbs); }
.macro-dot.fat     { background: var(--fat); }

.modal-serving { margin-bottom: 20px; }
.modal-serving label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}
.serving-row { display: flex; align-items: center; gap: 10px; }
.serving-row input {
  width: 110px;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1.1rem;
  font-weight: 700;
  outline: none;
  transition: border-color .15s;
  font-family: var(--font);
  color: var(--text);
}
.serving-row input:focus { border-color: var(--accent); }
.serving-unit { font-size: 0.88rem; color: var(--text-muted); font-weight: 500; }

/* ── Desktop frame ────────────────────────────────────────── */
@media (min-width: 430px) {
  body { background: #e5e7eb; }
  #app {
    border: 1px solid var(--border);
    margin-top: 24px;
    border-radius: var(--radius);
    min-height: calc(100dvh - 48px);
    overflow: hidden;
    box-shadow: 0 8px 40px rgba(0,0,0,.12);
  }
  #bottom-nav { border-radius: 0 0 var(--radius) var(--radius); }
}
