:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #f4f5f7);
  --card: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #16181d);
  --muted: var(--tg-theme-hint-color, #70757f);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e5484d;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

main {
  width: min(100%, 620px);
  min-height: 100vh;
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
}

header, .dialog-heading, .inline-field, .entry-top, .macros {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

header { flex-wrap: wrap; }

.date-navigation {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-left: auto;
}

h1, h2, p { margin: 0; }
h1 { font-size: 30px; letter-spacing: -0.04em; }
h2 { font-size: 22px; }

.profile-button {
  min-height: 32px;
  padding: 3px 8px;
  background: var(--card);
  color: var(--muted);
  font-size: 13px;
}

input, button {
  min-height: 44px;
  border-radius: 12px;
  font: inherit;
}

input {
  width: 100%;
  border: 1px solid color-mix(in srgb, var(--muted) 28%, transparent);
  background: var(--card);
  color: var(--text);
  padding: 10px 12px;
}

header input { width: 145px; }

.date-arrow {
  min-width: 38px;
  padding: 0;
  background: var(--card);
  color: var(--text);
  font-size: 28px;
  line-height: 1;
}

.date-arrow[hidden] { display: none; }

button {
  border: 0;
  cursor: pointer;
  font-weight: 650;
}

.summary {
  margin: 20px 0 14px;
  padding: 18px;
  border-radius: 18px;
  background: var(--card);
}

.progress-row { display: grid; gap: 6px; }
.progress-label { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--muted); }
.progress-label strong { color: var(--text); }
.calories-progress .progress-label { font-size: 16px; }
.macro-progress-grid { display: grid; gap: 12px; margin-top: 16px; }
.progress-track { height: 7px; overflow: hidden; border-radius: 999px; background: color-mix(in srgb, var(--muted) 18%, transparent); }
.progress-fill { width: 0; height: 100%; border-radius: inherit; background: var(--accent); transition: width 180ms ease; }
.progress-fill.protein { background: #5b8def; }
.progress-fill.fat { background: #f59e42; }
.progress-fill.carbs { background: #55b987; }
.progress-fill.over { background: var(--danger); }

.meal-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 14px;
}

.meal-tabs button { background: var(--card); color: var(--muted); font-size: 12px; }
.meal-tabs button.active { background: var(--accent); color: var(--accent-text); }

.entries { display: grid; gap: 8px; }

.entry, .empty {
  padding: 14px;
  border-radius: 15px;
  background: var(--card);
}

.entry h3 { margin: 0; font-size: 16px; }
.entry p, .empty { color: var(--muted); }
.delete { min-height: 32px; padding: 2px 8px; background: transparent; color: var(--danger); }

.primary, .secondary { width: 100%; padding: 11px 14px; }
.primary { background: var(--accent); color: var(--accent-text); }
.secondary { background: var(--card); color: var(--accent); border: 1px solid var(--accent); }
.compact { width: auto; }

.add-button {
  position: fixed;
  bottom: calc(16px + env(safe-area-inset-bottom));
  left: 50%;
  width: min(calc(100% - 32px), 588px);
  transform: translateX(-50%);
}

dialog {
  width: min(calc(100% - 24px), 520px);
  max-height: calc(100vh - 24px);
  border: 0;
  border-radius: 20px;
  background: var(--bg);
  color: var(--text);
  padding: 18px;
  overflow-y: auto;
}

dialog::backdrop { background: rgb(0 0 0 / 55%); }
form, fieldset { display: grid; gap: 12px; }
label { display: grid; gap: 5px; color: var(--muted); font-size: 13px; }
fieldset { margin: 0; border: 0; padding: 0; }
.nutrient-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.icon-button { min-height: 36px; min-width: 36px; background: transparent; color: var(--text); font-size: 26px; }
.status { min-height: 20px; color: var(--muted); }
.calorie-formula { color: var(--muted); font-size: 13px; }
#reader { overflow: hidden; border-radius: 14px; }

.target-fields legend {
  margin-bottom: 9px;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
}

.weight-section { display: grid; gap: 10px; padding-top: 5px; }
.weight-heading { display: flex; align-items: end; justify-content: space-between; gap: 12px; }
.weight-heading h3 { margin: 0; font-size: 18px; }
.weight-latest { margin-top: 2px; color: var(--text); font-size: 15px; font-weight: 650; }
.weight-hint { color: var(--muted); font-size: 12px; text-align: right; }
.weight-chart-card {
  padding: 12px;
  border-radius: 15px;
  background: var(--card);
}
.weight-chart-card > strong { font-size: 13px; }
.weight-chart { min-height: 150px; margin-top: 5px; color: var(--accent); }
.weight-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-grid { stroke: color-mix(in srgb, var(--muted) 22%, transparent); stroke-width: 1; }
.chart-line { fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.weight-chart circle { fill: var(--card); stroke: currentColor; stroke-width: 3; }
.weight-chart text { fill: var(--muted); font-size: 10px; font-weight: 500; }
.weight-chart .chart-label, .weight-chart .chart-value { text-anchor: middle; }
.weight-chart .chart-value { fill: var(--text); font-size: 10px; font-weight: 700; }
.weight-empty { display: grid; min-height: 135px; place-items: center; color: var(--muted); font-size: 13px; text-align: center; }

.saved-products-section { display: grid; gap: 8px; }
.saved-heading { display: grid; grid-template-columns: auto minmax(130px, 1fr); align-items: center; gap: 10px; }
.saved-heading input { min-height: 38px; padding: 7px 10px; }
.saved-products { display: flex; gap: 7px; overflow-x: auto; padding: 1px 0 6px; scrollbar-width: none; }
.saved-products::-webkit-scrollbar { display: none; }
.saved-product {
  flex: 0 0 auto;
  max-width: 210px;
  min-height: 58px;
  padding: 9px 11px;
  border: 1px solid color-mix(in srgb, var(--muted) 24%, transparent);
  background: var(--card);
  color: var(--text);
  text-align: left;
}
.saved-product strong, .saved-product small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved-product small { margin-top: 3px; color: var(--muted); font-weight: 400; }
.saved-empty { color: var(--muted); font-size: 13px; }

@media (prefers-color-scheme: dark) {
  :root { --bg: var(--tg-theme-bg-color, #17191d); --card: var(--tg-theme-secondary-bg-color, #24272e); --text: var(--tg-theme-text-color, #f2f3f5); }
}
