/**
 * BH Chat Widget — front-end float bubble chatbot.
 * Replaces the AI Engine chatbot UI. Talks to /wp-json/bh-support/v1/chat.
 *
 * Palette: basehead sage on dark. Matches the rest of the site's CTA system.
 */

/* ---------- root ---------- */

.bh-chat {
    --sage: #b0bc92;
    --sage-dk: #90a077;
    --sage-lt: #c0cca2;
    /* Slate-blue palette matched to the lifted my-licenses dashboard
       (2026-05-21 v39). Less saturated than the previous near-black blues
       so the widget harmonizes with the rest of the site instead of
       reading as a deep-dive popover. */
    --bg: #1d2128;
    --bg-2: #232730;
    --bg-3: #272c35;
    --panel-edge: rgba(255, 255, 255, 0.07);
    --ink: #ededf2;
    --ink-dim: #c8ccd6;
    --ink-mute: #8a90a0;
    --user-bg: #b0bc92;
    --user-ink: #0a0d14;
    --ai-bg: #323841;
    --ai-ink: #ededf2;
    --danger: #d97a7a;
    --z: 9000;

    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
    color: var(--ink);
    line-height: 1.5;
}

/* ---------- bubble ---------- */

.bh-chat .bh-chat__bubble {
    position: fixed !important;
    bottom: 24px !important;
    right: 24px !important;
    width: 60px !important;
    height: 60px !important;
    padding: 0 !important;
    margin: 0 !important;
    border-radius: 50% !important;
    background: var(--sage) !important;
    background-color: var(--sage) !important;
    color: var(--bg) !important;
    border: none !important;
    cursor: pointer;
    z-index: var(--z);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.04) inset;
    display: flex !important;
    align-items: center;
    justify-content: center;
    transition: transform 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
    text-shadow: none !important;
    text-transform: none !important;
    letter-spacing: 0 !important;
    font-size: 0;
    line-height: 1;
}

.bh-chat .bh-chat__bubble:hover {
    background: var(--sage-lt) !important;
    background-color: var(--sage-lt) !important;
    transform: translateY(-2px);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.06) inset;
}

.bh-chat__bubble:focus-visible {
    outline: 2px solid var(--sage);
    outline-offset: 4px;
}

.bh-chat__bubble svg {
    width: 26px;
    height: 26px;
}

.bh-chat__bubble--open .bh-chat__bubble-icon-chat { display: none; }
.bh-chat__bubble:not(.bh-chat__bubble--open) .bh-chat__bubble-icon-close { display: none; }

/* ---------- panel ---------- */

.bh-chat__panel {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 460px;
    max-width: calc(100vw - 32px);
    height: 640px;
    max-height: calc(100vh - 140px);
    background: var(--bg-3);
    border: 1px solid var(--panel-edge);
    border-radius: 14px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
    z-index: var(--z);
    display: none;
    flex-direction: column;
    overflow: hidden;
    transform: translateY(12px);
    opacity: 0;
    transition: transform 0.22s ease, opacity 0.22s ease,
                width 0.22s ease, height 0.22s ease;
}

.bh-chat__panel--open {
    display: flex;
    transform: translateY(0);
    opacity: 1;
}

/* Expanded mode — when responses are long and you want to read without
 * constant scrolling. Toggled by the expand button in the header. */
.bh-chat__panel--expanded {
    width: 760px;
    height: calc(100vh - 140px);
    max-height: 900px;
}

/* ---------- header ---------- */

.bh-chat__header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--bg-2);
    border-bottom: 1px solid var(--panel-edge);
}

.bh-chat__header-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 15px;
    letter-spacing: 0.01em;
    color: var(--ink);
}

.bh-chat__header-title::before {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--sage);
    box-shadow: 0 0 8px var(--sage);
}

.bh-chat__header-sub {
    margin-left: 18px;
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ink-mute);
}

.bh-chat__header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.bh-chat .bh-chat__close,
.bh-chat .bh-chat__expand {
    background: none !important;
    background-color: transparent !important;
    border: none !important;
    color: var(--ink-mute) !important;
    cursor: pointer;
    padding: 4px !important;
    margin: 0 !important;
    border-radius: 6px !important;
    line-height: 0;
    transition: color 0.15s ease, background 0.15s ease;
    text-shadow: none !important;
    text-transform: none !important;
    letter-spacing: 0 !important;
    font-size: 0;
}

.bh-chat .bh-chat__close:hover,
.bh-chat .bh-chat__expand:hover {
    color: var(--ink) !important;
    background: rgba(255, 255, 255, 0.05) !important;
    background-color: rgba(255, 255, 255, 0.05) !important;
}

.bh-chat__expand svg { width: 16px; height: 16px; }

/* Toggle which expand icon shows based on panel state */
.bh-chat__panel--expanded .bh-chat__expand-icon-out { display: none; }
.bh-chat__panel:not(.bh-chat__panel--expanded) .bh-chat__expand-icon-in { display: none; }

/* On mobile the panel is already near-fullscreen, hide the expand button */
@media (max-width: 480px) {
    .bh-chat__expand { display: none !important; }
}

.bh-chat__close svg { width: 18px; height: 18px; }

/* ---------- messages ---------- */

.bh-chat__body {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 18px 16px;
    background: var(--bg-3);
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.08) transparent;
}

.bh-chat__body::-webkit-scrollbar { width: 6px; }
.bh-chat__body::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.08); border-radius: 3px; }

.bh-chat__msg {
    display: flex;
    margin-bottom: 12px;
    animation: bh-chat-fade-in 0.2s ease;
}

@keyframes bh-chat-fade-in {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

.bh-chat__msg--user { justify-content: flex-end; }
.bh-chat__msg--ai   { justify-content: flex-start; }

.bh-chat__bubble-msg {
    max-width: 86%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.bh-chat__msg--user .bh-chat__bubble-msg {
    background: var(--user-bg);
    color: var(--user-ink);
    font-weight: 500;
    border-bottom-right-radius: 4px;
}

.bh-chat__msg--ai .bh-chat__bubble-msg {
    background: var(--ai-bg);
    color: var(--ai-ink);
    border-bottom-left-radius: 4px;
}

/* Markdown rendering inside AI bubbles */
.bh-chat__bubble-msg p { margin: 0 0 8px; }
.bh-chat__bubble-msg p:last-child { margin-bottom: 0; }
.bh-chat__bubble-msg strong { color: var(--ink); font-weight: 700; }
.bh-chat__bubble-msg em { font-style: italic; color: var(--ink-dim); }
.bh-chat__bubble-msg a { color: var(--sage); text-decoration: none; border-bottom: 1px solid rgba(176, 188, 146, 0.4); }
.bh-chat__bubble-msg a:hover { color: var(--ink); border-bottom-color: var(--ink); }
.bh-chat__bubble-msg ul, .bh-chat__bubble-msg ol { margin: 6px 0 8px; padding-left: 22px; }
.bh-chat__bubble-msg li { margin-bottom: 4px; }
.bh-chat__bubble-msg code {
    background: rgba(255, 255, 255, 0.06);
    padding: 1px 5px;
    border-radius: 3px;
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 12.5px;
    color: var(--sage);
}
.bh-chat__bubble-msg h2,
.bh-chat__bubble-msg h3 {
    font-size: 14px;
    font-weight: 700;
    margin: 10px 0 4px;
    color: var(--ink);
}

/* Loading dots */
.bh-chat__loading {
    display: inline-flex;
    gap: 4px;
    align-items: center;
}
.bh-chat__loading span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--ink-mute);
    animation: bh-chat-pulse 1.2s infinite ease-in-out;
}
.bh-chat__loading span:nth-child(2) { animation-delay: 0.15s; }
.bh-chat__loading span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bh-chat-pulse {
    0%, 80%, 100% { opacity: 0.3; transform: scale(0.85); }
    40% { opacity: 1; transform: scale(1); }
}

/* Error message */
.bh-chat__msg--error .bh-chat__bubble-msg {
    background: rgba(217, 122, 122, 0.12);
    color: var(--danger);
    border: 1px solid rgba(217, 122, 122, 0.3);
    font-size: 13px;
}

/* ---------- footer (input) ---------- */

.bh-chat__footer {
    flex: 0 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 12px 14px;
    background: var(--bg-2);
    border-top: 1px solid var(--panel-edge);
}

.bh-chat .bh-chat__input {
    flex: 1 1 auto;
    background: var(--bg-3) !important;
    background-color: var(--bg-3) !important;
    border: 1px solid var(--panel-edge) !important;
    border-radius: 10px !important;
    color: var(--ink) !important;
    padding: 10px 12px !important;
    margin: 0 !important;
    font-size: 14px !important;
    font-family: inherit;
    line-height: 1.4;
    resize: none !important;
    min-height: 40px;
    max-height: 110px;
    outline: none !important;
    box-shadow: none !important;
    transition: border-color 0.15s ease;
    text-transform: none !important;
}

.bh-chat .bh-chat__input::placeholder { color: var(--ink-mute) !important; opacity: 1 !important; }

.bh-chat .bh-chat__input:focus {
    border-color: var(--sage) !important;
    outline: none !important;
    box-shadow: none !important;
}

.bh-chat .bh-chat__send {
    flex: 0 0 auto;
    width: 40px !important;
    height: 40px !important;
    padding: 0 !important;
    margin: 0 !important;
    border-radius: 50% !important;
    background: var(--sage) !important;
    background-color: var(--sage) !important;
    color: var(--bg) !important;
    border: none !important;
    cursor: pointer;
    display: flex !important;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, transform 0.15s ease;
    text-shadow: none !important;
    text-transform: none !important;
    letter-spacing: 0 !important;
    font-size: 0;
    line-height: 1;
}

.bh-chat .bh-chat__send:hover:not(:disabled) {
    background: var(--sage-lt) !important;
    background-color: var(--sage-lt) !important;
}

.bh-chat .bh-chat__send:disabled {
    background: var(--bg-3) !important;
    background-color: var(--bg-3) !important;
    color: var(--ink-mute) !important;
    cursor: not-allowed;
}

.bh-chat__send svg { width: 18px; height: 18px; }

.bh-chat__disclaimer {
    text-align: center;
    font-size: 10.5px;
    color: var(--ink-mute);
    padding: 4px 16px 8px;
    background: var(--bg-2);
    letter-spacing: 0.02em;
}

.bh-chat__disclaimer a { color: var(--ink-mute); text-decoration: underline; }
.bh-chat__disclaimer a:hover { color: var(--ink-dim); }

/* ---------- mobile ---------- */

@media (max-width: 480px) {
    .bh-chat__panel {
        right: 12px;
        left: 12px;
        bottom: 86px;
        width: auto;
        max-width: none;
        height: calc(100vh - 110px);
        max-height: calc(100vh - 110px);
    }
    .bh-chat__bubble {
        right: 16px;
        bottom: 16px;
        width: 56px;
        height: 56px;
    }
}
