/*
 * The stylesheet of the chat on the site, loaded by ChatAsset. Built on the tokens of the site: the values of its
 * own are tokens too, so a theme can change them.
 *
 * A round button in the bottom right corner of the page (kupisa-chat-toggle) opens a window above it
 * (kupisa-chat-window): its head with the sign of the assistant, the messages, the visitor's on the right and the answers on the left, and the
 * field to write in. On a phone the window takes the whole screen.
 *
 * It stands above the round button of the cookie banner (z-index 999), which the window covers on a narrow screen;
 * the banner itself opens as a modal dialog, above everything, the chat too.
 */

:root {
    --chat-width: 24rem;
    --chat-height: 36rem;
    --chat-toggle-size: 3.5rem;
    --chat-avatar-size: 2.5rem;
    --chat-radius: 1.5rem;
    --chat-bubble-radius: 1.125rem;
    --chat-shadow: 0 1.25rem 3.5rem -0.75rem rgba(0, 0, 0, 0.22), 0 0.25rem 0.75rem rgba(0, 0, 0, 0.06);
    --chat-bubble-shadow: 0 0.0625rem 0.125rem rgba(0, 0, 0, 0.06);
    --chat-online: #1e9e61;
}

.kupisa-chat {
    position: fixed;
    right: var(--space-3);
    bottom: var(--space-3);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: var(--space-3);
}

.kupisa-chat-toggle {
    display: grid;
    place-items: center;
    width: var(--chat-toggle-size);
    height: var(--chat-toggle-size);
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--color-primary);
    color: var(--color-primary-contrast);
    box-shadow: var(--chat-shadow);
    cursor: pointer;
    transition: transform 0.2s ease;
}

.kupisa-chat-toggle:hover {
    transform: translateY(-0.125rem) scale(1.04);
}

.kupisa-chat-window {
    display: flex;
    flex-direction: column;
    width: min(calc(100vw - 2 * var(--space-3)), var(--chat-width));
    height: min(calc(100vh - var(--chat-toggle-size) - 3 * var(--space-3)), var(--chat-height));
    overflow: hidden;
    border-radius: var(--chat-radius);
    background: var(--color-surface);
    color: var(--color-text);
    box-shadow: var(--chat-shadow);
    animation: kupisa-chat-open 0.2s ease-out;
}

.kupisa-chat-window[hidden] {
    display: none;
}

@keyframes kupisa-chat-open {
    from {
        opacity: 0;
        transform: translateY(0.5rem) scale(0.98);
    }
}

.kupisa-chat-head {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-3);
    background: var(--color-background);
}

.kupisa-chat-avatar {
    display: grid;
    flex: none;
    place-items: center;
    width: var(--chat-avatar-size);
    height: var(--chat-avatar-size);
    border-radius: 50%;
    background: var(--color-secondary);
    color: var(--color-secondary-contrast);
}

.kupisa-chat-heading {
    flex: 1;
    min-width: 0;
}

.kupisa-chat-title {
    margin: 0;
    font-family: var(--font-heading);
    font-weight: var(--heading-weight);
    line-height: 1.2;
    color: var(--color-heading);
}

.kupisa-chat-status {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    margin: 0.125rem 0 0;
    font-size: var(--text-sm);
    line-height: 1.2;
    color: var(--color-text-muted);
}

.kupisa-chat-status::before {
    content: "";
    width: 0.5rem;
    height: 0.5rem;
    border-radius: 50%;
    background: var(--chat-online);
}

.kupisa-chat-close {
    display: grid;
    place-items: center;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: none;
    color: var(--color-text-muted);
    font-size: var(--text-2xl);
    line-height: 1;
    cursor: pointer;
}

.kupisa-chat-close:hover {
    background: var(--color-surface);
}

.kupisa-chat-messages {
    display: flex;
    flex: 1;
    min-height: 0;
    overscroll-behavior: contain;
    flex-direction: column;
    gap: var(--space-2);
    padding: var(--space-3);
    overflow-y: auto;
}

/* The messages stand at the bottom, right above the field, while they do not fill the window. */
.kupisa-chat-message:first-child {
    margin-top: auto;
}

.kupisa-chat-message {
    max-width: 85%;
    margin: 0;
    padding: 0.625rem 0.875rem;
    border-radius: var(--chat-bubble-radius);
    font-size: var(--text-sm);
    line-height: 1.5;
    overflow-wrap: anywhere;
}

.kupisa-chat-message p,
.kupisa-chat-message ul {
    margin: 0 0 var(--space-2);
}

.kupisa-chat-message ul {
    padding-left: var(--space-3);
}

.kupisa-chat-message > :last-child {
    margin-bottom: 0;
}

.kupisa-chat-message a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 0.15em;
}

.kupisa-chat-message-assistant {
    align-self: flex-start;
    border-bottom-left-radius: 0.375rem;
    background: var(--color-background);
    box-shadow: var(--chat-bubble-shadow);
}

.kupisa-chat-message-user {
    align-self: flex-end;
    border-bottom-right-radius: 0.375rem;
    background: var(--color-secondary);
    color: var(--color-secondary-contrast);
    white-space: pre-wrap;
}

.kupisa-chat-message.is-error {
    background: var(--color-danger-subtle);
    color: var(--color-danger);
    box-shadow: none;
}

/* Three dots while the answer has not started. */
.kupisa-chat-message.is-typing::after {
    content: "• • •";
    color: var(--color-text-muted);
    animation: kupisa-chat-typing 1.2s ease-in-out infinite;
}

@keyframes kupisa-chat-typing {
    50% {
        opacity: 0.3;
    }
}

.kupisa-chat-form {
    display: flex;
    flex: none;
    align-items: flex-end;
    gap: var(--space-2);
    margin: 0 var(--space-3) var(--space-3);
    padding: 0.375rem 0.375rem 0.375rem var(--space-3);
    border-radius: var(--chat-radius);
    background: var(--color-background);
    box-shadow: var(--chat-bubble-shadow);
}

.kupisa-chat-form:focus-within {
    box-shadow: 0 0 0 0.1875rem var(--color-focus);
}

.kupisa-chat-input {
    flex: 1;
    min-height: 2.25rem;
    padding: 0.4375rem 0;
    border: 0;
    background: none;
    font: inherit;
    font-size: var(--text-sm);
    line-height: 1.5;
    resize: none;
}

.kupisa-chat-input:focus {
    outline: none;
}

.kupisa-chat-send {
    height: 2.25rem;
    padding: 0 var(--space-3);
    border: 0;
    border-radius: 1.125rem;
    background: var(--color-primary);
    color: var(--color-primary-contrast);
    font: inherit;
    font-size: var(--text-sm);
    font-weight: var(--heading-weight);
    cursor: pointer;
}

.kupisa-chat-send:disabled {
    opacity: 0.4;
    cursor: wait;
}

/* The short question next to the button for a visitor who comes for the first time. */
.kupisa-chat-teaser {
    position: absolute;
    right: calc(var(--chat-toggle-size) + var(--space-2));
    bottom: calc(var(--chat-toggle-size) / 2);
    display: flex;
    align-items: flex-start;
    gap: var(--space-1);
    width: max-content;
    max-width: min(16rem, calc(100vw - var(--chat-toggle-size) - 3 * var(--space-3)));
    padding: 0.625rem 0.5rem 0.625rem 0.875rem;
    border-radius: var(--chat-bubble-radius);
    border-bottom-right-radius: 0.375rem;
    background: var(--color-background);
    color: var(--color-text);
    box-shadow: var(--chat-shadow);
    transform: translateY(50%);
    animation: kupisa-chat-teaser 0.35s ease-out;
}

.kupisa-chat-teaser[hidden] {
    display: none;
}

@keyframes kupisa-chat-teaser {
    from {
        opacity: 0;
        transform: translate(0.5rem, 50%) scale(0.96);
    }
}

.kupisa-chat-teaser-text {
    padding: 0;
    border: 0;
    background: none;
    color: inherit;
    font: inherit;
    font-size: var(--text-sm);
    line-height: 1.4;
    text-align: left;
    cursor: pointer;
}

.kupisa-chat-teaser-close {
    flex: none;
    width: 1.25rem;
    height: 1.25rem;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: none;
    color: var(--color-text-muted);
    font-size: var(--text-lg);
    line-height: 1;
    cursor: pointer;
}

.kupisa-chat-teaser-close:hover {
    background: var(--color-surface);
}

@media (prefers-reduced-motion: reduce) {
    .kupisa-chat-window,
    .kupisa-chat-teaser {
        animation: none;
    }
}

/*
 * On a phone the open window takes the whole screen, and its own close button replaces the round one. It covers
 * the part of the screen the visitor sees (`--chat-viewport-height` and `--chat-viewport-top`, which the script
 * sets from the visual viewport), so the keyboard never pushes its head out of sight: the head and the field stay
 * where they are and only the messages scroll. The page behind it does not scroll meanwhile.
 */
@media (max-width: 36rem) {
    .kupisa-chat.is-open {
        inset: var(--chat-viewport-top, 0) 0 auto 0;
        height: var(--chat-viewport-height, 100dvh);
    }

    .kupisa-chat.is-open .kupisa-chat-window {
        width: 100%;
        height: 100%;
        border-radius: 0;
        animation: none;
    }

    .kupisa-chat.is-open .kupisa-chat-toggle {
        display: none;
    }

    /* The page behind is held in place, at the place the visitor was at (the `top` the script gives it). */
    .kupisa-chat-locked,
    .kupisa-chat-locked body {
        overflow: hidden;
    }

    .kupisa-chat-locked body {
        position: fixed;
        right: 0;
        left: 0;
    }

    /* Under 1rem a phone zooms the page in when the field gets the focus. */
    .kupisa-chat-input {
        font-size: 1rem;
    }
}
