/* ─── In-browser LLM chat balloon ──────────────────────────────────── */
#ffm-chat-root {
    --ffm-chat-accent: #0c7489;
    --ffm-chat-accent-hover: #0a6275;
    --ffm-chat-bg: #ffffff;
    --ffm-chat-surface: #f7f8fa;
    --ffm-chat-border: #e5e7eb;
    --ffm-chat-text: #111827;
    --ffm-chat-muted: #6b7280;
    --ffm-chat-user-bg: #0c7489;
    --ffm-chat-user-fg: #ffffff;
    --ffm-chat-assistant-bg: #f0f8f9;
    --ffm-chat-error: #dc2626;
    --ffm-chat-radius: 14px;
    --ffm-chat-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);

    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    z-index: 1000;
    font-family: inherit;
    color: var(--ffm-chat-text);
}

[data-theme="dark"] #ffm-chat-root {
    --ffm-chat-bg: #1b1f27;
    --ffm-chat-surface: #242a34;
    --ffm-chat-border: #333a46;
    --ffm-chat-text: #e8eaef;
    --ffm-chat-muted: #9aa3b2;
    --ffm-chat-assistant-bg: #1f2a33;
    --ffm-chat-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

/* Reset Pico defaults that leak into our scoped widgets
   (Pico sets button width:100%, large margins, line-heights, etc.) */
#ffm-chat-root button,
#ffm-chat-root input {
    width: auto;
    margin: 0;
    line-height: 1.2;
    box-sizing: border-box;
}
#ffm-chat-root input { height: auto; }

#ffm-chat-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1rem;
    border: none;
    border-radius: 100px;
    background: var(--ffm-chat-accent);
    color: #fff;
    font-weight: 600;
    font-size: 0.88rem;
    cursor: pointer;
    box-shadow: var(--ffm-chat-shadow);
    transition: transform 0.15s ease, background 0.15s ease;
}

#ffm-chat-toggle:hover { background: var(--ffm-chat-accent-hover); transform: translateY(-1px); }
#ffm-chat-toggle[aria-expanded="true"] { display: none; }

#ffm-chat-panel {
    width: min(400px, calc(100vw - 2rem));
    max-width: 400px;
    height: min(560px, calc(100vh - 2rem));
    background: var(--ffm-chat-bg);
    border: 1px solid var(--ffm-chat-border);
    border-radius: var(--ffm-chat-radius);
    box-shadow: var(--ffm-chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.ffm-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--ffm-chat-border);
    background: var(--ffm-chat-surface);
}

.ffm-chat-heading { display: flex; flex-direction: column; gap: 0.1rem; min-width: 0; }
.ffm-chat-heading strong { font-size: 0.92rem; line-height: 1.25; }
.ffm-chat-heading strong span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 260px;
    display: inline-block;
    vertical-align: bottom;
}

.ffm-chat-status {
    color: var(--ffm-chat-muted);
    font-size: 0.7rem;
    font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
}
.ffm-chat-status::before {
    content: "";
    width: 7px; height: 7px; border-radius: 50%;
    background: #fbbf24;
    animation: ffm-chat-pulse 1.4s ease-in-out infinite;
}
.ffm-chat-status[data-state="ready"]::before { background: #34d399; animation: none; }
.ffm-chat-status[data-state="error"]::before { background: #f87171; animation: none; }

@keyframes ffm-chat-pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.5; transform: scale(0.85); }
}

.ffm-chat-close {
    border: none; background: transparent;
    color: var(--ffm-chat-muted); cursor: pointer;
    padding: 0.25rem; border-radius: 6px;
    display: inline-flex; align-items: center; justify-content: center;
}
.ffm-chat-close:hover { background: var(--ffm-chat-border); color: var(--ffm-chat-text); }

.ffm-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    background: var(--ffm-chat-bg);
}

.ffm-chat-msg {
    max-width: 85%;
    padding: 0.55rem 0.8rem;
    border-radius: 12px;
    font-size: 0.9rem;
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
}
.ffm-chat-msg.user {
    align-self: flex-end;
    background: var(--ffm-chat-user-bg);
    color: var(--ffm-chat-user-fg);
    border-bottom-right-radius: 4px;
}
.ffm-chat-msg.assistant {
    align-self: flex-start;
    background: var(--ffm-chat-assistant-bg);
    color: var(--ffm-chat-text);
    border: 1px solid var(--ffm-chat-border);
    border-bottom-left-radius: 4px;
}
.ffm-chat-msg.system {
    align-self: center;
    background: transparent;
    color: var(--ffm-chat-muted);
    font-size: 0.78rem;
    text-align: center;
    max-width: 100%;
    padding: 0.35rem 0.6rem;
}
.ffm-chat-msg.error {
    align-self: flex-start;
    background: #fee2e2;
    color: var(--ffm-chat-error);
    border: 1px solid #fecaca;
}

.ffm-chat-msg.assistant.streaming::after {
    content: "";
    display: inline-block;
    width: 2px; height: 1em;
    background: var(--ffm-chat-accent);
    margin-left: 2px; vertical-align: text-bottom;
    animation: ffm-chat-blink 0.7s step-end infinite;
}
@keyframes ffm-chat-blink { 50% { opacity: 0; } }

.ffm-chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    padding: 0.4rem 0.75rem;
    border-top: 1px solid var(--ffm-chat-border);
    background: var(--ffm-chat-surface);
}
#ffm-chat-root .ffm-chat-chip {
    width: auto;
    flex: 0 0 auto;
    padding: 0.25rem 0.6rem;
    min-height: 0;
    border-radius: 100px;
    border: 1px solid var(--ffm-chat-border);
    background: var(--ffm-chat-bg);
    color: var(--ffm-chat-accent);
    font-size: 0.72rem;
    line-height: 1.25;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s ease, color 0.15s ease;
}
.ffm-chat-chip:hover:not(:disabled) {
    background: var(--ffm-chat-accent);
    color: #fff;
    border-color: var(--ffm-chat-accent);
}
.ffm-chat-chip:disabled { opacity: 0.5; cursor: not-allowed; }

.ffm-chat-form {
    display: flex;
    align-items: stretch;
    gap: 0.4rem;
    padding: 0.65rem 0.85rem;
    background: var(--ffm-chat-surface);
    border-top: 1px solid var(--ffm-chat-border);
}
#ffm-chat-root .ffm-chat-input {
    flex: 1 1 auto;
    min-width: 0;
    width: auto;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--ffm-chat-border);
    border-radius: 10px;
    background: var(--ffm-chat-bg);
    color: var(--ffm-chat-text);
    font-size: 0.9rem;
    font-family: inherit;
    line-height: 1.4;
    outline: none;
}
#ffm-chat-root .ffm-chat-input:focus {
    border-color: var(--ffm-chat-accent);
    box-shadow: 0 0 0 3px rgba(12, 116, 137, 0.12);
}
#ffm-chat-root .ffm-chat-send {
    flex: 0 0 auto;
    width: auto;
    border: none;
    border-radius: 10px;
    padding: 0 0.9rem;
    background: var(--ffm-chat-accent);
    color: #fff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.ffm-chat-send:hover:not(:disabled) { background: var(--ffm-chat-accent-hover); }
.ffm-chat-send:disabled { opacity: 0.45; cursor: not-allowed; }

.ffm-chat-footer {
    padding: 0.4rem 0.85rem 0.6rem;
    text-align: center;
    background: var(--ffm-chat-surface);
}
.ffm-chat-footer small { color: var(--ffm-chat-muted); font-size: 0.68rem; }

@media (max-width: 560px) {
    #ffm-chat-root { bottom: 0.75rem; right: 0.75rem; left: 0.75rem; }
    #ffm-chat-toggle .ffm-chat-toggle-label { display: none; }
    #ffm-chat-toggle { padding: 0.7rem; border-radius: 50%; }
    #ffm-chat-panel {
        width: 100%;
        height: min(70vh, 520px);
    }
}
