/* ==========================================================================
   Overlay Styles
   ========================================================================== */

.overlay-backdrop {
    background-color: rgba(var(--app-ground-rgb), 0.5);
    backdrop-filter: blur(3px);
    position: fixed;
    left: 0;
    top: 0;
    z-index: 200;
    width: 100%;
    /* `dvh`, not `vh`: on iOS `100vh` is the largest viewport and taller than
       what is on screen, which pushes the bottom of an overlay past the edge
       (#541). The `vh` line stays as the fallback. */
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    animation-name: overlay-fade-in-1;
    animation-duration: 0.3s;
    animation-iteration-count: 1;
    animation-timing-function: ease-in-out;
    padding: 0.5rem;
}

/* The page behind an open overlay does not scroll. Tied to the backdrop being
   in the DOM, not to a class set and cleared in JS, so an overlay removed some
   other way cannot leave the page stuck; one that is already fading out lets go
   straight away. The body's `overflow` reaches the viewport, and the reserved
   gutter keeps a classic scrollbar's width, so the page does not jump sideways. */
body:has(> .overlay-backdrop:not(.fade-out, .swipe-down)) {
    overflow: hidden;
}

html:has(> body > .overlay-backdrop:not(.fade-out, .swipe-down)) {
    scrollbar-gutter: stable;
}

.overlay-backdrop.clear-background {
    backdrop-filter: none;
}

.overlay-backdrop.swipe-down {
    animation-name: overlay-fade-out-1;
    animation-duration: 0.25s;
    animation-fill-mode: forwards;
}

.overlay-backdrop.swipe-down .overlay {
    animation-name: overlay-swipe-down;
    animation-duration: 0.25s;
    animation-fill-mode: forwards;
    animation-timing-function: ease-out;
}

.overlay-backdrop.fade-out {
    animation-name: overlay-fade-out-1;
    animation-duration: 0.2s;
    animation-fill-mode: forwards;
}

.overlay-backdrop.fade-out .overlay {
    animation-name: overlay-fade-out-2;
    animation-duration: 0.2s;
    animation-fill-mode: forwards;
}

/* Overlay Container */
.overlay {
    max-width: min(100%, 1100px);
    max-height: calc(100vh - 1rem);
    max-height: calc(100dvh - 1rem);
    overflow-y: auto;
    overflow-x: hidden;
    width: fit-content;
    min-width: min(500px, 100%);
    margin: auto;
    animation-name: overlay-fade-in-2;
    animation-duration: 0.5s;
    animation-iteration-count: 1;
    animation-timing-function: ease-in-out;
}

@media screen and (max-width: 768px) {
    .overlay .card-body {
        padding: 1rem 1.5rem;
    }

    .overlay-header {
        padding: 1rem 1.5rem;
    }

    /* A `wide-on-mobile` table is pulled out of the card around it so its rows
       run edge to edge — by 2rem, the side padding of a page card. An overlay
       body has 1.5rem, so that pull put the table 0.5rem past the card on both
       sides, where `.overlay { overflow-x: hidden }` cut it off together with
       most of the padding of the first and last column: the player's name sat
       on the edge of the card. Pull it out by this card's own padding instead. */
    .overlay table.app-table.wide-on-mobile {
        margin: 0 -1.5rem;
        width: calc(100% + 3rem);
    }
}

.overlay-backdrop::after {
    content: "";
    display: block;
    height: env(safe-area-inset-bottom, 0);
    flex-shrink: 0;
    width: 100%;
}

.overlay.small {
    max-width: 480px;
}

/* The account dialog is a form, not a page: at the full overlay width its rows
   of buttons and inputs stretched across a desktop screen (#652). The card's
   own `min-width` keeps it from getting narrow. */
.overlay.account-overlay {
    max-width: min(100%, 560px);
}

.confirm-dialog-message {
    font-size: 1.25rem;
    line-height: 1.4;
    margin-bottom: 0;
}

.overlay-header {
    position: sticky;
    top: 0;
    z-index: 11;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: rgba(var(--app-surface-rgb), 0.8);
    backdrop-filter: blur(10px);
}

/* Title block, optional slot (`headerContent`) and close button share one row.
   The title keeps its intrinsic width, the slot takes what is left. */
.overlay-header__title {
    flex: 0 1 auto;
    min-width: 0;
}

.overlay-header__slot {
    flex: 1 1 auto;
    min-width: 0;
    margin: 0 3rem 0 0.75rem;
}

/* The X itself is a `CloseButton` since #706 — it brings its own padding,
   pointer and focus ring. What is left here is where it sits in the header
   row. */
.overlay-close-btn {
    flex-shrink: 0;
}

.overlay table.app-table {
    margin: 0;
    width: 100%;
}

/* Used for the game details modal */
.overlay .left {
    display: block;
    text-align: left;
    margin-bottom: 1rem;
}

.overlay .right {
    display: block;
    text-align: left;
    margin-bottom: 1rem;
    width: 70%;
    margin-left: 30%;
}

/* Search overlay */
.search-overlay {
    max-width: 500px;
}

/* Scrollable results container */
.search-results-scroll {
    max-height: 400px;
    overflow-y: auto;
}

/* Comment scroll container */
.comment-scroll {
    max-height: 300px;
    overflow-y: auto;
}

/* League standing history chart container */
.standing-history-chart-scroll {
    width: calc(100% + 1rem);
    overflow-x: auto;
    margin: 0 -0.5rem;
}

@media (max-width: 576px) {
    .standing-history-chart-scroll {
        width: calc(100% + 4rem);
        margin: 0 -2rem;
    }
}

.standing-history-chart-container {
    position: relative;
    min-width: 500px;
    height: min(65vh, 520px);
    height: min(65dvh, 520px);
}

/* --------------------------------------------------------------------------
 * Page overlays (#577)
 *
 * Pages without a tab of their own — a foreign team, a manager profile — open
 * as a near-fullscreen sheet on top of the current page instead of replacing
 * it. Wider and taller than a normal overlay card, because they carry a whole
 * page; the header with the close button stays sticky at the top.
 * -------------------------------------------------------------------------- */
.page-overlay-card {
    width: 100%;
    max-width: min(100%, 1100px);
    /* A whole page needs the room, so the sheet takes what the backdrop gives
       it. `dvh` for the same reason as the backdrop above. */
    height: calc(100vh - 1rem);
    height: calc(100dvh - 1rem);
    max-height: none;
}

/* Stacked overlays: every backdrop — page overlay, dialog, confirm sheet —
   keeps the single `z-index: 200` of `.overlay-backdrop`, so the one opened
   last is on top simply because it is last in the DOM. A ladder here (210 /
   220 / 230 for page overlays) broke that: a confirm dialog opened *out of* a
   team overlay stayed at 200 and disappeared behind it. Overlays must stack in
   the order they were opened, whatever kind they are. Toasts sit at 300 and
   stay above all of them. */

@media screen and (min-width: 769px) {
    .page-overlay-card {
        height: auto;
        min-height: min(70vh, 100%);
        min-height: min(70dvh, 100%);
        max-height: calc(100vh - 2rem);
        max-height: calc(100dvh - 2rem);
    }
}

/* --------------------------------------------------------------------------
 * Pending state for the element that opened an overlay (#578).
 *
 * From the tap until the overlay actually shows content, the clicked element
 * itself pulses — feedback stays where the user is already looking instead of
 * a full-screen spinner. Further clicks are swallowed so a slow request cannot
 * be fired twice.
 * -------------------------------------------------------------------------- */
.is-opening-overlay {
    animation: opening-overlay-pulse 900ms ease-in-out infinite;
    pointer-events: none;
    border-radius: inherit;
}

@keyframes opening-overlay-pulse {
    0%, 100% {
        opacity: 1;
        box-shadow: 0 0 0 0 rgba(var(--bs-info-rgb), 0);
    }
    50% {
        opacity: 0.5;
        box-shadow: 0 0 0 0.35rem rgba(var(--bs-info-rgb), 0.25);
    }
}

/* Table rows drop `box-shadow` in several engines, so they pulse on their
 * background instead — same rhythm, so both read as one animation. */
tr.is-opening-overlay {
    animation: opening-overlay-row-pulse 900ms ease-in-out infinite;
}

@keyframes opening-overlay-row-pulse {
    0%, 100% {
        opacity: 1;
        background-color: transparent;
    }
    50% {
        opacity: 0.5;
        background-color: rgba(var(--bs-info-rgb), 0.2);
    }
}

@media (prefers-reduced-motion: reduce) {
    .is-opening-overlay,
    tr.is-opening-overlay {
        animation: none;
        opacity: 0.5;
    }
}
