/**
 * KLIC AI Assistant & Support ChatBot — Widget Stylesheet
 * Consumes --klic-chat-* design tokens bound dynamically to KLIC theme system.
 */

:root {
    --klic-chat-bg: var(--bg-card, rgba(15, 23, 42, 0.92));
    --klic-chat-glass-blur: 16px;
    --klic-chat-border: var(--border-glass, rgba(255, 255, 255, 0.12));
    --klic-chat-text-primary: var(--text-primary, #ffffff);
    --klic-chat-text-secondary: var(--text-secondary, #cbd5e1);
    --klic-chat-text-muted: var(--text-muted, #94a3b8);
    --klic-chat-accent-start: #6366f1;
    --klic-chat-accent-end: #a855f7;
    --klic-chat-accent-gradient: var(--accent-gradient, linear-gradient(135deg, #6366f1 0%, #a855f7 100%));
    --klic-chat-shadow: var(--shadow-lg, 0 16px 40px rgba(0, 0, 0, 0.5));
    --klic-chat-user-msg-bg: var(--accent-gradient, linear-gradient(135deg, #6366f1 0%, #a855f7 100%));
    --klic-chat-user-msg-color: #ffffff;
    --klic-chat-bot-msg-bg: rgba(30, 41, 59, 0.7);
    --klic-chat-bot-msg-border: rgba(255, 255, 255, 0.08);
    --klic-chat-chip-bg: rgba(99, 102, 241, 0.12);
    --klic-chat-chip-border: rgba(99, 102, 241, 0.3);
    --klic-chat-chip-color: #a5b4fc;
    --klic-chat-chip-hover-bg: rgba(99, 102, 241, 0.25);
}

[data-theme="light"] {
    --klic-chat-bg: rgba(255, 255, 255, 0.96);
    --klic-chat-border: rgba(203, 213, 225, 0.8);
    --klic-chat-text-primary: #0f172a;
    --klic-chat-text-secondary: #334155;
    --klic-chat-text-muted: #64748b;
    --klic-chat-bot-msg-bg: #f1f5f9;
    --klic-chat-bot-msg-border: #e2e8f0;
    --klic-chat-chip-bg: #e0e7ff;
    --klic-chat-chip-border: #c7d2fe;
    --klic-chat-chip-color: #4338ca;
    --klic-chat-chip-hover-bg: #c7d2fe;
}

/* ─── Launcher Button ─── */
.klic-chat-launcher {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--klic-chat-accent-gradient);
    color: #ffffff;
    border: none;
    border-radius: 9999px;
    padding: 12px 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 8px 24px rgba(99, 102, 241, 0.4);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.klic-chat-launcher:hover {
    transform: translateY(-2px) scale(1.03);
    box-shadow: 0 12px 30px rgba(99, 102, 241, 0.55);
}

.klic-chat-launcher-icon {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

.klic-chat-status-dot {
    width: 8px;
    height: 8px;
    background: #10b981;
    border-radius: 50%;
    box-shadow: 0 0 8px #10b981;
    animation: klic-pulse 2s infinite;
}

@keyframes klic-pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.2); opacity: 0.7; }
}

/* ─── Chat Container Modal ─── */
.klic-chat-window {
    position: fixed;
    bottom: 84px;
    right: 24px;
    width: 420px;
    max-width: calc(100vw - 32px);
    height: 620px;
    max-height: calc(100vh - 120px);
    background: var(--klic-chat-bg);
    backdrop-filter: blur(var(--klic-chat-glass-blur));
    -webkit-backdrop-filter: blur(var(--klic-chat-glass-blur));
    border: 1px solid var(--klic-chat-border);
    border-radius: 20px;
    box-shadow: var(--klic-chat-shadow);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.96);
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.klic-chat-window.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

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

.klic-chat-header-title {
    display: flex;
    align-items: center;
    gap: 10px;
}

.klic-chat-avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--klic-chat-accent-gradient);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    font-weight: 700;
    font-size: 16px;
}

.klic-chat-header-text h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: var(--klic-chat-text-primary);
}

.klic-chat-header-text p {
    margin: 0;
    font-size: 11px;
    color: var(--klic-chat-text-muted);
}

.klic-chat-header-actions {
    display: flex;
    gap: 6px;
}

.klic-chat-btn-icon {
    background: transparent;
    border: none;
    color: var(--klic-chat-text-muted);
    cursor: pointer;
    padding: 6px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.klic-chat-btn-icon:hover {
    background: rgba(255, 255, 255, 0.08);
    color: var(--klic-chat-text-primary);
}

/* ─── Messages Area ─── */
.klic-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    scroll-behavior: smooth;
}

.klic-chat-msg {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: klic-msg-fade 0.25s ease-out;
}

@keyframes klic-msg-fade {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.klic-chat-msg.user {
    align-self: flex-end;
}

.klic-chat-msg.assistant {
    align-self: flex-start;
}

.klic-chat-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
}

.klic-chat-msg.user .klic-chat-bubble {
    background: var(--klic-chat-user-msg-bg);
    color: var(--klic-chat-user-msg-color);
    border-bottom-right-radius: 4px;
}

.klic-chat-msg.assistant .klic-chat-bubble {
    background: var(--klic-chat-bot-msg-bg);
    border: 1px solid var(--klic-chat-bot-msg-border);
    color: var(--klic-chat-text-secondary);
    border-bottom-left-radius: 4px;
}

.klic-chat-bubble strong {
    color: var(--klic-chat-text-primary);
}

.klic-chat-bubble a {
    color: #818cf8;
    text-decoration: underline;
}

.klic-chat-bubble ul, .klic-chat-bubble ol {
    margin: 6px 0;
    padding-left: 18px;
}

.klic-chat-bubble code {
    background: rgba(0, 0, 0, 0.25);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 12px;
}

/* ─── Action Chips ─── */
.klic-chat-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.klic-chat-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: var(--klic-chat-chip-bg);
    border: 1px solid var(--klic-chat-chip-border);
    color: var(--klic-chat-chip-color);
    border-radius: 9999px;
    font-size: 12px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: all 0.2s;
}

.klic-chat-chip:hover {
    background: var(--klic-chat-chip-hover-bg);
    transform: translateY(-1px);
}

/* ─── Sources Badges ─── */
.klic-chat-sources {
    margin-top: 8px;
    padding-top: 6px;
    border-top: 1px solid var(--klic-chat-border);
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
}

.klic-chat-sources-label {
    font-size: 10.5px;
    color: var(--klic-chat-text-muted);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.klic-chat-source-badge {
    font-size: 11px;
    padding: 2px 8px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--klic-chat-border);
    border-radius: 6px;
    color: var(--klic-chat-text-muted);
    text-decoration: none;
    transition: all 0.2s;
}

.klic-chat-source-badge:hover {
    color: var(--klic-chat-text-primary);
    background: rgba(255, 255, 255, 0.1);
}

/* ─── Typing Indicator ─── */
.klic-chat-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 10px 14px;
    background: var(--klic-chat-bot-msg-bg);
    border: 1px solid var(--klic-chat-bot-msg-border);
    border-radius: 16px;
    width: fit-content;
    border-bottom-left-radius: 4px;
}

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

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

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

/* ─── Input Area ─── */
.klic-chat-footer {
    padding: 14px 18px;
    background: rgba(255, 255, 255, 0.02);
    border-top: 1px solid var(--klic-chat-border);
}

.klic-chat-input-row {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--klic-chat-border);
    border-radius: 12px;
    padding: 4px 6px 4px 14px;
    transition: border-color 0.2s;
}

.klic-chat-input-row:focus-within {
    border-color: var(--border-focus, rgba(99, 102, 241, 0.6));
}

.klic-chat-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--klic-chat-text-primary);
    font-size: 13.5px;
    padding: 8px 0;
}

.klic-chat-input::placeholder {
    color: var(--klic-chat-text-muted);
}

.klic-chat-send-btn {
    background: var(--klic-chat-accent-gradient);
    color: #ffffff;
    border: none;
    border-radius: 8px;
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: opacity 0.2s, transform 0.1s;
}

.klic-chat-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.klic-chat-send-btn:not(:disabled):hover {
    transform: scale(1.05);
}

/* ─── Escalation Modal Form ─── */
.klic-chat-escalation-panel {
    display: none;
    padding: 16px 20px;
    background: var(--klic-chat-bg);
    border-top: 1px solid var(--klic-chat-border);
    flex-direction: column;
    gap: 10px;
}

.klic-chat-escalation-panel.active {
    display: flex;
}

.klic-chat-escalation-input {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--klic-chat-border);
    border-radius: 8px;
    padding: 8px 12px;
    color: var(--klic-chat-text-primary);
    font-size: 13px;
    outline: none;
}

/* ─── Mobile Bottom Sheet Behavior ─── */
@media (max-width: 640px) {
    .klic-chat-window {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100vw;
        max-width: 100vw;
        height: 85vh;
        max-height: 85vh;
        border-radius: 20px 20px 0 0;
        border-bottom: none;
    }

    .klic-chat-launcher {
        bottom: 16px;
        right: 16px;
        padding: 10px 16px;
    }
}
