:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1a2027;
  --muted: #6b7480;
  --border: #e3e7ec;
  --primary: #16a34a;
  --primary-dark: #128040;
  --primary-soft: #e7f6ec;
  --protein: #6366f1;
  --carbs: #f59e0b;
  --fat: #ec4899;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --radius: 16px;
  --radius-sm: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #1a2027;
    --surface-2: #232b34;
    --text: #e6eaef;
    --muted: #9aa4b0;
    --border: #2c353f;
    --primary-soft: #16341f;
    --shadow: 0 1px 3px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .3);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px calc(48px + env(safe-area-inset-bottom));
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 0 12px;
  position: sticky;
  top: 0;
  background: linear-gradient(var(--bg) 70%, transparent);
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { font-size: 32px; line-height: 1; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: -.02em; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); }
.header-actions { display: flex; gap: 8px; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  cursor: pointer;
  transition: background .15s, transform .05s, border-color .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-block { width: 100%; display: block; }
.btn-block + .btn-block { margin-top: 10px; }

/* Date nav */
.date-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  position: relative;
}
.date-arrow {
  font-size: 24px;
  line-height: 1;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.date-arrow:hover { background: var(--surface-2); }
.date-current {
  flex: 1;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  cursor: pointer;
  color: var(--text);
  font: inherit;
}
.date-current #dateLabel { font-weight: 700; font-size: 16px; display: block; }
.date-sub { font-size: 12px; color: var(--muted); }
.date-hidden-input {
  position: absolute; opacity: 0; pointer-events: none; width: 0; height: 0;
}

/* Card */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Summary */
.summary {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px;
  margin-bottom: 20px;
}
.calorie-ring-wrap { position: relative; flex-shrink: 0; width: 120px; height: 120px; }
.calorie-ring { transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 12; }
.ring-fill {
  fill: none;
  stroke: var(--primary);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-dasharray: 326.7;
  stroke-dashoffset: 326.7;
  transition: stroke-dashoffset .5s ease, stroke .3s;
}
.calorie-ring-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.cal-remaining { font-size: 28px; font-weight: 800; letter-spacing: -.03em; }
.cal-label { font-size: 11px; color: var(--muted); }
.macro-bars { flex: 1; min-width: 0; }
.cal-inline { display: flex; justify-content: space-between; margin-bottom: 12px; font-size: 14px; }
.cal-inline strong { font-size: 18px; }
.muted { color: var(--muted); }

.macro-row { margin-bottom: 10px; }
.macro-row:last-child { margin-bottom: 0; }
.macro-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.macro-name { font-weight: 600; }
.macro-val { color: var(--muted); font-variant-numeric: tabular-nums; }
.bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; border-radius: 999px; transition: width .5s ease, background .3s; }
[data-macro="protein"] .bar-fill { background: var(--protein); }
[data-macro="carbs"] .bar-fill { background: var(--carbs); }
[data-macro="fat"] .bar-fill { background: var(--fat); }
.bar-fill.over { background: var(--danger) !important; }

/* Meals */
.meal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  overflow: hidden;
}
.meal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
}
.meal-title { display: flex; align-items: center; gap: 10px; }
.meal-icon { font-size: 20px; }
.meal-title h3 { margin: 0; font-size: 16px; }
.meal-kcal { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.meal-add {
  border: none;
  background: var(--primary-soft);
  color: var(--primary-dark);
  width: 32px; height: 32px;
  border-radius: 50%;
  font-size: 20px;
  cursor: pointer;
  display: grid; place-items: center;
  line-height: 1;
}
.meal-add:hover { background: var(--primary); color: #fff; }
@media (prefers-color-scheme: dark) { .meal-add { color: var(--primary); } .meal-add:hover { color: #fff; } }

.entries { list-style: none; margin: 0; padding: 0; }
.entry {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
}
.entry-main { flex: 1; min-width: 0; }
.entry-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry-meta { font-size: 12px; color: var(--muted); }
.entry-macros { display: flex; gap: 10px; font-size: 11px; margin-top: 2px; }
.entry-macros span::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 3px; vertical-align: middle; }
.entry-macros .m-p::before { background: var(--protein); }
.entry-macros .m-c::before { background: var(--carbs); }
.entry-macros .m-f::before { background: var(--fat); }
.entry-kcal { font-weight: 700; font-size: 14px; font-variant-numeric: tabular-nums; }
.entry-del {
  border: none; background: none; color: var(--muted); cursor: pointer;
  font-size: 18px; padding: 4px; border-radius: 6px; line-height: 1;
}
.entry-del:hover { color: var(--danger); background: var(--surface-2); }
.meal-empty { padding: 14px 16px; color: var(--muted); font-size: 13px; border-top: 1px solid var(--border); font-style: italic; }

/* Footer */
.app-footer { text-align: center; margin-top: 24px; }
.app-footer p { font-size: 12px; color: var(--muted); }
.app-footer a { color: var(--primary); }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(16, 24, 40, .45);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0;
  z-index: 100;
  animation: fade .15s ease;
}
.modal-backdrop[hidden] { display: none; }
@media (min-width: 560px) { .modal-backdrop { align-items: center; padding: 20px; } }
@keyframes fade { from { opacity: 0; } }
.modal {
  background: var(--surface);
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  animation: slideup .2s ease;
  padding-bottom: env(safe-area-inset-bottom);
}
@media (min-width: 560px) { .modal { border-radius: 20px; } }
.modal-sm { max-width: 420px; }
@keyframes slideup { from { transform: translateY(30px); opacity: .5; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px 12px;
  position: sticky; top: 0; background: var(--surface); z-index: 2;
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-close {
  border: none; background: var(--surface-2); color: var(--text);
  width: 32px; height: 32px; border-radius: 50%; cursor: pointer; font-size: 15px;
}
.modal-close:hover { background: var(--border); }

/* Tabs */
.tabs { display: flex; gap: 4px; padding: 0 20px; border-bottom: 1px solid var(--border); }
.tab {
  border: none; background: none; color: var(--muted);
  padding: 10px 12px; font: inherit; font-weight: 600; font-size: 14px;
  cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab.is-active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-panel { display: none; padding: 16px 20px 20px; }
.tab-panel.is-active { display: block; }

/* Search */
.search-bar { display: flex; gap: 8px; }
.search-bar input[type="search"] {
  flex: 1; min-width: 0;
  padding: 11px 14px; font: inherit;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.search-bar input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.hint { font-size: 12.5px; color: var(--muted); margin: 10px 0; }

.results { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.result-item {
  display: flex; align-items: center; gap: 12px;
  padding: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); cursor: pointer; text-align: left; width: 100%; font: inherit; color: var(--text);
  transition: border-color .15s, background .15s;
}
.result-item:hover { border-color: var(--primary); background: var(--primary-soft); }
.result-thumb {
  width: 44px; height: 44px; border-radius: 8px; object-fit: cover;
  background: var(--surface-2); flex-shrink: 0;
}
.result-thumb.placeholder { display: grid; place-items: center; font-size: 20px; }
.result-info { flex: 1; min-width: 0; }
.result-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-brand { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.result-kcal { font-size: 12px; color: var(--muted); }
.result-kcal strong { color: var(--text); }
.result-del { color: var(--muted); font-size: 16px; padding: 4px 8px; border: none; background: none; cursor: pointer; border-radius: 6px; }
.result-del:hover { color: var(--danger); background: var(--surface-2); }

.state-msg { text-align: center; padding: 24px 12px; color: var(--muted); font-size: 14px; }
.spinner {
  width: 28px; height: 28px; margin: 0 auto 10px;
  border: 3px solid var(--surface-2); border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Forms */
.manual-form, .data-body, .portion-body, .scan-body { padding: 4px 20px 20px; }
.data-body { padding-top: 12px; }
label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 12px; }
label input, label select {
  display: block; width: 100%; margin-top: 5px;
  padding: 10px 12px; font: inherit; font-weight: 400;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
label input:focus, label select:focus { outline: 2px solid var(--primary); border-color: transparent; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.file-label { text-align: center; cursor: pointer; }

/* Portion */
.portion-food { margin-bottom: 14px; }
.portion-food strong { display: block; font-size: 16px; }
.portion-input-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.portion-preview {
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 12px 14px; margin: 6px 0 16px; font-size: 14px;
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px;
}
.portion-preview .pv-cal { font-weight: 800; font-size: 18px; }
.portion-preview .pv-macros { color: var(--muted); font-size: 13px; align-self: center; }

/* Scan */
.scan-body { text-align: center; }
#scanVideo { width: 100%; border-radius: var(--radius-sm); background: #000; max-height: 50vh; }

/* Toast */
.toast {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 11px 18px; border-radius: 999px; font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow); z-index: 200;
  animation: toastin .2s ease;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ============ v2 features ============ */

/* Icon-only header buttons */
.btn-icon { padding: 9px 11px; font-size: 16px; line-height: 1; }
.header-actions { flex-wrap: wrap; justify-content: flex-end; }

/* Water card */
.water { padding: 14px 16px; margin-bottom: 16px; }
.water-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.water-title { font-weight: 700; font-size: 15px; }
.water-count { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.water-count strong { color: var(--text); font-size: 15px; }
.water-glasses { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; min-height: 30px; }
.glass {
  width: 30px; height: 34px; border-radius: 5px 5px 8px 8px;
  border: 2px solid #7cc4f0; background: transparent; cursor: pointer;
  display: grid; place-items: center; font-size: 14px; padding: 0; line-height: 1;
  transition: background .15s, transform .05s;
}
.glass.filled { background: linear-gradient(#bfe3fb, #7cc4f0); border-color: #4aa8e8; }
.glass:active { transform: scale(.9); }
.water-actions { display: flex; gap: 8px; }
.water-btn { flex: 1; }
.water-btn.btn-ghost { max-width: 64px; font-size: 20px; }

/* Day actions */
.day-actions { display: flex; justify-content: flex-end; margin-bottom: 10px; }
.link-btn {
  border: none; background: none; color: var(--primary); font: inherit; font-weight: 600;
  font-size: 13px; cursor: pointer; padding: 4px 6px; border-radius: 6px;
}
.link-btn:hover { background: var(--primary-soft); }

/* Entry — make main area a button, keep favorite/delete distinct */
.entry-main {
  flex: 1; min-width: 0; text-align: left; background: none; border: none;
  color: var(--text); font: inherit; cursor: pointer; padding: 0; display: block;
}
.entry-main:hover .entry-name { color: var(--primary); }
.entry-name { display: block; }
.entry-meta { display: block; }
.entry-fav {
  border: none; background: none; cursor: pointer; font-size: 16px; padding: 4px;
  border-radius: 6px; line-height: 1; filter: grayscale(1); opacity: .6;
}
.entry-fav.on { filter: none; opacity: 1; }
.entry-fav:hover { background: var(--surface-2); }

/* Result actions (star / remove) */
.result-actions { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.result-star {
  border: none; background: none; cursor: pointer; font-size: 16px; padding: 6px;
  border-radius: 6px; line-height: 1; filter: grayscale(1); opacity: .6;
}
.result-star.on { filter: none; opacity: 1; }
.result-star:hover { background: var(--surface-2); }

/* Section titles inside modals */
.section-title {
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 18px 0 10px; font-weight: 700;
}
.save-goals { margin-top: 18px; }

/* Meals editor */
.meals-editor { display: flex; flex-direction: column; gap: 8px; }
.meal-row { display: flex; gap: 8px; align-items: center; }
.meal-icon-input {
  width: 46px; text-align: center; padding: 9px 4px; font-size: 16px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.meal-name-input {
  flex: 1; min-width: 0; padding: 9px 12px; font: inherit;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.meal-icon-input:focus, .meal-name-input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.meal-del {
  border: none; background: none; color: var(--muted); cursor: pointer; font-size: 17px;
  padding: 6px; border-radius: 6px; line-height: 1;
}
.meal-del:hover { color: var(--danger); background: var(--surface-2); }

/* Trends: range toggle + charts */
.range-toggle { display: flex; gap: 6px; padding: 16px 20px 4px; }
.range-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  padding: 6px 14px; border-radius: 999px; font: inherit; font-weight: 600; font-size: 13px; cursor: pointer;
}
.range-btn.is-active { background: var(--primary); color: #fff; border-color: var(--primary); }
.chart { padding: 8px 16px 4px; }
.chart-title { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.chart-svg { width: 100%; height: auto; display: block; }
.chart-label { font-size: 9px; fill: var(--muted); }

.trend-averages { padding: 4px 20px 20px; }
.avg-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.avg-cell {
  background: var(--surface-2); border-radius: var(--radius-sm); padding: 10px 6px; text-align: center;
}
.avg-num { display: block; font-size: 16px; font-weight: 800; letter-spacing: -.02em; }
.avg-cell[data-macro="protein"] .avg-num { color: var(--protein); }
.avg-cell[data-macro="carbs"] .avg-num { color: var(--carbs); }
.avg-cell[data-macro="fat"] .avg-num { color: var(--fat); }
.avg-lbl { display: block; font-size: 10px; color: var(--muted); margin-top: 2px; }

/* Weight */
.weight-form { padding: 16px 20px 4px; }
.weight-input-row { display: flex; gap: 8px; align-items: center; margin-top: 6px; }
.weight-input-row input {
  flex: 1; min-width: 0; padding: 10px 12px; font: inherit;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text);
}
.weight-input-row input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.weight-unit { font-weight: 700; color: var(--muted); }
.weight-list { padding: 4px 20px 20px; display: flex; flex-direction: column; }
.weight-row {
  display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center;
  padding: 9px 0; border-top: 1px solid var(--border); font-size: 14px;
}
.weight-del { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 15px; padding: 4px; border-radius: 6px; }
.weight-del:hover { color: var(--danger); background: var(--surface-2); }
