/* ============================================================================
 * ASHTAR KATKOOT - ADVENTURE 7, PHASE 3: RABBIT'S FOUR-PIECE PUZZLE
 *
 * ONE source image, four tiles. Every tile is `assets/animals/album-rabbit.png` at
 * `background-size: 200% 200%`, offset to a corner with `background-position`. Nothing
 * is stretched, no second artwork file exists, and the four tiles reconstruct the
 * original picture exactly - which the completed board relies on.
 *
 * ONE size for both places a tile lives. `--a7-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.
 *
 * Layering:
 *   .a7-puzzle     the activity surface, inert until the engine opens it
 *   .a7-p-board    four target cells
 *   .a7-p-piece    a tile - in the tray, in flight, or sitting in its cell
 *   .a7-p-ghost    the tile under the finger (never hit-tested)
 * ========================================================================== */

#app .a7-puzzle {
  --a7-cell: 36vmin;          /* the board cell AND the tray slot, always equal */
  --a7-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 `c7_puzzle_instruction` has genuinely finished. This is the FIRST of
     two independent guards; `unlocked` in JS is the second. */
  pointer-events: none;
}
#app .a7-puzzle.is-open { pointer-events: auto; }

/* ------------------------------------------------------------------------ *
 * THE BOARD
 * ------------------------------------------------------------------------ */
#app .a7-p-board {
  position: absolute;
  top: 15vh;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(2, var(--a7-cell));
  /* THE DOCUMENT IS RTL (`<html lang="ar" dir="rtl">`), so this grid laid its columns
     out RIGHT TO LEFT: cell 0 - which carries the image's TOP-LEFT quadrant - rendered
     at the top-RIGHT. The finished picture was therefore assembled mirrored, with the
     halves not meeting, and it looked like a partly-cropped second rabbit. Every
     `data-i` class and every DOM index was still correct, so only looking at the
     rendered result caught it.
     `direction: ltr` makes index order equal visual order, which is what the whole
     quadrant scheme is built on. */
  direction: ltr;
  gap: var(--a7-gap);
  /* A framed-picture ground: the Rabbit artwork has transparent margins, so the assembled
     picture needs something behind it or the pieces float on the sky. This is the SAME
     solid colour each tile carries, which is what keeps the assembled picture seamless. */
  background: #eaf4fb;
  border-radius: 16px;
  padding: var(--a7-gap);
  box-shadow: 0 6px 20px rgba(10, 40, 20, .30);
}
/* THE WORD - hidden until the picture is whole, then below the image. */
#app .a7-p-word {
  position: absolute;
  top: calc(15vh + 2 * var(--a7-cell) + 4 * var(--a7-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(10, 40, 20, .28);
  color: #14432c;
  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 .a7-p-word.is-on {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

/* ------------------------------------------------------------------------ *
 * A TARGET CELL
 * ------------------------------------------------------------------------ */
#app .a7-pcell {
  position: relative;
  width: var(--a7-cell);
  height: var(--a7-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 .a7-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 .a7-pcell.is-wrong { animation: a7-wrong 320ms ease; }
@keyframes a7-wrong {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}
#app .a7-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 .a7-p-board.is-complete {
  gap: 0;
  padding: 0;
}
#app .a7-p-board.is-complete .a7-pcell {
  border-radius: 0;
  box-shadow: none;
  animation: a7-settle 320ms cubic-bezier(.3, 1.45, .5, 1) both;
}
#app .a7-p-board.is-complete .a7-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 with each other. */
  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 .a7-p-piece {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  /* AN OPAQUE BACKING IS LOAD-BEARING.
     `album-rabbit.png` has wide transparent margins around the Rabbit. With the picture
     alone every tile was mostly hole, and the child saw the garden and the basket
     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: the assembled picture
     came out with a visible horizontal seam straight across Rabbit's face. A solid
     colour on the tile AND on the board means the four tiles form one continuous,
     seamless picture. */
  background-color: #eaf4fb;
  background-image: var(--a7-rabbit);
  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: a7-p-wiggle 3.6s ease-in-out infinite;
}
#app .a7-p-piece[data-i="0"] { --bx: 0%;   --by: 0%; }        /* top-left     */
#app .a7-p-piece[data-i="1"] { --bx: 100%; --by: 0%; }        /* top-right    */
#app .a7-p-piece[data-i="2"] { --bx: 0%;   --by: 100%; }      /* bottom-left  */
#app .a7-p-piece[data-i="3"] { --bx: 100%; --by: 100%; }      /* bottom-right */

@keyframes a7-p-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 .a7-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 .a7-p-piece.is-lifted { animation: none; opacity: .28; }
/* RETURNING - the gentle spring back to the tray after a wrong drop. */
#app .a7-p-piece.is-returning { animation: a7-return 300ms ease; }
@keyframes a7-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 .a7-p-piece.is-placed {
  cursor: default;
  pointer-events: none;
  transform: none;
  box-shadow: none;
  animation: a7-snap 260ms cubic-bezier(.3, 1.5, .5, 1) both;
}
@keyframes a7-snap {
  0%   { transform: scale(1.14); }
  55%  { transform: scale(.97); }
  100% { transform: scale(1); }
}
@keyframes a7-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. It is appended to `document.body` and positioned fixed, so it is never
 * clipped by the board or the tray.
 * ------------------------------------------------------------------------ */
#app .a7-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: #eaf4fb;
  background-image: var(--a7-rabbit);
  background-size: 200% 200%;
  background-position: var(--bx) var(--by);
  background-repeat: no-repeat;
  box-shadow: 0 10px 22px rgba(10, 40, 20, .40), 0 0 0 3px rgba(255, 255, 255, .95);
  transform: scale(1.08) rotate(2deg);
  opacity: .97;
}
#app .a7-p-ghost[data-i="0"] { --bx: 0%;   --by: 0%; }
#app .a7-p-ghost[data-i="1"] { --bx: 100%; --by: 0%; }
#app .a7-p-ghost[data-i="2"] { --bx: 0%;   --by: 100%; }
#app .a7-p-ghost[data-i="3"] { --bx: 100%; --by: 100%; }

/* ------------------------------------------------------------------------ *
 * THE TRAY
 * ------------------------------------------------------------------------ */
#app .a7-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(--a7-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, which
     keeps the shuffled tray readable. */
  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 collapsed to 33px tall, every tile became 140x1, and a drag aimed at its
     centre landed nowhere. */
  grid-template-rows: repeat(2, var(--a7-cell));
  gap: var(--a7-gap);
  padding: var(--a7-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 .a7-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 .a7-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: a7-p-spark 600ms ease-out both;
}
@keyframes a7-p-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. At 360x640 the default
 * `top` and `bottom` would put the tray over the board.
 * ------------------------------------------------------------------------ */
@media (max-height: 720px) {
  #app .a7-puzzle { --a7-cell: 27vmin; }
  #app .a7-p-board { top: 9vh; }
  #app .a7-p-word { top: calc(9vh + 2 * var(--a7-cell) + 4 * var(--a7-gap) + 8px); }
  #app .a7-p-tray { bottom: calc(env(safe-area-inset-bottom, 0px) + 3vh); }
}

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