/* Стиль — как у админки: тёмный фон, зелёный акцент, карточки с тонкой рамкой.
   Тема Telegram не подхватывается специально: мини-апп выглядит одинаково у всех (шапку и фон Telegram
   app.js красит в тот же цвет). */
:root {
  --bg: #0b0c10;
  --bg-elevated: #12141b;
  --surface: #1a1d26;
  --surface-hover: #222633;
  --border: #262a36;
  --text: #f3f4f8;
  --muted: #8d93a5;
  --accent: #7cf29a;
  --accent-text: #0b0c10;
  --danger: #ff6b6b;
  --warning: #ffb547;
  --kcal: #7cf29a;
  --protein: #5ab4ff;
  --fat: #ffc857;
  --carbs: #ff7a8a;
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }

main { padding: 0 16px 16px; max-width: 560px; margin: 0 auto; }
h2 { margin: 16px 0 12px; font-size: 21px; font-weight: 650; line-height: 1.25; }
h3 {
  margin: 22px 0 8px; font-size: 12px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--muted);
}
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* --- открыто не из Telegram --- */

body.is-outside { padding-bottom: 0; }
body.is-outside .top, body.is-outside main, body.is-outside .tabbar { display: none !important; }
.outside {
  min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 32px 24px; max-width: 420px; margin: 0 auto; text-align: center;
}
.outside__logo { width: 96px; height: 96px; border-radius: 50%; box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 12%, transparent); }
.outside h1 { margin: 8px 0 0; font-size: 22px; font-weight: 650; }
.outside p { margin: 0 0 12px; }
.outside .btn { width: 100%; text-decoration: none; }
.outside__status { margin: 0; color: var(--accent); }
.seller { margin: 16px 0 0; text-align: center; }
/* Только сайт / только Telegram: элемент сохраняет свой display, прячем лишь в «чужом» режиме. */
body:not(.is-web) .web-only { display: none !important; }
body.is-web .telegram-only { display: none !important; }

/* --- сайт: кто вошёл (шапка) и подвал --- */

.userbar { display: flex; align-items: center; gap: 8px; min-width: 0; margin-left: auto; }
.userbar__name { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
.btn.btn--small { min-height: 30px; padding: 0 10px; border-radius: 9px; font-size: 13px; font-weight: 500; }
.site-footer {
  max-width: 560px; margin: 8px auto 0; padding: 16px 16px 20px; border-top: 1px solid var(--border);
  display: grid; justify-items: center; gap: 6px; text-align: center;
}
.site-footer__link { border: 0; background: none; padding: 0; color: var(--muted); font: inherit; font-size: 13px; text-decoration: underline; cursor: pointer; }
.site-footer .seller { margin: 0; color: var(--muted); font-size: 12px; }
body.is-outside .site-footer { position: fixed; left: 0; right: 0; bottom: 0; border-top: 0; }

/* --- шапка --- */

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px;
  padding: 12px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 17px; margin-right: auto; }
.brand__logo { width: 26px; height: 26px; border-radius: 50%; }
.chip {
  padding: 5px 11px; border-radius: 999px; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent);
}
button.chip { font-family: inherit; cursor: pointer; }
.chip--empty {
  border-color: color-mix(in srgb, var(--danger) 35%, transparent);
  background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger);
}
.chip--pending { animation: pulse 1.2s ease-in-out infinite; }

/* Полоса загрузки под шапкой: видна, пока идёт любой запрос к серверу. */
.progress {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20; height: 3px; overflow: hidden;
  opacity: 0; transition: opacity .2s ease; pointer-events: none;
}
.progress::before {
  content: ""; position: absolute; top: 0; bottom: 0; width: 40%;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: progress 1.1s ease-in-out infinite;
}
body.is-loading .progress { opacity: 1; }
@keyframes progress { from { left: -40%; } to { left: 100%; } }
@keyframes pulse { 50% { opacity: .45; } }

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

.btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
  transition: background .15s ease, transform .1s ease, opacity .15s ease;
}
.btn:active { transform: scale(.98); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn:disabled { opacity: .5; cursor: default; }
.btn--link { width: 100%; margin-top: 8px; background: transparent; border-color: transparent; color: var(--accent); }

/* Кнопка, по которой идёт запрос: вместо текста — крутилка. */
.btn.is-busy, .plan.is-busy, .segmented__item.is-busy { color: transparent !important; pointer-events: none; }
.btn.is-busy::after, .plan.is-busy::after, .segmented__item.is-busy::after {
  content: ""; position: absolute; top: 50%; left: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px;
  border-radius: 50%; border: 2px solid color-mix(in srgb, var(--text) 25%, transparent);
  border-top-color: var(--text); animation: spin .8s linear infinite;
}
.btn--primary.is-busy::after {
  border-color: color-mix(in srgb, var(--accent-text) 25%, transparent); border-top-color: var(--accent-text);
}

textarea, .field input, .items--edit input {
  width: 100%; padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text); font: inherit;
}
textarea { margin-top: 12px; resize: none; }
textarea:focus, input:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); outline-offset: 1px;
}

/* --- общие блоки --- */

.panel { padding: 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-elevated); }

/* первая загрузка */
.loading {
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  padding: 72px 20px; color: var(--muted); text-align: center;
}
.spinner {
  width: 44px; height: 44px; border-radius: 50%;
  border: 3px solid color-mix(in srgb, var(--accent) 22%, transparent); border-top-color: var(--accent);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- покупка --- */

.plans { display: grid; gap: 16px; }
.plan-group { display: grid; gap: 8px; }
.plan-group__title { font-weight: 600; }
.plan-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.plan {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  min-height: 80px; padding: 10px 6px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg-elevated); color: var(--text); font: inherit; font-size: 14px; cursor: pointer;
  transition: border-color .15s ease, background .15s ease;
}
.plan:hover { border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.plan__price {
  padding: 3px 10px; border-radius: 999px; font-weight: 650; white-space: nowrap;
  background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent);
}
.plan:disabled { opacity: .5; }

/* --- выбор фото --- */

.drop {
  display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center;
  margin-top: 16px; padding: 36px 20px; border: 1px dashed color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: var(--radius); background: var(--bg-elevated); cursor: pointer;
}
.drop__icon { width: 88px; height: 88px; border-radius: 50%; box-shadow: 0 0 0 6px color-mix(in srgb, var(--accent) 10%, transparent); }
.drop__title { margin-top: 8px; font-size: 19px; font-weight: 650; }
.drop .btn { margin-top: 12px; width: 100%; }
.ready-photo {
  display: block; width: 100%; max-height: 50vh; margin-top: 16px; object-fit: cover;
  border: 1px solid var(--border); border-radius: var(--radius);
}

/* --- анализ идёт --- */

.busy {
  position: relative; margin-top: 16px; border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; aspect-ratio: 3 / 4; background: #000;
}
.busy img { width: 100%; height: 100%; object-fit: cover; filter: blur(6px) brightness(.5); transform: scale(1.06); }
.busy__overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px; text-align: center; color: #fff;
}
.busy__title { font-size: 19px; font-weight: 650; }
/* «сканирование» по фото, пока ИИ думает */
.busy::after {
  content: ""; position: absolute; left: 0; right: 0; height: 2px; top: 0;
  background: var(--accent); box-shadow: 0 0 18px 4px color-mix(in srgb, var(--accent) 60%, transparent);
  animation: scan 2.2s ease-in-out infinite alternate;
}
@keyframes scan { to { top: calc(100% - 2px); } }

/* --- результат --- */

.card {
  display: block; width: 100%; margin-top: 16px; border: 1px solid var(--border);
  border-radius: var(--radius); cursor: zoom-in;
}
.totals { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.total {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 10px 4px;
  border: 1px solid var(--border); border-radius: 12px; background: var(--bg-elevated);
  font-size: 12px; color: var(--muted); border-top: 3px solid var(--c);
}
.total b { font-size: 17px; color: var(--text); white-space: nowrap; font-variant-numeric: tabular-nums; }
.total--kcal { --c: var(--kcal); }
.total--protein { --c: var(--protein); }
.total--fat { --c: var(--fat); }
.total--carbs { --c: var(--carbs); }
.items {
  list-style: none; margin: 0; padding: 0; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--bg-elevated); overflow: hidden;
}
.items li { display: flex; justify-content: space-between; gap: 12px; padding: 12px 14px; }
.items li + li { border-top: 1px solid var(--border); }
.items li .muted { white-space: nowrap; font-variant-numeric: tabular-nums; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.actions__wide { grid-column: 1 / -1; }

.failure {
  margin-top: 16px; padding: 32px 20px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg-elevated); text-align: center;
}
.failure p { font-size: 16px; margin: 0 0 16px; }
.failure .btn { width: 100%; }
.failure .btn + .btn { margin-top: 8px; }

/* «Другой вид» под карточкой: лента превью шаблонов */
.result-themes h3 { margin-top: 16px; }
.result-themes__list { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.result-themes__list::-webkit-scrollbar { display: none; }
.result-theme {
  position: relative; flex: none; width: 64px; aspect-ratio: 9 / 16; padding: 0; overflow: hidden; cursor: pointer;
  border: 2px solid var(--border); border-radius: 10px; background: var(--surface);
}
.result-theme img { display: block; width: 100%; height: 100%; object-fit: cover; }
.result-theme.is-active { border-color: var(--accent); }
.result-theme.template--loading img { opacity: 0; }
.result-theme.is-busy::after {
  content: ""; position: absolute; top: 50%; left: 50%; width: 20px; height: 20px; margin: -10px 0 0 -10px;
  border-radius: 50%; border: 2px solid rgba(255, 255, 255, .3); border-top-color: #fff; animation: spin .8s linear infinite;
}
.result-theme.is-busy img { opacity: .4; }

/* --- настройки --- */

.segmented { display: flex; gap: 4px; padding: 4px; border: 1px solid var(--border); border-radius: 12px; background: var(--bg-elevated); }
.segmented__item {
  position: relative; flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: transparent;
  color: var(--text); font: inherit; cursor: pointer;
}
.segmented__item--active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.templates { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.template {
  position: relative; padding: 0; border: 2px solid var(--border); border-radius: var(--radius);
  overflow: hidden; background: var(--surface); cursor: pointer;
}
.template img { display: block; width: 100%; height: 100%; object-fit: cover; transition: opacity .25s ease; }
.template--active { border-color: var(--accent); }
.template--active::after {
  content: "✓"; position: absolute; top: 8px; right: 8px; width: 28px; height: 28px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-text); font-weight: 700;
}
/* превью ещё грузится — мерцающая заглушка */
.template--loading img { opacity: 0; }
.template--loading::before {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 30%, color-mix(in srgb, var(--text) 8%, transparent) 50%, transparent 70%);
  background-size: 200% 100%; animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }
.template.is-busy::before {
  content: ""; position: absolute; inset: 0; z-index: 1; background: rgba(0, 0, 0, .45);
}
.template.is-busy::after {
  content: ""; position: absolute; top: 50%; left: 50%; z-index: 2; width: 28px; height: 28px; margin: -14px 0 0 -14px;
  border-radius: 50%; border: 3px solid rgba(255, 255, 255, .25); border-top-color: #fff; animation: spin .8s linear infinite;
  background: none; display: block;
}
.template--broken img { visibility: hidden; }
.template__name {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 20px 10px 8px;
  background: linear-gradient(transparent, rgba(0, 0, 0, .75)); color: #fff; font-weight: 600; font-size: 13px;
}

.support { display: grid; gap: 12px; }
.support textarea { margin-top: 0; }
.support .btn { width: 100%; }
.legal-link { margin-top: 20px; color: var(--muted); font-weight: 500; font-size: 14px; }

/* --- лимит --- */

.limit-card { display: grid; gap: 10px; }
.limit-card__value { font-size: 40px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; color: var(--accent); }
.limit-card__value--text { font-size: 18px; color: var(--text); }
.limit-card__value.is-exhausted { color: var(--danger); }
.limit-bar { height: 8px; border-radius: 999px; background: var(--surface); overflow: hidden; }
.limit-bar span { display: block; height: 100%; border-radius: inherit; background: var(--accent); transition: width .4s ease; }
.limit-bar.is-exhausted span { background: var(--danger); }
.limit-note { margin: 12px 0 0; color: var(--accent); border-color: color-mix(in srgb, var(--accent) 30%, var(--border)); }
#limit-base { margin: 12px 2px; }
.limit-buy { width: 100%; margin-top: 4px; }
.referral { margin-top: 16px; }
.referral h3 { margin-top: 0; }
.referral p { margin: 0 0 10px; }
.referral .btn { width: 100%; }

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

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg-elevated) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); font: inherit; font-size: 12px; cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.tab span:first-child { font-size: 21px; filter: grayscale(1); opacity: .55; }
.tab--active { color: var(--accent); font-weight: 600; background: color-mix(in srgb, var(--accent) 10%, transparent); }
.tab--active span:first-child { filter: none; opacity: 1; }

/* Увеличенная карточка / шаблон — на весь экран, целиком (без обрезки), тап — закрыть. */
dialog#zoom {
  position: fixed; inset: 0; width: 100vw; height: 100vh; height: 100dvh; max-width: none; max-height: none;
  margin: 0; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  border: 0; background: transparent; cursor: zoom-out;
}
dialog#zoom[open] { display: flex; align-items: center; justify-content: center; }
dialog#zoom::backdrop { background: rgba(0, 0, 0, .85); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
dialog#zoom img { display: block; width: 100%; height: 100%; object-fit: contain; }

.toast {
  position: fixed; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); z-index: 10;
  transform: translateX(-50%); max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  box-shadow: 0 8px 30px rgba(0, 0, 0, .45); font-size: 14px; text-align: center;
}

/* --- правка результата --- */

.field { display: flex; flex-direction: column; gap: 6px; }
.items--edit li { flex-direction: column; align-items: stretch; gap: 8px; }
.items--edit input { padding: 10px 12px; border-radius: 10px; background: var(--surface); }
.edit-row { display: flex; align-items: center; gap: 8px; }
.edit-name { flex: 1; }
/* вес, ккал, Б, Ж, У — пять полей в ряд (на узком экране — в две строки) */
.edit-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
@media (max-width: 360px) { .edit-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.num { display: flex; flex-direction: column; align-items: stretch; gap: 2px; font-size: 12px; text-align: center; }
.items--edit .num input { padding: 8px 4px; text-align: center; }
.edit-remove {
  flex: none; width: 36px; height: 36px; border: 0; border-radius: 50%;
  background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); font: inherit; cursor: pointer;
}
.edit-remove:disabled { opacity: .3; }
.edit-total { margin: 12px 0 4px; font-weight: 600; }

/* плавная смена экранов */
.state:not([hidden]), .view:not([hidden]) { animation: appear .2s ease; }
@keyframes appear { from { opacity: 0; transform: translateY(4px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
