:root {
  --green: #16a34a;
  --green-dark: #15803d;
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --protein: #ef4444;
  --carbs: #f59e0b;
  --fat: #6366f1;
  --danger: #dc2626;
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--green);
  color: #fff;
  padding: 14px 16px;
  text-align: center;
}
.app-header h1 { margin: 0; font-size: 1.25rem; font-weight: 700; }

#views {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 96px;
}

.view-title { margin: 4px 0 16px; font-size: 1.4rem; }
.section-title { margin: 24px 0 10px; font-size: 1.05rem; color: var(--muted); }
.hidden { display: none !important; }

/* ---------- Snap ---------- */
.snap-intro p { color: var(--muted); text-align: center; margin: 8px 0 20px; }

.capture-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--card);
  border: 2px dashed var(--green);
  border-radius: var(--radius);
  padding: 40px 20px;
  cursor: pointer;
  color: var(--green-dark);
  font-weight: 600;
  transition: background 0.15s;
}
.capture-btn:active { background: #ecfdf5; }
.capture-icon { font-size: 2.5rem; }

.status {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: 10px;
  background: #eff6ff;
  color: #1e40af;
  text-align: center;
}
.status.error { background: #fef2f2; color: var(--danger); }
.status .spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid #bfdbfe;
  border-top-color: #1e40af;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

#preview-wrap { margin-top: 16px; text-align: center; }
#preview-img {
  max-width: 100%;
  max-height: 260px;
  border-radius: var(--radius);
  object-fit: cover;
}

/* ---------- Cards / results ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.result-head { display: flex; align-items: center; justify-content: space-between; }
.result-head h2 { margin: 0; font-size: 1.15rem; }
.badge {
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 999px;
  font-weight: 600;
  text-transform: capitalize;
}
.badge.high { background: #dcfce7; color: var(--green-dark); }
.badge.medium { background: #fef9c3; color: #a16207; }
.badge.low { background: #fee2e2; color: var(--danger); }
.notes { color: var(--muted); font-size: 0.9rem; margin: 8px 0 14px; }

.item-row {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
}
.item-row .item-top { display: flex; gap: 8px; align-items: center; }
.item-row input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 0.95rem;
  width: 100%;
}
.item-row .name-input { flex: 1; font-weight: 600; }
.item-row .qty-input { flex: 1; }
.macro-inputs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-top: 8px;
}
.macro-inputs label {
  font-size: 0.7rem;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.remove-item {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
.link-btn {
  background: none;
  border: none;
  color: var(--green-dark);
  font-weight: 600;
  cursor: pointer;
  padding: 4px 0;
}

.totals {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 16px 0;
  text-align: center;
}
.totals .t-val { font-weight: 700; font-size: 1.05rem; }
.totals .t-label { font-size: 0.7rem; color: var(--muted); text-transform: uppercase; }

.result-actions { display: flex; gap: 10px; }

/* ---------- Buttons ---------- */
.primary-btn {
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
}
.primary-btn:active { background: var(--green-dark); }
.primary-btn:disabled { opacity: 0.6; }
.ghost-btn {
  background: #fff;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 13px 16px;
  font-weight: 600;
  cursor: pointer;
}

/* ---------- Rings / Today ---------- */
.rings {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.ring { text-align: center; }
.ring svg { width: 100%; height: auto; max-width: 84px; }
.ring .ring-label { font-size: 0.72rem; color: var(--muted); margin-top: 4px; }
.ring .ring-val { font-size: 0.8rem; font-weight: 600; }

.advice-block { margin: 24px 0; }
.advice-out {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-top: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  line-height: 1.5;
}
.advice-out h1, .advice-out h2, .advice-out h3 { font-size: 1rem; margin: 12px 0 6px; }
.advice-out ul { padding-left: 20px; margin: 8px 0; }
.advice-out p { margin: 8px 0; }
.disclaimer { font-size: 0.72rem; color: var(--muted); text-align: center; margin-top: 8px; }

.entries { display: flex; flex-direction: column; gap: 10px; }
.entry {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  gap: 12px;
  align-items: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.entry img {
  width: 56px; height: 56px;
  border-radius: 10px;
  object-fit: cover;
  flex-shrink: 0;
  background: #eee;
}
.entry .entry-body { flex: 1; min-width: 0; }
.entry .entry-names { font-weight: 600; font-size: 0.92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry .entry-macros { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.entry .entry-time { font-size: 0.7rem; color: var(--muted); }
.entry .del-entry {
  background: none; border: none; color: var(--danger);
  font-size: 1.2rem; cursor: pointer;
}

.empty { color: var(--muted); text-align: center; padding: 24px 0; }

/* ---------- History ---------- */
.chart-wrap {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
.history-list { display: flex; flex-direction: column; gap: 8px; }
.hist-row {
  background: var(--card);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.hist-row .hist-date { font-weight: 600; font-size: 0.9rem; }
.hist-row .hist-macros { font-size: 0.75rem; color: var(--muted); }

/* ---------- Profile ---------- */
.profile-form { display: flex; flex-direction: column; gap: 14px; }
.profile-form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}
.profile-form input, .profile-form select, .profile-form textarea {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px;
  font-size: 1rem;
  color: var(--text);
  font-weight: 400;
  background: #fff;
}
.macro-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.macro-grid label { min-width: 0; }
.profile-form input { width: 100%; }
.saved-msg { color: var(--green-dark); font-weight: 600; text-align: center; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: #fff;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 6px 0 max(6px, env(safe-area-inset-bottom));
  z-index: 10;
}
.tab {
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.68rem;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 12px;
  flex: 1;
}
.tab span { font-size: 1.3rem; }
.tab.active { color: var(--green-dark); font-weight: 700; }

/* ---------- Login ---------- */
.login-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, var(--green) 0%, var(--green-dark) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}
.login-card {
  background: #fff;
  border-radius: 18px;
  padding: 32px 24px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  text-align: center;
}
.login-logo { font-size: 3rem; }
.login-card h1 { margin: 0; font-size: 1.6rem; }
.login-sub { margin: 0 0 8px; color: var(--muted); }
.login-card input {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 13px;
  font-size: 1rem;
  text-align: left;
}
.login-error { color: var(--danger); font-size: 0.88rem; margin: 4px 0 0; }

.account-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.account-name { color: var(--muted); font-size: 0.85rem; }
.account-row .ghost-btn { flex: 0 0 auto; }
