/* Мобильная адаптация */

@media (max-width: 768px) {
    /* Общие стили */
    body {
        font-size: 14px;
    }

    /* Контейнер */
    .container {
        flex-direction: column;
        margin-top: 0;
    }

    /* Сайдбар - скрываем по умолчанию, показываем по кнопке */
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        bottom: 0;
        width: 280px;
        z-index: 1000;
        transition: left 0.3s ease;
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
        height: 100vh;
        overflow-y: auto;
    }

    .sidebar.open {
        left: 0;
    }
    
    .sidebar-header h2 {
        padding-left: 48px;
    }

    /* Кнопка открытия сайдбара */
    .sidebar-toggle {
        display: flex !important;
        position: fixed;
        left: 10px;
        top: 9px;
        z-index: 1001;
        background-color: var(--white);
        border: 1px solid var(--ink-200);
        padding: 0;
        border-radius: var(--radius-md);
        font-size: 16px;
        height: 38px;
        width: 38px;
        align-items: center;
        justify-content: center;
        color: var(--ink-700);
        box-shadow: var(--fc-shadow-flat);
    }

    .sidebar-toggle:hover {
        background-color: var(--ink-50);
        border-color: var(--ink-400);
        color: var(--ink-950);
    }

    /* Оверлей для сайдбара */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.5);
        z-index: 999;
        backdrop-filter: blur(2px);
    }

    .sidebar-overlay.active {
        display: block;
    }

    /* Хедер */
    .user-header {
        padding: 9px 10px 9px 58px;
        flex-wrap: nowrap;
        gap: 6px;
        position: relative;
        align-items: center;
        min-height: 56px;
    }

    .user-header-left {
        flex: 1 1 auto;
        min-width: 0;
        overflow: hidden;
    }

    .user-header-right {
        flex: 0 0 auto;
        justify-content: flex-end;
        flex-wrap: nowrap;
        gap: 4px;
        margin-top: 0;
        align-items: center;
    }

    .user-header #projectTitle {
        font-size: 14px;
        padding-left: 0;
        margin: 0;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 100%;
        width: 100%;
        line-height: 1.2;
    }

    .user-header #userInfo {
        display: none !important;
    }

    /* Информация о пользователе в попапе настроек */
    #settingsUserInfo {
        display: block !important;
        font-size: 12px;
        line-height: 1.5;
        word-break: break-word;
    }

    #settingsUserInfo span {
        display: block;
        margin-top: 4px;
        color: var(--color-text-secondary);
        font-size: 11px;
        line-height: 1.4;
    }

    .user-header #btnLogout,
    .user-header #btnSettings {
        font-size: 12px;
        padding: 5px 12px;
        height: 36px;
        flex-shrink: 0;
        white-space: nowrap;
        min-width: auto;
    }

    .user-header #btnSettings {
        padding: 5px;
        width: 36px;
        min-width: 36px;
    }

    .user-header #activeCheckInfo {
        font-size: 10px;
        padding: 4px 6px;
        margin-right: 0;
        flex-shrink: 1;
        min-width: 0;
        max-width: 120px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Основной контент */
    .main-content {
        width: 100%;
    }

    .content-body {
        padding: 8px;
    }

    /* ============ ФИЛЬТРЫ (мобильная компоновка) ============ */
    /* Единая высота контролов и аккуратные отступы — без «растянутости» */
    .filters {
        margin-bottom: 12px;
    }

    .filters-row {
        flex-direction: column;
        align-items: stretch;
        gap: 6px;
        width: 100%;
        min-width: 0;
    }

    .filters-row > * {
        width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }

    /* Единая высота всех контролов фильтров */
    .filters-row .filter-select,
    .filters-row .filter-date-input,
    .filters-row .search-engine-toggle,
    .filters-row .btn-icon,
    #btnCheckPositions,
    #btnStopCheck {
        height: 38px;
    }

    /* Сегмент-контроль поисковых систем — на всю ширину, равные доли */
    .search-engine-toggle {
        width: 100%;
    }

    .search-engine-toggle .toggle-btn {
        flex: 1;
        padding: 0 8px;
        font-size: 13px;
    }

    .filter-select {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        font-size: 14px;
    }

    #filterRegion {
        max-width: 100%;
        min-width: 0;
    }

    .filter-date-input {
        width: 100%;
        min-width: 0;
        font-size: 14px;
    }

    .date-range-picker {
        width: 100%;
    }

    .date-filter-group {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 6px !important;
        width: 100% !important;
    }

    .date-range-picker {
        order: 2 !important;
        flex: 0 0 165px !important;
        width: 165px !important;
    }

    .filter-date-input {
        text-align: center !important;
        padding: 4px 4px !important;
    }

    .calendar-presets {
        order: 1 !important;
        flex: 1 1 auto !important;
        display: flex !important;
        gap: 4px !important;
    }

    .calendar-preset {
        flex: 1 !important;
        height: 38px !important;
        padding: 0 4px !important;
        font-size: 13px !important;
        border-radius: var(--radius-md) !important;
        white-space: nowrap !important;
    }

    .custom-calendar {
        width: 100%;
        left: 0 !important;
        right: 0;
        margin-left: 0;
        margin-right: 0;
    }

    /* Строка «группа + редактировать + добавить» — в один ряд, компактно */
    .filters-row > div:has(#filterGroup) {
        display: flex;
        flex-direction: row;
        align-items: stretch;
        gap: 6px;
        width: 100%;
        min-width: 0;
        overflow: visible;
    }

    .filters-row > div:has(#filterGroup) #filterGroup {
        flex: 1 1 0;
        min-width: 0;
        width: 0;
        max-width: none;
        height: 38px;
    }

    .filters-row > div:has(#filterGroup) #btnEditGroups,
    .filters-row > div:has(#filterGroup) #btnAddKeywords {
        flex-shrink: 0;
        width: 38px !important;
        min-width: 38px !important;
        max-width: 38px !important;
        height: 38px !important;
        padding: 0 !important;
        font-size: 15px;
    }

    /* Строка с кнопкой проверки и действиями.
       ВАЖНО: селектор обязан быть ограничен «.filters-row >», иначе
       div:has(#btnCheckPositions) матчит ВСЕ div-предки (включая саму
       .filters-row), а специфичность :has(#id) перебивает flex-direction
       у .filters-row и схлопывает фильтры в один ряд. */
    .filters-row > div:has(#btnCheckPositions) {
        display: flex;
        flex-direction: row;
        align-items: stretch;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        gap: 6px;
        flex-wrap: nowrap;
    }

    #btnCheckPositions {
        flex: 1 1 auto !important;
        min-width: 0 !important;
        font-size: 14px;
    }

    #btnStopCheck {
        flex: 0 0 44px;
        width: 44px !important;
        min-width: 44px !important;
        max-width: 44px !important;
        padding: 0 !important;
    }

    /* Эти действия на мобильных скрыты (доступны на десктопе) */
    #btnToggleGraph,
    #btnExportCSV {
        display: none !important;
    }

    /* Таблица */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        position: relative;
        margin: 0 -8px;
        padding: 0 8px;
    }

    .positions-table {
        min-width: 700px;
        table-layout: fixed;
    }

    .positions-table th,
    .positions-table td {
        padding: 4px 4px;
        font-size: 11px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .positions-table th:first-child,
    .positions-table td:first-child {
        width: 35px;
        min-width: 35px;
        max-width: 35px;
        padding: 4px 2px;
        text-align: center;
    }

    .positions-table th:nth-child(2),
    .positions-table td:nth-child(2) {
        width: 200px;
        min-width: 200px;
        max-width: 200px;
    }

    .positions-table th:nth-child(3),
    .positions-table td:nth-child(3) {
        display: none;
    }

    /* Колонки с датами */
    .positions-table th:nth-child(n+4),
    .positions-table td:nth-child(n+4) {
        width: 80px;
        min-width: 80px;
        max-width: 80px;
        text-align: center;
        padding: 4px 2px;
    }

    /* Ячейки с позициями */
    /* Ячейки с позициями */
    .positions-table td .position-top10,
    .positions-table td .position-top30,
    .positions-table td .position-error {
        display: inline-block;
        padding: 2px 4px;
        min-width: 20px;
        text-align: center;
        white-space: nowrap;
        overflow: visible;
        box-sizing: border-box;
    }

    /* Индикатор прокрутки таблицы */
    .table-container {
        position: relative;
    }

    .table-container::-webkit-scrollbar {
        height: 4px;
    }

    .table-container::-webkit-scrollbar-track {
        background: var(--color-bg-primary);
    }

    .table-container::-webkit-scrollbar-thumb {
        background: var(--color-border);
        border-radius: 2px;
    }

    /* Модальные окна */
    .modal-content {
        width: 95%;
        max-width: 95%;
        max-height: 95vh;
        margin: 10px;
        border-radius: var(--radius-lg);
    }

    /* Модальное окно выбора типа проверки */
    #checkTypeModal .modal-content {
        max-width: 500px;
    }

    #checkTypeModal #btnCheckTypeSelected,
    #checkTypeModal #btnCheckTypeAllRegions,
    #checkTypeModal #btnCheckTypeAllEngines {
        width: 100%;
        text-align: left;
        justify-content: flex-start;
        padding: 12px;
        height: auto;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
    }

    #checkTypeModal #btnCheckTypeSelected > div:first-child,
    #checkTypeModal #btnCheckTypeAllRegions > div:first-child,
    #checkTypeModal #btnCheckTypeAllEngines > div:first-child {
        font-weight: 600;
        margin-bottom: 4px;
    }

    #checkTypeModal #btnCheckTypeSelected > div:last-child,
    #checkTypeModal #btnCheckTypeAllRegions > div:last-child,
    #checkTypeModal #btnCheckTypeAllEngines > div:last-child {
        font-size: 11px;
        opacity: 0.8;
    }

    .modal-header {
        padding: 12px;
    }

    .modal-header h3 {
        font-size: 16px;
    }

    .modal-body {
        padding: 12px;
    }

    .modal-close {
        width: 32px;
        height: 32px;
        font-size: 20px;
    }

    /* Редактор групп — складываем строку вертикально */
    #groupsModal .modal-content {
        max-width: 95%;
    }

    .group-row {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }

    .group-row__actions {
        flex-wrap: wrap;
    }

    .group-row__actions .btn-secondary {
        flex: 1;
    }

    /* Формы в модальных окнах */
    .form-group {
        margin-bottom: 12px;
    }

    .form-actions {
        flex-direction: column;
        gap: 8px;
    }

    .form-actions button {
        width: 100%;
        padding: 10px;
        height: auto;
    }

    /* Проекты в модальном окне */
    .projectRegionsContainer {
        flex-direction: column !important;
        gap: 12px !important;
    }

    .projectRegionsColumn {
        width: 100% !important;
        min-width: 100% !important;
    }

    /* Статистика */
    .stats-container {
        display: grid !important;
        grid-template-columns: repeat(6, 1fr) !important;
        gap: 4px;
        overflow-x: auto;
        width: 100%;
    }
    
    .stats-item {
        min-width: 0;
        font-size: 10px;
        padding: 6px 4px;
        flex-shrink: 0;
    }
    
    .stats-label {
        font-size: 8px;
        margin-bottom: 2px;
    }
    
    .stats-value {
        font-size: 14px;
    }
    
    .stats-change {
        font-size: 8px;
    }

    .analytics-layout {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    
    #analyticsContent {
        display: block !important;
    }
    
    .analytics-content {
        display: block !important;
    }

    .chart-container {
        display: none !important;
    }

    #positionsChart {
        display: none !important;
    }

    /* Пустое состояние */
    .empty-state {
        padding: 20px;
        min-height: 300px;
    }

    .empty-state-content h2 {
        font-size: 20px;
    }

    .empty-state-content p {
        font-size: 13px;
    }

    /* Авторизация */
    .auth-box {
        margin: 20px;
        padding: 20px;
        max-width: 100%;
    }

    .auth-box h2 {
        font-size: 18px;
    }

    /* Календарь */
    .calendar-days {
        gap: 4px;
    }

    .calendar-day {
        padding: 8px 4px;
        font-size: 11px;
    }

    /* Уведомления */
    .notification {
        max-width: 90%;
        margin: 8px auto;
        padding: 12px;
        font-size: 13px;
    }
}

@media (max-width: 480px) {
    /* Очень маленькие экраны */
    .sidebar-toggle {
        left: 8px;
        top: 8px;
        width: 36px;
        height: 36px;
        padding: 0;
        font-size: 15px;
    }

    .sidebar-header h2 {
        padding-left: 48px;
    }

    .user-header {
        padding: 8px 8px 8px 52px;
        min-height: 52px;
        margin-bottom: 0;
    }

    .user-header #projectTitle {
        font-size: 14px;
    }

    .user-header #btnLogout,
    .user-header #btnSettings {
        font-size: 11px;
        padding: 5px 10px;
        height: 34px;
    }

    .user-header #btnSettings {
        padding: 5px;
        width: 34px;
        min-width: 34px;
    }

    .content-body {
        padding: 6px;
    }

    .filters-row {
        gap: 6px;
    }

    .search-engine-toggle .toggle-btn {
        padding: 6px 6px;
        font-size: 11px;
    }

    .positions-table {
        min-width: 500px;
    }

    .positions-table th,
    .positions-table td {
        padding: 4px 4px;
        font-size: 11px;
    }
    
    .positions-table th:nth-child(3),
    .positions-table td:nth-child(3) {
        display: none;
    }

    .modal-content {
        width: 100%;
        max-width: 100%;
        margin: 0;
        border-radius: 0;
        max-height: 100vh;
    }

    .modal-header {
        padding: 10px;
    }

    .modal-body {
        padding: 10px;
    }

    .auth-box {
        margin: 10px;
        padding: 16px;
    }

    .empty-state-content h2 {
        font-size: 18px;
    }
}

/* Горизонтальная ориентация на мобильных */
@media (max-width: 768px) and (orientation: landscape) {
    .sidebar {
        width: 250px;
    }

    .user-header {
        padding: 6px 12px;
    }

    .content-body {
        padding: 8px;
    }
}


