:root {
    --tg-bg-incoming: #ffffff;
    --tg-bg-outgoing: #dff1ff;
    --tg-bg-chat: #f2f5f8;
    --tg-bg-header: #ffffff;
    --tg-text: #17212b;
    --tg-text-muted: #7a8794;
    --tg-radius-chat: 15px;
    --tg-radius-msg: 12px;
}

body.popup-chat-page-locked {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
}

html.popup-chat-page-locked {
    overflow: hidden;
    overscroll-behavior: none;
}

body.popup-chat-page-locked::before {
    content: '';
    position: fixed;
    inset: 0;
    z-index: 9990;
    background: #f2f5f8;
    pointer-events: none;
}

/* Скрытие чата по умолчанию */
.popup-chat {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    opacity: 0;
    pointer-events: none;
    z-index: 99999;
    transition: opacity 0.3s ease;
}

.popup-chat--active {
    opacity: 1;
}

/* Затемнение заднего фона */
.popup-chat__backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1;
}

/* Контейнер чата, выезжающий снизу */
.popup-chat__container {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 2;
    width: 100%;
    height: 100svh;
    max-height: 100svh;
    background-color: var(--tg-bg-chat);
    border-radius: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.1, 0.76, 0.55, 0.94), bottom 0.1s ease, max-height 0.1s ease, height 0.1s ease;
}

.popup-chat.popup-chat--active .popup-chat__container {
    transform: translateY(0);
}

.popup-chat.popup-chat--active.popup-chat--collapsed .popup-chat__container {
    transform: translateY(calc(100% - 60px));
}

.popup-chat__back-icon {
    transition: transform 0.25s ease;
}

.popup-chat.popup-chat--collapsed .popup-chat__back-icon {
    transform: rotate(90deg);
}

.popup-chat.popup-chat--collapsed .popup-chat__header {
    cursor: pointer;
}

/* ==========================================================================
   2. ШАПКА ЧАТА (ПАРЯЩИЕ ОСТРОВКИ С БЛЮРОМ)
   ========================================================================== */
.popup-chat__header {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 60px;
    box-sizing: border-box;
    background-color: transparent;
    /* Полностью прозрачная подложка */
    padding: 10px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    z-index: 10;
}

.popup-chat--collapsed .popup-chat__header {
    background-color: var(--tg-bg-chat);
}

/* Кнопка назад — круглый островок */
.popup-chat__back-btn {
    width: 40px;
    height: 40px;
    padding: 0;
    background-color: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(23, 33, 43, 0.08);
    box-shadow: 0 8px 24px rgba(23, 33, 43, 0.08);
    border-radius: 50%;
    color: #2481cc;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.popup-chat__back-btn:active {
    background-color: rgba(255, 255, 255, 0.15);
}

.popup-chat__back-icon {
    width: 20px;
    height: 20px;
    margin-left: -2px;
    /* Оптический фикс центровки стрелки */
}

/* Блок с текстом по центру — овальный островок */
.popup-chat__header-info {
    flex-grow: 1;
    max-width: 60%;
    height: 40px;
    /* Фиксированная высота как у кнопки и аватарки */
    box-sizing: border-box;
    /* Чтобы паддинги не раздували высоту */
    background-color: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(23, 33, 43, 0.08);
    border-radius: 18px;
    padding: 2px 14px;
    /* Уменьшили вертикальные паддинги */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    /* Центрируем строки по вертикали внутри 40px */
    min-width: 0;
    box-shadow: 0 8px 24px rgba(23, 33, 43, 0.08);
}

.popup-chat__title {
    color: var(--tg-text);
    font-size: 14px;
    /* Уменьшили с 16px, чтобы аккуратно ложилось в два ряда */
    font-weight: 600;
    margin: 0;
    line-height: 1.2;
    text-align: center;
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.popup-chat__subtitle {
    color: var(--tg-text-muted);
    font-size: 11px;
    /* Уменьшили с 12px */
    margin-top: 1px;
    line-height: 1.1;
    text-align: center;
    width: 100%;
}

/* Аватарка менеджера справа */
.popup-chat__manager-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.popup-chat__avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ==========================================================================
   3. ТЕЛО ЧАТА И ЛЕНТА СООБЩЕНИЙ
   ========================================================================== */
.popup-chat__body {
    flex-grow: 1;
    overflow-y: auto;
    /* Отступы сверху и снизу по 72px, чтобы сообщения проплывали под островками */
    padding: 72px 16px 72px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background-color: var(--tg-bg-chat);
    -webkit-overflow-scrolling: touch;
}

/* Разделитель даты */
.popup-chat__date-divider {
    align-self: center;
    background: rgba(0, 0, 0, 0.3);
    color: var(--tg-text-muted);
    font-size: 12px;
    padding: 2px 12px;
    border-radius: 10px;
    margin: 8px 0;
}

/* Общие стили сообщений */
.popup-chat__message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 85%;
}

.popup-chat__message-content {
    padding: 8px 12px;
    position: relative;
    color: var(--tg-text);
    font-size: 15px;
    line-height: 1.4;
}

.popup-chat__message-text {
    margin: 0;
    padding-bottom: 4px;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.popup-chat__message-time {
    font-size: 11px;
    color: var(--tg-text-muted);
    float: right;
    margin-left: 8px;
}

/* Входящие (Менеджер) — Слева */
.popup-chat__message--incoming {
    align-self: flex-start;
}

.popup-chat__message--incoming .popup-chat__message-content {
    background-color: var(--tg-bg-incoming);
    border-radius: var(--tg-radius-msg) var(--tg-radius-msg) var(--tg-radius-msg) 4px;
}

.popup-chat__message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
}

/* Исходящие (Пользователь) — Справа */
.popup-chat__message--outgoing {
    align-self: flex-end;
}

.popup-chat__message--outgoing .popup-chat__message-content {
    background-color: var(--tg-bg-outgoing);
    border-radius: var(--tg-radius-msg) var(--tg-radius-msg) 4px var(--tg-radius-msg);
}

/* ==========================================================================
   4. ФУТЕР И ТЕКСТОВОЕ ПОЛЕ С БЛЮРОМ
   ========================================================================== */
.popup-chat__footer {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 10px 12px;
    background: transparent;
    /* Полностью прозрачный футер */
    border-top: none;
    z-index: 10;
}

.popup-chat__form { 
    width: 100%; 
    position: relative; 
}

/* Поле ввода как матовое стекло */
.popup-chat__input {
    width: 100%;
    background-color: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(23, 33, 43, 0.08);
    box-shadow: 0 8px 24px rgba(23, 33, 43, 0.08);
    border-radius: 18px;
    outline: none;
    color: var(--tg-text);
    font-size: 16px;
    resize: none;
    height: 38px;
    max-height: 120px;
    padding: 8px 40px 8px 12px;
    /* 40px справа защищает от заезда текста под кнопку */
    box-sizing: border-box;
    font-family: inherit;
    overflow-y: auto;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.popup-chat__input:focus {
    border-color: rgba(82, 136, 193, 0.4);
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}

.popup-chat__input::placeholder {
    color: #8a96a3;
}

/* Кнопка отправки внутри textarea */
.popup-chat__send-btn {
    background: url("../img/button.svg") center / contain no-repeat;
    width: 30px;
    height: 30px;
    border: none;
    cursor: pointer;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    right: 4px;
    bottom: 10px;
    z-index: 12;
}

.popup-chat__send-btn.is-loading {
    opacity: .4;
    pointer-events: none;
}

.popup-chat__subtitle--typing {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.popup-chat__typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 3px;
}

.popup-chat__typing-dots span {
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.35;
    animation: popupChatTypingDot 1.2s infinite ease-in-out;
}

.popup-chat__typing-dots span:nth-child(2) {
    animation-delay: 0.15s;
}

.popup-chat__typing-dots span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes popupChatTypingDot {
    0%, 80%, 100% {
        opacity: 0.35;
        transform: translateY(0);
    }

    40% {
        opacity: 1;
        transform: translateY(-2px);
    }
}

.popup-chat__message-status {
    display: inline-flex;
    align-items: center;
    margin-left: 4px;
    font-size: 11px;
    line-height: 1;
    opacity: 0.7;
}

.popup-chat__message-status::before {
    content: '✓';
}

.popup-chat__message-status--read::before {
    content: '✓✓';
}

/* ==========================================================================
   FIX: КЛИКИ ПО САЙТУ ПРИ СВЕРНУТОМ ЧАТЕ
   ========================================================================== */

/* В обычном скрытом состоянии чат ничего не перехватывает */
.popup-chat {
    pointer-events: none;
}

/* В раскрытом состоянии весь слой активен */
.popup-chat.popup-chat--active:not(.popup-chat--collapsed) {
    pointer-events: auto;
}

/* В раскрытом состоянии backdrop кликабельный, чтобы сворачивать чат */
.popup-chat.popup-chat--active:not(.popup-chat--collapsed) .popup-chat__backdrop {
    pointer-events: auto;
}

/* Контейнер чата всегда кликабельный */
.popup-chat .popup-chat__container {
    pointer-events: auto;
}

/* В свернутом состоянии общий слой не должен перекрывать сайт */
.popup-chat.popup-chat--collapsed {
    pointer-events: none;
}

/* В свернутом состоянии backdrop полностью отключаем */
.popup-chat.popup-chat--collapsed .popup-chat__backdrop {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

/* Но сам свернутый контейнер чата должен оставаться кликабельным */
.popup-chat.popup-chat--collapsed .popup-chat__container {
    pointer-events: auto;
}

/* ==========================================================================
   FIX: iOS keyboard background
   ========================================================================== */

html.popup-chat-keyboard-open,
body.popup-chat-keyboard-open {
    background-color: var(--tg-bg-chat) !important;
}

/* Если основной контент сайта просвечивает под клавиатурой */
body.popup-chat-keyboard-open main,
body.popup-chat-keyboard-open .site,
body.popup-chat-keyboard-open .wrapper,
body.popup-chat-keyboard-open #page,
body.popup-chat-keyboard-open .page {
    background-color: var(--tg-bg-chat) !important;
}

/* Сам слой чата тоже красим целиком */
body.popup-chat-keyboard-open .popup-chat {
    background-color: var(--tg-bg-chat);
}

/* Backdrop при открытой клавиатуре не нужен — он дает серый/сайтовый просвет */
body.popup-chat-keyboard-open .popup-chat__backdrop {
    background-color: var(--tg-bg-chat);
}

/* Нижняя маска на случай, если iOS показывает область под visualViewport */
body.popup-chat-keyboard-open .popup-chat.popup-chat--active:not(.popup-chat--collapsed)::after {
    content: '';
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 260px;
    background-color: var(--tg-bg-chat);
    z-index: 1;
    pointer-events: none;
}

body.popup-chat-keyboard-open .popup-chat__container {
    z-index: 2;
}

body.popup-chat-keyboard-open .popup-chat__footer {
    z-index: 20;
}

.popup-chat__message--incoming .popup-chat__message-content {
    background-color: var(--tg-bg-incoming);
    color: var(--tg-text);
    box-shadow: 0 2px 10px rgba(23, 33, 43, 0.06);
}

.popup-chat__message--outgoing .popup-chat__message-content {
    background-color: var(--tg-bg-outgoing);
    color: var(--tg-text);
}

.popup-chat__date-divider {
    background: rgba(255, 255, 255, 0.85);
    color: var(--tg-text-muted);
    box-shadow: 0 2px 10px rgba(23, 33, 43, 0.05);
}

.popup-chat__message-time {
    color: var(--tg-text-muted);
}

.popup-chat.popup-chat--collapsed .popup-chat__header {
    background-color: var(--tg-bg-chat);
}

html.popup-chat-keyboard-open,
body.popup-chat-keyboard-open,
body.popup-chat-keyboard-open .popup-chat,
body.popup-chat-keyboard-open .popup-chat__backdrop,
body.popup-chat-keyboard-open .popup-chat.popup-chat--active:not(.popup-chat--collapsed)::after {
    background-color: var(--tg-bg-chat) !important;
}

.popup-chat-launcher {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9998;

    display: flex;
    align-items: center;
    justify-content: center;

    min-height: 60px;
    padding: 10px 24px;

    background: #f1f4f7;

    opacity: 0;
    visibility: hidden;
    transform: translateY(100%);
    transition:
        opacity 0.25s ease,
        transform 0.25s ease,
        visibility 0.25s ease;
}

.popup-chat-launcher.is-visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.popup-chat-launcher__btn {
    width: min(330px, calc(100vw - 110px));
    min-height: 56px;

    padding: 14px 24px;

    border: 0;
    border-radius: 999px;

    background: var(--background);
    color: #fff;

    font-family: inherit;
    font-size: 16px;
    line-height: 22px;

    cursor: pointer;

    box-shadow: none;

    transition:
        transform 0.2s ease,
        box-shadow 0.2s ease;
}

.popup-chat-launcher__btn:hover {
    box-shadow: 0 5px 18px rgba(31, 42, 55, 0.1);
}

.popup-chat-launcher__btn:active {
    transform: scale(0.98);
}

@media (max-width: 767px) {
    .popup-chat-launcher {
        min-height: 60px;
        padding: 10px 20px;
    }

    .popup-chat-launcher__btn {
        width: min(330px, calc(100vw - 110px));
        min-height: 40px;
    }
}