/* Sales Chat Widget - <30KB total with JS */
#sales-chat-widget {
    --chat-color: #6366f1;
    position: fixed;
    bottom: 20px;
    z-index: 99999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.5;
}
#sales-chat-widget[data-position="bottom-right"] { right: 20px; }
#sales-chat-widget[data-position="bottom-left"] { left: 20px; }

/* Bubble */
#chat-bubble {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: var(--chat-color);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    transition: transform 0.2s, box-shadow 0.2s;
}
#chat-bubble:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

/* Nudge tooltip */
#chat-nudge {
    position: absolute;
    bottom: 72px;
    background: white;
    color: #1e293b;
    padding: 0.6rem 1rem;
    border-radius: 1rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.15);
    font-size: 0.9rem;
    white-space: nowrap;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    animation: nudgeFadeIn 0.4s ease;
    cursor: pointer;
}
#sales-chat-widget[data-position="bottom-right"] #chat-nudge { right: 0; }
#sales-chat-widget[data-position="bottom-left"] #chat-nudge { left: 0; }

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

/* Chat Panel */
#chat-panel {
    position: absolute;
    bottom: 72px;
    width: 380px;
    max-height: 550px;
    background: white;
    border-radius: 1rem;
    box-shadow: 0 8px 30px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
#sales-chat-widget[data-position="bottom-right"] #chat-panel { right: 0; }
#sales-chat-widget[data-position="bottom-left"] #chat-panel { left: 0; }

/* Header */
#chat-header {
    background: var(--chat-color);
    color: white;
    padding: 0.9rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}
#chat-header-info { display: flex; flex-direction: column; }
#chat-header-name { font-weight: 600; font-size: 1rem; }
#chat-header-status { font-size: 0.75rem; opacity: 0.8; }
#chat-close {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0 0.25rem;
    line-height: 1;
    opacity: 0.8;
}
#chat-close:hover { opacity: 1; }

/* Messages */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    min-height: 200px;
    max-height: 380px;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.chat-msg {
    max-width: 82%;
    padding: 0.6rem 0.9rem;
    border-radius: 1rem;
    word-wrap: break-word;
    white-space: pre-wrap;
    font-size: 0.93rem;
    line-height: 1.45;
    animation: msgFadeIn 0.25s ease;
}
@keyframes msgFadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.chat-msg-customer {
    align-self: flex-end;
    background: var(--chat-color);
    color: white;
    border-bottom-right-radius: 0.3rem;
}
.chat-msg-assistant {
    align-self: flex-start;
    background: #f1f5f9;
    color: #1e293b;
    border-bottom-left-radius: 0.3rem;
}
.chat-msg-system {
    align-self: center;
    background: #ecfdf5;
    color: #065f46;
    border: 1px solid #a7f3d0;
    border-radius: 0.75rem;
    text-align: center;
    max-width: 90%;
    font-size: 0.88rem;
}

/* Typing indicator */
#chat-typing {
    padding: 0.5rem 1rem;
    display: flex;
    gap: 4px;
    align-items: center;
}
.typing-dot {
    width: 8px;
    height: 8px;
    background: #94a3b8;
    border-radius: 50%;
    animation: typingBounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

/* Input */
#chat-form {
    display: flex;
    border-top: 1px solid #e5e7eb;
    flex-shrink: 0;
}
#chat-input {
    flex: 1;
    border: none;
    padding: 0.75rem 1rem;
    font-size: 0.93rem;
    outline: none;
    font-family: inherit;
}
#chat-input::placeholder { color: #94a3b8; }
#chat-send {
    background: none;
    border: none;
    padding: 0.75rem;
    cursor: pointer;
    color: var(--chat-color);
    display: flex;
    align-items: center;
}
#chat-send:hover { opacity: 0.8; }
#chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* Mobile */
@media (max-width: 480px) {
    #sales-chat-widget {
        bottom: 12px;
        right: 12px !important;
        left: auto !important;
    }
    #sales-chat-widget[data-position="bottom-left"] {
        left: 12px !important;
        right: auto !important;
    }
    #chat-bubble {
        width: 52px;
        height: 52px;
    }
    #chat-panel,
    #sales-chat-widget[data-position="bottom-right"] #chat-panel,
    #sales-chat-widget[data-position="bottom-left"] #chat-panel {
        position: fixed !important;
        top: env(safe-area-inset-top, 0) !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        max-height: none !important;
        height: calc(100% - env(safe-area-inset-top, 0)) !important;
        border-radius: 0 !important;
        z-index: 100000 !important;
        margin: 0 !important;
    }
    /* Lock body scroll when chat is open */
    body.chat-open {
        overflow: hidden !important;
        position: fixed !important;
        width: 100% !important;
    }
    #chat-header {
        padding-top: 0.9rem !important;
    }
    #chat-messages {
        max-height: none;
        flex: 1;
        -webkit-overflow-scrolling: touch;
    }
    #chat-form {
        position: sticky;
        bottom: 0;
        background: white;
        border-top: 1px solid #e5e7eb;
    }
    #chat-header {
        padding: 1rem;
        padding-top: max(1rem, env(safe-area-inset-top, 1rem));
    }
    #chat-close {
        font-size: 2rem;
        padding: 0 0.5rem;
    }
    #chat-input {
        font-size: 16px; /* prevents iOS zoom */
        padding: 0.85rem 1rem;
    }
    #chat-form {
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
    #chat-nudge {
        bottom: 62px;
        right: 0;
        max-width: 220px;
        font-size: 0.82rem;
    }
}
