/* Chat Widget Styles for VidFlow */
:root {
    --chat-bg: rgba(255, 255, 255, 0.06);
    --chat-border: rgba(255, 255, 255, 0.12);
    --chat-text: #f0f4ff;
    --chat-muted: #9aa4b2;
    --chat-accent: #00f5d4;
    --chat-accent2: #00bbf9;
    --chat-user-bg: linear-gradient(135deg, var(--chat-accent), var(--chat-accent2));
    --chat-user-text: #0a1020;
    --chat-support-bg: rgba(255, 255, 255, 0.08);
    --chat-support-text: var(--chat-text);
    --chat-error-bg: rgba(248, 113, 113, 0.15);
    --chat-error-text: #f87171;
    --chat-success-bg: rgba(34, 197, 94, 0.15);
    --chat-success-text: #22c55e;
}

/* FAB Button */
.chat-fab {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 1000;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent2));
    border: none;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(0, 245, 212, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: var(--chat-user-text);
    transition: transform .2s ease, box-shadow .2s ease;
}

.chat-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 32px rgba(0, 245, 212, 0.5);
}

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

.chat-fab-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 11px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(239, 68, 68, 0.4);
    animation: badge-pulse 2s infinite;
}

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

/* Widget Panel */
.chat-widget {
    position: fixed;
    right: 20px;
    bottom: 90px;
    z-index: 1000;
    width: min(380px, calc(100vw - 40px));
    height: min(600px, calc(100vh - 110px));
    height: min(600px, calc(100dvh - 110px)); /* mobile-safe */
    max-height: calc(100vh - 110px);
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: 16px;
    box-shadow: 0 24px 48px rgba(0, 0, 0, 0.5);
    display: none;
    flex-direction: column;
    overflow: hidden;
    backdrop-filter: blur(12px) saturate(1.3);
}

.chat-widget.open {
    display: flex;
    animation: slideUp .25s ease;
}

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

/* Header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid var(--chat-border);
}

.chat-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 700;
    color: var(--chat-text);
}

.chat-header-title {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.chat-header-sub {
    font-size: 11px;
    color: var(--chat-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.chat-header-sub::before {
    content: '';
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--chat-success-text);
    box-shadow: 0 0 6px var(--chat-success-text);
}

/* Подсказка в форме контакта */
.chat-form-hint {
    margin: 0 0 12px;
    font-size: 12px;
    line-height: 1.4;
    color: var(--chat-muted);
}

.chat-header-buttons {
    display: flex;
    gap: 4px;
    align-items: center;
}

.chat-icon-btn {
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid transparent;
    border-radius: 9px;
    min-width: 30px;
    min-height: 30px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    cursor: pointer;
    opacity: 0.65;
    transition: opacity .2s ease, background .2s ease, border-color .2s ease;
}

.chat-icon-btn:hover { opacity: 1; background: rgba(255, 255, 255, 0.13); }
.chat-icon-btn.active {
    opacity: 1;
    background: rgba(0, 245, 212, 0.14);
    border-color: rgba(0, 245, 212, 0.35);
}

/* Тост о новом ответе поддержки (виден и при закрытом окне чата) */
.chat-toast {
    position: fixed;
    right: 20px;
    bottom: 88px;
    z-index: 1100;
    width: min(340px, calc(100vw - 40px));
    padding: 12px 14px;
    border-radius: 12px;
    border: 1px solid rgba(0, 245, 212, 0.35);
    background: rgba(13, 20, 34, 0.96);
    color: var(--chat-text);
    font-size: 13px;
    line-height: 1.4;
    box-shadow: 0 14px 32px rgba(0, 0, 0, 0.45);
    cursor: pointer;
    opacity: 0;
    transform: translateY(12px);
    pointer-events: none;
    transition: opacity .25s ease, transform .25s ease;
}

.chat-toast.show { opacity: 1; transform: translateY(0); pointer-events: auto; }

.chat-close {
    background: none;
    border: none;
    color: var(--chat-text);
    font-size: 20px;
    cursor: pointer;
    opacity: 0.7;
    padding: 4px;
    transition: opacity .2s ease;
}

.chat-close:hover {
    opacity: 1;
}

.chat-faq-btn {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    opacity: 0.8;
    transition: opacity .2s ease;
}

.chat-faq-btn:hover {
    opacity: 1;
}

/* FAQ Panel */
.chat-faq-panel {
    padding: 12px 16px;
    border-bottom: 1px solid var(--chat-border);
    background: rgba(255, 255, 255, 0.03);
    max-height: 0;
    overflow: hidden;
    transition: max-height .3s ease, padding .3s ease;
}

.chat-faq-panel.open {
    max-height: 200px;
    padding: 12px 16px;
}

.chat-faq-panel h4 {
    margin: 0 0 8px;
    font-size: 13px;
    font-weight: 700;
    color: var(--chat-text);
}

.chat-faq-panel ul {
    list-style: none;
    margin: 0;
    padding: 0;
    max-height: 150px;
    overflow-y: auto;
}

.chat-faq-panel li {
    padding: 8px 10px;
    margin-bottom: 6px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.06);
    cursor: pointer;
    font-size: 13px;
    color: var(--chat-text);
    transition: background .15s ease;
}

.chat-faq-panel li:hover {
    background: rgba(255, 255, 255, 0.12);
}

/* Messages Area */
.chat-messages {
    flex: 1 1 auto;
    min-height: 0; /* важно: иначе flex-элемент не скроллится */
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.28) transparent;
}

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

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.22);
    border-radius: 6px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.38);
}

.chat-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    animation: fadeIn .2s ease;
}

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

.chat-msg.user {
    align-self: flex-end;
    background: var(--chat-user-bg);
    color: var(--chat-user-text);
    border-bottom-right-radius: 4px;
}

.chat-msg.support {
    align-self: flex-start;
    background: var(--chat-support-bg);
    color: var(--chat-support-text);
    border-bottom-left-radius: 4px;
}

.chat-msg.system {
    align-self: center;
    background: rgba(255, 255, 255, 0.05);
    color: var(--chat-muted);
    font-size: 12px;
    font-style: italic;
}

.chat-msg .meta {
    font-size: 11px;
    opacity: 0.7;
    margin-top: 4px;
    text-align: right;
}

.chat-msg.support .meta {
    text-align: left;
}

.chat-msg .attachment-preview {
    margin-top: 8px;
    border-radius: 8px;
    overflow: hidden;
}

.chat-msg .attachment-preview img {
    max-width: 100%;
    max-height: 200px;
    border-radius: 8px;
    display: block;
}

.chat-msg .attachment-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 6px;
    font-size: 12px;
    color: var(--chat-text);
    text-decoration: none;
}

.chat-msg .attachment-link:hover {
    background: rgba(255, 255, 255, 0.15);
}

/* Вложения внутри своего сообщения (привязаны к message_id) */
.chat-msg .msg-attachments {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.chat-msg .msg-attachment {
    max-width: 100%;
}

.chat-msg .msg-attachment .attachment-link {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Кнопка «Показать ранее» в начале переписки */
.chat-load-earlier {
    flex: 0 0 auto;
    align-self: center;
    margin: 0 auto 4px;
    padding: 6px 14px;
    font-size: 12px;
    font-weight: 600;
    color: var(--chat-text);
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid var(--chat-border);
    border-radius: 20px;
    cursor: pointer;
    transition: background .15s ease, opacity .15s ease;
}

.chat-load-earlier:hover {
    background: rgba(255, 255, 255, 0.16);
}

.chat-load-earlier:disabled {
    opacity: 0.6;
    cursor: default;
}

.chat-msg.failed {
    opacity: 0.6;
    border: 1px dashed var(--chat-error-text);
}

.chat-msg.pending {
    opacity: 0.7;
}

.chat-msg.pending .meta {
    font-style: italic;
}

.chat-msg .retry-btn {
    margin-top: 6px;
    padding: 4px 8px;
    font-size: 11px;
    background: var(--chat-error-bg);
    color: var(--chat-error-text);
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.chat-msg .retry-btn:hover {
    background: var(--chat-error-text);
    color: white;
}

/* Empty State */
.chat-empty {
    text-align: center;
    padding: 24px;
    color: var(--chat-muted);
    font-size: 14px;
}

/* Status Bar */
.chat-status {
    font-size: 12px;
    color: var(--chat-muted);
    padding: 0 16px 8px;
    text-align: center;
}

/* «Поддержка печатает…» */
.chat-typing {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex: 0 0 auto;
    font-size: 12px;
    line-height: 1.4;
    color: var(--chat-muted);
    padding: 0 16px 8px;
    white-space: nowrap;
    overflow: hidden;
}

/* hidden должен выигрывать у display:flex */
.chat-typing[hidden] {
    display: none;
}

/* Показ/скрыт управляются атрибутом hidden и классом .show
   (класс держим для CSS-анимации появления) */
.chat-typing.show {
    animation: fadeIn .2s ease;
}

.chat-typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 3px;
}

.chat-typing-dots i {
    display: inline-block;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: currentColor;
    animation: typing-dot 1.2s infinite ease-in-out;
}

.chat-typing-dots i:nth-child(2) {
    animation-delay: .15s;
}

.chat-typing-dots i:nth-child(3) {
    animation-delay: .3s;
}

@keyframes typing-dot {
    0%, 60%, 100% { opacity: .35; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-2px); }
}

/* Error Display */
.chat-error {
    background: var(--chat-error-bg);
    color: var(--chat-error-text);
    padding: 10px 16px;
    border-radius: 8px;
    margin: 8px 16px;
    font-size: 13px;
    display: none;
}

.chat-error.show {
    display: block;
}

/* Contact Form */
.chat-contact-form {
    padding: 16px;
    border-top: 1px solid var(--chat-border);
    background: rgba(255, 255, 255, 0.03);
    display: none;
}

.chat-contact-form.show {
    display: block;
}

.chat-contact-form label {
    display: block;
    font-size: 13px;
    color: var(--chat-muted);
    margin-bottom: 6px;
}

.chat-contact-form input,
.chat-contact-form textarea {
    width: 100%;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 14px;
    color: var(--chat-text);
    background: #0d1626;
    border: 1px solid var(--chat-border);
    outline: none;
    margin-bottom: 10px;
    font-family: inherit;
    resize: vertical;
}

.chat-contact-form input:focus,
.chat-contact-form textarea:focus {
    border-color: var(--chat-accent);
}

.chat-contact-form button {
    width: 100%;
    margin-top: 4px;
}

/* Input Area */
.chat-input-area {
    padding: 12px 16px;
    border-top: 1px solid var(--chat-border);
    background: rgba(255, 255, 255, 0.03);
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.chat-input-area textarea {
    flex: 1;
    min-height: 44px;
    max-height: 120px;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 14px;
    color: var(--chat-text);
    background: #0d1626;
    border: 1px solid var(--chat-border);
    outline: none;
    resize: none;
    font-family: inherit;
    overflow-y: auto;
}

.chat-input-area textarea:focus {
    border-color: var(--chat-accent);
}

.chat-input-area .input-buttons {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-input-area .btn {
    padding: 0 16px;
    height: 44px;
    background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent2));
    color: var(--chat-user-text);
    border: none;
    border-radius: 10px;
    font-weight: 700;
    cursor: pointer;
    transition: opacity .2s ease;
}

.chat-input-area .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.chat-input-area .attach-btn {
    padding: 8px 12px;
    height: auto;
    background: rgba(255, 255, 255, 0.1);
    color: var(--chat-text);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 18px;
    transition: background .2s ease;
}

.chat-input-area .attach-btn:hover {
    background: rgba(255, 255, 255, 0.15);
}

.chat-input-area .attach-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#chat-attachment-input {
    display: none;
}

/* New message indicator */
.new-message-indicator {
    position: absolute;
    bottom: 96px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--chat-accent);
    color: var(--chat-user-text);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 700;
    cursor: pointer;
    display: none;
    z-index: 3;
    white-space: nowrap;
    box-shadow: 0 4px 12px rgba(0, 245, 212, 0.35);
}

.new-message-indicator.show {
    display: block;
    animation: bounceIn .3s ease;
}

@keyframes bounceIn {
    0% { transform: scale(0); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Responsive */
@media (max-width: 480px) {
    .chat-widget {
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: calc(100vh - 64px);
        height: calc(100dvh - 64px);
        max-height: calc(100dvh - 64px);
        border-radius: 18px 18px 0 0;
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    
    .chat-fab {
        right: 16px;
        bottom: calc(16px + env(safe-area-inset-bottom, 0));
    }

    /* iOS не зумит при фокусе, если шрифт >= 16px */
    .chat-input-area textarea,
    .chat-contact-form input,
    .chat-contact-form textarea {
        font-size: 16px;
    }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    .chat-widget.open,
    .chat-msg,
    .chat-typing.show,
    .chat-typing-dots i,
    .new-message-indicator {
        animation: none;
        transition: none;
    }
}
