/*
 * wn_ai_bridge — site search assistant widget styles.
 * Self-contained and scoped under #wn-ai-assistant to avoid clashing with the
 * host theme. All colours are CSS custom properties that can be overridden
 * per-site; honours prefers-color-scheme and prefers-reduced-motion.
 *
 * The palette is stated twice, once per colour scheme, and the two are complete
 * rather than one being a patch on the other: a site configures a light set and
 * a dark set, and whichever one the browser asks for has to stand on its own.
 * Every pair below is measured against WCAG 2.2 — 4.5:1 for text, 3:1 for the
 * boundary of a control.
 */
/*
 * A site hands its own palette in through --wn-ai-light-* and --wn-ai-dark-*,
 * written onto the widget element as a style attribute by the controller.
 *
 * Those two sets are read here rather than being applied directly, and the
 * reason is the Content Security Policy: an inline <style> element needs
 * `style-src-elem`, which a site running `default-src 'self'` does not grant,
 * while a style *attribute* is commonly allowed and is what carries no media
 * query. So the values arrive as attributes and the scheme switch stays in this
 * file, where a media query can reach it.
 *
 * Each declaration therefore reads three deep: the site's value for this
 * scheme, then its value for the light scheme, then the default below. That
 * middle step is what makes a dark field left empty fall back to the light one.
 */
#wn-ai-assistant {
    --wn-ai-accent: var(--wn-ai-light-accent, #2563eb);                   /* 5.2:1 on the panel */
    --wn-ai-accent-contrast: var(--wn-ai-light-accent-contrast, #ffffff); /* 5.2:1 on the accent */
    --wn-ai-bg: var(--wn-ai-light-bg, #ffffff);
    --wn-ai-fg: var(--wn-ai-light-fg, #1f2933);                           /* 14.8:1 */
    --wn-ai-muted: #6b7280;                                               /* 4.8:1 */
    --wn-ai-border: color-mix(in srgb, var(--wn-ai-fg) 18%, transparent);

    --wn-ai-user-bg: var(--wn-ai-light-user-bg, #2563eb);
    --wn-ai-user-fg: var(--wn-ai-light-user-fg, #ffffff);
    --wn-ai-user-link: var(--wn-ai-light-user-link, var(--wn-ai-user-fg));

    --wn-ai-assistant-bg: var(--wn-ai-light-assistant-bg, #f3f4f6);
    --wn-ai-assistant-fg: var(--wn-ai-light-assistant-fg, var(--wn-ai-fg));
    --wn-ai-assistant-link: var(--wn-ai-light-assistant-link, var(--wn-ai-accent));

    --wn-ai-sources-bg: var(--wn-ai-light-sources-bg, var(--wn-ai-bg));
    --wn-ai-sources-fg: var(--wn-ai-light-sources-fg, var(--wn-ai-muted));
    --wn-ai-sources-link: var(--wn-ai-light-sources-link, var(--wn-ai-accent));

    --wn-ai-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
    --wn-ai-radius: 16px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    /* Base text size for the whole widget. Every text element inherits this, so
       the chat renders at a consistent 1rem throughout. */
    font-size: 1rem;
    line-height: 1.5;
}

/* Toggle button ------------------------------------------------------- */
#wn-ai-assistant .wn-ai-toggle {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: 58px;
    height: 58px;
    border: none;
    border-radius: 50%;
    background: var(--wn-ai-accent);
    color: var(--wn-ai-accent-contrast);
    cursor: pointer;
    box-shadow: var(--wn-ai-shadow);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2147483000;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

#wn-ai-assistant .wn-ai-toggle:hover {
    transform: scale(1.06);
}

#wn-ai-assistant .wn-ai-toggle:focus-visible {
    outline: 3px solid var(--wn-ai-accent);
    outline-offset: 3px;
}

#wn-ai-assistant.wn-ai--open .wn-ai-toggle {
    transform: scale(0.9);
    opacity: 0.85;
}

/* Panel --------------------------------------------------------------- */
#wn-ai-assistant .wn-ai-panel {
    position: fixed;
    right: 24px;
    bottom: 96px;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 560px;
    max-height: calc(100vh - 120px);
    background: var(--wn-ai-bg);
    color: var(--wn-ai-fg);
    border: 1px solid var(--wn-ai-border);
    border-radius: var(--wn-ai-radius);
    box-shadow: var(--wn-ai-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 2147483000;
    animation: wn-ai-in 0.18s ease;
}

/* The panel is closed by setting the hidden attribute. Without this rule the
   display above wins over the browser's own [hidden] style and the panel stays
   on screen — the close button then looks broken. */
#wn-ai-assistant .wn-ai-panel[hidden] {
    display: none;
}

@keyframes wn-ai-in {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Header — uses the panel background + text colour so the close button and
   title honour the configured Text Color. */
#wn-ai-assistant .wn-ai-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px;
    background: var(--wn-ai-bg);
    color: var(--wn-ai-fg);
    border-bottom: 1px solid var(--wn-ai-border);
}

#wn-ai-assistant .wn-ai-title {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--wn-ai-accent);
}

#wn-ai-assistant .wn-ai-header__actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex: 0 0 auto;
}

#wn-ai-assistant .wn-ai-close,
#wn-ai-assistant .wn-ai-new {
    border: none;
    background: transparent;
    color: var(--wn-ai-fg);
    cursor: pointer;
    padding: 4px;
    border-radius: 8px;
    display: flex;
}

#wn-ai-assistant .wn-ai-close:hover,
#wn-ai-assistant .wn-ai-new:hover {
    background: color-mix(in srgb, var(--wn-ai-fg) 12%, transparent);
}

#wn-ai-assistant .wn-ai-close:focus-visible,
#wn-ai-assistant .wn-ai-new:focus-visible {
    outline: 2px solid var(--wn-ai-accent);
    outline-offset: 2px;
}

/* Confirm overlay (custom, semi-transparent replacement for window.confirm) --- */
#wn-ai-assistant .wn-ai-confirm {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: color-mix(in srgb, var(--wn-ai-bg) 55%, transparent);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
    animation: wn-ai-in 0.15s ease;
}

#wn-ai-assistant .wn-ai-confirm__box {
    width: 100%;
    max-width: 300px;
    background: var(--wn-ai-bg);
    color: var(--wn-ai-fg);
    border: 1px solid var(--wn-ai-border);
    border-radius: 14px;
    box-shadow: var(--wn-ai-shadow);
    padding: 18px;
    text-align: center;
}

#wn-ai-assistant .wn-ai-confirm__text {
    margin: 0 0 16px;
    font-size: 1rem;
    line-height: 1.45;
}

#wn-ai-assistant .wn-ai-confirm__actions {
    display: flex;
    gap: 8px;
    justify-content: center;
}

#wn-ai-assistant .wn-ai-confirm__btn {
    border: none;
    border-radius: 10px;
    padding: 9px 16px;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

#wn-ai-assistant .wn-ai-confirm__btn--cancel {
    background: color-mix(in srgb, var(--wn-ai-fg) 12%, transparent);
    color: var(--wn-ai-fg);
}

#wn-ai-assistant .wn-ai-confirm__btn--ok {
    background: var(--wn-ai-accent);
    color: var(--wn-ai-accent-contrast);
}

#wn-ai-assistant .wn-ai-confirm__btn:hover {
    filter: brightness(1.05);
}

#wn-ai-assistant .wn-ai-confirm__btn:focus-visible {
    outline: 2px solid var(--wn-ai-accent);
    outline-offset: 2px;
}

/* Avatar (logo / photo) ----------------------------------------------- */
#wn-ai-assistant .wn-ai-header__group {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

#wn-ai-assistant .wn-ai-header__group .wn-ai-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#wn-ai-assistant .wn-ai-header__avatar {
    width: 36px;
    height: 36px;
    flex: 0 0 auto;
    border-radius: 50%;
    object-fit: cover;
    background: var(--wn-ai-bg);
    box-shadow: 0 0 0 2px var(--wn-ai-accent);
}

#wn-ai-assistant .wn-ai-msg--avatar {
    flex-direction: row;
    align-items: flex-end;
    gap: 8px;
}

#wn-ai-assistant .wn-ai-msg--avatar .wn-ai-bubble {
    max-width: calc(100% - 42px);
}

#wn-ai-assistant .wn-ai-msg__avatar {
    width: 30px;
    height: 30px;
    flex: 0 0 auto;
    border-radius: 50%;
    object-fit: cover;
    background: var(--wn-ai-bg);
    border: 1px solid var(--wn-ai-border);
}

/* Messages ------------------------------------------------------------ */
#wn-ai-assistant .wn-ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

#wn-ai-assistant .wn-ai-msg {
    display: flex;
    flex-direction: column;
    max-width: 100%;
}

#wn-ai-assistant .wn-ai-bubble {
    padding: 10px 13px;
    border-radius: 14px;
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

#wn-ai-assistant .wn-ai-msg--user {
    align-items: flex-end;
}

#wn-ai-assistant .wn-ai-msg--user .wn-ai-bubble {
    background: var(--wn-ai-user-bg);
    color: var(--wn-ai-user-fg);
    border-bottom-right-radius: 4px;
    max-width: 85%;
}

#wn-ai-assistant .wn-ai-msg--user .wn-ai-bubble a {
    color: var(--wn-ai-user-link);
}

#wn-ai-assistant .wn-ai-msg--assistant .wn-ai-bubble {
    background: var(--wn-ai-assistant-bg);
    color: var(--wn-ai-assistant-fg);
    border-bottom-left-radius: 4px;
    max-width: 90%;
}

#wn-ai-assistant .wn-ai-msg--assistant .wn-ai-bubble a {
    color: var(--wn-ai-assistant-link);
}

/* Inline page-title links inside the answer --------------------------- */
#wn-ai-assistant .wn-ai-cite {
    color: var(--wn-ai-accent);
    text-decoration: underline;
    text-underline-offset: 2px;
    font-weight: 600;
}

#wn-ai-assistant .wn-ai-cite:hover {
    text-decoration: none;
}

/* Sources — kept compact so a handful of hits do not fill the panel:
   a tight, bullet-free list of small title links (no snippets, no cards). */
#wn-ai-assistant .wn-ai-sources__label {
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--wn-ai-muted);
    margin-bottom: 4px;
}

#wn-ai-assistant .wn-ai-sources {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

#wn-ai-assistant .wn-ai-source {
    display: block;
    padding: 2px 0;
    text-decoration: none;
    color: var(--wn-ai-sources-link);
    font-size: 0.85rem;
    font-weight: 500;
    line-height: 1.35;
}

#wn-ai-assistant .wn-ai-source:hover {
    text-decoration: underline;
}

#wn-ai-assistant .wn-ai-source:focus-visible {
    outline: 2px solid var(--wn-ai-sources-link);
    outline-offset: 2px;
}

/* Typing indicator ---------------------------------------------------- */
#wn-ai-assistant .wn-ai-typing .wn-ai-bubble {
    color: var(--wn-ai-muted);
    font-style: italic;
}

/* Input --------------------------------------------------------------- */
#wn-ai-assistant .wn-ai-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px;
    border-top: 1px solid var(--wn-ai-border);
    background: var(--wn-ai-bg);
}

#wn-ai-assistant .wn-ai-input {
    flex: 1;
    resize: none;
    border: 1px solid var(--wn-ai-border);
    border-radius: 12px;
    padding: 10px 12px;
    font: inherit;
    color: var(--wn-ai-fg);
    background: var(--wn-ai-bg);
    max-height: 140px;
}

#wn-ai-assistant .wn-ai-input:focus-visible {
    outline: 2px solid var(--wn-ai-accent);
    outline-offset: 1px;
    border-color: var(--wn-ai-accent);
}

#wn-ai-assistant .wn-ai-send {
    flex: 0 0 auto;
    width: 42px;
    height: 42px;
    border: none;
    border-radius: 12px;
    background: var(--wn-ai-accent);
    color: var(--wn-ai-accent-contrast);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

#wn-ai-assistant .wn-ai-send:hover {
    filter: brightness(1.05);
}

#wn-ai-assistant .wn-ai-send:disabled {
    opacity: 0.5;
    cursor: default;
}

#wn-ai-assistant .wn-ai-send:focus-visible {
    outline: 2px solid var(--wn-ai-accent);
    outline-offset: 2px;
}

/* Mobile — stay anchored bottom-right instead of covering the whole page;
   leave ~30px breathing room on the left and top so the site stays visible. */
@media (max-width: 480px) {
    #wn-ai-assistant .wn-ai-panel {
        right: 0;
        bottom: 0;
        width: calc(100vw - 30px);
        max-width: calc(100vw - 30px);
        height: calc(100dvh - 30px);
        max-height: calc(100dvh - 30px);
    }

    #wn-ai-assistant .wn-ai-toggle {
        right: 16px;
        bottom: 16px;
    }
}

/* Dark mode (applies only to colours the site has not overridden) ------ */
/*
 * The dark scheme used to change four values and leave the accent alone, which
 * is why the link in an assistant bubble measured 2.45:1 and the toggle button
 * 3.0:1 against the panel — a blue mixed to be read on white cannot be read on
 * #1F2430. The accent is lifted here and the ink that sits on it goes dark with
 * it, so the round button and the visitor's own bubbles keep their contrast
 * instead of turning into white on mid blue.
 */
/*
 * Every role is restated here, including the ones whose default is unchanged.
 * The block above carries no media query, so anything left out of this one
 * would keep the light declaration and the site's dark value would never be
 * reached. Each line reads the dark value, then the light one, then the dark
 * default — which is the fallback order the site configuration promises.
 */
@media (prefers-color-scheme: dark) {
    #wn-ai-assistant {
        /* 7.4:1 on the panel, 6.1:1 in a bubble; the ink 9.0:1 on the accent. */
        --wn-ai-accent: var(--wn-ai-dark-accent, var(--wn-ai-light-accent, #8ab4ff));
        --wn-ai-accent-contrast: var(--wn-ai-dark-accent-contrast, var(--wn-ai-light-accent-contrast, #0b1220));
        --wn-ai-bg: var(--wn-ai-dark-bg, var(--wn-ai-light-bg, #1f2430));
        --wn-ai-fg: var(--wn-ai-dark-fg, var(--wn-ai-light-fg, #e5e7eb));  /* 12.5:1 */
        --wn-ai-muted: #9ca3af;                                            /* 6.1:1 */

        --wn-ai-user-bg: var(--wn-ai-dark-user-bg, var(--wn-ai-light-user-bg, #8ab4ff));
        --wn-ai-user-fg: var(--wn-ai-dark-user-fg, var(--wn-ai-light-user-fg, #0b1220));
        --wn-ai-user-link: var(--wn-ai-dark-user-link, var(--wn-ai-light-user-link, var(--wn-ai-user-fg)));

        --wn-ai-assistant-bg: var(--wn-ai-dark-assistant-bg, var(--wn-ai-light-assistant-bg, #2d3340));
        --wn-ai-assistant-fg: var(--wn-ai-dark-assistant-fg, var(--wn-ai-light-assistant-fg, var(--wn-ai-fg)));
        --wn-ai-assistant-link: var(--wn-ai-dark-assistant-link, var(--wn-ai-light-assistant-link, var(--wn-ai-accent)));

        --wn-ai-sources-bg: var(--wn-ai-dark-sources-bg, var(--wn-ai-light-sources-bg, var(--wn-ai-bg)));
        --wn-ai-sources-fg: var(--wn-ai-dark-sources-fg, var(--wn-ai-light-sources-fg, var(--wn-ai-muted)));
        --wn-ai-sources-link: var(--wn-ai-dark-sources-link, var(--wn-ai-light-sources-link, var(--wn-ai-accent)));

        --wn-ai-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    }
}

/* Reduced motion ------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
    #wn-ai-assistant .wn-ai-panel,
    #wn-ai-assistant .wn-ai-toggle {
        animation: none;
        transition: none;
    }
}
