/* Site assistant: floating launcher + chat panel (partials/assistant.php, js/assistant.js).
   Bottom-right, stacked above WhatsApp; back-to-top moves up above it.
   The launcher is the JOVAL mascot: the logo's drop, with eyes. It falls in on the first page of a
   visit (a ripple where it lands), then breathes, blinks and follows the pointer. The panel is a
   plain sheet of the site (white paper, hairlines, product rows), not a chat app. Open and close are
   transitions, so they can be interrupted. */

.assistant-bot {
    --assistant-drop: oklch(0.878 0.066 229.1); /* #aae0fa, the logo's drop */
    --assistant-eye: var(--joval-blue-active);
    /* Spring with about 12 % overshoot, sampled from a damped spring (hover squish). */
    --assistant-spring: linear(0, 0.112, 0.355, 0.624, 0.851, 1.009, 1.096, 1.126, 1.117, 1.088, 1.054, 1.023, 1.002, 0.989, 0.984, 0.985, 0.988, 0.992, 0.996, 0.999, 1.001, 1.002, 1.002, 1.002, 1);
    position: fixed;
    right: 20px;
    bottom: calc(84px + env(safe-area-inset-bottom, 0px));
    z-index: 1001;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.75rem;
}

.assistant-bot[hidden],
.page-price-table .assistant-bot,
.page-price-access .assistant-bot {
    display: none;
}

body:has(.assistant-bot:not([hidden])) .back-to-top,
body:has(.assistant-bot:not([hidden])) #back-to-top {
    bottom: calc(156px + env(safe-area-inset-bottom, 0px));
}

/* ── Mascot ─────────────────────────────────────────────────────────────── */

.assistant-bot__drop {
    fill: var(--assistant-drop);
}

.assistant-bot__eye {
    fill: var(--assistant-eye);
    transform-box: fill-box;
    transform-origin: center;
    animation: assistant-bot-blink 6s infinite;
}

@keyframes assistant-bot-blink {
    0%, 94%, 100% { transform: scaleY(1); }
    97% { transform: scaleY(0.15); }
}

/* Eyes follow the pointer (js sets the transform); the transition smooths each step. */
.assistant-bot__eyes {
    transition: transform 240ms var(--ease-out-expo);
}

/* ── Launcher ───────────────────────────────────────────────────────────── */

.assistant-bot__launcher {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 60px;
    height: 60px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 160ms var(--ease-out-expo), opacity 150ms var(--ease-out-expo);
}

.assistant-bot__launcher:active {
    transform: scale(0.94);
}

.assistant-bot__launcher:focus-visible {
    outline: 3px solid var(--color-accent);
    outline-offset: 3px;
}

/* The panel opens over the launcher's corner, so the launcher shrinks away under it and back. */
.assistant-bot.is-open .assistant-bot__launcher {
    opacity: 0;
    transform: scale(0.85);
    pointer-events: none;
}

/* The light drop needs its shadow to stand out on white sections. It squashes from its base. */
.assistant-bot__face {
    width: 100%;
    height: 100%;
    overflow: visible;
    filter: drop-shadow(0 3px 6px oklch(0.208 0.04 265.755 / 0.28));
    transform-origin: 50% 94%;
}

/* Idle: the drop breathes, stretching a little from its base. */
.assistant-bot__launcher .assistant-bot__body-shape {
    transform-box: fill-box;
    transform-origin: 50% 100%;
    animation: assistant-bot-breathe 4s var(--ease-in-out) infinite;
}

@keyframes assistant-bot-breathe {
    0%, 100% { transform: none; }
    50% { transform: scale(0.98, 1.035); }
}

/* Hover squishes the drop and widens the eyes, on a spring. `scale` composes with the keyframed
   `transform`, so the breath and the blink keep running underneath. */
@media (hover: hover) and (pointer: fine) {
    .assistant-bot__face,
    .assistant-bot__launcher .assistant-bot__eyes {
        transition: scale 450ms var(--ease-out-expo), transform 240ms var(--ease-out-expo);
        transition: scale 450ms var(--assistant-spring), transform 240ms var(--ease-out-expo);
    }

    .assistant-bot__launcher:hover .assistant-bot__face {
        scale: 1.08 0.93;
    }

    .assistant-bot__launcher:hover .assistant-bot__eyes {
        scale: 1 1.15;
    }
}

/* First page of a visit only (js adds the class): it falls in, squashes as it lands and settles,
   and a ripple spreads where it landed. Later pages keep it where it was. */
.assistant-bot--intro .assistant-bot__face {
    animation: assistant-bot-drop-in 650ms var(--ease-out-expo) backwards;
}

@keyframes assistant-bot-drop-in {
    0% { opacity: 0; transform: translateY(-28px) scale(0.88, 1.14); }
    45% { opacity: 1; transform: translateY(0) scale(1.14, 0.86); }
    70% { transform: scale(0.96, 1.05); }
    85% { transform: scale(1.02, 0.98); }
    100% { transform: none; }
}

.assistant-bot__ripple {
    position: absolute;
    left: 50%;
    bottom: 1px;
    width: 46px;
    height: 12px;
    margin-left: -23px;
    border: 2px solid var(--assistant-drop);
    border-radius: 50%;
    opacity: 0;
    pointer-events: none;
}

.assistant-bot--intro .assistant-bot__ripple {
    animation: assistant-bot-ripple 700ms var(--ease-out-expo) 270ms;
}

@keyframes assistant-bot-ripple {
    0% { opacity: 0.9; transform: scale(0.5); }
    100% { opacity: 0; transform: scale(1.7); }
}

/* Hello wobble when the suggestion appears, so the bubble reads as the drop speaking. */
.assistant-bot--hello .assistant-bot__face {
    animation: assistant-bot-hello 900ms var(--ease-in-out);
}

@keyframes assistant-bot-hello {
    0%, 100% { transform: none; }
    15% { transform: scale(1.08, 0.92); }
    35% { transform: rotate(-9deg) scale(0.97, 1.04); }
    55% { transform: rotate(6deg); }
    75% { transform: rotate(-2deg); }
}

/* Touch screens have no pointer to follow: an occasional glance towards the page instead. */
@media (hover: none), (pointer: coarse) {
    .assistant-bot__launcher .assistant-bot__eyes {
        animation: assistant-bot-glance 9s var(--ease-in-out) infinite;
    }
}

@keyframes assistant-bot-glance {
    0%, 40%, 58%, 100% { transform: translateX(0); }
    44%, 54% { transform: translateX(-3px); }
}

/* ── Teaser ─────────────────────────────────────────────────────────────── */

/* A note above the drop that grows from its corner and fades out when hidden. */
.assistant-bot__teaser {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    max-width: 260px;
    margin: 0;
    padding: 0.55rem 0.55rem 0.55rem 0.85rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--color-text-main);
    background: var(--surface-raised);
    border-radius: var(--radius-md) var(--radius-md) var(--radius-sm) var(--radius-md);
    box-shadow: 0 4px 14px oklch(0.208 0.04 265.755 / 0.16);
    transform-origin: 100% 100%;
    transition:
        opacity 200ms var(--ease-out-expo),
        transform 200ms var(--ease-out-expo),
        display 200ms allow-discrete;
}

@starting-style {
    .assistant-bot__teaser {
        opacity: 0;
        transform: translateY(6px) scale(0.95);
    }
}

.assistant-bot__teaser[hidden] {
    display: none;
    opacity: 0;
    transform: translateY(4px) scale(0.97);
    transition-duration: 150ms;
}

.assistant-bot__teaser > span {
    cursor: pointer;
}

/* About 30 px to tap (the icon is 14 px); the negative margin keeps the note compact. */
.assistant-bot__teaser-close {
    display: inline-flex;
    flex-shrink: 0;
    padding: 0.5rem;
    margin: -0.35rem -0.35rem -0.35rem 0;
    border: none;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-secondary);
    cursor: pointer;
}

/* ── Panel ──────────────────────────────────────────────────────────────── */

/* Grows from the launcher's corner, closes faster than it opens.
   visibility keeps it out of the tab order when closed. A floating panel: shadow, no border. */
.assistant-bot__panel {
    position: absolute;
    right: 0;
    bottom: 0;
    display: flex;
    flex-direction: column;
    width: min(400px, calc(100vw - 40px));
    max-height: min(640px, calc(100dvh - 104px));
    background: var(--surface-raised);
    border-radius: var(--radius-md);
    box-shadow: 0 12px 32px oklch(0.208 0.04 265.755 / 0.22);
    transform-origin: bottom right;
    opacity: 0;
    transform: translateY(8px) scale(0.96);
    visibility: hidden;
    transition:
        opacity 150ms var(--ease-out-expo),
        transform 150ms var(--ease-out-expo),
        visibility 0s linear 150ms;
}

.assistant-bot.is-open .assistant-bot__panel {
    opacity: 1;
    transform: none;
    visibility: visible;
    transition:
        opacity 220ms var(--ease-out-expo),
        transform 220ms var(--ease-out-expo),
        visibility 0s;
}

.assistant-bot__header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 0.6rem 0.6rem 1rem;
    border-bottom: 1px solid var(--border-subtle);
}

.assistant-bot__avatar {
    flex-shrink: 0;
}

.assistant-bot__title {
    flex: 1;
    min-width: 0;
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: var(--color-primary);
}

.assistant-bot__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--control-size);
    height: var(--control-size);
    padding: 0;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    background: none;
    cursor: pointer;
    transition: background-color 150ms ease, color 150ms ease;
}

.assistant-bot__close:hover {
    color: var(--color-primary);
    background: var(--surface-subtle);
}

.assistant-bot__close:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: -2px;
}

.assistant-bot__body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0.85rem 1rem 1rem;
}

/* A real status (only the team sees the assistant), so a status chip. */
.assistant-bot__badge {
    display: inline-block;
    margin: 0 0 0.6rem;
    padding: 0.15rem 0.5rem;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--warning-ink);
    background: var(--warning-bg);
    border-radius: var(--radius-sm);
}

/* The AI notice (EU AI Act art. 50) leads the panel as small print, ruled off from the chat. */
.assistant-bot__notice {
    margin: 0 0 1rem;
    padding-bottom: 0.85rem;
    font-size: 0.78rem;
    line-height: 1.45;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-subtle);
}

.assistant-bot__log {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* The assistant writes on the page itself; the visitor's own words sit in a ruled box on the right. */
.assistant-bot__message {
    max-width: 100%;
}

.assistant-bot__message--user {
    align-self: flex-end;
    max-width: 85%;
    padding: 0.55rem 0.8rem;
    background: var(--surface-subtle);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md) var(--radius-md) var(--radius-sm) var(--radius-md);
}

/* Messages sent or received now slide up into place; restored ones (page change) do not. */
.assistant-bot__message--new {
    transition: opacity 220ms var(--ease-out-expo), transform 220ms var(--ease-out-expo);
}

@starting-style {
    .assistant-bot__message--new {
        opacity: 0;
        transform: translateY(8px);
    }
}

.assistant-bot__text {
    margin: 0;
    font-size: 0.95rem;
    line-height: 1.55;
    color: var(--color-text-main);
    white-space: pre-line;
}

/* While the answer is on its way: the small drop bounces next to the «a pensar» line. */
.assistant-bot__pending {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.assistant-bot__pending .assistant-bot__body-shape {
    transform-box: fill-box;
    transform-origin: 50% 100%;
    animation: assistant-bot-bounce 900ms var(--ease-in-out) infinite;
}

@keyframes assistant-bot-bounce {
    0%, 100% { transform: scale(1.06, 0.92); }
    45% { transform: translateY(-14%) scale(0.95, 1.06); }
}

/* Suggested products: rows with the product photo, as in the catalogue. */
.assistant-bot__products-label {
    margin: 0.85rem 0 0.4rem;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.assistant-bot__products {
    list-style: none;
    margin: 0;
    padding: 0;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
}

.assistant-bot__products li + li {
    border-top: 1px solid var(--border-subtle);
}

.assistant-bot__product {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-height: 3.5rem;
    padding: 0.4rem 0.5rem 0.4rem 0.4rem;
    font-size: 0.92rem;
    font-weight: 600;
    line-height: 1.3;
    color: var(--color-primary);
    text-decoration: none;
    transition: background-color 150ms ease;
}

.assistant-bot__product:hover {
    background: var(--accent-surface);
}

.assistant-bot__product:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: -2px;
}

.assistant-bot__product-photo {
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    object-fit: contain;
    background: var(--surface-raised);
    border-radius: var(--radius-sm);
}

.assistant-bot__product-name {
    flex: 1;
    min-width: 0;
}

.assistant-bot__product svg {
    flex-shrink: 0;
    color: var(--text-secondary);
}

.assistant-bot__contact {
    margin-top: 0.75rem;
}

/* ── Compose ────────────────────────────────────────────────────────────── */

.assistant-bot__form {
    padding: 0.75rem 1rem 0.9rem;
    border-top: 1px solid var(--border-subtle);
}

.assistant-bot__compose {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.assistant-bot__input {
    flex: 1;
    min-width: 0;
    font: inherit;
    resize: none;
}

.assistant-bot__status {
    margin: 0.4rem 0 0;
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.assistant-bot__status:empty {
    display: none;
}

/* ── Placement ──────────────────────────────────────────────────────────── */

/* Same offsets as WhatsApp, one launcher higher: clear of the cookie panel and the product page's
   mobile action bar. back-to-top follows (its cookie-panel rule is !important). */
body:has(.cookie-consent.is-visible) .assistant-bot {
    bottom: calc(10.5rem + 64px + env(safe-area-inset-bottom, 0px));
}

body:has(.cookie-consent.is-visible):has(.assistant-bot:not([hidden])) .back-to-top,
body:has(.cookie-consent.is-visible):has(.assistant-bot:not([hidden])) #back-to-top {
    bottom: calc(10.5rem + 136px + env(safe-area-inset-bottom, 0px)) !important;
}

@media (max-width: 768px) {
    .assistant-bot {
        right: 16px;
        bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    }

    body:has(.assistant-bot:not([hidden])) .back-to-top,
    body:has(.assistant-bot:not([hidden])) #back-to-top {
        bottom: calc(140px + env(safe-area-inset-bottom, 0px));
    }

    /* WhatsApp and back-to-top are hidden there; the bot sits above the bar. */
    body:has(.mobile-action-bar) .assistant-bot {
        bottom: calc(88px + env(safe-area-inset-bottom, 0px));
    }
}

/* Phones: the panel is a sheet that slides up from the bottom edge over a dimmed page.
   Tapping the dimmed page closes it (js: a click on the root itself). */
@media (max-width: 640px) {
    .assistant-bot__launcher {
        width: 54px;
        height: 54px;
    }

    body:has(.cookie-consent.is-visible) .assistant-bot {
        bottom: calc(7.5rem + 60px + env(safe-area-inset-bottom, 0px));
    }

    body:has(.cookie-consent.is-visible):has(.assistant-bot:not([hidden])) .back-to-top,
    body:has(.cookie-consent.is-visible):has(.assistant-bot:not([hidden])) #back-to-top {
        bottom: calc(7.5rem + 124px + env(safe-area-inset-bottom, 0px)) !important;
    }

    .assistant-bot::before {
        content: "";
        position: fixed;
        inset: 0;
        background: oklch(0 0 0 / 0.4);
        opacity: 0;
        visibility: hidden;
        transition: opacity 200ms var(--ease-out-expo), visibility 0s linear 200ms;
    }

    .assistant-bot.is-open::before {
        opacity: 1;
        visibility: visible;
        transition: opacity 320ms var(--ease-out-expo), visibility 0s;
    }

    .assistant-bot__panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        max-height: 85dvh;
        border-radius: var(--radius-md) var(--radius-md) 0 0;
        padding-bottom: env(safe-area-inset-bottom, 0px);
        opacity: 1;
        transform: translateY(100%);
        transition:
            opacity 200ms var(--ease-drawer),
            transform 200ms var(--ease-drawer),
            visibility 0s linear 200ms;
    }

    .assistant-bot.is-open .assistant-bot__panel {
        transition:
            opacity 320ms var(--ease-drawer),
            transform 320ms var(--ease-drawer),
            visibility 0s;
    }
}

/* Reduced motion: no movement, only fades (the «a pensar» drop pulses in place). */
@media (prefers-reduced-motion: reduce) {
    .assistant-bot__eye,
    .assistant-bot__launcher .assistant-bot__eyes,
    .assistant-bot__launcher .assistant-bot__body-shape,
    .assistant-bot--hello .assistant-bot__face,
    .assistant-bot--intro .assistant-bot__ripple {
        animation: none;
    }

    .assistant-bot__launcher:hover .assistant-bot__face,
    .assistant-bot__launcher:hover .assistant-bot__eyes {
        scale: none;
    }

    .assistant-bot--intro .assistant-bot__face {
        animation: assistant-bot-fade-in 240ms var(--ease-out-expo) backwards;
    }

    .assistant-bot__pending .assistant-bot__body-shape {
        animation: assistant-bot-pulse 1.2s var(--ease-in-out) infinite;
    }

    .assistant-bot.is-open .assistant-bot__launcher,
    .assistant-bot__teaser,
    .assistant-bot__teaser[hidden],
    .assistant-bot__panel,
    .assistant-bot.is-open .assistant-bot__panel,
    .assistant-bot__message--new {
        transform: none;
    }

    /* The phone sheet fades instead of sliding. */
    .assistant-bot__panel {
        opacity: 0;
    }

    @starting-style {
        .assistant-bot__teaser,
        .assistant-bot__message--new {
            transform: none;
        }
    }
}

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

@keyframes assistant-bot-pulse {
    0%, 100% { opacity: 0.45; }
    50% { opacity: 1; }
}
