:root {
  --bg: #101a22;
  --felt: #0e5a43;
  --felt-dark: #08372c;
  --panel: #182235;
  --paper: #fff8e8;
  --paper-edge: #e0ceb0;
  --gold: #e8bf62;
  --blue: #68c7ff;
  --green: #35d084;
  --ink: #202124;
  --muted: #aebbd0;
  --shadow: rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--paper);
  font-family: "Mali", "Segoe UI", Tahoma, sans-serif;
  background:
    radial-gradient(circle at 18% 12%, rgba(104, 199, 255, 0.18), transparent 26%),
    radial-gradient(circle at 84% 8%, rgba(232, 191, 98, 0.16), transparent 24%),
    linear-gradient(135deg, #101a22, #08372c 62%, #0b1d18);
  overflow-x: hidden;
}

button,
select {
  font: inherit;
}

input,
select,
textarea {
  font-size: 16px;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
}

.game-shell {
  width: min(980px, calc(100% - 20px));
  margin: 0 auto;
  padding: 12px 0 24px;
  display: grid;
  gap: 12px;
}

.top-panel {
  display: grid;
  gap: 12px;
}

.title-block {
  display: grid;
  gap: 10px;
}

.back-link {
  width: max-content;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  color: rgba(255, 248, 232, 0.9);
  background: rgba(3, 28, 22, 0.38);
  font-size: 0.86rem;
  font-weight: 600;
  line-height: 1.25;
  text-decoration: none;
}

.eyebrow {
  margin: 0;
  color: var(--gold);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0;
  line-height: 1.35;
}

h1 {
  margin: 0;
  color: #fff8e8;
  font-size: 2.35rem;
  font-weight: 700;
  line-height: 1.22;
  letter-spacing: 0;
  text-shadow: 0 10px 24px rgba(0, 0, 0, 0.3);
}

.setup {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(118px, 0.72fr);
  align-items: end;
  gap: 10px;
}

.setup label {
  grid-column: 1 / -1;
  color: rgba(255, 248, 232, 0.76);
  font-size: 0.84rem;
  font-weight: 600;
  line-height: 1.35;
}

select,
.primary-btn {
  min-height: 52px;
  border-radius: 10px;
  font-weight: 600;
  line-height: 1.25;
}

select {
  min-width: 0;
  border: 1px solid rgba(255, 255, 255, 0.18);
  outline: 0;
  padding: 0 12px;
  color: var(--paper);
  background: rgba(4, 23, 18, 0.62);
}

select:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(232, 191, 98, 0.18);
}

.primary-btn {
  border: 0;
  color: #1d1607;
  background: linear-gradient(180deg, #fff0a8, var(--gold));
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.24), inset 0 1px 0 rgba(255, 255, 255, 0.58);
  transition: transform 160ms ease, filter 160ms ease;
}

.primary-btn:hover {
  filter: brightness(1.05);
  transform: translateY(-2px);
}

.score-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.score-box {
  min-height: 58px;
  padding: 9px 8px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  background: rgba(4, 23, 18, 0.48);
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.score-box span {
  display: block;
  color: rgba(255, 248, 232, 0.68);
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.25;
}

.score-box strong {
  display: block;
  margin-top: 3px;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.12;
}

.board-wrap {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 16px;
  padding: 12px;
  background:
    linear-gradient(140deg, rgba(10, 97, 70, 0.92), rgba(6, 54, 42, 0.98)),
    repeating-radial-gradient(circle at 28% 20%, rgba(255, 255, 255, 0.055) 0 1px, transparent 1px 9px);
  box-shadow: 0 20px 40px var(--shadow), inset 0 0 0 7px rgba(0, 0, 0, 0.12);
}

.board-wrap::before,
.board-wrap::after {
  content: "";
  position: absolute;
  inset: 9px;
  pointer-events: none;
  border-radius: 10px;
}

.board-wrap::before {
  border: 1px solid rgba(232, 191, 98, 0.46);
}

.board-wrap::after {
  border: 1px dashed rgba(255, 248, 232, 0.22);
}

.board {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: 8px;
}

.memory-card {
  position: relative;
  width: 100%;
  aspect-ratio: 5 / 7;
  border: 0;
  border-radius: 10px;
  padding: 0;
  background: transparent;
  perspective: 900px;
  -webkit-tap-highlight-color: transparent;
}

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

.memory-card.is-flipped .card-inner,
.memory-card.is-matched .card-inner {
  transform: rotateY(180deg);
}

.card-front,
.card-back {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 10px;
  overflow: hidden;
  backface-visibility: hidden;
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.26), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.card-front {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.16), transparent 34%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.18) 0 2px, transparent 2px 10px),
    linear-gradient(135deg, #265d9f, #16375f);
  border: 1px solid rgba(255, 255, 255, 0.24);
}

.card-front::before {
  content: "";
  width: 46%;
  aspect-ratio: 1;
  border: 2px solid rgba(255, 248, 232, 0.68);
  border-radius: 50%;
  box-shadow: 0 0 24px rgba(255, 255, 255, 0.14);
}

.card-back {
  color: var(--ink);
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.8), transparent 32%),
    var(--paper);
  border: 1px solid var(--paper-edge);
  transform: rotateY(180deg);
  align-content: center;
  gap: 6px;
  padding: 10px 6px;
}

.card-back::before {
  content: "";
  position: absolute;
  inset: 7px;
  border: 1px solid rgba(145, 111, 39, 0.25);
  border-radius: 7px;
}

.animal-icon,
.animal-name {
  position: relative;
  z-index: 1;
  font-weight: 600;
  line-height: 1;
}

.animal-icon {
  font-size: 2.85rem;
  filter: drop-shadow(0 7px 10px rgba(0, 0, 0, 0.16));
}

.animal-name {
  max-width: 100%;
  color: rgba(32, 33, 36, 0.72);
  font-size: 0.76rem;
  line-height: 1.24;
  text-align: center;
}

.memory-card.is-matched .card-back {
  animation: matchGlow 780ms ease both;
}

.memory-card.is-wrong .card-inner {
  animation: missShake 430ms ease both;
}

.finish-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
}

.spark {
  --x: 0px;
  --y: 0px;
  --delay: 0ms;
  position: absolute;
  left: 50%;
  top: 50%;
  width: 9px;
  height: 18px;
  border-radius: 2px;
  background: hsl(var(--h), 86%, 62%);
  animation: sparkBurst 960ms cubic-bezier(0.16, 0.86, 0.2, 1) var(--delay) both;
}

.message {
  min-height: 30px;
  margin: 0;
  color: rgba(255, 248, 232, 0.82);
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
}

@keyframes matchGlow {
  0%,
  100% {
    box-shadow: 0 10px 18px rgba(0, 0, 0, 0.26), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  }
  48% {
    box-shadow: 0 10px 18px rgba(0, 0, 0, 0.24), 0 0 30px rgba(53, 208, 132, 0.72), inset 0 0 0 3px rgba(53, 208, 132, 0.62);
  }
}

@keyframes missShake {
  0%,
  100% {
    transform: rotateY(180deg) translateX(0);
  }
  22% {
    transform: rotateY(180deg) translateX(-8px) rotateZ(-2deg);
  }
  46% {
    transform: rotateY(180deg) translateX(8px) rotateZ(2deg);
  }
  70% {
    transform: rotateY(180deg) translateX(-4px);
  }
}

@keyframes sparkBurst {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) rotate(0deg) scale(0.4);
  }
  16% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(calc(-50% + var(--x)), calc(-50% + var(--y))) rotate(540deg) scale(0.75);
  }
}

@media (min-width: 760px) {
  body {
    display: grid;
    place-items: center;
  }

  .game-shell {
    padding: 28px 0;
    gap: 18px;
  }

  .top-panel {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
  }

  h1 {
    font-size: 4.2rem;
  }

  .setup {
    width: 360px;
  }

  .board-wrap {
    padding: 18px;
  }

  .board {
    max-width: 560px;
    gap: 12px;
  }

  .animal-icon {
    font-size: 4.2rem;
  }
}

@media (min-width: 1024px) {
  .board {
    max-width: 940px;
  }

  .memory-card {
    border-radius: 8px;
  }

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

  .animal-icon {
    font-size: 3.1rem;
  }
}

@media (max-width: 390px) {
  .game-shell {
    width: min(100% - 14px, 980px);
  }

  .setup {
    grid-template-columns: 1fr;
  }

  .score-row {
    gap: 6px;
  }

  .score-box strong {
    font-size: 1.1rem;
  }

  .board {
    gap: 6px;
  }

  .animal-name {
    font-size: 0.72rem;
  }

  .animal-icon {
    font-size: 2.35rem;
  }
}
