:root {
  --cream: #FFF6E9;
  --yellow: #FFC93C;
  --sky: #4FB8E8;
  --pink: #FF6F91;
  --green: #58C97B;
  --ink: #2E2A4D;
  --white: #FFFFFF;
  --red: #E85757;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--cream);
  color: var(--ink);
  font-family: 'Quicksand', sans-serif;
  font-weight: 600;
  min-height: 100vh;
  padding-bottom: 3rem;
}

.back-link {
  display: inline-block;
  margin: 1.4rem 0 0 1.6rem;
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  color: var(--sky);
  text-decoration: none;
  font-size: 1rem;
}
.back-link:hover { text-decoration: underline; }

.game-header {
  text-align: center;
  padding: 1.2rem 1.5rem .5rem;
}
.game-header h1 {
  font-family: 'Baloo 2', sans-serif;
  font-size: clamp(1.8rem, 5vw, 2.6rem);
  margin: 0 0 .4rem;
}
.game-header p {
  color: #6a6486;
  margin: 0;
}

.hud {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1.4rem;
  flex-wrap: wrap;
  margin: 1.6rem 0;
  font-family: 'Baloo 2', sans-serif;
}
.hud-item {
  background: var(--white);
  padding: .5rem 1.1rem;
  border-radius: 999px;
  box-shadow: 0 4px 0 rgba(46,42,77,0.08);
}
#restart {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  background: var(--pink);
  color: var(--white);
  border: none;
  padding: .55rem 1.3rem;
  border-radius: 999px;
  box-shadow: 0 5px 0 #c94f6e;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease;
}
#restart:hover { transform: translateY(2px); box-shadow: 0 3px 0 #c94f6e; }

.game-layout {
  display: flex;
  gap: 1.6rem;
  max-width: 820px;
  margin: 0 auto;
  padding: 0 1rem;
  align-items: flex-start;
  justify-content: center;
}

.board {
  max-width: 560px;
  flex: 1 1 480px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: .8rem;
}

.leaderboard {
  flex: 0 0 220px;
  background: var(--white);
  border-radius: 22px;
  padding: 1.2rem 1.1rem;
  box-shadow: 0 6px 0 rgba(46,42,77,0.08);
  position: sticky;
  top: 1.5rem;
}
.leaderboard h2 {
  font-family: 'Baloo 2', sans-serif;
  font-size: 1.15rem;
  margin: 0 0 .2rem;
  text-align: center;
}
.leaderboard-sub {
  font-size: .75rem;
  color: #9089a8;
  text-align: center;
  margin: 0 0 .9rem;
}
.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .4rem;
  max-height: 480px;
  overflow-y: auto;
}
.leaderboard-list li {
  display: flex;
  align-items: center;
  gap: .5rem;
  font-size: .85rem;
  background: var(--cream);
  border-radius: 10px;
  padding: .4rem .6rem;
}
.leaderboard-list li .rank {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  color: var(--sky);
  width: 1.4rem;
  flex-shrink: 0;
}
.leaderboard-list li:nth-child(1) .rank { color: #E8B923; }
.leaderboard-list li:nth-child(2) .rank { color: #9AA0A6; }
.leaderboard-list li:nth-child(3) .rank { color: #C97A3C; }
.leaderboard-list li .lb-name {
  flex: 1;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leaderboard-list li .lb-time {
  font-variant-numeric: tabular-nums;
  color: #6a6486;
}
.leaderboard-empty {
  text-align: center;
  font-size: .85rem;
  color: #9089a8;
  padding: 1rem 0;
}

@media (max-width: 780px) {
  .game-layout { flex-direction: column; align-items: stretch; }
  .leaderboard { flex: none; position: static; max-width: 560px; margin: 0 auto; width: 100%; }
  .leaderboard-list { max-height: 220px; }
}

.tile {
  aspect-ratio: 1 / 1;
  perspective: 800px;
  cursor: pointer;
  /* retire le style par défaut des boutons (cadre gris) */
  border: none;
  background: none;
  padding: 0;
}
.tile-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform .4s cubic-bezier(.4,.2,.2,1);
  transform-style: preserve-3d;
}
.tile.flipped .tile-inner,
.tile.matched .tile-inner {
  transform: rotateY(180deg);
}

.tile-face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 18px;
  backface-visibility: hidden;
  font-size: 2rem;
}
/* Dos de carte : dégradé, petits pois et gros point d'interrogation */
.tile-back {
  background:
    radial-gradient(circle, rgba(255,255,255,0.22) 2px, transparent 2.5px) 0 0 / 16px 16px,
    linear-gradient(135deg, #7B6CFF, #FF6F91);
  box-shadow: 0 5px 0 rgba(46,42,77,0.15), inset 0 0 0 4px rgba(255,255,255,0.35);
  transition: transform .15s ease;
}
.tile-back span {
  display: grid;
  place-items: center;
  width: 52%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(255,255,255,0.95);
  color: #7B6CFF;
  font-family: 'Baloo 2', sans-serif;
  font-weight: 800;
  font-size: clamp(1.3rem, 4vw, 2.2rem);
  box-shadow: 0 4px 0 rgba(46,42,77,0.15);
}
.tile:hover:not(.flipped):not(.matched) .tile-back { transform: translateY(-3px) rotate(-2deg); }

/* Face : le dessin de l'animal sur son fond coloré */
.tile-front {
  flex-direction: column;
  gap: 0;
  padding: 6% 6% 5%;
  background: linear-gradient(135deg, var(--bg1), var(--bg2));
  transform: rotateY(180deg);
  box-shadow: 0 5px 0 rgba(46,42,77,0.12);
  overflow: hidden;
}
.tile-front svg {
  flex: 1;
  width: 100%;
  min-height: 0;
}
.tile-name {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 800;
  font-size: clamp(.6rem, 1.8vw, .9rem);
  line-height: 1.1;
  color: var(--ink);
  white-space: nowrap;
}
/* Paire trouvée : contour vert et petit saut de joie */
.tile.matched .tile-front {
  box-shadow: 0 5px 0 rgba(46,42,77,0.12), inset 0 0 0 4px var(--green);
}
.tile.matched .tile-inner {
  animation: matchJump .5s ease;
}
@keyframes matchJump {
  0%   { transform: rotateY(180deg) scale(1); }
  40%  { transform: rotateY(180deg) scale(1.1) rotate(-4deg); }
  70%  { transform: rotateY(180deg) scale(.97) rotate(3deg); }
  100% { transform: rotateY(180deg) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .tile.matched .tile-inner { animation: none; }
}

.win-banner {
  position: fixed;
  inset: 0;
  background: rgba(46,42,77,0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 10;
  padding: 1rem;
}
.win-banner.show { display: flex; }

.win-card {
  background: var(--white);
  border-radius: 28px;
  padding: 2.2rem 2.4rem;
  text-align: center;
  box-shadow: 0 14px 0 rgba(46,42,77,0.12);
}
.win-title {
  font-family: 'Baloo 2', sans-serif;
  font-size: 1.8rem;
  margin: 0 0 .5rem;
  color: var(--pink);
}
#winStats { color: #6a6486; margin: 0 0 1.1rem; font-family: 'Baloo 2', sans-serif; }
.score-label {
  display: block;
  font-size: .85rem;
  color: #6a6486;
  margin-bottom: .5rem;
}
#pseudoInput {
  width: 100%;
  font-family: 'Quicksand', sans-serif;
  font-weight: 700;
  font-size: 1rem;
  padding: .6rem .8rem;
  border: 2px solid #EFEAF6;
  border-radius: 12px;
  margin-bottom: .8rem;
  text-align: center;
}
#pseudoInput:focus { outline: none; border-color: var(--sky); }
.score-actions {
  display: flex;
  gap: .6rem;
  justify-content: center;
  margin-bottom: .4rem;
}
#saveScore {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  background: var(--sky);
  color: var(--white);
  border: none;
  padding: .6rem 1.1rem;
  border-radius: 999px;
  box-shadow: 0 5px 0 #2f8fb8;
  cursor: pointer;
  font-size: .9rem;
}
.ghost-btn {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  background: transparent;
  color: #9089a8;
  border: none;
  padding: .6rem .8rem;
  cursor: pointer;
  font-size: .9rem;
}
.score-saved {
  display: none;
  font-family: 'Baloo 2', sans-serif;
  color: var(--green);
  margin: .4rem 0 .8rem;
}
#playAgain {
  font-family: 'Baloo 2', sans-serif;
  font-weight: 700;
  background: var(--green);
  color: var(--white);
  border: none;
  padding: .7rem 1.6rem;
  border-radius: 999px;
  box-shadow: 0 5px 0 #3a9c58;
  cursor: pointer;
}
