:root {
  --bg: #f7f8f6;
  --surface: #ffffff;
  --text: #1e2521;
  --text-dim: #66716b;
  --border: #e1e6e2;
  --accent: #2f8f5b;
  --accent-hover: #257449;
  --accent-soft: #e5f3ec;
  --danger: #c0392b;
  --protein: #4f7fd9;
  --carbs: #d9a13f;
  --fat: #c96f9a;
  --chart-bar: #2f8f5b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(20, 30, 25, 0.08), 0 4px 16px rgba(20, 30, 25, 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141815;
    --surface: #1d2320;
    --text: #e8ece9;
    --text-dim: #97a29b;
    --border: #2c342f;
    --accent: #4bb87d;
    --accent-hover: #5fc78e;
    --accent-soft: #21332a;
    --danger: #e06052;
    --chart-bar: #3fa86e;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ---------- Auth ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 36px 32px;
  width: 100%;
  max-width: 400px;
}
.brand { text-align: center; margin-bottom: 24px; }
.brand-icon { font-size: 44px; display: block; }
.brand h1 { font-size: 24px; margin-top: 8px; }
.brand-sub { color: var(--text-dim); font-size: 14px; }

.auth-tabs {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  margin-bottom: 20px;
}
.auth-tab {
  flex: 1;
  padding: 9px 0;
  border: none;
  background: transparent;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
}
.auth-tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.field { display: block; margin-bottom: 16px; }
.field > span { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.password-wrap { position: relative; }
.password-wrap input { padding-right: 44px; }
.eye-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  display: flex;
  align-items: center;
}
.eye-btn:hover { color: var(--text); background: var(--bg); }

.hint { font-size: 12px; color: var(--text-dim); margin-top: 4px; display: inline-block; }
.error { color: var(--danger); font-size: 14px; margin: 4px 0 12px; }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  padding: 11px 20px;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--text-dim); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--text); background: var(--bg); }
.btn-block { width: 100%; }
.btn-sm { font-size: 13px; padding: 7px 14px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar-brand { font-size: 16px; }
.topbar-tabs { display: flex; gap: 4px; background: var(--bg); border-radius: 10px; padding: 4px; }
.nav-tab {
  border: none;
  background: transparent;
  padding: 8px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
}
.nav-tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--text-dim); }

.container { max-width: 720px; margin: 0 auto; padding: 24px 20px 64px; }

/* ---------- Dropzone ---------- */
.dropzone {
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  text-align: center;
  padding: 16px;
  transition: border-color 0.15s;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--accent); }
.dz-icon { font-size: 40px; margin-bottom: 8px; }
.dz-sub { color: var(--text-dim); font-size: 13px; }
.dz-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
/* "Take photo" only makes sense on touch devices with a camera */
.dz-camera { display: none; }
@media (pointer: coarse) {
  .dz-camera { display: inline-block; }
}
#preview { max-width: 100%; max-height: 380px; border-radius: 8px; }

.analyze-actions { display: flex; gap: 10px; margin-top: 16px; }

.or-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 18px 0 10px;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.or-divider::before, .or-divider::after { content: ""; flex: 1; border-top: 1px solid var(--border); }

.text-analyze, .refine-form { display: flex; gap: 8px; }
.refine-form { margin-top: 14px; }
.text-analyze input, .refine-form input {
  flex: 1;
  min-width: 0;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
}
.text-analyze input:focus, .refine-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}

.insight-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 14px;
  line-height: 1.45;
}
.insight-icon { flex-shrink: 0; }

.status { display: flex; align-items: center; gap: 10px; margin-top: 16px; color: var(--text-dim); }
.spinner {
  width: 18px; height: 18px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Results ---------- */
#results { margin-top: 24px; }
.results-header { display: flex; align-items: baseline; gap: 14px; margin-bottom: 16px; }
.calorie-hero { font-size: 44px; font-weight: 800; color: var(--accent); }
.cal-unit { font-size: 18px; font-weight: 600; color: var(--text-dim); margin-left: 4px; }
.badge {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--accent-soft);
  color: var(--accent);
  padding: 4px 10px;
  border-radius: 999px;
}

.macro-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 20px; }
.totals-grid { grid-template-columns: repeat(4, 1fr); }
.macro-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.macro-label { display: block; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-dim); }
.macro-value { display: block; font-size: 22px; font-weight: 700; margin: 4px 0 8px; }
.total-card .macro-value { color: var(--accent); }
.macro-bar { height: 6px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.macro-fill { height: 100%; border-radius: 999px; width: 0; transition: width 0.4s ease; }
.macro-fill.protein { background: var(--protein); }
.macro-fill.carbs { background: var(--carbs); }
.macro-fill.fat { background: var(--fat); }

.items-list { display: flex; flex-direction: column; gap: 8px; }
.item-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.item-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.scale-row { display: flex; align-items: center; gap: 4px; }
.scale-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin-right: 6px;
}
.scale-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  cursor: pointer;
}
.scale-btn:hover { color: var(--text); }
.scale-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.item-info { min-width: 0; }
.item-name { font-weight: 600; }
.item-meta { font-size: 12.5px; color: var(--text-dim); }
.item-nums { text-align: right; white-space: nowrap; flex-shrink: 0; }
.item-remove {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  line-height: 1;
  flex-shrink: 0;
}
.item-remove:hover { color: var(--danger); background: var(--bg); }
.item-kcal { display: block; font-weight: 700; }
.item-macros { display: inline-flex; gap: 8px; font-size: 12px; color: var(--text-dim); }
.item-macro { display: inline-flex; align-items: center; gap: 3px; }
.m-dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.m-dot.protein { background: var(--protein); }
.m-dot.carbs { background: var(--carbs); }
.m-dot.fat { background: var(--fat); }
.notes { color: var(--text-dim); font-size: 13px; margin-top: 12px; font-style: italic; }
.log-actions { display: flex; align-items: center; gap: 12px; margin-top: 18px; }

/* ---------- Daily log ---------- */
.log-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; flex-wrap: wrap; gap: 10px; }
.log-header h2 { font-size: 20px; }
.log-header-actions { display: flex; align-items: center; gap: 10px; }

.manual-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 18px;
}
.manual-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 12px; }
.manual-grid .field { margin-bottom: 12px; }
.manual-actions { display: flex; gap: 10px; }
@media (max-width: 560px) {
  .manual-grid { grid-template-columns: repeat(2, 1fr); }
}
.date-input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.week-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px 10px;
  margin-bottom: 12px;
}
.week-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin-bottom: 10px;
}
.week-strip { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.week-day {
  background: transparent;
  border: none;
  padding: 4px 0 2px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
}
.week-day:hover { background: var(--bg); }
.week-day:focus-visible { outline: 2px solid var(--accent); }
.week-bar-track { height: 64px; width: 100%; display: flex; align-items: flex-end; justify-content: center; }
.week-bar {
  width: 60%;
  max-width: 26px;
  border-radius: 4px 4px 0 0;
  background: var(--chart-bar);
  opacity: 0.45;
  transition: opacity 0.15s;
}
.week-bar.is-empty { background: var(--border); opacity: 1; border-radius: 2px; }
.week-day:hover .week-bar:not(.is-empty) { opacity: 0.75; }
.week-day.selected .week-bar:not(.is-empty) { opacity: 1; }
.week-label { font-size: 11px; font-weight: 600; color: var(--text-dim); }
.week-day.selected .week-label { color: var(--text); }

#log-entries { display: flex; flex-direction: column; gap: 10px; }
.entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
}
.entry-info { min-width: 0; }
.entry-name { font-weight: 600; }
.entry-meta { font-size: 12.5px; color: var(--text-dim); }
.entry-cals { font-weight: 700; white-space: nowrap; }
.entry-delete, .entry-edit {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 17px;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  line-height: 1;
}
.entry-delete:hover { color: var(--danger); background: var(--bg); }
.entry-edit { font-size: 15px; }
.entry-edit:hover { color: var(--accent); background: var(--bg); }

.meal-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  margin: 10px 2px 0;
}

.select-input {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.manual-grid .select-input { width: 100%; background: var(--bg); padding: 11px 12px; font-size: 15px; }

.goal-sub { display: block; font-size: 11px; color: var(--text-dim); margin: -6px 0 8px; }
.macro-fill.cal { background: var(--accent); }
.macro-fill.over { background: var(--danger); }
.remaining { font-size: 14px; font-weight: 600; color: var(--accent); margin: -8px 0 18px; }
.remaining.over { color: var(--danger); }

.week-bar-track { position: relative; }
.week-bar.over { background: var(--carbs); }
.week-goal-line {
  position: absolute;
  left: 8%;
  right: 8%;
  border-top: 2px dashed var(--text-dim);
  opacity: 0.5;
  pointer-events: none;
}
.empty { color: var(--text-dim); text-align: center; padding: 32px 0; }

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .container { padding: 16px 12px 48px; }

  /* Topbar: brand + user on the first row, full-width nav tabs below */
  .topbar { padding: 10px 12px; row-gap: 10px; }
  .topbar-brand { order: 1; font-size: 15px; }
  .topbar-user { order: 2; margin-left: auto; }
  .topbar-tabs { order: 3; width: 100%; }
  .nav-tab { flex: 1; text-align: center; padding: 9px 0; }
  #user-email { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Keep the upload area compact so the nav and Analyze button stay on screen */
  .dropzone { min-height: 150px; }
  #preview { max-height: 38vh; }
  .calorie-hero { font-size: 34px; }
  .results-header { flex-wrap: wrap; gap: 8px; }

  .macro-card { padding: 10px 12px; }
  .macro-value { font-size: 18px; }

  .item-card { padding: 11px 12px; }

  .log-header { flex-direction: column; align-items: stretch; }
  .log-header-actions { width: 100%; }
  .log-header-actions .date-input { flex: 1; min-width: 0; }

  .entry { padding: 10px 12px; }
  .auth-card { padding: 28px 20px; }
}

@media (max-width: 560px) {
  .totals-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 420px) {
  .analyze-actions { flex-direction: column; }
  .analyze-actions .btn { width: 100%; }
  .log-actions { flex-direction: column; align-items: stretch; }
  .macro-grid:not(.totals-grid) { grid-template-columns: 1fr; }
  .macro-grid:not(.totals-grid) .macro-card { display: grid; grid-template-columns: 70px 1fr; align-items: center; gap: 2px 10px; }
  .macro-grid:not(.totals-grid) .macro-value { margin: 0; grid-column: 2; grid-row: 1; text-align: right; }
  .macro-grid:not(.totals-grid) .macro-bar { grid-column: 1 / -1; }
}
