* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.ai-chat-widget {
    position: fixed;
    bottom: 8px;
    right: 8px;
    z-index: 10000;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    max-width: calc(100vw - 16px);
    max-height: 100vh;
    /* Предотвращаем выход за границы */
    left: auto;
    top: auto;
}

.chat-button {
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 8px 32px rgba(102, 126, 234, 0.4);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    color: white;
    position: fixed;
    bottom: 8px;
    right: 8px;
    z-index: 10001;
}

.chat-button::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
        opacity: 0.7;
    }
    50% {
        transform: scale(1.1);
        opacity: 0;
    }
}

.chat-button:hover {
    transform: scale(1.1) translateY(-2px);
    box-shadow: 0 12px 40px rgba(102, 126, 234, 0.6);
}

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

.chat-window {
    position: absolute;
    bottom: 90px;
    right: 0;
    width: 420px;
    min-width: 380px; /* Минимальная ширина, чтобы не становился полоской */
    max-width: calc(100vw - 40px);
    height: 650px;
    min-height: 500px; /* Минимальная высота */
    max-height: calc(100vh - 120px);
    background: #151b2e;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    display: none;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid #1e2742;
    /* Предотвращаем выход за границы */
    left: auto;
    transform: translateX(0);
}

/* На планшетах и мобильных виджет всегда виден */
@media (max-width: 1024px) {
    .chat-window {
        position: fixed;
        bottom: 8px;
        right: 8px;
        left: 8px;
    }
}

.chat-window.active {
    display: flex;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(30px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.chat-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 4px 20px rgba(102, 126, 234, 0.3);
    flex-shrink: 0; /* Шапка всегда видна */
}

.chat-header h3 {
    font-size: 20px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.close-button {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
    font-weight: 300;
}

.close-button:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: rotate(90deg);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    background: #0a0e27;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

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

.chat-messages::-webkit-scrollbar-track {
    background: #151b2e;
}

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

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #4a5568;
}

.message {
    display: flex;
    animation: fadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

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

.user-message {
    justify-content: flex-end;
}

.bot-message {
    justify-content: flex-start;
}

.message-content {
    max-width: 80%;
    padding: 14px 18px;
    border-radius: 20px;
    word-wrap: break-word;
    line-height: 1.5;
    font-size: 15px;
}

.user-message .message-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-bottom-right-radius: 6px;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.bot-message .message-content {
    background: #1e2742;
    color: #e0e6ed;
    border-bottom-left-radius: 6px;
    border: 1px solid #252d47;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.chat-input-container {
    display: flex;
    padding: 20px;
    background: #151b2e;
    border-top: 1px solid #1e2742;
    gap: 12px;
    align-items: center;
}

#chat-input {
    flex: 1;
    padding: 14px 18px;
    border: 2px solid #1e2742;
    border-radius: 24px;
    font-size: 15px;
    background: #0a0e27;
    color: #e0e6ed;
    transition: all 0.3s ease;
    font-family: inherit;
}

#chat-input::placeholder {
    color: #6b7280;
}

#chat-input:focus {
    outline: none;
    border-color: #667eea;
    background: #151b2e;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

#send-button {
    width: 48px;
    height: 48px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 50%;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
    flex-shrink: 0;
}

#send-button:hover {
    transform: scale(1.1) translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

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

.typing-indicator {
    display: flex;
    gap: 6px;
    padding: 14px 18px;
    background: #1e2742;
    border-radius: 20px;
    border-bottom-left-radius: 6px;
    width: fit-content;
}

.typing-indicator span {
    width: 10px;
    height: 10px;
    background: #667eea;
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.7;
    }
    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

/* Узкие десктопные экраны - предотвращаем превращение в полоску */
@media (max-width: 1400px) and (min-width: 1025px) {
    .chat-window {
        width: 400px;
        min-width: 380px;
    }
}

/* Планшеты и маленькие экраны */
@media (max-width: 1024px) {
    .ai-chat-widget {
        bottom: 8px;
        right: 8px;
        left: 8px;
        max-width: calc(100vw - 16px);
    }
    
    .chat-window {
        width: calc(100vw - 16px);
        min-width: auto; /* Убираем минимальную ширину на планшетах */
        max-width: calc(100vw - 16px);
        height: calc(100vh - 16px);
        min-height: auto; /* Убираем минимальную высоту на планшетах */
        max-height: calc(100vh - 16px);
        bottom: 8px;
        right: 8px;
        left: 8px;
        transform: translateX(0);
        border-radius: 16px;
    }
    
    /* Скрываем кнопку только когда виджет открыт на планшетах */
    .ai-chat-widget.chat-open .chat-button {
        display: none;
    }
}

/* Мобильная адаптация */
@media (max-width: 768px) {
    .ai-chat-widget {
        bottom: 8px;
        right: 8px;
        left: 8px;
        max-width: calc(100vw - 16px);
    }
    
    .chat-window {
        width: calc(100vw - 16px);
        min-width: auto; /* Убираем минимальную ширину на мобилках */
        max-width: calc(100vw - 16px);
        height: calc(100vh - 16px);
        min-height: auto; /* Убираем минимальную высоту на мобилках */
        max-height: calc(100vh - 16px);
        bottom: 8px;
        right: 8px;
        left: 8px;
        transform: translateX(0);
        border-radius: 16px;
    }
    
    /* Скрываем кнопку только когда виджет открыт на мобильных */
    .ai-chat-widget.chat-open .chat-button {
        display: none;
    }
    
    .chat-button {
        width: 56px;
        height: 56px;
        bottom: 8px;
        right: 8px;
    }
    
    .chat-header {
        padding: 16px;
        flex-shrink: 0;
    }
    
    .chat-header h3 {
        font-size: 17px;
    }
    
    .message-content {
        max-width: 85%;
        font-size: 14px;
        padding: 12px 16px;
    }
    
    .chat-messages {
        padding: 16px;
        flex: 1;
        min-height: 0;
    }
    
    .chat-input-container {
        padding: 14px;
        flex-shrink: 0;
    }
    
    #chat-input {
        font-size: 14px;
        padding: 12px 16px;
    }
    
    #send-button {
        width: 42px;
        height: 42px;
    }
}

@media (max-width: 480px) {
    .ai-chat-widget {
        bottom: 8px;
        right: 8px;
        left: 8px;
        max-width: calc(100vw - 16px);
        max-width: calc(100dvw - 16px); /* Динамический viewport для мобильных */
    }
    
    .chat-window {
        width: calc(100vw - 16px);
        width: calc(100dvw - 16px); /* Динамический viewport для мобильных */
        min-width: auto;
        height: calc(100vh - 16px);
        height: calc(100dvh - 16px); /* Динамический viewport для мобильных */
        min-height: auto;
        bottom: 8px;
        right: 8px;
        left: 8px;
        border-radius: 16px;
        max-width: calc(100vw - 16px);
        max-width: calc(100dvw - 16px);
        max-height: calc(100vh - 16px);
        max-height: calc(100dvh - 16px);
        transform: none;
    }
    
    /* Скрываем кнопку только когда виджет открыт на маленьких мобильных */
    .ai-chat-widget.chat-open .chat-button {
        display: none;
    }
    
    .chat-header {
        padding: 14px 16px;
        flex-shrink: 0;
    }
    
    .chat-header h3 {
        font-size: 15px;
    }
    
    .message-content {
        max-width: 88%;
        font-size: 13px;
        padding: 10px 14px;
        line-height: 1.4;
    }
    
    .chat-messages {
        padding: 14px;
        gap: 12px;
        flex: 1;
        min-height: 0;
    }
    
    .chat-input-container {
        padding: 12px;
        gap: 8px;
        flex-shrink: 0;
    }
    
    #chat-input {
        font-size: 14px;
        padding: 10px 14px;
    }
    
    #send-button {
        width: 38px;
        height: 38px;
        flex-shrink: 0;
    }
    
    .close-button {
        width: 30px;
        height: 30px;
        font-size: 18px;
    }
}

/* Очень маленькие экраны */
@media (max-width: 360px) {
    .chat-button {
        width: 48px;
        height: 48px;
        bottom: 12px;
        right: 12px;
    }
    
    .chat-header {
        padding: 12px 14px;
    }
    
    .chat-header h3 {
        font-size: 14px;
    }
    
    .message-content {
        font-size: 12px;
        padding: 9px 12px;
    }
    
    .chat-messages {
        padding: 12px;
    }
    
    .chat-input-container {
        padding: 10px;
    }
    
    #chat-input {
        font-size: 13px;
        padding: 9px 12px;
    }
    
    #send-button {
        width: 36px;
        height: 36px;
    }
}
