/* ===== УЛУЧШЕННЫЕ МОДАЛЬНЫЕ ОКНА ===== */

.modal_div {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90%;
    max-width: 600px;
    max-height: 90vh;           /* Ограничение высоты */
    display: flex;
    flex-direction: column;      /* Чтобы header и footer не сжимались */
    background: linear-gradient(145deg, #2a1e15, #1f150e);
    border: 2px solid #b8860b;
    border-radius: 24px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.8), 0 0 0 1px #d4af37 inset;
    z-index: 10000;
    color: #fff;
    overflow: hidden;            /* Скрываем лишнее, прокрутка будет внутри modal-body */
}

.modal_div.show {
    display: flex;
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
    pointer-events: auto;
}

.modal-header {
    flex-shrink: 0;              /* Запрещаем сжиматься */
    padding: 20px 25px 10px;
    border-bottom: 2px solid #b8860b;
    background: rgba(0,0,0,0.3);
}

.modal-header h3 {
    margin: 0;
    color: #ffd966;
    font-size: 26px;
    font-weight: 700;
    text-align: left;
    border-bottom: none;
    padding-bottom: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-header h3::before {
    content: '📜';
    font-size: 28px;
    filter: drop-shadow(0 0 5px gold);
}

.modal-body {
    position: relative;
    flex: 1 1 auto;
    padding: 20px 25px;
    max-height: 20vh;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
}

.modal-body .modal-scrollable {
    overflow-y: auto;             /* прокрутка только для этого блока */
    flex: 1;
    padding-right: 5px;           /* небольшой отступ для скроллбара */
}

/* Стилизация скроллбара для WebKit (Chrome, Safari) */
.modal-body::-webkit-scrollbar {
    width: 8px;
}
.modal-body::-webkit-scrollbar-track {
    background: #2b1e0e;
}
.modal-body::-webkit-scrollbar-thumb {
    background: #b8860b;
    border-radius: 4px;
}

.modal-footer {
    flex-shrink: 0;
    padding: 15px 25px 20px;
    background: rgba(0,0,0,0.3);
    border-top: 2px solid #8b5a2b;
}

/* Кнопки внутри модалок */
.modal-scrollable button{
    min-width: 29%;
    margin: 0 2%;
}
.modal-button-primary,
.modal-button-secondary {
    padding: 12px 30px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 40px;
    transition: all 0.2s;
    border: 2px solid transparent;
}

.modal-button-primary {
    background: linear-gradient(145deg, #b8860b, #8b5a2b);
    color: #1f140c;
    box-shadow: 0 4px 0 #5a3e2b;
}

.modal-button-primary:hover {
    background: linear-gradient(145deg, #8b5a2b, #b8860b);
    transform: translateY(-2px);
    box-shadow: 0 6px 0 #5a3e2b, 0 0 15px gold;
}

.modal-button-secondary {
    background: linear-gradient(145deg, #3f2e22, #2b1e0e);
    border: 2px solid #b8860b;
    color: #ffd966;
}

.modal-button-secondary:hover {
    background: #b8860b;
    color: #1f140c;
    border-color: #ffd700;
}

/* Поля ввода */
.modal-body input[type="text"],
.modal-body input[type="number"],
.modal-body input[type="email"],
.modal-body input[type="password"],
.modal-body textarea,
.modal-body select {
    width: 100%;
    background: #3f2e22;
    border: 2px solid #8b5a2b;
    border-radius: 12px;
    padding: 12px 16px;
    color: #fff;
    font-size: 16px;
    transition: border-color 0.2s, box-shadow 0.2s;
    margin-bottom: 15px;
}

.modal-body input:focus,
.modal-body textarea:focus,
.modal-body select:focus {
    border-color: #ffd700;
    box-shadow: 0 0 0 3px rgba(255,215,0,0.2);
    outline: none;
}

.modal-body select {
    appearance: none;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='%23ffd966'><path d='M7 10l5 5 5-5z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 15px center;
    padding-right: 40px;
}

.modal-body label {
    display: block;
    color: #ffd966;
    font-weight: 600;
    margin-bottom: 5px;
    font-size: 15px;
}

.modal-body .form-group {
    margin-bottom: 20px;
}

/* Чекбоксы в стиле переключателей */
.modal-body .checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #f0e6d2;
    cursor: pointer;
    margin-bottom: 10px;
}

.modal-body .checkbox-label input[type="checkbox"] {
    width: 20px;
    height: 20px;
    accent-color: #b8860b;
}

/* Поисковые результаты внутри модалок */
.modal-body .search-results {
    position: absolute;
    background: #2a1e15;
    border: 2px solid #b8860b;
    border-radius: 10px;
    max-height: 200px;
    overflow-y: auto;
    z-index: 10001;
    width: calc(100% - 50px);
    box-shadow: 0 5px 20px #000;
}

.modal-body .search-result-item {
    padding: 10px 15px;
    cursor: pointer;
    border-bottom: 1px solid #3f2e22;
    color: #fff;
    transition: background 0.2s;
}

.modal-body .search-result-item:hover {
    background: #b8860b;
    color: #1f140c;
}

/* Адаптация для мобильных */
@media (max-width: 768px) {
    .modal_div {
        width: 95%;
        max-width: none;
        border-radius: 20px;
    }

    .modal-header h3 {
        font-size: 22px;
    }

    .modal-body {
        padding: 20px;
    }

    .modal-footer {
        flex-direction: column;
        padding: 15px 20px;
    }

    .modal-button-primary,
    .modal-button-secondary {
        width: 100%;
        margin: 5px 0;
    }

    .modal-body input,
    .modal-body textarea,
    .modal-body select {
        font-size: 16px; /* предотвращает масштабирование на iOS */
    }
}

/* Кнопка закрытия */
.modal_close {
    position: absolute;
    top: 15px;
    right: 15px;
    width: 36px;
    height: 36px;
    background: rgba(0,0,0,0.5);
    border: 2px solid #b8860b;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    z-index: 10;
}

.modal_close:hover {
    background: #b8860b;
}

.closeButton {
    width: 20px;
    height: 20px;
    position: relative;
}

.closeButton span {
    position: absolute;
    width: 100%;
    height: 2px;
    background-color: #ffd966;
    top: 50%;
    left: 0;
}

.closeButton span:nth-child(1) {
    transform: rotate(45deg);
}

.closeButton span:nth-child(2) {
    transform: rotate(-45deg);
}

.modal_close:hover .closeButton span {
    background-color: #1f140c;
}

/* Оверлей */
#overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.7);
    z-index: 9999;
    display: none;
    opacity: 0;
    transition: opacity 0.2s ease;
    pointer-events: none;
}

#overlay.show {
    opacity: 1;
    pointer-events: auto;;
    display: block;
}

/* ===== МОБИЛЬНЫЕ МОДАЛЬНЫЕ ОКНА: ПОЛНЫЙ ЭКРАН =====
 *
 * На телефоне окно авторизации (и любые другие модалки) раньше оставалось
 * плавающей карточкой 95% × 85vh по центру. При нажатии на поле ввода
 * открывается экранная клавиатура: видимая область схлопывается до ~45%
 * экрана, а карточка стоит по центру макета — поле уезжало под клавиатуру,
 * и «ничего не видно».
 *
 * Здесь модалка становится полноэкранным листом:
 *   • во всю ширину и высоту, без внешних отступов и скруглений;
 *   • прокручивается сама (.modal_div  overflow-y: auto), поэтому места для
 *     скролла больше и поле всегда можно поднять над клавиатурой;
 *   • шапка и подвал «прилипают» (position: sticky), кнопки «Войти/Отмена»
 *     всегда на виду;
 *   • --pm-vh / --pm-offset выставляет p/js/portal_modal_mobile.js по
 *     visualViewport — на iOS, где клавиатура не меняет layout viewport,
 *     лист ужимается до реально видимой области.
 *
 * Селекторы с body нужны, чтобы перебить p/css/mobile_styles.css, который
 * грузится позже и тоже стилизует .modal_div в этом же диапазоне ширины.
 * display не трогаем: openModal() ставит inline display:block, а closeModal()
 * — inline display:none; раскладка на sticky работает и с block.
 */
@media (max-width: 992px), (orientation: landscape) and (max-height: 520px) {
    body .modal_div {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: auto;
        width: 100%;
        max-width: none;
        margin: 0;
        height: 100%;
        height: 100dvh;
        height: var(--pm-vh, 100dvh);
        max-height: none;
        transform: translateY(var(--pm-offset, 0));
        border: 0;
        border-radius: 0;
        box-shadow: none;
        background: linear-gradient(180deg, #241a12 0%, #171009 55%, #120c07 100%);
        overflow-y: auto;
        overflow-x: hidden;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
        padding: 0;
        z-index: 10000;
    }

    body .modal_div.show {
        transform: translateY(var(--pm-offset, 0));
    }

    /* Шапка прилипает к верху и не даёт прокрутить название. */
    body .modal_div .modal-header {
        position: sticky;
        top: 0;
        z-index: 6;
        padding: calc(12px + env(safe-area-inset-top)) 58px 10px 14px;
        border-bottom: 1px solid rgba(184, 134, 11, .55);
        background: rgba(26, 18, 12, .97);
        box-shadow: 0 6px 14px rgba(0, 0, 0, .35);
    }

    body .modal_div .modal-header h3 {
        font-size: 18px;
        line-height: 1.25;
        gap: 8px;
    }

    body .modal_div .modal-header h3::before {
        font-size: 20px;
    }

    /* Тело — единственное, что реально прокручивается вместе с листом. */
    body .modal_div .modal-body {
        flex: 1 1 auto;
        max-height: none;
        overflow: visible;
        padding: 14px 14px 4px;
    }

    body .modal_div .modal-body .form-group {
        margin-bottom: 14px;
    }

    body .modal_div .modal-body label {
        margin-bottom: 6px;
        font-size: 12.5px;
        letter-spacing: .02em;
        text-transform: none;
    }

    body .modal_div .modal-body input[type="text"],
    body .modal_div .modal-body input[type="number"],
    body .modal_div .modal-body input[type="email"],
    body .modal_div .modal-body input[type="password"],
    body .modal_div .modal-body input[type="search"],
    body .modal_div .modal-body textarea,
    body .modal_div .modal-body select {
        min-height: 48px;
        padding: 12px 14px;
        margin-bottom: 0;
        font-size: 16px; /* iOS не масштабирует страницу при фокусе */
        scroll-margin: 96px 0 132px; /* запас под шапку и подвал при скролле */
    }

    /* Подвал прилипает к низу: кнопки всегда под рукой. */
    body .modal_div .modal-footer {
        position: sticky;
        bottom: 0;
        z-index: 6;
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
        border-top: 1px solid rgba(184, 134, 11, .55);
        background: rgba(20, 14, 9, .97);
        box-shadow: 0 -6px 14px rgba(0, 0, 0, .35);
    }

    body .modal_div .modal-button-primary,
    body .modal_div .modal-button-secondary {
        width: 100%;
        margin: 0;
        min-height: 46px;
        padding: 11px 16px;
        font-size: 15px;
    }

    /* Крестик не уезжает при прокрутке листа. */
    body .modal_div .modal_close {
        position: fixed;
        top: calc(6px + env(safe-area-inset-top));
        right: 6px;
        width: 42px;
        height: 42px;
        z-index: 12;
    }

    body .modal_div .modal_close .closeButton {
        width: 18px;
        height: 18px;
    }

    /* Затемнение на всю видимую область. */
    body #overlay {
        height: 100%;
        height: 100dvh;
        height: var(--pm-vh, 100dvh);
    }
}