/* Mall 9 owns these rules. The shared plugin only loads the active Mall's frontend.php.
 * Keep one native introduction and one native Services list for every card count.
 * :has() counts the rendered cards, so later editor/AI content edits reflow without
 * rerunning generation or copying rules into a Custom CSS field.
 */
@container page-container (min-width: 992px) {
    body.sibly-mall-9 .row-section.template-1:has(> .ssb-row > .ssb-col > .ssb-item > .services.template-1 > .content-container > .services-grid) {
        --mall9-service-column-gap: calc(var(--section-flexbox-desktop-columnGap, 1.875) * 1rem);
        --mall9-service-row-gap: calc(var(--section-flexbox-desktop-rowGap, 1.875) * 1rem);
        --mall9-service-columns: 3;

        & > .ssb-row {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: var(--mall9-service-row-gap) var(--mall9-service-column-gap);
            align-items: stretch;
        }

        & > .ssb-row > .ssb-col {
            grid-column: 1 / -1;
            min-width: 0;
        }

        /* CSS owns track widths here; native content editing stays available. */
        & > .ssb-row > .ssb-row-divider {
            display: none;
        }

        & > .ssb-row > .ssb-col > .ssb-item > .custom-block.template-1 {
            width: 100%;

            & > .custom-block-content {
                min-height: 0;
                flex-wrap: nowrap;
                align-items: stretch;
            }

            & .custom-block-title-text,
            & .custom-block-desc-text,
            & .eyebrow-text {
                text-align: center;
                overflow-wrap: anywhere;
            }

            & .eyebrow {
                margin-inline: auto;
            }
        }

        & .services.template-1 > .content-container > .services-grid {
            display: grid;
            grid-template-columns: repeat(var(--mall9-service-columns), minmax(0, 1fr));
            gap: var(--mall9-service-row-gap) var(--mall9-service-column-gap);
            align-items: stretch;
        }

        & .services-grid > .services-card {
            width: 100%;
            min-width: 0;
            max-width: none;
        }

        /* 1, 2, 4: introduction in track one; two card tracks on its right.
         * Using the same three tracks as the other counts keeps card widths equal.
         */
        &:has(.services-grid > .services-card:only-child),
        &:has(.services-grid > .services-card:nth-child(2):last-child),
        &:has(.services-grid > .services-card:nth-child(4):last-child) {
            --mall9-service-columns: 2;

            & > .ssb-row > .ssb-col:has(> .ssb-item > .custom-block.template-1) {
                grid-column: 1;
            }

            & > .ssb-row > .ssb-col:has(> .ssb-item > .services.template-1) {
                grid-column: 2 / -1;
            }
        }

        /* 5: the introduction is the first cell of one three-column grid.
         * Flatten layout wrappers only, retaining the real headings, links,
         * native service list and its normal editable data bindings in the DOM.
         */
        &:has(.services-grid > .services-card:nth-child(5):last-child) {
            & > .ssb-row > .ssb-col,
            & > .ssb-row > .ssb-col > .ssb-item,
            & > .ssb-row > .ssb-col > .ssb-item > .services.template-1,
            & > .ssb-row > .ssb-col > .ssb-item > .services.template-1 > .content-container,
            & > .ssb-row > .ssb-col > .ssb-item > .services.template-1 > .content-container > .services-grid {
                display: contents;
            }

            & > .ssb-row > .ssb-col > .ssb-col-addbar {
                display: none;
            }
        }

        /* Side introductions and the five-card title cell retain start alignment. */
        &:has(.services-grid > .services-card:only-child),
        &:has(.services-grid > .services-card:nth-child(2):last-child),
        &:has(.services-grid > .services-card:nth-child(4):last-child),
        &:has(.services-grid > .services-card:nth-child(5):last-child) {
            & > .ssb-row > .ssb-col > .ssb-item > .custom-block.template-1 {
                & .custom-block-title-text,
                & .custom-block-desc-text,
                & .eyebrow-text {
                    text-align: start;
                }

                & .eyebrow {
                    margin-inline: 0 auto;
                }
            }
        }
    }
}
