/* ============================================================
   Calorie Tracker styles
   Phone-first, dark theme. All colors live in :root variables
   so they're easy to tweak in one place.
   ============================================================ */

:root {
  --bg: #0f172a;          /* page background (dark navy) */
  --card: #1e293b;        /* card surfaces */
  --card2: #334155;       /* inputs / secondary surfaces */
  --text: #f1f5f9;        /* main text */
  --muted: #94a3b8;       /* secondary text */
  --accent: #22c55e;      /* calories / primary green */
  --protein: #38bdf8;     /* blue */
  --carbs: #f59e0b;       /* amber */
  --fat: #f472b6;         /* pink */
  --fiber: #a78bfa;       /* violet */
  --danger: #ef4444;
  --radius: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  /* leave room for the floating + button */
  padding-bottom: 96px;
  /* No body padding-top: the sticky .topbar owns the status-bar inset so its
     background fills up to the top of the screen with no gap above it. */
}

h2 { font-size: 1rem; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.center { text-align: center; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* Keep clear of the status bar (0 on device — the native edge-to-edge
     margin handles it there; a real inset in a plain browser). */
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 14px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
.date-label { text-align: center; line-height: 1.2; }
.date-label small { display: block; color: var(--muted); font-size: .75rem; }

/* Date + "Today" share one row with a reserved height, so the header keeps
   the same size whether or not the button is showing. */
.date-sub-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 28px;
}
.today-btn {
  background: var(--card2);
  color: var(--accent);
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: .72rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}
.today-btn:active { background: var(--accent); color: #052e16; }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  margin: 12px;
  padding: 16px;
  border-radius: var(--radius);
}

/* ---------- Calorie headline ---------- */
.calorie-headline {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 12px;
}
.big-number { font-size: 2.2rem; font-weight: 700; line-height: 1; }
.remaining { text-align: right; }
.remaining .big-number { color: var(--accent); }

/* ---------- Bars ---------- */
.bar {
  background: var(--card2);
  border-radius: 999px;
  height: 12px;
  overflow: hidden;
}
.bar.small { height: 8px; }
.bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width .3s ease;
}
.bar-fill.over { background: var(--danger); }
.bar-fill.protein { background: var(--protein); }
.bar-fill.carbs { background: var(--carbs); }
.bar-fill.fat { background: var(--fat); }
.bar-fill.fiber { background: var(--fiber); }

.macros { margin-top: 16px; display: grid; gap: 12px; }
.macro-top {
  display: flex;
  justify-content: space-between;
  font-size: .8rem;
  margin-bottom: 4px;
}

/* ---------- Card header with an action on the right ---------- */
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.card-head h2 { margin: 0; }

/* ---------- Selection mode (grouping foods into a meal) ---------- */
.select-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--card2);
}
.select-actions { display: flex; align-items: center; gap: 4px; }
.select-left { display: flex; align-items: center; gap: 2px; }
.select-left .link-btn { padding: 4px 6px; }

/* Header actions (Paste / ✕ / Select) sit together on one row */
.head-actions { display: flex; align-items: center; gap: 2px; }
.head-actions .link-btn { padding: 6px 8px; }
.link-btn.subtle { color: var(--muted); }

/* A round check circle shown at the left of each entry while selecting */
.entry-check {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--muted);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  color: #052e16;
}
.entry.selected .entry-check {
  background: var(--accent);
  border-color: var(--accent);
}
.entry.selecting { cursor: pointer; }

/* ---------- Entry list ---------- */
.entry-list { list-style: none; margin: 0; padding: 0; }
.entry {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--card2);
}
.entry:last-child { border-bottom: none; }
.entry-info { flex: 1; min-width: 0; }
.entry-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.entry-sub { font-size: .78rem; color: var(--muted); }
.entry-cals { font-weight: 700; }
.entry-del {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 6px;
}

/* ---------- Buttons ---------- */
button { font-family: inherit; }
.icon-btn {
  background: var(--card2);
  color: var(--text);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  font-size: 1.3rem;
  cursor: pointer;
}
.primary {
  background: var(--accent);
  color: #052e16;
  border: none;
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 700;
  cursor: pointer;
}
.primary.wide, .wide { width: 100%; margin-top: 8px; }
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 8px;
  font-size: .9rem;
}
.link-btn.center { display: block; margin: 8px auto 0; }

/* Floating add button */
.fab {
  position: fixed;
  right: 20px;
  bottom: calc(20px + env(safe-area-inset-bottom));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--accent);
  color: #052e16;
  border: none;
  font-size: 2rem;
  line-height: 1;
  box-shadow: 0 6px 20px rgba(0,0,0,.4);
  cursor: pointer;
  z-index: 20;
}

/* ---------- Bottom sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: flex-end;
  z-index: 30;
}
.sheet {
  background: var(--card);
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 12px 16px calc(20px + env(safe-area-inset-bottom));
  animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--card2);
  border-radius: 999px;
  margin: 0 auto 12px;
}

.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; }

/* ---------- Inputs ---------- */
input {
  background: var(--card2);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 12px;
  font-size: 1rem;
  width: 100%;
}
input:focus { outline: none; border-color: var(--accent); }
label { display: block; font-size: .8rem; color: var(--muted); margin-top: 12px; }
label input { margin-top: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
/* Protein / carbs / fat / fiber sit on one row; the gap tightens so the
   four number fields still fit a narrow phone. */
.grid4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 6px; }

/* ---------- Search results ---------- */
.result-list { list-style: none; margin: 8px 0; padding: 0; }
.result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--card2);
  cursor: pointer;
}
.result-name { flex: 1; font-size: .9rem; }
.result-name small { display: block; color: var(--muted); }

.manual-form { margin-top: 8px; }

/* "Save to My foods" checkbox row */
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: .85rem;
  color: var(--muted);
  margin-top: 14px;
  cursor: pointer;
}
.check input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent); /* colors the checkbox green */
}

/* ---------- Quick-add (My foods) ---------- */
.quick-add-btn {
  background: var(--accent);
  color: #052e16;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}
.quick-del {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 4px;
  flex-shrink: 0;
}

/* Pencil button that opens the meal editor. Matches .quick-del sizing so
   the three buttons on a meal row line up. */
.quick-edit {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 4px;
  flex-shrink: 0;
}

/* ---------- Edit-meal sheet ---------- */
/* Sits above the add sheet (z-index 30) but below the scanner (50). */
.editor-backdrop { z-index: 40; }

/* Each food in the meal is its own bordered block rather than a flex row,
   because it holds a full set of inputs instead of a single label. */
.meal-item {
  display: block;
  border: 1px solid var(--card2);
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 10px;
  cursor: default;
}
.meal-item .mi-name { width: 100%; margin-bottom: 8px; }
.meal-item .mi-remove { padding: 4px 0; }
.meal-item label { font-size: .75rem; }

/* "Add another food" and "Scan a barcode" sit side by side. */
.me-add-row { display: flex; justify-content: space-between; align-items: center; }

/* ---------- AI estimate box (inside the manual form) ---------- */
.ai-box {
  border: 1px solid var(--card2);
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 12px;
}
/* The global `input` rule doesn't cover textarea, so restate the same look. */
.ai-box textarea {
  background: var(--card2);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 12px;
  font-family: inherit;
  font-size: 1rem;
  width: 100%;
  margin-top: 4px;
  resize: vertical;
}
.ai-box textarea:focus { outline: none; border-color: var(--accent); }
.ai-box .primary { margin-top: 8px; }
.ai-box p { margin: 6px 0 0; }

/* ---------- Weekly chart ---------- */
.week-chart {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  height: 120px;
  position: relative;
  margin-bottom: 6px;
}
.week-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
  gap: 4px;
}
.week-bar {
  width: 100%;
  max-width: 34px;
  background: var(--accent);
  border-radius: 6px 6px 0 0;
  min-height: 2px;
  transition: height .3s ease;
}
.week-bar.over { background: var(--danger); }
.week-bar.today { outline: 2px solid var(--text); outline-offset: 1px; }
.week-label { font-size: .7rem; color: var(--muted); }
/* dashed line showing the calorie goal */
.goal-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 2px dashed var(--muted);
  opacity: .6;
}

/* ---------- Scanner ---------- */
.scanner {
  position: fixed;
  inset: 0;
  background: #000;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.scanner video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scanner-frame {
  position: relative;
  width: 70%;
  max-width: 300px;
  aspect-ratio: 3/2;
  border: 3px solid var(--accent);
  border-radius: 16px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.45);
}
.scanner-hint {
  position: relative;
  color: #fff;
  margin-top: 20px;
  text-shadow: 0 1px 3px #000;
}
.scanner .primary { position: relative; margin-top: 20px; }

/* ---------- Energy balance (burned / net) ---------- */
.energy-row {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}
.energy-stat {
  flex: 1;
  background: var(--card2);
  border-radius: 12px;
  padding: 10px 12px;
}
.energy-stat b { font-size: 1.25rem; }
/* Deficit is the "good" direction when losing weight, so it gets the accent. */
.net-tag { font-size: .7rem; margin-top: 2px; min-height: .9em; }
.net-tag.deficit { color: var(--accent); }
.net-tag.surplus { color: var(--carbs); }
/* Obvious, full-width call-to-action so it clearly reads as tappable. */
.hc-btn {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 12px;
  background: var(--accent);
  color: #06210f;
  border: none;
  border-radius: 12px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
}
.hc-btn:active { opacity: .85; }

/* ---------- Weekly roundup ---------- */
.roundup-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 12px 0 4px;
}
.stat-tile {
  background: var(--card2);
  border-radius: 12px;
  padding: 12px 8px;
  text-align: center;
}
.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.1;
  color: var(--accent);
}
.stat-label {
  font-size: .7rem;
  color: var(--muted);
  margin-top: 4px;
}

.hidden { display: none !important; }

/* Destructive link button inside a sheet (delete this entry). */
.link-btn.danger-link { color: var(--danger); }

/* A logged entry is tappable to edit it, so give it the same affordance the
   selection rows get. The delete button keeps its own click. */
.entry.editable { cursor: pointer; }
