/**
 * Единые стили модальных окон NFS: токены, подложка с blur, Bootstrap и кастомные оверлеи.
 * Подключать после bootstrap.min.css (и после design-tokens на фронте — опционально).
 */

:root {
    --nfs-modal-max-width: 480px;
    --nfs-modal-max-width-wide: min(800px, calc(100vw - 2rem));
    --nfs-modal-max-width-compare: min(1200px, 94vw);
    --nfs-modal-max-height: min(90vh, 720px);
    --nfs-modal-radius: 1.5rem;
    --nfs-modal-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.15);
    --nfs-modal-backdrop-color: rgba(15, 23, 42, 0.52);
    --nfs-modal-backdrop-blur: 10px;
    --nfs-modal-z: 1056;
    --nfs-modal-content-bg: #ffffff;
    --nfs-modal-border-subtle: #e2e8f0;
}

/* Блокировка скролла страницы для кастомных оверлеев (не Bootstrap) */
html.nfs-scroll-lock,
html.nfs-scroll-lock body {
    overflow: hidden !important;
}

/* Усиление: фон не скроллится при открытой Bootstrap-модалке (html + body) */
body.modal-open {
    overflow: hidden !important;
}

html:has(.modal.show) {
    overflow: hidden !important;
}

/* Подложка Bootstrap 5 */
.modal-backdrop {
    --bs-backdrop-bg: var(--nfs-modal-backdrop-color);
    --bs-backdrop-opacity: 1;
    backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
    -webkit-backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
}

/* Диалог: стандартная и широкая ширина, без полноэкранного и медиа-браузера */
.modal .modal-dialog:not(.modal-fullscreen) {
    max-width: min(var(--nfs-modal-max-width), calc(100vw - 2rem));
    margin-left: auto;
    margin-right: auto;
}

.modal .modal-dialog.modal-lg:not(.modal-fullscreen),
.modal .modal-dialog.modal-xl:not(.modal-fullscreen) {
    max-width: min(var(--nfs-modal-max-width-wide), calc(100vw - 2rem));
}

/*
 * Выбор марки в hero на главной: нужна ширина больше общего лимита modal-xl (800px).
 * Иначе правило выше перебивает home.css (nfs-modals подключается позже).
 */
.modal.hero-brand-modal .modal-dialog.modal-xl,
.modal.hero-model-modal .modal-dialog.modal-xl {
    max-width: min(1596px, calc(100vw - 1.5rem));
    width: 100%;
}

/* Медиа-браузер: почти на весь экран (перекрывает общие лимиты) */
.modal.media-browser-modal .modal-dialog.modal-xl,
.modal.media-browser-modal .modal-dialog {
    max-width: 95vw !important;
    width: 95vw;
    height: 90vh;
    margin: 5vh auto;
}

.modal .modal-content {
    max-height: var(--nfs-modal-max-height);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border-radius: var(--nfs-modal-radius);
    border: none;
    box-shadow: var(--nfs-modal-shadow);
    background: var(--nfs-modal-content-bg);
}

.modal .modal-header {
    flex-shrink: 0;
    border-bottom-color: var(--nfs-modal-border-subtle);
}

.modal .modal-footer {
    flex-shrink: 0;
    border-top-color: var(--nfs-modal-border-subtle);
}

.modal .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Кастомные модалки (оверлей без Bootstrap) */
.nfs-native-modal {
    position: fixed;
    inset: 0;
    z-index: var(--nfs-modal-z);
    display: none !important;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    box-sizing: border-box;
}

.nfs-native-modal.nfs-native-modal--open,
.nfs-native-modal[style*="display: flex"],
.nfs-native-modal.show {
    display: flex !important;
}

.nfs-native-modal__backdrop {
    position: absolute;
    inset: 0;
    background: var(--nfs-modal-backdrop-color);
    backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
    -webkit-backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
}

.nfs-native-modal__panel {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: min(var(--nfs-modal-max-width), calc(100vw - 2rem));
    max-height: var(--nfs-modal-max-height);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--nfs-modal-content-bg);
    border-radius: var(--nfs-modal-radius);
    box-shadow: var(--nfs-modal-shadow);
    animation: nfsModalPanelIn 0.25s ease-out;
}

/* Широкая панель (обмен и т.п.) */
.nfs-native-modal__panel--wide {
    max-width: min(var(--nfs-modal-max-width-wide), calc(100vw - 2rem));
}

.nfs-native-modal__header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--nfs-modal-border-subtle);
}

.nfs-native-modal__header h3 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    color: #0f172a;
}

.nfs-native-modal__body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 1.5rem;
}

.nfs-native-modal__footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--nfs-modal-border-subtle);
}

@keyframes nfsModalPanelIn {
    from {
        opacity: 0;
        transform: translateY(-12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Режим «всегда flex», показ через opacity/visibility (история цен) */
.nfs-native-modal.nfs-native-modal--visibility-toggle {
    display: flex !important;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

.nfs-native-modal.nfs-native-modal--visibility-toggle.nfs-native-modal--open {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Диалог сравнения (каталог): отдельная разметка */
.compare-dialog {
    position: fixed;
    inset: 0;
    z-index: var(--nfs-modal-z);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    box-sizing: border-box;
}

.compare-dialog.compare-dialog--open {
    display: flex;
}

.compare-dialog__backdrop {
    position: absolute;
    inset: 0;
    background: var(--nfs-modal-backdrop-color);
    backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
    -webkit-backdrop-filter: blur(var(--nfs-modal-backdrop-blur));
}

.compare-dialog__panel {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: var(--nfs-modal-max-width-compare);
    max-height: var(--nfs-modal-max-height);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--nfs-modal-content-bg);
    border-radius: var(--nfs-modal-radius);
    box-shadow: var(--nfs-modal-shadow);
    animation: nfsModalPanelIn 0.25s ease-out;
}

.compare-dialog__header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.5rem;
    border-bottom: 1px solid var(--nfs-modal-border-subtle);
}

.compare-dialog__header h3 {
    font-size: 1.125rem;
    font-weight: 600;
    margin: 0;
    color: #0f172a;
}

.compare-dialog__body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 1.5rem;
}

.compare-dialog__close {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
}

.compare-dialog__close:hover {
    background: #f1f5f9;
    color: #0f172a;
}

/* -------------------------------------------------------------------------- */
/* Мобильная версия: модалка на весь экран (как отдельная страница)           */
/* До breakpoint md Bootstrap (768px) — телефоны и узкие планшеты в портрете */
/* -------------------------------------------------------------------------- */
@media (max-width: 767.98px) {
    /* --- Bootstrap Modal --- */
    .modal {
        padding: 0 !important;
        /* Скролл только внутри .modal-body, а не «карточки» по центру */
        overflow: hidden !important;
    }

    .modal .modal-dialog:not(.modal-fullscreen) {
        max-width: 100% !important;
        width: 100% !important;
        margin: 0 !important;
        height: 100%;
        min-height: 100vh;
        min-height: 100dvh;
    }

    .modal .modal-dialog.modal-dialog-centered {
        min-height: 100vh !important;
        min-height: 100dvh !important;
        display: flex !important;
        align-items: stretch !important;
    }

    .modal .modal-content {
        max-height: none !important;
        height: 100%;
        min-height: 100%;
        border-radius: 0 !important;
        box-shadow: none;
    }

    /* Медиа-браузер: на мобильном тоже на весь экран */
    .modal.media-browser-modal .modal-dialog,
    .modal.media-browser-modal .modal-dialog.modal-xl {
        max-width: 100% !important;
        width: 100% !important;
        height: 100% !important;
        min-height: 100vh !important;
        min-height: 100dvh !important;
        margin: 0 !important;
    }

    /* --- Кастомные nfs-native-modal --- */
    .nfs-native-modal {
        padding: 0;
        align-items: stretch;
        justify-content: stretch;
    }

    .nfs-native-modal__panel,
    .nfs-native-modal__panel--wide {
        max-width: none !important;
        width: 100%;
        max-height: none !important;
        height: 100%;
        min-height: 100vh;
        min-height: 100dvh;
        border-radius: 0 !important;
        box-shadow: none;
    }

    /* --- Диалог сравнения --- */
    .compare-dialog {
        padding: 0;
        align-items: stretch;
        justify-content: stretch;
    }

    .compare-dialog__panel {
        max-width: none !important;
        width: 100%;
        max-height: none !important;
        height: 100%;
        min-height: 100vh;
        min-height: 100dvh;
        border-radius: 0 !important;
        box-shadow: none;
    }
}
