/* ==========================================================================
   What Am I Eating? — Cognitive-First Botanical & Traffic-Light Design System
   Grounded in Pre-Attentive Visual Processing, Dual-Coding (Color + Level + Icon),
   OKLCH Perceptual Uniformity, and Zero-Overflow Mobile Ergonomics (Galaxy S24 Ultra).
   ========================================================================== */

:root {
  /* Neutral Warm Linen Canvas (keeps high-signal traffic-light colors unmistakable) */
  --color-bg:            oklch(96.8% 0.010 85);
  --color-surface:       oklch(99.5% 0.004 85);
  --color-surface-alt:   oklch(93.8% 0.012 82);
  --color-surface-inv:   oklch(21% 0.024 152);
  --color-border:        oklch(87% 0.013 78);
  --color-border-strong: oklch(76% 0.018 74);

  --color-text:          oklch(19% 0.016 55);
  --color-text-soft:     oklch(34% 0.016 58);
  --color-muted:         oklch(47% 0.016 65);

  /* Brand Forest Accent */
  --color-accent:        oklch(38% 0.095 154);
  --color-accent-hover:  oklch(32% 0.100 154);
  --color-accent-soft:   oklch(93% 0.032 150);
  --color-accent-ink:    oklch(98.5% 0.005 85);

  /* Pre-Attentive Traffic-Light Semantic System (WCAG AA + High Signal) */
  /* GREEN = Safe / Low Risk / Optimal */
  --tl-green:            oklch(43% 0.135 148);
  --tl-green-bg:         oklch(94.5% 0.042 148);
  --tl-green-border:     oklch(76% 0.095 148);

  /* AMBER = Medium / Caution / Watch Portion (e.g., GL 11–19) */
  --tl-amber:            oklch(48% 0.145 72);
  --tl-amber-bg:         oklch(95.5% 0.058 84);
  --tl-amber-border:     oklch(78% 0.125 78);

  /* RED = High Risk / Limit / Trigger (e.g., GL 20+, Nitrites, High Sodium) */
  --tl-red:              oklch(46% 0.195 25);
  --tl-red-bg:           oklch(94.5% 0.048 25);
  --tl-red-border:       oklch(74% 0.135 25);

  /* Backward-compatible semantic aliases */
  --color-positive:      var(--tl-green);
  --color-positive-bg:   var(--tl-green-bg);
  --color-caution:       var(--tl-amber);
  --color-caution-bg:    var(--tl-amber-bg);
  --color-warning:       var(--tl-red);
  --color-warning-bg:    var(--tl-red-bg);
  --color-danger:        var(--tl-red);
  --color-danger-bg:     var(--tl-red-bg);

  /* Typography */
  --font-display: 'Fraunces', Georgia, serif;
  --font-sans:    'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono:    'JetBrains Mono', ui-monospace, SFMono-Regular, monospace;

  /* 8px Spacing Rhythm */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  /* Radii */
  --radius-sm:   8px;
  --radius-md:   12px;
  --radius-lg:   16px;
  --radius-xl:   22px;
  --radius-full: 9999px;

  /* Motion */
  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --dur-fast: 140ms;
  --dur-base: 240ms;

  /* Subtle tactile shadows */
  --shadow-xs: 0 1px 2px oklch(18% 0.02 60 / 6%);
  --shadow-sm: 0 3px 10px oklch(18% 0.02 60 / 7%);
  --shadow-md: 0 10px 26px oklch(18% 0.02 60 / 10%);
  --shadow-lg: 0 20px 44px oklch(16% 0.02 60 / 16%);

  --rail-width: 244px;
  --bottom-nav-h: 64px;
}

/* Warm Espresso Dark Mode (calibrated traffic-light luminance for dark OLED like S24 Ultra!) */
[data-theme="dark"] {
  --color-bg:            oklch(14% 0.010 60);
  --color-surface:       oklch(19.5% 0.012 60);
  --color-surface-alt:   oklch(24.5% 0.014 62);
  --color-surface-inv:   oklch(27% 0.045 152);
  --color-border:        oklch(30% 0.014 62);
  --color-border-strong: oklch(42% 0.016 65);

  --color-text:          oklch(94% 0.010 85);
  --color-text-soft:     oklch(82% 0.010 82);
  --color-muted:         oklch(67% 0.012 76);

  --color-accent:        oklch(70% 0.125 152);
  --color-accent-hover:  oklch(76% 0.130 152);
  --color-accent-soft:   oklch(25% 0.045 152);
  --color-accent-ink:    oklch(14% 0.020 152);

  --tl-green:            oklch(74% 0.145 148);
  --tl-green-bg:         oklch(23% 0.048 148);
  --tl-green-border:     oklch(42% 0.095 148);

  --tl-amber:            oklch(80% 0.145 80);
  --tl-amber-bg:         oklch(25% 0.052 76);
  --tl-amber-border:     oklch(48% 0.110 78);

  --tl-red:              oklch(72% 0.185 26);
  --tl-red-bg:           oklch(24% 0.068 24);
  --tl-red-border:       oklch(48% 0.135 25);

  --shadow-xs: 0 1px 3px oklch(0% 0 0 / 28%);
  --shadow-sm: 0 4px 12px oklch(0% 0 0 / 34%);
  --shadow-md: 0 12px 28px oklch(0% 0 0 / 44%);
  --shadow-lg: 0 24px 48px oklch(0% 0 0 / 56%);
}

/* ==========================================================================
   Bulletproof Zero-Overflow Mobile Base (Prevents Chrome S24 Ultra Auto-Zoom)
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  width: 100%;
  max-width: 100%;
  overflow-x: clip;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: clip;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-sans);
  font-size: 0.94rem;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
  touch-action: manipulation;
}

/* Critical CSS Grid/Flex overflow protection on narrow phones */
main, section, article, div, aside, header {
  min-width: 0;
}

h1, h2, h3, h4, .font-display {
  font-family: var(--font-display);
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.02em;
  margin: 0;
  overflow-wrap: break-word;
}

p { margin: 0; overflow-wrap: break-word; }
button, input, select, textarea { font: inherit; color: inherit; }
/* Keep input font-size >= 16px on mobile so tapping inputs never triggers browser auto-zoom! */
input[type="text"], input[type="number"], input[type="date"], select {
  font-size: 16px;
}
@media (min-width: 768px) {
  input[type="text"], input[type="number"], input[type="date"], select {
    font-size: 0.94rem;
  }
}

button { cursor: pointer; }
img { max-width: 100%; display: block; }
a { color: var(--color-accent); text-underline-offset: 3px; }

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.skip-link {
  position: fixed; top: -100px; left: 16px; z-index: 9999;
  background: var(--color-accent); color: var(--color-accent-ink);
  padding: 10px 16px; border-radius: var(--radius-md); font-weight: 600;
}
.skip-link:focus { top: 12px; }

:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ==========================================================================
   App Shell (Tight Mobile App Chrome -> Desktop Left Editorial Rail)
   ========================================================================== */
.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  width: 100%;
  min-height: 100dvh;
}

.side-rail { display: none; }

.mobile-header {
  position: sticky;
  top: 0;
  z-index: 45;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top, 10px));
  background: color-mix(in oklch, var(--color-bg) 92%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--color-border);
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--color-text);
  min-width: 0;
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  flex-shrink: 0;
}

.brand-title {
  font-family: var(--font-display);
  font-size: 1.06rem;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
}

.brand-sub {
  font-size: 0.66rem;
  color: var(--color-muted);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  font-weight: 700;
}

.main-content {
  width: 100%;
  max-width: 1040px;
  margin-inline: auto;
  padding: 12px 14px calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px) + 24px);
}

/* Thumb-Friendly Native-Style Mobile Bottom Nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  height: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: color-mix(in oklch, var(--color-surface) 95%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--color-border);
}

.bottom-nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 0;
  background: transparent;
  color: var(--color-muted);
  font-size: 0.7rem;
  font-weight: 700;
  min-height: 48px;
  position: relative;
  transition: color var(--dur-fast), transform var(--dur-fast);
}
.bottom-nav-btn i { font-size: 1.32rem; }
.bottom-nav-btn.active { color: var(--color-accent); }
.bottom-nav-btn.active::before {
  content: '';
  position: absolute;
  top: 0;
  width: 28px;
  height: 3px;
  border-radius: 0 0 4px 4px;
  background: var(--color-accent);
}
.bottom-nav-btn:active { transform: scale(0.94); }

/* Desktop Layout (>= 960px) */
@media (min-width: 960px) {
  .app-shell {
    grid-template-columns: var(--rail-width) minmax(0, 1fr);
  }
  .mobile-header, .bottom-nav { display: none; }
  .side-rail {
    position: sticky;
    top: 0;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 22px 16px;
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    overflow-y: auto;
  }
  .main-content {
    padding: 24px 32px 48px;
  }
}

.rail-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 22px;
}

.rail-nav-btn {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 10px 13px;
  border: 0;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--color-text-soft);
  font-weight: 600;
  font-size: 0.9rem;
  text-align: left;
  transition: background var(--dur-fast), color var(--dur-fast);
}
.rail-nav-btn i { font-size: 1.2rem; }
.rail-nav-btn:hover { background: var(--color-surface-alt); color: var(--color-text); }
.rail-nav-btn.active { background: var(--color-accent-soft); color: var(--color-accent); }

.rail-badge {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: var(--radius-full);
  background: var(--color-surface-alt);
  color: var(--color-text-soft);
}
.rail-nav-btn.active .rail-badge {
  background: var(--color-accent);
  color: var(--color-accent-ink);
}

.rail-footer {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 14px;
  border-top: 1px solid var(--color-border);
}
.rail-credit { font-size: 0.72rem; color: var(--color-muted); line-height: 1.4; }

/* ==========================================================================
   Shared Primitives
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-weight: 700;
  font-size: 0.88rem;
  white-space: nowrap;
  max-width: 100%;
  transition: transform var(--dur-fast) var(--ease-out-expo), background var(--dur-fast), border-color var(--dur-fast);
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--color-accent);
  color: var(--color-accent-ink);
}
.btn-primary:hover:not(:disabled) { background: var(--color-accent-hover); }

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border-strong);
}
.btn-secondary:hover:not(:disabled) { background: var(--color-surface-alt); }

.btn-ghost {
  background: transparent;
  color: var(--color-text-soft);
}
.btn-ghost:hover:not(:disabled) { background: var(--color-surface-alt); color: var(--color-text); }

.btn-sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.81rem;
  border-radius: 9px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--color-surface-alt); }

.surface-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xs);
  max-width: 100%;
}

.section-kicker {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--color-accent);
}

.view-panel { display: none; animation: fadeSlideUp 220ms var(--ease-out-expo); }
.view-panel.active { display: block; }

@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Traffic-Light Pills — Dual-Coded, Safe Wrapping on Narrow Phones */
.tone-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: var(--radius-full);
  font-size: 0.73rem;
  font-weight: 700;
  line-height: 1.25;
  max-width: 100%;
}
.tone-pill.nowrap { white-space: nowrap; flex-shrink: 0; }
.tone-positive { background: var(--tl-green-bg); color: var(--tl-green); border: 1px solid color-mix(in oklch, var(--tl-green-border) 50%, transparent); }
.tone-caution  { background: var(--tl-amber-bg); color: var(--tl-amber); border: 1px solid color-mix(in oklch, var(--tl-amber-border) 55%, transparent); }
.tone-warning,
.tone-danger   { background: var(--tl-red-bg);   color: var(--tl-red);   border: 1px solid color-mix(in oklch, var(--tl-red-border) 55%, transparent); }
.tone-neutral  { background: var(--color-surface-alt); color: var(--color-text-soft); border: 1px solid var(--color-border); }

/* Toast Rack */
.toast-rack {
  position: fixed;
  bottom: calc(var(--bottom-nav-h) + env(safe-area-inset-bottom, 0px) + 10px);
  left: 12px;
  right: 12px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
@media (min-width: 960px) {
  .toast-rack { left: auto; right: 24px; bottom: 24px; width: 380px; }
}
.toast {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  background: var(--color-surface-inv);
  color: var(--color-accent-ink);
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  animation: fadeSlideUp 200ms var(--ease-out-expo);
}
