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

:root {
  --green:       #22c55e;
  --green-dark:  #16a34a;
  --green-light: #dcfce7;
  --blue:        #3b82f6;
  --red:         #ef4444;
  --orange:      #f97316;
  --purple:      #a855f7;
  --bg:          #f8fafc;
  --surface:     #ffffff;
  --border:      #e2e8f0;
  --text:        #0f172a;
  --muted:       #64748b;
  --radius:      16px;
  --radius-sm:   10px;
  --shadow:      0 1px 3px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --shadow-lg:   0 8px 32px rgba(0,0,0,.12);
  --nav-h:       68px;
}

body {
  font-family: -apple-system, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(var(--nav-h) + 16px);
  -webkit-font-smoothing: antialiased;
}

/* ── Header ───────────────────────────────────────── */
.app-header {
  background: linear-gradient(135deg, #16a34a 0%, #22c55e 100%);
  padding: 16px 20px 14px;
  padding-top: max(16px, env(safe-area-inset-top));
  color: #fff;
}
.header-inner { display: flex; align-items: center; gap: 12px; max-width: 600px; margin: 0 auto; }
.app-logo   { font-size: 2rem; line-height: 1; }
.app-title  { font-size: 1.3rem; font-weight: 800; letter-spacing: -0.5px; }
.app-sub    { font-size: 0.78rem; opacity: 0.8; margin-top: 1px; }

/* ── Layout ───────────────────────────────────────── */
#main { max-width: 600px; margin: 0 auto; padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 14px; }
.tab-section { display: flex; flex-direction: column; gap: 14px; }
.tab-section.hidden { display: none; }

/* ── Cards ────────────────────────────────────────── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-header-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-title { font-size: 1rem; font-weight: 700; color: var(--text); }

/* ── Buttons ──────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 20px; border: none; border-radius: var(--radius-sm);
  font-size: 0.92rem; font-weight: 600; cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
  text-decoration: none;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); box-shadow: 0 4px 14px rgba(34,197,94,.35); }
.btn-danger  { background: var(--red); color: #fff; }
.btn-danger-outline { border: 2px solid var(--red); color: var(--red); background: transparent; width: 100%; }
.btn-ghost { background: var(--bg); color: var(--muted); border: 1.5px solid var(--border); }
.btn-ghost-dark { background: var(--border); color: var(--text); border: none; }
.btn-sm { padding: 8px 16px; font-size: 0.82rem; }
.btn-link { background: none; border: none; font-size: 0.82rem; font-weight: 600; cursor: pointer; padding: 4px; }
.btn-link.danger { color: var(--red); }
.btn-link:hover { text-decoration: underline; }

.btn-scan-big {
  width: 100%; padding: 16px; background: linear-gradient(135deg, #16a34a, #22c55e);
  color: #fff; border-radius: var(--radius); font-size: 1rem; font-weight: 700;
  box-shadow: 0 4px 20px rgba(34,197,94,.3);
  gap: 10px;
}
.btn-scan-big:hover { box-shadow: 0 6px 24px rgba(34,197,94,.45); transform: translateY(-1px); }
.scan-icon { font-size: 1.3rem; }

/* ── Scanner ──────────────────────────────────────── */
.scanner-card { display: flex; flex-direction: column; gap: 12px; }

.scanner-wrap {
  position: relative; width: 100%; border-radius: var(--radius-sm);
  overflow: hidden; background: #0f172a; aspect-ratio: 4/3;
}
#scanner-viewport { width: 100%; height: 100%; }
#scanner-viewport video,
#scanner-viewport canvas {
  position: absolute; top: 0; left: 0;
  width: 100% !important; height: 100% !important; object-fit: cover;
}
#scanner-viewport canvas.drawingBuffer { display: none; }

.scan-overlay {
  position: absolute; inset: 0; z-index: 10;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(0,0,0,.3) 0%, transparent 30%, transparent 70%, rgba(0,0,0,.3) 100%);
}
.scan-frame { position: relative; width: 80%; height: 40%; min-height: 80px; }
.c { position: absolute; width: 26px; height: 26px; border-color: #22c55e; border-style: solid; }
.c.tl { top:0;left:0;   border-width:3px 0 0 3px; border-radius:6px 0 0 0; }
.c.tr { top:0;right:0;  border-width:3px 3px 0 0; border-radius:0 6px 0 0; }
.c.bl { bottom:0;left:0;  border-width:0 0 3px 3px; border-radius:0 0 0 6px; }
.c.br { bottom:0;right:0; border-width:0 3px 3px 0; border-radius:0 0 6px 0; }
.scan-line {
  position: absolute; left: 6px; right: 6px; height: 2px;
  background: linear-gradient(90deg, transparent, #22c55e 40%, #22c55e 60%, transparent);
  box-shadow: 0 0 10px rgba(34,197,94,.9), 0 0 20px rgba(34,197,94,.5);
  animation: scanMove 2s ease-in-out infinite;
}
@keyframes scanMove { 0%{top:6px} 50%{top:calc(100% - 8px)} 100%{top:6px} }
.scan-hint {
  margin-top: 14px; color: rgba(255,255,255,.9);
  font-size: 0.8rem; font-weight: 500;
  text-shadow: 0 1px 6px rgba(0,0,0,.6);
  background: rgba(0,0,0,.25); padding: 4px 12px; border-radius: 99px;
}
.scan-status { font-size: 0.82rem; color: var(--red); text-align: center; min-height: 16px; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 0.8rem; }
.divider::before, .divider::after { content:''; flex:1; height:1px; background:var(--border); }

.input-group { display: flex; gap: 8px; }
.input-field {
  flex: 1; padding: 11px 14px; border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); font-size: 0.95rem; outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  background: var(--bg);
}
.input-field:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(34,197,94,.15); }

/* ── Product card ─────────────────────────────────── */
.product-card { display: flex; flex-direction: column; gap: 16px; }

.product-header { display: flex; gap: 14px; align-items: flex-start; }
.product-image {
  width: 80px; height: 80px; object-fit: contain;
  border-radius: 12px; border: 1px solid var(--border);
  background: #f8fafc; flex-shrink: 0;
}
.product-title h2 { font-size: 1.05rem; font-weight: 700; line-height: 1.3; }
.prod-brand  { color: var(--muted); font-size: 0.82rem; margin-top: 3px; }
.prod-qty    { color: var(--muted); font-size: 0.78rem; margin-top: 1px; }

.badge-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ns-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 50%;
  font-weight: 800; font-size: 0.95rem; color: #fff; text-transform: uppercase;
}
.ns-a{background:#1a9641} .ns-b{background:#79c31e;color:#1a1a1a}
.ns-c{background:#f5c800;color:#1a1a1a} .ns-d{background:#e87d13} .ns-e{background:#e63e11}

.nova-badge {
  display: inline-flex; align-items: center; justify-content: center;
  width: 28px; height: 28px; border-radius: 6px;
  font-weight: 800; font-size: 0.85rem; color: #fff;
}
.nova-1{background:#1a9641} .nova-2{background:#79c31e;color:#1a1a1a}
.nova-3{background:#f5c800;color:#1a1a1a} .nova-4{background:#e63e11}
.badge-label { font-size: 0.78rem; color: var(--muted); }

.nutrients-section h3 { font-size: 0.88rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 10px; }
.nutrients-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.n-card {
  background: var(--bg); border-radius: var(--radius-sm);
  padding: 12px 14px; display: flex; flex-direction: column;
}
.n-card.hero {
  background: linear-gradient(135deg, #f0fdf4, #dcfce7);
  border: 1px solid #bbf7d0;
  grid-column: span 2;
  flex-direction: row; align-items: center; gap: 12px;
}
.n-val  { font-size: 1.4rem; font-weight: 800; color: var(--text); }
.n-val.hero-val { font-size: 2rem; color: var(--green-dark); }
.n-unit { font-size: 0.7rem; color: var(--muted); font-weight: 500; }
.n-lbl  { font-size: 0.75rem; color: var(--muted); margin-top: 3px; }
.n-lbl.hero-lbl { font-size: 0.88rem; font-weight: 600; color: var(--green-dark); }

.ingredients-box, .allergens-box { font-size: 0.82rem; color: var(--muted); line-height: 1.65; }
.allergens-box { color: #b91c1c; font-weight: 500; }
.section-label { font-size: 0.78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 5px; }

.add-log-btn { width: 100%; padding: 14px; border-radius: var(--radius-sm); font-size: 0.95rem; }
.product-source { font-size: 0.7rem; color: var(--border); text-align: right; }

/* ── Error card ───────────────────────────────────── */
.error-card { text-align: center; padding: 28px; color: var(--muted); }
.error-icon { display: block; font-size: 2.5rem; margin-bottom: 10px; }

/* ── History ──────────────────────────────────────── */
.pill-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.pill-list li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; background: var(--bg); border-radius: var(--radius-sm);
  cursor: pointer; font-size: 0.88rem; transition: background 0.15s;
}
.pill-list li:hover { background: #f1f5f9; }
.h-name { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-code { font-family: monospace; color: var(--muted); font-size: 0.75rem; }

/* ── Today ────────────────────────────────────────── */
.today-hero {
  background: linear-gradient(135deg, #16a34a 0%, #22c55e 100%);
  color: #fff; text-align: center; padding: 24px 18px 20px;
}
.today-label { font-size: 0.82rem; opacity: 0.85; font-weight: 500; margin-bottom: 6px; }
.today-calories { display: flex; align-items: baseline; justify-content: center; gap: 4px; margin-bottom: 14px; }
.cal-big  { font-size: 3rem; font-weight: 900; letter-spacing: -2px; line-height: 1; }
.cal-sep  { font-size: 1.4rem; opacity: 0.6; }
.cal-goal { font-size: 1.6rem; font-weight: 700; opacity: 0.85; }
.cal-unit { font-size: 0.9rem; opacity: 0.7; align-self: flex-end; padding-bottom: 4px; }

.progress-track {
  background: rgba(255,255,255,.25); border-radius: 99px; height: 10px;
  overflow: hidden; margin-bottom: 16px;
}
.progress-fill {
  height: 100%; background: #fff; border-radius: 99px;
  transition: width 0.5s cubic-bezier(.4,0,.2,1); width: 0%;
}
.progress-fill.over { background: #fca5a5; }

.chip-info {
  background: rgba(255,255,255,.2); border-radius: 99px;
  padding: 5px 14px; font-size: 0.78rem; margin-bottom: 14px; display: inline-block;
}

.macro-strip { display: flex; justify-content: center; gap: 12px; }
.macro-pill {
  display: flex; flex-direction: column; align-items: center;
  background: rgba(255,255,255,.18); border-radius: 12px;
  padding: 8px 16px; min-width: 80px;
}
.macro-pill span { font-size: 1.2rem; font-weight: 800; }
.macro-pill small { font-size: 0.68rem; opacity: 0.85; margin-top: 2px; }

.daily-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.daily-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; background: var(--bg); border-radius: var(--radius-sm);
  transition: background 0.15s;
}
.di-name { flex: 1; font-size: 0.9rem; font-weight: 500; }
.di-qty  { font-size: 0.78rem; color: var(--muted); }
.di-cal  { font-weight: 700; color: var(--green-dark); font-size: 0.88rem; white-space: nowrap; }
.di-del  { background: none; border: none; cursor: pointer; color: var(--muted); font-size: 1rem; padding: 3px 6px; border-radius: 6px; transition: background 0.15s; }
.di-del:hover { background: #fee2e2; color: var(--red); }
.empty-msg { color: var(--muted); font-size: 0.88rem; text-align: center; padding: 16px 0; line-height: 1.7; }

/* ── Search ───────────────────────────────────────── */
.search-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg); border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  padding: 4px 8px 4px 12px; margin-bottom: 12px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.search-bar:focus-within { border-color: var(--green); box-shadow: 0 0 0 3px rgba(34,197,94,.15); }
.search-icon-left { font-size: 1rem; color: var(--muted); flex-shrink: 0; }
.search-field { flex: 1; border: none; background: none; font-size: 0.95rem; outline: none; padding: 8px 0; color: var(--text); }

.search-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.stab {
  padding: 5px 14px; border: 1.5px solid var(--border); border-radius: 99px;
  font-size: 0.78rem; font-weight: 600; background: none; color: var(--muted); cursor: pointer;
  transition: all 0.15s;
}
.stab.active { background: var(--green); color: #fff; border-color: var(--green); }

.search-results { display: flex; flex-direction: column; gap: 10px; }
.search-source-label {
  font-size: 0.72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .6px;
  color: var(--muted); padding: 2px 0 4px;
}
.search-item {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 14px; display: flex; align-items: center; gap: 12px;
}
.search-item img { width: 54px; height: 54px; object-fit: contain; border-radius: 10px; border: 1px solid var(--border); background: #f8fafc; flex-shrink: 0; }
.si-info { flex: 1; min-width: 0; }
.si-name  { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.si-brand { font-size: 0.75rem; color: var(--muted); margin-top: 1px; }
.si-cal   { font-size: 0.82rem; color: var(--green-dark); font-weight: 600; margin-top: 4px; }
.si-add {
  flex-shrink: 0; background: var(--green); color: #fff; border: none;
  border-radius: 8px; padding: 8px 14px; font-weight: 700; font-size: 0.82rem; cursor: pointer;
  transition: background 0.15s;
}
.si-add:hover { background: var(--green-dark); }

/* ── Profile ──────────────────────────────────────── */
.stats-hero {
  background: linear-gradient(135deg, #0f172a, #1e293b);
  border-radius: var(--radius); padding: 20px; color: #fff;
  box-shadow: var(--shadow-lg);
}
.stat-row { display: flex; gap: 12px; margin-bottom: 12px; }
.stat-row.secondary { margin-bottom: 0; }
.stat-big {
  flex: 1; background: rgba(255,255,255,.08); border-radius: var(--radius-sm);
  padding: 16px; text-align: center;
}
.stat-big.green { background: rgba(34,197,94,.2); }
.stat-num  { display: block; font-size: 2rem; font-weight: 900; letter-spacing: -1px; }
.stat-lbl  { display: block; font-size: 0.72rem; opacity: 0.7; margin-top: 3px; font-weight: 500; }
.stat-sub  { display: block; font-size: 0.7rem; opacity: 0.55; margin-top: 2px; font-style: italic; }
.stat-small {
  flex: 1; background: rgba(255,255,255,.06); border-radius: var(--radius-sm);
  padding: 12px; text-align: center;
}
.stat-small span { display: block; font-size: 1.2rem; font-weight: 700; }
.stat-small small { font-size: 0.68rem; opacity: 0.6; }

.profile-form { display: flex; flex-direction: column; gap: 14px; }
.form-row { display: flex; gap: 12px; }
.form-label { display: flex; flex-direction: column; gap: 5px; font-size: 0.82rem; font-weight: 600; color: var(--muted); flex: 1; }
.form-select, .form-input {
  padding: 10px 12px; border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.92rem; outline: none; width: 100%;
  background: var(--bg); color: var(--text);
  transition: border-color 0.2s, box-shadow 0.2s;
}
.form-select:focus, .form-input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(34,197,94,.15); }
.input-unit-wrap { display: flex; align-items: center; gap: 8px; }
.input-unit-wrap .form-input { flex: 1; }
.unit-label { color: var(--muted); font-size: 0.82rem; white-space: nowrap; font-weight: 500; }

/* ── Bottom nav ───────────────────────────────────── */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--nav-h);
  background: rgba(255,255,255,.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 200;
  box-shadow: 0 -4px 20px rgba(0,0,0,.06);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; border: none; background: none; cursor: pointer;
  color: var(--muted); font-size: 0.62rem; font-weight: 600; padding: 8px 4px;
  transition: color 0.2s;
}
.nav-btn.active { color: var(--green-dark); }
.nav-btn.active .nav-icon { transform: scale(1.15); }
.nav-icon { font-size: 1.3rem; transition: transform 0.2s; }

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

.modal-sheet {
  background: #fff; border-radius: 24px 24px 0 0;
  padding: 20px 22px 32px; width: 100%; max-width: 600px;
  display: flex; flex-direction: column; gap: 14px;
  animation: slideUp .25s cubic-bezier(.4,0,.2,1);
}
@keyframes slideUp { from{transform:translateY(100%)} to{transform:translateY(0)} }
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 99px; align-self: center; margin-bottom: 4px; }
.modal-title { font-size: 1.1rem; font-weight: 800; }
.modal-info  { font-size: 0.88rem; color: var(--muted); line-height: 1.5; }
.portion-shortcuts { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 4px; }
.portion-btn {
  padding: 8px 14px; border: 1.5px solid var(--border); border-radius: 99px;
  font-size: 0.82rem; font-weight: 600; background: var(--bg); color: var(--text);
  cursor: pointer; transition: all 0.15s; white-space: nowrap;
}
.portion-btn:hover, .portion-btn.active { background: var(--green); color: #fff; border-color: var(--green); }

.modal-cal-preview { text-align: center; font-size: 2rem; font-weight: 900; color: var(--green-dark); padding: 8px 0; }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ── Loading ──────────────────────────────────────── */
.loading { text-align: center; padding: 32px 20px; color: var(--muted); }
.spinner {
  width: 36px; height: 36px; border: 3px solid var(--border);
  border-top-color: var(--green); border-radius: 50%;
  animation: spin .7s linear infinite; margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hint-text { font-size: 0.82rem; color: var(--muted); margin-bottom: 12px; line-height: 1.5; }

/* ── Utility ──────────────────────────────────────── */
.hidden { display: none !important; }
