:root {
  --text: #f5f5f5;
  --muted: rgba(255,255,255,0.7);
  --accent: #ffd166;
  --panel: rgba(255,255,255,0.06);
  --panel-border: rgba(255,255,255,0.12);
  --tile-back: #3a4658;
  --tile-back-border: rgba(255,255,255,0.10);
  --tile-front: #4d7eb8;
  --tile-front-border: rgba(255,255,255,0.22);
  --tile-matched: rgba(255,255,255,0.18);
  --match-glow: #4caf6a;
}
* { box-sizing: border-box; }
/* Author-stylesheet bump so [hidden] beats the display rules below (e.g. the
   .completion-overlay { display: flex } rule has equal specificity to [hidden]
   but wins on cascade order, leaving the overlay covering the page). */
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0;
  background: linear-gradient(135deg, #1a1f2e 0%, #2c3e50 100%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
}
/* Only `body` is the flex container. Applying `display: flex` to `html` AS WELL
   would make body the sole flex item and the `align-items: center` declaration
   on html would shrink body to its intrinsic content width (~170px on a phone),
   leaving huge empty margins on both sides of the page. */
body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 10px;
}
/* Web only, non-phone: never collapse below settings-page width.
   Phones (≤480px) drop the min-width so the page fills the viewport. */
@media (min-width: 481px) {
  html:not(.native) body { min-width: 592px; }
}
header {
  text-align: center;
  margin-bottom: 18px;
  max-width: 560px;
  width: 100%;
  position: relative;
}
.back-arrow {
  position: absolute;
  top: 0;
  left: 0;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  transition: background 0.15s ease, transform 0.15s ease, border-color 0.15s ease;
}
.back-arrow:hover {
  background: rgba(255,255,255,0.18);
  border-color: rgba(255,255,255,0.25);
  transform: translateX(-2px);
}
.back-arrow svg { width: 18px; height: 18px; }
/* Symmetric horizontal padding clears the absolutely-positioned back-arrow (38px
   + 8px breathing room) on the left, and mirrors it on the right so the centered
   title stays visually balanced even on narrow viewports. */
h1 { font-size: 24px; font-weight: 600; margin: 0; padding: 0 46px; }

.status-bar {
  /* 3-column grid so the New Game button sits truly centered between the
     left-aligned timer and the right-aligned Reset, regardless of how wide
     either of those is. */
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  column-gap: 12px;
  width: 100%;
  max-width: 560px;
  padding: 6px 4px;
  margin-bottom: 12px;
}
.status-bar .stat { justify-self: start; }
#newGameBtn       { justify-self: center; }
#restartBtn       { justify-self: end; }
/* On web (non-phone), stretch the status bar to the full body width so the Reset
   button (right-aligned) lands flush against the viewport's right edge instead
   of being trapped at the right edge of the centered 560px container. */
@media (min-width: 481px) {
  html:not(.native) .status-bar { max-width: 100%; }
}
.stat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.stat svg { width: 18px; height: 18px; opacity: 0.85; }
#newGameBtn,
#restartBtn {
  padding: 6px 14px;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
}
#newGameBtn:hover,
#restartBtn:hover {
  background: rgba(255,255,255,0.18);
  border-color: rgba(255,255,255,0.25);
}

.grid-wrap {
  width: 100%;
  max-width: 560px;
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  /* Don't stretch .memory-grid vertically — the grid uses auto rows and
     `align-content: normal` (≈ stretch), so a stretched-tall grid would
     stretch each row beyond the tile's 3/4 aspect-ratio, producing huge
     too-tall tiles on desktop where grid-wrap has lots of vertical room. */
  align-items: start;
}
.memory-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
/* Landscape: wide & short → 4 cols × 3 rows so the board fits the viewport. */
@media (orientation: landscape) {
  .memory-grid { grid-template-columns: repeat(4, 1fr); }
}

.tile {
  position: relative;
  /* Portrait aspect — narrower than tall, like a playing card. The 4-col grid
     keeps the column width fixed, so this makes each tile taller than wide
     instead of square. */
  aspect-ratio: 3 / 4;
  border-radius: 10px;
  cursor: pointer;
  perspective: 600px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.tile-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.35s ease;
}
.tile.flipped .tile-inner { transform: rotateY(180deg); }
.tile-face {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 6px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.tile-back {
  background: var(--tile-back);
  border: 1px solid var(--tile-back-border);
  color: rgba(255,255,255,0.35);
}
.tile-back svg { width: 38%; height: 38%; }
.tile-front {
  background: var(--tile-front);
  border: 1px solid var(--tile-front-border);
  color: #fff;
  transform: rotateY(180deg);
  font-weight: 600;
  /* Wrap long phrases gracefully; size scales with viewport for readability. */
  font-size: clamp(16px, 4.5vw, 24px);
  line-height: 1.2;
  word-break: break-word;
  hyphens: auto;
}
.tile-front.is-english { background: linear-gradient(135deg, #5a6f8c 0%, #4d7eb8 100%); }

/* Speaker button — top-right of each card face. Clicking it plays the card's
   source-language or English audio (memory.js wires `e.stopPropagation()` so a
   tap on the speaker doesn't bubble up to the tile-flip handler). */
.tile-speaker {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 48px;
  height: 48px;
  padding: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.18);
  border: none;
  color: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.9;
  transition: background 0.15s ease, opacity 0.15s ease;
}
.tile-speaker:hover { background: rgba(255,255,255,0.32); opacity: 1; }
.tile-speaker svg { width: 26px; height: 26px; }

/* Checkmark badge — sits in the top-left corner, shown on .matched. It's a
   direct child of .tile (not of .tile-inner) so it isn't rotated by the 3D
   face-flip transform. */
.tile-check {
  position: absolute;
  top: 6px;
  left: 6px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #ffffff;
  color: #1b9c46;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  opacity: 0;
  transform: scale(0.55);
  transition: opacity 0.25s ease, transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
  z-index: 3;
}
.tile-check svg { width: 20px; height: 20px; }
.tile.matched .tile-check { opacity: 1; transform: scale(1); }

/* Matched: keep cards visible with a festive, balloon-like light background;
   the bright-green checkmark badge above signals the match. Disable the
   click/hover affordances since matched tiles aren't replayable. */
.tile.matched {
  cursor: default;
  pointer-events: none;
}
.tile.matched .tile-front,
.tile.matched .tile-front.is-english {
  background: var(--match-bg, #81c784);
  background-image: none;
  color: #1f2a1a;
  border-color: rgba(0, 0, 0, 0.08);
}
.tile.matched .tile-speaker {
  background: rgba(0, 0, 0, 0.10);
  color: #1f2a1a;
  /* Re-enable pointer events on the speaker even though the tile itself doesn't
     accept clicks — the user should still be able to replay the audio. */
  pointer-events: auto;
}
.tile.matched .tile-speaker:hover { background: rgba(0, 0, 0, 0.18); }

/* One swatch per pair-id — 6 clearly distinct rainbow hues (red, orange,
   yellow, green, blue, purple). MAX_PAIRS is 6, so 0-5 are the only ids in
   play. No two entries share a primary, so adjacent matches always look
   obviously different. */
.tile[data-pair-id="0"].matched .tile-front { --match-bg: #ef5350; } /* red */
.tile[data-pair-id="1"].matched .tile-front { --match-bg: #ffb74d; } /* orange */
.tile[data-pair-id="2"].matched .tile-front { --match-bg: #fff176; } /* yellow */
.tile[data-pair-id="3"].matched .tile-front { --match-bg: #81c784; } /* green */
.tile[data-pair-id="4"].matched .tile-front { --match-bg: #64b5f6; } /* blue */
.tile[data-pair-id="5"].matched .tile-front { --match-bg: #ba68c8; } /* purple */

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
}
.empty-state .subtle { font-size: 14px; opacity: 0.75; }

.nav {
  width: 100%;
  max-width: 560px;
  display: flex;
  justify-content: flex-start;
  margin-top: 20px;
}
.nav a {
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
  border-bottom: 1px dashed transparent;
  transition: border-color 0.15s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.nav a:hover { border-bottom-color: var(--accent); }
.back-arrow-icon { width: 16px; height: 16px; flex-shrink: 0; }

/* Completion overlay */
.completion-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
  animation: overlay-in 0.25s ease;
}
@keyframes overlay-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.completion-card {
  position: relative;
  background: linear-gradient(135deg, #243042 0%, #2c3e50 100%);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 16px;
  padding: 32px 28px;
  text-align: center;
  max-width: 380px;
  width: 100%;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  animation: card-in 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
/* (X) close button — dismisses the overlay without starting a new game so the
   user can inspect their just-finished board. */
.completion-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.85;
  transition: background 0.15s ease, opacity 0.15s ease;
}
.completion-close:hover { background: rgba(255,255,255,0.18); opacity: 1; }
.completion-close svg { width: 16px; height: 16px; }
@keyframes card-in {
  from { transform: scale(0.7); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}
.completion-emoji { font-size: 56px; margin-bottom: 4px; line-height: 1; }
.completion-card h2 { margin: 8px 0 12px; font-size: 26px; }
.completion-time { font-size: 18px; color: var(--muted); margin: 0 0 6px; }
.completion-time strong { color: var(--text); font-size: 22px; }
.completion-best { font-size: 14px; color: var(--muted); margin: 4px 0 22px; min-height: 18px; }
.completion-best.new-record { color: var(--accent); font-weight: 600; font-size: 16px; }
.completion-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.primary-btn, .secondary-btn {
  display: inline-block;
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
  text-align: center;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.primary-btn {
  background: var(--accent);
  color: #1a1f2e;
  border-color: var(--accent);
}
.primary-btn:hover { filter: brightness(1.05); }
.primary-btn:active { transform: scale(0.98); }
.secondary-btn {
  background: rgba(255,255,255,0.06);
  color: var(--text);
  border-color: rgba(255,255,255,0.18);
}
.secondary-btn:hover { background: rgba(255,255,255,0.12); }

/* New-record extra polish on the completion card — gold border + faint gold halo.
   The trophy emoji is intentionally NOT animated; the surrounding card styling
   and confetti rain already signal "this was a record" without the bounce. */
.completion-card.is-record {
  border-color: var(--accent);
  box-shadow: 0 12px 40px rgba(0,0,0,0.5), 0 0 0 2px rgba(255, 209, 102, 0.25);
}

/* Confetti rain (only added to DOM on new record) */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 60;
}
.confetti-piece {
  position: absolute;
  top: -20px;
  width: 10px;
  height: 14px;
  opacity: 0.95;
  border-radius: 2px;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  0%   { transform: translateY(-10vh) rotate(0deg); }
  100% { transform: translateY(110vh) rotate(720deg); }
}

@media (max-width: 480px) {
  body { padding: 16px 4px; }
  .back-arrow { width: 34px; height: 34px; }
  h1 { font-size: 22px; }
  .status-bar { padding: 4px 4px; gap: 14px; }
  .stat { font-size: 15px; }
}

/* Native (Capacitor): respect safe-area insets so the header doesn't sit under the
   status bar / notch. Side padding is kept tiny so the 3×4 grid maximises tile size. */
html.native body {
  padding: max(env(safe-area-inset-top), 8px)
           max(env(safe-area-inset-right), 4px)
           max(env(safe-area-inset-bottom), 8px)
           max(env(safe-area-inset-left), 4px);
}
