/* ============================================================
   Enterprise AI Live Chat Widget
   Glassmorphism design, responsive, dark/light mode
   ============================================================ */

:root {
    --chat-primary: #c0392b;
    --chat-primary-hover: #a93226;
    --chat-bg: #ffffff;
    --chat-surface: #f8fafc;
    --chat-border: #e2e8f0;
    --chat-text: #1e293b;
    --chat-text-secondary: #64748b;
    --chat-radius: 16px;
    --chat-shadow: 0 20px 60px rgba(0,0,0,0.15), 0 4px 16px rgba(0,0,0,0.08);
    --chat-bubble-shadow: 0 8px 32px rgba(0,0,0,0.12);
    --chat-width: 380px;
    --chat-height: 560px;
    --chat-z: 999999;
}

/* ---- Chat Bubble Button ---- */
.chat-widget-bubble {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--chat-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    z-index: var(--chat-z);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--chat-bubble-shadow);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    animation: chatBubbleEntry 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    animation-delay: 1s;
}
.chat-widget-bubble:hover {
    transform: scale(1.08);
    box-shadow: 0 12px 40px rgba(192, 57, 43, 0.4);
}
.chat-widget-bubble.left {
    right: auto;
    left: 24px;
}
.chat-widget-bubble svg {
    width: 28px;
    height: 28px;
    transition: transform 0.3s ease;
}
.chat-widget-bubble.open svg.chat-icon { display: none; }
.chat-widget-bubble.open svg.close-icon { display: block; }
.chat-widget-bubble:not(.open) svg.close-icon { display: none; }

.chat-widget-bubble .chat-badge {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 20px;
    height: 20px;
    background: #ef4444;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    animation: badgePulse 2s infinite;
}
.chat-badge:empty { display: none; }

@keyframes chatBubbleEntry {
    from { opacity: 0; transform: scale(0.5) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
@keyframes badgePulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

/* ---- Chat Window ---- */
.chat-widget-window {
    position: fixed;
    bottom: 96px;
    right: 24px;
    width: var(--chat-width);
    height: var(--chat-height);
    max-height: calc(100vh - 120px);
    background: var(--chat-bg);
    border-radius: var(--chat-radius);
    box-shadow: var(--chat-shadow);
    z-index: var(--chat-z);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid var(--chat-border);
}
.chat-widget-window.active {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}
.chat-widget-window.left {
    right: auto;
    left: 24px;
}

/* ---- Header ---- */
.chat-widget-header {
    background: var(--chat-primary);
    color: #fff;
    padding: 18px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}
.chat-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.chat-header-avatar svg { width: 22px; height: 22px; }
.chat-header-info {
    flex: 1;
    min-width: 0;
}
.chat-header-title {
    font-size: 15px;
    font-weight: 700;
    margin: 0;
    line-height: 1.2;
}
.chat-header-status {
    font-size: 12px;
    opacity: 0.85;
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 2px;
}
.chat-status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    display: inline-block;
}
.chat-status-dot.offline { background: #fbbf24; }

.chat-header-actions {
    display: flex;
    gap: 4px;
}
.chat-header-btn {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: none;
    background: rgba(255,255,255,0.15);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.chat-header-btn:hover { background: rgba(255,255,255,0.25); }
.chat-header-btn svg { width: 16px; height: 16px; }

/* ---- Messages Area ---- */
.chat-widget-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background: var(--chat-surface);
    scroll-behavior: smooth;
}
.chat-widget-messages::-webkit-scrollbar { width: 4px; }
.chat-widget-messages::-webkit-scrollbar-track { background: transparent; }
.chat-widget-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

/* Message Bubbles */
.chat-msg {
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 13.5px;
    line-height: 1.55;
    word-wrap: break-word;
    animation: msgFadeIn 0.3s ease both;
    position: relative;
}
@keyframes msgFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-msg.visitor {
    align-self: flex-end;
    background: var(--chat-primary);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.chat-msg.staff,
.chat-msg.ai {
    align-self: flex-start;
    background: #fff;
    color: var(--chat-text);
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 4px;
}
.chat-msg.system {
    align-self: center;
    background: transparent;
    color: var(--chat-text-secondary);
    font-size: 12px;
    text-align: center;
    padding: 4px 12px;
    max-width: 90%;
}

.chat-msg-sender {
    font-size: 11px;
    font-weight: 600;
    margin-bottom: 3px;
    opacity: 0.7;
}
.chat-msg-time {
    font-size: 10px;
    opacity: 0.6;
    margin-top: 4px;
    text-align: right;
}

/* AI badge */
.chat-msg.ai .chat-ai-badge {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: 10px;
    color: var(--chat-primary);
    font-weight: 600;
    margin-bottom: 4px;
}
.chat-ai-badge svg { width: 12px; height: 12px; }

/* Typing indicator */
.chat-typing {
    align-self: flex-start;
    padding: 12px 18px;
    background: #fff;
    border: 1px solid var(--chat-border);
    border-radius: 12px;
    display: none;
    gap: 4px;
    align-items: center;
}
.chat-typing.active { display: flex; }
.chat-typing span {
    width: 6px;
    height: 6px;
    background: #94a3b8;
    border-radius: 50%;
    animation: typingDot 1.4s infinite;
}
.chat-typing span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingDot {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* ---- Pre-chat Form ---- */
.chat-prechat {
    flex: 1;
    padding: 24px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: var(--chat-surface);
}
.chat-prechat h3 {
    margin: 0;
    font-size: 16px;
    color: var(--chat-text);
}
.chat-prechat p {
    margin: 0;
    font-size: 13px;
    color: var(--chat-text-secondary);
    line-height: 1.5;
}
.chat-prechat input {
    padding: 10px 14px;
    border: 1px solid var(--chat-border);
    border-radius: 8px;
    font-size: 13px;
    background: #fff;
    color: var(--chat-text);
    outline: none;
    transition: border-color 0.2s;
}
.chat-prechat input:focus {
    border-color: var(--chat-primary);
}
.chat-prechat-btn {
    padding: 12px;
    background: var(--chat-primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}
.chat-prechat-btn:hover { background: var(--chat-primary-hover); }

/* ---- Input Area ---- */
.chat-widget-input {
    padding: 12px 16px;
    background: #fff;
    border-top: 1px solid var(--chat-border);
    display: flex;
    align-items: flex-end;
    gap: 8px;
    flex-shrink: 0;
}
.chat-input-field {
    flex: 1;
    border: 1px solid var(--chat-border);
    border-radius: 10px;
    padding: 10px 14px;
    font-size: 13.5px;
    color: var(--chat-text);
    background: var(--chat-surface);
    resize: none;
    max-height: 100px;
    min-height: 40px;
    line-height: 1.4;
    outline: none;
    font-family: inherit;
    transition: border-color 0.2s;
}
.chat-input-field:focus { border-color: var(--chat-primary); }
.chat-input-field::placeholder { color: #94a3b8; }

.chat-send-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--chat-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
}
.chat-send-btn:hover { background: var(--chat-primary-hover); transform: scale(1.05); }
.chat-send-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.chat-send-btn svg { width: 18px; height: 18px; }

.chat-input-counter {
    font-size: 10px;
    color: #94a3b8;
    text-align: right;
    margin-top: 2px;
}
.chat-input-counter.warning { color: #ef4444; }

/* ---- Rating Panel ---- */
.chat-rating-panel {
    padding: 20px;
    text-align: center;
    background: var(--chat-surface);
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
}
.chat-rating-panel h3 { margin: 0; font-size: 16px; color: var(--chat-text); }
.chat-rating-panel p { margin: 0; font-size: 13px; color: var(--chat-text-secondary); }
.chat-stars {
    display: flex;
    gap: 8px;
    margin: 8px 0;
}
.chat-star {
    width: 36px;
    height: 36px;
    cursor: pointer;
    color: #cbd5e1;
    transition: all 0.15s;
}
.chat-star.active, .chat-star:hover { color: #f59e0b; transform: scale(1.15); }
.chat-rating-submit {
    padding: 10px 24px;
    background: var(--chat-primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 8px;
}
.chat-rating-thanks {
    font-size: 14px;
    color: var(--chat-primary);
    font-weight: 600;
}

/* ---- Powered By ---- */
.chat-powered {
    padding: 6px;
    text-align: center;
    font-size: 10px;
    color: #94a3b8;
    background: #fff;
    border-top: 1px solid var(--chat-border);
    flex-shrink: 0;
}

/* ---- Sound toggle ---- */
.chat-sound-toggle {
    cursor: pointer;
    opacity: 0.7;
}
.chat-sound-toggle:hover { opacity: 1; }

/* ---- Mobile Responsive ---- */
@media (max-width: 480px) {
    .chat-widget-window {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }
    .chat-widget-window.left {
        left: 0;
        right: 0;
    }
    .chat-widget-bubble {
        bottom: 16px;
        right: 16px;
        width: 54px;
        height: 54px;
    }
    .chat-widget-bubble.left {
        left: 16px;
    }
}

/* ---- Print ---- */
@media print {
    .chat-widget-bubble,
    .chat-widget-window { display: none !important; }
}
