/* ============================================================================
 * ASHTAR KATKOOT - ADVENTURE 9, ACTIVITY 3: THE FOUR-PIECE PUZZLES (activity styles)
 *
 * This is the `a9p-` prefix of the proven Adventure 8 puzzle stylesheet. ONE stylesheet
 * serves BOTH animals: the only difference between the two boards is the value of
 * `--a9p-src`, which the module sets on each stage element as it builds it. Because the
 * property is set per stage and the two boards never share a stage, neither animal needs
 * its own rules - which is the whole point of reusing one implementation.
 *
 * ONE source image, four tiles. Every tile is `album-donkey.png` or `album-fox.png` at
 * `background-size: 200% 200%`, offset to a corner. Nothing is stretched, no second
 * artwork file exists, and the four tiles reconstruct the original exactly.
 *
 * ONE size for both places a tile lives. `--a9p-cell` is the board cell AND the tray slot,
 * so a piece does not change scale when it moves from the tray into its cell. That is what
 * removes the visual jump at the moment of placement.
 * ========================================================================== */

#app .a9p-puzzle {
  --a9p-cell: 36vmin;          /* the board cell AND the tray slot, always equal */
  --a9p-gap: 2.5vmin;
  position: absolute;
  inset: 0;
  z-index: 6;
  overflow: hidden;
  /* The page must not scroll while a finger is dragging a piece. `touch-action` has to be
     declared BEFORE the gesture: setting it once the drag has started does nothing for the
     gesture already in flight. */
  touch-action: none;
  overscroll-behavior: contain;
  /* Locked until the gate cue has genuinely finished. This is the FIRST of two independent
     guards; `unlocked` in the activity is the second. */
  pointer-events: none;
}
#app .a9p-puzzle.is-open { pointer-events: auto; }

/* ------------------------------------------------------------------------ *
 * THE BOARD
 * ------------------------------------------------------------------------ */
#app .a9p-p-board {
  position: absolute;
  top: 15vh;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(2, var(--a9p-cell));
  /* THE DOCUMENT IS RTL (`<html lang="ar" dir="rtl">`), so this grid would lay its columns
     out RIGHT TO LEFT: cell 0 - which carries the image's TOP-LEFT quadrant - would render at
     the top-RIGHT, and the finished picture would be assembled mirrored. Every `data-i` class
     and every DOM index would still be correct, so only looking at the rendered result
     catches it. `direction: ltr` makes index order equal visual order, which is what the
     whole quadrant scheme is built on. */
  direction: ltr;
  gap: var(--a9p-gap);
  /* A framed-picture ground. Both source images have transparent margins - the fox is a
     640x640 palette image with a tRNS chunk and the donkey is 1254x1254 true RGBA, and both
     were checked - so the assembled picture needs something behind it. This is the SAME
     solid colour each tile carries, which is what keeps the assembled picture seamless. */
  background: #eef3e4;
  border-radius: 16px;
  padding: var(--a9p-gap);
  box-shadow: 0 6px 20px rgba(20, 70, 30, .30);
}
/* THE WORD - hidden until the picture is whole, then below the image. */
#app .a9p-p-word {
  position: absolute;
  top: calc(15vh + 2 * var(--a9p-cell) + 4 * var(--a9p-gap) + 12px);
  left: 50%;
  transform: translateX(-50%) scale(.8);
  opacity: 0;
  padding: 6px 22px;
  border-radius: 18px;
  background: rgba(255, 253, 246, .92);
  box-shadow: 0 4px 14px rgba(20, 70, 30, .28);
  color: #1e3a20;
  font-size: 7.4vmin;
  font-weight: 900;
  letter-spacing: .02em;
  line-height: 1.15;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity .3s ease, transform .34s cubic-bezier(.3, 1.5, .5, 1);
}
#app .a9p-p-word.is-on {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

/* ------------------------------------------------------------------------ *
 * A TARGET CELL
 * ------------------------------------------------------------------------ */
#app .a9p-pcell {
  position: relative;
  width: var(--a9p-cell);
  height: var(--a9p-cell);
  border-radius: 10px;
  background: rgba(255, 255, 255, .40);
  box-shadow: inset 0 0 0 3px rgba(120, 150, 120, .38);
  transition: box-shadow .18s ease, background .18s ease;
}
/* The subtle highlight over the cell a held piece belongs in. */
#app .a9p-pcell.is-target {
  background: rgba(255, 255, 255, .70);
  box-shadow: inset 0 0 0 4px rgba(76, 175, 110, .85),
              0 0 0 4px rgba(120, 220, 150, .35);
}
/* A wrong drop: a short shake, and NOTHING else. No sound, no penalty, no lock-out. */
#app .a9p-pcell.is-wrong { animation: a9p-wrong 320ms ease; }
@keyframes a9p-wrong {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}
#app .a9p-pcell.is-filled { box-shadow: none; background: none; }

/* WHEN THE PICTURE IS WHOLE, the seams disappear. The four tiles were always one image;
   removing the gap and the shadows is what makes that visible. */
#app .a9p-p-board.is-complete {
  gap: 0;
  padding: 0;
}
#app .a9p-p-board.is-complete .a9p-pcell {
  border-radius: 0;
  box-shadow: none;
  animation: a9p-settle 320ms cubic-bezier(.3, 1.45, .5, 1) both;
}
#app .a9p-p-board.is-complete .a9p-p-piece {
  border-radius: 0;
  box-shadow: none;
  /* The cell side is a `vmin` value, so on most devices it lands on a FRACTIONAL pixel. Two
     boxes sharing a fractional edge are composited with a hairline seam between them - a
     visible line straight across the finished picture. A 1.5% overlap closes it. Every tile
     is scaled the same way about its own centre, so the four quadrants stay consistent. */
  transform: scale(1.015);
}

/* ------------------------------------------------------------------------ *
 * A TILE
 *
 * The four quadrant offsets. `data-i` is the ONE piece of state that decides what a tile
 * shows; everything else is presentation.
 * ------------------------------------------------------------------------ */
#app .a9p-p-piece {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  /* AN OPAQUE BACKING IS LOAD-BEARING. Both album images have transparent margins around
     the animal. With the picture alone every tile would be mostly hole and the child would
     see the scene showing THROUGH it. The backing is a single SOLID colour - not a gradient
     - because a gradient re-drawn per tile bands at every tile boundary and the assembled
     picture comes out with a visible seam straight across the animal's face. A solid colour
     on the tile AND on the board means the four tiles form one continuous picture. */
  background-color: #eef3e4;
  background-image: var(--a9p-src);
  background-size: 200% 200%;
  background-position: var(--bx) var(--by);
  background-repeat: no-repeat;
  cursor: grab;
  touch-action: none;
  /* A seam and a highlight edge so a free tile always reads as a separate object. */
  box-shadow: 0 3px 0 rgba(20, 70, 40, .30), 0 0 0 3px rgba(255, 255, 255, .92);
  animation: a9p-wiggle 3.6s ease-in-out infinite;
}
#app .a9p-p-piece[data-i="0"] { --bx: 0%;   --by: 0%; }        /* top-left     */
#app .a9p-p-piece[data-i="1"] { --bx: 100%; --by: 0%; }        /* top-right    */
#app .a9p-p-piece[data-i="2"] { --bx: 0%;   --by: 100%; }      /* bottom-left  */
#app .a9p-p-piece[data-i="3"] { --bx: 100%; --by: 100%; }      /* bottom-right */

@keyframes a9p-wiggle {
  0%, 100% { transform: rotate(-1.4deg); }
  50%      { transform: rotate(1.4deg); }
}

/* SELECTED - the tap-to-place affordance. Clear, and it does not depend on colour alone: the
   tile also lifts and grows a ring. */
#app .a9p-p-piece.is-selected {
  animation: none;
  transform: scale(1.06) translateY(-3px);
  box-shadow: 0 6px 0 rgba(20, 70, 40, .30), 0 0 0 5px rgba(120, 220, 150, .95);
}
/* LIFTED - the original while its ghost is under the finger. */
#app .a9p-p-piece.is-lifted { animation: none; opacity: .28; }
/* RETURNING - the gentle spring back to the tray after a wrong drop. */
#app .a9p-p-piece.is-returning { animation: a9p-return 300ms ease; }
@keyframes a9p-return {
  0%   { transform: translate(0,0) scale(1); }
  40%  { transform: translate(0,4px) scale(1.05,.94); }
  100% { transform: translate(0,0) scale(1); }
}
/* PLACED - permanently fixed in its cell. */
#app .a9p-p-piece.is-placed {
  cursor: default;
  pointer-events: none;
  transform: none;
  box-shadow: none;
  animation: a9p-snap 260ms cubic-bezier(.3, 1.5, .5, 1) both;
}
@keyframes a9p-snap {
  0%   { transform: scale(1.14); }
  55%  { transform: scale(.97); }
  100% { transform: scale(1); }
}
@keyframes a9p-settle {
  0%   { transform: scale(1.06); }
  60%  { transform: scale(.99); }
  100% { transform: scale(1); }
}

/* ------------------------------------------------------------------------ *
 * THE GHOST - the tile under the finger.
 *
 * `pointer-events: none` is load-bearing: the hit-test in JS walks up from
 * `elementFromPoint`, and a ghost under the fingertip would otherwise be the thing it finds.
 * ------------------------------------------------------------------------ */
#app .a9p-p-ghost {
  position: fixed;
  z-index: 60;
  border-radius: 10px;
  pointer-events: none;
  /* The SAME backing as a real tile, so a dragged piece looks identical to the one it came
     from - only bigger. */
  background-color: #eef3e4;
  background-image: var(--a9p-src);
  background-size: 200% 200%;
  background-position: var(--bx) var(--by);
  background-repeat: no-repeat;
  box-shadow: 0 10px 22px rgba(20, 70, 30, .40), 0 0 0 3px rgba(255, 255, 255, .95);
  transform: scale(1.08) rotate(2deg);
  opacity: .97;
}
#app .a9p-p-ghost[data-i="0"] { --bx: 0%;   --by: 0%; }
#app .a9p-p-ghost[data-i="1"] { --bx: 100%; --by: 0%; }
#app .a9p-p-ghost[data-i="2"] { --bx: 0%;   --by: 100%; }
#app .a9p-p-ghost[data-i="3"] { --bx: 100%; --by: 100%; }

/* ------------------------------------------------------------------------ *
 * THE TRAY
 * ------------------------------------------------------------------------ */
#app .a9p-p-tray {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 7vh);
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(2, var(--a9p-cell));
  /* Same reason as the board - see the note there. The tray has no quadrant meaning, but
     keeping it LTR means a tile's tray position and its cell position agree. */
  direction: ltr;
  /* The rows MUST be explicit. With only `grid-template-columns` the rows are auto-sized,
     and a tile whose height is `100%` inside an auto row resolves to zero: the tray
     collapses to a few pixels tall and a drag aimed at its centre lands nowhere. */
  grid-template-rows: repeat(2, var(--a9p-cell));
  gap: var(--a9p-gap);
  padding: var(--a9p-gap);
  border-radius: 18px;
  background: rgba(255, 253, 246, .30);
  border: 2px solid rgba(255, 255, 255, .55);
  transition: opacity .28s ease, transform .28s ease;
}
/* On completion the tray leaves completely - the child must be left with the finished
   picture, not with four holes where a tray used to be. */
#app .a9p-p-tray.is-gone {
  opacity: 0;
  transform: translateX(-50%) translateY(24px);
  pointer-events: none;
}

/* ------------------------------------------------------------------------ *
 * THE SPARKLE - visual only; it never plays audio, so it cannot collide with the narration
 * bus.
 * ------------------------------------------------------------------------ */
#app .a9p-p-spark {
  position: absolute;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  border-radius: 50%;
  pointer-events: none;
  background: radial-gradient(circle, rgba(255, 250, 210, .95) 0%, rgba(255, 214, 92, 0) 70%);
  animation: a9p-spark 600ms ease-out both;
}
@keyframes a9p-spark {
  0%   { transform: scale(.4); opacity: .95; }
  100% { transform: scale(3.4); opacity: 0; }
}

/* ------------------------------------------------------------------------ *
 * SHORTER SCREENS - the board, the word and the tray are positioned independently, so on a
 * short screen they must be pulled together rather than allowed to overlap.
 * ------------------------------------------------------------------------ */
@media (max-height: 720px) {
  #app .a9p-puzzle { --a9p-cell: 27vmin; }
  #app .a9p-p-board { top: 9vh; }
  #app .a9p-p-word { top: calc(9vh + 2 * var(--a9p-cell) + 4 * var(--a9p-gap) + 8px); }
  #app .a9p-p-tray { bottom: calc(env(safe-area-inset-bottom, 0px) + 3vh); }
}

@media (prefers-reduced-motion: reduce) {
  #app .a9p-p-piece { animation: none; }
  #app .a9p-p-piece.is-placed,
  #app .a9p-p-board.is-complete .a9p-pcell { animation-duration: 1ms; }
  #app .a9p-p-piece.is-returning,
  #app .a9p-pcell.is-wrong { animation-duration: 1ms; }
  #app .a9p-p-spark { animation-duration: 1ms; }
}