/* Оформление опирается на переменные темы Telegram: приложение должно выглядеть
   частью клиента, а не чужой страницей внутри него. Значения после запятой —
   запасные, для случая когда Mini App открыт вне Telegram. */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg-secondary: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --link: var(--tg-theme-link-color, #007aff);
  --button: var(--tg-theme-button-color, #007aff);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: #e5484d;
  --success: #30a46c;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(60px + env(safe-area-inset-bottom));
}

main { padding: 12px; }
h2 { font-size: 16px; margin: 18px 0 10px; }

/* --- поиск и навигация по разделу --- */

.search-bar { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 4px 0 8px; }
.search-row { display: flex; gap: 8px; }

#q {
  width: 100%;
  padding: 11px 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 16px; /* меньше 16px — iOS зумит поле при фокусе */
}

#q:focus { outline: 2px solid var(--button); outline-offset: -2px; }

.scan-btn {
  flex-shrink: 0;
  width: 46px;
  border: none; border-radius: var(--radius);
  background: var(--bg-secondary); color: var(--link);
  font-size: 20px; cursor: pointer;
}

.crumb {
  display: flex; align-items: center; gap: 10px;
  margin-top: 8px; font-weight: 600; font-size: 15px;
}
.crumb-back {
  border: none; background: none; padding: 0;
  color: var(--link); font: inherit; font-weight: 400; cursor: pointer;
  white-space: nowrap;
}
#crumb-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Сортировка: горизонтальная лента, чтобы не отнимать высоту у товаров. */
.chips {
  display: flex; gap: 6px; margin-top: 8px;
  overflow-x: auto; scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex-shrink: 0;
  padding: 6px 12px; border: none; border-radius: 999px;
  background: var(--bg-secondary); color: var(--hint);
  font: inherit; font-size: 13px; cursor: pointer;
}
.chips button.active { background: var(--button); color: var(--button-text); }

/* --- разделы --- */

.sections-title { margin-top: 22px; }

.sections {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px;
}

.section-tile {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 10px;
  border: none; border-radius: var(--radius);
  background: var(--bg-secondary); color: var(--text);
  font: inherit; text-align: left; cursor: pointer;
}
.section-icon { font-size: 20px; line-height: 1; }
.section-name { flex: 1; min-width: 0; font-size: 14px; }
.section-count { color: var(--hint); font-size: 12px; }

/* --- полки на главной --- */

.shelf-row {
  display: flex; gap: 10px;
  overflow-x: auto; scroll-snap-type: x proximity;
  scrollbar-width: none;
  /* Вылезаем за padding main, чтобы лента упиралась в край экрана. */
  margin: 0 -12px; padding: 0 12px 4px;
}
.shelf-row::-webkit-scrollbar { display: none; }
.shelf-tile { width: 140px; flex-shrink: 0; scroll-snap-align: start; }

/* --- сетка товаров --- */

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

.tile {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 8px;
  cursor: pointer;
  display: flex; flex-direction: column;
}

/* Фон плитки светлый всегда: у фотографий товаров подложка белая,
   и на тёмной теме они иначе висят в воздухе рваным прямоугольником. */
.tile-image {
  position: relative;
  background: #fff;
  border-radius: 8px;
  aspect-ratio: 1;
  margin-bottom: 8px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.tile-image img { width: 100%; height: 100%; object-fit: contain; }
.tile-noimage { font-size: 28px; opacity: .35; }

.tile-badge {
  position: absolute; left: 6px; top: 6px;
  background: var(--danger); color: #fff;
  font-size: 12px; font-weight: 600;
  padding: 2px 6px; border-radius: 6px;
}

.tile-price { font-size: 17px; font-weight: 700; }
.tile-price .from { font-size: 12px; font-weight: 400; color: var(--hint); }
.tile-was { font-size: 12px; }

.tile-name {
  margin-top: 3px; font-size: 13px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}

.tile-meta {
  display: flex; flex-wrap: wrap; gap: 6px;
  margin-top: 4px; color: var(--hint); font-size: 12px;
}

/* Разница между сетями — то, ради чего всё затевалось, поэтому она на плитке,
   а не в карточке. */
.tile-spread { margin-top: 4px; font-size: 12px; color: var(--success); }
.tile-single { margin-top: 4px; font-size: 12px; color: var(--hint); }

.more { margin-top: 12px; }
.more .action { margin-top: 0; }

/* --- списки (список покупок) --- */

.list { display: flex; flex-direction: column; gap: 8px; }

.card {
  background: var(--bg-secondary);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  cursor: pointer;
}

.card img {
  width: 48px; height: 48px;
  object-fit: contain;
  border-radius: 8px;
  background: #fff;
  flex-shrink: 0;
}

.card-body { flex: 1; min-width: 0; }
.card-name { font-weight: 500; margin-bottom: 3px; }

.card-meta {
  display: flex; flex-wrap: wrap; gap: 8px;
  color: var(--hint); font-size: 13px;
}

.price { font-weight: 600; white-space: nowrap; }
.promo { color: var(--danger); }
.strike { text-decoration: line-through; color: var(--hint); font-weight: 400; }

.badge {
  font-size: 12px; padding: 1px 7px; border-radius: 999px;
  background: var(--bg); color: var(--hint);
}

.hint { color: var(--hint); text-align: center; padding: 24px 12px; }

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

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--bg-secondary);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar button {
  flex: 1; padding: 14px 4px;
  border: none; background: none;
  color: var(--hint); font-size: 13px; font-family: inherit;
  cursor: pointer;
}

.tabbar button.active { color: var(--button); font-weight: 600; }

/* --- карточка товара --- */

.sheet { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); }

.sheet-body {
  position: relative;
  width: 100%; max-height: 88vh; overflow-y: auto;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
}

.sheet h2 { margin: 0 0 4px; font-size: 17px; }

.hero {
  display: block;
  width: 100%; max-width: 240px; height: 180px;
  margin: 0 auto 14px;
  object-fit: contain;
  background: #fff;
  border-radius: var(--radius);
}

.chain-row {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 10px; padding: 10px 0;
  border-bottom: 1px solid var(--bg-secondary);
}
.chain-row:last-of-type { border-bottom: none; }
.chain-name { font-weight: 500; }
.chain-sub { color: var(--hint); font-size: 13px; }

button.action {
  width: 100%; margin-top: 14px; padding: 13px;
  border: none; border-radius: var(--radius);
  background: var(--button); color: var(--button-text);
  font-size: 15px; font-weight: 600; font-family: inherit;
  cursor: pointer;
}
button.action.secondary { background: var(--bg-secondary); color: var(--text); }
button.action.danger { background: var(--bg-secondary); color: var(--danger); }

.qty-row { display: flex; align-items: center; gap: 12px; margin-top: 14px; }
.qty-row label { color: var(--hint); font-size: 14px; }
.qty-row input {
  flex: 1; padding: 9px 12px;
  border: none; border-radius: 10px;
  background: var(--bg-secondary); color: var(--text);
  font-size: 16px; font-family: inherit;
}

/* --- корзина --- */

.plan { background: var(--bg-secondary); border-radius: var(--radius); padding: 14px; margin-bottom: 10px; }
.plan h3 { margin: 0 0 8px; font-size: 15px; }
.plan-line { display: flex; justify-content: space-between; gap: 10px; padding: 3px 0; font-size: 14px; }
.plan-total { font-weight: 600; }
.plan-note { color: var(--hint); font-size: 13px; margin-top: 8px; }
.missing { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* --- уведомление сверху --- */

.banner {
  background: var(--bg-secondary);
  color: var(--hint);
  padding: 10px 14px;
  font-size: 13px;
  text-align: center;
}

/* --- сканер штрихкода --- */

.scanner {
  position: fixed; inset: 0; z-index: 30;
  background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 20px;
}

/* Сцена держит размер сама, не дожидаясь видеопотока: иначе там, где камеры
   нет, элемент схлопывается в ноль и рамка с подписью налезают друг на друга
   поверх чёрного экрана — ровно то, что выглядит как поломка. */
.scanner-stage {
  position: relative;
  width: 100%; max-width: 520px;
  aspect-ratio: 3 / 4; max-height: 70vh;
  background: #111;
  border-radius: var(--radius);
  overflow: hidden;
}

#scanner-video { width: 100%; height: 100%; object-fit: cover; }

/* Рамка-прицел: штрихкод длинный и низкий, поэтому окно широкое. */
.scanner-frame {
  position: absolute;
  left: 8%; right: 8%; top: 42%; height: 16%;
  border: 2px solid rgba(255, 255, 255, .9);
  border-radius: 8px;
  pointer-events: none;
}

.scanner-status { color: #fff; text-align: center; margin: 16px 0; font-size: 14px; }
.scanner .action { max-width: 320px; }
