/* ===== Reset y base ===== */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Nunito", "Baloo 2", "Comic Sans MS", "Segoe UI", system-ui, sans-serif;
  background: linear-gradient(180deg, #b8f2d8 0%, #8fe6c3 40%, #5fd2a0 100%);
  color: #3d4a3d;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ===== Fondo animado de selva ===== */
.jungle-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.leaf {
  position: absolute;
  width: clamp(80px, 18vw, 220px);
  height: clamp(80px, 18vw, 220px);
  background: rgba(46, 139, 87, 0.18);
  border-radius: 0 80% 0 80%;
  transform-origin: bottom left;
  animation: sway 7s ease-in-out infinite;
}

.leaf::after {
  content: "";
  position: absolute;
  inset: 10%;
  border-radius: 0 80% 0 80%;
  background: rgba(34, 120, 70, 0.12);
}

.leaf-1 { top: -5%; left: -5%; transform: rotate(-20deg); animation-delay: 0s; }
.leaf-2 { top: 10%; right: -8%; transform: rotate(110deg) scaleX(-1); animation-delay: 1.2s; }
.leaf-3 { bottom: -8%; left: 8%; transform: rotate(200deg); animation-delay: 2s; }
.leaf-4 { bottom: 5%; right: -5%; transform: rotate(260deg) scaleX(-1); animation-delay: 0.7s; }
.leaf-5 { top: 40%; left: -10%; transform: rotate(-45deg); animation-delay: 3s; }
.leaf-6 { top: 35%; right: -10%; transform: rotate(135deg) scaleX(-1); animation-delay: 1.8s; }

@keyframes sway {
  0%, 100% { transform: rotate(var(--r, 0deg)); }
  50% { transform: rotate(calc(var(--r, 0deg) + 8deg)); }
}

.leaf-1 { --r: -20deg; }
.leaf-2 { --r: 110deg; }
.leaf-3 { --r: 200deg; }
.leaf-4 { --r: 260deg; }
.leaf-5 { --r: -45deg; }
.leaf-6 { --r: 135deg; }

/* ===== App y pantallas ===== */
.app {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 980px;
  height: 100%;
  padding: clamp(12px, 3vw, 28px);
  display: flex;
  flex-direction: column;
}

.screen {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.5s ease;
}

.screen.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.card-panel {
  background: rgba(255, 255, 255, 0.92);
  border-radius: 28px;
  padding: clamp(24px, 5vw, 44px);
  box-shadow:
    0 18px 50px rgba(30, 90, 60, 0.18),
    0 0 0 6px rgba(255, 255, 255, 0.35);
  text-align: center;
  max-width: 520px;
  width: 92%;
}

/* ===== Pantalla de inicio ===== */
.start-panel .mascot {
  width: clamp(110px, 28vw, 160px);
  margin: 0 auto 16px;
}

.start-panel .mascot svg {
  width: 100%;
  height: auto;
  filter: drop-shadow(0 10px 18px rgba(70, 140, 100, 0.2));
}

.title {
  font-size: clamp(1.8rem, 6vw, 2.8rem);
  margin: 0 0 8px;
  color: #2a6b4a;
  letter-spacing: -0.5px;
  text-shadow: 1px 1px 0 #fff;
}

.subtitle {
  font-size: clamp(1rem, 3vw, 1.25rem);
  margin: 0 0 22px;
  color: #5a7a65;
  font-weight: 600;
}

.instructions {
  text-align: left;
  margin: 0 0 28px;
  padding-left: 22px;
  color: #4c6354;
  font-size: clamp(0.95rem, 2.6vw, 1.05rem);
  line-height: 1.55;
}

.instructions li {
  margin-bottom: 8px;
}

/* ===== Botones ===== */
.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-weight: 800;
  font-size: 1.05rem;
  padding: 14px 32px;
  border-radius: 50px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.12);
}

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

.btn--primary {
  background: linear-gradient(135deg, #ffcf56 0%, #ff9f6b 100%);
  color: #5a3a1a;
}

.btn--primary:hover {
  box-shadow: 0 10px 28px rgba(255, 159, 107, 0.35);
}

.btn--secondary {
  background: #fff;
  color: #3d8b63;
  border: 2px solid #8ddbb5;
  padding: 12px 26px;
  font-size: 0.95rem;
}

.btn--secondary:hover {
  background: #f0fff7;
}

/* ===== HUD ===== */
.hud {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: clamp(10px, 3vw, 24px);
  margin-bottom: clamp(14px, 3vw, 24px);
  flex-wrap: wrap;
}

.hud-item {
  background: rgba(255, 255, 255, 0.88);
  border-radius: 18px;
  padding: 10px 18px;
  min-width: 92px;
  box-shadow: 0 6px 18px rgba(30, 90, 60, 0.12);
  text-align: center;
}

.hud-label {
  display: block;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: #6a8a75;
  margin-bottom: 2px;
  font-weight: 700;
}

.hud-value {
  display: block;
  font-size: 1.25rem;
  font-weight: 800;
  color: #2a6b4a;
}

.hearts {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.heart {
  color: #ff6b6b;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.heart.lost {
  opacity: 0.25;
  transform: scale(0.8);
}

/* ===== Tablero ===== */
.screen--game {
  justify-content: flex-start;
  padding-top: clamp(10px, 2vh, 24px);
}

.board {
  display: grid;
  gap: clamp(8px, 2vw, 16px);
  width: 100%;
  margin: 0 auto;
  perspective: 1000px;
  justify-content: center;
  align-content: center;
  flex: 1;
}

/* ===== Carta ===== */
.card {
  aspect-ratio: 1 / 1;
  width: 100%;
  cursor: pointer;
  perspective: 800px;
  user-select: none;
  -webkit-user-select: none;
  border-radius: 16px;
}

.card__inner {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 16px;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 6px 14px rgba(30, 90, 60, 0.15);
}

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

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

.card.matched .card__front {
  box-shadow: inset 0 0 0 4px #8fe6a8;
}

.card.shake .card__inner {
  animation: shake 0.45s ease;
}

.card.pop .card__inner {
  animation: pop 0.45s ease;
}

@keyframes shake {
  0%, 100% { transform: rotateY(180deg) translateX(0); }
  20% { transform: rotateY(180deg) translateX(-6px); }
  40% { transform: rotateY(180deg) translateX(6px); }
  60% { transform: rotateY(180deg) translateX(-4px); }
  80% { transform: rotateY(180deg) translateX(4px); }
}

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

.card__face {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card__back {
  background:
    radial-gradient(circle at 30% 30%, #6dd5a8 0%, #3eb380 60%, #2a9d6e 100%);
  transform: rotateY(0deg);
}

.card__back::before {
  content: "?";
  font-size: clamp(1.8rem, 6vw, 3rem);
  font-weight: 900;
  color: rgba(255, 255, 255, 0.9);
  text-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
}

.card__back::after {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 12px;
  border: 3px dashed rgba(255, 255, 255, 0.35);
  pointer-events: none;
}

.card__front {
  background: #fffaf4;
  transform: rotateY(180deg);
  padding: 10%;
}

.card__front svg {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 4px 8px rgba(70, 50, 30, 0.1));
}

/* ===== Layouts responsivos por nivel ===== */
/* 8 cartas -> 4 columnas x 2 filas */
.board.cols-4 {
  grid-template-columns: repeat(4, minmax(0, 112px));
}

/* 10 cartas -> 5 columnas x 2 filas */
.board.cols-5 {
  grid-template-columns: repeat(5, minmax(0, 100px));
}

/* 12 cartas -> 4 columnas x 3 filas */
.board.cols-3x4 {
  grid-template-columns: repeat(4, minmax(0, 100px));
}

/* 14 cartas -> 7 columnas x 2 filas */
.board.cols-7 {
  grid-template-columns: repeat(7, minmax(0, 84px));
}

/* 16 cartas -> 4 columnas x 4 filas */
.board.cols-4x4 {
  grid-template-columns: repeat(4, minmax(0, 100px));
}

/* 20 cartas -> 5 columnas x 4 filas */
.board.cols-5x4 {
  grid-template-columns: repeat(5, minmax(0, 92px));
}

/* 24 cartas -> 6 columnas x 4 filas */
.board.cols-6x4 {
  grid-template-columns: repeat(6, minmax(0, 88px));
}

/* ===== Ajustes móviles ===== */
@media (max-width: 600px) {
  .board.cols-7 {
    grid-template-columns: repeat(4, minmax(0, 78px));
    grid-template-rows: repeat(4, auto);
  }

  .board.cols-6x4 {
    grid-template-columns: repeat(4, minmax(0, 74px));
    grid-template-rows: repeat(6, auto);
  }

  .board.cols-5x4 {
    grid-template-columns: repeat(4, minmax(0, 76px));
    grid-template-rows: repeat(5, auto);
  }

  .board.cols-4,
  .board.cols-5,
  .board.cols-3x4,
  .board.cols-4x4 {
    grid-template-columns: repeat(4, minmax(0, 78px));
  }

  .board.cols-5 {
    grid-template-columns: repeat(5, minmax(0, 64px));
  }

  .board.cols-3x4 {
    grid-template-columns: repeat(3, minmax(0, 88px));
  }

  .card__front {
    padding: 8%;
  }
}

@media (max-width: 360px) {
  .board.cols-4,
  .board.cols-4x4,
  .board.cols-3x4,
  .board.cols-5x4,
  .board.cols-6x4,
  .board.cols-7,
  .board.cols-5 {
    grid-template-columns: repeat(4, minmax(0, 64px));
    grid-template-rows: auto;
  }
}

/* ===== Footer del juego ===== */
.game-footer {
  margin-top: clamp(12px, 2.5vh, 22px);
  text-align: center;
}

/* ===== Modales ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(30, 80, 55, 0.45);
  backdrop-filter: blur(3px);
  animation: fadeIn 0.3s ease;
}

.modal.hidden {
  display: none;
}

.modal-panel {
  max-width: 420px;
  width: 88%;
  animation: popModal 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popModal {
  from { opacity: 0; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1); }
}

.modal-icon {
  font-size: 3.2rem;
  margin-bottom: 8px;
}

.modal-icon--big {
  font-size: 4.2rem;
}

.modal-title {
  margin: 0 0 10px;
  color: #2a6b4a;
  font-size: clamp(1.5rem, 5vw, 2rem);
}

.modal-text {
  margin: 0 0 24px;
  color: #5a6f62;
  font-size: 1.05rem;
  line-height: 1.45;
}

/* ===== Utilidades ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.rotate-device, .rotate-device.active { display: none !important; }
