/* xAPI AI Chat Widget */
:root {
    --xapi-primary: #2563eb;
    --xapi-primary-dark: #1d4ed8;
    --xapi-bg: #f8fafc;
    --xapi-surface: #ffffff;
    --xapi-border: #e2e8f0;
    --xapi-text: #1e293b;
    --xapi-text-muted: #64748b;
    --xapi-upsale: #f59e0b;
    --xapi-upsale-bg: #fffbeb;
    --xapi-error: #ef4444;
    --xapi-success: #22c55e;
    --radius: 12px;
    --shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
}

#xapi-chat-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.5;
}

/* Toggle button */
.xapi-chat-toggle {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--xapi-primary), var(--xapi-primary-dark));
    color: white;
    border: none;
    cursor: pointer;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, box-shadow 0.2s;
    position: relative;
}

.xapi-chat-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 25px 30px -5px rgb(0 0 0 / 0.15);
}

.xapi-chat-toggle svg {
    width: 28px;
    height: 28px;
}

.xapi-chat-toggle .xapi-chat-close {
    display: none;
}

.xapi-chat-widget--open .xapi-chat-toggle .xapi-chat-icon {
    display: none;
}

.xapi-chat-widget--open .xapi-chat-toggle .xapi-chat-close {
    display: block;
}

/* Notification dot */
.xapi-chat-notify {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    background: var(--xapi-error);
    border-radius: 50%;
    border: 2px solid white;
    display: none;
}

/* Chat window */
.xapi-chat-window {
    position: absolute;
    bottom: 72px;
    right: 0;
    width: 380px;
    max-width: calc(100vw - 40px);
    height: 520px;
    max-height: calc(100vh - 100px);
    background: var(--xapi-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    display: none;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--xapi-border);
}

.xapi-chat-widget--open .xapi-chat-window {
    display: flex;
    animation: xapi-chat-slide-up 0.25s ease-out;
}

@keyframes xapi-chat-slide-up {
    from { opacity: 0; transform: translateY(20px) scale(0.95); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* Header */
.xapi-chat-header {
    background: linear-gradient(135deg, var(--xapi-primary), var(--xapi-primary-dark));
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.xapi-chat-header-avatar {
    width: 36px;
    height: 36px;
    background: rgba(255,255,255,0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}

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

.xapi-chat-header-title {
    font-weight: 600;
    font-size: 15px;
    margin: 0;
}

.xapi-chat-header-subtitle {
    font-size: 12px;
    opacity: 0.85;
    margin: 2px 0 0;
}

.xapi-chat-header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.9;
}

.xapi-chat-header-status::before {
    content: '';
    width: 8px;
    height: 8px;
    background: #4ade80;
    border-radius: 50%;
    animation: xapi-pulse 2s infinite;
}

@keyframes xapi-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Messages area */
.xapi-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--xapi-bg);
}

.xapi-chat-message {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    word-wrap: break-word;
    animation: xapi-msg-fade-in 0.2s ease-out;
}

@keyframes xapi-msg-fade-in {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

.xapi-chat-message--user {
    align-self: flex-end;
    background: var(--xapi-primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.xapi-chat-message--bot {
    align-self: flex-start;
    background: var(--xapi-surface);
    color: var(--xapi-text);
    border: 1px solid var(--xapi-border);
    border-bottom-left-radius: 4px;
}

.xapi-chat-message p {
    margin: 0 0 8px;
}

.xapi-chat-message p:last-child {
    margin-bottom: 0;
}

.xapi-chat-message code {
    background: rgba(0,0,0,0.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: "SF Mono", Monaco, monospace;
    font-size: 12px;
}

.xapi-chat-message--user code {
    background: rgba(255,255,255,0.2);
}

.xapi-chat-message pre {
    background: #1e293b;
    color: #e2e8f0;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 12px;
    line-height: 1.4;
}

.xapi-chat-message pre code {
    background: none;
    padding: 0;
    color: inherit;
}

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

.xapi-chat-message--user a {
    color: #bfdbfe;
}

.xapi-chat-message ul, .xapi-chat-message ol {
    margin: 8px 0;
    padding-left: 20px;
}

.xapi-chat-message li {
    margin-bottom: 4px;
}

/* Upsale banner */
.xapi-chat-upsale {
    background: var(--xapi-upsale-bg);
    border: 1px solid #fcd34d;
    border-radius: 8px;
    padding: 12px;
    margin-top: 10px;
    display: flex;
    align-items: flex-start;
    gap: 10px;
}

.xapi-chat-upsale-icon {
    font-size: 18px;
    flex-shrink: 0;
}

.xapi-chat-upsale-text {
    font-size: 13px;
    color: #92400e;
    line-height: 1.4;
}

.xapi-chat-upsale-text strong {
    color: #78350f;
}

.xapi-chat-upsale-btn {
    display: inline-block;
    margin-top: 8px;
    padding: 6px 14px;
    background: var(--xapi-upsale);
    color: white;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 500;
    font-size: 12px;
    border: none;
    cursor: pointer;
}

.xapi-chat-upsale-btn:hover {
    background: #d97706;
}

/* Typing indicator */
.xapi-chat-typing {
    display: none;
    align-self: flex-start;
    background: var(--xapi-surface);
    border: 1px solid var(--xapi-border);
    border-radius: var(--radius);
    border-bottom-left-radius: 4px;
    padding: 14px 16px;
    gap: 4px;
}

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

.xapi-chat-typing-dot {
    width: 8px;
    height: 8px;
    background: var(--xapi-text-muted);
    border-radius: 50%;
    animation: xapi-typing-bounce 1.4s infinite ease-in-out;
}

.xapi-chat-typing-dot:nth-child(1) { animation-delay: -0.32s; }
.xapi-chat-typing-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes xapi-typing-bounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* Input area */
.xapi-chat-input-area {
    padding: 12px 16px;
    background: var(--xapi-surface);
    border-top: 1px solid var(--xapi-border);
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.xapi-chat-input {
    flex: 1;
    border: 1px solid var(--xapi-border);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 14px;
    outline: none;
    resize: none;
    max-height: 100px;
    font-family: inherit;
}

.xapi-chat-input:focus {
    border-color: var(--xapi-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.xapi-chat-send {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--xapi-primary);
    color: white;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    flex-shrink: 0;
}

.xapi-chat-send:hover {
    background: var(--xapi-primary-dark);
}

.xapi-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.xapi-chat-send svg {
    width: 18px;
    height: 18px;
}

/* API key input (shown when not set) */
.xapi-chat-apikey {
    padding: 12px 16px;
    background: #fef3c7;
    border-top: 1px solid #fcd34d;
    font-size: 12px;
    color: #92400e;
    display: none;
}

.xapi-chat-apikey--visible {
    display: block;
}

.xapi-chat-apikey input {
    width: 100%;
    margin-top: 6px;
    padding: 6px 10px;
    border: 1px solid #fcd34d;
    border-radius: 6px;
    font-size: 12px;
}

/* Quick actions */
.xapi-chat-quick {
    display: flex;
    gap: 8px;
    padding: 0 16px 12px;
    overflow-x: auto;
    flex-shrink: 0;
    scrollbar-width: none;
}

.xapi-chat-quick::-webkit-scrollbar {
    display: none;
}

.xapi-chat-quick-btn {
    white-space: nowrap;
    padding: 6px 14px;
    background: var(--xapi-bg);
    border: 1px solid var(--xapi-border);
    border-radius: 20px;
    font-size: 12px;
    color: var(--xapi-text);
    cursor: pointer;
    transition: all 0.2s;
}

.xapi-chat-quick-btn:hover {
    background: var(--xapi-primary);
    color: white;
    border-color: var(--xapi-primary);
}

/* Responsive */
@media (max-width: 480px) {
    .xapi-chat-window {
        width: calc(100vw - 40px);
        height: calc(100vh - 100px);
        right: -10px;
    }
}
