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

body {
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.9), transparent 38%),
    linear-gradient(180deg, #fff8ec 0%, #eef6ff 48%, #f3f7f8 100%);
  color: #1f2937;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
}

.container {
  width: 100%;
  height: 100vh;
  max-width: none;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: clamp(12px, 2vw, 20px);
}

.end-session-button {
  position: fixed;
  top: clamp(12px, 2vw, 22px);
  right: clamp(12px, 2vw, 22px);
  z-index: 20;
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 700;
  box-shadow: 0 16px 28px rgba(15, 23, 42, 0.16);
}

.start-screen {
  width: min(100%, 560px);
  padding: clamp(24px, 5vw, 48px);
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.7);
  box-shadow: 0 24px 60px rgba(31, 41, 55, 0.12);
  backdrop-filter: blur(10px);
}

.start-badge {
  display: inline-flex;
  padding: 8px 14px;
  border-radius: 999px;
  background: #fff1c9;
  color: #8a5a00;
  font-size: clamp(0.78rem, 2vw, 0.95rem);
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.start-title {
  margin: 18px 0 12px;
  font-size: clamp(2.2rem, 7vw, 4rem);
  font-weight: 800;
  line-height: 1.05;
}

.start-title span {
  display: inline-block;
  transform: rotate(-8deg);
}

.start-text {
  margin: 0 auto 28px;
  max-width: 28rem;
  color: #52606d;
  font-size: clamp(1rem, 2.7vw, 1.15rem);
  line-height: 1.6;
}

.size-selector {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.size-button {
  border: 0;
  border-radius: 20px;
  padding: 18px 16px;
  font-size: clamp(1.05rem, 3vw, 1.25rem);
  font-weight: 800;
  color: #fff;
  transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
}

.size-button[data-size="4"] {
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  box-shadow: 0 18px 28px rgba(37, 99, 235, 0.25);
}

.size-button[data-size="8"] {
  background: linear-gradient(135deg, #0f9f6e, #047857);
  box-shadow: 0 18px 28px rgba(4, 120, 87, 0.25);
}

.size-button:hover,
.size-button:focus-visible {
  transform: translateY(-2px);
  filter: brightness(1.03);
}

.size-button:active,
.size-button.active {
  transform: scale(0.98);
}

#game-board {
  width: min(100%, 1320px);
  height: min(94vh, 860px);
  padding: clamp(8px, 1.2vw, 14px);
  box-sizing: border-box;
  display: grid;
  gap: clamp(8px, 1.1vw, 14px);
  align-items: stretch;
  justify-content: stretch;
}

#game-board[data-size="4"] {
  width: min(100%, 760px);
  grid-template-columns: repeat(2, minmax(180px, 320px));
  grid-template-rows: repeat(2, auto);
  justify-content: center;
  align-content: center;
}

#game-board[data-size="8"] {
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

.card-item {
  width: 100%;
  height: 100%;
  perspective: 1000px;
  cursor: pointer;
}

#game-board[data-size="4"] .card-item {
  height: auto;
  aspect-ratio: 3 / 4;
}

.card-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.6s;
}

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

.card-front,
.card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 22px;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card-back {
  background: linear-gradient(160deg, #ff6aa2 0%, #ea4fff 42%, #8b3dff 78%, #5b21b6 100%);
  color: white;
  font-size: clamp(6.5rem, 18vw, 13rem);
  font-weight: 900;
  line-height: 0.82;
  text-shadow: 0 8px 18px rgba(91, 33, 182, 0.35);
  box-shadow: 0 20px 28px rgba(123, 58, 237, 0.24);
}

.card-front {
  transform: rotateY(180deg);
  background: white;
  box-shadow: 0 18px 28px rgba(15, 23, 42, 0.08);
}

.card-front img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: white;
}

#game-board[data-size="4"] .card-front img {
  object-fit: contain;
}

.card-item.matched {
  opacity: 1;
  transform: scale(0.985);
}

.win-screen {
  width: min(100%, 560px);
  margin: auto;
  padding: clamp(28px, 5vw, 44px);
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 24px 60px rgba(31, 41, 55, 0.14);
}

#game-board.win-state {
  width: 100%;
  max-width: none;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(16px, 3vw, 32px);
}

.win-screen h2 {
  margin: 0 0 10px;
  font-size: clamp(2rem, 6vw, 3rem);
  font-weight: 800;
}

.win-screen p {
  margin: 0 0 22px;
  color: #52606d;
}

.restart-button {
  border: 0;
  border-radius: 16px;
  padding: 14px 22px;
  background: #111827;
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
}

@media (max-width: 767px) {
  .container {
    align-items: center;
    padding: 10px;
  }

  .start-screen {
    border-radius: 24px;
  }

  .size-selector {
    grid-template-columns: 1fr;
  }

  #game-board {
    width: 100%;
    height: min(94vh, 820px);
  }

  #game-board[data-size="8"] {
    grid-template-columns: repeat(2, minmax(130px, 1fr));
    grid-template-rows: repeat(4, minmax(0, 1fr));
  }
}

@media (max-width: 420px) {
  .container {
    padding: 8px;
  }

  .start-screen {
    padding: 22px 18px;
  }

  .card-front,
  .card-back {
    border-radius: 18px;
  }

  #game-board {
    gap: 8px;
    padding: 8px;
  }

  #game-board[data-size="4"] {
    width: 100%;
    grid-template-columns: repeat(2, minmax(120px, 1fr));
  }

  #game-board[data-size="8"] {
    grid-template-columns: repeat(2, minmax(120px, 1fr));
  }

  .card-back {
    font-size: clamp(5rem, 20vw, 8rem);
  }
}
