/*
 * Стили Mini App (дневник КБЖУ).
 *
 * Фон и текст берутся из темы Telegram (--tg-theme-*), запасные значения нужны в обычном браузере.
 * Один «фирменный» элемент: табло как у кухонных весов (.lcd и поле веса). Всё остальное спокойное.
 */

:root {
  /* табло весов: одинаковое в светлой и тёмной теме */
  --lcd: #cbd6bf;
  --lcd-ink: #18221a;
  --lcd-dim: rgba(24, 34, 26, 0.62);

  /* белки, жиры, углеводы: только для полосок, не для текста */
  --p: #3f7ae0;
  --f: #d9951a;
  --c: #159a8a;
  --over: #cf3a41;

  --fb-bg: #eef0ee;
  --fb-card: #ffffff;
  --fb-text: #141a16;
  --fb-hint: #646d66;
  --line: rgba(20, 26, 22, 0.1);
  --fill: rgba(20, 26, 22, 0.06);
  --btn: #18221a;
  --btn-text: #e3ecd9;

  --bg: var(--tg-theme-secondary-bg-color, var(--fb-bg));
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--fb-card)));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));

  --radius: 14px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Roboto Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --kb: 0px; /* высота экранной клавиатуры, выставляется из app.js */
}

[data-scheme="dark"] {
  --p: #6b9bf0;
  --f: #e6a93a;
  --c: #35b8a6;
  --over: #ff6b70;
  --fb-bg: #0f1311;
  --fb-card: #191f1b;
  --fb-text: #e7ece7;
  --fb-hint: #8f9891;
  --line: rgba(255, 255, 255, 0.09);
  --fill: rgba(255, 255, 255, 0.08);
  --btn: #cbd6bf;
  --btn-text: #18221a;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 var(--sans);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3, p, ul { margin: 0; padding: 0; }
ul { list-style: none; }

:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }

.num { font-family: var(--mono); font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.hint { font-size: 12.5px; color: var(--hint); }
.grow { flex: 1; }

/* ---------- каркас ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--tg-viewport-stable-height, 100dvh);
  max-width: 460px;
  margin: 0 auto;
  background: var(--bg);
}
.view { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 6px 12px 20px; }

/* Свайп по дням: новый день въезжает с той стороны, куда листали */
.view.slide-next { animation: slide-next 0.22s ease-out; }
.view.slide-prev { animation: slide-prev 0.22s ease-out; }
@keyframes slide-next { from { transform: translateX(24px); opacity: 0.4; } to { transform: none; opacity: 1; } }
@keyframes slide-prev { from { transform: translateX(-24px); opacity: 0.4; } to { transform: none; opacity: 1; } }

@media (min-width: 520px) {
  body { background: color-mix(in srgb, var(--bg) 88%, #000); }
  .app { box-shadow: 0 0 0 1px var(--line); }
}

/* ---------- кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--fill);
  color: var(--text);
  font-weight: 600;
  font-size: 15px;
}
.btn:active { opacity: 0.75; }
.btn.primary { background: var(--btn); color: var(--btn-text); }
.btn.danger { color: var(--over); }
.btn.block { display: flex; width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  border: 0;
  border-radius: 12px;
  background: none;
  font-size: 24px;
  line-height: 1;
}
.icon-btn:active { background: var(--fill); }
.icon-btn.sm { width: 40px; height: 40px; font-size: 21px; }

.link {
  display: inline-block;
  padding: 8px 0;
  border: 0;
  background: none;
  font-weight: 600;
  font-size: 14px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 36px;
  padding: 0 12px;
  border: 0;
  border-radius: 18px;
  background: var(--fill);
  font-size: 14px;
  white-space: nowrap;
}
.chip.is-on { background: var(--btn); color: var(--btn-text); font-weight: 600; }

/* ---------- шапка дня ---------- */

.datebar { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; padding: 2px 0 6px; }
.datebar-title { text-align: center; line-height: 1.2; }
.datebar-title strong { display: block; font-size: 17px; font-weight: 650; }
.datebar-title > span { font-size: 12.5px; color: var(--hint); }
.badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--fill);
  color: var(--hint);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  vertical-align: 2px;
}

.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; padding: 2px; margin-bottom: 10px; border-radius: 12px; background: var(--fill); }
.seg-btn { height: 34px; border: 0; border-radius: 10px; background: none; color: var(--hint); font-size: 14px; font-weight: 500; }
.seg-btn.is-on { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); }

/* ---------- съедено за день ---------- */

.gauges { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; padding: 12px; border-radius: var(--radius); background: var(--card); }
.gauge { display: grid; gap: 3px; min-width: 0; }
.gauge-label { font-size: 12px; color: var(--hint); }
.gauge-val { font-size: 17px; font-weight: 600; }
.gauge-bar { height: 4px; border-radius: 2px; background: var(--fill); overflow: hidden; }
.gauge-bar i { display: block; height: 100%; border-radius: 2px; background: var(--text); }
.g-p .gauge-bar i { background: var(--p); }
.g-f .gauge-bar i { background: var(--f); }
.g-c .gauge-bar i { background: var(--c); }
.gauge-goal { font-size: 11px; color: var(--hint); white-space: nowrap; }
.gauge.is-over .gauge-bar i { background: var(--over); }
.gauge.is-over .gauge-goal { color: var(--over); font-weight: 600; }

/* ---------- приёмы пищи и строки ---------- */

.meal { margin-top: 12px; }
.meal-head { display: flex; align-items: center; gap: 2px; min-height: 40px; padding-left: 4px; }
.meal-head h2 { flex: 1; font-size: 15px; font-weight: 650; }
.meal-sum { margin-right: 4px; font-size: 12.5px; color: var(--hint); }

.rows { border-radius: var(--radius); background: var(--card); overflow: hidden; }
.rows > li + li { border-top: 1px solid var(--line); }
.row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 54px;
  padding: 9px 12px;
  border: 0;
  background: none;
  text-align: left;
}
button.row:active { background: var(--fill); }
.row-main { flex: 1; min-width: 0; display: grid; gap: 1px; }
.row-name, .row-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-name { font-size: 15px; }
.row-sub { font-size: 12.5px; color: var(--hint); }
.row-sub.num { font-size: 11.5px; }
.row-nums { flex: none; display: grid; gap: 1px; text-align: right; }
.row-kcal { font-size: 15px; font-weight: 600; }
.row-bju { font-size: 11px; color: var(--hint); }

.row-split { display: flex; align-items: stretch; }
.row-split .row { flex: 1; min-width: 0; }
.quick {
  flex: none;
  min-width: 68px;
  margin: 8px 8px 8px 0;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: var(--card);
  font: 600 12.5px var(--mono);
  white-space: nowrap;
}
.quick:active { opacity: 0.7; }

.empty { margin-top: 12px; padding: 16px 14px; border: 1px dashed var(--line); border-radius: var(--radius); display: grid; gap: 10px; }
.empty-title { font-weight: 600; }

/* ---------- табло «Осталось» ---------- */

.lcd-wrap { display: flex; gap: 8px; padding: 8px 12px; }
.lcd {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-template-rows: auto auto;
  column-gap: 10px;
  align-items: end;
  padding: 7px 10px 8px 12px;
  border: 0;
  border-radius: 12px;
  background: var(--lcd) linear-gradient(rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0) 45%);
  color: var(--lcd-ink);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  text-align: left;
  box-shadow: inset 0 0 0 1px rgba(24, 34, 26, 0.18), inset 0 -3px 8px rgba(24, 34, 26, 0.1);
}
.lcd:active { filter: brightness(0.96); }
.lcd-cap { grid-column: 1; font-size: 10.5px; font-weight: 600; letter-spacing: 0.14em; text-transform: uppercase; color: var(--lcd-dim); }
.lcd-main { grid-column: 1; grid-row: 2; font-size: 28px; font-weight: 600; line-height: 1; letter-spacing: -0.02em; white-space: nowrap; }
.lcd-main i { margin-left: 6px; font-style: normal; font-size: 12px; font-weight: 500; letter-spacing: 0; color: var(--lcd-dim); }
.lcd-bju { grid-column: 2; grid-row: 1 / span 2; align-self: center; display: grid; gap: 1px; justify-items: end; font-size: 12.5px; line-height: 1.25; color: var(--lcd-dim); }
.lcd-bju b { display: inline-block; min-width: 5ch; text-align: right; font-weight: 600; color: var(--lcd-ink); }
.lcd-more { grid-column: 3; grid-row: 1 / span 2; align-self: center; font-size: 20px; color: var(--lcd-dim); }
.lcd .inv { padding: 0 4px; border-radius: 4px; background: var(--lcd-ink); color: var(--lcd); }
.lcd:focus-visible, .lcd-add:focus-visible { outline-offset: 1px; }

.lcd-add {
  flex: none;
  width: 60px;
  border: 0;
  border-radius: 12px;
  background: var(--btn);
  color: var(--btn-text);
  font-size: 30px;
  font-weight: 300;
  line-height: 1;
}
.lcd-add:active { opacity: 0.8; }

/* ---------- нижние вкладки ---------- */

.nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--card);
}
.nav-btn { display: grid; justify-items: center; gap: 2px; padding: 6px 0 4px; border: 0; border-radius: 10px; background: none; color: var(--hint); font-size: 11px; }
.nav-btn.is-on { color: var(--text); font-weight: 600; }
.nav-btn svg { width: 22px; height: 22px; }

/* ---------- прочие экраны ---------- */

.page-head { display: flex; align-items: center; justify-content: space-between; min-height: 52px; padding: 6px 4px; }
.page-head h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; }
.list-cap { margin: 0 4px 6px; }

.card { padding: 14px; border-radius: var(--radius); background: var(--card); }
.card + .card { margin-top: 12px; }
.card h2 { font-size: 15px; font-weight: 650; margin-bottom: 10px; }
.card .hint { margin-bottom: 12px; }
#goal-check { min-height: 17px; }

.tpl { padding: 14px; border-radius: var(--radius); background: var(--card); display: grid; gap: 6px; }
.tpl + .tpl { margin-top: 10px; }
.tpl-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.tpl-head h2 { font-size: 16px; font-weight: 650; }
.tpl-head .num { font-weight: 600; white-space: nowrap; }
.tpl-items { font-size: 14px; }
.tpl-actions { display: flex; gap: 8px; margin-top: 6px; }

/* ---------- поля ---------- */

.field { display: grid; gap: 4px; margin-bottom: 10px; }
.field-label { font-size: 12.5px; color: var(--hint); }
.group-label { margin-bottom: 4px; }
.field input {
  width: 100%;
  min-width: 0;
  height: 46px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--fill);
  font-size: 16px; /* меньше 16px iOS увеличивает экран при фокусе */
  outline: none;
}
.field input:focus { border-color: var(--text); }
.field input::placeholder { color: var(--hint); opacity: 1; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.error { margin-bottom: 10px; font-size: 13.5px; color: var(--over); }
.notice { margin: 10px 0; padding: 10px 12px; border-radius: 12px; background: var(--fill); font-size: 13.5px; }
.notice .link { padding: 0; font-size: inherit; }

/* ---------- шторка ---------- */

.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.42); opacity: 0; transition: opacity 0.2s; }
.sheet {
  position: fixed;
  left: 50%;
  bottom: var(--kb);
  z-index: 50;
  width: min(100%, 460px);
  max-height: calc(90dvh - var(--kb));
  overflow-y: auto;
  padding: 6px 14px calc(16px + env(safe-area-inset-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--card);
  transform: translate(-50%, 100%);
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
#sheet-root.is-open .backdrop { opacity: 1; }
#sheet-root.is-open .sheet { transform: translate(-50%, 0); }
.sheet-grip { width: 36px; height: 4px; margin: 0 auto 2px; border-radius: 2px; background: var(--line); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 48px; }
.sheet-head h2 { font-size: 18px; font-weight: 700; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-head .icon-btn { margin-right: -8px; }
.sheet .rows { background: var(--bg); }
.sheet .hint { margin: 6px 2px; }
.sheet-actions { display: flex; gap: 8px; margin-top: 16px; }
.sub { margin: 14px 2px 6px; font-size: 12.5px; font-weight: 600; color: var(--hint); }
.add-q { margin: 12px 0 0; }
.new-product { margin-top: 10px; }
.clear-meal { margin-top: 6px; }
.pick-name { margin-top: 2px; font-size: 17px; font-weight: 650; }

/* поле веса: то же табло, что и «Осталось» */
.grams { display: grid; grid-template-columns: 54px 1fr 54px; gap: 8px; margin: 12px 0 10px; }
.step { height: 54px; border: 0; border-radius: 12px; background: var(--fill); font-size: 26px; line-height: 1; }
.step:active { opacity: 0.7; }
.grams-input {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  height: 54px;
  padding-top: 11px;
  border-radius: 12px;
  background: var(--lcd) linear-gradient(rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0) 45%);
  color: var(--lcd-ink);
  font-family: var(--mono);
  box-shadow: inset 0 0 0 1px rgba(24, 34, 26, 0.18), inset 0 -3px 8px rgba(24, 34, 26, 0.1);
}
.grams-input:focus-within { box-shadow: inset 0 0 0 2px var(--lcd-ink); }
.grams-input input {
  width: 5.5ch;
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: 600 28px/1 var(--mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
  outline: none;
}
.grams-input > span:last-child { font-size: 13px; color: var(--lcd-dim); }
.preview { min-height: 18px; margin: 10px 0 14px; text-align: center; font-size: 13px; color: var(--hint); }

/* ---------- всплывающая строка ---------- */

#toast-root {
  position: fixed;
  left: 50%;
  bottom: calc(76px + env(safe-area-inset-bottom));
  z-index: 60;
  width: min(100% - 24px, 436px);
  transform: translateX(-50%);
  pointer-events: none;
}
body[data-tab="diary"] #toast-root { bottom: calc(148px + env(safe-area-inset-bottom)); }
.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 10px 10px 14px;
  border-radius: 12px;
  background: #1d2420;
  color: #eef2ec;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
  pointer-events: auto;
}
.toast span { flex: 1; min-width: 0; }
.toast button { flex: none; padding: 6px 6px; border: 0; background: none; color: #cbd6bf; font-weight: 650; }

@media (prefers-reduced-motion: reduce) {
  .sheet, .backdrop { transition: none; }
  .view.slide-next, .view.slide-prev { animation: none; }
}
