/* Использование 100dvh для идеального отображения на мобильных без обрезки низа */
html,
body {
    height: 100%;
    height: 100dvh;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.chat-page-wrapper {
    width: 100%;
    height: calc(100vh - 65px);
    height: calc(100dvh - 65px);
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.chat-layout {
    display: flex;
    width: 100%;
    height: 100%;
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    overflow: hidden;
}

/* Список чатов */
.chat-sidebar {
    border-right: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
    background: #ffffff;
    height: 100%;
    overflow: hidden;
}

.sidebar-header {
    padding: 16px 20px;
    border-bottom: 1px solid #e2e8f0;
    background: #f8fafc;
    flex-shrink: 0;
}

.sidebar-header h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 800;
    color: #0f172a;
}

.chat-list {
    overflow-y: auto;
    flex: 1;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 14px 18px;
    border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
    transition: background 0.15s ease;
}

.chat-item:hover {
    background: #f8fafc;
}

.chat-item.active {
    background: #f1f5f9;
    border-left: 4px solid var(--xtorg-neon-pink, #cd0b64);
}

.chat-item-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, #6366f1, #a855f7);
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 18px;
    flex-shrink: 0;
}

.chat-item-info {
    flex: 1;
    min-width: 0;
}

.chat-item-title {
    font-weight: 700;
    font-size: 15px;
    color: #0f172a;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-lastmsg {
    font-size: 13px;
    color: #64748b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 4px;
}

/* Окно сообщений */
.chat-main {
    display: flex;
    flex-direction: column;
    position: relative;
    background: #f1f5f9;
    height: 100%;
    overflow: hidden;
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #94a3b8;
    font-size: 15px;
    font-weight: 600;
}

.chat-active-box {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

.chat-box-header {
    padding: 12px 20px;
    background: #ffffff;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.btn-back-chats {
    display: none;
    background: #f1f5f9;
    border: none;
    padding: 8px 14px;
    border-radius: 20px;
    font-weight: 700;
    color: var(--xtorg-neon-pink, #cd0b64);
    cursor: pointer;
    font-size: 14px;
}

.chat-user-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: #0f172a;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
}

.chat-username {
    font-weight: 800;
    font-size: 16px;
    color: #0f172a;
}

.chat-ad-link {
    font-size: 12px;
    color: #64748b;
    font-weight: 600;
}

.messages-container {
    flex: 1;
    min-height: 0;
    /* Критично для корректного скролла */
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* --- ТВОИ СУЩЕСТВУЮЩИЕ СТИЛИ (ОБНОВЛЕНЫ ПОД СВАЙП) --- */
.msg-bubble {
    max-width: 65%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.45;
    position: relative;
    word-break: break-word;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);

    /* Добавлено для работы свайпа влево */
    touch-action: pan-y;
    will-change: transform;
    transition: transform 0.1s ease-out;
}

.msg-bubble.swiping {
    transition: none;
}

.msg-bubble.my {
    align-self: flex-end;
    background: #0f172a;
    color: #ffffff;
    border-bottom-right-radius: 4px;
}

.msg-bubble.their {
    align-self: flex-start;
    background: #ffffff;
    color: #0f172a;
    border: 1px solid #e2e8f0;
    border-bottom-left-radius: 4px;
}

.msg-reply-quote {
    background: rgba(0, 0, 0, 0.05);
    border-left: 3px solid var(--xtorg-neon-pink, #cd0b64);
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
    margin-bottom: 8px;
}

.msg-bubble.my .msg-reply-quote {
    background: rgba(255, 255, 255, 0.15);
    border-left-color: #38bdf8;
    color: #f1f5f9;
}


/* --- НОВЫЕ СТИЛИ: КОНТЕКСТНОЕ МЕНЮ (ПРИЖАТИЕ) --- */
.msg-context-menu {
    position: fixed;
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 12px;
    padding: 6px 0;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    z-index: 100000;
    min-width: 160px;
    display: flex;
    flex-direction: column;
    animation: fadeIn 0.15s ease-out;
}

.msg-context-btn {
    background: transparent;
    border: none;
    color: #f8fafc;
    padding: 10px 16px;
    text-align: left;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.12s ease;
}

.msg-context-btn:hover {
    background: #334155;
}

.msg-context-btn.danger {
    color: #f87171;
}

.msg-context-btn.danger:hover {
    background: rgba(239, 68, 68, 0.15);
}

.msg-context-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.1);
}

/* Контейнер альбома (Telegram-style) */
.msg-gallery {
    display: flex;
    flex-direction: column;
    gap: 3px;
    margin-top: 6px;
    border-radius: 12px;
    overflow: hidden;
    max-width: 320px;
    /* До ~60% ширины сообщения */
    width: 100%;
}

/* Строка альбома (максимум 2 фото в ряд) */
.msg-gallery-row {
    display: flex;
    gap: 3px;
    width: 100%;
}

/* Картинка внутри альбома */
.msg-gallery-item {
    flex: 1;
    position: relative;
    height: 150px;
    cursor: pointer;
    overflow: hidden;
}

/* Если фото одно в строке — делаем его выше */
.msg-gallery-row.single .msg-gallery-item {
    height: 220px;
}

.msg-gallery-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: opacity 0.15s ease;
}

.msg-gallery-item:hover img {
    opacity: 0.9;
}

/* Оверлей "+N" для остальных фото */
.msg-gallery-more-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.55);
    color: #ffffff;
    font-size: 22px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}

.msg-time {
    font-size: 11px;
    opacity: 0.65;
    text-align: right;
    margin-top: 4px;
}

/* Форма ввода (прижата книзу) */
.reply-preview-bar,
.attach-preview-bar {
    background: #ffffff;
    padding: 10px 18px;
    border-top: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.chat-input-form {
    padding: 12px 18px;
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.btn-attach-file {
    font-size: 22px;
    cursor: pointer;
    padding: 4px;
    user-select: none;
}

.chat-input-form input[type="text"] {
    flex: 1;
    padding: 12px 18px;
    border: 1px solid #cbd5e1;
    border-radius: 24px;
    outline: none;
    font-size: 14px;
    background: #f8fafc;
}

.btn-send-msg {
    background: var(--xtorg-neon-pink, #cd0b64);
    color: #ffffff;
    border: none;
    padding: 12px 22px;
    border-radius: 24px;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
}

/* МОДАЛЬНОЕ ОКНО ЗУМА ФОТО (ИСПРАВЛЕНО ДЛЯ МОБИЛОК) */
.zoom-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100dvh;
    background: rgba(0, 0, 0, 0.92);
    z-index: 999999;
    align-items: center;
    justify-content: center;
    touch-action: none;
}

.zoom-modal.show {
    display: flex;
}

.zoom-image-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
}

#chat-zoom-img {
    max-width: 95vw;
    max-height: 80dvh;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

.zoom-close {
    position: absolute;
    top: 15px;
    right: 20px;
    color: #ffffff;
    font-size: 32px;
    font-weight: bold;
    cursor: pointer;
    z-index: 1000000;
    padding: 10px;
    line-height: 1;
}

.zoom-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.2);
    color: #ffffff;
    border: none;
    font-size: 20px;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000000;
}

.zoom-prev {
    left: 10px;
}

.zoom-next {
    right: 10px;
}

@media (max-width: 768px) {
    .zoom-nav {
        width: 36px;
        height: 36px;
        font-size: 16px;
        background: rgba(0, 0, 0, 0.5);
    }
}

/* -------------------------------------------------------------
   МОБИЛЬНАЯ АДАПТАЦИЯ + СМЕЩЕНИЕ КНОПКИ В СТИЛЕ GOOGLE PLAY GAMES
------------------------------------------------------------- */
@media (max-width: 768px) {
    .chat-page-wrapper {
        height: calc(100dvh - 60px);
    }

    .chat-layout {
        grid-template-columns: 1fr;
    }

    .chat-sidebar {
        width: 100%;
        display: flex;
    }

    .chat-main {
        display: none;
        width: 100%;
    }

    .chat-main.active-mobile {
        display: flex;
    }

    .chat-sidebar.hidden-mobile {
        display: none;
    }

    .btn-back-chats {
        display: block;
    }

    .msg-bubble {
        max-width: 85%;
    }

    /* КНОПКА «+ Додати оголошення» — Спрятана в боковой язычок */
    .btn-create-ad {
        position: fixed !important;
        top: 40% !important;
        right: -32px !important;
        /* Прячем основную часть за экран */
        bottom: auto !important;
        left: auto !important;
        width: 48px !important;
        height: 48px !important;
        border-radius: 24px 0 0 24px !important;
        background: var(--xtorg-neon-pink, #cd0b64) !important;
        box-shadow: -2px 4px 12px rgba(0, 0, 0, 0.25) !important;
        z-index: 9999 !important;
        transition: right 0.25s cubic-bezier(0.4, 0, 0.2, 1) !important;
        opacity: 0.85;
    }

    /* При касании/наведении язычок выезжает полностью */
    .btn-create-ad:hover,
    .btn-create-ad:active,
    .btn-create-ad:focus {
        right: 0 !important;
        opacity: 1;
    }

    .btn-create-ad::before {
        content: '+' !important;
        font-size: 28px !important;
        color: #ffffff !important;
        position: absolute !important;
        left: 12px !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
    }
}

/* Контейнер предпросмотра перед отправкой */
.chat-preview-box {
    display: flex;
    gap: 8px;
    padding: 8px 12px;
    background: #18181b;
    border-top: 1px solid #27272a;
    overflow-x: auto;
    max-height: 80px;
}

.chat-preview-box.hidden {
    display: none !important;
}

/* Карточка миниатюры */
.preview-thumb-wrapper {
    position: relative;
    width: 56px;
    height: 56px;
    flex-shrink: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #3f3f46;
}

/* Запрещаем картинке растягивать контейнер */
.preview-thumb-wrapper img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    /* Сохраняет пропорции без искажений */
    display: block;
}

/* Кнопка крестика для удаления */
.preview-remove-btn {
    position: absolute;
    top: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.75);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    font-size: 11px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    transition: background 0.15s ease;
}

.preview-remove-btn:hover {
    background: #ef4444;
}

/* Контекстное меню для элементов чата в сайдбаре */
.chat-context-menu {
    position: fixed;
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 12px;
    padding: 6px 0;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    z-index: 100000;
    min-width: 160px;
    display: flex;
    flex-direction: column;
}

.chat-context-btn {
    background: transparent;
    border: none;
    color: #f8fafc;
    padding: 10px 16px;
    text-align: left;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: background 0.12s ease;
}

.chat-context-btn:hover {
    background: #334155;
}

.chat-context-btn.danger {
    color: #f87171;
}

.chat-context-btn.danger:hover {
    background: rgba(239, 68, 68, 0.15);
}

.chat-context-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 99999;
    background: rgba(0, 0, 0, 0.1);
}

/* ==========================================
   РЕСАЙЗЕР И СХЛОПЫВАНИЕ САЙДБАРА (ПК ВЕРСИЯ)
   ================================---------- */
@media (min-width: 769px) {
    .chat-sidebar {
        width: 360px;
        /* Стартовая ширина */
        min-width: 80px;
        /* Минимально: только аватарки (как в Телеграме) */
        max-width: 500px;
        /* Максимальная ширина */
        flex-shrink: 0;
        transition: width 0.05s ease;
    }

    /* Когда сайдбар сужен до минимума — скрываем заголовки и тексты */
    .chat-sidebar.collapsed-sidebar .chat-item-info,
    .chat-sidebar.collapsed-sidebar .sidebar-header h3 {
        display: none;
    }

    .chat-sidebar.collapsed-sidebar .chat-item {
        justify-content: center;
        padding: 14px 10px;
    }

    /* Полоса ресайзера */
    .chat-resizer {
        width: 6px;
        background: transparent;
        cursor: ew-resize;
        flex-shrink: 0;
        position: relative;
        z-index: 10;
    }

    .chat-resizer:hover,
    .chat-resizer.active {
        background: #cbd5e1;
    }

    .chat-main {
        flex: 1;
        min-width: 320px;
        /* Ограничение минимального размера как у мобильной версии */
    }
}

/* На мобилках скрываем ресайзер */
@media (max-width: 768px) {
    .chat-resizer {
        display: none;
    }
}