/* ═════════════════════════════════════════════════════════════════════
   SocialChat Ultra — customer widget
   Scroll strategy: the messages container is a plain BLOCK element with
   min-height:0 inside a concrete-height grid cell and overflow-y:auto.
   No flex scroll tricks. Do not replace with flex — three previous
   attempts all failed because flex items ignore max-height in practice.

   Mobile strategy: under 480px the panel goes full-screen (fixed, inset 0)
   and sizes itself to --scu-vvh — the *visual* viewport height kept fresh
   by widget.js — so the iOS/Android keyboard never hides the composer.
   All text inputs are ≥16px on mobile to stop iOS focus-zoom.
   ═══════════════════════════════════════════════════════════════════ */

/* Reset our own elements so the host theme can't bleed into us. */
#scu-root,
#scu-root *,
#scu-root *::before,
#scu-root *::after {
    box-sizing: border-box;
}
#scu-root button,
#scu-root input,
#scu-root textarea,
#scu-root select {
    font-family: inherit;
    font-size: inherit;
    line-height: inherit;
    color: inherit;
    margin: 0;
    /* Neutralize theme/builder (e.g. Elementor kit) form styling. */
    appearance: none;
    -webkit-appearance: none;
    box-shadow: none;
    text-transform: none;
    letter-spacing: normal;
    min-width: 0;
    min-height: 0;
}
#scu-root button {
    background: none;
    border: 0;
    padding: 0;
    cursor: pointer;
    font: inherit;
    color: inherit;
    text-align: inherit;
}
#scu-root a { color: inherit; }
#scu-root p { margin: 0; }
/* The hidden attribute must always win, even over our own display rules. */
#scu-root [hidden] { display: none !important; }
/* Disable double-tap-to-zoom inside the widget — a fast second tap on a
   button otherwise zooms the whole page on iOS. Panning still works. */
#scu-root,
#scu-root * { touch-action: manipulation; }

/* Page-scroll lock while the panel is open full-screen on mobile. */
html.scu-no-scroll,
html.scu-no-scroll body {
    overflow: hidden !important;
    overscroll-behavior: none;
}

#scu-root {
    --scu-accent: #4f46e5;
    --scu-accent-ink: #ffffff;
    --scu-bg: #ffffff;
    --scu-ink: #0f172a;
    --scu-muted: #64748b;
    --scu-border: #e2e8f0;
    --scu-bubble-in: #f1f5f9;
    --scu-bubble-in-ink: #0f172a;
    --scu-bubble-out-ink: #ffffff;
    --scu-bubble-system: #fef3c7;
    --scu-bubble-system-ink: #78350f;
    --scu-shadow: 0 20px 60px rgba(15, 23, 42, .25);

    --scu-panel-w: 380px;
    --scu-panel-h: 600px;

    position: fixed;
    z-index: 2147483000;
    bottom: max(20px, env(safe-area-inset-bottom, 0px));
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.45;
    color: var(--scu-ink);
}
#scu-root[data-position="bottom-right"] { right: max(20px, env(safe-area-inset-right, 0px)); }
#scu-root[data-position="bottom-left"] { left:  max(20px, env(safe-area-inset-left, 0px)); }

/* ── Launcher button ─────────────────────────────────────────────── */
#scu-root #scu-launcher {
    width: 60px; height: 60px; border-radius: 50%;
    background: var(--scu-accent);
    color: var(--scu-accent-ink);
    box-shadow: var(--scu-shadow);
    display: grid; place-items: center;
    position: relative;
    transition: transform .15s ease, box-shadow .15s ease;
}
#scu-root #scu-launcher:hover { transform: scale(1.06); }
#scu-root #scu-launcher:active { transform: scale(.98); }
#scu-root #scu-launcher:focus-visible {
    outline: 3px solid var(--scu-accent);
    outline-offset: 3px;
}
#scu-root #scu-launcher svg { width: 26px; height: 26px; display: block; }
#scu-root #scu-launcher .scu-ico-close { display: none; }
#scu-root.is-open #scu-launcher .scu-ico-open { display: none; }
#scu-root.is-open #scu-launcher .scu-ico-close { display: block; }

#scu-root .scu-unread-dot {
    position: absolute; top: -2px; right: -2px;
    background: #ef4444; color: #fff;
    min-width: 22px; height: 22px; border-radius: 999px;
    font-size: 11px; font-weight: 700;
    display: grid; place-items: center;
    padding: 0 6px; border: 2px solid #fff;
}

/* ── Panel shell ─────────────────────────────────────────────────── */
#scu-panel {
    position: absolute;
    bottom: 76px;
    width: min(var(--scu-panel-w), calc(100vw - 40px));
    height: var(--scu-panel-h);
    max-height: calc(100vh - 120px);
    max-height: calc(100dvh - 120px);
    background: var(--scu-bg);
    border-radius: 16px;
    box-shadow: var(--scu-shadow);
    overflow: hidden;

    opacity: 0;
    transform: translateY(10px) scale(.98);
    pointer-events: none;
    /* visibility keeps the closed panel out of the tab order and the
       accessibility tree — opacity alone leaves it focusable. */
    visibility: hidden;
    transition: opacity .18s ease, transform .18s ease, visibility 0s linear .18s;

    /* Grid with concrete row tracks so the messages cell gets a real
       flexible height (1fr) the child can overflow into. Using grid
       instead of flex because flex children ignore max-height in a way
       that broke our scrolling in every previous version. */
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto minmax(0, 1fr) auto auto auto;
    grid-template-areas:
        "head"
        "pre"
        "msg"
        "type"
        "comp"
        "foot";
}
#scu-root[data-position="bottom-right"] #scu-panel { right: 0; }
#scu-root[data-position="bottom-left"]  #scu-panel { left:  0; }
#scu-root.is-open #scu-panel {
    opacity: 1; transform: none; pointer-events: auto;
    visibility: visible;
    transition: opacity .18s ease, transform .18s ease, visibility 0s;
}

/* ── Header ──────────────────────────────────────────────────────── */
#scu-root .scu-head {
    grid-area: head;
    display: flex; align-items: center; gap: 12px;
    min-height: 60px;
    padding: 10px 14px;
    background: var(--scu-accent);
    color: var(--scu-accent-ink);
}
#scu-root .scu-head-avatar {
    width: 38px; height: 38px; border-radius: 50%;
    background: rgba(255,255,255,.18);
    display: grid; place-items: center;
    flex: 0 0 auto;
}
#scu-root .scu-head-avatar svg { width: 22px; height: 22px; color: #fff; }
#scu-root .scu-head-text { flex: 1 1 auto; min-width: 0; }
#scu-root .scu-head-title {
    font-weight: 600; font-size: 15px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    color: #fff;
}
#scu-root .scu-head-sub {
    font-size: 12px; opacity: .92;
    display: flex; align-items: center; gap: 6px;
    margin-top: 1px;
}
#scu-root .scu-dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #22c55e;
    box-shadow: 0 0 0 2px rgba(34,197,94,.25);
    flex: 0 0 auto;
}
#scu-root.is-offline .scu-dot {
    background: #94a3b8;
    box-shadow: 0 0 0 2px rgba(148,163,184,.25);
}
#scu-root .scu-head-btn {
    background: rgba(255,255,255,.15);
    border-radius: 8px;
    width: 32px; height: 32px;
    color: #fff;
    display: grid; place-items: center;
    flex: 0 0 auto;
    transition: background .12s ease;
}
#scu-root .scu-head-btn:hover { background: rgba(255,255,255,.28); }
#scu-root .scu-head-btn:focus-visible { outline: 2px solid #fff; outline-offset: 1px; }
#scu-root .scu-head-btn svg { width: 16px; height: 16px; display: block; }

/* ── Prechat form ────────────────────────────────────────────────── */
#scu-root .scu-prechat {
    grid-area: pre;
    padding: 14px;
    display: grid; gap: 10px;
    border-bottom: 1px solid var(--scu-border);
    background: #f8fafc;
}
#scu-root .scu-prechat-lede { margin: 0; color: var(--scu-muted); font-size: 13px; }
#scu-root .scu-prechat label {
    display: grid; gap: 4px;
    font-size: 12px; color: var(--scu-muted);
    font-weight: 500;
}
#scu-root .scu-prechat input {
    padding: 9px 12px;
    border: 1px solid var(--scu-border);
    border-radius: 8px;
    font-size: 14px;
    color: var(--scu-ink);
    background: #fff;
    width: 100%;
}
#scu-root .scu-prechat input:focus {
    outline: 2px solid var(--scu-accent);
    outline-offset: -1px;
    border-color: transparent;
}

/* ── Messages: the bulletproof scroll container ──────────────────── */
#scu-root .scu-messages {
    grid-area: msg;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 14px;
    background: #fff;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}
#scu-root .scu-messages::-webkit-scrollbar { width: 6px; }
#scu-root .scu-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
#scu-root .scu-messages::-webkit-scrollbar-track { background: transparent; }

/* Message row: plain block element. No flex. */
#scu-root .scu-msg {
    display: block;
    margin: 0 0 12px 0;
    max-width: 85%;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}
#scu-root .scu-msg:last-child { margin-bottom: 0; }

#scu-root .scu-msg-bubble {
    display: inline-block;
    padding: 9px 13px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.5;
    text-align: left;
}
#scu-root .scu-msg-bubble a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
}
#scu-root .scu-msg-time {
    display: block;
    font-size: 11px;
    color: var(--scu-muted);
    margin-top: 4px;
}

/* Incoming (bot, agent) — left aligned */
#scu-root .scu-msg-in { margin-right: auto; text-align: left; }
#scu-root .scu-msg-in .scu-msg-bubble {
    background: var(--scu-bubble-in);
    color: var(--scu-bubble-in-ink);
    border-bottom-left-radius: 4px;
}
#scu-root .scu-msg-in .scu-msg-sender {
    font-size: 11px; font-weight: 600;
    color: var(--scu-muted);
    margin: 0 0 3px 2px;
    text-transform: uppercase;
    letter-spacing: .02em;
}

/* Outgoing (user) — right aligned */
#scu-root .scu-msg-out { margin-left: auto; text-align: right; }
#scu-root .scu-msg-out .scu-msg-bubble {
    background: var(--scu-accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* System */
#scu-root .scu-msg-system {
    text-align: center;
    max-width: 90%;
    margin: 12px auto;
}
#scu-root .scu-msg-system .scu-msg-bubble {
    background: var(--scu-bubble-system);
    color: var(--scu-bubble-system-ink);
    font-size: 12px;
    font-style: italic;
    text-align: center;
}

#scu-root .scu-msg-failed .scu-msg-bubble { opacity: .6; }
#scu-root .scu-msg-failed .scu-msg-time { color: #ef4444; }

/* ── Dynamic content cards (bot pulls from posts/pages) ──────────── */
#scu-root .scu-post-card {
    background: #fff;
    border: 1px solid rgba(0, 0, 0, .08);
    border-left: 3px solid var(--scu-accent, #4f46e5);
    border-radius: 10px;
    padding: 10px 12px;
    margin: 6px 0;
    font-size: 13px;
    line-height: 1.45;
    text-align: left;
}
#scu-root .scu-post-card:first-child { margin-top: 2px; }
#scu-root .scu-post-card:last-child { margin-bottom: 2px; }
#scu-root .scu-post-card strong {
    display: block;
    font-size: 13.5px;
    margin-bottom: 4px;
    color: var(--scu-ink, #0f172a);
}
#scu-root .scu-post-card a {
    color: var(--scu-accent, #4f46e5);
    font-weight: 500;
    text-decoration: none;
}
#scu-root .scu-post-card a:hover { text-decoration: underline; }

/* ── Department picker (rendered as a bot bubble) ────────────────── */
#scu-root .scu-dept-lede { margin-bottom: 8px; }
#scu-root .scu-dept-picker {
    display: flex; flex-wrap: wrap; gap: 6px;
}
#scu-root .scu-dept-btn {
    padding: 7px 12px;
    border: 1px solid var(--scu-accent);
    border-radius: 999px;
    background: #fff;
    color: var(--scu-accent);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background .12s ease, color .12s ease;
}
#scu-root .scu-dept-btn:hover,
#scu-root .scu-dept-btn:focus-visible {
    background: var(--scu-accent);
    color: #fff;
}

/* ── Typing indicator ────────────────────────────────────────────── */
#scu-root .scu-typing {
    grid-area: type;
    padding: 4px 16px 8px;
    display: flex; gap: 5px; align-items: center;
    background: #fff;
}
#scu-root .scu-typing span {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--scu-muted); opacity: .4;
    animation: scu-bounce 1.3s infinite ease-in-out;
}
#scu-root .scu-typing span:nth-child(2) { animation-delay: .15s; }
#scu-root .scu-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes scu-bounce {
    0%, 80%, 100% { transform: translateY(0);    opacity: .4; }
    40%           { transform: translateY(-4px); opacity: 1; }
}

/* ── Composer ────────────────────────────────────────────────────── */
#scu-root .scu-composer {
    grid-area: comp;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 12px;
    background: #fff;
    border-top: 1px solid var(--scu-border);
}
#scu-root #scu-input {
    flex: 1 1 auto;
    min-width: 0;
    border: 1px solid var(--scu-border);
    border-radius: 20px;
    padding: 9px 14px;
    font-size: 14px;
    font-family: inherit;
    color: var(--scu-ink);
    background: #f8fafc;
    resize: none;
    max-height: 120px;
    line-height: 1.4;
    outline: none;
    overflow-y: auto;
}
#scu-root #scu-input:focus {
    background: #fff;
    border-color: var(--scu-accent);
    box-shadow: 0 0 0 2px rgba(79, 70, 229, .2);
}
#scu-root #scu-input:disabled { opacity: .5; }

#scu-root .scu-btn-ghost,
#scu-root .scu-btn-send {
    flex: 0 0 auto;
    width: 38px; height: 38px;
    border-radius: 50%;
    display: grid; place-items: center;
    transition: filter .12s ease, background .12s ease, transform .1s ease;
}
#scu-root .scu-btn-ghost {
    background: #f1f5f9;
    color: var(--scu-ink);
}
#scu-root .scu-btn-ghost:hover { background: #e2e8f0; }
#scu-root .scu-btn-ghost:active { transform: scale(.94); }
#scu-root .scu-btn-ghost svg { width: 16px; height: 16px; display: block; color: var(--scu-ink); }

#scu-root .scu-btn-send {
    background: var(--scu-accent) !important;
    color: #fff !important;
}
#scu-root .scu-btn-send:hover { filter: brightness(1.08); }
#scu-root .scu-btn-send:active { transform: scale(.94); }
#scu-root .scu-btn-send:disabled { opacity: .5; cursor: not-allowed; }
#scu-root .scu-btn-send svg { width: 16px; height: 16px; display: block; color: #fff; }

/* .scu-btn is also used by the [socialchat_ultra] shortcode button, which
   renders OUTSIDE #scu-root — every property needs to stand on its own
   (accent fallback, border/cursor reset, font). */
#scu-root .scu-btn,
.scu-btn {
    display: inline-block;
    padding: 9px 14px;
    border: 0;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    text-align: center;
    cursor: pointer;
}
#scu-root .scu-btn-primary,
.scu-btn-primary {
    background: var(--scu-accent, #4f46e5);
    color: #fff;
}
#scu-root .scu-btn-primary:hover,
.scu-btn-primary:hover { filter: brightness(1.08); }

/* ── Footer ──────────────────────────────────────────────────────── */
#scu-root .scu-foot {
    grid-area: foot;
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 14px;
    background: #f8fafc;
    border-top: 1px solid var(--scu-border);
    font-size: 11px;
    color: var(--scu-muted);
    min-height: 28px;
}
#scu-root .scu-foot-btn {
    color: #dc2626;
    font-size: 11px;
    font-weight: 600;
    /* Padding grows the hit area without visually moving the label. */
    padding: 8px 10px;
    margin-left: -10px;
}
#scu-root .scu-foot-btn:hover { text-decoration: underline; }
/* No opacity here — 11px text needs every bit of its 4.5:1. */
#scu-root .scu-foot-brand { color: #475569; }

/* ── Rating prompt ───────────────────────────────────────────────── */
#scu-root .scu-rating {
    margin: 16px 0;
    padding: 14px;
    background: #f8fafc;
    border: 1px solid var(--scu-border);
    border-radius: 12px;
    text-align: center;
}
#scu-root .scu-rating-prompt {
    font-size: 13px; font-weight: 600;
    color: var(--scu-ink);
    margin-bottom: 8px;
}
#scu-root .scu-rating-stars {
    display: flex; justify-content: center; gap: 4px;
    margin-bottom: 8px;
}
#scu-root .scu-star {
    font-size: 28px; line-height: 1;
    min-width: 36px; min-height: 36px;
    color: #cbd5e1;
    transition: color .12s ease, transform .08s ease;
}
#scu-root .scu-star:hover { transform: scale(1.08); }
#scu-root .scu-star.is-hover,
#scu-root .scu-star.is-chosen { color: #f59e0b; }

#scu-root .scu-rating-comment {
    width: 100%;
    padding: 7px 10px;
    border: 1px solid var(--scu-border);
    border-radius: 8px;
    font-family: inherit; font-size: 13px;
    resize: none;
    margin-bottom: 8px;
    background: #fff;
    color: var(--scu-ink);
}
#scu-root .scu-rating-comment:focus {
    outline: 2px solid var(--scu-accent);
    outline-offset: -1px;
    border-color: transparent;
}
#scu-root .scu-rating-submit {
    width: 100%;
}
#scu-root .scu-btn-skip {
    display: block;
    width: 100%;
    margin-top: 6px;
    padding: 7px 0;
    font-size: 12px;
    color: var(--scu-muted);
}
#scu-root .scu-btn-skip:hover { text-decoration: underline; }
#scu-root .scu-rating-done {
    font-size: 13px;
    color: #166534;
    font-weight: 600;
    padding: 8px 0;
}

/* ═════════════════════════════════════════════════════════════════════
   Mobile — full-screen panel
   ═══════════════════════════════════════════════════════════════════ */
@media (max-width: 480px) {
    #scu-root {
        bottom: max(16px, env(safe-area-inset-bottom, 0px));
    }
    #scu-root[data-position="bottom-right"] { right: 16px; }
    #scu-root[data-position="bottom-left"] { left:  16px; }

    /* Full-screen takeover, sized to the *visible* viewport so the
       on-screen keyboard pushes the composer up instead of hiding it. */
    #scu-panel {
        position: fixed;
        inset: 0;
        width: 100%;
        height: 100vh;                    /* fallback chain */
        height: 100dvh;
        height: var(--scu-vvh, 100dvh);
        max-height: none;
        border-radius: 0;
        bottom: auto;
        right: auto;
        /* Track the visual viewport (size AND offset) so the panel stays
           glued to the visible area when Safari scrolls for the keyboard —
           and stays fully on-screen even if the page itself got zoomed. */
        top: var(--scu-vvo, 0px);
        left: var(--scu-vvl, 0px);
        width: var(--scu-vvw, 100%);
        transform: translateY(24px);
    }
    #scu-root[data-position="bottom-right"] #scu-panel,
    #scu-root[data-position="bottom-left"]  #scu-panel {
        right: auto;
        left: var(--scu-vvl, 0px);
    }
    #scu-root.is-open #scu-panel { transform: none; }

    /* The header close button is the only way out — hide the floating
       launcher so it doesn't overlap the composer. */
    #scu-root.is-open #scu-launcher { display: none; }

    /* Respect the home-indicator area. */
    #scu-root .scu-foot {
        padding-bottom: env(safe-area-inset-bottom, 0px);
        min-height: calc(28px + env(safe-area-inset-bottom, 0px));
    }

    /* ≥16px inputs stop iOS Safari from zooming the page on focus. */
    #scu-root #scu-input,
    #scu-root .scu-prechat input,
    #scu-root .scu-rating-comment {
        font-size: 16px;
    }

    /* Comfortable touch targets. */
    #scu-root .scu-btn-ghost,
    #scu-root .scu-btn-send { width: 44px; height: 44px; }
    #scu-root .scu-head-btn { width: 40px; height: 40px; }
    #scu-root .scu-msg { max-width: 90%; }
}

/* Short-but-wide viewports (landscape phones): pin the panel to the
   viewport edges instead of stacking it above the launcher. */
@media (max-height: 520px) and (min-width: 481px) {
    #scu-panel {
        position: fixed;
        top: 8px;
        bottom: 8px;
        height: auto;
        max-height: none;
    }
    #scu-root[data-position="bottom-right"] #scu-panel { right: 8px; left: auto; }
    #scu-root[data-position="bottom-left"]  #scu-panel { left: 8px;  right: auto; }
}

/* ── Reduced motion ──────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    #scu-root *,
    #scu-root *::before,
    #scu-root *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
    }
    #scu-root .scu-messages { scroll-behavior: auto; }
}
