/* ============================================================================
 * ASHTAR KATKOOT - ADVENTURE 9, ACTIVITY 2: MATCH FOUR CARDS (activity styles)
 *
 *   .a9m-match      the activity surface, inert until the instruction has finished
 *   .a9m-match-row  the four cards, a stable 2x2
 *   .a9m-mcard      one card - the WHOLE card is the touch target
 *   .a9m-mcard-inner  the flip wrapper (carries the 3D transform)
 *   .a9m-mcard-back   the BACK, drawn in CSS - no new asset
 *   .a9m-mcard-face   the FRONT: the picture or the word
 *
 * FOUR STATES, AND THE ORDER MATTERS
 *   is-retrying  a pair is wrong and the retry clip is playing - the whole board is inert
 *   is-wrong     this pair shakes; it is turned back over when the clip GENUINELY ends
 *   is-up        face-up and waiting to be resolved
 *   is-matched   a correct pair, kept face-up for good and out of play
 *
 * THE DOCUMENT IS RTL, so the row and the cards pin `direction: ltr`. Without it the four
 * cards laid out right-to-left and the recorded shuffle no longer matched the rendered
 * order - invisible to a class-based check and obvious to a child.
 * ========================================================================== */

#app .a9m-match {
  position: absolute;
  inset: 0;
  z-index: 6;
  overflow: hidden;
  overscroll-behavior: contain;
  touch-action: manipulation;
  /* Locked until the instruction cue has genuinely finished. First of two guards; the
     second is `unlocked` in the activity code. */
  pointer-events: none;
  /* A calm, low-contrast ground. The cards are the subject. */
  background: linear-gradient(180deg, #eef6e8 0%, #fff8ec 100%);
}
#app .a9m-match.is-open { pointer-events: auto; }
#app .a9m-match.is-closing {
  opacity: 0;
  transition: opacity .32s ease;
  pointer-events: none;
}

/* ------------------------------------------------------------------------ *
 * THE 2x2 GRID
 *
 * `aspect-ratio` is LOAD-BEARING here, exactly as it was a moment ago for the rows: with
 * `grid-template-rows: repeat(2, 1fr)` and no height to divide, both rows collapsed to a few
 * pixels and the cards came out thin and overlapping. The defined size is what makes two
 * `1fr` rows work at all. A 2x2 block of square cards is square, which keeps the artwork
 * undistorted and still leaves room for the words.
 * ------------------------------------------------------------------------ */
#app .a9m-match-row {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 88%;
  max-width: 420px;
  aspect-ratio: 1 / 1;
  display: grid;
  direction: ltr;
  grid-template-columns: repeat(2, 1fr);
  /* The rows MUST be explicit: an auto-sized row makes a percentage-height child resolve
     to nothing, which is what produced the collapsed cards. */
  grid-template-rows: repeat(2, 1fr);
  gap: 3.4vmin;
}

#app .a9m-mcard {
  position: relative;
  /* The button's own padding is inherited from `style.css` and MUST be zeroed. */
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 0;
  margin: 0;
  background: none;
  border-radius: 22px;
  cursor: pointer;
  /* THE GLOBAL BUTTON MUST BE FULLY NEUTRALISED. `style.css` declares a bare
     `button{border-radius:18px; box-shadow:0 6px 0 #bc6716; padding:14px 30px;
     background:linear-gradient(...)}`, and a rule only overrides what it actually DECLARES.
     The shadows now live on the card's FACES, so without this reset the bare button's own
     shadow showed through and every card wore a brown bar underneath it - visible in a
     screenshot as a coloured strip below each card. */
  box-shadow: none;
  overflow: visible;              /* the flip needs the 3D space; the faces clip themselves */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  /* A perspective on the PARENT so its children have somewhere to flip to. */
  perspective: 900px;
}

/* THE FLIP WRAPPER. `preserve-3d` is what lets the two faces occupy the same space and
   rotate around each other rather than merely fading. */
#app .a9m-mcard-inner {
  position: absolute;
  inset: 0;
  display: block;
  transform-style: preserve-3d;
  transition: transform .42s cubic-bezier(.3, 1.1, .4, 1);
}
#app .a9m-mcard.is-up .a9m-mcard-inner { transform: rotateY(180deg); }

/* A FACE. Both faces fill the card, hide their own reverse side, and only one is ever
   visible. The card is built with the BACK first, so a card that has never been tapped shows
   exactly one thing: the back. */
#app .a9m-mcard-face,
#app .a9m-mcard-back {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 22px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}
#app .a9m-mcard-face { transform: rotateY(180deg); }

/* THE FRONT - the white card the artwork and the word sit on. */
#app .a9m-mcard-face {
  background: #ffffff;
  box-shadow: 0 5px 0 rgba(20, 70, 40, .22), 0 8px 18px rgba(10, 40, 20, .16);
}
/* A soft inner ring so a white card reads as a card even on a pale background. */
#app .a9m-mcard-face::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 22px;
  box-shadow: inset 0 0 0 3px rgba(130, 180, 130, .26);
  pointer-events: none;
}

/* THE BACK - drawn entirely in CSS, so every card is identical and no asset is added. A
   warm rounded panel with a centred mark: unmistakably a covered card, and clearly not the
   artwork or the word. */
#app .a9m-mcard-back {
  background: linear-gradient(160deg, #5f8f5a 0%, #3f6b3c 55%, #2f5230 100%);
  box-shadow: 0 5px 0 rgba(20, 70, 40, .30), 0 8px 18px rgba(10, 40, 20, .22);
}
#app .a9m-mcard-backmark {
  width: 34%;
  height: 34%;
  border-radius: 50%;
  background: rgba(255, 253, 240, .30);
  box-shadow: inset 0 0 0 3px rgba(255, 253, 240, .55);
  pointer-events: none;
}

#app .a9m-mcard-chip {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 9%;
}
#app .a9m-mcard-img {
  display: block;
  /* Capped on both axes as well as `contain`, so the animal is always wholly inside its
     card even if a source image has an unexpected intrinsic ratio. */
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
/* THE WORD. The module sets an inline size from the word's own length so a longer word
   always fits on ONE line. This is the default for the short-word case. */
#app .a9m-mcard-word {
  direction: ltr;
  unicode-bidi: isolate;
  padding: 0 6px;
  color: #1e3a20;
  font-size: 9.4vmin;
  font-weight: 900;
  line-height: 1.05;
  text-align: center;
  text-wrap: balance;
  max-width: 100%;
  /* A long word must not run off the card at the largest size. */
  overflow-wrap: break-word;
}

/* Pressed - immediate acknowledgement, and nothing that moves the card's position. */
#app .a9m-mcard:active .a9m-mcard-inner { transform: scale(.97); }
#app .a9m-mcard.is-up:active .a9m-mcard-inner { transform: rotateY(180deg) scale(.97); }

/* UP - the two cards currently revealed. A slight lift so it is obvious which are showing. */
#app .a9m-mcard.is-up { z-index: 2; }

/* MATCHED - kept face-up for good. `pointer-events: none` is not decoration: together with
   the JS `done` flag it is what takes the pair out of play, so it can never be re-tapped. */
#app .a9m-mcard.is-matched { pointer-events: none; }
#app .a9m-mcard.is-matched .a9m-mcard-face {
  background: #eafbe8;
  box-shadow: 0 4px 0 rgba(20, 70, 40, .20), 0 0 0 5px rgba(120, 210, 140, .95);
  animation: a9m-locked 420ms cubic-bezier(.3, 1.4, .5, 1) both;
}
@keyframes a9m-locked {
  0%   { transform: rotateY(180deg) scale(1.08); }
  60%  { transform: rotateY(180deg) scale(.98); }
  100% { transform: rotateY(180deg) scale(1); }
}

/* WRONG - a short rock on BOTH cards of the pair. Removed only inside the retry clip's
   genuine release, never on a timer. */
#app .a9m-mcard.is-wrong { animation: a9m-no 400ms ease; }
@keyframes a9m-no {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px) rotate(-1.5deg); }
  55% { transform: translateX(6px) rotate(1.5deg); }
  80% { transform: translateX(-3px); }
}
/* The rock must not cancel the flip, so it is applied to the WRAPPER and the flip stays on
   the inner element. */
#app .a9m-mcard.is-wrong .a9m-mcard-inner { animation: a9m-no 400ms ease; }

/* WHILE THE RETRY CLIP IS SPEAKING - the whole board is held, so a burst of taps cannot
   start a second pair while the audio is still running. The JS refuses them too. */
#app .a9m-match.is-retrying .a9m-mcard { pointer-events: none; }

/* Once every pair is matched, no card takes a tap any more. */
#app .a9m-match.is-solved .a9m-mcard { pointer-events: none; }

/* THE HELP LINE - shown after two wrong attempts. It points; it never answers. */
#app .a9m-match-hint {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 7vh);
  transform: translateX(-50%) translateY(8px);
  opacity: 0;
  padding: 9px 20px;
  border-radius: 18px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 4px 14px rgba(10, 40, 20, .20);
  color: #1e3a20;
  font-size: 4.2vmin;
  font-weight: 800;
  line-height: 1.4;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity .26s ease, transform .26s ease;
}
#app .a9m-match.is-help .a9m-match-hint {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-height: 700px) {
  /* `aspect-ratio` is deliberately NOT overridden: the rows need a defined height on a
     short screen more than they do on a tall one. */
  #app .a9m-match-row { gap: 2.6vmin; width: 92%; }
}
@media (prefers-reduced-motion: reduce) {
  #app .a9m-mcard-inner { transition-duration: 1ms; }
  #app .a9m-mcard.is-wrong .a9m-mcard-inner,
  #app .a9m-mcard.is-matched .a9m-mcard-face { animation-duration: 1ms; }
}