:root {
  --bg: oklch(88% 0.07 231);
  --card: #ffffff;
  --ink: oklch(28% 0.045 258);
  --ink-soft: oklch(48% 0.03 258);
  --ink-faint: oklch(60% 0.02 258);
  --red: oklch(60% 0.19 26);
  --red-soft: oklch(60% 0.19 26 / 0.16);
  --mint: oklch(72% 0.12 167);
  --mint-tint: oklch(93% 0.05 167);
  --track: oklch(92% 0.02 231);
  --track-strong: oklch(85% 0.015 231);
  --divider: oklch(91% 0.012 231);
  --divider-soft: oklch(94% 0.01 231);
  --neutral-fill: oklch(50% 0.03 258);
  --scan-bg: oklch(22% 0.03 255);
  --pill-bg: oklch(93% 0.015 231);
  --shadow-card: 0 1px 6px oklch(28% 0.045 258 / 0.05);
  --shadow-card-lg: 0 2px 14px oklch(28% 0.045 258 / 0.07);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  font-family: -apple-system, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button, input {
  font-family: inherit;
}

#app {
  position: relative;
  min-height: 100vh;
  min-height: 100dvh;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.05'/%3E%3C/svg%3E");
}

.screen {
  position: absolute;
  inset: 0;
  background: var(--bg);
  background-image: inherit;
}

@keyframes scanline { 0% { transform: translateY(-100px); } 50% { transform: translateY(100px); } 100% { transform: translateY(-100px); } }
@keyframes sheetUp { from { transform: translateY(24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Onboarding ---------- */
.onb {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  padding: calc(70px + var(--safe-top)) 28px calc(44px + var(--safe-bottom));
  background: var(--bg);
  background-image: inherit;
}
.onb-step { flex: 1; display: flex; flex-direction: column; gap: 22px; justify-content: center; }
.onb-hero { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px; text-align: center; overflow: hidden; }
.onb-shape { position: absolute; z-index: 0; }
.onb-fg { position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center; gap: 22px; }
.onb-icon {
  width: 92px; height: 92px; border-radius: 26px; background: var(--red);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 20px 40px oklch(60% 0.19 26 / 0.32), 0 4px 10px oklch(28% 0.045 258 / 0.15);
}
.onb-brand { font-size: 34px; font-weight: 800; color: var(--ink); letter-spacing: 0.01em; }
.onb-brand-sub { font-size: 15px; font-weight: 700; color: var(--red); letter-spacing: 0.12em; margin-top: 2px; }
.onb-value-prop { font-size: 16px; line-height: 1.5; color: var(--ink-soft); max-width: 280px; }
.onb-title { font-size: 24px; font-weight: 700; color: var(--ink); }
.onb-subtitle { font-size: 15px; color: var(--ink-soft); margin-top: 8px; }
.onb-center { text-align: center; }

.card {
  background: var(--card);
  border-radius: 20px;
  box-shadow: var(--shadow-card);
}
.stepper-card {
  background: var(--card); border-radius: 24px; padding: 28px 20px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  box-shadow: 0 2px 10px oklch(28% 0.045 258 / 0.06);
}
.stepper-label { font-size: 13px; letter-spacing: 0.06em; color: var(--ink-faint); text-transform: uppercase; }
.stepper-row { display: flex; align-items: center; gap: 20px; }
.step-btn {
  border: none; background: var(--pill-bg); color: var(--ink); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.step-btn.lg { width: 44px; height: 44px; border-radius: 22px; font-size: 22px; }
.step-btn.sm { width: 30px; height: 30px; border-radius: 15px; font-size: 17px; }
.step-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.stepper-value.lg { font-size: 38px; font-weight: 700; color: var(--red); min-width: 110px; text-align: center; }
.stepper-unit { font-size: 13px; color: var(--ink-faint); }

.watch-card { background: var(--card); border-radius: 20px; padding: 16px 18px; display: flex; align-items: center; gap: 14px; box-shadow: 0 2px 10px oklch(28% 0.045 258 / 0.06); }
.watch-icon { width: 44px; height: 44px; border-radius: 14px; background: var(--mint-tint); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.watch-name { font-size: 16px; font-weight: 600; color: var(--ink); }
.watch-sub { font-size: 13px; color: var(--ink-faint); }

.toggle-card { background: var(--card); border-radius: 20px; padding: 14px 18px; display: flex; align-items: center; justify-content: space-between; box-shadow: 0 2px 10px oklch(28% 0.045 258 / 0.06); }
.toggle-title { font-size: 15px; font-weight: 600; color: var(--ink); }
.toggle-sub { font-size: 12.5px; color: var(--ink-faint); margin-top: 2px; max-width: 230px; }

.toggle-switch { width: 46px; height: 27px; border-radius: 14px; border: none; cursor: pointer; position: relative; flex-shrink: 0; transition: background .15s; padding: 0; }
.toggle-switch.on { background: var(--mint); }
.toggle-switch.off { background: oklch(88% 0.012 78); }
.toggle-knob { position: absolute; top: 2.5px; width: 22px; height: 22px; border-radius: 11px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.25); transition: left .15s; }
.toggle-switch.on .toggle-knob { left: 22px; }
.toggle-switch.off .toggle-knob { left: 3px; }

.onb-footer { display: flex; flex-direction: column; gap: 16px; margin-top: 24px; }
.dots { display: flex; justify-content: center; gap: 7px; }
.dot { height: 7px; border-radius: 4px; background: oklch(88% 0.015 78); transition: width .2s; }
.dot.active { width: 18px; background: var(--red); }
.dot:not(.active) { width: 7px; }
.btn-primary {
  width: 100%; padding: 16px; border-radius: 18px; border: none; background: var(--red);
  color: #fff; font-size: 17px; font-weight: 600; cursor: pointer;
}
.btn-primary:active { opacity: 0.85; }

/* ---------- Main app ---------- */
.main-app { position: absolute; inset: 0; display: flex; flex-direction: column; }
.main-scroll { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.tab-view { padding: calc(60px + var(--safe-top)) 20px 110px; display: flex; flex-direction: column; gap: 16px; min-height: 100%; }

.tab-header { display: flex; justify-content: space-between; align-items: flex-start; }
.greeting { font-size: 15px; color: var(--ink-soft); }
.username { font-size: 26px; font-weight: 700; color: var(--ink); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 20px; border: none; background: var(--card);
  box-shadow: 0 1px 4px oklch(28% 0.045 258 / 0.1); display: flex; align-items: center; justify-content: center; cursor: pointer;
  flex-shrink: 0;
}

.hero-card { background: var(--card); border-radius: 28px; padding: 22px; box-shadow: var(--shadow-card-lg); }
.hero-top { display: flex; align-items: center; gap: 20px; }
.hero-eyebrow { font-size: 12.5px; letter-spacing: 0.05em; color: var(--ink-faint); text-transform: uppercase; }
.hero-remaining { font-size: 34px; font-weight: 700; color: var(--ink); line-height: 1.1; }
.hero-sub { font-size: 13px; color: var(--ink-faint); }
.hero-divider { height: 1px; background: var(--divider); margin: 18px 0; }
.hero-stats { display: flex; justify-content: space-between; }
.hero-stat-label { font-size: 11.5px; color: var(--ink-faint); letter-spacing: 0.04em; }
.hero-stat-value { font-size: 17px; font-weight: 600; color: var(--ink); margin-top: 2px; }
.hero-stat-value.mint { color: var(--mint); }
.hero-stat.right { text-align: right; }

.macro-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.macro-card { background: var(--card); border-radius: 18px; padding: 14px; box-shadow: var(--shadow-card); }
.macro-label { font-size: 11px; color: var(--ink-faint); letter-spacing: 0.04em; }
.macro-value { font-size: 17px; font-weight: 700; color: var(--ink); margin-top: 2px; }
.macro-of { font-size: 11px; color: var(--ink-faint); margin-bottom: 8px; }
.bar-track { height: 5px; border-radius: 3px; background: var(--track); }
.bar-fill { height: 100%; border-radius: 3px; }
.bar-fill.mint { background: var(--mint); }
.bar-fill.neutral { background: var(--neutral-fill); }

.exercise-card {
  background: var(--card); border: none; border-radius: 20px; padding: 14px 16px;
  display: flex; align-items: center; gap: 12px; box-shadow: var(--shadow-card); cursor: pointer; text-align: left; width: 100%;
}
.exercise-icon { width: 40px; height: 40px; border-radius: 13px; background: var(--mint-tint); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.exercise-name { font-size: 15px; font-weight: 600; color: var(--ink); }
.exercise-sub { font-size: 12.5px; color: var(--ink-faint); }
.exercise-plus { font-size: 15px; font-weight: 700; color: var(--mint); white-space: nowrap; }

.section-header { display: flex; justify-content: space-between; align-items: baseline; margin-top: 6px; }
.section-title { font-size: 18px; font-weight: 700; color: var(--ink); }
.link-btn { border: none; background: none; color: var(--red); font-size: 14px; font-weight: 600; cursor: pointer; padding: 0; }

.food-row { background: var(--card); border-radius: 16px; padding: 12px 14px; display: flex; align-items: center; gap: 12px; box-shadow: 0 1px 5px oklch(28% 0.045 258 / 0.04); cursor: pointer; }
.avatar { width: 36px; height: 36px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 14px; flex-shrink: 0; }
.food-name { font-size: 14.5px; font-weight: 600; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.food-meta { font-size: 12px; color: var(--ink-faint); }
.food-cals { font-size: 14.5px; font-weight: 700; color: var(--ink); flex-shrink: 0; }

.page-title { font-size: 26px; font-weight: 700; color: var(--ink); }

/* Diary */
.meal-card { background: var(--card); border-radius: 20px; padding: 14px 16px; box-shadow: var(--shadow-card); }
.meal-header { display: flex; justify-content: space-between; align-items: center; }
.meal-name { font-size: 15.5px; font-weight: 700; color: var(--ink); }
.meal-total { font-size: 13px; color: var(--ink-faint); }
.meal-entry { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--divider-soft); cursor: pointer; }
.meal-entry-avatar { width: 30px; height: 30px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12.5px; flex-shrink: 0; }
.meal-entry-name { font-size: 14px; font-weight: 600; color: var(--ink); }
.meal-entry-macro { font-size: 11.5px; color: var(--ink-faint); }
.meal-entry-cals { font-size: 14px; font-weight: 700; color: var(--ink); }
.add-food-btn {
  width: 100%; margin-top: 8px; padding: 9px; border-radius: 12px; border: 1px dashed var(--track-strong);
  background: none; color: var(--ink-soft); font-size: 13px; font-weight: 600; cursor: pointer;
}

/* Targets */
.settings-card { background: var(--card); border-radius: 20px; overflow: hidden; box-shadow: var(--shadow-card); }
.settings-row { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--divider-soft); }
.settings-row:last-child { border-bottom: none; }
.settings-row-title { font-size: 15px; font-weight: 600; color: var(--ink); }
.stepper-row.sm-gap { gap: 14px; }
.stepper-value.sm { font-size: 16px; font-weight: 700; color: var(--ink); min-width: 52px; text-align: center; }

.conn-row { display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--divider-soft); }
.conn-row:last-child { border-bottom: none; }
.conn-dot { width: 8px; height: 8px; border-radius: 4px; }
.conn-dot.mint { background: var(--mint); }
.conn-dot.off { background: var(--track-strong); }
.conn-name { flex: 1; font-size: 14.5px; font-weight: 600; color: var(--ink); }
.conn-status { font-size: 13px; color: var(--ink-faint); }
.conn-status.link { color: var(--red); }

.text-link-btn { background: none; border: none; color: var(--ink-faint); font-size: 13px; text-align: center; padding: 8px; cursor: pointer; }
.targets-note { font-size: 12.5px; color: var(--ink-faint); line-height: 1.4; padding: 0 4px; margin-top: -6px; }

/* Tab bar */
.tab-bar {
  position: relative; flex-shrink: 0; display: flex; align-items: center; justify-content: space-around;
  padding: 10px 10px calc(var(--safe-bottom) + 10px);
  background: rgba(255,255,255,0.92); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--divider);
}
.tab-btn { background: none; border: none; display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer; padding: 4px 10px; }
.tab-label { font-size: 10.5px; font-weight: 600; }
.scan-fab {
  width: 56px; height: 56px; border-radius: 28px; background: var(--red); border: 4px solid rgba(255,255,255,0.92);
  display: flex; align-items: center; justify-content: center; box-shadow: 0 6px 16px oklch(60% 0.19 26 / 0.4);
  cursor: pointer; margin-top: -26px;
}

/* Sub-screen (Exercise) */
.subscreen { position: absolute; inset: 0; background: var(--bg); background-image: inherit; display: flex; flex-direction: column; }
.subscreen-header { padding: calc(60px + var(--safe-top)) 20px 14px; display: flex; align-items: center; gap: 14px; }
.back-btn { width: 36px; height: 36px; border-radius: 18px; border: none; background: var(--card); box-shadow: 0 1px 4px oklch(28% 0.045 258 / 0.1); display: flex; align-items: center; justify-content: center; cursor: pointer; }
.subscreen-title { font-size: 20px; font-weight: 700; color: var(--ink); }
.subscreen-body { flex: 1; overflow: auto; padding: 6px 20px 40px; display: flex; flex-direction: column; gap: 14px; }
.section-label { font-size: 15px; font-weight: 700; color: var(--ink); }
.workout-row { background: var(--card); border-radius: 18px; padding: 14px 16px; display: flex; align-items: center; gap: 12px; box-shadow: var(--shadow-card); }
.workout-name { font-size: 14.5px; font-weight: 600; color: var(--ink); }
.workout-mins { font-size: 12.5px; color: var(--ink-faint); }
.workout-cals { font-size: 15px; font-weight: 700; color: var(--mint); }
.log-workout-btn { padding: 12px; border-radius: 14px; border: 1px dashed var(--track-strong); background: none; color: var(--ink-soft); font-size: 13.5px; font-weight: 600; cursor: pointer; }

/* Scan overlay */
.scan-overlay { position: absolute; inset: 0; background: var(--scan-bg); display: flex; flex-direction: column; z-index: 80; }
.scan-header { padding: calc(60px + var(--safe-top)) 18px 10px; display: flex; align-items: center; justify-content: space-between; }
.scan-close-btn { width: 36px; height: 36px; border-radius: 18px; border: none; background: rgba(255,255,255,0.15); display: flex; align-items: center; justify-content: center; cursor: pointer; }
.scan-title { font-size: 16px; font-weight: 600; color: #fff; }
.scan-body { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 26px; }
.scan-body-camera { position: relative; overflow: hidden; gap: 0; }
.scan-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.scan-scrim-top { position: absolute; top: 0; left: 0; right: 0; height: 160px; background: linear-gradient(rgba(0,0,0,0.4), transparent); pointer-events: none; }
.scan-foreground { position: relative; display: flex; flex-direction: column; align-items: center; gap: 16px; margin-top: 24px; }
.scan-spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid rgba(255,255,255,0.25); border-top-color: #fff; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.viewfinder { position: relative; width: 250px; height: 250px; }
.corner { position: absolute; width: 38px; height: 38px; border-color: var(--red); border-style: solid; border-width: 0; }
.corner.tl { top: 0; left: 0; border-top-width: 4px; border-left-width: 4px; border-radius: 8px 0 0 0; }
.corner.tr { top: 0; right: 0; border-top-width: 4px; border-right-width: 4px; border-radius: 0 8px 0 0; }
.corner.bl { bottom: 0; left: 0; border-bottom-width: 4px; border-left-width: 4px; border-radius: 0 0 0 8px; }
.corner.br { bottom: 0; right: 0; border-bottom-width: 4px; border-right-width: 4px; border-radius: 0 0 8px 0; }
.scan-line { position: absolute; left: 14px; right: 14px; top: 50%; height: 2.5px; background: var(--red); box-shadow: 0 0 12px var(--red); animation: scanline 2.2s ease-in-out infinite; }
.scan-hint { color: rgba(255,255,255,0.65); font-size: 14.5px; }
.scan-simulate-btn { padding: 14px 28px; border-radius: 26px; border: none; background: var(--red); color: #fff; font-size: 15px; font-weight: 600; cursor: pointer; }
.scan-manual-link { background: none; border: none; color: rgba(255,255,255,0.7); font-size: 14px; text-decoration: underline; cursor: pointer; }

.result-sheet-wrap { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; }
.result-sheet { background: var(--card); border-radius: 28px 28px 0 0; padding: 24px 22px calc(30px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 16px; animation: sheetUp 0.3s ease-out; }
.result-name { font-size: 19px; font-weight: 700; color: var(--ink); }
.result-meta { font-size: 12.5px; color: var(--ink-faint); margin-top: 3px; }
.result-macros { display: grid; grid-template-columns: repeat(5, minmax(0,1fr)); gap: 6px; }
.result-macro-value { font-size: 17px; font-weight: 700; }
.result-macro-label { font-size: 10.5px; color: var(--ink-faint); }

.meal-chips { display: flex; gap: 8px; flex-wrap: wrap; }
.meal-chip { padding: 8px 16px; border-radius: 16px; border: 1.5px solid; font-size: 13.5px; font-weight: 600; cursor: pointer; }
.meal-chip.active { background: var(--red); color: #fff; border-color: var(--red); }
.meal-chip.inactive { background: #fff; color: var(--ink); border-color: oklch(88% 0.012 78); }

/* Manual entry overlay */
.manual-overlay { position: absolute; inset: 0; background: var(--bg); background-image: inherit; display: flex; flex-direction: column; z-index: 80; }
.manual-header { padding: calc(60px + var(--safe-top)) 20px 12px; display: flex; align-items: center; justify-content: space-between; }
.manual-title { font-size: 20px; font-weight: 700; color: var(--ink); }
.manual-close-btn { width: 34px; height: 34px; border-radius: 17px; border: none; background: var(--pill-bg); display: flex; align-items: center; justify-content: center; cursor: pointer; }
.manual-body { flex: 1; overflow: auto; padding: 4px 20px 40px; display: flex; flex-direction: column; gap: 18px; }
.search-placeholder { padding: 12px 14px; border-radius: 14px; background: var(--card); box-shadow: 0 1px 5px oklch(28% 0.045 258 / 0.05); color: var(--ink-faint); font-size: 14.5px; }
.subsection-label { font-size: 13px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0.04em; text-transform: uppercase; margin-bottom: 8px; }
.quick-food-list { display: flex; flex-direction: column; gap: 8px; }
.quick-food-btn { background: var(--card); border: none; border-radius: 16px; padding: 12px 14px; display: flex; align-items: center; gap: 12px; box-shadow: 0 1px 5px oklch(28% 0.045 258 / 0.05); cursor: pointer; text-align: left; width: 100%; }
.quick-food-avatar { width: 34px; height: 34px; border-radius: 11px; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0; }
.quick-food-serving { font-size: 12px; color: var(--ink-faint); }
.hr { height: 1px; background: var(--divider); }
.custom-food-form { display: flex; flex-direction: column; gap: 12px; }
.text-input {
  padding: 13px 14px; border-radius: 14px; border: 1px solid var(--divider);
  background: var(--card); font-size: 14.5px; color: var(--ink); outline: none; width: 100%;
}
.text-input:focus { border-color: var(--red); }
.input-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.submit-btn { width: 100%; padding: 15px; border-radius: 18px; border: none; background: var(--red); color: #fff; font-size: 16px; font-weight: 600; cursor: pointer; margin-top: 4px; }
.submit-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.delete-food-btn { width: 100%; padding: 13px; border-radius: 18px; border: 1.5px solid var(--red); background: none; color: var(--red); font-size: 15px; font-weight: 600; cursor: pointer; }

/* Install toast */
.install-toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(var(--safe-bottom) + 16px);
  background: var(--ink); color: #fff; border-radius: 16px; padding: 14px 16px;
  display: flex; align-items: center; gap: 12px; box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  z-index: 200; animation: fadeIn .25s ease-out;
}
.install-toast span { flex: 1; font-size: 14px; }
.install-toast button { border: none; border-radius: 10px; padding: 8px 14px; font-size: 13px; font-weight: 600; cursor: pointer; }
.install-toast .install-btn { background: var(--red); color: #fff; }
.install-toast .dismiss-btn { background: rgba(255,255,255,0.15); color: #fff; }

@media (min-width: 560px) {
  #app { max-width: 480px; margin: 0 auto; box-shadow: 0 0 40px rgba(0,0,0,0.08); }
  html, body { background: oklch(80% 0.02 231); }
}
