.ai-chat-widget,
.ai-chat-widget *,
.ai-chat-widget *::before,
.ai-chat-widget *::after {
    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;
    
    /* CSS переменные для цветов */
    --widget-primary-color: #667eea;
    --widget-secondary-color: #764ba2;
    --widget-button-color: #667eea;
    --widget-message-color: #667eea;
    --widget-background-color: #151b2e;
    --widget-header-color: #667eea;
    --widget-header-text-color: #ffffff;
    --widget-user-message-color: #667eea;
    --widget-bot-message-color: #1e2742;
    --widget-text-color: #e0e6ed;
    --widget-messages-area-color: #0a0e27;
    --widget-input-background-color: #0a0e27;
    --widget-border-color: #1e2742;
    --widget-button-size: 72px;
}

/* Контейнер для единого блока виджета */
.ai-chat-widget-container {
    position: fixed;
    bottom: 8px;
    right: 8px;
    z-index: 10001;
    display: flex;
    align-items: stretch;
    gap: 0;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    line-height: 0;
}

.chat-button {
    width: var(--widget-button-size);
    height: var(--widget-button-size);
    background: linear-gradient(135deg, var(--widget-button-color) 0%, var(--widget-secondary-color) 100%);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    color: white;
    position: relative;
    z-index: 2;
    flex-shrink: 0;
    vertical-align: middle;
    line-height: 0;
}

.chat-button::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--widget-button-color) 0%, var(--widget-secondary-color) 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(0, 0, 0, 0.4);
}

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

.chat-expanded-message {
    background: linear-gradient(135deg, var(--widget-message-color) 0%, var(--widget-secondary-color) 100%);
    color: white;
    padding: 0 32px;
    border-radius: 28px 0 0 28px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    opacity: 0;
    transform: translateX(20px);
    pointer-events: none;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
    font-size: 18px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    max-width: calc(100vw - 100px);
    position: relative;
    z-index: 1;
    vertical-align: middle;
    line-height: 1;
    height: var(--widget-button-size);
    box-sizing: border-box;
}

.ai-chat-widget.expanded .chat-expanded-message {
    opacity: 1;
    transform: translateX(0);
    pointer-events: auto;
}

.ai-chat-widget.expanded .chat-button {
    border-radius: 0 50% 50% 0;
}

.chat-expanded-message:hover {
    transform: translateX(-2px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

.chat-expanded-message:active {
    transform: translateX(0) scale(0.98);
}

.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: var(--widget-background-color);
    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 var(--widget-border-color);
    /* Предотвращаем выход за границы */
    left: auto;
    transform: translateX(0);
    /* Убеждаемся, что окно чата имеет правильный stacking context */
    isolation: isolate;
    z-index: 10000;
}

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

.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, var(--widget-header-color) 0%, var(--widget-secondary-color) 100%);
    color: var(--widget-header-text-color);
    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;
    color: var(--widget-header-text-color);
}

.close-button {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: var(--widget-header-text-color);
    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: var(--widget-messages-area-color);
    display: flex;
    flex-direction: column;
    gap: 16px;
    position: relative;
    z-index: 1;
    min-height: 0;
    /* Предотвращаем перекрытие input контейнера */
    overflow-x: hidden;
}

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

.chat-messages::-webkit-scrollbar-track {
    background: var(--widget-background-color);
}

.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, var(--widget-user-message-color) 0%, var(--widget-secondary-color) 100%);
    color: white;
    border-bottom-right-radius: 6px;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.bot-message .message-content {
    background: var(--widget-bot-message-color);
    color: var(--widget-text-color);
    border-bottom-left-radius: 6px;
    border: 1px solid var(--widget-border-color);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

/* Стили для Markdown контента в сообщениях бота */
.bot-message .message-content {
    font-size: 15px;
    color: var(--widget-text-color);
    white-space: pre-line; /* Сохраняем переносы строк */
    line-height: 1.6;
}

.bot-message .message-content * {
    color: var(--widget-text-color) !important;
    font-size: 15px !important;
}

.bot-message .message-content h1,
.bot-message .message-content h2,
.bot-message .message-content h3,
.bot-message .message-content h4,
.bot-message .message-content h5,
.bot-message .message-content h6 {
    color: var(--widget-text-color) !important;
    font-size: 15px !important;
    margin: 0;
    font-weight: 600;
    line-height: 1.6;
}

.bot-message .message-content p {
    margin: 0; /* Убираем отступы между абзацами */
    line-height: 1.6;
    color: var(--widget-text-color) !important;
    font-size: 15px !important;
}

.bot-message .message-content p.compact-paragraph {
    margin-top: 0.3em; /* Компактный отступ между параграфами */
    margin-bottom: 0;
}

.bot-message .message-content p:first-child {
    margin-top: 0;
}

.bot-message .message-content p:last-child {
    margin-bottom: 0;
}

/* Компактные пустые строки - уменьшаем их высоту */
.bot-message .message-content .empty-line {
    display: block;
    height: 0.3em; /* Компактная высота пустой строки */
    line-height: 0.3em;
    margin: 0;
    padding: 0;
}

.bot-message .message-content ul,
.bot-message .message-content ol {
    margin: 0.5px 0;
    padding-left: 20px;
    line-height: 1.3;
    color: var(--widget-text-color) !important;
}

.bot-message .message-content ul {
    list-style-type: disc !important;
}

.bot-message .message-content ol {
    list-style-type: decimal !important;
}

.bot-message .message-content li {
    margin: 0;
    padding: 0;
    color: var(--widget-text-color) !important;
    font-size: 15px !important;
    line-height: 1.3;
    display: list-item !important;
}

/* Убираем отступы между параграфами и списками */
.bot-message .message-content p + ul,
.bot-message .message-content p + ol {
    margin-top: 0.5px;
}

/* КРИТИЧЕСКИ ВАЖНО: убираем большой отступ после списков */
.bot-message .message-content ul + p,
.bot-message .message-content ol + p,
.bot-message .message-content ul + p.compact-paragraph,
.bot-message .message-content ol + p.compact-paragraph {
    margin-top: 0.5px !important;
    margin-bottom: 0 !important;
}

/* Специальный класс для параграфов после списков */
.bot-message .message-content p.after-list {
    margin-top: 0.5px !important;
    margin-bottom: 0 !important;
}

/* Параграфы внутри элементов списка */
.bot-message .message-content li p {
    margin: 0;
    padding: 0;
    line-height: 1.4;
}

.bot-message .message-content li p:first-child {
    margin-top: 0;
}

.bot-message .message-content li p:last-child {
    margin-bottom: 0;
}

.bot-message .message-content strong {
    font-weight: 600;
    color: var(--widget-text-color) !important;
    font-size: 15px !important;
}

.bot-message .message-content em {
    font-style: italic;
    color: var(--widget-text-color) !important;
}

.bot-message .message-content code {
    background: rgba(0, 0, 0, 0.2);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 15px !important;
    color: var(--widget-text-color) !important;
}

.bot-message .message-content pre {
    background: rgba(0, 0, 0, 0.2);
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 10px 0;
}

.bot-message .message-content pre code {
    background: none;
    padding: 0;
    font-size: 15px !important;
}

.bot-message .message-content hr {
    border: none;
    border-top: 1px solid var(--widget-border-color);
    margin: 12px 0;
}

.chat-input-container {
    display: flex;
    padding: 20px;
    background: var(--widget-background-color);
    border-top: 1px solid var(--widget-border-color);
    gap: 12px;
    align-items: center;
    position: relative;
    z-index: 100;
    flex-shrink: 0;
    /* Убеждаемся, что контейнер всегда виден и не перекрывается */
    isolation: isolate;
}

#chat-input {
    flex: 1;
    padding: 14px 18px;
    border: 2px solid var(--widget-border-color);
    border-radius: 24px;
    font-size: 15px;
    background: var(--widget-input-background-color);
    color: var(--widget-text-color);
    transition: all 0.3s ease;
    font-family: inherit;
}

#chat-input::placeholder {
    color: var(--widget-text-color);
    opacity: 0.6;
}

#chat-input:focus {
    outline: none;
    border-color: var(--widget-button-color);
    background: var(--widget-input-background-color);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

#send-button {
    width: 48px;
    height: 48px;
    background: linear-gradient(135deg, var(--widget-button-color) 0%, var(--widget-secondary-color) 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;
    /* Оптимизация для touch-устройств */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
    position: relative;
    z-index: 200;
    /* Убеждаемся, что кнопка всегда кликабельна */
    pointer-events: auto;
}

#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);
}

/* SVG внутри кнопки отправки не должен перехватывать события */
#send-button svg {
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
}

.typing-indicator {
    display: flex;
    gap: 6px;
    padding: 14px 18px;
    background: var(--widget-bot-message-color);
    border-radius: 20px;
    border-bottom-left-radius: 6px;
    width: fit-content;
    border: 1px solid var(--widget-border-color);
}

.typing-indicator span {
    width: 10px;
    height: 10px;
    background: var(--widget-button-color);
    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);
    }
    
    .ai-chat-widget-container {
        bottom: 8px;
        right: 8px;
    }
    
    .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 !important;
        pointer-events: none !important;
    }
    
    .ai-chat-widget.chat-open .ai-chat-widget-container {
        display: none !important;
        pointer-events: none !important;
        z-index: -1 !important;
    }
}

/* Мобильная адаптация */
@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 !important;
        pointer-events: none !important;
    }
    
    .ai-chat-widget.chat-open .ai-chat-widget-container {
        display: none !important;
        pointer-events: none !important;
        z-index: -1 !important;
    }
    
    .ai-chat-widget-container {
        bottom: 8px;
        right: 8px;
    }
    
    .chat-button {
        width: 64px;
        height: 64px;
    }
    
    .ai-chat-widget {
        --widget-button-size: 64px;
    }
    
    .chat-expanded-message {
        padding: 0 28px;
        font-size: 16px;
        max-width: calc(100vw - 100px);
    }
    
    .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;
        z-index: 1;
        overflow-x: hidden;
    }
    
    .chat-input-container {
        padding: 14px;
        flex-shrink: 0;
        z-index: 100;
        position: relative;
        isolation: isolate;
    }
    
    #chat-input {
        font-size: 14px;
        padding: 12px 16px;
    }
    
    #send-button {
        width: 42px;
        height: 42px;
        z-index: 200;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
}

@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 !important;
        pointer-events: none !important;
    }
    
    .ai-chat-widget.chat-open .ai-chat-widget-container {
        display: none !important;
        pointer-events: none !important;
        z-index: -1 !important;
    }
    
    .ai-chat-widget-container {
        bottom: 8px;
        right: 8px;
    }
    
    .ai-chat-widget {
        --widget-button-size: 56px;
    }
    
    .chat-expanded-message {
        padding: 0 24px;
        font-size: 15px;
        max-width: calc(100vw - 90px);
    }
    
    .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;
        z-index: 1;
        overflow-x: hidden;
    }
    
    .chat-input-container {
        padding: 12px;
        gap: 8px;
        flex-shrink: 0;
        z-index: 100;
        position: relative;
        isolation: isolate;
    }
    
    #chat-input {
        font-size: 14px;
        padding: 10px 14px;
    }
    
    #send-button {
        width: 38px;
        height: 38px;
        flex-shrink: 0;
        z-index: 200;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    
    .close-button {
        width: 30px;
        height: 30px;
        font-size: 18px;
    }
}

/* Очень маленькие экраны */
@media (max-width: 360px) {
    .ai-chat-widget-container {
        bottom: 12px;
        right: 12px;
    }
    
    .chat-button {
        width: 48px;
        height: 48px;
    }
    
    .ai-chat-widget {
        --widget-button-size: 48px;
    }
    
    .chat-expanded-message {
        padding: 0 20px;
        font-size: 14px;
        max-width: calc(100vw - 90px);
    }
    
    .ai-chat-widget {
        --widget-button-size: 48px;
    }
    
    .chat-header {
        padding: 12px 14px;
    }
    
    .chat-header h3 {
        font-size: 14px;
    }
    
    .message-content {
        font-size: 12px;
        padding: 9px 12px;
    }
    
    .chat-messages {
        padding: 12px;
        z-index: 1;
        overflow-x: hidden;
    }
    
    .chat-input-container {
        padding: 10px;
        z-index: 100;
        position: relative;
        isolation: isolate;
    }
    
    #chat-input {
        font-size: 13px;
        padding: 9px 12px;
    }
    
    #send-button {
        width: 36px;
        height: 36px;
        z-index: 200;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
}
