/* ==========================================
   Krue Web — Mobile Hyper-Optimization
   Подключается только к style.css, не меняет десктоп.
   ========================================== */

/* ==========================================
   Переменные для мобильных
   ========================================== */
:root {
    --mobile-nav-height: 56px;
    --bottom-sheet-radius: 20px;
    --touch-target: 44px;
}

/* ==========================================
   Базовые улучшения для тач-устройств
   ========================================== */
@media (max-width: 991px) {

    /* Убираем 300ms delay на всех элементах */
    * {
        touch-action: manipulation;
    }

    /* Запрещаем выделение текста при тапе по интерактивным элементам */
    button,
    .chat-item,
    .new-chat-btn,
    .chat-header__back,
    .chat-input__send,
    .chat-input__file {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }

    /* ==========================================
       Header — компактнее на мобиле
       ========================================== */
    .app-header {
        padding: 0 12px;
    }

    /* ==========================================
       Минимальные размеры тач-целей — 44px
       ========================================== */
    .new-chat-btn {
        width: var(--touch-target);
        height: var(--touch-target);
        border-radius: var(--radius-md);
        font-size: 1.2rem;
    }

    .chat-header__back {
        width: var(--touch-target);
        height: var(--touch-target);
        display: flex;
        border-radius: var(--radius-md);
        font-size: 1.2rem;
        margin-left: -8px;
    }

    .chat-input__send {
        width: var(--touch-target);
        height: var(--touch-target);
        font-size: 1.1rem;
        flex-shrink: 0;
    }

    .chat-input__file {
        width: var(--touch-target);
        height: var(--touch-target);
        font-size: 1.2rem;
        flex-shrink: 0;
    }

    /* ==========================================
       Список чатов — более просторные элементы
       ========================================== */
    .chat-item {
        padding: 12px 14px;
        min-height: var(--touch-target);
        /* Плавная тач-подсветка вместо hover */
        transition: background 0.1s ease;
    }

    .chat-item:active {
        background: var(--bg-hover);
    }

    .chat-item__avatar,
    .chat-item__avatar-wrap {
        width: 50px;
        height: 50px;
    }

    .chat-item__name {
        font-size: 0.95rem;
    }

    .chat-item__preview {
        font-size: 0.82rem;
    }

    /* ==========================================
       Поле ввода — оптимизировано для мобиле
       ========================================== */
    .chat-input {
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom, 8px));
        /* Подпираем input над клавиатурой */
        position: relative;
        z-index: 10;
    }

    .chat-input__field {
        font-size: 16px; /* Предотвращает zoom на iOS при фокусе */
        padding: 10px 14px;
        border-radius: var(--radius-xl);
        max-height: 120px;
    }

    /* Панель ответа — подгон под мобиль */
    .reply-preview {
        margin: 0 12px 0 12px;
    }

    /* ==========================================
       Сообщения — шире на мобиле
       ========================================== */
    .msg {
        max-width: 88%;
    }

    /* Активное состояние bubble при тапе */
    .msg__bubble {
        transition: opacity 0.1s ease;
    }

    /* Индикатор свайпа для ответа */
    .msg.swipe-reply-active .msg__bubble {
        opacity: 0.85;
    }

    /* ==========================================
       Стрелка свайп-для-ответа
       ========================================== */
    .msg {
        position: relative;
        overflow: visible;
    }

    .msg__swipe-icon {
        position: absolute;
        top: 50%;
        transform: translateY(-50%) scale(0.4);
        opacity: 0;
        width: 32px;
        height: 32px;
        background: var(--accent);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 0.9rem;
        pointer-events: none;
        transition: opacity 0.15s ease, transform 0.15s ease;
        z-index: 5;
    }

    .msg--own .msg__swipe-icon {
        right: calc(100% + 8px);
    }

    .msg--other .msg__swipe-icon {
        left: calc(100% + 8px);
    }

    .msg.swipe-reply-ready .msg__swipe-icon {
        opacity: 1;
        transform: translateY(-50%) scale(1);
    }

    /* ==========================================
       Кнопка "Scroll to bottom" — крупнее
       ========================================== */
    #scrollDownBtn {
        width: 44px !important;
        height: 44px !important;
        bottom: calc(var(--input-height) + 16px + env(safe-area-inset-bottom, 0px)) !important;
        right: 16px !important;
        font-size: 1.2rem !important;
    }

    /* ==========================================
       Нижняя навигация (мобильный таббар)
       ========================================== */
    .mobile-tabbar {
        display: flex;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom, 0px));
        padding-bottom: env(safe-area-inset-bottom, 0px);
        background: var(--bg-sidebar);
        border-top: 1px solid var(--border);
        z-index: 150;
        align-items: stretch;
    }

    /* Таббар виден только на страницах без открытого чата */
    .mobile-tabbar--hidden {
        display: none;
    }

    .mobile-tabbar__item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 3px;
        color: var(--text-secondary);
        text-decoration: none;
        font-size: 0.65rem;
        font-weight: 500;
        letter-spacing: 0.2px;
        -webkit-tap-highlight-color: transparent;
        transition: color 0.15s ease;
        position: relative;
    }

    .mobile-tabbar__item i {
        font-size: 1.4rem;
        line-height: 1;
    }

    .mobile-tabbar__item.active {
        color: var(--accent);
    }

    .mobile-tabbar__item:active {
        opacity: 0.7;
    }

    /* Бейдж непрочитанных на таббаре */
    .mobile-tabbar__badge {
        position: absolute;
        top: 4px;
        right: calc(50% - 14px);
        background: var(--accent-red);
        color: #fff;
        font-size: 0.6rem;
        font-weight: 700;
        min-width: 16px;
        height: 16px;
        border-radius: 8px;
        padding: 0 4px;
        display: none;
        align-items: center;
        justify-content: center;
        line-height: 1;
    }

    .mobile-tabbar__badge.visible {
        display: flex;
    }

    /* Страница чата — таббар скрыт когда открыт чат */
    body.chat-open .mobile-tabbar {
        display: none;
    }

    /* На странице чата — добавляем отступ снизу под safe area */
    .chat-layout {
        padding-bottom: 0;
    }

    /* Вне страницы чата — добавляем отступ под таббар */
    .page {
        padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom, 0px) + 16px);
    }

    /* Список чатов — отступ снизу чтобы таббар не перекрывал последний чат */
    #chatList,
    .chat-list,
    .sidebar__list {
        padding-bottom: calc(var(--mobile-nav-height) + env(safe-area-inset-bottom, 0px) + 8px);
    }

    /* ==========================================
       Pull-to-refresh индикатор
       ========================================== */
    .ptr-indicator {
        position: absolute;
        top: 0;
        left: 50%;
        transform: translateX(-50%) translateY(-48px);
        width: 36px;
        height: 36px;
        background: var(--bg-card);
        border: 1px solid var(--border);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--accent);
        font-size: 1rem;
        transition: transform 0.2s ease;
        z-index: 20;
        pointer-events: none;
    }

    .ptr-indicator.visible {
        transform: translateX(-50%) translateY(8px);
    }

    .ptr-indicator.spinning i {
        animation: ptr-spin 0.6s linear infinite;
    }

    @keyframes ptr-spin {
        from { transform: rotate(0deg); }
        to   { transform: rotate(360deg); }
    }

    /* ==========================================
       Bottom Sheet (для контекстного меню и forward)
       ========================================== */
    .bottom-sheet-overlay {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1999;
        opacity: 0;
        transition: opacity 0.25s ease;
        -webkit-backdrop-filter: blur(2px);
        backdrop-filter: blur(2px);
    }

    .bottom-sheet-overlay.open {
        opacity: 1;
    }

    .bottom-sheet {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-card);
        border-radius: var(--bottom-sheet-radius) var(--bottom-sheet-radius) 0 0;
        z-index: 2000;
        padding-bottom: env(safe-area-inset-bottom, 0px);
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
        max-height: 75vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .bottom-sheet.open {
        transform: translateY(0);
    }

    /* Ручка для потяжки */
    .bottom-sheet__handle {
        width: 36px;
        height: 4px;
        background: var(--border);
        border-radius: 2px;
        margin: 10px auto 8px;
        flex-shrink: 0;
    }

    /* Превью сообщения в шторке */
    .bottom-sheet__msg-preview {
        padding: 8px 16px 12px;
        border-bottom: 1px solid var(--border-light);
        font-size: 0.8rem;
        color: var(--text-secondary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Элементы контекстного меню */
    .bottom-sheet__item {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 14px 20px;
        color: var(--text-primary);
        cursor: pointer;
        font-size: 1rem;
        transition: background 0.1s ease;
        border-bottom: 1px solid var(--border-light);
        -webkit-tap-highlight-color: transparent;
    }

    .bottom-sheet__item:last-child {
        border-bottom: none;
    }

    .bottom-sheet__item:active {
        background: var(--bg-hover);
    }

    .bottom-sheet__item i {
        font-size: 1.25rem;
        width: 28px;
        text-align: center;
        flex-shrink: 0;
        color: var(--accent);
    }

    .bottom-sheet__item--danger {
        color: var(--accent-red);
    }

    .bottom-sheet__item--danger i {
        color: var(--accent-red);
    }

    .bottom-sheet__item--cancel {
        color: var(--text-secondary);
        font-weight: 600;
        border-top: 6px solid var(--bg-dark);
    }

    .bottom-sheet__item--cancel i {
        color: var(--text-secondary);
    }

    /* ==========================================
       Forward bottom sheet — поиск чатов
       ========================================== */
    .forward-sheet__search {
        padding: 8px 16px 12px;
        border-bottom: 1px solid var(--border);
    }

    .forward-sheet__search input {
        width: 100%;
        padding: 10px 14px;
        background: var(--bg-input);
        border: 1px solid var(--border);
        border-radius: var(--radius-xl);
        color: var(--text-primary);
        font-size: 16px; /* предотвращает zoom */
        font-family: var(--font);
        outline: none;
        box-sizing: border-box;
    }

    .forward-sheet__list {
        padding: 8px 0;
    }

    .forward-sheet__item {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 10px 16px;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        transition: background 0.1s;
    }

    .forward-sheet__item:active {
        background: var(--bg-hover);
    }

    .forward-sheet__item img {
        width: 44px;
        height: 44px;
        border-radius: 50%;
        object-fit: cover;
        flex-shrink: 0;
    }

    .forward-sheet__item-name {
        font-weight: 600;
        font-size: 0.9rem;
    }

    .forward-sheet__item-type {
        font-size: 0.75rem;
        color: var(--text-secondary);
        margin-top: 2px;
    }

    /* ==========================================
       Анимация боковой панели (слайд)
       ========================================== */
    .sidebar {
        transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
        will-change: transform;
    }

    .chat-area {
        transition: none;
        will-change: auto;
    }

    /* Оверлей при открытом сайдбаре — для закрытия тапом */
    .sidebar-overlay {
        display: none;
        position: absolute;
        inset: 0;
        z-index: 99;
        background: transparent;
    }

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

    /* ==========================================
       Typing indicator — крупнее на мобиле
       ========================================== */
    .typing-dots span {
        width: 5px;
        height: 5px;
    }

    /* ==========================================
       Профиль и другие страницы
       ========================================== */
    .profile-card {
        padding: 24px 20px;
    }

    .profile-card__avatar {
        width: 90px;
        height: 90px;
    }

    .profile-card__actions .btn {
        min-height: var(--touch-target);
        padding: 10px 18px;
    }

    /* ==========================================
       Кнопки — крупнее тач-цели
       ========================================== */
    .btn {
        min-height: var(--touch-target);
    }

    .btn--small {
        min-height: 36px;
    }

    /* ==========================================
       Модалка изображений — на весь экран
       ========================================== */
    .image-modal {
        padding: 0;
        -webkit-overflow-scrolling: touch;
    }

    .image-modal img {
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
    }

    .image-modal::before {
        top: max(20px, env(safe-area-inset-top, 20px));
        right: 20px;
        font-size: 2rem;
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0,0,0,0.5);
        border-radius: 50%;
    }

    /* ==========================================
       Уведомления — полная ширина
       ========================================== */
    .page__container {
        max-width: 100%;
    }
}

/* ==========================================
   Только для iOS Safari (notch / safe areas)
   ========================================== */
@supports (-webkit-touch-callout: none) {
    @media (max-width: 991px) {
        .chat-input {
            padding-bottom: max(12px, env(safe-area-inset-bottom));
        }

        .app-header {
            padding-top: env(safe-area-inset-top, 0);
        }
    }
}

/* ==========================================
   Убираем hover-эффекты на тач (экономия батареи)
   ========================================== */
@media (hover: none) {
    .chat-item:hover {
        background: transparent;
    }

    .chat-item.active:hover {
        background: var(--bg-active);
    }

    .btn:hover:not(:disabled) {
        transform: none;
        filter: none;
    }

    .new-chat-btn:hover {
        transform: none;
        background: var(--accent);
    }

    .app-header__link:hover {
        color: var(--text-secondary);
    }

    .chat-input__send:hover:not(:disabled) {
        transform: none;
        background: var(--accent);
    }

    .result-item:hover {
        background: var(--bg-card);
        border-color: var(--border);
    }
}

/* ==========================================
   Клавиатура открыта — body получает класс
   ========================================== */
body.keyboard-open .mobile-tabbar {
    display: none;
}

body.keyboard-open .chat-layout {
    /* При открытой клавиатуре layout перестраивается через visualViewport */
}
