:root {
  --bg: #08090b;
  --card: #121317;
  --card-2: #1a1c21;
  --text: #f2f2f0;
  --text-dim: #888d94;
  --border: #26292f;
  --green: #22c55e;
  --green-deep: #16a34a;
  --amber: #f59e0b;
  --red: #ef4444;
  --protein: #ef4444;
  --carbs: #f59e0b;
  --fat: #22c55e;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, "Liberation Mono", monospace;
  overscroll-behavior-y: none;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex-direction: column;
  min-height: 100vh;
  padding-bottom: calc(72px + var(--safe-bottom));
}
.screen.active { display: flex; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 16px 12px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
.date-label { font-size: 17px; font-weight: 700; }
.icon-btn {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  width: 34px; height: 34px;
  border-radius: 8px;
  font-size: 18px;
  display: flex; align-items: center; justify-content: center;
}

.content { padding: 4px 16px 16px; flex: 1; }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  margin-bottom: 16px;
}

.ring-card { display: flex; align-items: center; gap: 18px; }
.ring-wrap { position: relative; width: 140px; height: 140px; flex-shrink: 0; }
.ring { width: 140px; height: 140px; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--card-2); stroke-width: 12; }
.ring-fg {
  fill: none;
  stroke: var(--green);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-dasharray: 389.6;
  stroke-dashoffset: 389.6;
  transition: stroke-dashoffset .4s ease, stroke .4s ease;
}
.ring-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.ring-kcal { font-size: 21px; font-weight: 800; }
.ring-kcal-sep { color: var(--text-dim); margin: 0 1px; }
.ring-kcal #kcalGoalTxt { color: var(--text-dim); font-weight: 600; }
.ring-sub { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.ring-macros { display: flex; flex-direction: column; gap: 10px; min-width: 0; flex: 1; }
.rm-block { display: flex; flex-direction: column; gap: 5px; }
.rm-top { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.rm-bottom { display: flex; align-items: center; gap: 8px; }
.bar-track { flex: 1; height: 6px; background: var(--card-2); border-radius: 4px; overflow: hidden; }
.bar-track.mini { height: 5px; }
.bar-fill { height: 100%; width: 0%; border-radius: 4px; transition: width .4s ease; }
.bar-fill.protein { background: var(--protein); }
.bar-fill.carbs { background: var(--carbs); }
.bar-fill.fat { background: var(--fat); }
.rm-val { font-size: 11px; color: var(--text); font-weight: 700; white-space: nowrap; flex-shrink: 0; }

.section-title { font-size: 13px; font-weight: 700; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; margin: 18px 2px 10px; }

.meal-section { margin-bottom: 6px; }
.meal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 2px 10px;
}
.meal-header-left { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.meal-name { font-size: 13px; font-weight: 700; color: var(--text-dim); text-transform: uppercase; letter-spacing: .04em; }
.meal-kcal { font-size: 12px; color: var(--text-dim); }
.meal-add-btn {
  background: var(--card-2); border: 1px solid var(--border); color: var(--green);
  width: 28px; height: 28px; border-radius: 8px; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
}

.food-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.empty-state { color: var(--text-dim); font-size: 14px; text-align: center; padding: 30px 10px; }
.meal-empty { color: var(--text-dim); font-size: 13px; padding: 4px 2px 18px; }

.meal-pills { display: flex; gap: 8px; margin: 16px 0 4px; }
.meal-pills[hidden] { display: none; }
.meal-pill {
  flex: 1; padding: 10px 4px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--card-2); color: var(--text-dim); font-size: 12px; font-weight: 600;
}
.meal-pill.active { background: var(--green-deep); color: #fff; border-color: var(--green-deep); }

.food-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
}
.food-item-main { flex: 1; min-width: 0; }
.food-item-name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.food-item-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.food-item-kcal { font-size: 15px; font-weight: 700; color: var(--green); flex-shrink: 0; }
.food-item-del {
  background: none; border: none; color: var(--text-dim); font-size: 18px; padding: 4px 6px; flex-shrink: 0;
}

.fab {
  position: fixed;
  right: calc(50% - 240px + 20px);
  bottom: calc(88px + var(--safe-bottom));
  width: 58px; height: 58px;
  border-radius: 14px;
  background: var(--green-deep);
  color: #fff;
  font-size: 30px;
  font-weight: 400;
  border: none;
  box-shadow: 0 6px 18px rgba(22,163,74,.35);
  display: flex; align-items: center; justify-content: center;
  z-index: 20;
}
@media (max-width: 480px) {
  .fab { right: 20px; }
}

.search-row { display: flex; gap: 8px; margin-bottom: 6px; }
.search-input {
  flex: 1;
  width: 100%;
  padding: 14px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 16px;
}
.search-input::placeholder { color: var(--text-dim); }
.scan-btn { width: 48px; height: 48px; font-size: 20px; flex-shrink: 0; }

.scanner-overlay {
  position: fixed; inset: 0; background: #000;
  display: flex; flex-direction: column;
  z-index: 60;
}
.scanner-overlay[hidden] { display: none; }
.scanner-topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-top)) 16px 12px;
  color: var(--text); font-size: 15px; font-weight: 700;
}
.scanner-video-wrap { flex: 1; position: relative; overflow: hidden; background: #000; }
#scannerVideo { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner-frame {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 78%; max-width: 320px; height: 130px;
  border: 3px solid var(--green); border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0,0,0,.5);
  pointer-events: none;
}
.scanner-hint {
  text-align: center; color: var(--text-dim); font-size: 13px;
  padding: 16px 20px calc(24px + var(--safe-bottom));
}


.results-list { display: flex; flex-direction: column; gap: 8px; }
.result-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
}
.result-main { flex: 1; min-width: 0; }
.result-name { font-size: 15px; font-weight: 600; }
.result-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.result-add { background: var(--green-deep); color: #fff; border: none; width: 30px; height: 30px; border-radius: 8px; font-size: 18px; flex-shrink: 0; }

.link-btn {
  background: none; border: none; color: var(--green); font-size: 14px; font-weight: 600; padding: 14px 4px; width: 100%; text-align: center;
}

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.65);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 50;
}
.modal-overlay[hidden] { display: none; }
.modal-sheet {
  background: var(--card);
  border: 1px solid var(--border);
  border-bottom: none;
  width: 100%; max-width: 480px;
  border-radius: 16px 16px 0 0;
  padding: 10px 20px calc(24px + var(--safe-bottom));
}
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 3px; margin: 4px auto 14px; }
.modal-sheet h3 { margin: 0 0 4px; font-size: 18px; }
.modal-hint { color: var(--text-dim); font-size: 12px; margin: 0 0 14px; }

.unit-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; margin: 16px 0 0;
}
.unit-row[hidden] { display: none; }
.unit-label { font-size: 13px; color: var(--text-dim); }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper-btn {
  width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); font-size: 18px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.stepper-input {
  width: 40px; text-align: center; font-size: 16px; font-weight: 700;
  background: none; border: none; color: var(--text);
}

.portion-input-row { display: flex; align-items: center; gap: 10px; margin: 16px 0; }
.portion-input-row input {
  flex: 1; font-size: 22px; font-weight: 700; padding: 12px 14px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--card-2); color: var(--text);
}
.portion-input-row span { color: var(--text-dim); }

.portion-preview { display: flex; gap: 10px; margin-bottom: 20px; }
.pp-item { flex: 1; background: var(--card-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px; text-align: center; }
.pp-item span { display: block; font-size: 17px; font-weight: 700; }
.pp-item small { color: var(--text-dim); font-size: 11px; }

.modal-actions { display: flex; gap: 10px; }
.btn-primary, .btn-secondary, .btn-danger {
  flex: 1; padding: 14px; border-radius: 10px; border: none; font-size: 15px; font-weight: 700;
}
.btn-primary { background: var(--green-deep); color: #fff; }
.btn-secondary { background: var(--card-2); border: 1px solid var(--border); color: var(--text); }
.btn-danger { background: rgba(239,68,68,.12); color: var(--red); width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); margin-bottom: 12px; }
.field:last-child { margin-bottom: 0; }
.field input {
  padding: 12px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--card-2); color: var(--text); font-size: 15px;
}
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 10px; }

.slider-row { margin-bottom: 16px; }
.slider-row:last-of-type { margin-bottom: 8px; }
.slider-head { display: flex; align-items: center; gap: 8px; font-size: 14px; margin-bottom: 8px; }
.slider-head-right { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.grams-input {
  width: 54px; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--card-2); color: var(--text); font-size: 13px; text-align: right;
}
.grams-unit { color: var(--text-dim); font-size: 12px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.protein { background: var(--protein); }
.dot.carbs { background: var(--carbs); }
.dot.fat { background: var(--fat); }
input[type="range"] { width: 100%; accent-color: var(--green); }

.sum-hint { text-align: center; font-size: 12px; color: var(--text-dim); margin-top: 4px; }

.tabbar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  max-width: 480px; margin: 0 auto;
  background: rgba(18,19,23,.94);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: var(--safe-bottom);
  z-index: 30;
}
.tab-btn {
  flex: 1; background: none; border: none; color: var(--text-dim);
  padding: 10px 0 8px; font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tab-btn.active { color: var(--green); }
.tab-ic { font-size: 20px; }

.toast {
  position: fixed; bottom: calc(90px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--card); border: 1px solid var(--border); color: var(--text); padding: 10px 18px; border-radius: 10px; font-size: 13px;
  z-index: 100; box-shadow: 0 4px 16px rgba(0,0,0,.4);
}
.toast[hidden] { display: none; }
