/* =========================================================
   RHAI CHATBOT
   Modern, isolated, responsive UI
   ========================================================= */

#rhai-root {
    --rhai-primary: #2563eb;
    --rhai-primary-dark: #1d4ed8;
    --rhai-accent: #f4f7ff;
    --rhai-text: #172033;
    --rhai-muted: #718096;
    --rhai-border: #e5eaf1;
    --rhai-radius: 18px;
    --rhai-shadow:
        0 20px 60px rgba(15, 23, 42, .18),
        0 5px 18px rgba(15, 23, 42, .08);

    position: fixed;
    right: 20px;
    bottom: 20px;

    z-index: 99999;

    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        Roboto,
        Helvetica,
        Arial,
        sans-serif;

    font-size: 14px;
    line-height: 1.4;

    isolation: isolate;
}

/* Left position */
#rhai-root.rhai-side-left {
    left: 20px;
    right: auto;
}


/* =========================================================
   TOGGLE BUTTON
   ========================================================= */

#rhai-toggle {
    position: relative;

    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;

    gap: 9px;

    width: 60px;
    height: 60px;

    min-width: 60px;
    min-height: 60px;

    padding: 0 !important;
    margin: 0 !important;

    border: 0 !important;
    outline: none !important;

    border-radius: 50%;

    background: var(--rhai-primary) !important;
    color: #fff !important;

    cursor: pointer;

    box-shadow:
        0 8px 25px rgba(37, 99, 235, .28),
        0 3px 10px rgba(0, 0, 0, .12);

    transition:
        transform .2s ease,
        box-shadow .2s ease,
        background .2s ease;

    -webkit-tap-highlight-color: transparent;
}

/* Hover */
#rhai-toggle:hover {
    transform: translateY(-2px) scale(1.03);

    background: var(--rhai-primary-dark) !important;

    box-shadow:
        0 12px 30px rgba(37, 99, 235, .35),
        0 5px 15px rgba(0, 0, 0, .12);
}

/* Click */
#rhai-toggle:active {
    transform: scale(.96);
}


/* =========================================================
   TOGGLE ICON
   ========================================================= */

.rhai-toggle-icon {
    display: inline-flex !important;

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

    width: 26px;
    height: 26px;

    flex: 0 0 26px;

    font-size: 24px !important;
    line-height: 1 !important;

    color: #fff !important;
}


/* =========================================================
   TOGGLE LABEL
   ========================================================= */

/*
   IMPORTANT:
   This selector works whether the class is added to the
   button by JavaScript or not, provided the label element
   itself has class="rhai-toggle-label".
*/

#rhai-toggle .rhai-toggle-label,
.rhai-toggle-label {
    display: inline-flex !important;
    align-items: center;

    max-width: 180px;

    margin: 0 !important;
    padding: 0 !important;

    color: #fff !important;

    font-size: 14px !important;
    font-weight: 600 !important;
    line-height: 1 !important;

    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

    opacity: 1 !important;
    visibility: visible !important;

    flex: 0 1 auto;
}


/*
   Button WITH label

   Do NOT rely only on JavaScript to add the class.
   The :has() rule catches the label automatically
   in modern browsers.
*/

#rhai-toggle.rhai-toggle-with-label,
#rhai-toggle:has(.rhai-toggle-label) {

    width: auto !important;
    min-width: 60px;

    height: 52px;

    padding: 0 18px !important;

    border-radius: 28px !important;

    gap: 9px;
}


/* Slightly smaller icon when label is visible */

#rhai-toggle.rhai-toggle-with-label .rhai-toggle-icon,
#rhai-toggle:has(.rhai-toggle-label) .rhai-toggle-icon {
    font-size: 21px !important;
}


/* =========================================================
   PANEL
   ========================================================= */

#rhai-panel {
    position: absolute;

    right: 0;
    bottom: 74px;

    display: flex !important;
    flex-direction: column !important;

    width: 380px;
    max-width: calc(100vw - 32px);

    height: 570px;
    max-height: calc(100vh - 110px);

    overflow: hidden;

    background: #fff;

    border: 1px solid rgba(226, 232, 240, .9);

    border-radius: var(--rhai-radius);

    box-shadow: var(--rhai-shadow);

    transform-origin: bottom right;

    animation: rhai-panel-in .2s ease-out;

    isolation: isolate;
}


/* Left-side panel */

#rhai-root.rhai-side-left #rhai-panel {
    left: 0;
    right: auto;

    transform-origin: bottom left;
}


/* Hide */

#rhai-panel.rhai-hidden,
#rhai-toggle.rhai-hidden {
    display: none !important;
}


/* Panel animation */

@keyframes rhai-panel-in {
    from {
        opacity: 0;
        transform: translateY(12px) scale(.97);
    }

    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}


/* =========================================================
   HEADER
   ========================================================= */

.rhai-header {
    position: relative;

    display: flex !important;
    align-items: center !important;

    gap: 11px;

    min-height: 64px;

    padding: 12px 14px;

    background:
        linear-gradient(
            135deg,
            var(--rhai-primary),
            #3b82f6
        ) !important;

    color: #fff !important;
}


/* Header avatar */

.rhai-avatar {
    display: flex !important;

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

    width: 38px;
    height: 38px;

    flex: 0 0 38px;

    border-radius: 50%;

    background: rgba(255, 255, 255, .18);

    font-size: 19px;
}


/* Title */

.rhai-title {
    flex: 1 1 auto;

    min-width: 0;

    color: #fff !important;

    font-size: 15px !important;
    font-weight: 700 !important;

    line-height: 1.2;
}


/* Optional status */

.rhai-title::after {
    content: "Online";

    display: block;

    margin-top: 3px;

    font-size: 11px;
    font-weight: 400;

    opacity: .8;
}


/* Close */

#rhai-close {
    display: inline-flex !important;

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

    width: 34px;
    height: 34px;

    padding: 0 !important;
    margin: 0 !important;

    border: 0 !important;

    border-radius: 50%;

    background: transparent !important;

    color: #fff !important;

    font-size: 22px !important;
    line-height: 1 !important;

    cursor: pointer;

    opacity: .85;

    transition:
        background .15s ease,
        opacity .15s ease;
}

#rhai-close:hover {
    background: rgba(255, 255, 255, .14) !important;
    opacity: 1;
}


/* =========================================================
   MESSAGES
   ========================================================= */

.rhai-messages {
    flex: 1 1 auto;

    min-height: 0;

    overflow-y: auto;

    display: flex;
    flex-direction: column;

    gap: 9px;

    padding: 16px;

    background:
        radial-gradient(
            circle at top left,
            rgba(37, 99, 235, .06),
            transparent 45%
        ),
        var(--rhai-accent);

    scrollbar-width: thin;
    scrollbar-color: #cbd5e1 transparent;
}


/* Chrome scrollbar */

.rhai-messages::-webkit-scrollbar {
    width: 5px;
}

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

.rhai-messages::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}


/* =========================================================
   MESSAGE BUBBLES
   ========================================================= */

.rhai-msg {
    position: relative;

    max-width: 82%;

    padding: 10px 13px;

    border-radius: 16px;

    font-size: 13.5px;
    line-height: 1.5;

    white-space: pre-wrap;

    word-break: break-word;
    overflow-wrap: anywhere;

    animation: rhai-message-in .18s ease-out;
}


@keyframes rhai-message-in {
    from {
        opacity: 0;
        transform: translateY(4px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}


/* User */

.rhai-msg-user {
    align-self: flex-end;

    color: #fff !important;

    background:
        linear-gradient(
            135deg,
            var(--rhai-primary),
            #3b82f6
        ) !important;

    border-bottom-right-radius: 5px;

    box-shadow:
        0 2px 5px rgba(37, 99, 235, .15);
}


/* Assistant */

.rhai-msg-assistant {
    align-self: flex-start;

    color: var(--rhai-text) !important;

    background: #fff !important;

    border: 1px solid #edf0f4;

    border-bottom-left-radius: 5px;

    box-shadow:
        0 2px 5px rgba(15, 23, 42, .05);
}


/* Admin */

.rhai-msg-admin {
    align-self: flex-start;

    color: var(--rhai-text) !important;

    background: #fffaf0 !important;

    border: 1px solid #f8df9a;

    border-bottom-left-radius: 5px;
}


/* Admin label */

.rhai-msg-admin::before {
    content: "Support agent";

    display: block;

    margin-bottom: 5px;

    color: #a16207;

    font-size: 10px;
    font-weight: 700;

    text-transform: uppercase;

    letter-spacing: .06em;
}


/* =========================================================
   TYPING INDICATOR
   ========================================================= */

.rhai-typing {
    min-height: 25px;

    padding: 4px 16px 8px;

    color: var(--rhai-muted);

    background: var(--rhai-accent);

    font-size: 12px;
}


/* Optional typing dots */

.rhai-typing::after {
    content: "•••";

    margin-left: 5px;

    letter-spacing: 2px;

    animation: rhai-dots 1.2s infinite;
}

@keyframes rhai-dots {
    0%, 100% {
        opacity: .25;
    }

    50% {
        opacity: 1;
    }
}


/* =========================================================
   ATTACHMENT PREVIEW
   ========================================================= */

.rhai-preview-chip {
    display: flex !important;

    align-items: center;

    gap: 9px;

    margin: 0 10px 8px;

    padding: 8px 10px;

    background: #f6f8fb !important;

    border: 1px solid #e8edf3;

    border-radius: 10px;

    color: var(--rhai-text);

    font-size: 12px;
}

.rhai-preview-chip img {
    width: 36px;
    height: 36px;

    object-fit: cover;

    border-radius: 7px;
}

.rhai-preview-chip button {
    margin-left: auto;

    border: 0 !important;
    background: transparent !important;

    color: #b42318 !important;

    cursor: pointer;
}
.rhai-preview-file-icon { font-size: 20px; }
.rhai-file-chip {
    display: inline-flex; align-items: center; gap: 6px; margin-top: 6px; padding: 6px 10px;
    background: rgba(0,0,0,.06); border-radius: 8px; font-size: 12.5px; text-decoration: none; color: inherit;
}
.rhai-file-chip:hover { background: rgba(0,0,0,.1); text-decoration: none; }
.rhai-msg-user .rhai-file-chip { background: rgba(255,255,255,.25); color: #fff; }


/* =========================================================
   INPUT AREA
   ========================================================= */

.rhai-input-row {
    display: flex !important;

    align-items: center !important;

    gap: 7px;

    padding: 10px;

    background: #fff !important;

    border-top: 1px solid var(--rhai-border);

    flex-shrink: 0;
}


/* Input */

#rhai-input {
    display: block !important;

    flex: 1 1 auto;

    min-width: 0;

    height: 40px;

    padding: 9px 14px !important;

    margin: 0 !important;

    border: 1px solid #dce2e9 !important;

    border-radius: 21px !important;

    outline: none !important;

    background: #fff !important;

    color: #172033 !important;

    caret-color: #172033 !important;

    -webkit-text-fill-color: #172033 !important;

    font-family: inherit !important;

    font-size: 13.5px !important;

    line-height: 20px !important;

    box-shadow: none !important;

    appearance: none;
}

#rhai-input:focus {
    border-color: var(--rhai-primary) !important;

    box-shadow:
        0 0 0 3px rgba(37, 99, 235, .09) !important;
}

#rhai-input::placeholder {
    color: #98a2b3 !important;
    opacity: 1 !important;
}


/* =========================================================
   ATTACH BUTTON
   ========================================================= */

#rhai-attach {
    display: inline-flex !important;

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

    width: 34px;
    height: 34px;

    flex: 0 0 34px;

    padding: 0 !important;
    margin: 0 !important;

    border: 0 !important;

    border-radius: 50%;

    background: transparent !important;

    color: #718096 !important;

    font-size: 18px;

    cursor: pointer;

    transition:
        color .15s ease,
        background .15s ease;
}

#rhai-attach:hover {
    color: var(--rhai-primary) !important;

    background: #f1f5ff !important;
}


/* =========================================================
   SEND BUTTON
   ========================================================= */

#rhai-send {
    display: inline-flex !important;

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

    width: 40px;
    height: 40px;

    min-width: 40px;

    flex: 0 0 40px;

    padding: 0 !important;
    margin: 0 !important;

    border: 0 !important;

    border-radius: 50% !important;

    background: var(--rhai-primary) !important;

    color: #fff !important;

    font-size: 15px;

    cursor: pointer;

    box-shadow:
        0 3px 8px rgba(37, 99, 235, .22);

    transition:
        transform .15s ease,
        background .15s ease;
}

#rhai-send:hover {
    background: var(--rhai-primary-dark) !important;

    transform: scale(1.04);
}

#rhai-send:active {
    transform: scale(.95);
}


/* =========================================================
   ATTACHMENTS INSIDE MESSAGES
   ========================================================= */

.rhai-msg img.rhai-attachment {
    display: block;

    max-width: 100%;
    max-height: 240px;

    margin-top: 7px;

    border-radius: 10px;

    object-fit: cover;

    cursor: zoom-in;
}


/* =========================================================
   LIGHTBOX
   ========================================================= */

.rhai-lightbox_OLD {
    position: fixed;

    inset: 0;

    display: flex;

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

    padding: 25px;

    background: rgba(15, 18, 28, .88);

    z-index: 999999;

    cursor: zoom-out;

    animation: rhai-fade-in .15s ease-out;
}

.rhai-lightbox {
    position: fixed;
    inset: 0;

    background: rgba(15, 18, 28, .85);

    z-index: 999999;

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

    cursor: zoom-out;
    padding: 30px;

    box-sizing: border-box;
}

.rhai-lightbox.rhai-hidden {
    display: none !important;
}

@keyframes rhai-fade-in {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.rhai-lightbox img {
    display: block;

    max-width: 92vw;
    max-height: 90vh;

    border-radius: 12px;

    box-shadow:
        0 20px 70px rgba(0, 0, 0, .45);
}


/* =========================================================
   MOBILE
   ========================================================= */

@media (max-width: 600px) {

    #rhai-root {
        right: 14px;
        bottom: 14px;
        left: auto;
    }

    #rhai-root.rhai-side-left {
        left: 14px;
        right: auto;
    }


    /* Chat button */

    #rhai-toggle {
        width: 56px;
        height: 56px;

        min-width: 56px;
        min-height: 56px;
    }


    /* Label button */

    #rhai-toggle.rhai-toggle-with-label,
    #rhai-toggle:has(.rhai-toggle-label) {
        height: 50px;

        padding: 0 16px !important;
    }


    /* Chat panel */

    #rhai-panel,
    #rhai-root.rhai-side-left #rhai-panel {

        position: fixed;

        left: 12px;
        right: 12px;

        bottom: 82px;

        width: auto;
        max-width: none;

        height: min(620px, calc(100vh - 105px));

        max-height: calc(100vh - 105px);

        border-radius: 18px;
    }


    .rhai-messages {
        padding: 13px;
    }


    .rhai-msg {
        max-width: 88%;
    }
}


/* Very small screens */

@media (max-width: 380px) {

    #rhai-panel,
    #rhai-root.rhai-side-left #rhai-panel {
        left: 7px;
        right: 7px;

        bottom: 76px;

        height: calc(100vh - 92px);

        border-radius: 15px;
    }

    #rhai-root {
        right: 10px;
        bottom: 10px;
    }

    #rhai-root.rhai-side-left {
        left: 10px;
    }
}


/* =========================================================
   ACCESSIBILITY
   ========================================================= */

#rhai-toggle:focus-visible,
#rhai-close:focus-visible,
#rhai-send:focus-visible,
#rhai-attach:focus-visible {
    outline: 3px solid rgba(37, 99, 235, .35) !important;
    outline-offset: 2px;
}


/* =========================================================
   REDUCED MOTION
   ========================================================= */

@media (prefers-reduced-motion: reduce) {

    #rhai-toggle,
    #rhai-send,
    #rhai-panel,
    .rhai-msg,
    .rhai-lightbox {
        animation: none !important;
        transition: none !important;
    }
}