/**
 * Chat Agent Widget
 * Floating conversational assistant – matches the site's
 * minimalistic scientific aesthetic (Inter, blue accent).
 */

#chat-agent {
    --chat-accent: var(--color-accent, #1e40af);
    --chat-accent-dark: var(--color-accent-hover, #1e3a8a);
    --chat-bg: #ffffff;
    --chat-surface: #f3f4f6;
    --chat-border: var(--color-border, #e5e7eb);
    --chat-text: var(--color-text-primary, #1a1a1a);
    --chat-muted: var(--color-text-muted, #6b6b6b);
    --chat-radius: 14px;

    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    z-index: 9999;
    font-family: var(--font-body, 'Inter', -apple-system, BlinkMacSystemFont, sans-serif);
}

/* ── toggle button ───────────────────────────────────────── */

#chat-toggle {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--chat-accent);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 14px rgba(30, 64, 175, 0.30);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}
#chat-toggle:hover {
    transform: scale(1.06);
    box-shadow: 0 6px 20px rgba(30, 64, 175, 0.40);
}
#chat-toggle.chat-open { display: none; }

/* ── panel ───────────────────────────────────────────────── */

#chat-panel {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    width: 370px;
    max-width: calc(100vw - 2rem);
    height: 500px;
    max-height: calc(100vh - 4rem);
    background: var(--chat-bg);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.12);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
#chat-panel.chat-hidden { display: none; }

/* ── header ──────────────────────────────────────────────── */

#chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--chat-border);
}
#chat-header span {
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--chat-text);
    letter-spacing: -0.01em;
}
#chat-close {
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--chat-muted);
    cursor: pointer;
    padding: 0 0.25rem;
    line-height: 1;
    transition: color 0.15s;
}
#chat-close:hover { color: var(--chat-text); }

/* ── messages ────────────────────────────────────────────── */

#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.875rem;
    display: flex;
    flex-direction: column;
    gap: 0.625rem;
    scroll-behavior: smooth;
}
#chat-messages::-webkit-scrollbar { width: 4px; }
#chat-messages::-webkit-scrollbar-thumb {
    background: var(--chat-border);
    border-radius: 2px;
}

.chat-msg { max-width: 88%; line-height: 1.5; }
.chat-msg.user   { align-self: flex-end; }
.chat-msg.assistant { align-self: flex-start; }

.chat-msg p {
    margin: 0;
    padding: 0.5rem 0.75rem;
    border-radius: 10px;
    font-size: 0.8125rem;
    line-height: 1.55;
    color: var(--chat-text);
    white-space: pre-wrap;
    word-break: break-word;
}
.chat-msg.user p {
    background: var(--chat-accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.chat-msg.assistant p {
    background: var(--chat-surface);
    border-bottom-left-radius: 4px;
}

/* ── source pills ────────────────────────────────────────── */

.chat-sources {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    margin-top: 0.35rem;
    padding-left: 0.125rem;
}
.chat-source-link {
    font-size: 0.6875rem;
    color: var(--chat-accent);
    text-decoration: none;
    padding: 0.15rem 0.45rem;
    border: 1px solid var(--chat-border);
    border-radius: 5px;
    transition: background 0.15s;
    white-space: nowrap;
}
.chat-source-link:hover { background: #eef2ff; }

/* ── loading dots ────────────────────────────────────────── */

.chat-dots {
    display: flex;
    gap: 4px;
    padding: 0.6rem 0.85rem;
    background: var(--chat-surface);
    border-radius: 10px;
    border-bottom-left-radius: 4px;
    width: fit-content;
}
.chat-dots span {
    width: 5px;
    height: 5px;
    background: var(--chat-muted);
    border-radius: 50%;
    animation: chatBounce 1.4s infinite both;
}
.chat-dots span:nth-child(2) { animation-delay: 0.16s; }
.chat-dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes chatBounce {
    0%, 80%, 100% { transform: scale(0.6); opacity: 0.35; }
    40%           { transform: scale(1);   opacity: 1; }
}

/* ── input form ──────────────────────────────────────────── */

#chat-form {
    display: flex;
    align-items: center;
    padding: 0.625rem;
    border-top: 1px solid var(--chat-border);
    gap: 0.4rem;
    background: var(--chat-bg);
}
#chat-input {
    flex: 1;
    border: 1px solid var(--chat-border);
    border-radius: 8px;
    padding: 0.45rem 0.65rem;
    font-size: 0.8125rem;
    font-family: inherit;
    outline: none;
    transition: border-color 0.15s;
    color: var(--chat-text);
    background: var(--chat-bg);
}
#chat-input:focus { border-color: var(--chat-accent); }
#chat-input::placeholder { color: var(--chat-muted); }

#chat-send {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: var(--chat-accent);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}
#chat-send:hover { background: var(--chat-accent-dark); }
#chat-send:disabled { opacity: 0.45; cursor: not-allowed; }

/* ── responsive ──────────────────────────────────────────── */

@media (max-width: 480px) {
    #chat-panel {
        width: calc(100vw - 1rem);
        height: calc(100vh - 4rem);
        right: 0.5rem;
        bottom: 0.5rem;
        border-radius: 12px;
    }
    #chat-agent { bottom: 0.75rem; right: 0.75rem; }
}

@media print {
    #chat-agent { display: none !important; }
}
