/* AI Chat Widget Styles */

/* Цветовая схема (управляется из админки) */
:root {
    --ai-chat-color: #2563eb;
    --ai-chat-color-hover: #1d4ed8;
    --ai-chat-color-rgb: 37, 99, 235;
}

/* Сброс стилей */
.ai-chat *, .ai-chat *::before, .ai-chat *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Кнопка открытия чата */
#ai-chat-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-chat-color) 0%, var(--ai-chat-color-hover) 100%);
    border: none;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(var(--ai-chat-color-rgb), 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
}

#ai-chat-toggle.visible {
    opacity: 1;
    visibility: visible;
}

#ai-chat-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 32px rgba(var(--ai-chat-color-rgb), 0.5);
}

#ai-chat-toggle svg {
    width: 28px;
    height: 28px;
    fill: white;
}

/* Окно чата */
#ai-chat-container {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 380px;
    height: 500px;
    min-width: 280px;
    min-height: 350px;
    max-width: 90vw;
    max-height: 90vh;
    background: white;
    border-radius: 16px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.2);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 9998;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    visibility: hidden;
    transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

#ai-chat-container.resizing {
    transition: none !important;
}

#ai-chat-container.dragging {
    transition: none !important;
    opacity: 0.8;
}

/* Ресайзеры */
.ai-chat-resizer {
    position: absolute;
    z-index: 10001; /* Поверх всего */
}

.ai-chat-resizer-nw { width: 15px; height: 15px; left: -5px; top: -5px; cursor: nw-resize; }
.ai-chat-resizer-ne { width: 15px; height: 15px; right: -5px; top: -5px; cursor: ne-resize; }
.ai-chat-resizer-sw { width: 15px; height: 15px; left: -5px; bottom: -5px; cursor: sw-resize; }
.ai-chat-resizer-se { width: 20px; height: 20px; right: -5px; bottom: -5px; cursor: se-resize; }
.ai-chat-resizer-n { height: 10px; left: 10px; right: 10px; top: -5px; cursor: n-resize; }
.ai-chat-resizer-s { height: 10px; left: 10px; right: 10px; bottom: -5px; cursor: s-resize; }
.ai-chat-resizer-e { width: 10px; top: 10px; bottom: 10px; right: -5px; cursor: e-resize; }
.ai-chat-resizer-w { width: 10px; top: 10px; bottom: 10px; left: -5px; cursor: w-resize; }

#ai-chat-container.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    visibility: visible;
}

/* Широкое окно для длинных ответов и таблиц */
#ai-chat-container.wide {
    width: 600px;
    height: 600px;
}

/* Стили для таблиц в сообщениях */
.table-container {
    width: 100%;
    overflow-x: auto;
    margin: 10px 0;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
}

.ai-chat-message table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    background: white;
    table-layout: auto;
}

.ai-chat-message th, 
.ai-chat-message td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid #eee;
    vertical-align: top;
}

.ai-chat-message th {
    background: #f8fafc;
    font-weight: 700;
    color: #334155;
    border-bottom: 2px solid #e2e8f0;
}

.ai-chat-message tr:nth-child(even) {
    background-color: #fcfcfc;
}

.ai-chat-message tr:hover {
    background-color: #f1f5f9;
}

.ai-chat-message td strong {
    color: #1e293b;
}

.ai-chat-message tr:last-child td {
    border-bottom: none;
}

/* Заголовок чата */
.ai-chat-header {
    background: linear-gradient(135deg, var(--ai-chat-color) 0%, var(--ai-chat-color-hover) 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: move;
    user-select: none;
}

.ai-chat-header .avatar {
    width: 36px;
    height: 36px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.ai-chat-header .avatar svg {
    width: 22px;
    height: 22px;
    fill: var(--ai-chat-color);
}

.ai-chat-header .info {
    flex: 1;
}

.ai-chat-header .info h3 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
}

.ai-chat-header .info span {
    font-size: 12px;
    opacity: 0.85;
}

.ai-chat-header .close-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    opacity: 0.8;
    transition: opacity 0.2s;
    flex-shrink: 0;
}

.ai-chat-header .close-btn:hover {
    opacity: 1;
}

.ai-chat-header .close-btn svg {
    width: 20px;
    height: 20px;
    fill: white;
}

.ai-chat-header .new-chat-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    opacity: 0.8;
    transition: opacity 0.2s, transform 0.2s;
    flex-shrink: 0;
    margin-right: 8px;
}

.ai-chat-header .new-chat-btn:hover {
    opacity: 1;
    transform: scale(1.1);
}

.ai-chat-header .new-chat-btn svg {
    width: 20px;
    height: 20px;
    fill: white;
}

/* Область сообщений */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: #f9fafb;
}

.ai-chat-message {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    animation: aiChatMessageIn 0.3s ease;
    word-wrap: break-word;
}

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

.ai-chat-message.user {
    align-self: flex-end;
    background: #e0e7ff;
    color: #1f2937;
    border-bottom-right-radius: 4px;
}

.ai-chat-message.assistant {
    align-self: flex-start;
    background: white;
    color: #1f2937;
    border: 1px solid #e5e7eb;
    border-bottom-left-radius: 4px;
}

.ai-chat-message .reasoning {
    font-size: 11px;
    color: #9ca3af;
    font-style: italic;
    margin-bottom: 6px;
    padding-bottom: 6px;
    border-bottom: 1px solid #e5e7eb;
}

.ai-chat-message a {
    color: var(--ai-chat-color);
    text-decoration: underline;
}

.ai-chat-message a:hover {
    color: var(--ai-chat-color-hover);
}

/* CTA-кнопки в сообщениях */
.ai-chat-cta {
    margin-top: 8px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.ai-chat-btn {
    display: inline-block;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    text-decoration: none;
    text-align: center;
    transition: all 0.2s ease;
    cursor: pointer;
    border: none;
}

.ai-chat-btn-primary {
    background: linear-gradient(135deg, var(--ai-chat-color) 0%, var(--ai-chat-color-hover) 100%);
    color: white;
}

.ai-chat-btn-primary:hover {
    background: linear-gradient(135deg, var(--ai-chat-color-hover) 0%, var(--ai-chat-color-hover) 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(var(--ai-chat-color-rgb), 0.3);
    color: white;
}

.ai-chat-btn-outline {
    background: white;
    color: var(--ai-chat-color);
    border: 1px solid var(--ai-chat-color);
}

.ai-chat-btn-outline:hover {
    background: rgba(var(--ai-chat-color-rgb), 0.08);
    transform: translateY(-1px);
    color: var(--ai-chat-color);
}

.ai-chat-btn-phone {
    background: #059669;
    color: white;
}

.ai-chat-btn-phone:hover {
    background: #047857;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(5, 150, 105, 0.3);
    color: white;
}

.ai-chat-link {
    font-weight: 600;
}

/* Бейдж источника ответа */
.ai-chat-source {
    font-size: 10px;
    color: #9ca3af;
    margin-top: 4px;
    font-style: italic;
}

/* Стриминговый текст — постепенно появляющийся ответ AI */
.ai-chat-streaming-content::after {
    content: '▋';
    color: var(--ai-chat-color, #2563eb);
    animation: aiChatCursor 1s steps(2) infinite;
    margin-left: 2px;
}

@keyframes aiChatCursor {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* Индикатор загрузки */
.ai-chat-typing {
    display: none;
    align-self: flex-start;
    background: white;
    border: 1px solid #e5e7eb;
    padding: 12px 16px;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    gap: 4px;
}

.ai-chat-typing.visible {
    display: flex;
}

.ai-chat-typing span {
    width: 8px;
    height: 8px;
    background: #9ca3af;
    border-radius: 50%;
    animation: aiChatBounce 1.4s infinite ease-in-out;
}

.ai-chat-typing span:nth-child(1) { animation-delay: 0s; }
.ai-chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-chat-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiChatBounce {
    0%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-6px); }
}

/* Область ввода */
.ai-chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid #e5e7eb;
    background: white;
    display: flex;
    gap: 10px;
}

.ai-chat-input-area input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid #d1d5db;
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
}

.ai-chat-input-area input:focus {
    border-color: var(--ai-chat-color);
    box-shadow: 0 0 0 3px rgba(var(--ai-chat-color-rgb), 0.1);
}

.ai-chat-input-area button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--ai-chat-color);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.2s;
    flex-shrink: 0;
}

.ai-chat-input-area button:hover {
    background: var(--ai-chat-color-hover);
}

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

.ai-chat-input-area button:disabled {
    background: #9ca3af;
    cursor: not-allowed;
}

.ai-chat-input-area button svg {
    width: 20px;
    height: 20px;
    fill: white;
}

/* Адаптивность */
@media (max-width: 480px) {
    #ai-chat-container {
        width: calc(100vw - 32px);
        right: 16px;
        bottom: 90px;
        height: calc(100vh - 140px);
    }

    #ai-chat-toggle {
        right: 16px;
        bottom: 16px;
    }
}

/* Полноэкранный режим для мобильных */
@media (max-width: 480px) {
    #ai-chat-container.fullscreen {
        position: fixed;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        border-radius: 0;
        z-index: 99999;
    }

    #ai-chat-container.fullscreen .ai-chat-resizer {
        display: none;
    }
}