/**
 * TOCHKA Chat Window v22.9.2 — Atmos Core Edition
 * Фичи: Smart Grouping, Dark Glass, iOS Bubbles, Fluid Input, Status Indicators
 * Интеграция: Safe Areas, Report Trigger (2026-02-01), Haptic Feedback Ready
 * ОБНОВЛЕНО: [2026-05-13] — Оптимизация пространства между сообщениями
 * FIX: Убран отрицательный отступ, добавлено 2px "воздуха" внутри групп
 */

:root {
    --chat-bg: #000000;
    --msg-in-bg: rgba(255, 255, 255, 0.08);
    --msg-out-grad: linear-gradient(145deg, #FF3B30, #D72C24);
    --msg-border: rgba(255, 255, 255, 0.12);
    --safe-top: env(safe-area-inset-top, 20px);
    --safe-bottom: env(safe-area-inset-bottom, 20px);
    --primary: #FF3B30;
    --ios-blue: #0A84FF;
    --read-blue: #34b7f1; /* Цвет прочтения как в мессенджерах */
}

/* Основной контейнер окна чата */
.chat-window-v22 {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: -webkit-fill-available; /* Фикс для мобильного Safari */
    background: var(--chat-bg);
    position: relative;
    overflow: hidden;
    padding-top: var(--safe-top);
}

/* Лента сообщений с эффектом глубины */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 4px; /* Базовый отступ между баблами */
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    background: radial-gradient(circle at top right, rgba(255,59,48,0.08), transparent 450px),
                radial-gradient(circle at bottom left, rgba(255,59,48,0.03), transparent 300px);
}

/* Скрытие скроллбара для эстетики iOS */
#chat-messages::-webkit-scrollbar {
    width: 0px;
    background: transparent;
}

/* Обертка сообщения */
.message-wrapper {
    display: flex;
    flex-direction: column;
    width: 100%;
    position: relative;
    max-width: 85%;
    margin-bottom: 16px; /* Увеличен отступ между разными группами */
    will-change: transform, opacity;
}

/* Базовый бабл */
.bubble {
    padding: 10px 16px;
    border-radius: 20px;
    font-size: 16px;
    line-height: 1.45;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 4px 15px rgba(0,0,0,0.25);
    transition: transform 0.2s cubic-bezier(0.32, 0.72, 0, 1);
}

/* Входящие (Glass Dark) */
.msg-in { 
    align-self: flex-start; 
    margin-right: auto;
}
.msg-in .bubble {
    background: var(--msg-in-bg);
    backdrop-filter: blur(25px) saturate(180%);
    -webkit-backdrop-filter: blur(25px) saturate(180%);
    color: #ffffff;
    border: 1px solid var(--msg-border);
    border-bottom-left-radius: 6px;
}

/* Исходящие (TOCHKA Red Gradient) */
.msg-out { 
    align-self: flex-end; 
    margin-left: auto;
}
.msg-out .bubble {
    background: var(--msg-out-grad);
    color: #ffffff;
    border-bottom-right-radius: 6px;
    box-shadow: 0 4px 12px rgba(255, 59, 48, 0.2);
}

/* --- СМАРТ-ГРУППИРОВКА (iOS Style) --- */

/* Сообщения в одной группе (один автор подряд) */
.msg-in + .msg-in, 
.msg-out + .msg-out { 
    margin-top: 2px; /* Добавлено пространство */
}

/* Скрываем аватар и имя у всех ПОВТОРНЫХ сообщений в группе */
.msg-in + .msg-in .msg-author {
    display: none !important;
}

/* Скругляем углы баблов для визуальной связки */
.msg-in + .msg-in .bubble { 
    border-top-left-radius: 6px; 
}
.msg-out + .msg-out .bubble { 
    border-top-right-radius: 6px; 
}

/* Мета-информация (Время + Статус) */
.bubble-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    font-size: 11px;
    margin-top: 4px;
    opacity: 0.8;
    font-weight: 400;
}

/* --- ИНДИКАТОРЫ СТАТУСА (ГАЛОЧКИ) --- */
.status-icon {
    display: inline-block;
    width: 16px;
    height: 11px;
    vertical-align: middle;
    transition: background-color 0.3s ease, mask 0.3s ease;
}

/* Одна галочка (Отправлено) */
.status-icon.icon-sent {
    mask: url('/wp-content/themes/generatepress_child/assets/img/icons/check.svg') no-repeat center;
    -webkit-mask: url('/wp-content/themes/generatepress_child/assets/img/icons/check.svg') no-repeat center;
    mask-size: contain;
    -webkit-mask-size: contain;
    background-color: rgba(255, 255, 255, 0.5);
}

/* Две галочки (Прочитано) */
.status-icon.icon-read {
    width: 19px; 
    mask: url('/wp-content/themes/generatepress_child/assets/img/icons/check-double.svg') no-repeat center;
    -webkit-mask: url('/wp-content/themes/generatepress_child/assets/img/icons/check-double.svg') no-repeat center;
    mask-size: contain;
    -webkit-mask-size: contain;
    background-color: var(--read-blue);
}

/* Состояние: Прочитано (Синие галочки) */
.is-read-confirmed .status-icon.icon-read {
    background-color: var(--read-blue);
    opacity: 1;
}

/* Триггер жалобы */
.msg-report-trigger {
    position: absolute;
    left: -45px; 
    top: 50%;
    transform: translateY(-50%);
    opacity: 0;
    padding: 12px;
    cursor: pointer;
    transition: opacity 0.25s, transform 0.2s;
    color: rgba(255, 255, 255, 0.25);
    background: none;
    border: none;
    font-size: 18px;
}
.msg-in:hover .msg-report-trigger { 
    opacity: 1; 
    transform: translateY(-50%) scale(1.1);
}

/* Футер (Input Area) */
.chat-footer {
    padding: 12px 12px calc(12px + var(--safe-bottom));
    background: rgba(10, 10, 12, 0.82);
    backdrop-filter: blur(40px) saturate(160%);
    -webkit-backdrop-filter: blur(40px) saturate(160%);
    border-top: 1px solid var(--msg-border);
    z-index: 100;
}

.input-container {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 26px;
    padding: 6px 6px 6px 18px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.input-container:focus-within {
    border-color: var(--primary);
    background: rgba(255, 255, 255, 0.09);
    box-shadow: 0 0 0 3px rgba(255, 59, 48, 0.1);
}

#chat-input {
    flex: 1;
    background: transparent;
    border: none;
    color: #ffffff;
    padding: 10px 0;
    font-size: 17px;
    outline: none;
    min-height: 44px;
    max-height: 150px;
    overflow-y: auto;
    resize: none; 
    line-height: 1.3;
}

/* Кнопка отправки */
.btn-send {
    background: var(--msg-out-grad);
    color: #ffffff;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: none;
    flex-shrink: 0;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s;
    box-shadow: 0 4px 10px rgba(255, 59, 48, 0.3);
    cursor: pointer;
}

.btn-send:disabled {
    opacity: 0.3;
    transform: scale(0.9);
}

/* --- АНИМАЦИИ (ОБНОВЛЕНО 2026-05-13) --- */
.animate-msg {
    animation: bubblePopIn 0.3s ease-out forwards; /* Время уменьшено до 0.3s */
    transition-delay: 0s; /* Нулевая задержка для мгновенного отклика */
}

@keyframes bubblePopIn {
    0% { transform: translateY(10px); opacity: 0; } /* Уменьшена амплитуда смещения */
    100% { transform: translateY(0); opacity: 1; }
}

.ios-shake {
    animation: iosShake 0.4s ease-in-out;
    border-color: #ff3b30 !important;
}

@keyframes iosShake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-8px); }
    75% { transform: translateX(8px); }
}

/* Оформление автора сообщения */
.msg-author {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    padding-left: 4px;
    cursor: pointer;
    transition: opacity 0.2s;
}

.msg-author:hover { 
    opacity: 0.8; 
}

.chat-msg-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    object-fit: cover;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.chat-msg-author-name {
    font-size: 13px;
    color: #8E8E93; /* Цвет системных подписей iOS */
    font-weight: 500;
}

/* Скрываем автора для своих сообщений */
.msg-out .msg-author { 
    display: none; 
}

/* Индикатор набора текста */
.typing-indicator {
    padding: 10px 16px;
    display: flex;
    gap: 4px;
}
.typing-indicator span {
    width: 6px;
    height: 6px;
    background: rgba(255,255,255,0.4);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}