/**
 * Warehouse scene: "Näin varasto toimii". A grid of pallet slots seen from
 * above plays the four steps of the warehouse (goods arrive, are stored, an
 * order is picked, parcels leave), driven by the scroll position. On a tall
 * enough screen the section pins to the screen until the steps have played.
 * Project block (PPL Logistiikka).
 *
 * globalClasses:
 *   warehouse-grid   an empty container; includes/blocks/warehouse-scene.php
 *                    prints the labels and the inline SVG into it on the
 *                    front end (the editor shows an empty box)
 *   pinned-scene     on the section (.color-section) that holds the grid
 *                    and a .step-cards row: the section becomes
 *                    100svh + 4 steps tall (plus its normal top and bottom
 *                    padding, so it sits in the page like any section
 *                    before and after the pin) and its inner container sticks.
 *                    Mechanics of the base theme's scroll-scene, but the
 *                    heading, the grid and all cards stay in the pin.
 *
 * Structure:
 *   .color-section.pinned-scene.gb-container.alignfull
 *     .gb-container (inner, max width: the pin)
 *       heading block
 *       .warehouse-grid.gb-container
 *       .step-cards.gb-container > .step-card x4
 *
 * State: js/blocks/warehouse-scene.js sets data-phase="0-4" on the grid and
 * .is-active / .is-done on the step cards. Without the script, with reduced
 * motion and before the grid is reached the grid shows phase 2.
 *
 * Variables:
 *   --pinned-scene-step   scroll per step, default 60svh
 *   --pinned-scene-top    pin offset (admin bar, sticky header)
 *
 * Chain animations: heading and cards as usual; never on the grid's shapes.
 */

.warehouse-grid {
    margin-bottom: clamp(20px, 3vw, 36px);
}

.warehouse-grid svg {
    display: block;
    width: 100%;
    height: auto;
    overflow: hidden;
}

.warehouse-grid-labels {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-family: var(--code-font);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.warehouse-slot {
    fill: var(--border-clr);
    transition: fill 0.4s;
}

.warehouse-pallet {
    fill: var(--heading-clr);
}

/* Arriving pallets: lane on the left, then their slots */
.warehouse-arrival {
    fill: var(--accent-clr);
    opacity: 0;
    transform: translate(var(--x0), var(--y0));
    transition: transform 0.9s cubic-bezier(0.2, 0.7, 0.2, 1) var(--d), opacity 0.3s var(--d), fill 0.5s 0.5s;
}

[data-phase="1"] .warehouse-arrival {
    opacity: 1;
    transform: translate(var(--x1), var(--y1));
}

[data-phase="2"] .warehouse-arrival,
[data-phase="3"] .warehouse-arrival,
[data-phase="4"] .warehouse-arrival {
    opacity: 1;
    transform: translate(var(--x2), var(--y2));
    fill: var(--heading-clr);
}

/* Picked parcels: appear in the aisle, then leave to the right */
.warehouse-parcel {
    fill: var(--pak-clr);
    opacity: 0;
    transform: translate(var(--x3), var(--y3)) scale(0.2);
    transform-box: fill-box;
    transform-origin: center;
    transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.2, 1) var(--d), opacity 0.3s var(--d);
}

[data-phase="3"] .warehouse-parcel {
    opacity: 1;
    transform: translate(var(--x3), var(--y3)) scale(1);
}

[data-phase="4"] .warehouse-parcel {
    opacity: 1;
    transform: translate(var(--x4), var(--y4)) scale(1);
    transition: transform 1.3s cubic-bezier(0.5, 0, 0.8, 0.4) var(--d), opacity 0.3s;
}

[data-phase="3"] .warehouse-slot--picked {
    fill: var(--accent-clr);
}

/* ---- Pinned scene ---- */
.pinned-scene {
    --pinned-scene-step: 60svh;
    --pinned-scene-top: var(--wp-admin--admin-bar--height, 0px);
}

:root:has(.navigation-stick:not(.auto-hide-sticky)) .pinned-scene {
    --pinned-scene-top: calc(var(--wp-admin--admin-bar--height, 0px) + var(--header-height, 0px));
}

@media (prefers-reduced-motion: no-preference) and (min-height: 560px) {
    .js .gb-container.color-section.pinned-scene {
        height: calc(100svh + 4 * var(--pinned-scene-step) + 2 * var(--section-padding-y));
    }

    .js .pinned-scene > .gb-container {
        position: sticky;
        top: var(--pinned-scene-top);
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        height: calc(100svh - var(--pinned-scene-top));
        padding-top: clamp(24px, 8svh, 64px);
        padding-bottom: 24px;
    }

    /* Phones: the grid and only the running step */
    .js .pinned-scene .step-card {
        display: none;
    }

    .js .pinned-scene .step-card.is-active,
    .js .pinned-scene .warehouse-grid[data-phase="0"] ~ .step-cards .step-card:first-child {
        display: grid;
    }
}

/* Desktop pins only when heading, grid and four cards fit */
@media (prefers-reduced-motion: no-preference) and (min-width: 990px) {
    .js .gb-container.color-section.pinned-scene {
        height: auto;
    }

    .js .pinned-scene > .gb-container {
        position: static;
        display: block;
        height: auto;
        padding-top: 0;
        padding-bottom: 0;
    }

    .js .pinned-scene .step-card {
        display: grid;
    }
}

@media (prefers-reduced-motion: no-preference) and (min-width: 990px) and (min-height: 740px) {
    .js .gb-container.color-section.pinned-scene {
        height: calc(100svh + 4 * var(--pinned-scene-step) + 2 * var(--section-padding-y));
    }

    .js .pinned-scene > .gb-container {
        position: sticky;
        display: flex;
        justify-content: center;
        height: calc(100svh - var(--pinned-scene-top));
        /* Room for the sticky header, which slides in over the pin when the
           visitor scrolls up */
        padding-top: calc(var(--header-height, 60px) + clamp(20px, 4svh, 48px));
        padding-bottom: clamp(32px, 6svh, 64px);
    }

    /* The pinned content is sized to the screen height, so there is air above
       the heading and below the cards on a laptop too */
    .js .pinned-scene h2 {
        font-size: clamp(2rem, 5.2svh, 3.6rem);
    }

    .js .pinned-scene .warehouse-grid {
        margin-bottom: clamp(16px, 3svh, 36px);
    }

    .js .pinned-scene .warehouse-grid svg {
        width: auto;
        max-width: 100%;
        max-height: 32svh;
        margin: 0 auto;
    }

    .js .pinned-scene .step-cards .step-card {
        padding: clamp(16px, 2.6svh, 24px);
    }
}

@media (prefers-reduced-motion: reduce) {
    .warehouse-slot,
    .warehouse-arrival,
    .warehouse-parcel {
        transition: none;
    }
}
