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

:root {
  --bg: #0f0f0f;
  --surface: #1c1c1e;
  --surface2: #2c2c2e;
  --border: #38383a;
  --accent: #6366f1;
  --accent-dim: rgba(99,102,241,.15);
  --text: #ffffff;
  --text2: #aeaeb2;
  --text3: #636366;
  --green: #30d158;
  --yellow: #ffd60a;
  --orange: #ff9f0a;
  --red: #ff453a;
  --blue: #64d2ff;
  --tab-h: calc(60px + env(safe-area-inset-bottom));
  --header-h: calc(52px + env(safe-area-inset-top));
}

html, body {
  height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', sans-serif;
  -webkit-tap-highlight-color: transparent; overflow: hidden;
}

#app { height: 100%; display: flex; flex-direction: column; position: relative; }

/* ── Nav header (history only) ──────────────────────────────── */
.nav-header {
  height: var(--header-h); padding-top: env(safe-area-inset-top);
  display: flex; align-items: flex-end; justify-content: center; padding-bottom: 12px;
  background: rgba(15,15,15,.9); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-bottom: .5px solid var(--border); position: sticky; top: 0; z-index: 10; flex-shrink: 0;
}
.nav-title { font-size: 17px; font-weight: 600; letter-spacing: -.3px; }

/* ── Tab contents ───────────────────────────────────────────── */
.tab-content { display: flex; flex-direction: column; flex: 1; overflow: hidden; }
.tab-content.hidden { display: none; }

/* ══════════════════════════════════════════════════════════════
   CAMERA — fullscreen redesign
══════════════════════════════════════════════════════════════ */
.camera-wrap {
  flex: 1; position: relative; overflow: hidden; background: #000;
}

#video, #preview {
  width: 100%; height: 100%; object-fit: cover;
  position: absolute; inset: 0;
}

/* Top gradient + brand bar */
.cam-top-bar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  padding: calc(env(safe-area-inset-top) + 16px) 20px 40px;
  background: linear-gradient(to bottom, rgba(0,0,0,.6) 0%, transparent 100%);
  display: flex; align-items: center; justify-content: space-between;
}

.cam-brand {
  display: flex; align-items: center; gap: 8px;
  font-size: 16px; font-weight: 700; color: #fff; letter-spacing: -.3px;
}
.cam-brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}

.cam-gallery-btn {
  width: 40px; height: 40px; border-radius: 12px;
  background: rgba(255,255,255,.18); backdrop-filter: blur(10px);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; color: #fff; border: .5px solid rgba(255,255,255,.2);
}
.cam-gallery-btn svg { width: 20px; height: 20px; }
.cam-gallery-btn:active { opacity: .7; }

/* Viewfinder guides */
.viewfinder {
  position: absolute; inset: 0; z-index: 5; pointer-events: none;
  display: flex; align-items: center; justify-content: center;
}
.vf-corner {
  position: absolute; width: 24px; height: 24px;
  border-color: rgba(255,255,255,.7); border-style: solid;
}
.vf-corner.tl { top: 30%; left: 10%; border-width: 2px 0 0 2px; border-radius: 3px 0 0 0; }
.vf-corner.tr { top: 30%; right: 10%; border-width: 2px 2px 0 0; border-radius: 0 3px 0 0; }
.vf-corner.bl { bottom: 32%; left: 10%; border-width: 0 0 2px 2px; border-radius: 0 0 0 3px; }
.vf-corner.br { bottom: 32%; right: 10%; border-width: 0 2px 2px 0; border-radius: 0 0 3px 0; }

/* Hide viewfinder when preview is shown */
.viewfinder.hidden { display: none; }

/* Bottom controls */
.cam-bottom-bar {
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 10;
  padding: 32px 0 calc(24px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(0,0,0,.65) 0%, transparent 100%);
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}

.cam-hint {
  font-size: 13px; color: rgba(255,255,255,.6); letter-spacing: .2px;
}

/* Capture button */
.capture-btn {
  width: 76px; height: 76px; border-radius: 50%;
  background: none; border: none; cursor: pointer;
  position: relative; display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.capture-ring {
  position: absolute; inset: 0; border-radius: 50%;
  border: 3px solid rgba(255,255,255,.85);
  transition: transform .15s, border-color .15s;
}
.capture-core {
  width: 58px; height: 58px; border-radius: 50%;
  background: #fff;
  transition: transform .15s, background .15s;
}
.capture-btn:active .capture-ring { transform: scale(.92); border-color: #fff; }
.capture-btn:active .capture-core { transform: scale(.88); background: rgba(255,255,255,.8); }

/* After-capture panel — slides up from bottom */
.after-capture-panel {
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 20;
  background: rgba(20,20,22,.92);
  backdrop-filter: blur(24px); -webkit-backdrop-filter: blur(24px);
  border-top: .5px solid rgba(255,255,255,.12);
  border-radius: 20px 20px 0 0;
  padding: 8px 20px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
  animation: slideUp .28s cubic-bezier(.32,1,.23,1);
}
.after-capture-panel.hidden { display: none; }

.after-capture-bar {
  width: 36px; height: 4px; border-radius: 2px;
  background: rgba(255,255,255,.2); margin: 4px auto 2px;
}

.desc-input {
  width: 100%; padding: 13px 16px;
  background: rgba(255,255,255,.08); border: .5px solid rgba(255,255,255,.12);
  border-radius: 14px; color: var(--text); font-size: 16px; outline: none;
}
.desc-input::placeholder { color: rgba(255,255,255,.35); }
.desc-input:focus { border-color: var(--accent); background: rgba(99,102,241,.1); }

.after-capture-actions {
  display: flex; gap: 10px;
}
.after-capture-actions > * { flex: 1; }

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  padding: 13px 18px; border-radius: 14px; font-size: 15px; font-weight: 600;
  border: none; cursor: pointer; transition: opacity .15s, transform .1s;
  display: flex; align-items: center; justify-content: center; gap: 7px;
}
.btn:active { opacity: .72; transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: rgba(255,255,255,.1); color: rgba(255,255,255,.8); }
.btn.full { width: 100%; }

/* ── Tab bar ─────────────────────────────────────────────────── */
.tab-bar {
  height: var(--tab-h); padding-bottom: env(safe-area-inset-bottom);
  background: rgba(18,18,20,.95); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border-top: .5px solid var(--border); display: flex; position: relative; z-index: 20;
}
.tab-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; border: none; background: none; color: var(--text3);
  font-size: 10px; font-weight: 500; cursor: pointer; transition: color .2s;
}
.tab-btn svg { width: 24px; height: 24px; }
.tab-btn.active { color: var(--accent); }

/* ── History ─────────────────────────────────────────────────── */
.history-list {
  flex: 1; overflow-y: auto;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 10px;
}
.history-card {
  background: var(--surface); border-radius: 16px; overflow: hidden;
  display: flex; align-items: stretch; cursor: pointer; transition: opacity .15s;
  border: .5px solid var(--border);
}
.history-card:active { opacity: .7; }
.history-thumb { width: 80px; flex-shrink: 0; object-fit: cover; }
.history-thumb-placeholder {
  width: 80px; flex-shrink: 0; background: var(--surface2);
  display: flex; align-items: center; justify-content: center; color: var(--text3); font-size: 24px;
}
.history-info { flex: 1; padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; }
.history-date { font-size: 12px; color: var(--text3); }
.history-summary { font-size: 14px; font-weight: 500; line-height: 1.3; }
.history-macros { font-size: 12px; color: var(--text2); margin-top: 2px; }
.history-delete {
  padding: 0 14px; background: none; border: none; color: var(--text3);
  font-size: 18px; cursor: pointer; display: flex; align-items: center;
}
.history-delete:active { color: var(--red); }
.empty-state { color: var(--text3); text-align: center; padding: 48px 20px; font-size: 15px; }

/* ── Overlays ────────────────────────────────────────────────── */
.overlay { position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column; }
.overlay.hidden { display: none; }

.sheet-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}

/* Loading */
#loading-overlay { align-items: center; justify-content: center; }
.loader-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(8px); }
.loader-card {
  position: relative;
  background: var(--surface); border-radius: 24px;
  padding: 36px 44px; display: flex; flex-direction: column;
  align-items: center; gap: 18px; min-width: 230px;
  border: .5px solid var(--border);
}

/* Concentric ring loader */
.loader-rings { position: relative; width: 52px; height: 52px; }
.loader-ring {
  position: absolute; border-radius: 50%; border-style: solid; border-color: transparent;
  animation: spin linear infinite;
}
.loader-ring.r1 { inset: 0; border-width: 3px; border-top-color: var(--accent); animation-duration: .9s; }
.loader-ring.r2 { inset: 8px; border-width: 2px; border-top-color: rgba(99,102,241,.5); animation-duration: 1.4s; animation-direction: reverse; }
.loader-ring.r3 { inset: 16px; border-width: 2px; border-top-color: rgba(99,102,241,.25); animation-duration: 2s; }
@keyframes spin { to { transform: rotate(360deg); } }

.loader-text { font-size: 16px; font-weight: 600; text-align: center; }
.loader-sub { font-size: 13px; color: var(--text2); text-align: center; min-height: 18px; }

/* Sheet */
.sheet {
  background: var(--bg); border-radius: 22px 22px 0 0;
  margin-top: auto; max-height: 92vh;
  display: flex; flex-direction: column;
  animation: slideUp .32s cubic-bezier(.32,1,.23,1);
  position: relative; z-index: 1;
  border-top: .5px solid var(--border);
  /* Zorgt dat flex child kan krimpen en scroll werkt op iOS */
  min-height: 0;
}
@keyframes slideUp { from { transform: translateY(100%); opacity: .4; } }

.sheet-handle {
  width: 36px; height: 4px; border-radius: 2px;
  background: var(--border); margin: 10px auto 0; flex-shrink: 0;
}
.sheet-scroll {
  flex: 1; min-height: 0;           /* cruciaal voor iOS scroll */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* iOS momentum scroll */
  overscroll-behavior: contain;
  padding: 16px 20px calc(32px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}

/* ── Result content ──────────────────────────────────────────── */
.result-summary { font-size: 15px; color: var(--text2); line-height: 1.55; }

.macro-totals {
  background: var(--surface); border-radius: 18px; padding: 18px 16px 14px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
}
.macro-pill { text-align: center; }
.macro-pill-val { font-size: 21px; font-weight: 800; letter-spacing: -.5px; }
.macro-pill-label { font-size: 11px; color: var(--text3); margin-top: 3px; text-transform: uppercase; letter-spacing: .4px; }
.macro-pill.cal .macro-pill-val { color: var(--yellow); }
.macro-pill.prot .macro-pill-val { color: var(--blue); }
.macro-pill.carb .macro-pill-val { color: var(--green); }
.macro-pill.fat .macro-pill-val { color: var(--orange); }

.confidence-bar {
  height: 4px; border-radius: 2px; background: var(--surface2);
  margin-top: 12px; overflow: hidden; grid-column: 1/-1;
}
.confidence-fill { height: 100%; border-radius: 2px; background: var(--accent); transition: width .5s; }

/* Items */
.items-list { display: flex; flex-direction: column; gap: 8px; }
.item-card { background: var(--surface); border-radius: 14px; overflow: hidden; border: .5px solid var(--border); }
.item-header { padding: 13px 14px; display: flex; align-items: center; gap: 10px; cursor: pointer; }
.item-name { flex: 1; font-size: 15px; font-weight: 500; }
.item-cal { font-size: 15px; font-weight: 700; color: var(--yellow); }
.item-chevron {
  color: var(--text3); transition: transform .22s;
  display: flex; align-items: center;
}
.item-chevron.open { transform: rotate(180deg); }

.item-detail {
  padding: 0 14px 13px; border-top: .5px solid var(--border);
  display: none; flex-direction: column; gap: 7px;
}
.item-detail.open { display: flex; }
.item-macros { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 2px; }
.item-macro { font-size: 13px; color: var(--text2); }
.item-macro span { color: var(--text); font-weight: 600; }
.item-weight { font-size: 13px; color: var(--text3); }
.item-source { font-size: 12px; color: var(--text3); }
.item-assumptions { font-size: 12px; color: var(--text3); font-style: italic; line-height: 1.4; }

.confidence-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }

/* ══════════════════════════════════════════════════════════════
   AI TOELICHTING accordion
══════════════════════════════════════════════════════════════ */
.ai-accordion {
  background: var(--surface); border-radius: 14px; overflow: hidden;
  border: .5px solid var(--border);
}
.ai-accordion-header {
  width: 100%; padding: 13px 14px; display: flex; align-items: center; gap: 9px;
  background: none; border: none; cursor: pointer; color: var(--text);
  text-align: left;
}
.ai-accordion-header:active { opacity: .75; }
.ai-accordion-icon { color: var(--accent); display: flex; flex-shrink: 0; }
.ai-accordion-title { flex: 1; font-size: 15px; font-weight: 600; }
.ai-conf-badge {
  font-size: 12px; font-weight: 700; color: var(--accent);
  background: var(--accent-dim); padding: 3px 8px; border-radius: 20px;
}
.ai-chev { color: var(--text3); transition: transform .22s; flex-shrink: 0; }
.ai-chev.open { transform: rotate(180deg); }

.ai-accordion-body {
  border-top: .5px solid var(--border);
  padding: 14px; display: flex; flex-direction: column; gap: 14px;
}
.ai-accordion-body.hidden { display: none; }

/* Confidence row */
.ai-conf-row {
  display: flex; align-items: center; gap: 10px;
}
.ai-conf-label { font-size: 13px; color: var(--text2); flex-shrink: 0; }
.ai-conf-bar {
  flex: 1; height: 6px; border-radius: 3px; background: var(--surface2); overflow: hidden;
}
.ai-conf-bar-fill { height: 100%; border-radius: 3px; transition: width .5s; }
.ai-conf-pct { font-size: 13px; font-weight: 700; flex-shrink: 0; width: 34px; text-align: right; }

/* Sources */
.ai-section { display: flex; flex-direction: column; gap: 5px; }
.ai-section-label {
  font-size: 11px; font-weight: 600; color: var(--text3);
  text-transform: uppercase; letter-spacing: .5px;
}
.ai-source-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-source-chip {
  font-size: 12px; color: var(--text2); background: var(--surface2);
  padding: 4px 10px; border-radius: 20px; border: .5px solid var(--border);
}

/* Per-item assumptions */
.ai-item-row {
  display: flex; flex-direction: column; gap: 3px;
  padding: 8px 0; border-bottom: .5px solid var(--border);
}
.ai-item-row:last-child { border-bottom: none; padding-bottom: 0; }
.ai-item-header { display: flex; align-items: center; gap: 8px; }
.ai-item-name { font-size: 13px; font-weight: 600; flex: 1; }
.ai-item-conf { font-size: 12px; color: var(--text3); }
.ai-item-assumptions-text { font-size: 12px; color: var(--text3); font-style: italic; line-height: 1.4; }

/* Notes */
.ai-notes-list { display: flex; flex-direction: column; gap: 5px; }
.ai-note {
  display: flex; gap: 8px; font-size: 13px; color: var(--text2); line-height: 1.45;
}
.ai-note::before { content: '•'; color: var(--accent); flex-shrink: 0; }

.ai-redenering {
  font-size: 13px; color: var(--text2); line-height: 1.7;
  white-space: pre-wrap; background: var(--surface2);
  border-radius: 10px; padding: 12px 14px;
}

/* ── Sheet actions ───────────────────────────────────────────── */
.sheet-actions { display: flex; flex-direction: column; gap: 8px; padding-top: 4px; }

/* ── History detail ──────────────────────────────────────────── */
.history-detail-date { font-size: 13px; color: var(--text3); text-align: center; padding: 4px 0 12px; }
.history-detail-img { width: 100%; max-height: 240px; object-fit: cover; border-radius: 14px; }
.detail-close { margin-top: 6px; }
