/* =========================
   WORKS
========================= */

.works {
    --works-stage-frame: var(--space-24);

    position: relative;

    min-height: 100svh;

    overflow: hidden;
}

.works__inner {
    min-height: 100svh;

    display: grid;
    grid-template-columns: 1fr 640px 1fr;
    align-items: center;

    padding-inline: var(--gutter-stage);
}


/* =========================
   SIDE LABELS
========================= */

.works__project-name,
.works__all {
    margin: 0;

    font-family: var(--font-mono);
    font-size: min(var(--font-body-desktop), 1.95vh);
    font-weight: var(--weight-regular);
    line-height: 1.2;

    color: var(--color-text);
    text-decoration: none;
}

.works__project-name {
    justify-self: start;

    /*
     * The 680px card stack is centered on the 640px
     * middle column, so the front card reaches 20px
     * into this column. On narrow Desktop screens a
     * long project name wraps before it reaches the
     * card, keeping a 24px gap.
     */
    max-width: calc(100% - (680px - 640px) / 2 - var(--space-24));

    transition:
        opacity 250ms ease,
        color 180ms ease;
}

.works__project-name:hover {
    opacity: 0.6;
}

.works__project-name:active {
    opacity: 1;
    color: #000000;
}

.works__all {
    justify-self: end;

    transition:
        opacity 250ms ease,
        color 180ms ease;
}

.works__all:hover {
    opacity: 0.6;
}

.works__all:active {
    opacity: 1;
    color: #000000;
}


/* =========================
   SLIDER
========================= */

.works__slider {
    width: 640px;

    display: flex;
    flex-direction: column;
    align-items: center;
}


/* =========================
   COUNTER
========================= */

.works__counter {
    margin: 0 0 var(--works-stage-frame);

    font-family: var(--font-mono);
    font-size: min(var(--font-label-desktop), 1.56svh);
    font-weight: var(--weight-regular);
    line-height: 1;

    color: var(--color-text);
}


/* =========================
   PREVIOUS / NEXT
========================= */

/*
 * The arrows on either side of the count switch
 * projects without dragging (js/works-slider.js
 * shows them). They take the counter's type, size
 * and colour on every breakpoint. The ::before
 * grows the hit area to about 28 x 38px without
 * moving anything.
 */

.works__control {
    position: relative;

    margin: 0;
    padding: 0;

    border: 0;
    background: none;

    font: inherit;
    line-height: inherit;
    color: inherit;

    cursor: pointer;

    transition:
        opacity 250ms ease,
        color 180ms ease;
}

.works__control[hidden] {
    display: none;
}

.works__control--prev {
    margin-right: 1.5ch;
}

.works__control--next {
    margin-left: 1.5ch;
}

.works__control::before {
    content: "";

    position: absolute;
    inset: -12px -10px;
}

@media (hover: hover) {
    .works__control:hover {
        opacity: 0.6;
    }
}

.works__control:active {
    opacity: 1;
    color: #000000;
}


/* =========================
   IMAGE STACK
========================= */

.works__stack {
    position: relative;

    width: 680px;
    height: calc((680px * 9 / 16) + 105px);

    flex-shrink: 0;
}


/* Every slide */

.works__slide {
    position: absolute;

    left: 0;
    bottom: 0;

    width: 100%;

    /*
     * Every depth keeps the front card's shape
     * (640 x 382.5): back cards are the same card,
     * smaller. So a card moving forward only
     * scales, and its image keeps the same crop.
     */
    height: auto;
    aspect-ratio: 640 / 382.5;

    display: block;

    object-fit: cover;

    /* Centred, unless the card sets its own focus */
    object-position: var(--works-slide-focus, center);

    -webkit-user-drag: none;
    user-select: none;
    -webkit-user-select: none;

    cursor: grab;
}

.works__slide:active {
    cursor: grabbing;
}


/* =========================
   STACK POSITIONS
========================= */

/* Front card */

.works__slide--1 {
    z-index: 4;

    width: 640px;

    left: 0;
    top: 105px;
}


/* Second card */

.works__slide--2 {
    z-index: 3;

    width: 590px;

    left: 25px;
    top: 65px;
}


/* Third card */

.works__slide--3 {
    z-index: 2;

    width: 540px;

    left: 50px;
    top: 32px;
}


/* Back card */

.works__slide--4,
.works__slide--5,
.works__slide--6 {
    z-index: 1;

    width: 480px;

    left: 80px;
    top: 0;
}


/*
 * Cards waiting for their turn: exactly under
 * the back card, so the stack still shows four.
 * When the back card moves forward, the next one
 * is already in its place.
 */

.works__slide--5,
.works__slide--6 {
    z-index: 0;
}


/* =========================
   HINT
========================= */

.works__hint {
    position: relative;
    z-index: 10;

    margin: var(--works-stage-frame) 0 0;

    font-family: var(--font-mono);
    font-size: min(var(--font-label-desktop), 1.56svh);
    font-weight: var(--weight-regular);
    line-height: 1;

    color: var(--color-text);

    text-align: center;
}
/* =========================
   WORKS — STICKY HOLD
========================= */

.works {
    height: 150svh;
    min-height: 150svh;

    overflow: visible;
}

.works__inner {
    position: sticky;
    top: 0;

    height: 100svh;
    min-height: 100svh;

    overflow: hidden;
}

/* =========================
   WORKS — DESKTOP
========================= */

@media (min-width: 1025px) {

    /*
     * Works is the next panel in the stack. It
     * rises over the pinned Works Intro stage
     * through a -100svh overlap into the cover
     * screen Works Intro reserves for it.
     */

    main > .works {
        margin-top: -100svh;
    }

    .works {
        z-index: 5;

        background-color: var(--color-background);

        border-radius: 16px 16px 0 0;

        box-shadow:
            0 -40px 40px -28px rgba(33, 33, 33, 0.045);
    }
}

/* =========================
   WORKS — TABLET
========================= */

@media (min-width: 768px) and (max-width: 1024.98px) {

    /*
     * Works is the next panel in the stack. It
     * rises over the pinned Works Intro stage
     * through a -100svh overlap into the cover
     * screen Works Intro reserves for it.
     */

    main > .works {
        margin-top: -100svh;
    }

    .works {
        /*
         * Height-aware stage. The composition keeps its
         * 15svh offset and 64px stack-to-row frame
         * whenever the bottom row fits with 8px to spare.
         * On shorter screens the offset gives way first,
         * until the name row sits 16px under the 72px
         * nav, and only then the frame, down to 24px.
         * The stack never changes size.
         *
         * 7% and 12% are the name and stack anchors,
         * 464px the stack and 21.6px the "All projects"
         * line (18px x 1.2).
         */

        --works-clear-top: calc(72px + var(--space-16));
        --works-clear-bottom: var(--space-8);

        --works-stage-frame: clamp(
            var(--space-24),
            95svh - var(--works-clear-top) - 464px - 21.6px -
                var(--works-clear-bottom),
            var(--space-64)
        );

        --works-shift: clamp(
            var(--works-clear-top) - 7svh,
            88svh - 464px - var(--works-stage-frame) - 21.6px -
                var(--works-clear-bottom),
            15svh
        );

        height: 150svh;
        min-height: 150svh;

        z-index: 5;

        background-color: var(--color-background);

        border-radius: 16px 16px 0 0;

        box-shadow:
            0 -40px 40px -28px rgba(33, 33, 33, 0.045);
    }

    .works__inner {
        position: sticky;
        top: 0;

        width: 100%;
        height: 100svh;
        min-height: 100svh;

        display: block;

        overflow: hidden;
    }


    /* =========================
       PROJECT NAME
    ========================= */

    .works__project-name {
        position: absolute;

        top: 7%;
        left: var(--gutter-stage);

        font-size: 18px;
        line-height: 1.2;

        z-index: 10;

        transform: translateY(var(--works-shift));
    }


    /* =========================
       SLIDER AREA
    ========================= */

    .works__slider {
        position: relative;

        width: 100%;
        height: 100%;

        display: block;

        transform: translateY(var(--works-shift));
    }


    /* Counter */

    .works__counter {
        position: absolute;

        top: 7%;
        right: 0;

        margin: 0;

        font-size: var(--font-label-tablet);
        line-height: 1.2;
    }


    /* =========================
       IMAGE STACK
    ========================= */

    .works__stack {
        position: absolute;

        top: 12%;
        left: 50%;

        width: 448px;
        height: 464px;

        transform: translateX(-50%);
    }


    .works__slide {
        height: auto;
        aspect-ratio: 4 / 3;

        object-fit: cover;
    }


    /* Front */

    .works__slide--1 {
        width: 448px;

        left: 0;
        top: 128px;
    }


    /* Second */

    .works__slide--2 {
        width: 382px;

        left: 33px;
        top: 80px;
    }


    /* Third */

    .works__slide--3 {
        width: 295px;

        left: 76.5px;
        top: 40px;
    }


    /* Back, and the cards waiting under it */

    .works__slide--4,
    .works__slide--5,
    .works__slide--6 {
        width: 199px;

        left: 124.5px;
        top: 0;
    }


    /* =========================
       BOTTOM LABELS
    ========================= */

    /*
     * The hint and "All projects" keep their 64%
     * line, but never come closer to the stack than
     * the stage frame: on short screens they move
     * down to the bottom of the stack (12% + 464px)
     * plus 64px instead of covering the front card.
     */

    .works__hint {
        position: absolute;

        left: 0;
        top: max(
            64%,
            calc(12% + 464px + var(--works-stage-frame))
        );
        bottom: auto;

        margin: 0;

        font-size: var(--font-label-tablet);
        line-height: 1.2;

        text-align: left;
    }

    .works__all {
        position: absolute;

        right: var(--gutter-stage);
        top: max(
            64%,
            calc(12% + 464px + var(--works-stage-frame))
        );
        bottom: auto;

        font-size: 18px;
        line-height: 1.2;

        z-index: 10;

        transform: translateY(var(--works-shift));
    }
}

/* =========================
   WORKS — MOBILE
========================= */

@media (max-width: 767.98px) {

    /*
     * Works is the next panel in the stack.
     *
     * It rises over Works Intro the same way Works
     * Intro rises over About: a -100svh overlap into
     * the cover screen Works Intro reserves for it.
     * The Works Intro composition stays pinned
     * underneath until Works docks.
     */

    main > .works {
        margin-top: -100svh;
    }


    .works {
        /*
         * Height-aware stage, as on Tablet. Whenever the
         * bottom row would come closer than 8px to the
         * bottom of the stage, the whole composition
         * lifts, until the name row sits 16px under the
         * 64px nav, and only then the stack-to-row frame
         * shrinks, down to 16px. The stack never changes
         * size.
         *
         * 23% and 28.5% are the name and stack anchors,
         * 19.2px the "All projects" line (16px x 1.2).
         */

        --works-clear-top: calc(64px + var(--space-16));
        --works-clear-bottom: var(--space-8);
        --works-stack-height: calc((100vw - 40px) * 0.75 + 100px);

        --works-stage-frame: clamp(
            var(--space-16),
            94.5svh - var(--works-clear-top) -
                var(--works-stack-height) - 19.2px -
                var(--works-clear-bottom),
            var(--space-32)
        );

        --works-lift: clamp(
            0px,
            var(--works-stack-height) + var(--works-stage-frame) +
                19.2px + var(--works-clear-bottom) - 71.5svh,
            23svh - var(--works-clear-top)
        );

        height: 150svh;
        min-height: 150svh;

        z-index: 5;

        background-color: var(--color-background);

        border-radius: 16px 16px 0 0;

        box-shadow:
            0 -40px 40px -28px rgba(33, 33, 33, 0.045);
    }

    .works__inner {
        position: sticky;
        top: 0;

        width: 100%;
        height: 100svh;
        min-height: 100svh;

        display: block;

        overflow: hidden;
    }


    /* =========================
       PROJECT NAME
    ========================= */

    .works__project-name {
        position: absolute;

        top: calc(23% - var(--works-lift));
        left: var(--gutter-stage);

        font-size: 16px;
        line-height: 1.2;

        z-index: 10;
    }


    /* =========================
       SLIDER AREA
    ========================= */

    .works__slider {
        position: relative;

        width: 100%;
        height: 100%;

        display: block;
    }


    /* Counter */

    /*
     * The counter and the hint use the Mobile label
     * size (13px) from 360px up. Narrower screens
     * scale them down to 12px at 320px, where the
     * 13px hint would run into "All projects".
     */

    .works__counter {
        position: absolute;

        top: calc(23% - var(--works-lift));
        right: 0;

        margin: 0;

        font-size: clamp(
            12px,
            calc(2.5vw + 4px),
            var(--font-label-mobile)
        );
        line-height: 1.2;
    }


    /* =========================
       IMAGE STACK
    ========================= */

    .works__stack {
        position: absolute;

        top: calc(28.5% - var(--works-lift));
        left: 50%;

        width: calc(100vw - 40px);
        height: calc((100vw - 40px) * 0.75 + 100px);

        transform: translateX(-50%);

        flex-shrink: 0;
    }


    .works__slide {
        height: auto;

        aspect-ratio: 4 / 3;

        object-fit: cover;
        object-position: var(--works-slide-focus, center);
    }


    /* Front */

    .works__slide--1 {
        width: 100%;

        left: 0;
        top: 100px;
    }


    /* Second */

    .works__slide--2 {
        width: calc(100% - 40px);

        left: 20px;
        top: 64px;
    }


    /* Third */

    .works__slide--3 {
        width: calc(100% - 80px);

        left: 40px;
        top: 32px;
    }


    /* Back, and the cards waiting under it */

    .works__slide--4,
    .works__slide--5,
    .works__slide--6 {
        width: calc(100% - 120px);

        left: 60px;
        top: 0;
    }


    /* =========================
       BOTTOM LABELS
    ========================= */

    /*
     * The hint and "All projects" keep their 75%
     * line, but never come closer to the stack than
     * the stage frame: on short screens they move
     * down to the bottom of the stack (28.5% plus
     * its height) plus 32px instead of covering the
     * front card.
     */

    .works__hint {
        position: absolute;

        left: 0;
        top: calc(
            max(
                75%,
                calc(
                    28.5% + (100vw - 40px) * 0.75 + 100px +
                    var(--works-stage-frame)
                )
            ) - var(--works-lift)
        );

        margin: 0;

        font-size: clamp(
            12px,
            calc(2.5vw + 4px),
            var(--font-label-mobile)
        );
        line-height: 1.2;

        text-align: left;
    }


    .works__all {
        position: absolute;

        right: var(--gutter-stage);
        top: calc(
            max(
                75%,
                calc(
                    28.5% + (100vw - 40px) * 0.75 + 100px +
                    var(--works-stage-frame)
                )
            ) - var(--works-lift)
        );

        font-size: 16px;
        line-height: 1.2;

        z-index: 10;
    }
}

/* =========================
   WORKS — LARGE TABLET (PORTRAIT)
========================= */

@media (min-width: 1025px) and (max-width: 1199px) and (orientation: portrait) {

    /*
     * Large tablets in portrait take the Tablet Works
     * composition, at Desktop type and card sizes and
     * on the Desktop 7.5vw side line: one group of
     * three rows (project name and counter, the card
     * stack, hint and link), 64px above and below the
     * cards.
     *
     * The group is centered where Tablet centers it
     * (its middle at 51.5% of the screen), and the
     * sticky stage ends at the bottom of the group
     * instead of at the bottom of the screen. Contact
     * then follows Works in the normal flow, as on
     * every other breakpoint, without the empty end of
     * a full-screen stage in between. The stage still
     * holds for 50svh.
     */

    .works {
        --works-label-row: 24px;
        --works-stage-frame: var(--space-64);
        --works-stack-height: calc((680px * 9 / 16) + 105px);
        --works-group-height: calc(
            2 * (var(--works-label-row) + var(--works-stage-frame)) +
            var(--works-stack-height)
        );
        --works-stage-height: calc(51.5svh + var(--works-group-height) / 2);

        height: calc(var(--works-stage-height) + 50svh);
        min-height: calc(var(--works-stage-height) + 50svh);
    }

    .works__inner {
        height: var(--works-stage-height);
        min-height: var(--works-stage-height);

        display: grid;
        grid-template-columns: 1fr 1fr;
        grid-template-areas:
            "name  counter"
            "stack stack"
            "hint  all";
        align-content: end;
        row-gap: var(--works-stage-frame);
    }

    /* The counter, the stack and the hint join the grid */

    .works__slider {
        display: contents;
    }

    .works__project-name {
        grid-area: name;
        justify-self: start;

        position: relative;
        z-index: 10;
    }

    /*
     * The counter and the hint take the 24px line box
     * of the project name and the link (20px x 1.2),
     * so each label row shares one baseline.
     */

    .works__counter {
        grid-area: counter;
        justify-self: end;

        margin: 0;

        line-height: 1.5;
    }

    /*
     * The stack box matches its front card (640px)
     * instead of the Desktop 680px, so the cards are
     * centered on the screen. Card sizes are unchanged.
     */

    .works__stack {
        grid-area: stack;
        justify-self: center;

        width: 640px;
    }

    .works__hint {
        grid-area: hint;
        justify-self: start;

        margin: 0;

        line-height: 1.5;

        text-align: left;
    }

    .works__all {
        grid-area: all;
        justify-self: end;

        position: relative;
        z-index: 10;
    }
}

/* =========================
   WORKS — PHONE LANDSCAPE
========================= */

/*
 * A phone held sideways has far less height than
 * the Tablet or Mobile stage, and the fixed-size
 * stack ran off the bottom of the pinned stage, with
 * the hint and "All projects" never shown. Here the
 * stack takes its size from the height under the nav
 * and keeps its four-card composition, and the four
 * labels stay in their corners, level with the top
 * and the bottom of the stack.
 */

@media (max-width: 1024.98px) and (orientation: landscape) and (max-height: 500px) {

    .works {
        --works-shift: 0px;
        --works-lift: 0px;

        --works-landscape-stack: calc(
            100svh - var(--works-clear-top) - var(--space-16)
        );
    }

    .works__project-name,
    .works__counter {
        top: var(--works-clear-top);
    }

    .works__stack {
        top: var(--works-clear-top);

        width: calc(
            var(--works-landscape-stack) * var(--works-stack-ratio)
        );
        height: var(--works-landscape-stack);
    }

    .works__hint,
    .works__all {
        top: auto;
        bottom: var(--space-16);
    }
}

/* =========================
   WORKS — PHONE LANDSCAPE, TABLET WIDTHS
========================= */

/*
 * The Tablet composition (448 x 464 stack) in
 * percentages of the stack, so it keeps its shape
 * at the landscape size.
 */

@media (min-width: 768px) and (max-width: 1024.98px) and (orientation: landscape) and (max-height: 500px) {

    .works {
        --works-row: 21.6px;
        --works-stack-ratio: calc(448 / 464);
    }

    .works__slide--1 {
        width: 100%;

        left: 0;
        top: 27.586%;
    }

    .works__slide--2 {
        width: 85.268%;

        left: 7.366%;
        top: 17.241%;
    }

    .works__slide--3 {
        width: 65.848%;

        left: 17.076%;
        top: 8.621%;
    }

    .works__slide--4,
    .works__slide--5,
    .works__slide--6 {
        width: 44.42%;

        left: 27.79%;
        top: 0;
    }
}

/* =========================
   WORKS — PHONE LANDSCAPE, MOBILE WIDTHS
========================= */

/*
 * The Mobile composition as laid out at 393px
 * (353 x 364.75 stack), in percentages of the
 * stack.
 */

@media (max-width: 767.98px) and (orientation: landscape) and (max-height: 500px) {

    .works {
        --works-row: 19.2px;
        --works-stack-ratio: calc(353 / 364.75);
    }

    .works__slide--1 {
        width: 100%;

        left: 0;
        top: 27.416%;
    }

    .works__slide--2 {
        width: 88.669%;

        left: 5.666%;
        top: 17.546%;
    }

    .works__slide--3 {
        width: 77.337%;

        left: 11.331%;
        top: 8.773%;
    }

    .works__slide--4,
    .works__slide--5,
    .works__slide--6 {
        width: 66.006%;

        left: 16.997%;
        top: 0;
    }
}
