/* ── Agent Chat Widget ──────────────────────────────────────────
 *
 * Drawer on the right side of the viewport. Non-modal: dashboard
 * underneath stays interactive. Designed to read long agent replies
 * comfortably (200-500 words with metrics blocks) without forcing
 * the user to scroll inside a tiny bubble.
 *
 * Theme hooks: uses the existing CSS variables from dashboard.css
 * so glass/terminal × dark/light all inherit correctly. No new
 * palette knobs.
 */

/* Floating toggle button — always visible, bottom-right. */
.chat-fab {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: var(--btn-bg);
    border: 1px solid var(--btn-border);
    color: var(--color);
    cursor: pointer;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    box-shadow: var(--card-shadow);
    transition: transform .15s, background .2s, border-color .2s,
        box-shadow .2s;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.chat-fab:hover {
    background: var(--btn-bg-hover);
    border-color: var(--btn-border-hover);
    box-shadow: var(--btn-shadow-hover), var(--card-shadow);
    transform: scale(1.05);
}

/* Reachability dot on the FAB — green when proxy/agent answer
 * /api/agent/status, amber when checking, red when offline. */
.chat-fab-dot {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 2px solid var(--bg);
}

.chat-fab-dot.online {
    background: #4ade80;
}

.chat-fab-dot.offline {
    background: #f87171;
}

.chat-fab-dot.checking {
    background: #fbbf24;
}

/* Drawer container — hidden by default, slides in when open. */
.chat-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 420px;
    max-width: 100vw;
    background: var(--card-bg);
    backdrop-filter: blur(24px) saturate(1.2);
    -webkit-backdrop-filter: blur(24px) saturate(1.2);
    border-left: 1px solid var(--card-border);
    box-shadow: -8px 0 32px rgba(0, 0, 0, .2);
    display: flex;
    flex-direction: column;
    z-index: 1001;
    transform: translateX(100%);
    transition: transform .25s ease-out;
}

.chat-drawer.open {
    transform: translateX(0);
}

/* On narrow screens the drawer becomes full-width. The FAB stays
 * where it is, moved by the drawer's own z-index. */
@media (max-width: 520px) {
    .chat-drawer {
        width: 100vw;
    }
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--card-border);
}

.chat-title {
    font-size: .95rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: .5rem;
}

.chat-title-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted2);
}

.chat-title-dot.online {
    background: #4ade80;
}

.chat-title-dot.offline {
    background: #f87171;
}

.chat-title-dot.checking {
    background: #fbbf24;
}

.chat-actions {
    display: flex;
    gap: .3rem;
}

.chat-icon-btn {
    padding: .3rem .5rem;
    font-size: .75rem;
    border-radius: 8px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--muted3);
    cursor: pointer;
    transition: all .15s;
}

.chat-icon-btn:hover {
    color: var(--color);
    border-color: var(--btn-border);
}

.chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 1.25rem;
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) transparent;
    display: flex;
    flex-direction: column;
    gap: .75rem;
}

.chat-body::-webkit-scrollbar {
    width: 4px;
}

.chat-body::-webkit-scrollbar-track {
    background: transparent;
}

.chat-body::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 4px;
}

.chat-empty {
    color: var(--muted);
    font-size: .85rem;
    text-align: center;
    padding: 2rem 1rem;
    line-height: 1.5;
}

.chat-empty .chat-empty-hint {
    margin-top: .75rem;
    font-size: .75rem;
    color: var(--muted2);
}

/* Each message is a bubble. ``you`` on the right, ``agent`` on the
 * left. Role chip above the bubble makes the threading clear at a
 * glance without us building a whole conversation-tree UI. */
.chat-msg {
    display: flex;
    flex-direction: column;
    gap: .25rem;
    max-width: 100%;
}

.chat-msg.you {
    align-items: flex-end;
}

.chat-msg.agent {
    align-items: flex-start;
}

.chat-msg.error {
    align-items: flex-start;
}

.chat-role {
    font-size: .65rem;
    color: var(--muted2);
    text-transform: uppercase;
    letter-spacing: .5px;
    padding: 0 .3rem;
}

.chat-bubble {
    padding: .6rem .85rem;
    border-radius: 12px;
    font-size: .85rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-wrap: break-word;
    max-width: 100%;
    border: 1px solid var(--card-border);
}

.chat-msg.you .chat-bubble {
    background: var(--btn-bg);
    border-color: var(--btn-border);
    border-top-right-radius: 4px;
}

.chat-msg.agent .chat-bubble {
    background: var(--input-bg);
    border-top-left-radius: 4px;
}

.chat-msg.error .chat-bubble {
    background: rgba(248, 113, 113, .08);
    border-color: rgba(248, 113, 113, .25);
    color: #fca5a5;
    border-top-left-radius: 4px;
}

.chat-meta {
    font-size: .65rem;
    color: var(--muted);
    padding: 0 .3rem;
}

/* Thinking indicator — bouncing dots inside an agent-style bubble */
.chat-thinking {
    display: inline-flex;
    gap: .3rem;
    padding: .6rem .85rem;
    border-radius: 12px;
    border-top-left-radius: 4px;
    border: 1px solid var(--card-border);
    background: var(--input-bg);
}

.chat-thinking-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted3);
    animation: chat-dot-bounce 1.2s infinite ease-in-out;
}

.chat-thinking-dot:nth-child(2) {
    animation-delay: .15s;
}

.chat-thinking-dot:nth-child(3) {
    animation-delay: .3s;
}

@keyframes chat-dot-bounce {

    0%,
    60%,
    100% {
        opacity: .3;
        transform: translateY(0);
    }

    30% {
        opacity: 1;
        transform: translateY(-3px);
    }
}

.chat-footer {
    padding: .75rem 1rem;
    border-top: 1px solid var(--card-border);
    display: flex;
    gap: .5rem;
    align-items: flex-end;
}

.chat-input {
    flex: 1;
    min-height: 40px;
    max-height: 160px;
    padding: .5rem .75rem;
    border-radius: 10px;
    border: 1px solid var(--input-border);
    background: var(--input-bg);
    color: var(--color);
    font-family: inherit;
    font-size: .85rem;
    resize: none;
    line-height: 1.4;
}

.chat-input:focus {
    outline: none;
    border-color: var(--btn-border);
}

.chat-send {
    padding: .5rem .9rem;
    border-radius: 10px;
    border: 1px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--color);
    font-size: .8rem;
    cursor: pointer;
    font-weight: 500;
    transition: all .15s;
}

.chat-send:hover:not(:disabled) {
    background: var(--btn-bg-hover);
    border-color: var(--btn-border-hover);
}

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

/* Terminal theme tweaks — match the existing overrides' vibe. */
[data-style="terminal"] .chat-fab {
    border-radius: 2px;
    font-family: var(--font-mono);
}

[data-style="terminal"] .chat-drawer {
    border-radius: 0;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
}

[data-style="terminal"] .chat-bubble,
[data-style="terminal"] .chat-thinking,
[data-style="terminal"] .chat-input,
[data-style="terminal"] .chat-send,
[data-style="terminal"] .chat-icon-btn {
    border-radius: 2px;
    font-family: var(--font-mono);
}

[data-style="terminal"] .chat-bubble {
    font-size: .8rem;
}