/* ============================================================
   Bridge_AIChat — Chat Widget Styles
   Full-height sidebar docked to the right edge of the viewport.
   ============================================================ */

:root {
    /* Default panel width. Overwritten at runtime as an inline custom property
       on <html> by setupPanelResize() in chat-widget.js — both the panel and
       the page's push margin read from this one variable, so they can never
       drift apart while the curtain is being dragged. */
    --aichat-panel-width: 380px;

    /* ── Tenant theme ────────────────────────────────────────
       Every value below is overwritten at runtime by
       applyTenantTheme() in chat-widget.js from the tenant's
       Appearance settings (GET /tenants/{id}/theme/public).

       The defaults here MUST byte-match _DEFAULT_THEME_MAGENTO in
       ecom-chat-api/app/services/tenant_service.py. They paint the
       first frame, before the theme fetch resolves — and since the
       backend fills every unset field with exactly these values, a
       tenant who has never opened Appearance sees no swap at all.
       Drift between the two files shows up as a visible flicker on
       every page load, not as a broken build. */
    --aichat-header-bg: #1979C3;
    --aichat-header-text: #FFFFFF;
    --aichat-launcher-bg: #1979C3;
    --aichat-msg-user-bg: #1979C3;
    --aichat-msg-user-text: #FFFFFF;
    --aichat-msg-bot-bg: #F0F0F0;
    --aichat-msg-bot-text: #333333;
    --aichat-input-bg: #FFFFFF;
    --aichat-input-border: #CCCCCC;
    --aichat-input-placeholder: #757575;
    --aichat-button-bg: #1979C3;
    --aichat-button-hover-bg: #006BB4;
    --aichat-footer-text: #757575;
}

/* ── Page push ───────────────────────────────────────────────
   Instead of the panel overlaying the page, the page itself
   shifts left by the panel's width so nothing sits hidden
   behind the chat — the whole site stays visible, just shrunk
   over to make room on the right.

   Narrow viewports get no push at all: see the overlay
   breakpoint at the bottom of this file.
   ──────────────────────────────────────────────────────────── */
html.aichat-open,
html.aichat-open body {
    overflow-x: hidden;
}

html.aichat-open .page-wrapper {
    margin-right: var(--aichat-panel-width);
}

.page-wrapper {
    transition: margin-right 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

/* ── Keyframes ───────────────────────────────────────────── */
@keyframes aichat-dot-bounce {

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

    30% {
        transform: translateY(-6px);
    }
}

/* ── Launcher Button ─────────────────────────────────────── */
#bridge-aichat-launcher {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;

    display: flex;
    align-items: center;
    justify-content: center;

    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    /* A <button>'s UA padding is inherited here and eats into the content box,
       which the built-in 28px glyph never noticed but a tenant's uploaded icon
       does — it is sized to fill, and came out 48x58 inside a 60x60 launcher. */
    padding: 0;
    cursor: pointer;

    background: var(--aichat-launcher-bg);
    color: #ffffff;

    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

#bridge-aichat-launcher:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 28px rgba(0, 0, 0, 0.3);
}

#bridge-aichat-launcher:focus-visible {
    outline: 3px solid var(--aichat-launcher-bg);
    outline-offset: 3px;
}

/* Hidden while the panel is open — the header close button (and Escape)
   handle closing, so the launcher no longer needs to sit on top of the
   input area's send button. */
#bridge-aichat-launcher.is-hidden {
    opacity: 0;
    pointer-events: none;
    transform: scale(0.8);
}

/* Dimmed while the session is being initialized on first open */
#bridge-aichat-launcher.is-loading {
    opacity: 0.6;
    pointer-events: none;
}

/* ── Chat Window: full-height sidebar, docked right ───────── */
#bridge-aichat-window {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 9998;

    width: var(--aichat-panel-width);
    max-width: 85vw;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    overflow: hidden;

    display: flex;
    flex-direction: column;

    background: #ffffff;
    box-shadow: -5px 0 0px rgba(0, 0, 0, 0.1);

    transform: translateX(100%);
    visibility: hidden;
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), visibility 0s linear 0.3s;
}

#bridge-aichat-window.is-open {
    transform: translateX(0);
    visibility: visible;
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

/* ── Drag-to-resize ──────────────────────────────────────────
   The curtain is dragged from its left edge. Both the panel and
   the page follow --aichat-panel-width, so the two move as one.
   ──────────────────────────────────────────────────────────── */

/* The drag only feels attached to the cursor with the page-push
   transition off for its duration — otherwise the page trails
   0.3s behind the edge being dragged. */
html.aichat-resizing .page-wrapper,
html.aichat-resizing #bridge-aichat-window {
    transition: none !important;
}

html.aichat-resizing {
    cursor: col-resize;
    user-select: none;
    -webkit-user-select: none;
}

/* Hit area is deliberately wider than the visible line so the edge
   is grabbable without pixel-hunting. */
.aichat-resize-handle {
    position: absolute;
    left: 0;
    top: 0;
    width: 10px;
    height: 100%;
    z-index: 3;
    cursor: col-resize;
    background: transparent;
    touch-action: none;
}

.aichat-resize-handle::after {
    content: '';
    position: absolute;
    left: 3px;
    top: 0;
    width: 2px;
    height: 100%;
    background: var(--aichat-button-bg);
    opacity: 0;
    transition: opacity 0.15s ease;
}

.aichat-resize-handle:hover::after,
.aichat-resize-handle:focus-visible::after,
html.aichat-resizing .aichat-resize-handle::after {
    opacity: 1;
}

.aichat-resize-handle:focus-visible {
    outline: none;
}

.aichat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px 16px;
    background: var(--aichat-header-bg);
    color: var(--aichat-header-text);
    flex-shrink: 0;
    border-bottom: 1px solid rgba(30, 41, 59, 0.08);
}

.aichat-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.aichat-header-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    /* Tinted from the header's own text color rather than given a color of its
       own. A fixed backing (or the launcher color, which the stock Magento
       palette sets to the same blue as the header) would vanish into the
       header for some tenants; a wash of the text color reads on a light and a
       dark header alike, whatever the tenant picked. */
    background: rgba(255, 255, 255, 0.18);
    background: color-mix(in srgb, var(--aichat-header-text) 22%, transparent);
    color: var(--aichat-header-text);
    flex-shrink: 0;
    overflow: hidden;
}

.aichat-header-title {
    color: var(--aichat-header-text);
    font-size: 18px;
    font-weight: 600;
    font-family: 'Lexend Deca', sans-serif;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
}

.aichat-header-subtitle {
    color: var(--aichat-header-text);
    opacity: 0.72;
    font-size: 12px;
    font-weight: 400;
    font-family: 'Lexend', sans-serif;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
    margin-top: 2px;
}

.aichat-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.aichat-header-btn {
    background: rgba(255, 255, 255, 0.12);
    background: color-mix(in srgb, var(--aichat-header-text) 10%, transparent);
    border: none;
    color: var(--aichat-header-text);
    cursor: pointer;
    padding: 9px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    line-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.aichat-header-btn:hover {
    background: rgba(255, 255, 255, 0.24);
    background: color-mix(in srgb, var(--aichat-header-text) 20%, transparent);
    color: var(--aichat-header-text);
}

.aichat-header-btn:focus-visible {
    outline: 2px solid var(--aichat-header-text);
    outline-offset: 2px;
}

/* ── Messages Area ───────────────────────────────────────── */
.aichat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background: #ffffff;
}

.aichat-messages::-webkit-scrollbar {
    width: 4px;
}

.aichat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.aichat-messages::-webkit-scrollbar-thumb {
    background: rgba(30, 41, 59, 0.15);
    border-radius: 4px;
}

/* ── Message Bubbles ─────────────────────────────────────── */
.aichat-msg {
    max-width: 82%;
    padding: 11px 15px;
    font-size: 14px;
    line-height: 1.5;
    font-family: 'Lexend', sans-serif;
    word-break: break-word;
}

.aichat-msg-bot {
    align-self: flex-start;
    background: var(--aichat-msg-bot-bg);
    color: var(--aichat-msg-bot-text);
    border-radius: 4px 14px 14px 14px;
}

.aichat-msg-user {
    align-self: flex-end;
    background: var(--aichat-msg-user-bg);
    color: var(--aichat-msg-user-text);
    border-radius: 14px 4px 14px 14px;
}

/* ── Markdown inside a bubble ─────────────────────────────
   Bot replies are rendered from Markdown, so a bubble can hold block
   elements. First/last child margins are zeroed so a one-paragraph reply
   looks exactly like a plain-text bubble. */
.aichat-msg > *:first-child { margin-top: 0; }
.aichat-msg > *:last-child { margin-bottom: 0; }
.aichat-msg p { margin: 0 0 8px; }
.aichat-msg ul,
.aichat-msg ol { margin: 6px 0 8px; padding-left: 20px; }
.aichat-msg li { margin: 3px 0; }
.aichat-msg li > ul,
.aichat-msg li > ol { margin: 3px 0; }
.aichat-msg h1,
.aichat-msg h2,
.aichat-msg h3,
.aichat-msg h4,
.aichat-msg h5,
.aichat-msg h6 {
    margin: 10px 0 6px;
    font-weight: 600;
    line-height: 1.35;
}
.aichat-msg h1 { font-size: 16px; }
.aichat-msg h2 { font-size: 15px; }
.aichat-msg h3,
.aichat-msg h4,
.aichat-msg h5,
.aichat-msg h6 { font-size: 14px; }
.aichat-msg strong { font-weight: 600; }
.aichat-msg a {
    color: var(--aichat-button-bg);
    text-decoration: underline;
    word-break: break-word;
}
/* Inside a user bubble the CTA color is the background, so a link painted with
   it would be invisible — inherit the bubble's own text color instead. */
.aichat-msg-user a { color: var(--aichat-msg-user-text); }
.aichat-msg code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12.5px;
    background: rgba(15, 23, 42, 0.08);
    padding: 1px 5px;
    border-radius: 4px;
}
.aichat-msg-user code { background: rgba(255, 255, 255, 0.2); }
.aichat-md-pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: 10px 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 6px 0 8px;
}
.aichat-md-pre code {
    background: none;
    padding: 0;
    color: inherit;
    font-size: 12px;
}
.aichat-msg blockquote {
    margin: 6px 0 8px;
    padding-left: 10px;
    border-left: 3px solid #cbd5e1;
    color: #475569;
}
.aichat-msg-user blockquote {
    border-left-color: rgba(255, 255, 255, 0.45);
    color: #eff6ff;
}
.aichat-msg hr {
    border: none;
    border-top: 1px solid #e2e8f0;
    margin: 10px 0;
}

/* ── Typing Indicator ────────────────────────────────────── */
.aichat-typing {
    align-self: flex-start;
    background: var(--aichat-msg-bot-bg);
    border-radius: 4px 14px 14px 14px;
    padding: 13px 16px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.aichat-typing span {
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    /* The dots sit inside a bot bubble, so they follow its text color — a fixed
       gray disappears the moment a tenant picks a dark AI-message background. */
    background: var(--aichat-msg-bot-text);
    opacity: 0.55;
    animation: aichat-dot-bounce 1.2s infinite ease-in-out;
}

.aichat-typing span:nth-child(2) {
    animation-delay: 0.2s;
}

.aichat-typing span:nth-child(3) {
    animation-delay: 0.4s;
}

/* ── Input Area ──────────────────────────────────────────── */
.aichat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 14px 16px;
    border-top: 1px solid rgba(30, 41, 59, 0.08);
    background: #ffffff;
    flex-shrink: 0;
}

.aichat-textarea {
    flex: 1 1 auto;
    resize: none;
    /* Stated rather than inherited from the theme: autosizeTextarea() in
       chat-widget.js measures against this box model, and Magento's reset and
       a custom theme do not always agree on what a textarea's box is. */
    box-sizing: border-box;
    border: 1px solid var(--aichat-input-border);
    border-radius: 22px;
    padding: 11px 16px;
    font-size: 14px;
    font-family: 'Lexend', sans-serif;
    line-height: 1.4;
    max-height: 120px;
    /* Hidden, not auto: the box grows with its content, so up to max-height
       there is nothing to scroll and a scrollbar would only be the rounding
       error of a single pixel showing through. autosizeTextarea() switches
       this to auto once the box stops growing. */
    overflow-y: hidden;
    outline: none;
    color: var(--aichat-msg-bot-text);
    transition: border-color 0.15s, background 0.15s;
    background: var(--aichat-input-bg);
}

.aichat-textarea:focus {
    border-color: var(--aichat-button-bg);
    background: var(--aichat-input-bg);
}

.aichat-textarea::placeholder {
    color: var(--aichat-input-placeholder);
}

/* A full-height native scrollbar would run straight through the 22px pill
   corners. Kept thin and held off the curved ends by the track margin, so it
   sits inside the outline instead of cutting across it. */
.aichat-textarea {
    scrollbar-width: thin;
    scrollbar-color: rgba(30, 41, 59, 0.28) transparent;
}

.aichat-textarea::-webkit-scrollbar {
    width: 6px;
}

.aichat-textarea::-webkit-scrollbar-track {
    background: transparent;
    /* Clears the rounded ends — a square track would poke out of the curve. */
    margin: 14px 0;
}

.aichat-textarea::-webkit-scrollbar-thumb {
    background: rgba(30, 41, 59, 0.28);
    border-radius: 3px;
}

.aichat-textarea::-webkit-scrollbar-thumb:hover {
    background: rgba(30, 41, 59, 0.42);
}

.aichat-send-btn {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background: var(--aichat-button-bg);
    color: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, transform 0.15s;
    padding: 0;
}

.aichat-send-btn:hover:not(:disabled) {
    background: var(--aichat-button-hover-bg);
    transform: scale(1.06);
}

.aichat-send-btn:disabled {
    background: rgba(30, 41, 59, 0.15);
    cursor: not-allowed;
    transform: none;
}

/* ── Footer / branding ───────────────────────────────────── */
.aichat-footer {
    flex-shrink: 0;
    padding: 0 16px 12px;
    text-align: center;
}

.aichat-footer-branding {
    color: var(--aichat-footer-text);
    font-size: 11px;
    font-family: 'Lexend', sans-serif;
    line-height: 1.4;
}

/* A tenant who turns branding off in Appearance gets the [hidden] attribute
   set by applyTenantTheme(). Magento's own styles set display on a lot of
   elements, so [hidden] is restated here rather than left to the UA sheet. */
.aichat-footer-branding[hidden] {
    display: none;
}

/* ── Uploaded icons ──────────────────────────────────────────
   Swapped in by applyTenantTheme() for the built-in SVG glyph in the launcher
   and the header avatar. Sized to fill the round host either way, so a tenant's
   square logo does not change the widget's geometry. */
.aichat-custom-icon {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    border-radius: inherit;
}

#bridge-aichat-launcher .aichat-custom-icon {
    border-radius: 50%;
}

/* ── Responsive: overlay drawer on small screens ──────────────
   Below this width there is nothing meaningful left to push the
   page into, so the curtain covers it instead: full width, no
   page push, no drag handle. The threshold is MIN_PANEL_WIDTH +
   320 from chat-widget.js — keep the two in step. */
@media (max-width: 640px) {
    #bridge-aichat-window {
        width: 100vw;
        max-width: 100vw;
    }

    html.aichat-open .page-wrapper {
        margin-right: 0;
    }

    .aichat-resize-handle {
        display: none;
    }
}

@media (max-width: 480px) {
    #bridge-aichat-launcher {
        right: 16px;
        bottom: 16px;
    }
}
