/* ═══════════════════════════════════════════════════════════
   Memory «Игра» — design system aligned with Карман
   navy / red primary / glass surfaces / tight viewport fit
   ═══════════════════════════════════════════════════════════ */

#page-game.page {
  --mem-accent: var(--primary, #dc2626);
  --mem-accent-2: #f87171;
  --mem-glow: var(--primary-glow, rgba(220, 38, 38, 0.35));
  --mem-glass: rgba(36, 48, 66, 0.72);
  --mem-glass-border: rgba(255, 255, 255, 0.1);

  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  box-sizing: border-box;
  overflow: hidden;
  isolation: isolate;
}

#page-game.page.hidden {
  display: none !important;
}

/* Ambient stage */
#page-game.page::before {
  content: '';
  position: absolute;
  inset: -20% -10% auto -10%;
  height: 55%;
  background:
    radial-gradient(ellipse 50% 70% at 18% 30%, rgba(220, 38, 38, 0.16), transparent 60%),
    radial-gradient(ellipse 45% 60% at 88% 20%, rgba(96, 165, 250, 0.1), transparent 55%),
    radial-gradient(ellipse 40% 50% at 55% 0%, rgba(255, 255, 255, 0.04), transparent 50%);
  pointer-events: none;
  z-index: 0;
}

#page-game.page > * {
  position: relative;
  z-index: 1;
}

/* ─── Header ─── */
#page-game > .page-header {
  flex: 0 0 auto;
  margin-bottom: 12px !important;
  align-items: center;
}

/* Help tooltip above HUD (was clipped / under stats) */
#page-game > .page-header {
  position: relative;
  z-index: 30 !important;
  overflow: visible !important;
}
#page-game .page-title-with-help {
  position: relative;
  z-index: 31;
  overflow: visible;
}
#page-game .page-help {
  position: relative;
  z-index: 32;
}
#page-game .page-help-bubble {
  z-index: 200 !important;
  left: 0 !important;
  right: auto !important;
  top: calc(100% + 10px) !important;
  transform: none !important;
  width: min(360px, calc(100vw - 48px)) !important;
  max-width: 360px;
  background: #1e2a3a !important;
  border: 1px solid rgba(255, 255, 255, 0.14) !important;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55) !important;
  color: rgba(255, 255, 255, 0.88) !important;
}
#page-game .page-help-bubble::before {
  left: 10px !important;
  background: #1e2a3a !important;
}
#page-game .memory-wrap {
  position: relative;
  z-index: 1 !important;
}


#page-game > .page-header h1 {
  font-size: clamp(1.35rem, 2.4vw, 1.75rem) !important;
  letter-spacing: -0.04em;
  margin: 0 !important;
}

#page-game .memory-kicker {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 6px;
  padding: 4px 10px 4px 6px;
  border-radius: 999px;
  background: var(--primary-light, rgba(220, 38, 38, 0.16));
  border: 1px solid rgba(220, 38, 38, 0.28);
  color: #fca5a5;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

#page-game .memory-kicker-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--mem-accent);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.25);
  animation: memPulse 2.2s ease-in-out infinite;
}

@keyframes memPulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.15); opacity: 0.75; }
}

#page-game .memory-lead {
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  font-size: 0.88rem;
  margin: 0.35rem 0 0 !important;
  line-height: 1.4;
  max-width: 36rem;
}

#page-game .page-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

#page-game .page-header-actions .btn {
  min-height: 40px;
  border-radius: 12px;
  font-weight: 650;
}

#page-game #memory-records-btn {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border, rgba(255, 255, 255, 0.08));
  color: var(--text-secondary, rgba(255, 255, 255, 0.82));
  backdrop-filter: blur(10px);
}

#page-game #memory-records-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  border-color: var(--border-strong, rgba(255, 255, 255, 0.14));
}

/* ─── Stage shell ─── */
#page-game .memory-wrap {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: min(1280px, 100%);
  margin: 0 auto;
  padding: 0 0 10px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
}

#page-game[data-round="2"] .memory-wrap {
  max-width: min(1240px, 100%);
  gap: 8px;
  padding-bottom: 6px;
}


/* Glass HUD */
.memory-hud {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  padding: 10px;
  border-radius: var(--radius, 16px);
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.05), transparent 42%),
    var(--mem-glass);
  border: 1px solid var(--mem-glass-border);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.05) inset,
    0 16px 40px rgba(0, 0, 0, 0.22);
  backdrop-filter: blur(18px) saturate(1.2);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
}

.memory-stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.05);
  transition: border-color 0.2s ease, background 0.2s ease, transform 0.2s ease;
}

.memory-stat:hover {
  background: rgba(0, 0, 0, 0.26);
  border-color: rgba(255, 255, 255, 0.1);
}

.memory-stat-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  line-height: 1.2;
}

.memory-stat strong,
.memory-stat .memory-stat-value {
  color: var(--text, #fff);
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  font-size: 1rem;
  letter-spacing: -0.02em;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.memory-stat.is-accent {
  background: linear-gradient(160deg, rgba(220, 38, 38, 0.22), rgba(220, 38, 38, 0.08));
  border-color: rgba(220, 38, 38, 0.35);
}

.memory-stat.is-accent .memory-stat-label {
  color: #fca5a5;
}

.memory-stat.is-accent strong {
  color: #fff;
}

/* Progress */
.memory-progress {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.memory-progress-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.memory-status {
  min-height: 1.2em;
  margin: 0;
  font-size: 0.86rem;
  font-weight: 500;
  color: var(--text-secondary, rgba(255, 255, 255, 0.82));
  letter-spacing: -0.01em;
}

.memory-status[data-kind="win"] {
  color: var(--success, #34d399);
  font-weight: 650;
}

.memory-progress-track {
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.memory-progress-bar {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #b91c1c, #dc2626 45%, #f87171);
  box-shadow: 0 0 16px rgba(220, 38, 38, 0.45);
  transition: width 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Board stage */
.memory-board-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border-radius: var(--radius-lg, 22px);
  background:
    radial-gradient(ellipse 70% 80% at 50% 0%, rgba(220, 38, 38, 0.07), transparent 55%),
    linear-gradient(180deg, rgba(36, 48, 66, 0.55), rgba(18, 25, 32, 0.35));
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.04) inset,
    0 20px 50px rgba(0, 0, 0, 0.28);
}

.memory-board {
  --mem-gap: 8px;
  --mem-cols: 5;
  --mem-rows: 4;
  --mem-max-h: min(500px, calc(100dvh - 250px));
  --mem-max-w: min(100%, 680px);
  --mem-w: min(var(--mem-max-w), calc(var(--mem-max-h) * var(--mem-cols) / var(--mem-rows)));
  --mem-h: min(var(--mem-max-h), calc(var(--mem-w) * var(--mem-rows) / var(--mem-cols)));

  display: grid !important;
  grid-template-columns: repeat(var(--mem-cols), 1fr);
  grid-template-rows: repeat(var(--mem-rows), 1fr);
  gap: var(--mem-gap);
  width: var(--mem-w);
  height: var(--mem-h);
  max-width: 100%;
  box-sizing: border-box;
  margin: 0 auto;
  flex: 0 1 auto;
}


/* ═══════════════════════════════════════════════════
   Round 2 — 40 cards
   Fill the stage with MAX square cells (container query).
   8×5 grid: larger tiles, faces not crushed.
   ═══════════════════════════════════════════════════ */

#page-game[data-round="2"] {
  --mem-accent: #8b5cf6;
  --mem-accent-2: #c4b5fd;
  --mem-glow: rgba(139, 92, 246, 0.4);
}

#page-game[data-round="2"] > .page-header {
  margin-bottom: 6px !important;
}
#page-game[data-round="2"] .memory-lead,
#page-game[data-round="2"] .memory-kicker {
  display: none;
}
#page-game[data-round="2"] .memory-status {
  margin: 2px 0 4px !important;
  font-size: 0.78rem;
  opacity: 0.88;
}
#page-game[data-round="2"] .memory-progress {
  margin-bottom: 4px !important;
  height: 4px;
}
#page-game[data-round="2"] .memory-hud {
  gap: 6px;
  padding: 7px 10px;
  margin-bottom: 4px !important;
}
#page-game[data-round="2"] .memory-stat {
  padding: 5px 9px;
}
#page-game[data-round="2"] .memory-stat strong {
  font-size: 0.92rem;
}
#page-game[data-round="2"] .memory-stat.is-accent {
  background: linear-gradient(160deg, rgba(139, 92, 246, 0.24), rgba(139, 92, 246, 0.08));
  border-color: rgba(139, 92, 246, 0.4);
}
#page-game[data-round="2"] .memory-stat.is-accent .memory-stat-label {
  color: #c4b5fd;
}
#page-game[data-round="2"] .memory-progress-bar {
  background: linear-gradient(90deg, #6d28d9, #8b5cf6 45%, #c4b5fd);
  box-shadow: 0 0 16px rgba(139, 92, 246, 0.45);
}

#page-game[data-round="2"] .memory-wrap {
  max-width: min(1280px, 100%);
  gap: 6px;
  padding-bottom: 4px;
  height: 100%;
  min-height: 0;
}

/* Stage fills remaining viewport */
#page-game[data-round="2"] .memory-board-shell {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  padding: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* container for sizing the board */
  container-type: size;
  container-name: memstage;
  background:
    radial-gradient(ellipse 80% 70% at 50% 0%, rgba(139, 92, 246, 0.14), transparent 58%),
    radial-gradient(ellipse 50% 40% at 100% 100%, rgba(99, 102, 241, 0.08), transparent 50%),
    linear-gradient(180deg, rgba(40, 36, 64, 0.55), rgba(18, 16, 28, 0.4));
  border-color: rgba(167, 139, 250, 0.16);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.05) inset,
    0 24px 56px rgba(0, 0, 0, 0.32);
}

/*
 * R2 board: 8 columns × 5 rows = 40 cells.
 * Size board so each cell is the largest possible SQUARE that fits the stage.
 * cell = min(stageW/8, stageH/5)  (gap adjusted via formula)
 */
.memory-board[data-round="2"],
.memory-board[data-pairs="20"] {
  --mem-cols: 8;
  --mem-rows: 5;
  --mem-gap: 10px;
  /* Fallback if no container queries */
  --mem-gap: 10px;
  /* fallback without container queries */
  --mem-cell: min(120px, min(calc((min(1100px, 96vw) - 70px) / 8), calc((min(72dvh, 100dvh - 180px) - 40px) / 5)));
  --mem-w: calc(var(--mem-cell) * 8 + var(--mem-gap) * 7);
  --mem-h: calc(var(--mem-cell) * 5 + var(--mem-gap) * 4);

  width: var(--mem-w) !important;
  height: var(--mem-h) !important;
  max-width: 100%;
  max-height: 100%;
  gap: var(--mem-gap);
  grid-template-columns: repeat(8, 1fr) !important;
  grid-template-rows: repeat(5, 1fr) !important;
  flex: 0 0 auto;
}

/* Preferred: size from real stage box */
@supports (width: 1cqw) {
  .memory-board[data-round="2"],
  .memory-board[data-pairs="20"] {
    --mem-gap: 10px;
    --mem-cell: min(
      calc((100cqw - (8 - 1) * var(--mem-gap)) / 8),
      calc((100cqh - (5 - 1) * var(--mem-gap)) / 5)
    );
    /* Cap cell so faces stay comfortable, not tiny */
    --mem-cell: max(
      64px,
      min(
        132px,
        min(
          calc((100cqw - 70px) / 8),
          calc((100cqh - 40px) / 5)
        )
      )
    );
    width: calc(var(--mem-cell) * 8 + var(--mem-gap) * 7) !important;
    height: calc(var(--mem-cell) * 5 + var(--mem-gap) * 4) !important;
  }
}

/* R2 card chrome */
#page-game[data-round="2"] .memory-card,
#page-game[data-round="2"] .memory-board > .memory-card {
  border-radius: 14px;
}
#page-game[data-round="2"] .memory-card-inner,
#page-game[data-round="2"] .memory-card-face {
  border-radius: 14px;
}
#page-game[data-round="2"] .memory-card-back {
  background:
    radial-gradient(120% 90% at 20% 15%, rgba(196, 181, 253, 0.28), transparent 50%),
    radial-gradient(90% 80% at 85% 90%, rgba(139, 92, 246, 0.35), transparent 45%),
    linear-gradient(155deg, #2d2a48 0%, #1c1a30 48%, #12101c 100%) !important;
  border-color: rgba(167, 139, 250, 0.45) !important;
}
#page-game[data-round="2"] .memory-card-back::before {
  inset: 9%;
  border-radius: 10px;
  border-color: rgba(196, 181, 253, 0.14);
}
#page-game[data-round="2"] .memory-card-logo {
  width: 46%;
  height: 46%;
  min-width: 28px;
  min-height: 28px;
  max-width: 48px;
  max-height: 48px;
  font-size: clamp(0.9rem, 1.6vw, 1.15rem);
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.2), transparent 50%),
    linear-gradient(145deg, rgba(139, 92, 246, 0.65), rgba(76, 29, 149, 0.8));
  border-color: rgba(221, 214, 254, 0.5);
  box-shadow:
    0 6px 16px rgba(139, 92, 246, 0.4),
    0 0 0 4px rgba(139, 92, 246, 0.14);
}

/* Faces: square crop, head-biased but not sliced */
#page-game[data-round="2"] .memory-card-front {
  background: #0b1016;
}
#page-game[data-round="2"] .memory-card-front img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 22%;
  image-rendering: auto;
  transform: none;
}
#page-game[data-round="2"] .memory-card.is-flipped:not(.is-matched) .memory-card-front img {
  transform: scale(1.04);
  transform-origin: center 22%;
}
#page-game[data-round="2"] .memory-card:not(.is-flipped):not(.is-matched):hover {
  transform: translateY(-2px) scale(1.03);
  z-index: 2;
}
#page-game[data-round="2"] .memory-card:not(.is-flipped):not(.is-matched):hover .memory-card-back {
  border-color: rgba(196, 181, 253, 0.8) !important;
  filter: brightness(1.08);
}

/* Tablet: keep 8×5 if possible, slightly tighter gap */
@media (max-width: 960px) {
  .memory-hud {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .memory-board[data-round="2"],
  .memory-board[data-pairs="20"] {
    --mem-gap: 8px;
  }
  @supports (width: 1cqw) {
    .memory-board[data-round="2"],
    .memory-board[data-pairs="20"] {
      --mem-gap: 8px;
      --mem-cell: max(
        56px,
        min(
          112px,
          min(
            calc((100cqw - 48px) / 8),
            calc((100cqh - 32px) / 5)
          )
        )
      );
      width: calc(var(--mem-cell) * 8 + var(--mem-gap) * 7) !important;
      height: calc(var(--mem-cell) * 5 + var(--mem-gap) * 4) !important;
    }
  }
}

/* Narrow: 5×8 portrait stack — still square cells */
@media (max-width: 700px) {
  .memory-board[data-round="2"],
  .memory-board[data-pairs="20"] {
    --mem-cols: 5;
    --mem-rows: 8;
    --mem-gap: 7px;
    grid-template-columns: repeat(5, 1fr) !important;
    grid-template-rows: repeat(8, 1fr) !important;
  }
  @supports (width: 1cqw) {
    .memory-board[data-round="2"],
    .memory-board[data-pairs="20"] {
      --mem-gap: 7px;
      --mem-cell: max(
        52px,
        min(
          96px,
          min(
            calc((100cqw - 28px) / 5),
            calc((100cqh - 40px) / 8)
          )
        )
      );
      width: calc(var(--mem-cell) * 5 + var(--mem-gap) * 4) !important;
      height: calc(var(--mem-cell) * 8 + var(--mem-gap) * 7) !important;
    }
  }
  .memory-hud {
    gap: 6px;
    padding: 8px;
  }
  .memory-stat {
    padding: 6px 8px;
  }
  .memory-stat strong {
    font-size: 0.9rem;
  }
}

@media (max-width: 520px) {
  .memory-hud {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  #page-game .memory-lead {
    display: none;
  }
  .memory-board {
    --mem-cols: 4;
    --mem-rows: 5;
    --mem-gap: 5px;
    --mem-max-h: min(520px, calc(100dvh - 270px));
  }
}

@media (max-height: 700px) {
  #page-game > .page-header {
    margin-bottom: 4px !important;
  }
  #page-game .memory-lead,
  #page-game .memory-kicker {
    display: none;
  }
  #page-game[data-round="2"] .memory-status {
    display: none;
  }
  .memory-board {
    --mem-gap: 5px;
    --mem-max-h: calc(100dvh - 200px);
  }
}

@media (max-height: 560px) {
  .memory-status {
    display: none;
  }
  .memory-board {
    --mem-max-h: calc(100dvh - 150px);
    --mem-gap: 4px;
  }
}

/* Big desktop: bigger square tiles */
@media (min-width: 1280px) and (min-height: 820px) {
  @supports (width: 1cqw) {
    .memory-board[data-round="2"],
    .memory-board[data-pairs="20"] {
      --mem-gap: 12px;
      --mem-cell: max(
        72px,
        min(
          148px,
          min(
            calc((100cqw - 80px) / 8),
            calc((100cqh - 48px) / 5)
          )
        )
      );
      width: calc(var(--mem-cell) * 8 + var(--mem-gap) * 7) !important;
      height: calc(var(--mem-cell) * 5 + var(--mem-gap) * 4) !important;
    }
  }
}

/* ─── Cards ─── */
#page-game .memory-card,
.memory-board > .memory-card {
  appearance: none !important;
  -webkit-appearance: none !important;
  border: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  background: transparent !important;
  cursor: pointer;
  width: 100% !important;
  height: 100% !important;
  min-height: 0 !important;
  min-width: 0 !important;
  border-radius: 14px;
  position: relative !important;
  display: block !important;
  box-sizing: border-box !important;
  -webkit-tap-highlight-color: transparent;
  overflow: visible;
  aspect-ratio: auto !important;
  transition: transform 0.18s ease;
}

#page-game .memory-card:not(.is-flipped):not(.is-matched):hover {
  transform: translateY(-2px) scale(1.02);
  z-index: 2;
}

#page-game .memory-card:not(.is-flipped):not(.is-matched):active {
  transform: scale(0.97);
}

.memory-card:focus-visible {
  outline: 2px solid var(--mem-accent, #dc2626);
  outline-offset: 2px;
}

.memory-card-inner {
  position: absolute !important;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  transform-style: preserve-3d;
  transition: transform 0.48s cubic-bezier(0.2, 0.85, 0.25, 1);
  border-radius: 14px;
}

.memory-card.is-flipped .memory-card-inner,
.memory-card.is-matched .memory-card-inner {
  transform: rotateY(180deg);
}

.memory-card-face {
  position: absolute !important;
  inset: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: 14px;
  overflow: hidden;
  box-shadow:
    0 10px 24px rgba(0, 0, 0, 0.35),
    0 1px 0 rgba(255, 255, 255, 0.06) inset;
}

/* Card back — brand red glass */
.memory-card-back {
  display: flex !important;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(120% 90% at 20% 15%, rgba(248, 113, 113, 0.28), transparent 50%),
    radial-gradient(90% 80% at 85% 90%, rgba(220, 38, 38, 0.35), transparent 45%),
    linear-gradient(155deg, #2a3548 0%, #1a2433 48%, #121920 100%) !important;
  border: 1.5px solid rgba(248, 113, 113, 0.35) !important;
}

.memory-card-back::before {
  content: '';
  position: absolute;
  inset: 8%;
  border-radius: 12px;
  border: 1px dashed rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

.memory-card-logo {
  position: relative;
  z-index: 1;
  width: 44%;
  height: 44%;
  min-width: 22px;
  min-height: 22px;
  max-width: 52px;
  max-height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.2), transparent 50%),
    linear-gradient(145deg, rgba(220, 38, 38, 0.55), rgba(127, 29, 29, 0.75));
  border: 1px solid rgba(254, 202, 202, 0.45);
  color: #fff;
  font-weight: 800;
  font-size: clamp(0.85rem, 2.1vw, 1.2rem);
  line-height: 1;
  font-family: inherit;
  box-shadow:
    0 6px 16px rgba(220, 38, 38, 0.35),
    0 0 0 4px rgba(220, 38, 38, 0.12);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

#page-game[data-round="2"] .memory-card-back {
  background:
    radial-gradient(120% 90% at 20% 15%, rgba(196, 181, 253, 0.28), transparent 50%),
    radial-gradient(90% 80% at 85% 90%, rgba(139, 92, 246, 0.35), transparent 45%),
    linear-gradient(155deg, #2d2a48 0%, #1c1a30 48%, #12101c 100%) !important;
  border-color: rgba(167, 139, 250, 0.45) !important;
}

#page-game[data-round="2"] .memory-card-logo {
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.2), transparent 50%),
    linear-gradient(145deg, rgba(139, 92, 246, 0.65), rgba(76, 29, 149, 0.8));
  border-color: rgba(221, 214, 254, 0.5);
  box-shadow:
    0 6px 16px rgba(139, 92, 246, 0.4),
    0 0 0 4px rgba(139, 92, 246, 0.14);
}

.memory-card:not(.is-flipped):not(.is-matched):hover .memory-card-back {
  border-color: rgba(248, 113, 113, 0.7) !important;
  filter: brightness(1.08);
}

#page-game[data-round="2"] .memory-card:not(.is-flipped):not(.is-matched):hover .memory-card-back {
  border-color: rgba(196, 181, 253, 0.75) !important;
}

.memory-card-front {
  transform: rotateY(180deg);
  background: #0e141b;
  border: 1.5px solid rgba(255, 255, 255, 0.14);
}

.memory-card-front img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
  user-select: none;
  transition: transform 0.45s ease;
}

.memory-card.is-flipped:not(.is-matched) .memory-card-front img {
  transform: scale(1.03);
}

.memory-card.is-matched .memory-card-front {
  border-color: rgba(52, 211, 153, 0.65);
  box-shadow:
    0 0 0 2px rgba(52, 211, 153, 0.35),
    0 10px 28px rgba(0, 0, 0, 0.32),
    0 0 24px rgba(52, 211, 153, 0.18);
}

.memory-card.is-matched {
  cursor: default;
  animation: memMatchPop 0.45s cubic-bezier(0.2, 0.9, 0.3, 1);
}

.memory-card.is-matched .memory-card-front::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, transparent 50%, rgba(52, 211, 153, 0.12));
  pointer-events: none;
}

@keyframes memMatchPop {
  0% { transform: scale(1); }
  40% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* ─── Win panels ─── */
.memory-win {
  flex: 0 0 auto;
  margin-top: 0;
  padding: 14px 16px;
  border-radius: var(--radius, 16px);
  background:
    linear-gradient(135deg, rgba(52, 211, 153, 0.12), rgba(220, 38, 38, 0.08)),
    rgba(36, 48, 66, 0.88);
  border: 1px solid rgba(52, 211, 153, 0.35);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.05) inset,
    0 18px 40px rgba(0, 0, 0, 0.28);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1rem;
  width: 100%;
  box-sizing: border-box;
  animation: memWinIn 0.4s cubic-bezier(0.2, 0.85, 0.25, 1);
}

@keyframes memWinIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.memory-win.hidden {
  display: none !important;
}

.memory-win h3 {
  margin: 0 0 0.2rem;
  font-size: 1.05rem;
  font-weight: 750;
  letter-spacing: -0.02em;
  color: #fff;
}

.memory-win p {
  margin: 0;
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  font-size: 0.88rem;
}

.memory-win p strong {
  color: #e8eef4;
  font-variant-numeric: tabular-nums;
}

.memory-win-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.memory-final-detail {
  margin-top: 0.3rem !important;
  font-size: 0.8rem !important;
  opacity: 0.9;
}

/* ─── Records modal polish ─── */
.memory-records-modal {
  max-width: 500px;
  width: min(100%, 500px);
  border-radius: var(--radius-lg, 22px) !important;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, rgba(220, 38, 38, 0.12), transparent 55%),
    var(--surface-solid, #243042) !important;
  box-shadow: var(--shadow-lg, 0 28px 64px rgba(0, 0, 0, 0.4));
  overflow: hidden;
}

.memory-records-modal .modal-header h2 {
  letter-spacing: -0.03em;
}

.memory-records-body {
  padding: 0 4px 8px;
}

.memory-records-me {
  margin: 0 0 0.9rem;
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(220, 38, 38, 0.16), rgba(220, 38, 38, 0.06));
  border: 1px solid rgba(220, 38, 38, 0.3);
  font-size: 0.9rem;
  color: var(--text-secondary, rgba(255, 255, 255, 0.82));
}

.memory-records-me strong {
  color: #fff;
}

.memory-records-table-wrap {
  overflow-x: auto;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(0, 0, 0, 0.15);
}

.memory-records-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.memory-records-table th,
.memory-records-table td {
  padding: 0.7rem 0.85rem;
  text-align: left;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.memory-records-table th {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  font-weight: 700;
  background: rgba(255, 255, 255, 0.03);
}

.memory-records-table tbody tr:last-child td {
  border-bottom: 0;
}

.memory-records-table tbody tr:hover td {
  background: rgba(255, 255, 255, 0.03);
}

.memory-records-rank {
  width: 2.4rem;
  font-weight: 800;
  color: #fca5a5;
  font-variant-numeric: tabular-nums;
}

.memory-records-name {
  color: #fff;
  font-weight: 600;
}

.memory-records-time,
.memory-records-moves {
  font-variant-numeric: tabular-nums;
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  white-space: nowrap;
}

.memory-records-empty {
  text-align: center !important;
  color: var(--text-muted, rgba(255, 255, 255, 0.52));
  padding: 1.4rem 0.75rem !important;
}

.memory-records-table tbody tr:nth-child(1) .memory-records-rank { color: #f5c542; }
.memory-records-table tbody tr:nth-child(2) .memory-records-rank { color: #c0c8d4; }
.memory-records-table tbody tr:nth-child(3) .memory-records-rank { color: #d4956a; }

@media (max-height: 640px) {
  #page-game .page-header-actions .btn {
    padding: 0.4rem 0.85rem;
    font-size: 0.85rem;
  }
}


/* anticheat pending */
#page-game .memory-card.is-pending .memory-card-back {
  filter: brightness(1.12);
}
#page-game .memory-card.is-pending {
  pointer-events: none;
}
