/* ==========================================================
   Chat Widget — Фарма+ Live Chat
   FAB + меню каналов + live-чат с пузырьками
   ========================================================== */

/* --- FAB (Floating Action Button) --- */
.cw-fab {
    position: fixed;
    bottom: 24px;
    right: 22px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--color-primary, #007bff);
    color: #fff;
    border: none;
    cursor: pointer;
    z-index: 10000;
    box-shadow: 0 4px 15px rgba(0, 123, 255, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s, background 0.2s;
}
.cw-fab:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 123, 255, 0.5);
}
.cw-fab.active {
    background: #5a6268;
}
.cw-fab svg {
    transition: transform 0.25s;
}
.cw-fab.active svg {
    transform: rotate(45deg);
}

/* Badge: online-пульс по умолчанию, счётчик при непрочитанных */
.cw-fab__badge {
    position: absolute;
    top: 2px;
    right: 2px;
    min-width: 12px;
    height: 12px;
    background: #28a745;
    border-radius: 50%;
    border: 2px solid #fff;
    animation: cw-pulse 2s ease-in-out infinite;
    font-size: 0;
    line-height: 12px;
    text-align: center;
    color: #fff;
    font-weight: 700;
}
.cw-fab__badge.has-unread {
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    font-size: 11px;
    line-height: 16px;
    padding: 0 4px;
    background: #dc3545;
    animation: none;
    top: -4px;
    right: -4px;
}
@keyframes cw-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(40, 167, 69, 0.5); }
    50% { box-shadow: 0 0 0 6px rgba(40, 167, 69, 0); }
}

/* --- Menu --- */
.cw-menu {
    position: fixed;
    bottom: 90px;
    right: 22px;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition: opacity 0.2s, transform 0.2s, visibility 0.2s;
}
.cw-menu.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}
.cw-menu__item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: #fff;
    border-radius: 28px;
    text-decoration: none;
    color: #333;
    font-size: 14px;
    font-weight: 500;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
    white-space: nowrap;
    transition: box-shadow 0.2s, transform 0.15s;
    cursor: pointer;
    border: none;
    font-family: inherit;
    line-height: 1.3;
}
.cw-menu__item:hover {
    box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
    transform: translateX(-4px);
}
.cw-menu__icon {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.cw-menu__icon--tg { background: #0088cc; }
.cw-menu__icon--wa { background: #25D366; }
.cw-menu__icon--phone { background: #007bff; }
.cw-menu__icon--chat { background: #1976d2; }
.cw-menu__icon svg { width: 20px; height: 20px; fill: #fff; }

/* --- Chat window --- */
.cw-chat {
    position: fixed;
    bottom: 90px;
    right: 22px;
    width: 380px;
    max-width: calc(100vw - 32px);
    max-height: calc(100vh - 120px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.18);
    z-index: 10001;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition: opacity 0.25s, transform 0.25s, visibility 0.25s;
}
.cw-chat.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

/* Header */
.cw-chat__header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%);
    color: #fff;
    flex-shrink: 0;
}
.cw-chat__avatar-wrap {
    position: relative;
    flex-shrink: 0;
}
.cw-chat__avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    border: 2.5px solid rgba(255, 255, 255, 0.6);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
.cw-chat__online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 12px;
    height: 12px;
    background: #4caf50;
    border-radius: 50%;
    border: 2.5px solid #0d47a1;
    box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.5);
    animation: cw-online-pulse 2.5s ease-in-out infinite;
}
@keyframes cw-online-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(76, 175, 80, 0.4); }
    50% { box-shadow: 0 0 0 4px rgba(76, 175, 80, 0); }
}
.cw-chat__header-info {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
    gap: 2px;
}
.cw-chat__title {
    font-size: 15px;
    font-weight: 600;
    line-height: 1.2;
    letter-spacing: 0.01em;
}
.cw-chat__subtitle {
    font-size: 12px;
    opacity: 0.85;
    line-height: 1.2;
}
.cw-chat__online-text {
    color: #a5d6a7;
    font-weight: 500;
}
.cw-chat__close {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 6px;
    line-height: 1;
    opacity: 0.8;
    transition: opacity 0.15s, background 0.15s;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.cw-chat__close:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.2);
}

/* Screens */
.cw-screen {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* --- Screen 1: Start form --- */
.cw-chat__body-wrap { flex: 0 0 auto; }
.cw-chat__body {
    padding: 18px;
    background: #f0f4f8;
}
.cw-chat__greeting {
    background: #fff;
    border-radius: 12px;
    padding: 14px 16px;
    font-size: 14px;
    line-height: 1.5;
    color: #333;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
    position: relative;
}
.cw-chat__greeting::after {
    content: '';
    position: absolute;
    bottom: -6px;
    left: 20px;
    width: 12px;
    height: 12px;
    background: #fff;
    transform: rotate(45deg);
    box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.04);
}
.cw-chat__form {
    padding: 16px 18px 18px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.cw-chat__field { position: relative; }
.cw-chat__field label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: #555;
    margin-bottom: 4px;
}
.cw-chat__field input,
.cw-chat__field textarea {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    color: #333;
    background: #fff;
    transition: border-color 0.15s;
    box-sizing: border-box;
}
.cw-chat__field input:focus,
.cw-chat__field textarea:focus {
    outline: none;
    border-color: var(--color-primary, #007bff);
}
.cw-chat__field input.error,
.cw-chat__field textarea.error { border-color: #dc3545; }
.cw-chat__field textarea {
    resize: vertical;
    min-height: 70px;
    max-height: 140px;
}
.cw-chat__field .cw-field-error {
    font-size: 12px;
    color: #dc3545;
    margin-top: 3px;
    display: none;
}
.cw-chat__submit {
    padding: 12px;
    background: var(--color-primary, #007bff);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
    font-family: inherit;
}
.cw-chat__submit:hover { background: #0056b3; }
.cw-chat__submit:disabled { opacity: 0.6; cursor: not-allowed; }

/* Form error */
.cw-chat__error {
    padding: 10px 14px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 8px;
    color: #dc3545;
    font-size: 13px;
    display: none;
    margin: 0 18px;
}
.cw-chat__error.show { display: block; }

/* Honeypot */
.cw-hp { position: absolute; left: -9999px; opacity: 0; height: 0; }

/* --- Screen 2: Live chat messages --- */
#cw-screen-chat {
    flex-direction: column;
}
.cw-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: #f0f4f8;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 200px;
    max-height: 400px;
}

/* Message bubbles */
.cw-msg {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: cw-fadeIn 0.2s ease;
}
@keyframes cw-fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.cw-msg--visitor { align-self: flex-end; }
.cw-msg--operator {
    align-self: flex-start;
    flex-direction: row;
    align-items: flex-end;
    gap: 6px;
}
.cw-msg__avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}
.cw-msg__content {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.cw-msg__bubble {
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.45;
    word-break: break-word;
    white-space: pre-wrap;
}
.cw-msg--visitor .cw-msg__bubble {
    background: linear-gradient(135deg, #1a73e8, #1565c0);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.cw-msg--operator .cw-msg__bubble {
    background: #fff;
    color: #333;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.cw-msg__meta {
    font-size: 11px;
    color: #999;
    margin-top: 3px;
    padding: 0 4px;
}
.cw-msg--visitor .cw-msg__meta { text-align: right; }

/* System message */
.cw-msg--system {
    align-self: center;
    max-width: 90%;
    text-align: center;
    font-size: 13px;
    color: #888;
    padding: 8px 16px;
    background: #e8ecf0;
    border-radius: 12px;
}

/* Input area */
.cw-input-area {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border-top: 1px solid #e5e7eb;
    background: #fff;
    flex-shrink: 0;
}
.cw-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid #d1d5db;
    border-radius: 20px;
    font-size: 14px;
    font-family: inherit;
    color: #333;
    background: #f9fafb;
    outline: none;
    transition: border-color 0.15s;
    box-sizing: border-box;
}
.cw-input:focus { border-color: var(--color-primary, #007bff); }
.cw-input:disabled { opacity: 0.5; cursor: not-allowed; }
.cw-send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--color-primary, #007bff);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s;
}
.cw-send-btn:hover { background: #0056b3; }
.cw-send-btn svg { width: 18px; height: 18px; }

/* Send error */
#cw-send-error {
    padding: 8px 14px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 8px;
    color: #dc3545;
    font-size: 12px;
    display: none;
    margin: 0 14px;
}
#cw-send-error.show { display: block; }

/* --- Mobile: bottom-sheet --- */
@media (max-width: 480px) {
    .cw-chat {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        max-height: 90vh;
        border-radius: 16px 16px 0 0;
    }
    .cw-messages {
        max-height: none;
        flex: 1;
        min-height: 150px;
    }
    .cw-menu {
        right: 16px;
        bottom: 84px;
    }
    .cw-fab {
        right: 16px;
        bottom: 18px;
    }
    .cw-input-area {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
    }
}
