/* The body is the scrollport below the fixed, full-width navbar. Keeping the
   existing content hierarchy preserves page layouts and fixed dialogs. */
html.page-scroll-shell {
    --page-scroll-owner: body;
    height: 100%;
    overflow: hidden;
}

html.page-scroll-shell > body[data-ad-layout] {
    height: var(--page-available-height);
    min-height: 0;
    margin: var(--navbar-footprint) 0 0;
    overflow-x: hidden;
    overflow-y: auto;
    /* The fixed navbar paints above this scrollport. An isolated scrolling
       body can clip its paint in iOS WebKit while leaving buttons clickable.
       Keep the root stacking context so the bar can paint outside the body. */
    isolation: auto;
}

html.page-scroll-shell > body > .navbar {
    position: fixed;
    left: 0;
    right: 0;
    width: 100% !important;
    margin: 0 !important;
}

html.page-scroll-shell > body[data-ad-layout].report-modal-open { overflow-y: hidden; }

/* Saved free-position ads use document coordinates, including the navbar. */
.page-scroll-shell .ad-layout-layer { top: calc(-1 * var(--navbar-footprint)); }

/* Sticky rails now stick within the area below the navbar. */
.page-scroll-shell .side-ad { top: 4px; }
.page-scroll-shell .responsive-ads-shell > :is([data-ad-slot="left"], [data-ad-slot="right"]) { top: 22px; }

/* CSS estimates paint immediately; the navbar observer refines its footprint. */
@media (max-width: 800px) {
    html.page-scroll-shell { --navbar-footprint: 64px; }
}
@media (max-width: 600px) {
    html.page-scroll-shell {
        --navbar-footprint: calc(clamp(58px, 14.5vw, 68px) + env(safe-area-inset-top, 0px));
    }
}

/* Let Safari composite real page content behind its bottom toolbar rather
   than a nested scroller. Keep the same scroll owner through rotation. */
html.page-scroll-shell:has(> body.page-home) {
    --page-scroll-owner: root;
    height: auto;
    overflow-x: clip;
    overflow-y: auto;
}
html.page-scroll-shell > body[data-ad-layout].page-home {
    height: auto;
    min-height: var(--page-available-height);
    overflow: visible;
}

/* All mobile pages share the homepage's native scrolling, so Safari can
   composite the content beneath its controls and retain pull-to-refresh. */
@media (max-width: 800px), (hover: none) and (pointer: coarse) {
    html.page-scroll-shell {
        --page-scroll-owner: root;
        height: auto;
        overflow-x: clip;
        overflow-y: auto;
    }
    html.page-scroll-shell > body[data-ad-layout] {
        height: auto;
        min-height: var(--page-available-height);
        overflow: visible;
    }
    .page-scroll-shell .side-ad { top: calc(var(--navbar-footprint) + 4px); }
    .page-scroll-shell .responsive-ads-shell > :is([data-ad-slot="left"], [data-ad-slot="right"]) {
        top: calc(var(--navbar-footprint) + 22px);
    }
}

/* Lock the actual viewport while a dialog is open without changing which
   element owns the scroll position used by history and layout tools. */
html.page-scroll-shell:has(> body.report-modal-open),
html.page-scroll-shell.profile-modal-open {
    overflow-y: hidden;
}
html.page-scroll-shell.profile-modal-open > body[data-ad-layout] {
    overflow-y: hidden;
}
/* In native mode only the root is locked. Clipping the body's overflow as
   well can shorten the document and jump the saved scroll position. */
html.page-scroll-shell > body[data-ad-layout].page-home.report-modal-open,
html.page-scroll-shell.profile-modal-open > body[data-ad-layout].page-home {
    overflow: visible;
}
@media (max-width: 800px), (hover: none) and (pointer: coarse) {
    html.page-scroll-shell > body[data-ad-layout].report-modal-open,
    html.page-scroll-shell.profile-modal-open > body[data-ad-layout] {
        overflow: visible;
    }
}
