/* The rules every theme obeys (see themes/README.md): written once, here, in terms of what an element is and not of which
   theme it belongs to. The theme's own drawings and moving layers are in its folder (background.css, board.css). */

/* ----- while the table turns (body.flipping, a little longer than the .6s turn: startFlip in theme-core.js) -----
   Everything that moves on its own steps aside, so the phone's graphics chip only has the turn itself to do. A turning layer
   full of moving drawings is drawn into a surface of its own at every frame, and the glow under the player's box is
   repainted by the CPU at every frame: that, on top of the turn, is what made themed games stutter. Every moving layer of a
   theme is marked data-fx (scope.layer): it is hidden, keeps turning unseen, and fades back in after the turn. The glow and
   the DONE/ROLL nudge wait. The still sky (body::before) and the board turn as they always did. */
body.flipping [data-fx] {
  visibility: hidden;
}

body:not(.flipping) [data-fx] {
  animation: flipFadeIn .45s ease-out;
}

/* Every moving layer is a layer of the browser's own all the time, not only while it moves (see rule 6 in README.md): the layers
   a sky or a board makes and drops (a flash, a falling petal, a glint) change the page's layer tree each time, and what lies
   over them (the dice, the pieces, the home) was seen to vanish for a frame. A layer that is already there does not change when
   something starts moving on it. A theme gets this by making its layers with ctx.layer() (marked data-fx); one that makes an
   animated element some other way must write will-change: transform on it itself. */
[data-fx] {
  will-change: transform;
}

@keyframes flipFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

body.flipping .player.current::before,
body.flipping .button.done.nudge,
body.flipping .button.roll.nudge {
  animation-play-state: paused;
}

/* ----- the pieces' moving layers (themes/core/piece-fx.js): each theme's piece gives its own layers and movements ----- */
.piece.pfx-on {
  background-image: none !important; /* the layers draw the whole piece: its own drawing steps aside */
}

.pfx {
  position: absolute;
  inset: 10%; /* the size of the piece's drawing (80% of the tile, centred) */
  pointer-events: none;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  /* Always a layer of its own, not only while it moves: a layer that is made and dropped at every movement (about one every
     second late in a game, with twenty pieces) makes the browser rebuild its layers each time, and a piece, a lit tile's
     ring or a die was seen to blink for a frame then. Two small layers per piece, a few MB in all. */
  will-change: transform;
}

/* ----- what turns at the table, and how (FLIP, same table) -----
   The sky steps over in one go half way through the turn (the body::before and .bg-fx rules below), the board and its frame
   never turn (the board counter-turns the page), and the 25 symbols of the tiles turn smoothly with the page. (They turned in
   six steps for a while to spare the phone's graphics chip: measured on a phone it made no difference, and it looked bad.) */
.tile::before {
  transition: rotate .6s ease;
}

/* ----- the moving background: the host of the layers, one copy drawn, the effects off, the table turned ----- */
/* The still sky is the background picture; the rest is drawn once and moved by the GPU (transform and opacity only), in
   a layer of its own inside whatever shows the background (see syncBgFx in linotte.js). An animation inside the
   background picture would be redrawn by the CPU at every frame. A layer that drifts steps at a lower rate: it looks the
   same and wakes the screen less. */
.bg-fx {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  contain: paint;
}

body > .bg-fx {
  position: fixed;
  z-index: -1;
}

/* Only the copy on top is drawn (syncFxLive in linotte.js). */
.bg-fx:not(.live) {
  display: none;
}

/* Effects off (the options, a low battery, less motion): the skies stand still. */
body.fx-off .bg-fx > i {
  animation: none !important;
}

/* A picture that animates by itself (an animated GIF or WebP) cannot be paused from CSS, so a layer that is one carries .fx-anim and says in
   --fx-still which picture to show instead: it is shown while the effects are off (the same as above) and while a game is over or the
   sparks of a line play (body.fx-held, rule 9). A theme gets this by putting .fx-anim on the layer and --fx-still on its rule. */
body.fx-off .fx-anim,
body.fx-held .fx-anim {
  background-image: var(--fx-still);
}

/* Turned for the player on the other side (FLIP, same table, portrait): the whole background turns with the game, so
   for them the sky is above and what falls goes down. The picture is drawn again on a layer of its own (the body's own
   background can't turn), under the moving parts, which turn with it. */
@media (orientation: portrait) {
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    background: inherit;
    /* Not turned smoothly: a full-screen layer turning with the table (and the sky's layers under it, some of them several times
       as wide as the screen) was the biggest part of what the phone's graphics chip could not keep up with. The sky steps
       over in one go, half way through the turn (measured on a phone: fewer stalls than turning it). */
    transition: rotate 0s linear .3s;
  }

  body > .bg-fx {
    transition: rotate 0s linear .3s;
  }

  body.table-flipped::before,
  body.table-flipped > .bg-fx {
    rotate: 180deg;
  }
}

.bg-fx > i {
  position: absolute;
  display: block;
}
