/* AI sales chat widget
   Configured from the Sales IQ module in the admin panel, rendered by
   php/includes/ai-chat-widget.php.

   Deliberately self-contained and prefixed: this loads on top of whatever theme the site is
   running, so nothing here styles an element the theme also owns. */

.aduvio-ai-chat {
    position: fixed;
    /* env() past the notch and the home indicator - a phone in landscape puts its rounded corner
       exactly where the launcher sits, and the safe area is 0 on everything that has neither.
       The plain pair first, for a browser that does not know env() and drops the line after it */
    right: 20px;
    bottom: 20px;
    right: calc(20px + env(safe-area-inset-right));
    bottom: calc(20px + env(safe-area-inset-bottom));
    z-index: 9998;
    font-size: 14px;
    line-height: 1.4;
}

/* Launcher */

.aduvio-ai-chat-launcher {
    position: relative;
    width: 56px;
    height: 56px;
    border: none;
    border-radius: 50%;
    background: #2b3643;
    color: #fff;
    font-size: 22px;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0,0,0,0.25);
    transition: transform 0.15s ease;
}

.aduvio-ai-chat-launcher:hover {
    transform: scale(1.05);
}

/* A tap on a phone leaves a grey box behind the round button for a moment, and :hover sticks on
   the launcher after the tap in some mobile browsers - neither is wanted on a floating circle */
.aduvio-ai-chat-launcher,
.aduvio-ai-chat-close,
.aduvio-ai-chat-send {
    -webkit-tap-highlight-color: rgba(0,0,0,0);
    touch-action: manipulation;
}

@media (hover: none) {

    .aduvio-ai-chat-launcher:hover {
        transform: none;
    }
}

.aduvio-ai-chat.is-open .aduvio-ai-chat-launcher {
    display: none;
}

/* Unread badge - the waiting greeting.

   Its own element rather than a ::after on the launcher so the count can be read by a screen
   reader, and so the JS has something to remove once the panel has been opened. */

.aduvio-ai-chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    box-sizing: border-box;
    border-radius: 10px;
    background: #d9534f;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    line-height: 20px;
    text-align: center;
    box-shadow: 0 0 0 2px #fff;
}

/* Opened once, and the greeting is no longer unread - for the rest of the visit */

.aduvio-ai-chat.is-seen .aduvio-ai-chat-badge {
    display: none;
}

/* Panel */

.aduvio-ai-chat-panel {
    display: none;
    width: 340px;
    max-width: calc(100vw - 40px);
    height: 460px;
    max-height: calc(100vh - 40px);
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.25);
    overflow: hidden;
}

.aduvio-ai-chat.is-open .aduvio-ai-chat-panel {
    display: flex;
    flex-direction: column;
}

.aduvio-ai-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 14px;
    background: #2b3643;
    color: #fff;
    font-weight: 600;
    flex: 0 0 auto;
}

/* Padding pulled back out with a negative margin: the button is a 16px cross that has to be a
   target a thumb can hit, without the header growing around it */
.aduvio-ai-chat-close {
    border: none;
    background: none;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    padding: 11px;
    margin: -11px -11px -11px 0;
    line-height: 1;
}

/* Messages */

/* overscroll-behavior: reaching the top or the bottom of the thread must not hand the scroll on to
   the page behind it, which on a phone is the whole site moving under a full screen panel */
.aduvio-ai-chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 14px;
    background: #f5f6f8;
}

.aduvio-ai-chat-message {
    max-width: 85%;
    margin-bottom: 10px;
    padding: 9px 12px;
    border-radius: 12px;
    /* Model replies arrive as plain text with real newlines in them - kept, not collapsed */
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.aduvio-ai-chat-message-assistant {
    background: #fff;
    color: #333;
    border-bottom-left-radius: 3px;
}

.aduvio-ai-chat-message-visitor {
    background: #2b3643;
    color: #fff;
    margin-left: auto;
    border-bottom-right-radius: 3px;
}

/* Links the assistant hands back - a vehicle page, a service page.
   Underlined rather than colour-only: the assistant bubble is white and the visitor bubble is dark,
   so an underline is the one cue that reads on both. */

.aduvio-ai-chat-message a {
    text-decoration: underline;
    word-break: break-word;
}

.aduvio-ai-chat-message-assistant a {
    color: #1b5faa;
}

.aduvio-ai-chat-message-visitor a {
    color: #fff;
}

.aduvio-ai-chat-message-error {
    background: #f8d7da;
    color: #842029;
}

.aduvio-ai-chat-message-thinking {
    background: #fff;
    color: #888;
    font-style: italic;
}

/* Footer */

.aduvio-ai-chat-footer {
    display: flex;
    align-items: flex-end;
    padding: 10px;
    background: #fff;
    border-top: 1px solid #e3e5e8;
    flex: 0 0 auto;
}

.aduvio-ai-chat-footer textarea {
    flex: 1 1 auto;
    resize: none;
    border: 1px solid #d5d8dc;
    border-radius: 6px;
    padding: 8px 10px;
    max-height: 110px;
    font-size: 14px;
    font-family: inherit;
    color: #333;
    background: #fff;
}

.aduvio-ai-chat-send {
    flex: 0 0 auto;
    margin-left: 8px;
    width: 38px;
    height: 38px;
    border: none;
    border-radius: 6px;
    background: #2b3643;
    color: #fff;
    cursor: pointer;
}

.aduvio-ai-chat-send[disabled] {
    opacity: 0.5;
    cursor: default;
}

/* The page behind a full screen panel does not scroll. Added to <html> and <body> by the JS while
   the panel is open, and only at the width where it is full screen - so it is never on a desktop */

html.aduvio-ai-chat-locked,
body.aduvio-ai-chat-locked {
    overflow: hidden;
}

/* Phones: the panel takes the screen rather than floating in a corner of it.

   767px rather than 480px - a 340 x 460 panel has nowhere to float on a phone held either way
   round, and every width in between was getting the desktop treatment on a screen too small
   for it. Mirrored by the breakpoint in js/common/ai-chat.js - keep the two in step. */

@media (max-width: 767px) {

    .aduvio-ai-chat.is-open {
        right: 0;
        left: 0;
        top: 0;
        /* A height rather than top + bottom, because the JS replaces it with the visual viewport's
           own height while the on screen keyboard is up. dvh is what it is the rest of the time:
           100vh on a phone is the viewport behind the browser's toolbars, not the one being
           looked at, and the box to type in is the part that falls off the bottom of it. */
        bottom: auto;
        height: 100%;
        height: 100dvh;
    }

    .aduvio-ai-chat.is-open .aduvio-ai-chat-panel {
        width: 100%;
        max-width: none;
        height: 100%;
        max-height: none;
        border-radius: 0;
    }

    /* Full screen means the panel owns the notch and the home indicator too */

    .aduvio-ai-chat.is-open .aduvio-ai-chat-header {
        padding-top: calc(12px + env(safe-area-inset-top));
    }

    .aduvio-ai-chat.is-open .aduvio-ai-chat-footer {
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
    }

    /* Anything under 16px and Safari zooms the whole page in the moment the box takes focus - and
       leaves it zoomed after the keyboard goes away */
    .aduvio-ai-chat-footer textarea {
        font-size: 16px;
        max-height: 96px;
    }

    /* Thumb sized, on a screen with no pointer to aim with */
    .aduvio-ai-chat-send {
        width: 44px;
        height: 44px;
    }

    .aduvio-ai-chat-close {
        font-size: 20px;
    }

    .aduvio-ai-chat-message {
        max-width: 90%;
    }
}

/* The theme's back to top button is bottom right as well, and on a narrow screen it is a 100px
   wide strip in exactly the corner the launcher wants - the launcher is drawn over it and takes
   the taps meant for it. Lift the widget clear of it, on the sites that have it turned on.

   :has() is what makes this possible without the two files knowing about each other; a browser
   without it lands back on the overlap, which is where every browser was before. */

@media (max-width: 960px) {

    body:has(#back-top) .aduvio-ai-chat:not(.is-open) {
        bottom: 72px;
        bottom: calc(72px + env(safe-area-inset-bottom));
    }
}
