/* Базовые стили для виджета чата */
#chat-widget-root {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
}

#cw-chat-button {
    width: 60px;
    height: 60px;
    background: #25D366;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

#cw-chat-button:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}

#cw-chat-button:active {
    transform: scale(0.95);
}

#cw-chat-window {
    position: absolute;
    bottom: 70px;
    right: 0;
    width: 380px;
    height: 550px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 5px 25px rgba(0,0,0,0.2);
    display: none;
    flex-direction: column;
    overflow: hidden;
}

#cw-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background: #e5ddd5;
}

#cw-chat-messages::-webkit-scrollbar {
    width: 6px;
}

#cw-chat-messages::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}

#cw-chat-messages::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

#cw-chat-messages::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* Анимация появления */
@keyframes cw-slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#cw-chat-window {
    animation: cw-slideIn 0.3s ease;
}

/* Стили для сообщений */
.cw-message {
    animation: cw-fadeIn 0.2s ease;
}

@keyframes cw-fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Стили для кнопок */
#cw-chat-window button {
    transition: opacity 0.2s ease, transform 0.1s ease;
    cursor: pointer;
}

#cw-chat-window button:active {
    transform: scale(0.97);
}

/* Стили для формы пароля */
#cw-password-prompt {
    border-radius: 8px;
    margin: 10px;
    animation: cw-slideDown 0.3s ease;
}

@keyframes cw-slideDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#cw-password-prompt input {
    border-radius: 5px;
    transition: border-color 0.2s;
}

#cw-password-prompt input:focus {
    outline: none;
    border-color: #28a745;
}

#cw-save-password-btn:hover {
    background: #218838;
}

#cw-skip-password-btn:hover {
    background: #5a626d;
}

/* ========== АДАПТИВНЫЕ СТИЛИ ДЛЯ МОБИЛЬНЫХ УСТРОЙСТВ ========== */

/* Планшеты (до 768px) */
@media (max-width: 768px) {
    #chat-widget-root {
        bottom: 15px;
        right: 15px;
    }

    #cw-chat-window {
        position: fixed;
        bottom: 15px;
        right: 15px;
        width: calc(100vw - 30px);
        max-width: 400px;
        height: calc(100vh - 100px);
        min-height: 400px;
        max-height: 650px;
        border-radius: 16px;
    }

    #cw-chat-button {
        width: 55px;
        height: 55px;
    }

    #cw-chat-button svg {
        width: 28px;
        height: 28px;
    }
}

/* Мобильные телефоны (до 480px) - ПОЛНОЭКРАННЫЙ РЕЖИМ */
@media (max-width: 480px) {
    /* Контейнер для кнопки - не растягиваем */
    #chat-widget-root {
        position: fixed;
        bottom: 15px;
        right: 15px;
        width: auto;
        height: auto;
        top: auto;
        left: auto;
        background: transparent;
        z-index: 9999;
    }
    
    /* Кнопка */
    #cw-chat-button {
        width: 50px;
        height: 50px;
        position: relative;
        bottom: auto;
        right: auto;
        z-index: 10001;
    }
    
    /* Когда окно чата открыто, скрываем кнопку */
    body.cw-chat-open #cw-chat-button {
        display: none !important;
    }
    
    /* Окно чата - полноэкранное, независимо от позиции кнопки */
    #cw-chat-window {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        height: 100% !important;
        max-width: none !important;
        max-height: none !important;
        min-height: auto !important;
        border-radius: 0 !important;
        margin: 0 !important;
        z-index: 10000 !important;
        bottom: auto !important;
        right: auto !important;
    }

    #cw-chat-button svg {
        width: 26px;
        height: 26px;
    }

    #cw-chat-messages {
        padding: 12px;
    }

    .cw-message {
        margin: 8px;
        padding: 6px 10px;
    }

    .cw-message-content {
        padding: 8px 12px;
        font-size: 14px;
    }

    .cw-message img,
    .cw-message video {
        max-width: 150px;
    }

    .cw-message-time {
        font-size: 9px;
    }

    #cw-chat-window > div:last-child {
        padding: 12px;
    }

    #cw-message-input {
        padding: 8px 12px;
        font-size: 15px;
    }

    #cw-send-btn {
        padding: 8px 16px;
        font-size: 14px;
    }

    #cw-attach-btn {
        font-size: 18px;
        width: 36px;
        height: 36px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    #cw-chat-window > div:first-child {
        padding: 10px 12px;
        font-size: 13px;
    }

    #cw-close-btn,
    #cw-edit-id-btn {
        font-size: 18px;
        padding: 5px;
        width: 32px;
        height: 32px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    #cw-password-prompt {
        padding: 12px;
        margin: 8px;
    }

    #cw-password-prompt > div:first-child {
        font-size: 12px;
    }

    #cw-password-input,
    #cw-confirm-password-input {
        padding: 8px;
        font-size: 14px;
    }

    #cw-save-password-btn,
    #cw-skip-password-btn {
        padding: 8px;
        font-size: 13px;
    }

    /* Индикатор загрузки */
    .cw-upload-loading,
    #cw-upload-loading {
        font-size: 12px;
        padding: 8px;
        margin: 8px;
    }
}

/* Очень маленькие телефоны (до 380px) */
@media (max-width: 380px) {
    .cw-message-content {
        max-width: 90%;
    }

    .cw-message img,
    .cw-message video {
        max-width: 120px;
    }
    
    #cw-chat-window > div:first-child span {
        font-size: 11px;
    }

    #cw-user-id-display {
        font-size: 11px;
    }

    #cw-message-input {
        font-size: 14px;
        padding: 6px 10px;
    }

    #cw-send-btn {
        padding: 6px 12px;
        font-size: 13px;
    }
}

/* Альбомная ориентация на мобильных */
@media (max-width: 768px) and (orientation: landscape) {
    #cw-chat-window {
        height: 100%;
    }

    #cw-chat-messages {
        max-height: calc(100vh - 120px);
    }
}

/* Touch-friendly увеличенные области нажатия */
@media (max-width: 768px) {
    #cw-close-btn,
    #cw-edit-id-btn,
    #cw-attach-btn,
    #cw-send-btn {
        min-width: 44px;
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    #cw-message-input {
        min-height: 40px;
    }

    .cw-message-actions button {
        min-width: 32px;
        min-height: 32px;
    }
}

/* Поддержка безопасной зоны на iPhone X и новее */
@supports (padding: max(0px)) {
    @media (max-width: 480px) {
        #cw-chat-window {
            padding-top: env(safe-area-inset-top);
            padding-bottom: env(safe-area-inset-bottom);
            padding-left: env(safe-area-inset-left);
            padding-right: env(safe-area-inset-right);
            box-sizing: border-box !important;
        }

        #cw-chat-window > div:first-child {
            padding-top: max(10px, env(safe-area-inset-top));
        }

        #cw-chat-window > div:last-child {
            padding-bottom: max(12px, env(safe-area-inset-bottom));
        }
    }
}

/* Темная тема (опционально) */
@media (prefers-color-scheme: dark) {
    #cw-chat-window {
        background: #1a1a1a;
    }

    #cw-chat-messages {
        background: #2c2c2c;
    }

    .cw-message.user .cw-message-content {
        background: #2e7d32;
        color: #fff;
    }

    .cw-message.admin .cw-message-content {
        background: #3c3c3c;
        color: #fff;
    }

    .cw-message.system .cw-message-content {
        background: #4a4a4a;
        color: #fff;
    }

    .cw-message-time {
        color: #aaa;
    }

    #cw-message-input {
        background: #3c3c3c;
        border-color: #555;
        color: #fff;
    }

    #cw-message-input::placeholder {
        color: #aaa;
    }

    #cw-password-prompt {
        background: #4a4a4a;
    }

    #cw-password-prompt > div:first-child {
        color: #ffc107;
    }

    .cw-upload-loading,
    #cw-upload-loading {
        background: #3c3c3c;
        color: #aaa;
    }

    #cw-chat-window > div:first-child {
        background: #0a5a44;
    }
}
