* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(160deg, #dcfce7 0%, #bbf7d0 40%, #86efac 100%);
  color: #14532d;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
}

.logo {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  flex: none;
}

h1 {
  font-size: 1.35rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.stats {
  margin-left: auto;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.stat {
  background: rgba(255, 255, 255, 0.75);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.8rem;
  white-space: nowrap;
  box-shadow: 0 1px 2px rgba(20, 83, 45, 0.15);
}

.btn {
  border: none;
  border-radius: 999px;
  background: #166534;
  color: #f0fdf4;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 10px 18px;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 3px 0 #0f3d24, 0 4px 8px rgba(20, 83, 45, 0.25);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #0f3d24;
}

main {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 8px 12px 16px;
}

.board-wrap {
  width: min(88vw, 500px);
  aspect-ratio: 1;
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(8px, 2vw, 14px);
  width: 100%;
  height: 100%;
}

.card {
  position: relative;
  min-width: 0;
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  perspective: 800px;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0, 0.2, 1);
}

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

.face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  font-size: clamp(1.6rem, 7vw, 2.6rem);
}

.face-back {
  background: linear-gradient(150deg, #16a34a, #166534);
  color: #f0fdf4;
  border: 2px solid #14532d;
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, 0.12), 0 3px 6px rgba(20, 83, 45, 0.3);
}

.face-front {
  background: linear-gradient(160deg, #ffffff, #f0fdf4);
  border: 2px solid #4ade80;
  transform: rotateY(180deg);
}

.card.matched .card-inner {
  animation: pop 0.4s ease;
}

.card.matched .face-front {
  border-color: #22c55e;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.25);
}

.card.matched .face-back {
  display: none;
}

@keyframes pop {
  0% {
    transform: rotateY(180deg) scale(1);
  }
  50% {
    transform: rotateY(180deg) scale(1.08);
  }
  100% {
    transform: rotateY(180deg) scale(1);
  }
}

.footer {
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  font-size: 0.78rem;
  opacity: 0.8;
  text-align: center;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 83, 45, 0.45);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

.overlay-card {
  background: #f0fdf4;
  border-radius: 20px;
  padding: 28px 24px;
  text-align: center;
  max-width: 340px;
  width: 100%;
  box-shadow: 0 18px 40px rgba(20, 83, 45, 0.35);
}

.overlay-card h2 {
  margin-bottom: 10px;
}

.overlay-card p {
  margin-bottom: 18px;
  line-height: 1.5;
}

@media (max-height: 480px) {
  .topbar {
    padding: 6px 12px;
    gap: 8px;
  }
  h1 {
    font-size: 1.05rem;
  }
  .stat {
    font-size: 0.7rem;
    padding: 4px 8px;
  }
  .logo {
    width: 36px;
    height: 36px;
  }
  .btn {
    padding: 7px 12px;
    font-size: 0.85rem;
  }
  .board-wrap {
    width: min(70vh, 90vw);
  }
  main {
    padding: 4px 10px 8px;
  }
  .footer {
    display: none;
  }
}
