/* ─── Базовые сбросы ─────────────────────────────────────────────────────── */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

/* HTML-атрибут [hidden] должен скрывать элемент даже когда у него
   задан display: flex/grid/block. По стандарту это display: none. */
[hidden] {
  display: none !important;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, sans-serif;
  font-size: 1rem;
  line-height: 1.5;
  color: #1a1a1a;
  background: #f5f5f7;
  min-height: 100vh;
  padding: 24px 16px;
}

/* ─── Layout ─────────────────────────────────────────────────────────────── */

.container {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

header {
  text-align: center;
  padding: 16px 0;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #1a1a1a;
  margin-bottom: 4px;
}

.subtitle {
  font-size: 0.875rem;
  color: #666;
}

/* ─── Поле поиска ───────────────────────────────────────────────────────── */

.search-box {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: white;
  padding: 16px;
  border-radius: 12px;
  border: 1px solid #e5e5e7;
}

.input-row {
  display: flex;
  gap: 8px;
}

.input-row input {
  flex: 1;
  font-size: 1rem;
  padding: 12px 14px;
  border: 1px solid #d1d1d6;
  border-radius: 8px;
  background: white;
  color: #1a1a1a;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.02em;
  outline: none;
  transition: border-color 0.15s ease;
}

.input-row input:focus {
  border-color: #007aff;
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

#scan-btn {
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: white;
  border: 1px solid #d1d1d6;
  border-radius: 8px;
  cursor: pointer;
  color: #333;
  transition: all 0.15s ease;
}

#scan-btn:hover {
  background: #f5f5f7;
  border-color: #999;
}

#scan-btn:active {
  background: #e5e5e7;
}

.btn-primary {
  width: 100%;
  padding: 12px 24px;
  font-size: 1rem;
  font-weight: 600;
  color: white;
  background: #007aff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn-primary:hover {
  background: #006fe0;
}

.btn-primary:active {
  background: #0062c4;
}

.btn-primary:disabled {
  background: #b0d4ff;
  cursor: not-allowed;
}

/* ─── Сообщение ────────────────────────────────────────────────────────── */

.message {
  background: white;
  border: 1px solid #e5e5e7;
  border-left: 4px solid #999;
  border-radius: 8px;
  padding: 16px;
  font-size: 0.9375rem;
  color: #333;
  text-align: center;
}

.message.error {
  border-left-color: #ff3b30;
  color: #ff3b30;
}

.message.empty {
  border-left-color: #ff9500;
  color: #cc7700;
}

/* ─── Загрузка ─────────────────────────────────────────────────────────── */

.loader {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px;
  color: #666;
  font-size: 0.875rem;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e5e5e7;
  border-top-color: #007aff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ─── Результат ────────────────────────────────────────────────────────── */

.result {
  background: white;
  border: 1px solid #e5e5e7;
  border-radius: 12px;
  overflow: hidden;
}

.result-header {
  background: #fafafa;
  padding: 14px 16px;
  border-bottom: 1px solid #e5e5e7;
}

.result-ttn-row,
.result-source-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.875rem;
}

.result-source-row {
  margin-top: 4px;
}

.result-label {
  color: #666;
  flex-shrink: 0;
}

.result-ttn-value {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-weight: 600;
  font-size: 0.9375rem;
  color: #1a1a1a;
  letter-spacing: 0.02em;
}

.result-source-value {
  color: #1a1a1a;
  font-weight: 500;
}

.products-group {
  padding: 16px;
}

.products-group + .products-group {
  border-top: 1px solid #e5e5e7;
}

.products-group-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #666;
  margin-bottom: 12px;
}

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

.products-list li {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 8px 12px;
  background: #fafafa;
  border-radius: 6px;
  font-size: 0.9375rem;
}

.product-name {
  color: #1a1a1a;
}

.product-qty {
  flex-shrink: 0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-weight: 600;
  color: #007aff;
  font-size: 0.875rem;
}

/* ─── Сканер ───────────────────────────────────────────────────────────── */

.scanner-overlay {
  position: fixed;
  inset: 0;
  background: black;
  z-index: 1000;
  display: flex;
  flex-direction: column;
}

.scanner-header {
  padding: calc(16px + env(safe-area-inset-top, 0px)) 16px 16px 16px;
  background: rgba(0, 0, 0, 0.85);
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9375rem;
}

.scanner-close {
  background: none;
  border: none;
  color: white;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scanner-view {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}

.scanner-view video {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover;
}

/* Рамка-прицел поверх видео — подсказка куда наводить штрих-код.
   Прямоугольник, узкий по вертикали (штрих-коды длинные и узкие). */
.scanner-frame {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(85vw, 500px);
  height: min(30vh, 180px);
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 12px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  z-index: 10;
}

/* Скрываем встроенную «облицовку» html5-qrcode (она может рисовать
   свою рамку которая перекрывает нашу) */
#qr-reader__dashboard,
#qr-reader__scan_region > img,
#qr-reader__header_message {
  display: none !important;
}
#qr-reader {
  border: none !important;
}

/* Уголки рамки — для лучшей видимости */
.scanner-frame::before,
.scanner-frame::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 24px;
  border: 3px solid #00ff00;
}
.scanner-frame::before {
  top: -2px;
  left: -2px;
  border-right: none;
  border-bottom: none;
  border-top-left-radius: 12px;
}
.scanner-frame::after {
  bottom: -2px;
  right: -2px;
  border-left: none;
  border-top: none;
  border-bottom-right-radius: 12px;
}

/* Общий стиль для кнопок-действий поверх сканера (фонарик, фото).
   Используем calc с safe-area-inset чтобы кнопки не уезжали под нав-бар Safari iOS 26+. */
.scanner-action-btn {
  position: absolute;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  color: white;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 11;
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
  /* Если бэкдроп-фильтр не работает (iOS Safari иногда) — даём фолбэк фон */
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}

.scanner-action-btn:active {
  transform: scale(0.95);
}

/* Кнопка фонарика — справа снизу */
.torch-btn {
  right: 24px;
}

.torch-btn.torch-on {
  background: #ffeb3b;
  color: #1a1a1a;
}

/* Кнопка "Сделать фото" — слева снизу. Для iPhone когда живой сканер не справляется. */
.photo-btn {
  left: 24px;
}

/* Краткое сообщение поверх сканера ("не распознан" и т.д.) */
.scanner-snack {
  position: absolute;
  left: 50%;
  bottom: 100px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85);
  color: white;
  padding: 10px 18px;
  border-radius: 24px;
  font-size: 0.875rem;
  z-index: 12;
  white-space: nowrap;
  animation: scanner-snack-show 0.2s ease;
}

.scanner-snack-hide {
  animation: scanner-snack-hide 0.3s ease forwards;
}

@keyframes scanner-snack-show {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

@keyframes scanner-snack-hide {
  from { opacity: 1; transform: translate(-50%, 0); }
  to   { opacity: 0; transform: translate(-50%, 10px); }
}

.scanner-hint {
  padding: 16px;
  background: rgba(0, 0, 0, 0.8);
  color: rgba(255, 255, 255, 0.8);
  text-align: center;
  font-size: 0.875rem;
}

/* На больших экранах сделаем сканер компактнее */
@media (min-width: 768px) {
  .scanner-view {
    max-width: 600px;
    max-height: 600px;
    margin: auto;
    border-radius: 12px;
  }
}
