/*
 * The board: a small Hresh board that plays itself. Six pieces hold the left,
 * the Hresh keep coming from the right, the tree stands in the middle, and
 * every so often the King takes his turn as the game shows it. Used on the
 * grigoriangames.com home page; hresh.com borrows .unit for its own board.
 * Script: static/js/ui/skirmish.js.
 *
 *   <p class="hud"><span>Kills <b data-skirmish-kills>000</b></span></p>
 *   <div class="board" data-skirmish style="--tiles:url('...');--fight:url('...')">
 *       <img class="px board__tree" ...>                                   the tree
 *       <img class="px unit" style="--c:2;--r:1;--d:0ms" ...>            a piece
 *       <img class="px unit unit--foe" style="--c:6;--r:1;--d:80ms" ...> a Hresh
 *       <img class="px unit" data-fighter ...> / <img class="... unit--foe" data-fighter ...>
 *                                     the King and the Hresh the fight frames show
 *       <div class="board__shade"></div>
 *       <div class="board__fight"></div>
 *   </div>
 *
 * --c / --r are the unit's column and row; --d staggers its idle bob. The hud
 * and the board share a parent, which is how the script finds the counter.
 */
@layer components {
    /* wave and kill counters above the board */
    .hud {
        display: flex;
        justify-content: space-between;
        font-family: var(--font-pixel);
        font-size: 8px;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        color: var(--dim);
    }

    .hud b {
        font-weight: 400;
        color: var(--gold);
    }

    .hud b.is-bumped {
        animation: hud-bump 500ms steps(4, end);
    }

    @keyframes hud-bump {
        0% {
            color: #fff;
            text-shadow: 0 0 10px var(--gold);
        }
    }

    /* The board: a grid of square tiles with a unit standing on some of them.
       --tile is always the 64px art divided by a whole number, so sprites
       land on device pixels. The tiles (--tiles, set on the board) are the
       game's own, cut from the same art as the tree and the fight frames, so
       those sit on them without a seam. */
    .board {
        --tile: 64px;
        --cols: 8;
        --rows: 4;
        position: relative;
        display: grid;
        grid-template-columns: repeat(var(--cols), var(--tile));
        grid-template-rows: repeat(var(--rows), var(--tile));
        background: #0d1b2a var(--tiles, none) 0 0 / calc(var(--tile) * 2) calc(var(--tile) * 2);
        image-rendering: pixelated;
        box-shadow:
            0 calc(var(--px) * -1) 0 0 var(--line-strong),
            0 var(--px) 0 0 var(--line-strong),
            calc(var(--px) * -1) 0 0 0 var(--line-strong),
            var(--px) 0 0 0 var(--line-strong),
            0 30px 60px -30px rgb(0 0 0 / 0.9);
        overflow-x: clip; /* units marching on appear at the board's edge */
    }

    /* The tree in the middle, drawn with the two tiles under it. The units
       come after it in the markup, so a piece lunging past is drawn over it. */
    .board__tree {
        grid-column: 4 / span 2;
        grid-row: 2 / span 2;
        width: calc(var(--tile) * 2);
        height: calc(var(--tile) * 2);
    }

    .unit {
        grid-column: var(--c);
        grid-row: var(--r);
        width: var(--tile);
        height: var(--tile);
        animation: unit-idle 1.2s steps(2, jump-none) var(--d, 0ms) infinite;
        filter: drop-shadow(0 3px 0 rgb(0 0 0 / 0.45));
    }

    .unit--foe {
        scale: -1 1; /* face the pieces */
    }

    @keyframes unit-idle {
        50% {
            translate: 0 calc(var(--tile) / -32);
        }
    }

    /* Both sides wait off the board and march on, in steps, once the board
       itself is in view (the script marks it .is-staged). transform is
       applied after the foes' mirror, so the same value sends each side to
       its own edge. Where the browser can tie an animation to scrolling, the
       rule further down replaces this with a march the scrollbar drives. */
    html.js .board .unit {
        transition:
            transform 640ms steps(8, end) var(--d, 0ms),
            opacity 240ms linear var(--d, 0ms);
    }

    html.js .board.is-armed:not(.is-staged) .unit {
        transform: translateX(calc(var(--tile) * -3));
        opacity: 0;
    }

    /* a number that floats up from a unit when it is struck */
    .board__dmg {
        grid-column: var(--c);
        grid-row: var(--r);
        z-index: 2;
        justify-self: center;
        align-self: start;
        font-family: var(--font-pixel);
        font-size: calc(var(--tile) / 6.4);
        line-height: 1;
        color: var(--gold-hi);
        text-shadow:
            1px 1px 0 #000,
            -1px 1px 0 #000;
        pointer-events: none;
        animation: dmg-rise 700ms steps(7, end) forwards;
    }

    .board__dmg--hero {
        color: #ff8c7a;
    }

    @keyframes dmg-rise {
        to {
            translate: 0 calc(var(--tile) / -2);
            opacity: 0;
        }
    }

    /* The King's turn. The board goes dark, as in the game, and the fight
       plays across the four tiles from the King's to the Hresh's, the tree
       between them. The frames carry their own tiles and tree. */
    .board__shade {
        grid-column: 1 / -1;
        grid-row: 1 / -1;
        z-index: 4;
        background: rgb(4 8 16 / 0.84);
        opacity: 0;
        pointer-events: none;
        transition: opacity 220ms ease;
    }

    /* sixteen 256x128 frames side by side; --fight is set on the board. Kept
       in the page, not display:none, so the frames are loaded before the
       first fight. */
    .board__fight {
        grid-column: 3 / span 4;
        grid-row: 2 / span 2;
        z-index: 5;
        background: var(--fight) 0 0 / calc(var(--tile) * 64) 100% no-repeat;
        opacity: 0;
        pointer-events: none;
        transition: opacity 220ms ease;
    }

    .board.is-fighting .board__shade,
    .board.is-fighting .board__fight {
        opacity: 1;
    }

    .board__fight.is-playing {
        animation: board-fight 1.6s steps(16, jump-none) forwards;
    }

    @keyframes board-fight {
        to {
            background-position-x: calc(var(--tile) * -60);
        }
    }
}

/* The board's two sides march on as it rises into view, where the browser can
   tie an animation to scrolling and the visitor has not asked for less
   motion. Elsewhere the class-based march above is used. */
@media (prefers-reduced-motion: no-preference) {
    @supports (animation-timeline: view()) {
        @layer components {
            /* the further the board has scrolled in, the further they have
               come; in place by about a third of the way up the screen */
            .board {
                view-timeline: --board block;
            }

            html.js .board .unit,
            html.js .board.is-armed:not(.is-staged) .unit {
                transition: none;
                transform: none;
                opacity: 1;
                animation:
                    unit-idle 1.2s steps(2, jump-none) var(--d, 0ms) infinite,
                    unit-march steps(12, end) both;
                animation-timeline: auto, --board;
                animation-range: normal, entry 30% cover 42%;
            }

            @keyframes unit-march {
                from {
                    transform: translateX(calc(var(--tile) * -4));
                    opacity: 0;
                }
                35% {
                    opacity: 1;
                }
                to {
                    transform: none;
                    opacity: 1;
                }
            }
        }
    }
}

/* eight 64px tiles need 512px plus the card's padding and the page gutters */
@media (max-width: 640px) {
    @layer components {
        .board {
            --tile: 32px;
        }
    }
}
