:root {
    --tg-theme-bg-color: #ffffff;
    --tg-theme-text-color: #000000;
    --tg-theme-button-color: #2481cc;
    --tg-theme-button-text-color: #ffffff;
    --primary: #2481cc;
    --radius: 12px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--tg-theme-bg-color);
    color: var(--tg-theme-text-color);
    min-height: 100vh;
    padding-bottom: 80px;
}

#app {
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.header {
    padding: 16px;
    text-align: center;
    border-bottom: 1px solid rgba(0,0,0,0.1);
    position: relative;
}

.header h1 {
    font-size: 1.5rem;
}

.home-link {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.9rem;
    color: var(--primary);
    text-decoration: none;
    white-space: nowrap;
}

.home-link:hover {
    text-decoration: underline;
}

.home-link.hidden {
    display: none;
}

.main {
    padding: 16px;
}

.section {
    margin-bottom: 24px;
}

.section.hidden {
    display: none;
}

.section h2 {
    font-size: 1.2rem;
    margin-bottom: 12px;
}

.grid {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Сетка категорий: адаптивно — 2 колонки на телефоне, больше на широком экране */
#categories-list.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
}

/* Сетка товаров: адаптивно — 2 колонки на телефоне, больше на широком экране */
#products-list.grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
}

.card {
    background: var(--tg-theme-bg-color);
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: var(--radius);
    padding: 16px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.card:active {
    transform: scale(0.98);
}

.card h3 {
    font-size: 1rem;
    margin-bottom: 4px;
}

.card .price {
    color: var(--primary);
    font-weight: 600;
}

/* Карточки категорий: 2 в ряд, картинка по ширине ячейки */
.card-category {
    padding: 0;
    overflow: hidden;
    min-width: 0; /* чтобы не вылезало из grid-ячейки */
}
.card-category .category-img-wrap {
    position: relative;
    aspect-ratio: 3 / 4;
    overflow: hidden;
    background: rgba(0,0,0,0.05);
}
.card-category .category-img-wrap .category-img {
    position: absolute;
    left: 50%;
    top: 50%;
    transform-origin: center;
    /* Размер и позиция управляются полностью через JS-трансформ,
       как в редакторе: scale хранится относительно натурального размера */
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    display: block;
}
.card-category .category-placeholder {
    width: 100%;
    aspect-ratio: 3 / 4;
    min-height: 100px;
    background: linear-gradient(135deg, #e8eef4 0%, #dde4eb 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}
.card-category .category-placeholder::after {
    content: "🛒";
    font-size: 2rem;
    opacity: 0.6;
}
.card-category h3 {
    padding: 8px 10px 2px;
    margin: 0;
    font-size: 0.95rem;
    line-height: 1.2;
}
.card-category .category-desc {
    padding: 0 10px 10px;
    margin: 0;
    font-size: 0.8rem;
    color: var(--tg-theme-hint-color, #666);
    line-height: 1.2;
    display: none; /* в сетке 2 колонки описание скрыто для компактности */
}

.back-btn {
    display: block;
    background: var(--tg-theme-bg-color, #fff);
    border: none;
    border-bottom: 1px solid rgba(0,0,0,0.07);
    color: var(--primary);
    font-size: 1rem;
    cursor: pointer;
    /* Выход за паддинг .main чтобы кнопка была во всю ширину */
    margin: -16px -16px 12px;
    padding: 10px 16px;
    width: calc(100% + 32px);
    text-align: left;
    box-sizing: border-box;
    /* Sticky: прилипает к верху при прокрутке (после того как header уйдёт) */
    position: sticky;
    top: 0;
    z-index: 50;
}

.cart-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px;
    background: var(--tg-theme-bg-color);
    border-top: 1px solid rgba(0,0,0,0.1);
}

.cart-btn {
    width: 100%;
    padding: 14px 24px;
    background: var(--tg-theme-button-color);
    color: var(--tg-theme-button-text-color);
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

.cart-btn:active {
    opacity: 0.9;
}

.loading-text,
.error-text,
.empty {
    text-align: center;
    padding: 24px;
    color: var(--tg-theme-hint-color, #666);
}

.error-text {
    color: #c00;
}

.product-img {
    max-width: 100%;
    border-radius: var(--radius);
    margin: 12px 0;
}

/* ─── Детальная страница товара ─────────────────────── */

.detail-title {
    font-size: 1.1rem;
    line-height: 1.3;
    margin: 10px 0 6px;
}
.detail-price-row {
    display: flex;
    align-items: baseline;
    gap: 6px;
    margin-bottom: 10px;
}
.detail-price-row .price {
    font-size: 1.2rem;
}
.detail-price-row .old-price {
    font-size: 0.95rem;
}
.detail-desc {
    font-size: 0.9rem;
    color: var(--tg-theme-hint-color, #555);
    margin: 10px 0 16px;
    line-height: 1.4;
}

/* Устаревшие классы из предыдущей версии */
.detail-header { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 12px; }
.detail-info { flex: 1; min-width: 0; }
.detail-img-wrap { width: 46%; flex-shrink: 0; aspect-ratio: 1/1; overflow: hidden; position: relative; background: rgba(0,0,0,.05); border-radius: var(--radius); }

/* Кнопка «В корзину» в детальной странице */
.btn-add {
    display: inline-block;
    padding: 8px 14px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
}
.btn-add:active { opacity: 0.8; }

/* ─── Сетка аксессуаров: адаптивно ─────────────────── */

.accessories-heading {
    font-size: 0.95rem;
    margin: 20px 0 10px;
    border-top: 1px solid rgba(0,0,0,0.08);
    padding-top: 14px;
}
.accessories-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
    gap: 8px;
}
.acc-card {
    background: var(--tg-theme-bg-color);
    border: 1px solid rgba(0,0,0,0.1);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.acc-img-wrap {
    width: 100%;
    aspect-ratio: 1 / 1;
    overflow: hidden;
    position: relative;
    background: rgba(0,0,0,0.05);
}
.acc-no-img {
    background: linear-gradient(135deg,#e8eef4,#dde4eb);
}
.acc-body {
    padding: 5px 6px 8px;
    display: flex;
    flex-direction: column;
    gap: 3px;
}
.acc-name {
    font-size: 0.72rem;
    font-weight: 600;
    line-height: 1.2;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.acc-price {
    font-size: 0.78rem;
    color: var(--primary);
    font-weight: 600;
}
.acc-card .btn-add {
    font-size: 0.7rem;
    padding: 5px 8px;
    margin-top: 2px;
}
.acc-card .btn-select-variant {
    background: #f0f0f0;
    color: #333;
    font-size: 0.7rem;
    padding: 5px 8px;
    margin-top: 2px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: inline-block;
}
.acc-card .qty-controls {
    gap: 4px;
}
.acc-card .qty-btn {
    width: 24px;
    height: 24px;
    font-size: 0.9rem;
}
.acc-card .qty-val {
    font-size: 0.8rem;
    min-width: 14px;
}

/* Общий класс для кадрированных изображений */
.framed-img {
    position: absolute;
    left: 50%;
    top: 50%;
    transform-origin: center;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    display: block;
    pointer-events: none;
}

/* Устаревшие классы — оставляем совместимость */
.product-img-wrap {
    width: 100%;
    aspect-ratio: 4 / 3;
    overflow: hidden;
    position: relative;
    background: rgba(0,0,0,0.05);
    border-radius: var(--radius);
    margin: 12px 0;
}
.product-img-framed {
    position: absolute;
    left: 50%; top: 50%;
    transform-origin: center;
    width: auto; height: auto;
    max-width: none; max-height: none;
    display: block;
}

.out-of-stock {
    color: #c00;
    font-size: 0.9rem;
    font-weight: 500;
}

/* Кнопки количества товара */
.qty-controls {
    display: flex;
    align-items: center;
    gap: 8px;
}

.qty-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid var(--primary);
    background: transparent;
    color: var(--primary);
    font-size: 1.1rem;
    font-weight: 700;
    cursor: pointer;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.qty-btn:active { opacity: 0.7; }

.qty-val {
    font-weight: 600;
    min-width: 20px;
    text-align: center;
}

/* Кнопка оформить заказ */
.btn-checkout {
    width: 100%;
    padding: 14px;
    background: #16a34a;
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    margin-top: 8px;
}

.btn-checkout:active { opacity: 0.85; }

/* Модальное окно корзины */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 100;
}

.modal-box {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--tg-theme-bg-color, #fff);
    border-radius: 16px 16px 0 0;
    padding: 20px 16px 32px;
    z-index: 101;
    max-height: 80vh;
    overflow-y: auto;
}

/* ─── Карточки товаров в сетке 2 колонки (формат как у категорий) ─── */

.product-thumb {
    width: 100%;
    aspect-ratio: 3 / 4;
    display: block;
    position: relative;
}
/* Обёртка для кадрированного фото (как category-img-wrap) */
.product-thumb-wrap {
    position: relative;
    aspect-ratio: 3 / 4;
    overflow: hidden;
    background: rgba(0,0,0,0.05);
}
.product-thumb-wrap .framed-img {
    position: absolute;
    left: 50%; top: 50%;
    transform-origin: center;
    width: auto; height: auto;
    max-width: none; max-height: none;
    display: block;
}
.product-thumb-placeholder {
    width: 100%;
    aspect-ratio: 3 / 4;
    background: linear-gradient(135deg, #e8eef4 0%, #dde4eb 100%);
    display: flex;
    align-items: center;
    justify-content: center;
}
.product-thumb-placeholder::after {
    content: "🛍";
    font-size: 2rem;
    opacity: 0.5;
}

#products-list .card {
    padding: 0;
    overflow: hidden;
    min-width: 0;
    display: flex;
    flex-direction: column;
}
#products-list .card-body {
    padding: 8px 8px 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
#products-list .card-body h3 {
    font-size: 0.82rem;
    line-height: 1.2;
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
#products-list .card-body .product-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: auto;
}
#products-list .card-body .price {
    font-size: 0.85rem;
    font-weight: 600;
}
#products-list .card-body .out-of-stock {
    font-size: 0.72rem;
    font-weight: 400;
    margin: 0;
}

/* Старая / зачёркнутая цена */
.old-price {
    text-decoration: line-through;
    color: var(--tg-theme-hint-color, #888);
    font-weight: 400;
    font-size: 0.8em;
    margin-right: 4px;
}

/* ─── Галерея фото товара на детальной странице ──── */
.product-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 6px;
    margin-bottom: 12px;
}
.product-gallery-item {
    aspect-ratio: 3 / 4;
    overflow: hidden;
    position: relative;
    background: rgba(0,0,0,0.05);
    border-radius: var(--radius);
}
/* Если только 1 фото — на всю ширину */
.product-gallery.single .product-gallery-item {
    grid-column: 1 / -1;
    aspect-ratio: 3 / 4;
}

/* ─── Гостевая форма в корзине ──────────────────────────────────────────── */

.guest-form {
    background: rgba(36, 129, 204, 0.07);
    border: 1px solid rgba(36, 129, 204, 0.2);
    border-radius: 10px;
    padding: 12px 14px;
    margin: 10px 0;
}

.guest-form-title {
    font-weight: 600;
    font-size: 0.95rem;
    margin: 0 0 10px;
}

.guest-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 9px 12px;
    border: 1px solid #ccc;
    border-radius: 7px;
    font-size: 0.95rem;
    margin-bottom: 8px;
    outline: none;
    transition: border-color 0.2s;
}

.guest-input:focus {
    border-color: #2481cc;
}

.guest-form-hint {
    font-size: 0.75rem;
    color: #64748b;
    margin: 4px 0 0;
}

/* ─── Баннер успеха (для браузерного режима) ────────────────────────────── */

.success-banner {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(30px);
    background: #16a34a;
    color: #fff;
    padding: 14px 22px;
    border-radius: 12px;
    font-size: 0.95rem;
    font-weight: 500;
    max-width: 90vw;
    text-align: center;
    box-shadow: 0 4px 16px rgba(0,0,0,0.18);
    opacity: 0;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 9999;
}

.success-banner--visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ═══════════════════════════════════════════════════════════
   АДАПТИВНОСТЬ — ПЛАНШЕТ И ДЕСКТОП
   ═══════════════════════════════════════════════════════════ */

/* ── Планшет: от 600px ── */
@media (min-width: 600px) {
    /* Шрифты в карточках чуть крупнее */
    #products-list .card-body h3 {
        font-size: 0.9rem;
    }
    #products-list .card-body .price {
        font-size: 0.95rem;
    }
    .card-category h3 {
        font-size: 1rem;
    }
    /* Показываем описание категорий */
    .card-category .category-desc {
        display: block;
    }
    /* Аксессуары чуть крупнее */
    .acc-name { font-size: 0.78rem; }
    .acc-price { font-size: 0.82rem; }
}

/* ── Десктоп: от 768px ── */
@media (min-width: 768px) {
    body {
        /* Чуть больший отступ снизу, т.к. футер корзины шире */
        padding-bottom: 90px;
        /* Лёгкий фон страницы, чтобы #app выделялся */
        background: #f0f2f5;
    }

    #app {
        /* Карточка приложения на десктопе */
        background: var(--tg-theme-bg-color, #fff);
        min-height: 100vh;
        box-shadow: 0 0 32px rgba(0,0,0,0.10);
    }

    .main {
        padding: 20px 24px;
    }

    /* Шапка */
    .header {
        padding: 20px 24px;
    }
    .header h1 {
        font-size: 1.7rem;
    }

    /* Заголовки разделов */
    .section h2 {
        font-size: 1.3rem;
    }

    /* Карточки категорий */
    .card-category h3 {
        font-size: 1.05rem;
        padding: 10px 12px 4px;
    }
    .card-category .category-desc {
        display: block;
        font-size: 0.82rem;
        padding: 0 12px 12px;
    }

    /* Карточки товаров */
    #products-list .card-body {
        padding: 10px 10px 12px;
    }
    #products-list .card-body h3 {
        font-size: 0.95rem;
        -webkit-line-clamp: 3;
    }
    #products-list .card-body .price {
        font-size: 1rem;
    }

    /* Футер корзины — центрируем содержимое по ширине #app */
    .cart-footer {
        display: flex;
        justify-content: center;
        padding: 14px 0;
    }
    .cart-footer .cart-btn {
        max-width: 900px;
        width: calc(100% - 48px);
        border-radius: var(--radius);
    }

    /* Модальное окно корзины — центрируем и ограничиваем ширину */
    .modal-box {
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        width: 100%;
        max-width: 900px;
        border-radius: 16px 16px 0 0;
        padding: 24px 32px 40px;
    }

    /* Детальная страница товара: галерея слева, инфо справа */
    #product-content {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 24px;
        align-items: start;
    }
    /* Галерея — левая колонка, занимает несколько строк */
    #product-content .product-gallery {
        grid-column: 1;
        grid-row: 1 / 2;
        margin-bottom: 0;
        align-self: start;
    }
    /* Правая панель с информацией */
    #product-content .detail-info-panel {
        grid-column: 2;
        grid-row: 1;
        display: flex;
        flex-direction: column;
        gap: 12px;
    }
    #product-content .detail-info-panel .detail-title {
        margin-top: 0;
    }
    /* Аксессуары — под обеими колонками */
    #product-content .accessories-heading { grid-column: 1 / -1; }
    #product-content .accessories-grid    { grid-column: 1 / -1; }

    /* Аксессуары крупнее */
    .acc-name  { font-size: 0.82rem; }
    .acc-price { font-size: 0.86rem; }
    .acc-card .btn-add { font-size: 0.78rem; padding: 6px 10px; }

    /* Кнопка «Назад» */
    .back-btn {
        font-size: 1.05rem;
    }

    /* Баннер успеха — привязываем к #app */
    .success-banner {
        max-width: 860px;
    }
}

/* ── Широкий десктоп: от 1024px ── */
@media (min-width: 1024px) {
    #products-list .card-body h3 {
        font-size: 1rem;
    }
    /* Галерея детали — чуть мельче карточки, чтобы влезло 3-4 */
    .product-gallery {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    }
}

/* ── Варианты товара (цвета) ── */
.variants-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 10px 0 4px;
}

.variant-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    outline: none;
    transition: transform 0.15s, border-color 0.15s;
    box-shadow: 0 1px 3px rgba(0,0,0,0.25);
    padding: 0;
    flex-shrink: 0;
}

.variant-btn:hover {
    transform: scale(1.12);
}

.variant-btn.active {
    border-color: #1e293b;
    transform: scale(1.1);
    box-shadow: 0 0 0 2px #fff, 0 0 0 4px #1e293b;
}

.variant-btn.oos {
    opacity: 0.35;
    cursor: not-allowed;
}

.variant-label {
    font-size: 0.82rem;
    color: #64748b;
    margin-bottom: 8px;
    min-height: 1.2em;
}
