/*
 * Mobile/tablet scroll affordance for the active Grammar activities.
 *
 * Grammar slides already own their vertical overflow. This layer only makes
 * that existing scroll area visible on compact/coarse-pointer devices and
 * keeps the desktop presentation unchanged.
 */

html,
body {
    overscroll-behavior: none;
}

/* Editable sequence formulas can have long bilingual labels and any number
 * of components. Wrap whole operator/card pairs, never crop their text. These
 * selectors only apply to upgraded theory slides, not sentence exercises. */
[data-grammar-explanation] {
    min-height: 0;
    justify-content: safe center !important;
    overflow-y: auto !important;
}

[data-grammar-explanation] > div {
    flex-shrink: 0;
    min-width: 0;
}

[data-grammar-formula-layout="sequence"] {
    flex-wrap: wrap !important;
    gap: 1rem 0.75rem;
}

[data-grammar-formula-layout="sequence"] > .lingobao-grammar-formula-part {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    width: max-content;
    min-width: 0;
    max-width: 100%;
    margin: 0 !important;
    flex: 0 1 auto;
}

.lingobao-grammar-formula-part > div:not(:last-child) {
    flex: 0 0 auto;
}

.lingobao-grammar-formula-part > div:last-child {
    min-width: 0;
    max-width: 100%;
    flex-shrink: 1;
}

.lingobao-grammar-formula-part > div:last-child > div {
    min-width: 0 !important;
    max-width: 100%;
    box-sizing: border-box;
    transform: none !important;
    rotate: none !important;
}

.lingobao-grammar-formula-part > div:last-child > div > div {
    white-space: normal;
    /* Size cards from complete words, not single letters. Only use an
     * emergency break when one word is wider than the whole viewport. */
    overflow-wrap: break-word;
    word-break: normal;
    line-height: 1.3;
}

[data-grammar-formula-layout="comparison"] > div {
    min-width: 0;
    overflow-wrap: break-word;
    word-break: normal;
}

[data-grammar-formula-layout="comparison"] > div > div {
    min-width: 0 !important;
    max-width: 100%;
    transform: none !important;
    rotate: none !important;
}

@media (max-width: 520px) {
    [data-grammar-explanation] {
        padding-inline: 1rem !important;
    }
    .lingobao-grammar-formula-part > div:last-child > div {
        padding-inline: 0.75rem !important;
    }
}

/*
 * The page shell is a column flexbox. Without min-height: 0, its flexible
 * stage grows to the slide's min-content height and the outer shell clips it;
 * the element marked overflow-y-auto then has nothing of its own to scroll.
 * Constrain that stage to the viewport so the existing slide scroller owns
 * the real overflow on mobile, tablet and short desktop viewports.
 */
#root > .h-screen > .flex-1 {
    min-height: 0 !important;
    overflow: hidden !important;
}

#root > .h-screen > .flex-1 > .cc {
    min-height: 0 !important;
}

/*
 * Live students must keep theory activation locked, but the transparent lock
 * cannot consume their local reading/scroll gestures. The companion script
 * still cancels clicks before they reach Grammar controls.
 */
#lingobao-theory-lock {
    pointer-events: none !important;
}

/* Keep the native scrolling behavior, but use one consistent visual control. */
.overflow-y-auto {
    overflow-y: auto !important;
    overscroll-behavior-y: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none !important;
}

.overflow-y-auto::-webkit-scrollbar {
    display: none !important;
    width: 0 !important;
    height: 0 !important;
}

.lingobao-grammar-scroll-track {
    position: fixed;
    z-index: 80;
    display: none;
    width: 12px;
    min-height: 3rem;
    border-radius: 999px;
    background: rgba(99, 102, 241, 0.16);
    box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.18);
    cursor: pointer;
    touch-action: none;
    pointer-events: auto !important;
    user-select: none;
    -webkit-user-select: none;
}

.lingobao-grammar-scroll-track.is-visible {
    display: block;
}

.lingobao-grammar-scroll-thumb {
    position: absolute;
    top: 0;
    right: 0;
    left: 0;
    min-height: 2.75rem;
    border: 2px solid transparent;
    border-radius: 999px;
    background: #6366f1;
    background-clip: padding-box;
    box-shadow: 0 2px 8px rgba(79, 70, 229, 0.35);
}

.lingobao-grammar-scroll-track:hover .lingobao-grammar-scroll-thumb,
.lingobao-grammar-scroll-track:focus-visible .lingobao-grammar-scroll-thumb,
.lingobao-grammar-scroll-track.is-dragging .lingobao-grammar-scroll-thumb {
    background-color: #4338ca;
}

.lingobao-grammar-scroll-track:focus-visible {
    outline: 3px solid rgba(99, 102, 241, 0.3);
    outline-offset: 2px;
}

@media (max-width: 520px) {
    .lingobao-grammar-scroll-track {
        width: 10px;
    }

    /*
     * Compact rule formulas used by the six legacy Grammar theory pages.
     * Their desktop rows contain three or four padded cards plus operators;
     * shrink those intrinsic widths without changing the tablet/desktop view.
     */
    .grammar-rule-panel {
        margin-block: 0.5rem !important;
        padding: 0.75rem !important;
        border-radius: 2rem !important;
    }

    .grammar-rule-formula {
        flex-direction: column !important;
        gap: clamp(0.125rem, 1vw, 0.375rem) !important;
    }

    .grammar-rule-formula > :nth-child(even) {
        flex: 0 0 auto !important;
        font-size: clamp(1rem, 5vw, 1.375rem) !important;
    }

    .grammar-rule-formula--wrapped > :nth-child(odd),
    .grammar-rule-formula--direct > :nth-child(odd) {
        min-width: 0 !important;
        width: min(100%, 18rem) !important;
        flex: 0 0 auto !important;
    }

    .grammar-rule-formula--wrapped > :nth-child(odd) > div,
    .grammar-rule-formula--direct > :nth-child(odd) {
        min-width: 0 !important;
        width: 100% !important;
        padding: 0.5rem 0.2rem !important;
        font-size: clamp(0.875rem, 4vw, 1rem) !important;
        letter-spacing: normal !important;
    }

    .grammar-rule-formula--particle > :nth-child(3) {
        width: auto !important;
        flex: 0 0 auto !important;
    }

    .grammar-rule-formula--particle > :nth-child(3) > div {
        width: clamp(3.5rem, 18vw, 4.5rem) !important;
        height: clamp(3.5rem, 18vw, 4.5rem) !important;
        padding: 0.35rem !important;
    }

    .grammar-rule-formula--particle > :nth-child(3) .font-hanzi {
        font-size: clamp(1.65rem, 8vw, 2.25rem) !important;
    }

}

@media (max-width: 900px) {
    .grammar-rule-description {
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: clip !important;
        overflow-wrap: anywhere;
    }
}
