:root {
  --bg: #0a1628;
  --card-back: linear-gradient(145deg, #1a56db, #2563eb);
  --card-back-pattern: radial-gradient(circle at 30% 30%, rgba(255,255,255,0.15) 0%, transparent 50%);
  --accent: #3b82f6;
  --accent-dark: #1a56db;
  --gold: #fbbf24;
  --green: #06d6a0;
  --surface: rgba(255,255,255,0.08);
  --text: #fff;
  --text-dim: rgba(255,255,255,0.6);
  --radius: 16px;
  --font: 'Fredoka', sans-serif;
}

/* ---- Reset ---- */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  background: var(--bg);
  font-family: var(--font);
  color: var(--text);
  min-height: 100vh;
  min-height: -webkit-fill-available;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ---- Hidden ---- */
.hidden {
  display: none !important;
}

/* ---- Shared Buttons ---- */
.btn {
  font-family: var(--font);
  font-weight: 600;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  transform: scale(0.95);
}

.btn-play {
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  font-size: 20px;
  padding: 16px 44px;
  border-radius: 50px;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4);
  animation: glow-pulse 2s ease-in-out infinite;
}

.btn-icon {
  font-size: 18px;
}

.btn-restart {
  background: rgba(255,255,255,0.12);
  color: var(--text);
  font-size: 15px;
  padding: 14px 36px;
  border-radius: 30px;
  border: 1px solid rgba(255,255,255,0.18);
}

.btn-restart:active {
  background: rgba(255,255,255,0.12);
}

/* ---- Screen ---- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* ---- Start Screen ---- */
.start-screen {
  align-items: center;
  justify-content: center;
  background: var(--bg);
  overflow: hidden;
}

.start-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.start-bg-pattern {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 20% 80%, rgba(59, 130, 246, 0.15) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 20%, rgba(251, 191, 36, 0.1) 0%, transparent 60%);
  pointer-events: none;
}

.start-flags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 280px;
  margin-bottom: 28px;
}

.start-flag {
  width: 48px;
  height: 32px;
  border-radius: 6px;
  object-fit: cover;
  border: 2px solid rgba(255,255,255,0.2);
  animation: float-in 0.6s ease backwards;
}

.start-flag:nth-child(odd) {
  animation-delay: 0.1s;
}

.start-flag:nth-child(even) {
  animation-delay: 0.25s;
}

.start-flag:nth-child(3n) {
  animation-delay: 0.4s;
}

.start-title {
  font-size: 52px;
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
  margin-bottom: 10px;
  background: linear-gradient(135deg, #fff 30%, var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.start-subtitle {
  font-size: 18px;
  color: var(--text-dim);
  margin-bottom: 40px;
  text-align: center;
}

/* ---- Game Screen ---- */
.game-screen {
  background: var(--bg);
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px 10px;
}

.home-btn {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.1);
  color: var(--text);
  font-size: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
  touch-action: manipulation;
}
.home-btn:active { transform: scale(0.9); }

.game-title {
  font-size: 20px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.game-progress {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress-bar {
  width: 100px;
  height: 8px;
  background: var(--surface);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--gold));
  border-radius: 4px;
  transition: width 0.4s ease;
}

.progress-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  min-width: 40px;
  text-align: right;
}

/* ---- Game Scroll Area ---- */
.game-scroll-area {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* ---- Card Grid ---- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  max-width: 400px;
  width: 100%;
  padding: 4px 0;
}

/* ---- Card Wrapper ---- */
.card-wrapper {
  perspective: 800px;
  aspect-ratio: 3 / 4;
  touch-action: manipulation;
}

/* ---- Card ---- */
.card {
  position: relative;
  width: 100%;
  height: 100%;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  border-radius: var(--radius);
}

.card.flipped,
.card.matched {
  transform: rotateY(180deg);
}

.card.flipped .card-back,
.card.matched .card-back {
  visibility: hidden;
}

.card.matched .card-front {
  box-shadow: 0 0 0 3px var(--gold), 0 0 24px rgba(251, 191, 36, 0.3);
}

/* ---- Card Faces ---- */
.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

/* ---- Card Back ---- */
.card-back {
  background: var(--card-back);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}

.card-back::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--card-back-pattern);
  border-radius: var(--radius);
  pointer-events: none;
}

.card-back-icon {
  font-size: 44px;
  position: relative;
  z-index: 1;
  opacity: 0.9;
  font-weight: 700;
  color: #fff;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}

/* ---- Card Front ---- */
.card-front {
  transform: rotateY(180deg);
  background: #0d1f3d;
  display: flex;
  flex-direction: column;
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
  transition: box-shadow 0.3s ease;
}

.card-photo {
  width: 100%;
  flex: 1;
  object-fit: contain;
  padding: 8px;
  display: block;
}

.card-name {
  padding: 5px 4px;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  background: #0d1f3d;
  letter-spacing: 0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Card Interactions ---- */
@media (hover: hover) {
  .card-wrapper:hover .card:not(.flipped):not(.matched) {
    transform: scale(1.03);
  }
}
.card-wrapper:active .card:not(.flipped):not(.matched) {
  transform: scale(0.97);
}

.card.match-celebrate {
  animation: match-boom 0.5s ease;
}

/* ---- Game Footer ---- */
.game-footer {
  padding: 10px 20px 16px;
  display: flex;
  justify-content: center;
}

/* ---- Win Screen ---- */
.win-screen {
  background: var(--bg);
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.win-content {
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

.win-burst {
  position: absolute;
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.2) 0%, transparent 70%);
  border-radius: 50%;
  animation: burst-spin 8s linear infinite;
  pointer-events: none;
}

.win-emoji {
  font-size: 80px;
  margin-bottom: 16px;
  animation: bounce-in 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.win-title {
  font-size: 48px;
  font-weight: 700;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--gold), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.win-subtitle {
  font-size: 18px;
  color: var(--text-dim);
  margin-bottom: 28px;
}

.flag-parade {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 32px;
  max-width: 300px;
}

.parade-flag {
  width: 64px;
  height: 42px;
  object-fit: cover;
  border-radius: 8px;
  border: 3px solid var(--gold);
  box-shadow: 0 2px 12px rgba(251, 191, 36, 0.3);
  animation: pop-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.parade-flag:nth-child(1) { animation-delay: 0.05s; }
.parade-flag:nth-child(2) { animation-delay: 0.1s; }
.parade-flag:nth-child(3) { animation-delay: 0.15s; }
.parade-flag:nth-child(4) { animation-delay: 0.2s; }
.parade-flag:nth-child(5) { animation-delay: 0.25s; }
.parade-flag:nth-child(6) { animation-delay: 0.3s; }

.win-home-link {
  margin-top: 12px;
  color: var(--text-dim);
  font-size: 15px;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
}

.win-home-link:hover {
  color: var(--text);
}

/* ---- Confetti ---- */
.confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1000;
  overflow: hidden;
}

.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  top: -20px;
  animation: confetti-fall linear forwards;
}

/* ---- Keyframes ---- */
@keyframes glow-pulse {
  0%, 100% {
    box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4);
  }
  50% {
    box-shadow: 0 4px 35px rgba(59, 130, 246, 0.6), 0 0 60px rgba(59, 130, 246, 0.2);
  }
}

@keyframes float-in {
  0% {
    opacity: 0;
    transform: translateY(20px) scale(0.8);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes pop-in {
  0% {
    opacity: 0;
    transform: scale(0);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes match-boom {
  0% {
    transform: rotateY(180deg) scale(1);
  }
  40% {
    transform: rotateY(180deg) scale(1.15);
  }
  100% {
    transform: rotateY(180deg) scale(1);
  }
}

@keyframes bounce-in {
  0% {
    opacity: 0;
    transform: scale(0.3);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes burst-spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes confetti-fall {
  0% {
    transform: translateY(0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(100vh) rotate(720deg);
    opacity: 0;
  }
}

@keyframes card-enter {
  0% {
    opacity: 0;
    transform: scale(0.5) translateY(20px);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* ---- Scrollbar ---- */
.game-scroll-area::-webkit-scrollbar {
  width: 4px;
}

.game-scroll-area::-webkit-scrollbar-track {
  background: transparent;
}

.game-scroll-area::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 2px;
}

.game-scroll-area::-webkit-scrollbar-thumb:hover {
  background: rgba(255,255,255,0.25);
}
