/**
 * Мобильная адаптация для проекта ФПК РБ
 * 
 * Обеспечивает корректное отображение на всех устройствах,
 * сохраняя весь функционал и интерфейс.
 */

/* ============================================
   БАЗОВЫЕ НАСТРОЙКИ ДЛЯ МОБИЛЬНЫХ
   ============================================ */

/* Улучшенный viewport для мобильных */
@media screen and (max-width: 768px) {
    html {
        -webkit-text-size-adjust: 100%;
        -ms-text-size-adjust: 100%;
    }
    
    body {
        font-size: 14px;
        line-height: 1.5;
    }
    
    /* Уменьшаем отступы контейнера */
    .container-fluid {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
}

/* ============================================
   НАВИГАЦИЯ (HEADER)
   ============================================ */

@media screen and (max-width: 991px) {
    /* Навигация на мобильных */
    .navbar-nav {
        margin-top: 1rem;
    }
    
    .navbar-nav .dropdown-menu {
        position: static !important;
        float: none;
        width: 100%;
        margin-top: 0;
        background-color: #343a40;
        border: 0;
        box-shadow: none;
    }
    
    .navbar-nav .dropdown-item {
        color: rgba(255, 255, 255, 0.75);
        padding: 0.75rem 1.5rem;
        font-size: 0.95rem;
    }
    
    .navbar-nav .dropdown-item:hover {
        color: #fff;
        background-color: rgba(255, 255, 255, 0.1);
    }
    
    .navbar-nav .dropdown-divider {
        border-color: rgba(255, 255, 255, 0.1);
        margin: 0.5rem 0;
    }
    
    /* Бренд на мобильных */
    .navbar-brand {
        font-size: 1.1rem;
    }
    
    /* Профиль пользователя */
    .navbar-nav .nav-link {
        padding: 0.5rem 1rem;
    }
}

/* ============================================
   ТАБЛИЦЫ
   ============================================ */

@media screen and (max-width: 768px) {
    /* Все таблицы становятся прокручиваемыми */
    .table-responsive {
        -webkit-overflow-scrolling: touch;
        border: 1px solid #dee2e6;
        border-radius: 0.375rem;
    }
    
    /* Улучшаем отображение таблиц */
    .table {
        font-size: 0.85rem;
        margin-bottom: 0;
    }
    
    .table th,
    .table td {
        padding: 0.5rem 0.4rem;
        white-space: nowrap;
    }
    
    .table th {
        font-size: 0.8rem;
        font-weight: 600;
    }
    
    /* Карточки вместо строк для очень маленьких экранов */
    @media screen and (max-width: 576px) {
        .table-responsive {
            display: block;
        }
        
        .table-responsive table {
            display: block;
            width: 100%;
        }
        
        .table-responsive thead {
            display: none;
        }
        
        .table-responsive tbody {
            display: block;
        }
        
        .table-responsive tbody tr {
            display: block;
            margin-bottom: 1rem;
            border: 1px solid #dee2e6;
            border-radius: 0.375rem;
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        
        .table-responsive tbody td {
            display: block;
            text-align: right;
            padding: 0.5rem 1rem;
            border: none;
            border-bottom: 1px solid #f0f0f0;
            position: relative;
            padding-left: 50%;
        }
        
        .table-responsive tbody td:before {
            content: attr(data-label);
            position: absolute;
            left: 0;
            width: 45%;
            padding-left: 1rem;
            text-align: left;
            font-weight: 600;
            color: #495057;
        }
        
        .table-responsive tbody td:last-child {
            border-bottom: none;
        }
    }
}

/* Специальные таблицы (например, экран выполнения нормативов) */
@media screen and (max-width: 768px) {
    .execution-table-wrapper {
        font-size: 0.75rem;
    }
    
    .execution-table-wrapper table {
        min-width: 1200px;
    }
    
    .execution-table-wrapper thead th {
        font-size: 0.7rem;
        padding: 0.4rem 0.3rem;
    }
    
    .execution-table-wrapper tbody td {
        padding: 0.4rem 0.3rem;
        font-size: 0.7rem;
    }
    
    .execution-document-btn {
        font-size: 0.7rem;
        padding: 0.3rem 0.4rem;
    }
}

/* Улучшение для touch-устройств */
.touch-device * {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
}

.touch-device .btn,
.touch-device .nav-link,
.touch-device .dropdown-item {
    min-height: 44px; /* Минимальный размер для удобного нажатия */
    display: flex;
    align-items: center;
}

/* Закрытие dropdown при клике на затемненный фон */
.multi-select-container.active::after {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1049;
    animation: fadeIn 0.2s ease-in;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Клик на затемненный фон закрывает dropdown */
.multi-select-container.active::after {
    cursor: pointer;
}

/* ============================================
   ФИЛЬТРЫ (МУЛЬТИСЕЛЕКТ)
   ============================================ */

@media screen and (max-width: 768px) {
    /* Мультиселект фильтры */
    .multi-select-container {
        margin-bottom: 1rem;
        position: relative;
    }
    
    .multi-select-input {
        min-height: 44px;
        font-size: 16px; /* Предотвращает зум на iOS */
        cursor: pointer;
    }
    
    .multi-select-dropdown {
        position: fixed !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%);
        width: 90vw !important;
        max-width: 500px !important;
        max-height: 70vh !important;
        z-index: 1050 !important;
        box-shadow: 0 10px 40px rgba(0,0,0,0.3);
        border-radius: 0.5rem;
        background: white;
        border: 1px solid #dee2e6;
    }
    
    
    .multi-select-options {
        max-height: calc(70vh - 100px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .multi-select-item {
        padding: 0.75rem 1rem;
        min-height: 44px;
    }
    
    .multi-select-item label {
        font-size: 0.95rem;
        margin-left: 0.5rem;
    }
    
    .multi-select-search input {
        font-size: 16px; /* Предотвращает зум на iOS */
        padding: 0.75rem;
    }
    
    .selected-tags {
        margin-top: 0.5rem;
    }
    
    .selected-tags .badge {
        margin: 0.25rem;
        padding: 0.4rem 0.6rem;
        font-size: 0.85rem;
    }
}

/* ============================================
   СПЕЦИАЛЬНЫЕ КОМПОНЕНТЫ МОДУЛЕЙ
   ============================================ */

/* Управление нормативами - боковая панель */
@media screen and (max-width: 991px) {
    .normative-manage-card {
        margin-bottom: 1.5rem;
    }
}

/* Экран выполнения нормативов - фильтры */
@media screen and (max-width: 768px) {
    .execution-table-wrapper {
        margin: 0 -0.75rem;
    }
    
    #executionStats .row > div {
        margin-bottom: 0.75rem;
    }
    
    #executionStats h5 {
        font-size: 0.9rem;
    }
    
    #executionStats h2 {
        font-size: 1.5rem;
    }
}

/* Список документов нормативов */
@media screen and (max-width: 768px) {
    .table-responsive table {
        min-width: 800px;
    }
}

/* Дашборд нормативов */
@media screen and (max-width: 768px) {
    .stat-card {
        margin-bottom: 1rem;
    }
    
    .stat-number {
        font-size: 1.75rem;
    }
}

/* Отчеты нормативов */
@media screen and (max-width: 768px) {
    .card.shadow-sm {
        margin-bottom: 1rem;
    }
    
    .card-header h5 {
        font-size: 0.95rem;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ГОРИЗОНТАЛЬНОЙ ПРОКРУТКИ
   ============================================ */

@media screen and (max-width: 768px) {
    /* Улучшаем горизонтальную прокрутку таблиц */
    .table-responsive {
        position: relative;
    }
    
    .table-responsive::before,
    .table-responsive::after {
        content: '';
        position: absolute;
        top: 0;
        bottom: 0;
        width: 20px;
        z-index: 1;
        pointer-events: none;
    }
    
    .table-responsive::before {
        left: 0;
        background: linear-gradient(to right, rgba(245, 245, 245, 0.9), transparent);
    }
    
    .table-responsive::after {
        right: 0;
        background: linear-gradient(to left, rgba(245, 245, 245, 0.9), transparent);
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ФОРМ
   ============================================ */

@media screen and (max-width: 768px) {
    /* Группы полей в формах */
    .mb-3 {
        margin-bottom: 1rem !important;
    }
    
    /* Кнопки в формах */
    .btn-group {
        display: flex;
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    /* Чекбоксы и радиокнопки */
    .form-check {
        margin-bottom: 0.75rem;
        padding-left: 1.75rem;
    }
    
    .form-check-input {
        width: 1.25rem;
        height: 1.25rem;
        margin-top: 0.2rem;
    }
    
    .form-check-label {
        font-size: 0.9rem;
        padding-left: 0.5rem;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ СПИСКОВ И КАРТОЧЕК
   ============================================ */

@media screen and (max-width: 768px) {
    /* Списки */
    .list-group-item {
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
    }
    
    /* Карточки статистики */
    .card.shadow-lg {
        box-shadow: 0 2px 8px rgba(0,0,0,0.15) !important;
    }
    
    /* Иконки в карточках */
    .card-body i[style*="font-size: 3rem"] {
        font-size: 2rem !important;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ТАБЛИЦ С ДЕЙСТВИЯМИ
   ============================================ */

@media screen and (max-width: 576px) {
    /* Кнопки действий в таблицах */
    .table-actions,
    .normative-table-actions {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        white-space: normal;
    }
    
    .table-actions .btn,
    .normative-table-actions .btn {
        width: 100%;
        margin: 0;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ГРАФИКОВ
   ============================================ */

@media screen and (max-width: 768px) {
    .chart-container {
        height: 250px !important;
        margin: 1rem 0;
    }
    
    /* Графики Chart.js */
    canvas {
        max-width: 100%;
        height: auto !important;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ПАГИНАЦИИ
   ============================================ */

@media screen and (max-width: 576px) {
    .pagination {
        font-size: 0.8rem;
    }
    
    .page-link {
        padding: 0.35rem 0.5rem;
        min-width: 36px;
    }
    
    .pagination .page-item:not(:first-child):not(:last-child) .page-link {
        display: none;
    }
    
    .pagination .page-item:first-child .page-link,
    .pagination .page-item:last-child .page-link {
        display: block;
    }
    
    /* Показываем только первую, последнюю и текущую страницу */
    .pagination .page-item.active .page-link {
        display: block;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ХЛЕБНЫХ КРОШЕК
   ============================================ */

@media screen and (max-width: 576px) {
    .breadcrumb {
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
    }
    
    .breadcrumb-item {
        display: inline-block;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ЗАГОЛОВКОВ С КНОПКАМИ
   ============================================ */

@media screen and (max-width: 768px) {
    .d-flex.justify-content-between {
        flex-direction: column;
        gap: 1rem;
    }
    
    .d-flex.justify-content-between h1,
    .d-flex.justify-content-between h2,
    .d-flex.justify-content-between h3 {
        margin-bottom: 0;
    }
    
    .d-flex.justify-content-between .btn {
        width: 100%;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ ЛЕГЕНДЫ И ИНФОРМАЦИОННЫХ БЛОКОВ
   ============================================ */

@media screen and (max-width: 768px) {
    .alert {
        font-size: 0.9rem;
        line-height: 1.5;
    }
    
    .alert h4,
    .alert h5,
    .alert h6 {
        font-size: 1rem;
        margin-bottom: 0.5rem;
    }
}

/* ============================================
   УЛУЧШЕНИЯ ДЛЯ СПЕЦИАЛЬНЫХ ТАБЛИЦ
   ============================================ */

@media screen and (max-width: 768px) {
    /* Таблицы с множеством колонок */
    .table-responsive-acceptance {
        font-size: 0.75rem;
    }
    
    .table-responsive-acceptance table {
        min-width: 1500px;
    }
    
    .table-responsive-acceptance thead th {
        font-size: 0.7rem;
        padding: 0.4rem 0.3rem;
    }
    
    .table-responsive-acceptance tbody td {
        font-size: 0.7rem;
        padding: 0.4rem 0.3rem;
    }
}

/* ============================================
   ФОРМЫ И ФИЛЬТРЫ
   ============================================ */

@media screen and (max-width: 768px) {
    /* Формы */
    .form-control,
    .form-select {
        font-size: 16px; /* Предотвращает зум на iOS */
        padding: 0.5rem 0.75rem;
    }
    
    .form-label {
        font-size: 0.9rem;
        font-weight: 500;
        margin-bottom: 0.4rem;
    }
    
    /* Кнопки */
    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
    }
    
    .btn-sm {
        padding: 0.35rem 0.7rem;
        font-size: 0.8rem;
    }
    
    .btn-lg {
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
    }
    
    /* Мультиселект фильтры */
    .multi-select-container {
        position: relative;
    }
    
    .multi-select-dropdown {
        max-height: 300px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .multi-select-options {
        max-height: 250px;
    }
    
    .selected-tags {
        flex-wrap: wrap;
        gap: 0.25rem;
    }
    
    .selected-tags .badge {
        font-size: 0.75rem;
        padding: 0.25rem 0.5rem;
    }
}

/* ============================================
   КАРТОЧКИ (CARDS)
   ============================================ */

@media screen and (max-width: 768px) {
    .card {
        margin-bottom: 1rem;
    }
    
    .card-header {
        padding: 0.75rem 1rem;
        font-size: 0.95rem;
    }
    
    .card-body {
        padding: 1rem;
    }
    
    .card-title {
        font-size: 1.1rem;
    }
    
    /* Статистические карточки */
    .stat-card {
        margin-bottom: 1rem;
    }
    
    .stat-number {
        font-size: 2rem;
    }
}

/* ============================================
   СЕТКА (GRID)
   ============================================ */

@media screen and (max-width: 768px) {
    /* Колонки на мобильных становятся полной ширины */
    .row > [class*="col-"] {
        margin-bottom: 1rem;
    }
    
    /* Специальные случаи - 2 колонки на средних экранах */
    @media screen and (min-width: 576px) and (max-width: 768px) {
        .col-md-3 {
            flex: 0 0 50%;
            max-width: 50%;
        }
        
        .col-md-4 {
            flex: 0 0 50%;
            max-width: 50%;
        }
    }
}

/* ============================================
   МОДАЛЬНЫЕ ОКНА
   ============================================ */

@media screen and (max-width: 768px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 0.5rem;
    }
    
    .modal-header {
        padding: 1rem;
        flex-wrap: wrap;
    }
    
    .modal-header .btn-close {
        margin-left: auto;
        margin-top: -0.5rem;
    }
    
    .modal-body {
        padding: 1rem;
        max-height: calc(100vh - 200px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .modal-footer {
        padding: 0.75rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        flex: 1 1 auto;
        min-width: 100px;
        margin: 0;
    }
    
    /* На очень маленьких экранах модальное окно на всю высоту */
    @media screen and (max-width: 576px) {
        .modal-dialog {
            margin: 0;
            max-width: 100%;
            height: 100vh;
        }
        
        .modal-content {
            height: 100%;
            border-radius: 0;
        }
        
        .modal-body {
            max-height: calc(100vh - 150px);
        }
    }
}

/* ============================================
   ХЛЕБНЫЕ КРОШКИ (BREADCRUMBS)
   ============================================ */

@media screen and (max-width: 768px) {
    .breadcrumb {
        font-size: 0.85rem;
        padding: 0.5rem 0.75rem;
        margin-bottom: 1rem;
    }
    
    .breadcrumb-item {
        display: inline-block;
    }
    
    .breadcrumb-item + .breadcrumb-item::before {
        padding: 0 0.5rem;
    }
}

/* ============================================
   ЗАГОЛОВКИ
   ============================================ */

@media screen and (max-width: 768px) {
    h1 {
        font-size: 1.75rem;
    }
    
    h2 {
        font-size: 1.5rem;
    }
    
    h3 {
        font-size: 1.25rem;
    }
    
    h4 {
        font-size: 1.1rem;
    }
    
    h5 {
        font-size: 1rem;
    }
}

/* ============================================
   АЛЕРТЫ (ALERTS)
   ============================================ */

@media screen and (max-width: 768px) {
    .alert {
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
        margin-bottom: 1rem;
    }
    
    .alert-dismissible {
        padding-right: 3rem;
    }
}

/* ============================================
   ПАГИНАЦИЯ
   ============================================ */

@media screen and (max-width: 768px) {
    .pagination {
        font-size: 0.85rem;
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .page-link {
        padding: 0.4rem 0.6rem;
    }
}

/* ============================================
   БЕЙДЖИ И МЕТКИ
   ============================================ */

@media screen and (max-width: 768px) {
    .badge {
        font-size: 0.75rem;
        padding: 0.25rem 0.5rem;
    }
    
    .badge-lg {
        font-size: 0.85rem;
        padding: 0.35rem 0.65rem;
    }
}

/* ============================================
   СПЕЦИАЛЬНЫЕ КОМПОНЕНТЫ
   ============================================ */

/* Фильтры в боковой панели */
@media screen and (max-width: 991px) {
    .col-md-3:has(.card.shadow-sm) {
        margin-bottom: 1.5rem;
    }
}

/* Графики Chart.js */
@media screen and (max-width: 768px) {
    .chart-container {
        height: 250px !important;
    }
}

/* Таблицы с действиями */
@media screen and (max-width: 576px) {
    .table-actions {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
    }
    
    .table-actions .btn {
        width: 100%;
    }
}

/* ============================================
   УТИЛИТЫ
   ============================================ */

/* Скрытие на мобильных */
@media screen and (max-width: 768px) {
    .d-none-mobile {
        display: none !important;
    }
}

/* Показ только на мобильных */
@media screen and (min-width: 769px) {
    .d-mobile-only {
        display: none !important;
    }
}

/* Улучшенная прокрутка */
@media screen and (max-width: 768px) {
    * {
        -webkit-overflow-scrolling: touch;
    }
    
    /* Улучшаем скроллбар на мобильных */
    ::-webkit-scrollbar {
        width: 8px;
        height: 8px;
    }
    
    ::-webkit-scrollbar-track {
        background: #f1f1f1;
    }
    
    ::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 4px;
    }
    
    ::-webkit-scrollbar-thumb:hover {
        background: #555;
    }
}

/* ============================================
   ОПТИМИЗАЦИЯ ДЛЯ ОЧЕНЬ МАЛЕНЬКИХ ЭКРАНОВ
   ============================================ */

@media screen and (max-width: 375px) {
    body {
        font-size: 13px;
    }
    
    .container-fluid {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
    
    .btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.85rem;
    }
    
    .card-body {
        padding: 0.75rem;
    }
    
    h1 {
        font-size: 1.5rem;
    }
    
    h2 {
        font-size: 1.3rem;
    }
}
