/* --- Глобальные переменные и сброс --- */
:root {
    --bg-dark: #0f111a;
    --bg-sidebar: #ffffff;
    --chat-received: #f1f3f9;
    --chat-sent: #0d6efd;
    --top-nav-height: 60px;
    --sidebar-width: 320px;
}

body,
.app-wrapper {
    height: 100dvh;
    overflow: hidden;
}

body {
    background: #f8f9fa;
    font-family: Inter, -apple-system, sans-serif;
}

.app-wrapper {
    display: flex;
    flex-direction: column;
}

/* --- Вспомогательные классы и базовые элементы --- */
.chat-item,
.cursor-pointer {
    cursor: pointer;
}

.logo-circle,
.msg-status,
.btn-icon,
.scroll-down-btn {
    display: flex;
    align-items: center;
}

.logo-circle {
    width: 40px;
    height: 40px;
    background: #0d6efd;
    color: #fff;
    justify-content: center;
    border-radius: 30%;
    overflow: hidden;
    z-index: 1000;
}

.bg-dark-soft {
    background: #232735 !important;
}

.smaller {
    font-size: .8rem;
}

/* --- Навигация и Сетка --- */
.top-nav {
    height: var(--top-nav-height);
    background: var(--bg-dark);
    z-index: 1000;
}

.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.sidebar {
    width: var(--sidebar-width);
    background: #fff;
    display: flex;
    flex-direction: column;
}

/* --- Список чатов --- */
.chat-list {
    flex-grow: 1;
    overflow-y: auto;
}

.chat-item {
    transition: .2s;
    border-radius: 12px;
    margin: 0 8px;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.chat-item:hover {
    background: #f8f9fa;
}

.chat-item.active {
    background: #eef2ff;
    color: #0d6efd;
}

.chat-item.unread-chat .text-truncate {
    color: var(--chat-sent) !important;
    font-weight: 600 !important;
}

.unread-chat .smaller {
    color: #007bff !important;
}

.unread-dot {
    width: 8px;
    height: 8px;
    background-color: #007bff;
    border-radius: 50%;
    flex-shrink: 0;
}

/* --- Аватары --- */
.avatar,
.avatar-sm {
    background: #ddd;
    border-radius: 12px;
    flex-shrink: 0;
}

.avatar {
    width: 45px;
    height: 45px;
    position: relative;
}

.avatar-sm {
    width: 32px;
    height: 32px;
}

.online::after {
    content: '';
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 12px;
    height: 12px;
    background: #22c55e;
    border: 2px solid #fff;
    border-radius: 50%;
}

/* --- Окно чата --- */
.chat-window {
    flex-grow: 1;
    background: #fff;
}

.chat-header {
    height: 64px;
}

.chat-body {
    background: #fdfdfd;
    overflow-y: auto;
}

/* --- Сообщения --- */
.msg-received,
.msg-sent {
    display: flex;
    width: 100%;
    margin-bottom: 15px;
}

.msg-sent {
    justify-content: flex-end;
}

.msg-received {
    justify-content: flex-start;
}

.msg-bubble {
    max-width: 70%;
    min-width: 80px;
    border-radius: 18px;
    font-size: .95rem;
    padding: 8px 60px 8px 12px;
    word-wrap: break-word;
    position: relative;
    flex-grow: 0;
    flex-shrink: 0;
}

.msg-sent .msg-bubble {
    background: var(--chat-sent);
    color: #fff;
    border-bottom-right-radius: 4px;
    box-shadow: 0 4px 12px rgba(13, 110, 253, .2);
}

.msg-received .msg-bubble {
    background: var(--chat-received);
    color: inherit;
    border-bottom-left-radius: 4px;
}

.msg-status,
.msg-time {
    position: absolute;
    bottom: 4px;
    right: 10px;
    font-size: .7rem;
    pointer-events: none;
}

.msg-time {
    opacity: .7;
}

.msg-status {
    gap: 3px;
    opacity: .8;
    transition: .3s;
    display: inline-flex;
}

.status-icon {
    font-size: 1rem;
    line-height: 1;
}

.status-read {
    color: #4fc3f7;
}

.msg-sent .status-icon:not(.status-read) {
    color: rgba(255, 255, 255, .7);
}

/* --- Разделитель дат --- */
.chat-date-separator {
    display: flex;
    align-items: center;
    text-align: center;
    margin: 20px 0;
    color: #888;
    font-size: .85rem;
}

.chat-date-separator::after,
.chat-date-separator::before {
    content: '';
    flex: 1;
    border-bottom: 1px solid #eee;
}

.chat-date-separator:not(:empty)::before {
    margin-right: 1em;
}

.chat-date-separator:not(:empty)::after {
    margin-left: 1em;
}

/* --- Подвал и инпуты --- */
.input-wrapper {
    background: #f8f9fa;
    border-radius: 20px;
    padding: 8px 12px;
    display: flex;
    align-items: center;
}

.btn-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    padding: 0;
    justify-content: center;
    flex-shrink: 0;
}

.chat-footer {
    padding-bottom: calc(1rem + env(safe-area-inset-bottom)) !important;
    background: #fff;
}

/* --- Кастомные элементы --- */
.nav-logo-img {
    width: 130%;
    height: 130%;
    object-fit: contain;
}


#search-results {
    max-height: 400px;
    overflow-y: auto;
    scrollbar-width: thin;
}

.hover-bg:hover {
    background-color: rgba(var(--bs-primary-rgb), .1);
}

.scroll-down-btn {
    position: absolute;
    right: 20px;
    bottom: 80px;
    width: 40px;
    height: 40px;
    background-color: #fff;
    color: #007bff;
    border-radius: 50%;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, .15);
    cursor: pointer;
    z-index: 1000;
    transition: .3s;
    opacity: 1;
}

.scroll-down-btn:hover {
    background-color: #f8f9fa;
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, .2);
}

.scroll-down-btn.d-none {
    display: none !important;
}

.scroll-down-btn i {
    font-size: 1.2rem;
    line-height: 0;
}

#contextMenu {
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, .15);
    padding: 8px;
    min-width: 200px;
    max-width: 90vw;
    font-size: 1rem;
}

#contextMenu a {
    display: block;
    padding: 8px 12px;
    color: #333;
    text-decoration: none;
    border-radius: 6px;
}

#contextMenu a:hover {
    background-color: #f5f5f5;
}

/* --- Скроллбар --- */
.custom-scrollbar::-webkit-scrollbar {
    width: 5px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
    background: #eee;
    border-radius: 10px;
}

/* --- Анимации --- */
@keyframes statusPulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.4);
    }
}

/* --- Медиа-запросы --- */
@media (max-height: 500px) {
    .top-nav {
        display: none;
    }
}

@media (max-width: 767.98px) {
    .sidebar {
        width: 100% !important;
        display: flex;
    }

    .chat-window,
    .search-bar {
        display: none !important;
    }

    /* Базовое состояние мобильного окна чата (когда закрыт) */
    .chat-window {
        position: fixed;
        top: var(--top-nav-height);
        left: 0;
        width: 100%;
        height: calc(100dvh - var(--top-nav-height));
        z-index: 1050;
        background: #fff;
    }

    .chat-window.mobile-active {
        display: flex !important;
    }

    .chat-body {
        flex: 1;
        overflow-y: auto;
    }

    .chat-footer {
        flex-shrink: 0;
        padding-bottom: calc(15px + env(safe-area-inset-bottom)) !important;
    }

}



/* --- Глобальный принудительный режим скрытия при открытом чате --- */
body.chat-opened .top-nav,
body.chat-opened .sidebar .sidebar-footer {
    display: none !important;
    visibility: hidden !important;
    height: 0 !important;
    padding: 0 !important;
    overflow: hidden !important;
}

/* Принудительно разворачиваем окно чата, обнуляя любые отступы сверху */
body.chat-opened .chat-window {
    top: 0 !important;
    margin-top: 0 !important;
}

/* --- Жесткое мобильное скрытие шапки и статус-бара --- */
@media (max-width: 767.98px) {

    /* Скрываем верхнюю навигацию */
    body.chat-opened .top-nav {
        display: none !important;
        /* На случай, если d-flex уступит */
        height: 0 !important;
        min-height: 0 !important;
        padding-top: 0 !important;
        padding-bottom: 0 !important;
        margin: 0 !important;
        border: none !important;
        opacity: 0 !important;
        overflow: hidden !important;
        visibility: hidden !important;
    }

    /* Скрываем нижний статус-бар в сайдбаре */
    body.chat-opened .sidebar .sidebar-footer {
        display: none !important;
        height: 0 !important;
        min-height: 0 !important;
        padding-top: 0 !important;
        padding-bottom: 0 !important;
        margin: 0 !important;
        border: none !important;
        opacity: 0 !important;
        overflow: hidden !important;
        visibility: hidden !important;
    }

    /* Принудительно разворачиваем окно активного чата на весь экран */
    body.chat-opened .chat-window.mobile-active {
        top: 0 !important;
        height: 100dvh !important;
    }
}

/* Персонализированный фикс для PWA-режима */
@media (max-width: 767.98px) and (display-mode: standalone) {
    body.chat-opened .chat-window.mobile-active {
        height: 100vh !important;
        padding-bottom: env(safe-area-inset-bottom) !important;
    }
}

@keyframes spin {
    100% {
        transform: rotate(360deg);
    }
}

.spin-icon {
    display: inline-block;
    animation: spin 1s linear infinite;
}

/* retry menu */
#chat-context-menu {
    min-width: 130px;
    background: white;
    border-radius: 8px;
    font-size: 14px;
}

#chat-context-menu .dropdown-item {
    cursor: pointer;
    transition: background 0.2s;
}

#chat-context-menu .dropdown-item:hover {
    background-color: #f8f9fa;
}