/**
 * ND54 — универсальная адаптация под телефоны, планшеты, десктоп и все основные браузеры.
 * Подключать ПОСЛЕДНИМ (после nd54-layout-unified.css).
 */

/* --- Safe-area: только оболочки, без двойного inset на body --- */
body.nd54-cabinet,
body.nd54-druz-dashboard,
body.nd54-press-dashboard,
body.nd54-zayavitel-dashboard,
body.nd54-cabinet--ruk,
body.nd54-cabinet--druz,
body.nd54-cabinet--press,
body.nd54-cabinet--candidate {
    padding-left: 0 !important;
    padding-right: 0 !important;
}

/* --- Фиксированная шапка: запас под контент --- */
html.nd54-cab-fixed-header body.nd54-cabinet--press {
    --nd54-cab-header-h: 168px;
}

html.nd54-cab-fixed-header body.nd54-cabinet--candidate {
    --nd54-cab-header-h: 220px;
}

@media (max-width: 767.98px) {
    html.nd54-cab-fixed-header body.nd54-cabinet--press {
        --nd54-cab-header-h: 196px;
    }

    html.nd54-cab-fixed-header body.nd54-cabinet--candidate {
        --nd54-cab-header-h: 268px;
    }
}

/* --- Нижняя навигация: много вкладок — горизонтальный скролл --- */
@media (max-width: 991.98px) {
    .nd54-cab-mobile-nav-inner,
    .ruk-mobile-nav-inner {
        justify-content: flex-start;
        max-width: none;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 2px;
        padding-bottom: 2px;
    }

    .nd54-cab-mobile-nav-inner::-webkit-scrollbar,
    .ruk-mobile-nav-inner::-webkit-scrollbar {
        display: none;
    }

    .nd54-cab-mobile-nav-btn,
    .ruk-mobile-nav-btn {
        flex: 0 0 auto;
        min-width: 3.35rem;
        max-width: 5.5rem;
        padding-left: 6px;
        padding-right: 6px;
    }

    .nd54-cab-mobile-nav-btn span,
    .ruk-mobile-nav-btn span {
        max-width: 5rem;
        font-size: 0.65rem;
    }
}

/* --- Таблицы: заголовки компактно --- */
@media (max-width: 767.98px) {
    .table-modern td small,
    .table td .small {
        display: block;
        max-width: 100%;
    }
}

/* --- Чат: колонки друг под другом на узком экране --- */
.modal-dialog.modal-xl,
.modal-dialog.modal-lg {
    max-width: min(calc(100vw - 1rem), var(--bs-modal-width, 800px));
}

.modal-fullscreen-sm-down {
    max-width: 100%;
}

.ocr-modal-overlay,
[class*="ocr-modal"],
.custom-modal--open,
#taskModalCustom {
    max-width: 100vw;
    max-height: min(100dvh, calc(var(--nd54-vh, 1vh) * 100));
    z-index: var(--nd54-z-custom-modal, 10090) !important;
}

.modal-body,
.modal-dialog-scrollable .modal-body {
    max-height: min(78dvh, calc(var(--nd54-vh, 1vh) * 78));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* --- Хабы, фильтры, чипы — горизонтальный скролл вместо обрезания --- */
.nd54-task-hub-inner,
.nd54-task-hub,
.nd54-druz-task-strip,
.nd54-hub-pill,
.filter-bar,
.readiness-indicator,
.executive-strip,
.nd54-ruk-workspace-divider,
.press-home-grid {
    max-width: 100%;
    min-width: 0;
}

.nd54-task-hub-inner,
.nd54-druz-task-strip {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    gap: 0.5rem;
    padding-bottom: 4px;
    scrollbar-width: thin;
}

.nd54-hub-pill,
.nd54-druz-task-chip {
    flex-shrink: 0;
}

.preset-btn,
.nd54-hub-pill,
.nd54-task-hub-title {
    overflow-wrap: normal;
    word-break: normal;
    white-space: nowrap;
    max-width: none;
}

@media (max-width: 991.98px) {
    .nd54-task-hub-inner,
    .nd54-druz-task-strip {
        flex-wrap: wrap;
        overflow-x: visible;
        overflow-y: visible;
        row-gap: 0.55rem;
    }

    .nd54-task-hub-title {
        flex: 0 0 100%;
        width: 100%;
        margin-right: 0;
    }
}

/* --- Таблицы: горизонтальный скролл вместо разрыва по буквам --- */
@media (max-width: 991.98px) {
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }

    .table-responsive > .table,
    .table-responsive > table,
    .table-responsive .table-modern,
    #appealsTable {
        min-width: 720px;
        width: max-content;
        max-width: none;
        overflow: visible;
    }

    body.nd54-cabinet .table th,
    body.nd54-cabinet .table td,
    body.nd54-cabinet .table-modern th,
    body.nd54-cabinet .table-modern td {
        word-break: normal;
        overflow-wrap: normal;
        word-wrap: normal;
        white-space: nowrap;
    }

    body.nd54-cabinet .table tbody td:nth-child(4),
    body.nd54-cabinet .table-modern tbody td:nth-child(4) {
        white-space: normal;
        max-width: 220px;
        overflow-wrap: break-word;
    }
}

@media (max-width: 767.98px) {
    .table thead th {
        white-space: nowrap;
    }

    .table tbody td {
        white-space: nowrap;
        overflow-wrap: normal;
        word-break: normal;
        vertical-align: top;
    }
@media (max-width: 767.98px) {
    .chat-layout-row,
    .chat-container .row {
        flex-direction: column !important;
    }

    .chat-users-list {
        flex: none !important;
        width: 100% !important;
        max-width: 100% !important;
        max-height: min(40vh, 280px);
        border-right: none !important;
        border-bottom: 1px solid var(--ruk-border, #dee2e6);
    }

    .chat-messages-area {
        min-height: 280px;
        width: 100% !important;
    }
}

/* --- Сетки KPI / карточки --- */
.stats-grid,
.quick-actions-grid,
.exec-grid,
.premium-insights,
.press-home-grid,
.kpi-row {
    width: 100%;
    max-width: 100%;
}

@media (max-width: 575.98px) {
    .stats-grid,
    .quick-actions-grid {
        grid-template-columns: 1fr !important;
    }

    .kpi-card,
    .quick-action-item,
    .stat-card {
        min-width: 0;
    }
}

/* --- Кабинет заявителя --- */
body.nd54-zayavitel-dashboard {
    align-items: stretch !important;
}

body.nd54-zayavitel-dashboard .dashboard {
    max-width: min(800px, 100%);
    padding-left: var(--nd54-shell-pad-x, max(12px, env(safe-area-inset-left)));
    padding-right: var(--nd54-shell-pad-x-right, max(12px, env(safe-area-inset-right)));
    padding-bottom: var(--nd54-shell-pad-bottom, max(16px, env(safe-area-inset-bottom)));
}

body.nd54-zayavitel-dashboard .profile-header {
    flex-direction: column;
    align-items: flex-start !important;
}

body.nd54-zayavitel-dashboard .appeal-card,
body.nd54-zayavitel-dashboard .card {
    overflow: hidden;
    max-width: 100%;
}

/* --- Гостевая карта --- */
body.nd54-guest-map,
.guest-map-shell {
    min-height: 100dvh;
    min-height: -webkit-fill-available;
}

.guest-bottom-panel,
.guest-sheet {
    max-height: min(72dvh, calc(var(--nd54-vh, 1vh) * 72));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* --- Mafia hideout / прочие standalone --- */
html.mafia-hideout-page,
html.mafia-hideout-page body.mafia-hideout {
    overflow-x: hidden;
    overflow-y: auto;
    height: auto;
    min-height: 100%;
}

body.mafia-hideout .container {
    max-width: min(520px, 100%);
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(1.5rem, env(safe-area-inset-bottom, 0px));
    overflow: visible;
}

/* --- Алерты, тосты, выпадашки --- */
.alert,
.toast,
.dropdown-menu {
    max-width: min(100vw - 1.5rem, 480px);
    overflow-wrap: anywhere;
}

.toast-container,
.nd54-toast-stack {
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}

@media (max-width: 991.98px) {
    .toast-container,
    .nd54-toast-stack {
        bottom: calc(var(--nd54-mobile-nav-h, 72px) + env(safe-area-inset-bottom, 0px) + 12px) !important;
    }
}

/* --- Длинные URL, коды, email --- */
a,
.text-break,
.user-select-all,
code,
pre {
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* --- Touch targets (Apple HIG / Material) --- */
@media (hover: none) and (pointer: coarse) {
    .btn,
    .form-control,
    .form-select,
    .nav-link,
    .main-nav-item,
    .sub-nav-tab,
    .workspace-tab,
    .nd54-cab-mobile-nav-btn,
    .ruk-mobile-nav-btn,
    .notification-bell {
        min-height: 44px;
    }

    .btn-sm {
        min-height: 40px;
        min-width: 40px;
    }
}

/* --- Скрыть дублирующий select при нижней навигации --- */
@media (max-width: 991.98px) {
    .nd54-druz-dashboard.nd54-cabinet .mobile-nav-select {
        display: none !important;
    }
}

/* --- Публичный сайт: меню не вылезает за экран --- */
@media (max-width: 991.98px) {
    .menu,
    .nd54-site-nav,
    nav.navbar {
        max-width: 100vw;
    }

    .menu ul,
    .navbar-nav {
        flex-wrap: wrap;
        max-width: 100%;
    }
}

/* --- Логин-портал --- */
.nd54-login-portal,
body.nd54-login-page {
    min-height: 100dvh;
    min-height: -webkit-fill-available;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
}

/* --- iOS 100vh fix --- */
@supports (-webkit-touch-callout: none) {
    .modal,
    .modal-dialog {
        max-height: -webkit-fill-available;
    }
}

/* --- Очень узкие экраны (320px) --- */
@media (max-width: 359.98px) {
    .dashboard-wrapper,
    .dashboard-container,
    .press-shell {
        padding-left: 8px !important;
        padding-right: 8px !important;
    }

    .main-header,
    .profile-header {
        padding: 12px !important;
    }

    .nd54-cab-toolbar,
    .ruk-toolbar {
        flex-direction: column;
        align-items: stretch !important;
    }

    .nd54-cab-global-search,
    .ruk-global-search {
        width: 100%;
        flex: 1 1 100%;
    }
}

/* --- Landscape phone: модалки и шапка --- */
@media (max-height: 500px) and (orientation: landscape) {
    .modal-body {
        max-height: 55dvh;
    }

    html.nd54-cab-fixed-header .nd54-cab-sticky-zone .main-header .header-tagline,
    html.nd54-cab-fixed-header .nd54-cab-sticky-zone .main-header small {
        display: none !important;
    }
}
