/*
 * The page as a band, in 4/4 time.
 * Each element plays a different part, and every duration is a whole
 * number of bars or beats so the parts stay in time.
 * 0.5s = 120 BPM. For another tempo use 60 / BPM seconds.
 * Uses `translate`, `rotate` and `scale` so any existing `transform` is left alone.
 */

:root {
    --beat: 0.5s;
    --bar: calc(var(--beat) * 4);
}

@media (prefers-reduced-motion: no-preference) {
    /* Kick drum: beats 1 and 3, with a pickup before 3 */
    body>header h1 {
        transform-origin: bottom center;
        animation: kick var(--bar) infinite;
    }

    /* Snare: backbeat on 2 and 4 */
    figure img {
        animation: snare var(--bar) infinite;
    }

    /* Hi-hat: quick ticks on every half beat, accented on the beat */
    aside img {
        animation: hihat var(--beat) infinite;
    }

    /* Melody: a two-bar phrase that rises, then resolves */
    article h2 {
        animation: melody calc(var(--bar) * 2) infinite;
    }

    /* Call and response: second heading answers one bar later */
    article:nth-of-type(even) h2 {
        animation-delay: calc(var(--bar) * -1);
    }

    /* Bass: slow, heavy sway across two bars */
    blockquote {
        animation: bass calc(var(--bar) * 2) ease-in-out infinite;
    }

    blockquote:nth-of-type(even) {
        animation-direction: reverse;
    }

    /* Crash cymbal: one accent every four bars */
    iframe {
        animation: crash calc(var(--bar) * 4) infinite;
    }

    /* Hold still while being read, clicked or watched */
    body>header h1:hover,
    aside img:hover,
    article h2:hover,
    figure img:hover,
    blockquote:hover,
    iframe:hover,
    iframe:focus {
        animation-play-state: paused;
    }
}

/* Percentages within one bar: beats fall at 0, 25, 50, 75 */
@keyframes kick {
    0% { scale: 1.12 0.86; }
    12%, 40% { scale: 1; }
    43% { scale: 1.05 0.94; }
    47% { scale: 1; }
    50% { scale: 1.12 0.86; }
    62%, 95% { scale: 1; }
    100% { scale: 1.12 0.86; }
}

@keyframes snare {
    0%, 23% { translate: 0 0; rotate: 0deg; }
    25% { translate: 0 -8px; rotate: -1.5deg; }
    35%, 73% { translate: 0 0; rotate: 0deg; }
    75% { translate: 0 -8px; rotate: 1.5deg; }
    85%, 100% { translate: 0 0; rotate: 0deg; }
}

@keyframes hihat {
    0% { translate: 0 -5px; }
    25% { translate: 0 0; }
    50% { translate: 0 -2px; }
    75%, 100% { translate: 0 0; }
}

/* Eight notes, one per beat: higher pitch means higher position */
@keyframes melody {
    0%, 8% { translate: 0 0; }
    12.5%, 20% { translate: 6px -5px; }
    25%, 33% { translate: 12px -10px; }
    37.5%, 45% { translate: 6px -5px; }
    50%, 58% { translate: 14px -14px; }
    62.5%, 70% { translate: 10px -9px; }
    75%, 83% { translate: 4px -4px; }
    87.5%, 100% { translate: 0 0; }
}

@keyframes bass {
    0%, 100% { translate: -10px 0; rotate: -1deg; }
    25%, 75% { translate: 0 5px; rotate: 0deg; }
    50% { translate: 10px 0; rotate: 1deg; }
}

@keyframes crash {
    0% { scale: 1.07; rotate: 1.5deg; }
    3% { rotate: -1deg; }
    6% { rotate: 0.5deg; }
    12%, 100% { scale: 1; rotate: 0deg; }
}
