/* ==========================================================================
   Mellio AI — frontend-widget
   Färger, typsnitt och launcher-storlek kommer från CSS-variabler som
   sätts i wp_head (se class-widget.php).
   ========================================================================== */

#mellio-root {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    font-family: var(--mellio-font, inherit);
}

/*
 * Knappar, fält och textareor ärver INTE typsnitt av sig själva — utan detta
 * får de webbläsarens systemfont oavsett vad temat använder.
 */
#mellio-root button,
#mellio-root input,
#mellio-root textarea,
#mellio-root a {
    font-family: var(--mellio-font, inherit);
}

#mellio-root.mellio-pos-left {
    right: auto;
    left: 24px;
}

/* ---------- Launcher ---------- */

#mellio-launcher {
    width: var(--mellio-launcher-size, 64px);
    height: var(--mellio-launcher-size, 64px);
    border-radius: 50%;
    background: var(--mellio-primary);
    border: none;
    cursor: pointer;
    position: relative;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(0,0,0,0.18);
    transition: transform 0.15s ease;
    color: var(--mellio-text);
}

#mellio-launcher:hover { transform: scale(1.06); }

#mellio-launcher-face {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
}

#mellio-launcher img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#mellio-launcher .mellio-launcher-symbol {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 56%;
    height: 56%;
    color: var(--mellio-text);
}

#mellio-launcher .mellio-launcher-symbol svg {
    width: 100%;
    height: 100%;
}

/*
 * Brickan ska läsas som en liten markering på avataren, inte som ett andra
 * element bredvid den: mindre, innanför kanten och utan ring som konkurrerar
 * med avatarens egen form.
 */
#mellio-launcher-badge {
    position: absolute;
    right: 1%;
    bottom: 1%;
    width: 38%;
    height: 38%;
    min-width: 21px;
    min-height: 21px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    box-sizing: border-box;
}

/*
 * Primärfärg med vit ikon: fungerar mot både ljusa och mörka avatarer och
 * sidbakgrunder, till skillnad från en vit bricka som försvinner mot vitt.
 */
#mellio-launcher-badge.mellio-badge-bubble {
    background: var(--mellio-primary);
    color: var(--mellio-text, #fff);
}

/*
 * Balansen: brickan stor nog att synas, symbolen liten nog att få luft.
 * 38 % bricka och 52 % symbol är det som fungerar mot både foto och platt
 * bakgrund — mindre bricka försvinner, större symbol ser hoptryckt ut.
 */
#mellio-launcher-badge.mellio-badge-bubble svg {
    width: 52% !important;
    height: 52% !important;
    margin: 0 auto;
}

#mellio-launcher-badge.mellio-badge-avatar {
    background: var(--mellio-bg, #fff);
    border: 1px solid rgba(0, 0, 0, 0.06);
}

#mellio-launcher-badge.mellio-badge-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#mellio-launcher .mellio-launcher-symbol svg {
    width: 100%;
    height: 100%;
}

#mellio-root.mellio-pos-left #mellio-launcher-badge {
    right: auto;
    left: 1%;
}

/* ---------- Teaserbubbla ----------
 * Ligger vid sidan om launchern. Utan width: max-content ärver en
 * absolutpositionerad ruta launcherns bredd som tillgängligt utrymme
 * och radbryter efter varje ord.
 */

#mellio-teaser {
    position: absolute;
    bottom: 6px;
    right: calc(var(--mellio-launcher-size, 64px) + 12px);
    display: flex;
    align-items: flex-start;
    gap: 2px;
    width: max-content;
    max-width: min(280px, calc(100vw - var(--mellio-launcher-size, 64px) - 60px));
    background: var(--mellio-bg, #fff);
    border-radius: 14px;
    border-bottom-right-radius: 4px;
    box-shadow: 0 6px 24px rgba(0,0,0,0.18);
    padding: 2px 4px 2px 6px;
    animation: mellio-teaser-in 0.35s ease both;
}

#mellio-root.mellio-pos-left #mellio-teaser {
    right: auto;
    left: calc(var(--mellio-launcher-size, 64px) + 12px);
    border-bottom-right-radius: 14px;
    border-bottom-left-radius: 4px;
    animation-name: mellio-teaser-in-left;
}

#mellio-teaser[hidden] { display: none; }

#mellio-teaser-open {
    background: none;
    border: none;
    padding: 9px 4px 9px 8px;
    text-align: left;
    font-size: 13.5px;
    line-height: 1.45;
    color: inherit;
    cursor: pointer;
}

#mellio-teaser-close {
    background: none;
    border: none;
    font-size: 16px;
    line-height: 1;
    padding: 4px 6px;
    margin-top: 6px;
    cursor: pointer;
    color: inherit;
    opacity: 0.45;
    flex-shrink: 0;
}

#mellio-teaser-close:hover { opacity: 0.9; }

@keyframes mellio-teaser-in {
    from { opacity: 0; transform: translateX(8px); }
    to   { opacity: 1; transform: translateX(0); }
}

@keyframes mellio-teaser-in-left {
    from { opacity: 0; transform: translateX(-8px); }
    to   { opacity: 1; transform: translateX(0); }
}

/* ---------- Chattfönster ---------- */

#mellio-window {
    display: none;
    flex-direction: column;
    position: absolute;
    bottom: calc(var(--mellio-launcher-size, 64px) + 12px);
    right: 0;
    width: 340px;
    height: 500px;
    max-height: calc(100vh - 120px);
    background: var(--mellio-bg);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.18);
    overflow: hidden;
}

#mellio-root.mellio-pos-left #mellio-window {
    right: auto;
    left: 0;
}

#mellio-root.mellio-open #mellio-window {
    display: flex;
}

#mellio-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 16px;
    background: var(--mellio-primary);
    color: var(--mellio-text);
    flex-shrink: 0;
}

#mellio-header img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
}

#mellio-header span {
    flex: 1;
    font-weight: 600;
    font-size: 15px;
}

#mellio-close {
    background: none;
    border: none;
    color: var(--mellio-text);
    font-size: 22px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    opacity: 0.8;
}

#mellio-close:hover { opacity: 1; }

/* ---------- Meddelanden ---------- */

#mellio-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.mellio-msg {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.mellio-msg-bot {
    align-self: flex-start;
    background: var(--mellio-bubble);
    border-bottom-left-radius: 4px;
}

.mellio-msg-user {
    align-self: flex-end;
    background: var(--mellio-primary);
    color: var(--mellio-text);
    border-bottom-right-radius: 4px;
}

.mellio-msg-bot .mellio-link {
    color: var(--mellio-primary);
    text-decoration: underline;
    text-underline-offset: 2px;
    word-break: break-word;
}

.mellio-msg-bot .mellio-link:hover {
    text-decoration-thickness: 2px;
}

.mellio-msg-user .mellio-link {
    color: inherit;
    text-decoration: underline;
}

.mellio-typing {
    display: flex;
    gap: 5px;
    padding: 12px 14px;
    align-items: center;
}

.mellio-typing span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--mellio-primary);
    opacity: 0.4;
    animation: mellio-pulse 1.2s ease-in-out infinite;
}

.mellio-typing span:nth-child(2) { animation-delay: 0.2s; }
.mellio-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes mellio-pulse {
    0%, 80%, 100% { opacity: 0.4; transform: scale(1); }
    40% { opacity: 1; transform: scale(1.2); }
}

/* ---------- Snabbsvar ---------- */

#mellio-quick-replies {
    padding: 8px 12px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    flex-shrink: 0;
}

.mellio-qr-btn {
    background: transparent;
    border: 1.5px solid var(--mellio-primary);
    color: var(--mellio-primary);
    border-radius: 20px;
    padding: 6px 14px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.mellio-qr-btn:hover {
    background: var(--mellio-primary);
    color: var(--mellio-text);
}

/* ---------- Inmatning ---------- */

#mellio-input-row {
    display: flex;
    align-items: center;
    border-top: 1px solid rgba(0,0,0,0.08);
    padding: 8px 12px;
    gap: 8px;
    flex-shrink: 0;
}

#mellio-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 14px;
    outline: none;
    padding: 6px 4px;
    color: inherit;
}

#mellio-send {
    background: var(--mellio-primary);
    color: var(--mellio-text);
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: opacity 0.15s;
}

#mellio-send:hover { opacity: 0.85; }

/* ---------- Små skärmar ---------- */

@media (max-width: 480px) {
    #mellio-window {
        width: calc(100vw - 32px);
    }

    #mellio-teaser {
        max-width: calc(100vw - var(--mellio-launcher-size, 64px) - 48px);
    }

    #mellio-teaser-open {
        font-size: 13px;
    }
}

@media (prefers-reduced-motion: reduce) {
    #mellio-teaser { animation: none; }
    #mellio-launcher { transition: none; }
}

/* ---------- Fotrad ---------- */

#mellio-footnote {
    margin: 0;
    padding: 6px 14px 8px;
    font-size: 10.5px;
    line-height: 1.4;
    text-align: center;
    opacity: 0.5;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 5px;
    flex-shrink: 0;
}

#mellio-footnote a {
    color: inherit;
    text-decoration: none;
}

#mellio-footnote a:hover {
    text-decoration: underline;
}

.mellio-footnote-sep {
    opacity: 0.6;
}

/* ---------- Mobil ----------
 * De flesta besökare sitter i telefonen. Chatten tar nästan hela skärmen,
 * använder dynamisk höjd (dvh) så att adressfältet inte klipper bort
 * inmatningen, och respekterar iPhones säkra ytor.
 */

@media (max-width: 600px) {
    #mellio-root {
        bottom: 16px;
        right: 16px;
    }

    #mellio-root.mellio-pos-left {
        left: 16px;
    }

    #mellio-window {
        position: fixed;
        inset: auto 0 0 0;
        width: 100%;
        height: 88dvh;
        max-height: 100dvh;   /* JS sätter exakt höjd när tangentbordet är uppe */
        border-radius: 16px 16px 0 0;
        padding-bottom: env(safe-area-inset-bottom, 0);
        transition: height 0.12s ease-out, transform 0.12s ease-out;
    }

    #mellio-root.mellio-pos-left #mellio-window,
    #mellio-root.mellio-open #mellio-window {
        left: 0;
        right: 0;
    }

    #mellio-header {
        padding: 14px 16px;
    }

    #mellio-messages {
        padding: 14px;
        overscroll-behavior: contain;
    }

    .mellio-msg {
        max-width: 88%;
        font-size: 15px;   /* under 16px zoomar iOS in vid fokus */
    }

    #mellio-input {
        font-size: 16px;
    }

    #mellio-input-row {
        padding: 10px 12px;
    }

    #mellio-teaser {
        max-width: calc(100vw - var(--mellio-launcher-size, 64px) - 40px);
    }
}

/* Äldre webbläsare utan dvh-stöd faller tillbaka på vh. */
@supports not (height: 1dvh) {
    @media (max-width: 600px) {
        #mellio-window {
            height: 85vh;
            max-height: 85vh;
        }
    }
}

/* ---------- Ikoner ----------
 * Teman sätter ofta egna regler för svg i knappar, vilket kan krympa våra
 * ikoner till en prick. Storleken sätts därför explicit.
 */

#mellio-root svg {
    display: block !important;
    fill: currentColor !important;
    max-width: none !important;
    max-height: none !important;
    overflow: visible !important;
    flex: none !important;
    vertical-align: middle;
}

#mellio-send svg {
    width: 18px !important;
    height: 18px !important;
}

#mellio-send {
    color: var(--mellio-text, #fff);
}

#mellio-header .mellio-header-symbol {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    color: var(--mellio-text);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#mellio-header .mellio-header-symbol svg {
    width: 19px;
    height: 19px;
}

.mellio-msg strong {
    font-weight: 600;
}
