:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --section-bg: var(--tg-theme-section-bg-color, #ffffff);
  --section-header: var(--tg-theme-section-header-text-color, #6d6d72);
  --destructive: var(--tg-theme-destructive-text-color, #ff3b30);

  --radius: 12px;
  --radius-sm: 8px;
  --gap: 12px;
}

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

html, body {
  height: 100%;
  background: var(--secondary-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior: none;
}

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ── Page ── */
.page {
  flex: 1;
  padding: var(--gap);
  padding-bottom: calc(var(--gap) + env(safe-area-inset-bottom));
  display: none;
  flex-direction: column;
  gap: var(--gap);
}
.page.active {
  display: flex;
}

/* ── Section card ── */
.card {
  background: var(--section-bg);
  border-radius: var(--radius);
  overflow: hidden;
}

.section-header {
  font-size: 13px;
  font-weight: 600;
  color: var(--section-header);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0 4px 6px;
}

/* ── Macro tiles (2×2 grid) ── */
.macro-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

.macro-tile {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.macro-tile .label {
  font-size: 13px;
  color: var(--hint);
}

.macro-tile .value {
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}

.macro-tile .unit {
  font-size: 13px;
  color: var(--hint);
}

.tile-kcal .value  { color: #FF6B35; }
.tile-prot .value  { color: #4CAF50; }
.tile-fat  .value  { color: #FF9800; }
.tile-carb .value  { color: #2196F3; }

/* ── Meal list ── */
.meal-list {
  display: flex;
  flex-direction: column;
}

.meal-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 12px;
  border-bottom: 1px solid var(--secondary-bg);
}
.meal-item:last-child {
  border-bottom: none;
}

.meal-info {
  flex: 1;
  min-width: 0;
}

.meal-name {
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meal-meta {
  font-size: 13px;
  color: var(--hint);
  margin-top: 2px;
}

.meal-kcal {
  font-size: 15px;
  font-weight: 600;
  color: #FF6B35;
  white-space: nowrap;
}

.meal-actions {
  display: flex;
  gap: 8px;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
  border-radius: 6px;
  line-height: 1;
  opacity: 0.7;
  transition: opacity 0.15s;
}
.btn-icon:active { opacity: 0.4; }

.meal-empty {
  padding: 24px 16px;
  text-align: center;
  color: var(--hint);
  font-size: 15px;
}

/* ── FAB ── */
.fab {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  right: 20px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--button);
  color: var(--button-text);
  font-size: 28px;
  line-height: 1;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  transition: transform 0.15s;
}
.fab:active { transform: scale(0.92); }

/* ── Nav bottom tabs ── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--section-bg);
  border-top: 1px solid var(--secondary-bg);
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 200;
}

.nav-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 4px;
  gap: 3px;
  cursor: pointer;
  border: none;
  background: none;
  color: var(--hint);
  font-size: 10px;
  font-weight: 500;
  transition: color 0.15s;
  text-decoration: none;
}
.nav-tab .icon { font-size: 22px; line-height: 1; }
.nav-tab.active { color: var(--button); }

/* Pages need bottom padding for nav + FAB */
.has-nav {
  padding-bottom: calc(64px + var(--gap) + env(safe-area-inset-bottom));
}

/* ── Loading / Spinner ── */
.spinner {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 32px;
}
.spinner::after {
  content: "";
  width: 28px;
  height: 28px;
  border: 3px solid var(--secondary-bg);
  border-top-color: var(--button);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Add page ── */
.tabs {
  display: flex;
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 4px;
  gap: 4px;
}
.tab-btn {
  flex: 1;
  border: none;
  background: none;
  padding: 9px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 500;
  color: var(--hint);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab-btn.active {
  background: var(--button);
  color: var(--button-text);
}

textarea, input[type="text"], input[type="number"] {
  width: 100%;
  background: var(--section-bg);
  border: 1px solid var(--secondary-bg);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  padding: 12px 14px;
  resize: none;
  font-family: inherit;
  outline: none;
}
textarea:focus, input:focus {
  border-color: var(--button);
}

.btn-primary {
  width: 100%;
  background: var(--button);
  color: var(--button-text);
  border: none;
  border-radius: var(--radius);
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-primary:disabled { opacity: 0.5; }
.btn-primary:active:not(:disabled) { opacity: 0.8; }

.btn-secondary {
  width: 100%;
  background: var(--secondary-bg);
  color: var(--text);
  border: none;
  border-radius: var(--radius);
  padding: 14px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-secondary:active { opacity: 0.7; }

/* ── Confirm / Edit form ── */
.form-group {
  padding: 12px 16px;
  border-bottom: 1px solid var(--secondary-bg);
}
.form-group:last-child { border-bottom: none; }

.form-label {
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 4px;
}

.form-input {
  background: none;
  border: none;
  padding: 0;
  font-size: 16px;
  color: var(--text);
  width: 100%;
  outline: none;
  font-family: inherit;
}

/* ── History page ── */
.period-toggle {
  display: flex;
  gap: var(--gap);
  margin-bottom: 4px;
}
.period-btn {
  flex: 1;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--section-bg);
  color: var(--hint);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.period-btn.active {
  background: var(--button);
  color: var(--button-text);
}

.chart-wrap {
  padding: 16px;
  position: relative;
  height: 200px;
}

/* ── Utility ── */
.hidden { display: none !important; }
.text-hint { color: var(--hint); font-size: 13px; }
.text-center { text-align: center; }
