.memory-stage {
  padding: clamp(1rem, 3vw, 2rem) 0 clamp(2rem, 5vw, 4rem);
}

.memory-layout {
  width: min(100%, 68rem);
}

.memory-back-link {
  display: inline-block;
  margin-bottom: 0.75rem;
  padding: 0.45rem 0.75rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 4px 14px rgba(33, 37, 41, 0.14);
  color: #0056b3;
  font-weight: 700;
}

.memory-back-link:hover {
  color: #003f84;
  text-decoration: none;
}

.memory-back-link:focus-visible,
.memory-button:focus-visible,
.memory-card:focus-visible {
  outline: 3px solid #80bdff;
  outline-offset: 3px;
}

.memory-panel {
  padding: clamp(1rem, 3vw, 2rem);
  border: 1px solid rgba(0, 0, 0, 0.08);
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 16px 44px rgba(33, 37, 41, 0.22);
}

.memory-header {
  max-width: 42rem;
  margin: 0 auto;
}

.memory-header h1 {
  margin: 0;
  font-size: clamp(2rem, 5vw, 3rem);
}

.memory-header p:last-child {
  margin: 0.5rem 0 0;
  color: #495057;
  font-size: clamp(0.95rem, 2vw, 1.1rem);
}

.memory-kicker {
  margin: 0 0 0.15rem;
  color: #0069d9;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.memory-scoreboard {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  max-width: 42rem;
  margin: 1.25rem auto 0;
  overflow: hidden;
  border: 1px solid #d6dde3;
  border-radius: 12px;
  background: #f8f9fa;
}

.memory-scoreboard div {
  display: flex;
  min-width: 0;
  padding: 0.65rem 0.5rem;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.memory-scoreboard div + div {
  border-left: 1px solid #d6dde3;
}

.memory-scoreboard span {
  color: #6c757d;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.memory-scoreboard output {
  margin-top: 0.1rem;
  color: #212529;
  font-size: 1rem;
  font-weight: 700;
}

.memory-status {
  min-height: 1.5rem;
  margin: 1rem auto 0.75rem;
  color: #495057;
  font-weight: 700;
  text-align: center;
}

.memory-status[data-state="success"] {
  color: #176b32;
}

.memory-status[data-state="miss"] {
  color: #a33b20;
}

.memory-board {
  --memory-columns: 2;
  display: grid;
  grid-template-columns: repeat(var(--memory-columns), minmax(0, 1fr));
  gap: clamp(0.35rem, 1.2vw, 0.75rem);
  width: min(100%, calc(var(--memory-columns) * 6.5rem));
  margin: 0 auto;
}

.memory-card {
  position: relative;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 13px;
  aspect-ratio: 4 / 5;
  background: transparent;
  color: #212529;
  cursor: pointer;
  perspective: 700px;
  -webkit-tap-highlight-color: transparent;
}

.memory-card-inner {
  position: absolute;
  inset: 0;
  display: block;
  border-radius: inherit;
  box-shadow: 0 5px 12px rgba(33, 37, 41, 0.2);
  transform-style: preserve-3d;
  transition: transform 360ms ease;
}

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

.memory-card-face {
  position: absolute;
  inset: 0;
  display: flex;
  border: 2px solid rgba(0, 0, 0, 0.12);
  border-radius: inherit;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.memory-card-back {
  background-color: #087fbd;
  background-image:
    radial-gradient(circle at 25% 25%, rgba(255, 255, 255, 0.3) 0 7%, transparent 8%),
    radial-gradient(circle at 75% 75%, rgba(255, 255, 255, 0.22) 0 7%, transparent 8%);
  background-size: 1.8rem 1.8rem;
  color: #fff;
  font-size: clamp(1.35rem, 4vw, 2.1rem);
  font-weight: 700;
}

.memory-card-front {
  background: #fffdf6;
  font-size: clamp(1.5rem, 5vw, 2.8rem);
  transform: rotateY(180deg);
}

.memory-card:hover .memory-card-back {
  background-color: #066da3;
}

.memory-card.is-matched .memory-card-front {
  border-color: #39a85a;
  background: #e9f8ed;
  box-shadow: inset 0 0 0 3px rgba(57, 168, 90, 0.18);
}

.memory-card:disabled {
  cursor: default;
  opacity: 1;
}

.memory-board.is-complete .memory-card.is-matched {
  animation: memory-celebrate 520ms ease both;
}

.memory-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1.25rem;
}

.memory-button {
  min-height: 44px;
  padding: 0.65rem 1rem;
  border: 1px solid #0069d9;
  border-radius: 9px;
  background: #007bff;
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.memory-button:hover {
  border-color: #005cbf;
  background: #0069d9;
}

.memory-button-secondary {
  border-color: #6c757d;
  background: #fff;
  color: #495057;
}

.memory-button-secondary:hover {
  border-color: #495057;
  background: #eef1f3;
  color: #212529;
}

@keyframes memory-celebrate {
  50% {
    transform: translateY(-6px) rotate(1.5deg);
  }
}

@media (max-width: 575.98px) {
  .memory-stage {
    padding-top: 0.75rem;
  }

  .memory-panel {
    padding: 1rem 0.65rem 1.25rem;
    border-radius: 15px;
  }

  .memory-scoreboard {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .memory-scoreboard div:nth-child(3) {
    border-left: 0;
  }

  .memory-scoreboard div:nth-child(n + 3) {
    border-top: 1px solid #d6dde3;
  }

  .memory-board {
    width: min(100%, calc(var(--memory-columns) * 5.25rem));
  }

  .memory-card-back {
    background-size: 1.35rem 1.35rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .memory-card-inner {
    transition: none;
  }

  .memory-board.is-complete .memory-card.is-matched {
    animation: none;
  }
}
