/*
 * SG Colure, the studio's seal. Markup and the story of the piece are in
 * templates/components/UiSgColure.html; this file is its choreography.
 *
 * Timings are the original prototype's, unchanged:
 *   arrival   rings fade in over 1s or 2s; the gates turn a quarter and part
 *             over 3s; the first logo fades in after 1s
 *   idle      every ring turns once a minute, neighbouring layers in opposite
 *             directions
 *   click     each layer spins against its idle direction for 1s and fades,
 *             outside in, at 0 / 400 / 700 / 900 / 1000ms; the first logo
 *             fades at 2s; the second fades in at 4s; done at 5s
 *
 * "First logo" is .sgc__logo--idle and "second" is .sgc__logo--open. Which
 * image plays which part is decided in the markup.
 */
@layer components {
    .sgc {
        --sgc-size: min(600px, 78vmin);
        position: relative;
        width: var(--sgc-size);
        height: var(--sgc-size);
        flex: none;
        cursor: pointer;
        border-radius: 50%;
        -webkit-tap-highlight-color: transparent;
        user-select: none;
        /* The ring images are squares; as they turn, their (empty) corners
           swing outside this box and would widen the page on a phone. */
        overflow: clip;
    }

    .sgc:focus-visible {
        outline-offset: 8px;
        border-radius: 50%;
    }

    .sgc.is-playing {
        cursor: default;
    }

    .sgc__orbit,
    .sgc__ring,
    .sgc__gate {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        max-width: none;
        pointer-events: none;
    }

    /* idle: one turn a minute */
    .sgc__orbit {
        animation: sgc-turn-cw 60s linear infinite;
    }

    .sgc__orbit--ccw {
        animation-direction: reverse;
    }

    /* arrival */
    .sgc__ring {
        opacity: 0;
        animation: sgc-in var(--fade, 1s) ease forwards;
    }

    .sgc__gate--left {
        animation: sgc-gate-left 3s linear forwards;
    }

    .sgc__gate--right {
        animation: sgc-gate-right 3s linear forwards;
    }

    .sgc__logo {
        position: absolute;
        left: 50%;
        top: 50%;
        height: auto;
        max-width: none;
        translate: -50% -50%;
        opacity: 0;
        pointer-events: none;
        /* a one-pixel pale edge, so the mark's dark outline reads on a dark page */
        filter: drop-shadow(1px 0 0 var(--sapphire-pale)) drop-shadow(-1px 0 0 var(--sapphire-pale))
            drop-shadow(0 1px 0 var(--sapphire-pale)) drop-shadow(0 -1px 0 var(--sapphire-pale));
    }

    /* sizes: each image at whole-pixel scale when the seal is its full 600px */
    .sgc__logo--wordmark {
        width: 41.667%; /* 250px, its drawn size; it fits inside the inner ring */
    }

    .sgc__logo--mark {
        width: 18.5%; /* 111px, its drawn size */
    }

    /* standing alone after the rings have gone, the mark is shown at twice
       its drawn size */
    .sgc__logo--open.sgc__logo--mark {
        width: 37%;
    }

    /* parts: the first logo arrives with the rings */
    .sgc__logo--idle {
        animation: sgc-in 1s ease 1s forwards;
    }

    /* click: the rings leave, outside in */
    .sgc.is-playing .sgc__ring {
        animation:
            sgc-spin 1s linear var(--wait, 0ms) both,
            sgc-out 1s ease var(--wait, 0ms) both;
    }

    .sgc.is-playing .sgc__ring[data-ease] {
        animation-timing-function: ease, ease;
    }

    .sgc.is-playing .sgc__gate {
        animation: none;
        opacity: 0;
    }

    .sgc.is-playing .sgc__logo--idle {
        animation: sgc-out 1s ease 2s both;
    }

    .sgc.is-playing .sgc__logo--open {
        animation: sgc-in 1s ease 4s both;
    }

    /* open: the end of the sequence, held */
    .sgc.is-open .sgc__ring,
    .sgc.is-open .sgc__logo--idle {
        animation: none;
        opacity: 0;
    }

    .sgc.is-open .sgc__logo--open {
        animation: none;
        opacity: 1;
    }

    @keyframes sgc-turn-cw {
        to {
            transform: rotate(360deg);
        }
    }

    @keyframes sgc-spin {
        to {
            transform: rotate(var(--turn, -360deg));
        }
    }

    @keyframes sgc-in {
        from {
            opacity: 0;
        }
        to {
            opacity: 1;
        }
    }

    @keyframes sgc-out {
        from {
            opacity: 1;
        }
        to {
            opacity: 0;
        }
    }

    @keyframes sgc-gate-left {
        0% {
            transform: rotate(0deg) translateY(0);
            opacity: 1;
        }
        25% {
            transform: rotate(-90deg) translateY(0);
            opacity: 1;
        }
        50%,
        100% {
            transform: rotate(-90deg) translateY(-16.667%);
            opacity: 0;
        }
    }

    @keyframes sgc-gate-right {
        0% {
            transform: rotate(0deg) translateY(0);
            opacity: 1;
        }
        25% {
            transform: rotate(-90deg) translateY(0);
            opacity: 1;
        }
        50%,
        100% {
            transform: rotate(-90deg) translateY(16.667%);
            opacity: 0;
        }
    }
}

/* The line that invites a first-time visitor to open the seal (.sgc-hint,
   placed by the page). Gone for good once they have: see sgcolure.js. */
html.seal-known .sgc-hint {
    display: none;
}

/* Opened on this visit: the line fades and stays away, but keeps its place,
   so nothing around the seal moves. */
html.seal-played .sgc-hint {
    opacity: 0;
    transition: opacity 0.4s ease;
}

/* With reduced motion the rings stand still and a click cross-fades straight
   to the second logo (the script skips the wait). */
@media (prefers-reduced-motion: reduce) {
    .sgc__orbit {
        animation: none;
    }

    .sgc__gate {
        display: none;
    }
}
