/* =========================================================
   Мобильная версия (телефоны и планшеты).
   Подключается после style.css на всех страницах.
   ========================================================= */

/* ---------- 1. Базовые защиты от «ломающейся» вёрстки ---------- */
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 80px; }
img, video, svg { max-width: 100%; }
body { overflow-x: hidden; }

/* ---------- 2. Бургер-меню ---------- */
.nav-burger,
.mobile-nav-backdrop,
.mobile-nav-panel { display: none; }

.phone-icon-mobile { white-space: nowrap; }

@media (max-width: 768px) {
    /* Шапка: бургер, куда нужно, иконка телефона вместо номера */
    .header, .header-simple { padding: 8px 0; }
    .nav, .nav-simple { gap: 8px; flex-wrap: nowrap; }
    .nav .nav-menu, .nav-simple .nav-menu { display: none !important; }

    .nav-burger {
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 5px;
        width: 44px;
        height: 44px;
        padding: 0;
        border: 0;
        border-radius: 10px;
        background: transparent;
        cursor: pointer;
        flex: 0 0 auto;
        -webkit-tap-highlight-color: transparent;
    }
    .nav-burger span {
        display: block;
        width: 22px;
        height: 2px;
        border-radius: 2px;
        background: #fff;
        transition: transform .2s, opacity .2s;
    }
    .header-simple .nav-burger span { background: #222; }
    .nav-burger:focus-visible { outline: 2px solid #667eea; outline-offset: 2px; }

    /* На страницах без логотипа бургер слева, на страницах с логотипом справа */
    .nav-no-logo .nav-burger { order: -1; }
    .nav-has-logo .nav-burger { order: 3; }
    .nav-has-logo .nav-contact { margin-left: auto; }
    .nav-no-logo .nav-contact { margin-left: auto; }

    .nav-contact { gap: 8px; flex: 0 0 auto; }
    .logo { font-size: 18px; white-space: nowrap; }

    /* Номер телефона превращается в круглую иконку 44x44 */
    .phone-icon-mobile {
        font-size: 0 !important;
        width: 44px;
        height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 50%;
        background: rgba(102, 126, 234, .18);
        flex: 0 0 auto;
    }
    .phone-icon-mobile::before {
        content: "\260E";
        font-size: 20px;
        line-height: 1;
        color: #fff;
    }
    .header-simple .phone-icon-mobile::before { color: #5b4bc4; }

    /* Профиль в шапке: только аватар и «Выйти», без длинной почты */
    .user-profile-wrapper { padding: 4px !important; }
    .user-profile-wrapper .user-info { display: none !important; }
    .logout-btn-small {
        margin-left: 0 !important;
        padding: 0 12px !important;
        min-height: 44px;
        font-size: 13px !important;
    }
    .auth-btn { padding: 0 18px !important; min-height: 44px; font-size: 14px !important; }

    /* Выезжающая панель */
    .mobile-nav-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, .55);
        opacity: 0;
        pointer-events: none;
        transition: opacity .25s;
        z-index: 2000;
    }
    .mobile-nav-panel {
        display: flex;
        flex-direction: column;
        position: fixed;
        top: 0; bottom: 0; left: 0;
        width: min(84vw, 320px);
        background: #fff;
        color: #222;
        transform: translateX(-102%);
        visibility: hidden;
        transition: transform .25s ease, visibility 0s .25s;
        z-index: 2001;
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: 4px 0 24px rgba(0, 0, 0, .25);
        overflow-y: auto;
    }
    html.nav-open { overflow: hidden; }
    html.nav-open body { overflow: hidden; }
    html.nav-open .mobile-nav-backdrop { opacity: 1; pointer-events: auto; }
    html.nav-open .mobile-nav-panel {
        transform: none;
        visibility: visible;
        transition: transform .25s ease, visibility 0s;
    }
    .mobile-nav-head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 12px 8px 12px 20px;
        border-bottom: 1px solid #eee;
        font-weight: 700;
        font-size: 18px;
    }
    .mobile-nav-close {
        width: 44px; height: 44px;
        border: 0; background: transparent;
        font-size: 30px; line-height: 1; color: #444; cursor: pointer;
    }
    .mobile-nav-links { display: flex; flex-direction: column; padding: 8px 0; }
    .mobile-nav-links a {
        display: flex;
        align-items: center;
        min-height: 52px;
        padding: 0 20px;
        font-size: 17px;
        color: #222;
        border-bottom: 1px solid #f2f2f2;
    }
    .mobile-nav-links a:active { background: #f5f5f7; }
    .mobile-nav-call {
        margin: 16px 20px 20px;
        min-height: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        background: #667eea;
        color: #fff !important;
        font-weight: 600;
    }
}

/* ---------- 3. Страница квартиры: нижняя панель бронирования ---------- */
.mobile-book-bar { display: none; }

@media (max-width: 768px) {
    .mobile-book-bar {
        position: fixed;
        left: 0; right: 0; bottom: 0;
        z-index: 900;
        display: none;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
        background: #fff;
        border-top: 1px solid #e6e6ea;
        box-shadow: 0 -4px 18px rgba(0, 0, 0, .12);
        transition: transform .2s ease;
    }
    .mobile-book-bar[data-ready="1"] { display: flex; }
    .mobile-book-bar.is-hidden { transform: translateY(110%); pointer-events: none; }
    .mobile-book-price { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
    .mobile-book-price strong { font-size: 20px; color: #1f1f24; white-space: nowrap; }
    .mobile-book-from, .mobile-book-per { font-size: 14px; color: #5f5f68; }
    .mobile-book-btn {
        min-height: 48px;
        padding: 0 22px;
        border: 0;
        border-radius: 12px;
        background: #d6393f;
        color: #fff;
        font-size: 16px;
        font-weight: 700;
        cursor: pointer;
        flex: 0 0 auto;
    }
    .mobile-book-btn:active { filter: brightness(.92); }
    /* Чтобы панель не закрывала футер и последний блок */
    body.has-book-bar { padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
}

/* ---------- 4. Индикатор «идёт запрос» на кнопках ---------- */
button.is-busy, .is-busy[type="submit"] {
    position: relative;
    cursor: progress;
    opacity: .75;
}
button.is-busy::after {
    content: "";
    display: inline-block;
    width: 14px; height: 14px;
    margin-left: 10px;
    vertical-align: -2px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: mobile-spin .7s linear infinite;
}
@keyframes mobile-spin { to { transform: rotate(360deg); } }

/* ---------- 5. Картинки: пока грузятся или не загрузились ---------- */
img.is-broken { visibility: hidden; }
.gallery-main-photo, .side-photo { background: #e9e9ee; }
.gallery-main-photo img, .side-photo img { display: block; }
.apartment-card img, .card-image img { background: #e9e9ee; aspect-ratio: 4 / 3; object-fit: cover; }
#lightbox-image { touch-action: pan-y; -webkit-user-drag: none; user-select: none; }
#lightbox-image.is-broken { visibility: hidden; }

/* ---------- 6. Тач-удобство ---------- */
@media (max-width: 768px) {
    /* 16px в полях: иначе iOS Safari увеличивает страницу при фокусе */
    input:not([type="checkbox"]):not([type="radio"]),
    select, textarea {
        font-size: 16px !important;
        min-height: 44px;
    }
    textarea { min-height: 96px; }
    button, .btn, .book-button, .auth-submit-btn, .all-photos-btn {
        min-height: 44px;
    }
    input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; }
    .consent-check { font-size: 14px; line-height: 1.45; }
    .consent-check input { margin-top: 0; }

    a, button, [role="button"] { -webkit-tap-highlight-color: rgba(102, 126, 234, .15); }
    button, [role="button"], .calendar-day, .ac-cell { touch-action: manipulation; }

    /* Модалки: помещаются в экран с открытой клавиатурой и прокручиваются */
    .auth-modal.active { align-items: flex-start; overflow-y: auto; padding: 12px 0; }
    .auth-modal .auth-modal-content {
        width: calc(100% - 24px);
        max-width: 450px;
        padding: 28px 20px 24px;
        margin: auto;
        max-height: calc(100dvh - 24px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .pc-overlay.open { align-items: flex-start; overflow-y: auto; padding: 12px; }
    .pc-box {
        margin: auto;
        max-height: calc(100dvh - 24px);
        overflow-y: auto;
        padding: 24px 18px 20px;
    }
    .pc-close, .auth-modal-close { width: 44px; height: 44px; }
    .pc-code { font-size: 22px !important; letter-spacing: .25em; text-align: center; }

    /* Календарь в выпадашке: на всю ширину и без залипания прокрутки */
    .calendar-dropdown.active { max-width: 100vw; overscroll-behavior: contain; }

    /* Шахматка админа: горизонтальная прокрутка внутри блока, страница не уезжает */
    .ac-scroll {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
        touch-action: pan-x pan-y;
    }

    /* Ничего не вылезает за экран */
    .container, main, section { max-width: 100%; }
    table { display: block; overflow-x: auto; max-width: 100%; }
    pre, code { white-space: pre-wrap; word-break: break-word; }
    p, h1, h2, h3, h4, li, td, a { overflow-wrap: anywhere; }

    /* Лайтбокс: стрелки крупнее, миниатюры не мешают свайпу */
    .lightbox-close { width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; }
    .lightbox-arrow { width: 48px; height: 48px; }
    .lightbox-thumbnails { padding-bottom: env(safe-area-inset-bottom); }
}

/* Админка: карточки статистики по бронированиям не должны быть шире экрана */
@media (max-width: 480px) {
    #bookings-stats { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px !important; }
    #bookings-stats > div { padding: 14px 10px !important; min-width: 0; overflow-wrap: anywhere; }
    #bookings-stats > div > div:first-child { font-size: 24px !important; }
}

/* Админка: карточка брони на телефоне складывается в одну колонку, кнопки под данными */
@media (max-width: 600px) {
    #admin-bookings-list div[style*="grid-template-columns: 1fr auto"] { grid-template-columns: minmax(0, 1fr) !important; }
    #admin-bookings-list div[style*="min-width: 150px"] { min-width: 0 !important; }
}
