/* The speaking pair owns its bounds; the surrounding onboarding UI is unchanged. */
#nameScreen .onboarding-avatar-guide[hidden] {
    display: none !important;
}

#nameScreen .onboarding-avatar-guide:not([hidden]) {
    position: relative;
    inset: auto;
    display: grid;
    grid-template-columns: 14% 8% 78%;
    align-items: end;
    direction: ltr;
    container-type: inline-size;
    width: min(100%, 420px);
    max-width: calc(100vw - 32px);
    height: auto;
    min-height: auto;
    aspect-ratio: 100 / 77;
    max-height: none;
    box-sizing: border-box;
    flex: 0 0 auto;
    align-self: start;
    margin: 12px auto;
    padding: 0;
    gap: 0;
    transform: none;
    isolation: isolate;
    pointer-events: none;
    z-index: 2;
}

#nameScreen .onboarding-avatar-guide-image {
    position: static;
    grid-column: 1 / 3;
    grid-row: 1;
    display: block;
    width: 100%;
    height: auto;
    margin: 0;
    padding: 0;
    transform: none;
    object-fit: contain;
    filter: drop-shadow(0 5px 7px rgba(63, 22, 102, 0.22));
    z-index: 2;
}

#nameScreen .onboarding-avatar-guide-bubble {
    position: relative;
    inset: auto;
    grid-column: 2 / 4;
    grid-row: 1;
    display: block;
    align-self: start;
    width: 100%;
    height: auto;
    min-height: 0;
    aspect-ratio: 4 / 3;
    margin: 0 0 12cqi;
    padding: 0;
    transform: none;
    background: url("../img/onboarding/companion/speech-bubble.webp") center / contain no-repeat;
    isolation: isolate;
    z-index: 1;
}

/* Retain the original bubble artwork and softened text interior. */
#nameScreen .onboarding-avatar-guide-bubble::after {
    content: "";
    position: absolute;
    inset: 19% 13% 23% 15%;
    border-radius: 50%;
    background: radial-gradient(
        ellipse at center,
        rgba(255, 255, 255, 0.99) 0%,
        rgba(255, 255, 255, 0.98) 48%,
        rgba(255, 255, 255, 0.92) 73%,
        rgba(255, 255, 255, 0) 100%
    );
    filter: blur(7px);
    z-index: 0;
    pointer-events: none;
}

#nameScreen .onboarding-avatar-guide-message {
    position: absolute;
    inset: 10% 7% 12% 13%;
    display: block;
    align-content: center;
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: inherit;
    font-size: clamp(12px, 3.8cqi, 18px);
    font-weight: 850;
    line-height: 1.18;
    text-align: center;
    white-space: normal;
    overflow-wrap: break-word;
    word-break: normal;
    unicode-bidi: plaintext;
    color: #42136b;
    z-index: 1;
}

#nameScreen .onboarding-avatar-guide-message > span {
    display: inline;
}

#nameScreen .onboarding-guide-brand {
    display: inline;
    direction: ltr;
    unicode-bidi: isolate;
    white-space: nowrap;
    color: #d92c91;
    font-weight: 900;
}

#nameScreen #nameWelcomeGuide .onboarding-avatar-guide-image {
    filter: none;
}

#nameScreen #nameWelcomeGuide .onboarding-avatar-guide-bubble::after {
    content: none;
}

#nameScreen #nameWelcomeGuide .onboarding-avatar-guide-message {
    font-weight: 700;
    color: #53236b;
    background: rgba(255, 255, 255, 0.20);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 42%;
}

/*
 * A side position is used only when both the center content lane (640px)
 * and a readable pair (at least 350px) fit, with a 48px clearance.
 * Otherwise the pair stays in flow, reserving its complete scrollable height.
 */
@media (min-width: 1440px) {
    #nameScreen .onboarding-avatar-guide:not([hidden]) {
        position: fixed;
        top: 120px;
        left: 24px;
        right: auto;
        bottom: auto;
        width: min(520px, calc((100vw - 640px) / 2 - 48px));
        margin: 0;
    }
}

@media (prefers-reduced-motion: reduce) {
    #nameScreen .onboarding-avatar-guide,
    #nameScreen .onboarding-avatar-guide * {
        animation: none !important;
        transition: none !important;
    }
}



/* Content-sized rows may scroll; a short viewport must not center the card over the pair. */
#nameScreen:not([hidden]):has(#stepName:not([hidden])) {
    grid-template-rows: max-content max-content minmax(max-content, 1fr);
}

/* One scrollport prevents nested scrolling from clipping the speaking pair. */
@media (max-width: 767px) {
    #nameScreen:not([hidden]) :is(#stepName, #stepAvatar, #stepGoal, #stepLang, #stepLangLevel):not([hidden]) {
        overflow-y: visible;
        overscroll-behavior-y: auto;
    }

    #nameScreen[data-onboarding-obstructed]:not([hidden]) {
        bottom: var(--onboarding-bottom-clearance);
        margin-block: 0;
    }

    #nameScreen[data-onboarding-obstructed]:not([hidden]):has(#stepName:not([hidden])) {
        min-height: calc(100svh - var(--onboarding-bottom-clearance));
    }
}


/* Preserve the existing absolute Back control and reserve its measured height. */
@media (min-width: 768px) and (max-width: 1439px) {
    #nameScreen:not([hidden]) #stepLangLevel:not([hidden]) {
        height: max-content;
        min-height: 100%;
        padding-bottom: calc(var(--onboarding-level-actions-height, 0px) + 36px);
    }
}

/* Existing onboarding controls; avatar spacing uses normal flow. */


/* Avatar guide responsive layout v2 */

/* Mobile and tablet: avatar carousel first, companion below it. */
#nameScreen:not([hidden]) #stepAvatar:not([hidden]) {
    justify-content: flex-start;
    gap: clamp(6px, 1svh, 12px);
    padding-top: max(20px, env(safe-area-inset-top));
    height: calc(100% - var(--onboarding-avatar-cookie-clearance, 0px));
    min-height: 0;
    overflow-y: auto;
}

/* Prioritize selection before the speaking pair at every screen width. */
#nameScreen #stepAvatar .onboarding-avatar-guide:not([hidden]) {
    position: relative;
    inset: auto;
    order: 2;
    width: min(100%, 420px);
    margin: 12px auto;
}

#stepAvatar .avatar-arrow {
    display: inline-flex;
}

#stepAvatar .avatar-title {
    flex: 0 0 auto;
    margin-bottom: 0;
}

#stepAvatar .avatar-picker-shell {
    order: 1;
    flex: 0 0 auto;
    height: clamp(165px, 25svh, 220px);
    min-height: 0;
    margin: 0;
}

#stepAvatar .avatar-picker-shell .avatar-picker {
    --avatar-size: clamp(155px, 25svh, 215px);
    height: 100%;
    min-height: 0;
}

@media (min-width: 1100px) {
    /* Wide desktop: use the free space beside the center panel. */
    #stepAvatar .avatar-picker-shell {
        height: clamp(210px, 29svh, 265px);
    }

    #stepAvatar .avatar-picker-shell .avatar-picker {
        --avatar-size: clamp(190px, 25svh, 245px);
    }}

/* Avatar guide mobile speaking pair v4 */
@media (max-width: 767px) {
    #nameScreen #stepAvatar:not([hidden]) {
        gap: 6px;
    }

    #stepAvatar .avatar-title {
        position: relative;
        z-index: 3;
        flex: 0 0 auto;
        margin: 0 auto 2px;
    }

    #stepAvatar .avatar-picker-shell {
        flex: 0 0 auto;
        height: clamp(160px, 22svh, 190px);
        min-height: 0;
        margin: 0;
    }

    #stepAvatar .avatar-picker-shell .avatar-picker {
        --avatar-size: clamp(150px, 21svh, 185px);
        height: 100%;
        min-height: 0;
    }}

@media (max-width: 767px) and (max-height: 700px) {
    #stepAvatar .avatar-picker-shell {
        height: 155px;
    }

    #stepAvatar .avatar-picker-shell .avatar-picker {
        --avatar-size: 150px;
    }}

/* Language companion responsive layout v4 */

/*
 * One responsive coordinate system for the character and bubble.
 * No viewport-based offsets or transform translations.
 * The pair reserves its own space before the action buttons.
 */
@media (max-width: 767px) {
    #nameScreen #stepLang:not([hidden]) {
        overflow-y: auto;
        overscroll-behavior-y: contain;
    }

    #stepLang .step-actions {
        position: relative;
        inset: auto;
        margin-top: 10px;
        flex-shrink: 0;
    }}

@media (max-width: 767px) {
    /* Allow short screens to scroll rather than overlap controls. */
    #nameScreen #stepLangLevel:not([hidden]) {
        overflow-y: auto;
        overscroll-behavior-y: contain;
    }

    /* Replace the old absolute back-button placement. */
    #stepLangLevel .step-actions {
        position: relative;
        inset: auto;
        margin: 12px auto 18px;
        padding: 0;
        flex: 0 0 auto;
    }}


/* Mobile: larger, readable guide above the name card. */
@media (max-width: 767px) {
    #nameScreen:has(#stepName:not([hidden])) {
        overflow-y: auto;
        overscroll-behavior-y: contain;
    }}
/* WORDYKID_MOBILE_GUIDE_TUNING_END */

/* Name onboarding: prevent moving CTA from losing pointer clicks. */
#nameScreen #nextToAvatarBtn.btn-start.is-ready {
    animation: none !important;
}

/* WORDYKID_MOBILE_LANGUAGE_STICKY_CTA_V1 */
@media (max-width: 767px) {

    /*
     * Preserve the existing language-selection eligibility.
     * The disabled state is owned by home.js.
     */
    #stepLang #saveNameBtn:disabled {
        display: none !important;
    }

    /*
     * Keep the action visible after a valid selection,
     * without moving the selected languages or companion.
     */
    #nameScreen:not([hidden])
    #stepLang:not([hidden])
    #saveNameBtn:not(:disabled) {
        display: block;
        position: fixed;
        top: auto;
        right: 16px;
        bottom: max(16px, env(safe-area-inset-bottom));
        left: 16px;
        z-index: 100;
        width: min(calc(100vw - 32px), 420px);
        min-height: 54px;
        margin: 0 auto;
        padding: 12px 18px;
        box-sizing: border-box;
        animation: none !important;
        transform: none;
        box-shadow:
            0 12px 28px rgba(58, 27, 108, 0.25),
            0 0 0 3px rgba(255, 255, 255, 0.85);
    }

    /*
     * Reserve scroll space so the fixed CTA cannot
     * permanently obscure language choices or the guide.
     */
    #nameScreen:not([hidden])
    #stepLang:not([hidden]) {
        padding-bottom: calc(
            100px + env(safe-area-inset-bottom)
        );
        scroll-padding-bottom: calc(
            100px + env(safe-area-inset-bottom)
        );
    }

    /*
     * Keep the original Back action in normal flow.
     */
    #stepLang .step-actions {
        position: relative;
        inset: auto;
    }}


/* WORDYKID_MODERN_LANGUAGE_CTA_2027 */

#nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled) {
    color: #ffffff;
    background: linear-gradient(
        112deg,
        #5146e8 0%,
        #9443ef 48%,
        #ed3da4 100%
    );
    border: 2px solid rgba(255, 255, 255, 0.92);
    border-radius: 22px;
    font-weight: 900;
    text-shadow: 0 1px 3px rgba(45, 20, 95, 0.26);
    box-shadow:
        0 14px 30px rgba(77, 49, 180, 0.38),
        0 0 0 1px rgba(150, 80, 255, 0.2),
        inset 0 2px 0 rgba(255, 255, 255, 0.42);
    overflow: hidden;
    animation: wkModernCtaGlow 2.8s ease-in-out infinite !important;
    transition: filter 0.2s ease, transform 0.2s ease;
}

#nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled)::before {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(
        110deg,
        transparent 30%,
        rgba(255, 255, 255, 0.38) 50%,
        transparent 70%
    );
    background-size: 220% 100%;
    animation: wkModernCtaShine 4.2s ease-in-out infinite;
}

@keyframes wkModernCtaGlow {
    0%, 100% {
        box-shadow:
            0 14px 30px rgba(77, 49, 180, 0.38),
            0 0 0 1px rgba(150, 80, 255, 0.2),
            inset 0 2px 0 rgba(255, 255, 255, 0.42);
    }
    50% {
        box-shadow:
            0 18px 38px rgba(124, 58, 237, 0.55),
            0 0 0 5px rgba(190, 110, 255, 0.18),
            inset 0 2px 0 rgba(255, 255, 255, 0.5);
    }
}

@keyframes wkModernCtaShine {
    0%, 18% {
        background-position: 180% 0;
    }
    55%, 100% {
        background-position: -100% 0;
    }
}

#nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled):hover {
    filter: brightness(1.08);
}

#nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled):active {
    transform: scale(0.985);
}

#nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled):focus-visible {
    outline: 3px solid #ffffff;
    outline-offset: 4px;
}

@media (prefers-reduced-motion: reduce) {
    #nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled) {
        animation: none !important;
        transition: none !important;
    }

    #nameScreen:not([hidden]) #stepLang:not([hidden]) #saveNameBtn.is-ready:not(:disabled)::before {
        animation: none !important;
        opacity: 0;
    }}

/* WORDYKID_COMPACT_COMPANION_HIDE_V2 */
/*
 * Keep onboarding companions on wide desktops only.
 * This selector outranks older !important display rules.
 * Applies to initial setup and settings re-entry.
 */
@media (max-width: 1439px) {
    body #nameScreen:not([hidden])
    :is(#stepName, #stepAvatar, #stepGoal, #stepLang, #stepLangLevel):not([hidden])
    .onboarding-avatar-guide:not([hidden]),
    body #nameScreen:not([hidden])
    #nameWelcomeGuide:not([hidden]) {
        display: none !important;
    }
}
