/* ФИКС ДЛЯ ВЕРХНЕГО МЕНЮ (HEADER) */

/* iOS PWA специфичные стили - ПРИОРИТЕТНЫЕ */
@supports (-webkit-touch-callout: none) {
    /* ВАЖНО: Header должен быть выше всего */
    .kiwi-header {
        padding-top: env(safe-area-inset-top) !important;
        height: calc(60px + env(safe-area-inset-top)) !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        z-index: 9999 !important; /* ВЫШЕ ВСЕГО! */
        background: #1a1a1a !important;
        border-bottom: 1px solid #333 !important;
        display: flex !important;
        align-items: center !important;
        -webkit-tap-highlight-color: rgba(0,0,0,0) !important;
    }

    /* Бренд/заголовок */
    .kiwi-header-brand {
        height: 60px !important;
        line-height: 60px !important;
        padding-left: 16px !important;
        font-size: 20px !important;
        color: #fff !important;
        font-weight: 600 !important;
        flex-grow: 1 !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }

    /* Кнопки в header */
    .kiwi-header-button {
        min-width: 60px !important;
        min-height: 60px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        cursor: pointer !important;
        color: #fff !important;
        font-size: 24px !important;
        -webkit-tap-highlight-color: rgba(255,255,255,0.1) !important;
        position: relative !important;
        z-index: 10000 !important;
    }

    /* Иконки в кнопках */
    .kiwi-header-button i,
    .kiwi-header-button svg {
        pointer-events: none !important; /* Чтобы клики проходили через иконку к кнопке */
    }

    /* Анимация нажатия */
    .kiwi-header-button:active {
        background: rgba(255,255,255,0.1) !important;
        transform: scale(0.95) !important;
        transition: transform 0.1s !important;
    }

    /* Контейнер кнопок */
    .kiwi-header-buttons {
        display: flex !important;
        align-items: center !important;
        height: 100% !important;
        position: relative !important;
        z-index: 10000 !important;
    }

    /* Dropdown меню */
    .kiwi-header-dropdown {
        position: absolute !important;
        top: 100% !important;
        right: 0 !important;
        background: #1a1a1a !important;
        border: 1px solid #333 !important;
        border-radius: 8px !important;
        box-shadow: 0 4px 20px rgba(0,0,0,0.5) !important;
        z-index: 10001 !important; /* Еще выше! */
        min-width: 200px !important;
    }

    /* Пункты dropdown меню */
    .kiwi-header-dropdown-item {
        padding: 16px !important;
        color: #fff !important;
        border-bottom: 1px solid #333 !important;
        cursor: pointer !important;
        font-size: 16px !important;
        min-height: 50px !important;
        display: flex !important;
        align-items: center !important;
        -webkit-tap-highlight-color: rgba(255,255,255,0.1) !important;
    }

    .kiwi-header-dropdown-item:last-child {
        border-bottom: none !important;
    }

    .kiwi-header-dropdown-item:active {
        background: rgba(255,255,255,0.1) !important;
    }

    /* Overlay для dropdown */
    .kiwi-header-overlay {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        background: rgba(0,0,0,0.5) !important;
        z-index: 9998 !important; /* Ниже header, но выше всего остального */
    }
}

/* ОБЩИЕ ФИКСЫ ДЛЯ HEADER НА ВСЕХ УСТРОЙСТВАХ */
.kiwi-header {
    position: relative !important;
    z-index: 1000 !important;
}

/* Убедитесь, что ничего не перекрывает header */
body::before {
    content: '' !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    height: env(safe-area-inset-top) !important;
    background: #1a1a1a !important;
    z-index: 9998 !important;
    pointer-events: none !important;
}

/* ФИКС ДЛЯ ВСЕХ КЛИКАБЕЛЬНЫХ ЭЛЕМЕНТОВ В HEADER */
.kiwi-header * {
    pointer-events: auto !important;
}

/* Удалите любые перекрывающие элементы */
.kiwi-app::before,
.kiwi-container::before,
.kiwi-wrapper::before {
    display: none !important;
}

/* Дополнительные гарантии для iOS PWA */
body.pwa-mode .kiwi-header,
body.ios-pwa .kiwi-header,
body.kiwi-ios-standalone .kiwi-header {
    z-index: 2147483647 !important; /* Максимальный z-index */
}

/* ФИКС: Убедитесь, что контент под header не перекрывает его */
.kiwi-messagelist-container {
    margin-top: calc(60px + env(safe-area-inset-top)) !important;
}

/* Для мобильных устройств */
@media (max-width: 768px) {
    .kiwi-header {
        height: 60px !important;
        z-index: 9999 !important;
    }

    .kiwi-header-brand {
        font-size: 18px !important;
        padding-left: 16px !important;
    }

    .kiwi-header-button {
        min-width: 50px !important;
        min-height: 50px !important;
        font-size: 20px !important;
    }

    /* Гарантия кликабельности на мобильных */
    .kiwi-header * {
        -webkit-tap-highlight-color: rgba(255,255,255,0.1) !important;
    }
}

/* Дополнительный фикс для notch iPhone */
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3) {

    .kiwi-header {
        height: calc(50px + env(safe-area-inset-top)) !important;
        padding-top: env(safe-area-inset-top) !important;
    }

    .kiwi-header-brand {
        height: 50px !important;
        line-height: 50px !important;
    }

    .kiwi-header-button {
        min-height: 50px !important;
        min-width: 50px !important;
    }
}

/* ========== ФИКС ДЛЯ НАВИГАЦИИ МЕЖДУ КАНАЛАМИ/ПРИВАТНЫМИ СООБЩЕНИЯМИ ========== */

/* Контейнер навигации (должен быть виден всегда) */
.kiwi-navigation-custom {
    display: flex !important;
    align-items: center !important;
    padding: 0 16px !important;
    height: 60px !important;
    background: #1a1a1a !important;
    border-bottom: 1px solid #333 !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    z-index: 9999 !important;
}

/* Кнопка "Назад" для приватных сообщений */
.kiwi-navigation-back {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 44px !important;
    height: 44px !important;
    margin-right: 12px !important;
    color: #4a9eff !important;
    font-size: 24px !important;
    cursor: pointer !important;
    -webkit-tap-highlight-color: rgba(74, 158, 255, 0.2) !important;
    border-radius: 8px !important;
}

.kiwi-navigation-back:active {
    background: rgba(74, 158, 255, 0.1) !important;
    transform: scale(0.95) !important;
}

/* Индикатор текущего канала/приватки */
.kiwi-navigation-current {
    flex-grow: 1 !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
    min-height: 44px !important;
}

.kiwi-navigation-channel-name {
    font-size: 18px !important;
    font-weight: 600 !important;
    color: #fff !important;
    margin-bottom: 2px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

.kiwi-navigation-channel-topic {
    font-size: 14px !important;
    color: #aaa !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

/* Индикатор приватного сообщения */
.kiwi-navigation-private-indicator::before {
    content: "🔒 " !important;
    margin-right: 4px !important;
}

/* Список каналов (sidebar) */
.kiwi-channel-list-custom {
    position: fixed !important;
    top: 60px !important;
    bottom: 70px !important;
    left: 0 !important;
    width: 280px !important;
    background: #1a1a1a !important;
    border-right: 1px solid #333 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    z-index: 9998 !important;
    transform: translateX(-100%) !important;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
}

.kiwi-channel-list-custom--active {
    transform: translateX(0) !important;
}

/* Элемент списка каналов */
.kiwi-channel-list-item-custom {
    display: flex !important;
    align-items: center !important;
    padding: 14px 16px !important;
    border-bottom: 1px solid #2a2a2a !important;
    color: #fff !important;
    cursor: pointer !important;
    -webkit-tap-highlight-color: rgba(255,255,255,0.1) !important;
    min-height: 52px !important;
    transition: background 0.2s, transform 0.1s !important;
}

.kiwi-channel-list-item-custom--active {
    background: rgba(74, 158, 255, 0.15) !important;
    border-left: 4px solid #4a9eff !important;
}

.kiwi-channel-list-item-custom:active {
    background: rgba(255,255,255,0.1) !important;
    transform: scale(0.98) !important;
}

/* Иконка канала */
.kiwi-channel-list-item-icon {
    margin-right: 12px !important;
    font-size: 18px !important;
    width: 24px !important;
    text-align: center !important;
}

.kiwi-channel-list-item-name {
    flex-grow: 1 !important;
    font-size: 16px !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

.kiwi-channel-list-item-unread {
    background: #4a9eff !important;
    color: white !important;
    font-size: 12px !important;
    padding: 2px 6px !important;
    border-radius: 10px !important;
    min-width: 20px !important;
    text-align: center !important;
}

/* Overlay для sidebar */
.kiwi-channel-list-overlay-custom {
    position: fixed !important;
    top: 60px !important;
    bottom: 70px !important;
    left: 0 !important;
    right: 0 !important;
    background: rgba(0,0,0,0.5) !important;
    z-index: 9997 !important;
    display: none !important;
}

.kiwi-channel-list-overlay-custom--active {
    display: block !important;
}

/* Кнопка меню каналов */
.kiwi-header-channel-menu {
    display: flex !important;
    align-items: center !important;
    padding: 0 16px !important;
    cursor: pointer !important;
    -webkit-tap-highlight-color: rgba(255,255,255,0.1) !important;
    min-height: 60px !important;
}

.kiwi-header-channel-menu:active {
    background: rgba(255,255,255,0.05) !important;
}

.kiwi-header-channel-menu-icon {
    font-size: 20px !important;
    margin-right: 8px !important;
    color: #fff !important;
}

.kiwi-header-channel-menu-name {
    font-size: 18px !important;
    font-weight: 600 !important;
    color: #fff !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    max-width: 200px !important;
}

/* Для приватных сообщений показываем кнопку "Назад" */
.kiwi-state-private .kiwi-navigation-back {
    display: flex !important;
    animation: fadeIn 0.3s ease !important;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateX(-10px); }
    to { opacity: 1; transform: translateX(0); }
}

.kiwi-state-channel .kiwi-navigation-back {
    display: none !important;
}

/* Анимации для списка каналов */
@keyframes slideIn {
    from { transform: translateX(-100%); }
    to { transform: translateX(0); }
}

@keyframes slideOut {
    from { transform: translateX(0); }
    to { transform: translateX(-100%); }
}

/* Адаптация для мобильных */
@media (max-width: 768px) {
    .kiwi-navigation-custom {
        height: 50px !important;
        padding: 0 12px !important;
    }

    .kiwi-navigation-back {
        width: 40px !important;
        height: 40px !important;
        margin-right: 8px !important;
        font-size: 20px !important;
    }

    .kiwi-navigation-channel-name {
        font-size: 16px !important;
    }

    .kiwi-navigation-channel-topic {
        font-size: 13px !important;
    }

    .kiwi-channel-list-custom {
        width: 100% !important;
        top: 50px !important;
        bottom: 60px !important;
    }
    
    .kiwi-channel-list-overlay-custom {
        top: 50px !important;
        bottom: 60px !important;
    }

    .kiwi-header-channel-menu-name {
        max-width: 150px !important;
        font-size: 16px !important;
    }
    
    .kiwi-header-channel-menu-icon {
        font-size: 18px !important;
    }
}

/* Для iOS PWA */
@supports (-webkit-touch-callout: none) {
    .kiwi-navigation-custom {
        padding-top: env(safe-area-inset-top) !important;
        height: calc(60px + env(safe-area-inset-top)) !important;
    }

    .kiwi-channel-list-custom {
        top: calc(60px + env(safe-area-inset-top)) !important;
    }
    
    .kiwi-channel-list-overlay-custom {
        top: calc(60px + env(safe-area-inset-top)) !important;
    }
    
    /* iOS специфичные фиксы */
    .kiwi-channel-list-items {
        -webkit-overflow-scrolling: touch !important;
        overflow-scrolling: touch !important;
    }
    
    /* Предотвращение bounce эффекта */
    .kiwi-channel-list-custom {
        overscroll-behavior: contain !important;
    }
}

/* Для iPhone с вырезом (notch) */
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 390px) and (device-height: 844px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 414px) and (device-height: 896px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 428px) and (device-height: 926px) and (-webkit-device-pixel-ratio: 3) {
    
    .kiwi-navigation-custom {
        height: calc(50px + env(safe-area-inset-top)) !important;
    }
    
    .kiwi-channel-list-custom {
        top: calc(50px + env(safe-area-inset-top)) !important;
    }
    
    .kiwi-channel-list-overlay-custom {
        top: calc(50px + env(safe-area-inset-top)) !important;
    }
}

/* Дополнительные гарантии для навигации */
.kiwi-navigation-custom * {
    -webkit-user-select: none !important;
    user-select: none !important;
}

/* Индикатор свайпа (для отладки) */
.swipe-indicator {
    position: fixed !important;
    top: 50% !important;
    left: 20px !important;
    width: 40px !important;
    height: 40px !important;
    background: rgba(74, 158, 255, 0.3) !important;
    border-radius: 50% !important;
    z-index: 99999 !important;
    pointer-events: none !important;
    opacity: 0 !important;
    transition: opacity 0.3s !important;
}

.swipe-indicator.active {
    opacity: 1 !important;
}

/* Корректировка отступов для контента */
.kiwi-messagelist-container,
.kiwi-startup-container,
.kiwi-container {
    margin-top: 60px !important;
}

@supports (-webkit-touch-callout: none) {
    .kiwi-messagelist-container,
    .kiwi-startup-container,
    .kiwi-container {
        margin-top: calc(60px + env(safe-area-inset-top)) !important;
    }
}

/* Скрываем оригинальный header Kiwi */
.kiwi-header.kiwi-header--hidden {
    display: none !important;
}

/* Анимация перехода между каналами */
.kiwi-messagelist-messages {
    animation: fadeInUp 0.3s ease !important;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Стили для пустого списка каналов */
.kiwi-channel-list-empty {
    padding: 40px 20px !important;
    text-align: center !important;
    color: #888 !important;
    font-style: italic !important;
    font-size: 14px !important;
}

/* Ховер-эффекты (для устройств с мышью) */
@media (hover: hover) and (pointer: fine) {
    .kiwi-channel-list-item-custom:hover {
        background: rgba(255,255,255,0.05) !important;
    }
    
    .kiwi-navigation-back:hover {
        background: rgba(74, 158, 255, 0.1) !important;
    }
    
    .kiwi-header-channel-menu:hover {
        background: rgba(255,255,255,0.05) !important;
    }
}