/* ── Design tokens ── */
:root {
  --navy:    #0a0f1e;
  --teal:    #1D9E75;
  --teal-lt: #E1F5EE;
  --teal-dk: #085041;
  --amber:   #EF9F27;
  --red:     #E24B4A;
  --surface: #f7f8fa;
  --white:   #ffffff;
  --border:  #e2e4e8;
  --text-primary:   #0a0f1e;
  --text-secondary: #888;
  --text-hint:      #bbb;
  --radius-sm:  8px;
  --radius-md: 12px;
  --radius-lg: 20px;
  --radius-xl: 28px;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface:        #0f1520;
    --white:          #1a2035;
    --border:         #2a3248;
    --text-primary:   #e8eaf0;
    --text-secondary: #7a8099;
    --text-hint:      #4a5270;
  }
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  color: var(--text-primary);
  background: var(--surface);
  -webkit-font-smoothing: antialiased;
}
input, select, button, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}
button { cursor: pointer; }
a { color: inherit; text-decoration: none; }

/* ── App shell ── */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* ── Screens wrapper — must fill remaining height after navbar ── */
#screens {
  flex: 1;
  min-height: 0;        /* critical — lets flex child shrink below content size */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Loading screen ── */
#loading-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  background: var(--navy);
  gap: 8px;
}
.loading-logo {
  font-size: 28px;
  font-weight: 600;
  color: #1D9E75;
  letter-spacing: .02em;
}
.loading-sub { font-size: 13px; color: rgba(255,255,255,.4); }

/* ── Screen container ── */
.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;        /* critical — allows children to scroll */
  overflow: hidden;
}
.screen.active { display: flex; }

/* ── Screen header ── */
.screen-header {
  background: var(--navy);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;       /* never compress the header */
}
.screen-header h1 {
  color: white;
  font-size: 16px;
  font-weight: 500;
  flex: 1;
}
.back-btn {
  width: 32px; height: 32px;
  background: rgba(255,255,255,.12);
  border: none;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white;
  flex-shrink: 0;
}
.header-action-btn {
  width: 32px; height: 32px;
  background: var(--teal);
  border: none;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white;
  font-size: 22px;
  line-height: 1;
}

/* ── Scrollable body ── */
.screen-body {
  flex: 1;
  min-height: 0;        /* critical — lets this div shrink and scroll */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px 90px; /* 90px bottom clears the fixed navbar */
  overscroll-behavior: contain;
}

/* ── Bottom nav ── */
#navbar {
  display: none;
  position: fixed;
  bottom: 0; left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 480px;
  background: var(--white);
  border-top: 0.5px solid var(--border);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  z-index: 100;
}
#navbar.visible { display: flex; }
.nav-items { display: flex; width: 100%; }
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 4px 0;
  border: none;
  background: transparent;
  color: var(--text-hint);
  font-size: 10px;
}
.nav-item.active { color: var(--teal); }
.nav-item svg { width: 22px; height: 22px; }
.nav-badge {
  position: absolute;
  top: 4px; right: calc(50% - 18px);
  width: 7px; height: 7px;
  background: var(--red);
  border-radius: 50%;
}

/* ── Cards ── */
.card {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 10px;
}
.card-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .07em;
  margin-bottom: 10px;
}

/* ── Metric grid ── */
.metric-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}
.metric-card {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.metric-label { font-size: 11px; color: var(--text-secondary); margin-bottom: 4px; }
.metric-value { font-size: 22px; font-weight: 500; }
.metric-sub { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }

/* ── Alerts ── */
.alert {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 8px;
  font-size: 12px;
}
.alert-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; margin-top: 3px; }
.alert.crit  { background: #FCEBEB; color: #A32D2D; }
.alert.crit .alert-dot  { background: var(--red); }
.alert.warn  { background: #FAEEDA; color: #854F0B; }
.alert.warn .alert-dot  { background: var(--amber); }
.alert.info  { background: #E6F1FB; color: #185FA5; }
.alert.info .alert-dot  { background: #378ADD; }
.alert.ok    { background: #EAF3DE; color: #3B6D11; }
.alert.ok .alert-dot    { background: var(--teal); }

/* ── Badges ── */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 500;
}
.badge-ok   { background: #EAF3DE; color: #3B6D11; }
.badge-warn { background: #FAEEDA; color: #854F0B; }
.badge-crit { background: #FCEBEB; color: #A32D2D; }
.badge-info { background: #E6F1FB; color: #185FA5; }

/* ── Form elements ── */
.field-group { margin-bottom: 14px; }
.field-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 5px;
}
.field-input {
  width: 100%;
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
}
.field-input:focus { outline: none; border-color: var(--teal); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.toggle-row { display: flex; gap: 6px; }
.toggle-opt {
  flex: 1;
  padding: 10px 8px;
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  transition: all .15s;
}
.toggle-opt.active {
  background: var(--navy);
  color: white;
  border-color: var(--navy);
  font-weight: 500;
}

.tag-group { display: flex; flex-wrap: wrap; gap: 6px; }
.tag {
  padding: 6px 12px;
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: 20px;
  font-size: 12px;
  color: var(--text-secondary);
  transition: all .15s;
}
.tag.selected {
  background: var(--navy);
  color: white;
  border-color: var(--navy);
}

/* ── Buttons ── */
.btn-primary {
  width: 100%;
  padding: 13px;
  background: var(--navy);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  margin-top: 6px;
}
.btn-primary:active { opacity: .85; }
.btn-success {
  width: 100%;
  padding: 13px;
  background: var(--teal);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
}
.btn-ghost {
  background: transparent;
  border: 0.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-size: 13px;
  color: var(--text-secondary);
}

/* ── Scan button ── */
.scan-btn {
  width: 100%;
  padding: 12px;
  background: var(--navy);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 10px;
}

/* ── Inventory item ── */
.inv-item {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-bottom: 8px;
}
.inv-item-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.inv-item-name { font-size: 14px; font-weight: 500; }
.inv-fields {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}
.inv-field {
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 8px;
}
.inv-field-label {
  font-size: 9px;
  color: var(--text-hint);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: 3px;
}
.inv-field input {
  font-size: 12px;
  font-weight: 500;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border);
  width: 100%;
  padding: 1px 0;
}
.inv-field input:focus { outline: none; border-color: var(--teal); }
.inv-field-static { font-size: 12px; font-weight: 500; color: var(--text-secondary); }
.inv-field-static small { font-size: 9px; color: var(--text-hint); margin-left: 3px; }

/* ── Progress bars ── */
.progress-wrap { margin-bottom: 10px; }
.progress-label-row {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.progress-track {
  height: 7px;
  background: var(--surface);
  border-radius: 20px;
  overflow: hidden;
  border: 0.5px solid var(--border);
}
.progress-fill { height: 100%; border-radius: 20px; transition: width .4s; }

/* ── Shopping list item ── */
.shop-item {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-bottom: 7px;
  display: flex;
  gap: 10px;
  align-items: center;
}
.shop-check {
  width: 22px; height: 22px;
  border-radius: 7px;
  border: 1.5px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .15s;
}
.shop-check.checked { background: var(--navy); border-color: var(--navy); }
.shop-check.checked::after {
  content: '';
  display: block;
  width: 5px; height: 9px;
  border: 2px solid white;
  border-top: none; border-left: none;
  transform: rotate(45deg) translateY(-1px);
}
.shop-item-body { flex: 1; }
.shop-item-name { font-size: 13px; font-weight: 500; }
.shop-item-meta { font-size: 11px; color: var(--text-secondary); margin-top: 1px; }
.shop-item-qty {
  font-size: 12px;
  font-weight: 500;
  background: var(--surface);
  padding: 4px 9px;
  border-radius: 8px;
  white-space: nowrap;
}

/* ── Home screen header ── */
.home-header {
  background: var(--navy);
  padding: 20px 18px 22px;
}
.home-appname {
  color: var(--teal);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .12em;
  text-transform: uppercase;
  margin-bottom: 4px;
}
.home-title {
  color: white;
  font-size: 21px;
  font-weight: 500;
  line-height: 1.25;
  margin-bottom: 16px;
}
.home-title span { color: var(--teal); }
.home-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.home-stat {
  background: rgba(255,255,255,.08);
  border-radius: 12px;
  padding: 10px 12px;
  border-left: 3px solid transparent;
}
.home-stat.ok   { border-left-color: var(--teal); }
.home-stat.warn { border-left-color: var(--amber); }
.home-stat.crit { border-left-color: var(--red); }
.home-stat-val  { font-size: 20px; font-weight: 500; color: white; }
.home-stat-lbl  { font-size: 11px; color: rgba(255,255,255,.5); margin-top: 1px; }

/* ── Menu grid ── */
.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 14px;
}
.menu-btn {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 14px 14px;
  text-align: left;
  transition: all .15s;
  position: relative;
}
.menu-btn:active { transform: scale(.97); }
.menu-btn.full { grid-column: 1 / -1; }
.menu-icon {
  width: 36px; height: 36px;
  border-radius: 12px;
  margin-bottom: 10px;
  display: flex; align-items: center; justify-content: center;
}
.menu-btn-title { font-size: 14px; font-weight: 500; }
.menu-btn-sub { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }
.menu-alert-dot {
  position: absolute;
  top: 12px; right: 12px;
  width: 8px; height: 8px;
  background: var(--red);
  border-radius: 50%;
}

/* ── Bottom sheet overlay ── */
.sheet-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10,15,30,.55);
  z-index: 200;
  align-items: flex-end;
}
.sheet-overlay.open { display: flex; }
.sheet {
  background: var(--surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  width: 100%;
  padding: 0 0 calc(16px + env(safe-area-inset-bottom));
  max-height: 90dvh;
  overflow-y: auto;
}
.sheet-handle {
  width: 36px; height: 4px;
  background: var(--border);
  border-radius: 20px;
  margin: 12px auto 14px;
}
.sheet-title {
  font-size: 15px;
  font-weight: 500;
  padding: 0 16px 12px;
  border-bottom: 0.5px solid var(--border);
}

/* ── Scanner viewfinder ── */
.scanner-frame {
  background: #0a0f1e;
  border-radius: var(--radius-sm);
  height: 200px;
  position: relative;
  overflow: hidden;
  margin-bottom: 10px;
}
/* video fills the frame */
.scanner-frame video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.scanner-corner {
  position: absolute;
  width: 20px; height: 20px;
  border-color: var(--teal);
  border-style: solid;
  z-index: 2;
}
.scanner-corner.tl { top: 10px; left: 10px; border-width: 2px 0 0 2px; border-radius: 3px 0 0 0; }
.scanner-corner.tr { top: 10px; right: 10px; border-width: 2px 2px 0 0; border-radius: 0 3px 0 0; }
.scanner-corner.bl { bottom: 10px; left: 10px; border-width: 0 0 2px 2px; border-radius: 0 0 0 3px; }
.scanner-corner.br { bottom: 10px; right: 10px; border-width: 0 2px 2px 0; border-radius: 0 0 3px 0; }
.scanner-line {
  position: absolute;
  width: 80%;
  height: 2px;
  background: var(--teal);
  animation: scan 2s ease-in-out infinite;
}
@keyframes scan {
  0%, 100% { top: 20%; }
  50% { top: 75%; }
}
.scanner-hint { color: rgba(255,255,255,.45); font-size: 11px; }

/* ── Meal day card ── */
.meal-day {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 13px 14px;
  margin-bottom: 8px;
}
.meal-day-title { font-size: 13px; font-weight: 500; margin-bottom: 10px; }
.meal-row {
  display: flex;
  gap: 8px;
  margin-bottom: 5px;
  align-items: baseline;
}
.meal-lbl {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-hint);
  text-transform: uppercase;
  letter-spacing: .05em;
  width: 54px;
  flex-shrink: 0;
}
.meal-txt { font-size: 12px; color: var(--text-primary); line-height: 1.4; }
.meal-footer {
  font-size: 10px;
  color: var(--text-hint);
  margin-top: 8px;
  padding-top: 6px;
  border-top: 0.5px solid var(--border);
}

/* ── Help screen ── */
.help-step-card {
  background: var(--white);
  border: 0.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 10px;
}
.help-step-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.help-step-num {
  width: 24px; height: 24px;
  background: var(--navy);
  color: white;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 500;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.help-step-title { font-size: 13px; font-weight: 500; }
.help-step-body p {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 6px;
}
.help-tip {
  background: var(--teal-lt);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--teal-dk);
  line-height: 1.5;
}

/* ── Success state ── */
.success-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 40px 20px;
}
.success-icon {
  width: 52px; height: 52px;
  background: var(--teal);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}
.success-title { font-size: 16px; font-weight: 500; }
.success-sub { font-size: 13px; color: var(--text-secondary); text-align: center; }

/* ── Utilities ── */
.divider { height: 0.5px; background: var(--border); margin: 12px 0; }
.section-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .07em;
  margin: 14px 0 8px;
}
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-8 { gap: 8px; }
.mt-8 { margin-top: 8px; }
.mt-14 { margin-top: 14px; }
.pb-safe { padding-bottom: env(safe-area-inset-bottom); }
.text-muted { color: var(--text-secondary); }
.text-small { font-size: 12px; }
