/* ============================================================
   SANDBOX — /sandbox/ (archive-sandbox.php, sandbox.js)
   ============================================================
   A viewport composition (no document scroll), from the 1728 × 1174
   mockup:

       header row
       ── --sb-top ──────────────  139px  (11.9%)
       STAGE  (one active image)   747px  (the rest)
       ── --sb-gap ──────────────   70px  (6%)
       INDEX  (thumbnail strip)     66px
       ── --sb-bottom ───────────  151px  (12.85%) — footer inside

   The whitespace scales with the viewport height (with minimums that
   keep the × pill and the footer clear), so short laptops keep a
   large image instead of the same fixed margins.

   MEDIA SIZING — every image keeps its own ratio, never cropped. Rule
   ("equal area"): all images get the same visual area, the area at
   which a 4:5 image exactly fills the stage height (the mockup). Then
   each dimension is capped by the stage. Square → 0.894 × stage height,
   landscapes wider and lower, portraits taller than 4:5 all reach the
   stage height and get narrower. Per image (CSS, from --r = w/h and
   --sr = √r written by PHP from the attachment metadata):
       width = min(k · H · √r,  H · r,  W)      k = √0.8
   H / W = the stage (a size container, so cqh / cqw). Horizontally the
   stage is columns 3–10 (symmetric about the axis), the cap for very
   wide images; the image is centred on the axis like the mockup's.
   The thumbnails use the same rule at 66px — miniatures of the stage.
   ============================================================ */

body.post-type-archive-sandbox {
    overflow: hidden;
    overscroll-behavior: none;
}

/* Footer fixed to the viewport bottom, as on Home (same place, so
   Home ↔ Sandbox never moves it). */
body.post-type-archive-sandbox .site-footer {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    pointer-events: none;
}

body.post-type-archive-sandbox .site-footer__inner > * {
    pointer-events: auto;
}

.sb {
    --sb-k:          0.8944;   /* √0.8 — area reference: 4:5 fills the stage height */
    --sb-top:        max(88px, 11.9vh);
    --sb-top:        max(88px, 11.9dvh);
    --sb-gap:        max(32px, 6vh);
    --sb-gap:        max(32px, 6dvh);
    --sb-bottom:     max(80px, 12.85vh);
    --sb-bottom:     max(80px, 12.85dvh);
    --sb-thumb-h:    66px;
    --sb-thumb-max:  calc(var(--sb-thumb-h) * 2);
    --sb-thumb-gap:  28.5px;  /* mockup: 21 thumbnails span 1677.8px */
    --sb-cursor:     #D9D9D9;  /* = Single Work minimap indicator */

    display: grid;
    grid-template-rows: var(--sb-top) minmax(0, 1fr) var(--sb-gap) var(--sb-thumb-h) var(--sb-bottom);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

/* ============================================================
   STAGE
   ============================================================ */

.sb-stage-row {
    grid-row: 2;
    min-height: 0;
}

/* area: lines 3 → 11 (columns 3–10, symmetric about the axis) */
.sb-stage {
    grid-column: 3 / 11;
    position: relative;
    container-type: size;
    touch-action: pinch-zoom;   /* swipes reach sandbox.js; pinch stays native */
}

/* All slides share the stage; only the active one is shown. Box = the
   image's own ratio, centred (inset 0 + auto margins on a sized box). */
.sb-slide {
    position: absolute;
    inset: 0;
    margin: auto;
    width: min(var(--sb-k) * 100cqh * var(--sr), 100cqh * var(--r), 100cqw);
    height: auto;
    aspect-ratio: var(--r);
    visibility: hidden;
    opacity: 0;
}

.sb-slide.is-active {
    visibility: visible;
    opacity: 1;
}

.sb-slide__ph,
.sb-slide__img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: none;
    object-fit: cover;          /* no-op: the box already has the image's ratio */
}

/* Full image fades in over the placeholder when sandbox.js loaded it
   (the first image is in the markup and shows as soon as it arrives). */
.sb-slide__img {
    transition: opacity 0.5s ease;
}

.sb-slide__img.is-pending {
    opacity: 0;
}

/* Full image shown: the placeholder goes (after the fade) — it would
   otherwise show through transparent PNGs. */
.sb-slide.is-loaded .sb-slide__ph {
    visibility: hidden;
    transition: visibility 0s 0.5s;
}

/* ============================================================
   INDEX — an endless filmstrip under a FIXED cursor on the axis.
   The viewport clips the strip at the viewport edges; the track
   (copies of the end of the sequence, the list, copies of its
   start) slides under the cursor by a translateX that sandbox.js
   writes. Before sandbox.js runs (first paint, PJAX entrance) the
   track's left edge sits on the axis and CSS shifts it back by the
   leading copies and half the first thumbnail — the first thumbnail
   centred on the cursor, the state sandbox.js starts from.
   ============================================================ */

.sb-index {
    grid-row: 4;
    min-width: 0;
    position: relative;         /* the cursor */
}

.sb-index__viewport {
    position: relative;
    height: 100%;
    overflow: hidden;
    overflow: clip;             /* never scrollable (focus, find-in-page) */
    touch-action: pan-y pinch-zoom;   /* horizontal drags reach sandbox.js */
    -webkit-user-select: none;
    user-select: none;
}

.sb-index__track {
    --sb-w0: min(var(--sb-k) * var(--sb-thumb-h) * var(--sr0), var(--sb-thumb-h) * var(--r0), var(--sb-thumb-max));

    position: absolute;
    top: 0;
    left: 50%;
    display: flex;
    gap: var(--sb-thumb-gap);
    width: max-content;
    height: 100%;
    transform: translateX(calc(-1 * (var(--sb-thumb-h) * var(--sb-tail-f, 0) + var(--sb-tail-n, 0) * var(--sb-thumb-gap)) - var(--sb-w0) / 2));
    will-change: transform;
}

.sb-index__list,
.sb-index__run {
    display: flex;
    flex: none;
    align-items: center;
    gap: var(--sb-thumb-gap);
    height: 100%;
    list-style: none;
}

.sb-index__entry {
    display: flex;
    flex: none;
}

.sb-index__item {
    display: block;
    flex: none;
    width: min(var(--sb-k) * var(--sb-thumb-h) * var(--sr), var(--sb-thumb-h) * var(--r), var(--sb-thumb-max));
    aspect-ratio: var(--r);
    background-color: var(--color-ui-background);   /* until the thumbnail arrives */
}

.sb-index__item img {
    width: 100%;
    height: 100%;
    object-fit: cover;          /* no-op, same ratio */
    pointer-events: none;       /* drags move the strip, never the image */
    -webkit-user-drag: none;
}

.sb-index__item:focus-visible {
    outline: 1px solid var(--color-text-primary);
    outline-offset: 3px;
}

/* 2px line, 6px beyond the thumbnails above and below — the Single Work
   indicator turned vertical. Fixed on the axis (under the image and the
   ×), drawn over the thumbnail that slides beneath it. Outside the
   clipping viewport, so its overhang is never cut. */
.sb-index__cursor {
    position: absolute;
    top: -6px;
    bottom: -6px;
    left: 50%;
    width: 2px;
    margin-left: -1px;
    background-color: var(--sb-cursor);
    pointer-events: none;
}

/* ============================================================
   CLOSE × — contextual control, centred on the axis, at the
   Single Work controls height (header row + --sw-controls-drop).
   Same pill as the Single Work × (36px, as on mobile).
   ============================================================ */

.site-work-controls-layer--sandbox .sb-close {
    grid-column: 1 / -1;
    justify-self: center;
    width: 36px;
    padding: 0 0 1px;
    justify-content: center;
    pointer-events: auto;
}

/* Short desktop-width viewports (landscape phones, small windows): the
   fixed minimums would leave the image ~120px tall — tighter frame and
   smaller thumbnails, so the image keeps most of the height. */
@media (min-width: 769px) and (max-height: 600px) {
    .sb {
        --sb-top:     64px;    /* × pill bottom (48px) + 16px */
        --sb-gap:     20px;
        --sb-thumb-h: 44px;
        --sb-bottom:  60px;    /* footer row (≈47px) + air */
    }
}

/* ============================================================
   MOBILE (≤768px) — one column on the mobile inset, no footer;
   the × floats at the bottom centre like the Single Work controls
   (single-work.css moves the layer), the index sits above it.
   ============================================================ */

@media (max-width: 768px) {

    .sb {
        --sb-top:    calc(var(--header-inset-top) + var(--pill-height) + 36px);
        --sb-gap:    28px;
        --sb-bottom: calc(var(--sw-nav-bottom) + var(--pill-height) + 28px);
    }

    .sb-stage-row {
        display: block;
        padding: 0 var(--page-inset-x);
    }

    .sb-stage {
        height: 100%;
    }

    .site-work-controls-layer--sandbox .sb-close {
        position: relative;
    }

    .site-work-controls-layer--sandbox .sb-close::after {
        content: '';
        position: absolute;
        inset: -10px -4px;
    }

    /* No hover on touch: thumbnails never keep a tap highlight. */
    .sb-index__item {
        -webkit-tap-highlight-color: transparent;
    }
}
