/**
 * AI Chat Assistant for Business — front-end widget
 * All rules scoped under #aicafb-chat-portal to reduce theme CSS collisions
 * (Twenty Twenty*, block themes, global button/input styles, etc.).
 */

#aicafb-chat-portal.aicafb-chat-portal {
    /* Design tokens (scoped; do not use :root — themes often redefine it). */
    --aicafb-primary: #2563eb;
    --aicafb-primary-hover: #1d4ed8;
    --aicafb-header-gradient: linear-gradient(135deg, #1d4ed8 0%, #2563eb 55%, #3b82f6 100%);
    --aicafb-bg: #f8fafc;
    --aicafb-panel: #ffffff;
    --aicafb-text: #0f172a;
    --aicafb-muted: #64748b;
    --aicafb-placeholder: #94a3b8;
    --aicafb-border: #e2e8f0;
    --aicafb-divider: #cbd5e1;
    --aicafb-online: #22c55e;
    --aicafb-user-bubble: #2563eb;
    --aicafb-widget-width: 400px;
    --aicafb-radius-xl: 24px;
    --aicafb-radius-lg: 22px;
    --aicafb-radius-md: 20px;
    --aicafb-shadow-widget: 0 20px 50px rgba(15, 23, 42, 0.12);
    --aicafb-shadow-bubble: 0 6px 18px rgba(15, 23, 42, 0.06);
    --aicafb-shadow-button: 0 8px 20px rgba(37, 99, 235, 0.28);
    --aicafb-font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

    position: static;
    z-index: auto;
    isolation: isolate;
    contain: style;
    font-family: var(--aicafb-font);
    font-size: 16px;
    line-height: 1.5;
    color: var(--aicafb-text);
    -webkit-font-smoothing: antialiased;
}

#aicafb-chat-portal.aicafb-chat-portal *,
#aicafb-chat-portal.aicafb-chat-portal *::before,
#aicafb-chat-portal.aicafb-chat-portal *::after {
    box-sizing: border-box;
}

/* Screen reader label: avoid theme stripping visibility */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-sr-only {
    border: 0;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    white-space: nowrap;
}

/* Launcher */
#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: 58px;
    height: 58px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--aicafb-primary) !important;
    color: #fff !important;
    box-shadow: var(--aicafb-shadow-button);
    z-index: 100000;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font: inherit;
    line-height: 0;
    transition: transform 0.2s ease, background-color 0.2s ease;
}

#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher:hover {
    background: var(--aicafb-primary-hover) !important;
    color: #fff !important;
    transform: scale(1.04);
}

#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher:focus-visible {
    outline: 2px solid var(--aicafb-primary);
    outline-offset: 3px;
}

/* Widget shell */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget {
    position: fixed;
    right: 24px;
    bottom: 24px;
    width: min(var(--aicafb-widget-width), calc(100vw - 48px));
    height: 600px;
    max-height: min(600px, calc(100dvh - 48px));
    background: var(--aicafb-bg) !important;
    color: var(--aicafb-text) !important;
    border-radius: var(--aicafb-radius-xl);
    border: 1px solid var(--aicafb-border);
    box-shadow: var(--aicafb-shadow-widget);
    overflow: hidden;
    z-index: 99999;
    font-family: var(--aicafb-font) !important;
    font-size: 16px;
    line-height: 1.5;
    display: flex;
    flex-direction: column;
    min-height: 0;
    text-align: left;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized {
    width: min(320px, calc(100vw - 48px));
    height: 92px;
    max-height: none;
    border-radius: 18px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-messages,
#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-input-area {
    display: none !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-header {
    min-height: auto;
    padding: 16px 18px;
    border-radius: 18px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-title {
    font-size: 17px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-status {
    display: none !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-bot-mark {
    width: 56px;
    height: 56px;
}

/* Header */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header {
    flex: 0 0 auto;
    min-height: auto;
    padding: 24px 28px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: var(--aicafb-header-gradient) !important;
    color: #fff !important;
    border: 0;
    margin: 0;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header-left {
    display: flex;
    gap: 16px;
    align-items: center;
    min-width: 0;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-bot-mark {
    flex: 0 0 auto;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.12) !important;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-svg {
    width: 25.75px;
    height: 24.71px;
    display: block;
    flex-shrink: 0;
    color: inherit;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-svg--lg {
    width: 24px;
    height: 24px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header-texts {
    min-width: 0;
}

#aicafb-chat-portal.aicafb-chat-portal h2.aicafb-chat-title {
    margin: 0 !important;
    padding: 0 !important;
    font-family: var(--aicafb-font) !important;
    font-size: 18px !important;
    line-height: 1.2 !important;
    font-weight: 700 !important;
    color: #fff !important;
    letter-spacing: normal;
    text-transform: none;
    border: 0;
    background: transparent !important;
}

#aicafb-chat-portal.aicafb-chat-portal p.aicafb-chat-status {
    margin: 8px 0 0 !important;
    padding: 0 !important;
    font-family: var(--aicafb-font) !important;
    font-size: 14px !important;
    font-weight: 500 !important;
    line-height: 1.2 !important;
    color: #fff !important;
    opacity: 0.95;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 0;
    background: transparent !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--aicafb-online) !important;
    box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.25);
    flex-shrink: 0;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

#aicafb-chat-portal.aicafb-chat-portal #ai-chat-menu {
    display: none !important;
}

#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-icon-btn {
    width: 36px;
    height: 36px;
    margin: 0;
    padding: 0;
    border: 0 !important;
    border-radius: 16px;
    background: transparent !important;
    color: #fff !important;
    cursor: pointer;
    opacity: 0.92;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font: inherit;
    line-height: 0;
    box-shadow: none !important;
    transition: opacity 0.2s ease, background-color 0.2s ease, transform 0.2s ease;
}

#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-icon-btn:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.15) !important;
    color: #fff !important;
}

#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-icon-btn:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-minimize-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: inherit;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-min-restore {
    display: none;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-minimize-icon[data-state="minimized"] .aicafb-chat-min-open {
    display: none;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-minimize-icon[data-state="minimized"] .aicafb-chat-min-restore {
    display: block;
}

/* Messages */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 22px 24px;
    background: var(--aicafb-bg) !important;
    display: flex;
    flex-direction: column;
    gap: 18px;
    scrollbar-width: thin;
    scrollbar-color: #c8d5e6 transparent;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-messages::-webkit-scrollbar {
    width: 8px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-messages::-webkit-scrollbar-thumb {
    background: #c8d5e6;
    border-radius: 999px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row {
    display: flex;
    flex-direction: column;
    max-width: 100%;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user {
    align-items: flex-end;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant {
    align-items: flex-start;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble {
    font-family: var(--aicafb-font) !important;
    font-size: 16px !important;
    line-height: 1.6 !important;
    font-weight: 400 !important;
    border-radius: var(--aicafb-radius-lg);
    padding: 15px 25px 15px 25px !important;
    box-shadow: var(--aicafb-shadow-bubble);
    word-break: break-word;
    overflow-wrap: anywhere;
    text-align: left;
    border: 0;
    margin: 0;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant .aicafb-msg-bubble {
    max-width: 80%;
    background: var(--aicafb-panel) !important;
    color: var(--aicafb-text) !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-bubble {
    max-width: 60%;
    background: var(--aicafb-user-bubble) !important;
    color: #fff !important;
    border-radius: 22px 22px 6px 22px;
    box-shadow: 0 10px 25px rgba(37, 99, 235, 0.22);
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble p {
    margin: 0 !important;
    padding: 0 !important;
    color: inherit !important;
    font: inherit !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant .aicafb-msg-bubble strong {
    color: var(--aicafb-primary) !important;
    font-weight: 600 !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-bubble strong {
    color: #fff !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble a {
    color: var(--aicafb-primary) !important;
    text-decoration: underline;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-bubble a {
    color: #fff !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble img,
#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble video,
#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble iframe {
    max-width: 100%;
    width: auto;
    height: auto;
    border-radius: 12px;
    display: block;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble pre,
#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble code {
    max-width: 100%;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace !important;
    font-size: 0.9em !important;
    color: inherit !important;
    background: rgba(15, 23, 42, 0.06);
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-meta {
    margin-top: 8px;
    font-family: var(--aicafb-font) !important;
    font-size: 14px !important;
    line-height: 1.2 !important;
    font-weight: 400 !important;
    color: var(--aicafb-placeholder) !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-meta {
    text-align: right;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant .aicafb-msg-meta {
    text-align: left;
}

/* Typing */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing .aicafb-msg-bubble {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    width: fit-content;
    max-width: none;
    background: var(--aicafb-panel) !important;
    color: var(--aicafb-text) !important;
    border-radius: var(--aicafb-radius-md);
    padding: 16px 20px;
    box-shadow: 0 4px 14px rgba(15, 23, 42, 0.05);
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-label {
    color: var(--aicafb-muted) !important;
    font-size: 14px !important;
    font-weight: 500 !important;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #bfdbfe;
    animation: aicafb-typing-pulse 1.3s infinite ease-in-out;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots span:nth-child(1),
#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots span:nth-child(2) {
    background: var(--aicafb-primary);
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes aicafb-typing-pulse {
    0%,
    80%,
    100% {
        transform: scale(0.72);
        opacity: 0.45;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Input */
#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-input-area {
    flex: 0 0 auto;
    margin-top: auto;
    padding: 18px 24px 12px;
    background: var(--aicafb-bg) !important;
    border-top: 1px solid var(--aicafb-divider);
    position: sticky;
    bottom: 0;
    z-index: 2;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-input-shell {
    height: auto;
    border-radius: 999px;
    border: 1px solid #d7e0ea;
    background: var(--aicafb-panel) !important;
    display: flex;
    align-items: center;
    padding: 10px;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

#aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-input-shell:focus-within {
    border-color: #93c5fd;
    box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.08);
}

#aicafb-chat-portal.aicafb-chat-portal input#ai-chat-input.aicafb-chat-input {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0 !important;
    padding: 0 10px !important;
    border: none !important;
    border-radius: 0 !important;
    outline: none !important;
    box-shadow: none !important;
    background: transparent !important;
    font-family: var(--aicafb-font) !important;
    font-size: 16px !important;
    line-height: 1.4 !important;
    color: var(--aicafb-text) !important;
    height: auto !important;
    min-height: 0 !important;
}

#aicafb-chat-portal.aicafb-chat-portal input#ai-chat-input::placeholder {
    color: var(--aicafb-placeholder) !important;
    opacity: 1;
}

#aicafb-chat-portal.aicafb-chat-portal button#ai-chat-send.aicafb-chat-send {
    width: 40px;
    height: 40px;
    margin: 0 !important;
    padding: 0 !important;
    border: none !important;
    border-radius: 50% !important;
    background: var(--aicafb-primary) !important;
    color: #fff !important;
    cursor: pointer;
    box-shadow: var(--aicafb-shadow-button);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font: inherit;
    line-height: 0;
    transition: background-color 0.2s ease, transform 0.2s ease;
}

#aicafb-chat-portal.aicafb-chat-portal button#ai-chat-send:hover {
    background: var(--aicafb-primary-hover) !important;
    color: #fff !important;
    transform: scale(1.03);
}

#aicafb-chat-portal.aicafb-chat-portal button#ai-chat-send:focus-visible,
#aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher:focus-visible {
    outline: 2px solid var(--aicafb-primary);
    outline-offset: 2px;
}

#aicafb-chat-portal.aicafb-chat-portal p.aicafb-chat-footer {
    margin: 14px 0 6px !important;
    padding: 0 !important;
    text-align: center;
    font-family: var(--aicafb-font) !important;
    color: var(--aicafb-muted) !important;
    font-size: 12px !important;
    font-weight: 500 !important;
    line-height: 1.2 !important;
    background: transparent !important;
    border: 0;
}

#aicafb-chat-portal.aicafb-chat-portal p.aicafb-chat-footer span {
    font-size: 12px;
    color: inherit;
}

@media (max-width: 1024px) {
    #aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher {
        right: 16px;
        bottom: 16px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget {
        right: 16px;
        bottom: 16px;
        width: min(380px, calc(100vw - 32px));
        height: auto;
        max-height: min(600px, calc(100dvh - 32px));
    }
}

@media (max-width: 767px) {
    #aicafb-chat-portal.aicafb-chat-portal button.aicafb-chat-launcher {
        right: 12px;
        bottom: calc(12px + env(safe-area-inset-bottom, 0px));
        width: 54px;
        height: 54px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget {
        right: 0;
        bottom: 0;
        width: 90vw;
        height: 90dvh;
        max-height: 90dvh;
        border-radius: 24px;
        border-left: none;
        border-right: none;
        border-bottom: none;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized {
        width: calc(100vw - 24px);
        left: 12px;
        right: 12px;
        bottom: 12px;
        top: auto;
        height: 82px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header {
        padding: 18px 16px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-header-left {
        gap: 12px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-bot-mark {
        width: 48px;
        height: 48px;
    }

    #aicafb-chat-portal.aicafb-chat-portal h2.aicafb-chat-title {
        font-size: 18px !important;
        max-width: 180px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-messages {
        padding: 16px;
        gap: 14px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant .aicafb-msg-bubble {
        max-width: 86%;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-bubble {
        max-width: 78%;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-input-area {
        padding: 12px 12px calc(8px + env(safe-area-inset-bottom, 0px));
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-input-shell {
        padding: 5px;
    }

    #aicafb-chat-portal.aicafb-chat-portal button#ai-chat-send.aicafb-chat-send {
        width: 46px;
        height: 46px;
    }

    #aicafb-chat-portal.aicafb-chat-portal input#ai-chat-input.aicafb-chat-input {
        font-size: 15px !important;
    }
}

@media (max-width: 1440px) {
    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget:not(.is-minimized) {
        bottom: 0;
        height: 550px;
        max-height: min(550px, calc(100dvh - 24px));
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-bot-mark {
        width: 52px;
        height: 52px;
    }
}

@media (max-width: 767.98px) {
    body.admin-bar #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget:not(.is-minimized) {
        top: 46px;
        bottom: auto;
        height: calc(100dvh - 46px);
        max-height: calc(100dvh - 46px);
    }
}

@media (max-width: 320px) {
    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget.is-minimized .aicafb-chat-bot-mark {
        width: 44px;
        height: 44px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-bot-mark {
        width: 44px;
        height: 44px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-svg {
        width: 24px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-chat-widget:not(.is-minimized) {
        height: 450px;
        max-height: min(450px, calc(100dvh - 16px));
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-bubble p {
        font-size: 14px !important;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--assistant .aicafb-msg-bubble {
        padding: 10px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-row--user .aicafb-msg-bubble {
        padding: 12px;
    }

    #aicafb-chat-portal.aicafb-chat-portal .aicafb-msg-meta {
        font-size: 12px !important;
    }
}
