/*
 * Mini App «📷 Сканировать» (#55, этап 4).
 * Минималистичный full-screen видоискатель + рамка прицела +
 * статус-строка + оверлей ошибки. Цвета привязаны к Telegram theme
 * через --tg-theme-* CSS-переменные (Telegram WebApp их прокидывает).
 */

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

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: var(--tg-theme-bg-color, #000);
    color: var(--tg-theme-text-color, #fff);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.hidden {
    display: none !important;
}

/* Видоискатель: html5-qrcode сам впишет <video> сюда. Растягиваем
 * на весь экран, чтобы продавцу было привычно — как камера в ОС. */
#reader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;
    z-index: 1;
}

/* html5-qrcode по умолчанию накладывает свой UI (выпадашка камеры,
 * stop-кнопка, прогресс-бар). Прячем — у нас своё управление через TG. */
#reader__dashboard,
#reader__scan_region > img,
#reader__header_message {
    display: none !important;
}

/* html5-qrcode рисует свой scan-region с border'ом и затемнением.
 * Это конфликтует с нашей .aim-frame (две рамки, разный цвет). Гасим
 * библиотечный визуал, оставляем только нашу. Шире, чем перебивать
 * каждый класс — `> div` ловит угловые SVG-скобки, shaded_region —
 * фоновый затемнитель.
 */
#reader__scan_region,
#reader__scan_region__shaded_region,
#reader__scan_region > div {
    border: none !important;
    box-shadow: none !important;
    background: transparent !important;
}

#reader video {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover;
}

/* Прицел — рамка по центру, ~85% ширины × 40% высоты с aspect-ratio
 * 1.0 (квадратнее, чем 16:9 — продавец промахивается по узкой полосе).
 * Не интерактивная, не блокирует касания: pointer-events: none. */
#aim-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.aim-frame {
    width: 85vw;
    height: 40vh;
    max-width: 480px;
    border: 2px solid var(--tg-theme-button-color, #2481cc);
    border-radius: 12px;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.4);
}

/* Индикатор «🔍 сканирую…» — под рамкой, пульсирующая точка слева. */
#scan-indicator {
    margin-top: 16px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 14px;
    background: rgba(0, 0, 0, 0.6);
    border-radius: 16px;
    color: #fff;
    font-size: 14px;
}

.scan-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--tg-theme-button-color, #2481cc);
    animation: scan-pulse 1.2s ease-in-out infinite;
}

@keyframes scan-pulse {
    0%, 100% { opacity: 1.0; transform: scale(1.0); }
    50%      { opacity: 0.3; transform: scale(0.7); }
}

/* Подсказка про tap-to-focus — мелко под индикатором. */
#scan-hint {
    margin-top: 6px;
    padding: 0 24px;
    text-align: center;
    color: rgba(255, 255, 255, 0.7);
    font-size: 12px;
    line-height: 1.3;
}

/* Debug-счётчик кадров — мелким курсивом под индикатором. */
#debug-counter {
    margin-top: 8px;
    color: rgba(255, 255, 255, 0.6);
    font-size: 11px;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
}

/* Тулбар сверху — кнопки 🔦 и ✍️ */
#toolbar {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 5;
    display: flex;
    gap: 8px;
}

#toolbar button {
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 22px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: 20px;
    cursor: pointer;
}

#toolbar button.active {
    background: var(--tg-theme-button-color, #2481cc);
}

#toolbar button:active {
    opacity: 0.7;
}

/* Статус-строка по нижнему краю — поверх видео. */
#status {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px;
    text-align: center;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: 16px;
    z-index: 3;
}

#status.error {
    background: rgba(180, 30, 30, 0.85);
}

/* Manual EAN-ввод — fullscreen overlay с input на 13 цифр. */
#manual-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 8;
}

.manual-box {
    background: var(--tg-theme-secondary-bg-color, #1f1f1f);
    border-radius: 14px;
    padding: 24px 20px;
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.manual-box p {
    margin-bottom: 16px;
    font-size: 16px;
    color: var(--tg-theme-text-color, #fff);
}

.manual-box input {
    width: 100%;
    padding: 14px;
    margin-bottom: 16px;
    border: 1.5px solid var(--tg-theme-hint-color, #555);
    border-radius: 10px;
    background: var(--tg-theme-bg-color, #000);
    color: var(--tg-theme-text-color, #fff);
    font-size: 18px;
    font-family: ui-monospace, "SF Mono", Menlo, monospace;
    text-align: center;
    letter-spacing: 2px;
}

.manual-box input:focus {
    border-color: var(--tg-theme-button-color, #2481cc);
    outline: none;
}

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

.manual-actions button {
    flex: 1;
    padding: 14px;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
}

#btn-manual-cancel {
    background: var(--tg-theme-secondary-bg-color, #444);
    color: var(--tg-theme-text-color, #fff);
}

#btn-manual-ok {
    background: var(--tg-theme-button-color, #2481cc);
    color: var(--tg-theme-button-text-color, #fff);
}

#btn-manual-ok:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Полноэкранный оверлей с ошибкой («EAN не найден»). */
#error-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 10;
}

.error-box {
    background: var(--tg-theme-secondary-bg-color, #1f1f1f);
    color: var(--tg-theme-text-color, #fff);
    border-radius: 14px;
    padding: 24px 20px;
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.error-box p {
    margin-bottom: 20px;
    font-size: 17px;
    line-height: 1.4;
}

.error-box button {
    display: block;
    width: 100%;
    margin-top: 10px;
    padding: 14px;
    border: none;
    border-radius: 10px;
    background: var(--tg-theme-button-color, #2481cc);
    color: var(--tg-theme-button-text-color, #fff);
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
}

.error-box button:active {
    opacity: 0.8;
}

/* ── Оверлей остатков (режим ?mode=stock) ─────────────────────────── */
#stock-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    z-index: 11;
}

.stock-box {
    background: var(--tg-theme-secondary-bg-color, #1f1f1f);
    color: var(--tg-theme-text-color, #fff);
    border-radius: 14px;
    padding: 22px 20px;
    width: 100%;
    max-width: 360px;
    text-align: center;
}

#stock-title {
    margin: 0 0 4px;
    font-size: 18px;
    font-weight: 600;
    line-height: 1.3;
}

#stock-color {
    margin: 0 0 16px;
    font-size: 14px;
    opacity: 0.65;
}

#stock-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 20px;
}

.stock-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.06);
    font-size: 17px;
}

.stock-size {
    font-weight: 600;
    letter-spacing: 0.02em;
}

.stock-qty {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
}

/* Нет в наличии — приглушённо-красным; неизвестно (NULL) — серым. */
.stock-qty.zero {
    color: #ff6b6b;
    font-weight: 500;
}

.stock-qty.unknown {
    opacity: 0.5;
    font-weight: 400;
}

.stock-box button {
    display: block;
    width: 100%;
    padding: 14px;
    border: none;
    border-radius: 10px;
    background: var(--tg-theme-button-color, #2481cc);
    color: var(--tg-theme-button-text-color, #fff);
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
}

.stock-box button:active {
    opacity: 0.8;
}

#btn-manual {
    background: var(--tg-theme-secondary-bg-color, #444) !important;
    color: var(--tg-theme-text-color, #fff) !important;
}
