/* ============================================================================
 * ASHTAR KATKOOT - ADVENTURE 8, ACTIVITY 2: CAMEL'S FOUR-PIECE PUZZLE (activity styles)
 *
 * This is the `a8-` prefix of Adventure 7's proven puzzle stylesheet. The mechanic is
 * reused, so the geometry and the comments are deliberately IDENTICAL - only the source
 * image variable changed, from `--a7-rabbit` to `--a8-camel`.
 *
 * ONE source image, four tiles. Every tile is `assets/animals/album-camel.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. `--a8-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.
 * ========================================================================== */

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

/* ------------------------------------------------------------------------ *
 * THE BOARD
 * ------------------------------------------------------------------------ */
#app .a8-p-board {
  position: absolute;
  top: 15vh;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  grid-template-columns: repeat(2, var(--a8-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(--a8-gap);
  /* A framed-picture ground: `album-camel.png` has transparent margins around the Camel, 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: #f4ead6;
  border-radius: 16px;
  padding: var(--a8-gap);
  box-shadow: 0 6px 20px rgba(90, 60, 20, .30);
}
/* THE WORD - hidden until the picture is whole, then below the image. */
#app .a8-p-word {
  position: absolute;
  top: calc(15vh + 2 * var(--a8-cell) + 4 * var(--a8-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(90, 60, 20, .28);
  color: #3a2b12;
  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 .a8-p-word.is-on {
  opacity: 1;
  transform: translateX(-50%) scale(1);
}

/* ------------------------------------------------------------------------ *
 * A TARGET CELL
 * ------------------------------------------------------------------------ */
#app .a8-pcell {
  position: relative;
  width: var(--a8-cell);
  height: var(--a8-cell);
  border-radius: 10px;
  background: rgba(255, 255, 255, .40);
  box-shadow: inset 0 0 0 3px rgba(165, 140, 95, .38);
  transition: box-shadow .18s ease, background .18s ease;
}
/* The subtle highlight over the cell a held piece belongs in. */
#app .a8-pcell.is-target {
  background: rgba(255, 255, 255, .70);
  box-shadow: inset 0 0 0 4px rgba(198, 140, 60, .85),
              0 0 0 4px rgba(255, 205, 120, .35);
}
/* A wrong drop: a short shake, and NOTHING else. No sound, no penalty, no lock-out. */
#app .a8-pcell.is-wrong { animation: a8-wrong 320ms ease; }
@keyframes a8-wrong {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}
#app .a8-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 .a8-p-board.is-complete {
  gap: 0;
  padding: 0;
}
#app .a8-p-board.is-complete .a8-pcell {
  border-radius: 0;
  box-shadow: none;
  animation: a8-settle 320ms cubic-bezier(.3, 1.45, .5, 1) both;
}
#app .a8-p-board.is-complete .a8-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 .a8-p-piece {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  /* AN OPAQUE BACKING IS LOAD-BEARING.
     `album-camel.png` has transparent margins around the Camel, exactly as
     `album-rabbit.png` does. 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, seamless picture. */
  background-color: #f4ead6;
  background-image: var(--a8-camel);
  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(90, 56, 16, .30), 0 0 0 3px rgba(255, 255, 255, .92);
  animation: a8-p-wiggle 3.6s ease-in-out infinite;
}
#app .a8-p-piece[data-i="0"] { --bx: 0%;   --by: 0%; }        /* top-left     */
#app .a8-p-piece[data-i="1"] { --bx: 100%; --by: 0%; }        /* top-right    */
#app .a8-p-piece[data-i="2"] { --bx: 0%;   --by: 100%; }      /* bottom-left  */
#app .a8-p-piece[data-i="3"] { --bx: 100%; --by: 100%; }      /* bottom-right */

@keyframes a8-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 .a8-p-piece.is-selected {
  animation: none;
  transform: scale(1.06) translateY(-3px);
  box-shadow: 0 6px 0 rgba(90, 56, 16, .30), 0 0 0 5px rgba(255, 205, 120, .95);
}
/* LIFTED - the original while its ghost is under the finger. */
#app .a8-p-piece.is-lifted { animation: none; opacity: .28; }
/* RETURNING - the gentle spring back to the tray after a wrong drop. */
#app .a8-p-piece.is-returning { animation: a8-return 300ms ease; }
@keyframes a8-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 .a8-p-piece.is-placed {
  cursor: default;
  pointer-events: none;
  transform: none;
  box-shadow: none;
  animation: a8-snap 260ms cubic-bezier(.3, 1.5, .5, 1) both;
}
@keyframes a8-snap {
  0%   { transform: scale(1.14); }
  55%  { transform: scale(.97); }
  100% { transform: scale(1); }
}
@keyframes a8-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 .a8-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: #f4ead6;
  background-image: var(--a8-camel);
  background-size: 200% 200%;
  background-position: var(--bx) var(--by);
  background-repeat: no-repeat;
  box-shadow: 0 10px 22px rgba(90, 60, 20, .40), 0 0 0 3px rgba(255, 255, 255, .95);
  transform: scale(1.08) rotate(2deg);
  opacity: .97;
}
#app .a8-p-ghost[data-i="0"] { --bx: 0%;   --by: 0%; }
#app .a8-p-ghost[data-i="1"] { --bx: 100%; --by: 0%; }
#app .a8-p-ghost[data-i="2"] { --bx: 0%;   --by: 100%; }
#app .a8-p-ghost[data-i="3"] { --bx: 100%; --by: 100%; }

/* ------------------------------------------------------------------------ *
 * THE TRAY
 * ------------------------------------------------------------------------ */
#app .a8-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(--a8-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, every tile becomes wide-and-1px, and a drag aimed at
     its centre lands nowhere. */
  grid-template-rows: repeat(2, var(--a8-cell));
  gap: var(--a8-gap);
  padding: var(--a8-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 .a8-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 .a8-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: a8-p-spark 600ms ease-out both;
}
@keyframes a8-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.
 * ------------------------------------------------------------------------ */
@media (max-height: 720px) {
  #app .a8-puzzle { --a8-cell: 27vmin; }
  #app .a8-p-board { top: 9vh; }
  #app .a8-p-word { top: calc(9vh + 2 * var(--a8-cell) + 4 * var(--a8-gap) + 8px); }
  #app .a8-p-tray { bottom: calc(env(safe-area-inset-bottom, 0px) + 3vh); }
}

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