/* Memory Game — visual adaptation for MargoStory.
   Flip animation and layout structure follow SEN LLC Memory Game (MIT). */

.memory-app {
  --mg-bg: #F7F2EA;
  --mg-surface: #FFF8F1;
  --mg-surface2: #F3E6D4;
  --mg-border: #E4D3BA;
  --mg-text: #3F352C;
  --mg-muted: #8A7460;
  --mg-accent: #C9A86A;
  --mg-accent-h: #B48B43;
  --mg-card-back: linear-gradient(145deg, #EFE2D0 0%, #E4D3BA 100%);
  --mg-card-face: #FFF8F1;
  --mg-card-matched: #E8D5B0;
  --mg-shadow: 0 8px 22px rgba(0, 0, 0, 0.08);
  --mg-radius: 18px;
  --mg-transition: 0.2s ease;
  color: var(--mg-text);
  font-family: "Poppins", sans-serif;
  width: 100%;
  max-width: min(680px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.memory-app * {
  box-sizing: border-box;
}

.memory-app .header-title h1,
.memory-app .win-box h2 {
  font-family: "Cormorant Garamond", serif;
  font-weight: 600;
  color: #4B3D2F;
}

.memory-app .header-title h1 {
  font-size: 28px;
  line-height: 1.1;
}

.memory-app .subtitle {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--mg-muted);
}

.memory-app .controls {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-wrap: wrap;
  background: var(--mg-surface);
  border-radius: 14px;
  padding: 8px 12px;
  box-shadow: var(--mg-shadow);
}

.memory-app .control-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.memory-app .control-group label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--mg-muted);
}

.memory-app .control-group select {
  background: var(--mg-surface2);
  border: 1px solid var(--mg-border);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  color: var(--mg-text);
  min-width: 128px;
}

.memory-app .control-group select:focus {
  outline: none;
  border-color: var(--mg-accent);
}

.memory-app .reset-btn,
.memory-app .win-box button {
  margin-left: auto;
  background: var(--mg-accent);
  color: #3F352C;
  border: none;
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.12);
}

.memory-app .reset-btn:hover,
.memory-app .win-box button:hover {
  background: var(--mg-accent-h);
  color: #fff;
}

.memory-app .stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.memory-app .stat {
  background: var(--mg-surface);
  border-radius: 14px;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  box-shadow: var(--mg-shadow);
}

.memory-app .stat-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--mg-muted);
  text-align: center;
}

.memory-app .stat-value {
  font-family: "Cormorant Garamond", serif;
  font-size: 20px;
  font-weight: 600;
  color: #5C4A3A;
  font-variant-numeric: tabular-nums;
}

.memory-app .grid {
  display: grid;
  gap: clamp(4px, 0.7vw, 8px);
  width: 100%;
  max-width: min(100%, 520px);
  margin: 0 auto;
  height: min(56vh, calc(100dvh - 270px));
  max-height: 520px;
  grid-auto-rows: minmax(0, 1fr);
  direction: ltr;
}

/* Reset MargoStory homepage .card rules inside the game */
.memory-app .card {
  aspect-ratio: auto;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  cursor: pointer;
  perspective: 600px;
  background: none;
  border-radius: 10px;
  overflow: visible;
  box-shadow: none;
  animation: none;
  transform: none;
  transition: none;
}

.memory-app .card:hover {
  transform: none;
  box-shadow: none;
}

.memory-app .card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 10px;
}

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

.memory-app .card-front,
.memory-app .card-back {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  box-shadow: 0 6px 16px rgba(63, 53, 44, 0.12);
}

.memory-app .card-front {
  background: var(--mg-card-back);
  background-image:
    repeating-linear-gradient(
      135deg,
      transparent 0 12px,
      rgba(201, 168, 106, 0.12) 12px 24px
    );
  border: 1px solid rgba(201, 168, 106, 0.35);
}

.memory-app .card-front::after {
  content: "";
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(180, 139, 67, 0.45);
}

.memory-app .card-back {
  background: var(--mg-card-face);
  color: #4B3D2F;
  transform: rotateY(180deg);
  font-weight: 600;
  font-size: clamp(0.85rem, 2.4vw, 1.45rem);
  border: 1px solid var(--mg-border);
}

.memory-app .card.matched .card-back {
  background: var(--mg-card-matched);
  color: #5C4A3A;
}

.memory-app .card-back svg {
  width: 58%;
  height: 58%;
  max-width: 52px;
  max-height: 52px;
}

.memory-app .card:not(.matched):hover .card-inner {
  transform: translateY(-2px) scale(1.02);
}

.memory-app .card.flipped:not(.matched):hover .card-inner {
  transform: rotateY(180deg) translateY(-3px) scale(1.03);
}

.memory-app .card:focus-visible .card-inner {
  outline: 3px solid var(--mg-accent);
  outline-offset: 2px;
}

.memory-app .win-overlay {
  position: absolute;
  inset: 0;
  background: rgba(63, 53, 44, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  border-radius: 24px;
}

.memory-app .win-overlay[hidden] {
  display: none;
}

.memory-app .win-box {
  background: var(--mg-surface);
  border-radius: 24px;
  padding: 32px 28px;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.16);
  max-width: 340px;
  width: 90%;
}

.memory-app .win-box h2 {
  font-size: 34px;
  margin-bottom: 8px;
}

.memory-app .win-box p {
  color: var(--mg-muted);
  margin-bottom: 18px;
  font-size: 14px;
  line-height: 1.5;
}

.memory-app .win-box button {
  margin-left: 0;
}

.memory-app .credit {
  font-size: 10px;
  color: var(--mg-muted);
  text-align: center;
  opacity: 0.85;
  margin: 0;
}

.memory-app .board-wrap {
  position: relative;
  background: var(--mg-surface);
  border-radius: 18px;
  padding: 8px;
  box-shadow: var(--mg-shadow);
}

#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 80;
  display: none;
}

.memory-overlay {
  padding: 10px 16px 16px;
}

.memory-overlay-bar {
  margin-bottom: 2px;
}

.memory-close {
  margin: 0 0 8px;
  padding: 8px 14px;
}

@media (max-width: 1023px) {
  .memory-app {
    max-width: 100%;
  }

  .memory-app .grid {
    max-width: 100%;
    height: min(52vh, calc(100dvh - 260px));
  }
}

@media (max-width: 700px) {
  .memory-overlay {
    height: 100dvh;
    max-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    padding: 8px 10px 12px;
    box-sizing: border-box;
  }

  .memory-overlay-bar {
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 4px;
  }

  .memory-app {
    max-width: 100%;
    gap: 6px;
  }

  .memory-app .stats {
    grid-template-columns: repeat(2, 1fr);
  }

  .memory-app .controls {
    gap: 8px;
    padding: 8px;
  }

  .memory-app .control-group select {
    min-width: 0;
    width: 100%;
  }

  .memory-app .reset-btn {
    width: 100%;
    margin-left: 0;
  }

  .memory-app .header-title h1 {
    font-size: 22px;
  }

  .memory-app .grid {
    height: min(48dvh, calc(100dvh - 280px));
    min-height: 180px;
    max-height: none;
    max-width: 100%;
  }

  .memory-app .card {
    min-height: 44px;
  }

  .memory-app .board-wrap {
    padding: 6px;
  }
}

@media (max-width: 700px) and (orientation: landscape) {
  .memory-app .header-title h1 {
    font-size: 18px;
  }

  .memory-app .grid {
    height: min(62dvh, calc(100dvh - 160px));
    min-height: 140px;
  }
}

@media (min-width: 1100px) {
  .memory-app {
    max-width: min(740px, 100%);
    gap: 10px;
  }

  .memory-app .header-title h1 {
    font-size: 30px;
  }

  .memory-app .grid {
    height: min(60vh, calc(100dvh - 250px));
    max-height: 580px;
    max-width: min(100%, 560px);
  }
}
