:root {
  color-scheme: dark;
  font-family: ui-rounded, "Avenir Next", Avenir, system-ui, sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-tap-highlight-color: transparent;
  --background: #211d23;
  --surface: #2b262d;
  --tile: #302b32;
  --line: #514952;
  --cream: #fff4d8;
  --muted: #c7bcb8;
  --gold: #f3cb72;
  --green: #6d9c79;
  --yellow: #c7a64e;
  --gray: #625b63;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(ellipse at 50% -18%, rgb(148 105 93 / 27%), transparent 58%),
    radial-gradient(ellipse at 50% 108%, rgb(134 94 52 / 9%), transparent 40%),
    var(--background);
  color: var(--cream);
  touch-action: manipulation;
}

button {
  color: inherit;
  font: inherit;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  position: relative;
}

.game-ui {
  display: grid;
  height: 100vh;
  height: 100dvh;
  grid-template-rows: auto minmax(0, 1fr) auto;
  max-width: 560px;
  margin: 0 auto;
  opacity: 1;
  padding: max(12px, env(safe-area-inset-top)) 18px max(12px, env(safe-area-inset-bottom));
  row-gap: clamp(4px, 1vh, 10px);
  transition: opacity 800ms ease;
  will-change: opacity;
}

.game-ui[hidden] {
  display: none;
}

.game-ui.is-ending {
  opacity: 0;
  pointer-events: none;
}

.top {
  min-width: 0;
  text-align: center;
}

.eyebrow,
.collection-label {
  color: var(--gold);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.19em;
  margin: 0;
  text-transform: uppercase;
}

h1 {
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.45rem, 6vw, 1.9rem);
  font-weight: 500;
  letter-spacing: 0.01em;
  margin: 4px 0 2px;
}

h1 span {
  color: var(--gold);
  font-size: 0.8em;
}

.subtitle {
  color: var(--muted);
  font-size: 0.82rem;
  margin: 0;
}

.collection {
  margin: 10px auto 0;
  max-width: 390px;
  min-height: 48px;
  text-align: left;
}

.collection-label {
  font-size: 0.6rem;
  letter-spacing: 0.16em;
  margin: 0 0 6px 2px;
}

.collected-words {
  align-items: center;
  display: flex;
  flex-wrap: nowrap;
  gap: 7px;
  height: 28px;
  overflow: hidden;
}

.word-chip {
  align-items: center;
  background: rgb(243 203 114 / 12%);
  border: 1px solid rgb(243 203 114 / 42%);
  border-radius: 999px;
  color: var(--gold);
  display: inline-flex;
  font-size: clamp(0.65rem, 3.2vw, 0.75rem);
  font-weight: 800;
  flex: 0 0 auto;
  justify-content: center;
  letter-spacing: 0.08em;
  min-height: 25px;
  padding: 3px 8px;
}

.word-chip.is-arriving {
  opacity: 0;
}

.flying-word {
  inset: 0 auto auto 0;
  pointer-events: none;
  position: fixed;
  transition: transform 850ms cubic-bezier(.2, .8, .25, 1), opacity 850ms ease;
  will-change: transform, opacity;
  z-index: 5;
}

.play-area {
  align-items: center;
  display: flex;
  justify-content: center;
  min-height: 0;
  padding: 0;
}

.board {
  aspect-ratio: 5 / 6;
  display: grid;
  gap: clamp(5px, 1.7vw, 8px);
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(6, minmax(0, 1fr));
  height: min(100%, 390px);
  max-width: 390px;
  max-height: 100%;
  perspective: 700px;
  transition: opacity 220ms ease;
  will-change: transform, opacity;
  width: auto;
}

.board.is-transitioning,
.keyboard.is-transitioning,
.board.is-entering,
.keyboard.is-entering {
  opacity: 0;
}

.tile {
  align-items: center;
  background: var(--tile);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--cream);
  display: flex;
  font-size: clamp(1.15rem, 6vw, 1.7rem);
  font-weight: 800;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  backface-visibility: hidden;
  text-transform: uppercase;
  transition: background-color 180ms ease, border-color 180ms ease, transform 180ms ease;
}

.tile.is-filled {
  animation: pop 100ms ease-out;
  border-color: #9c8b83;
  will-change: transform;
}

.tile.is-revealed {
  animation: flip-in 420ms cubic-bezier(0.2, 0.7, 0.2, 1) both;
  animation-delay: var(--reveal-delay, 0ms);
  transform-style: preserve-3d;
  will-change: transform;
}

.tile[data-state="correct"] {
  background: var(--green);
  border-color: var(--green);
}

.tile[data-state="present"] {
  background: var(--yellow);
  border-color: var(--yellow);
}

.tile[data-state="absent"] {
  background: var(--gray);
  border-color: var(--gray);
}

.board.is-winning .tile[data-state="correct"] {
  animation: winning-glow 650ms ease-in-out infinite alternate;
  box-shadow: 0 0 0 1px rgb(243 203 114 / 50%), 0 0 16px rgb(243 203 114 / 20%);
  will-change: transform;
}

.board.is-shaking {
  animation: shake 350ms ease;
  will-change: transform;
}

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0 auto;
  max-width: 500px;
  padding-bottom: 2px;
  transition: opacity 220ms ease;
  will-change: transform, opacity;
  width: 100%;
}

.key-row {
  display: flex;
  gap: clamp(4px, 1.2vw, 7px);
  justify-content: center;
  width: 100%;
}

.key {
  align-items: center;
  background: linear-gradient(155deg, #908488, #756b71);
  border: 0;
  border-radius: 6px;
  display: flex;
  flex: 1 1 0;
  font-size: clamp(0.78rem, 3.8vw, 1rem);
  font-weight: 750;
  justify-content: center;
  min-height: clamp(43px, 6.5vh, 57px);
  padding: 0 3px;
  box-shadow: inset 0 1px rgb(255 255 255 / 9%), 0 2px 4px rgb(0 0 0 / 15%);
  transition: background-color 180ms ease, transform 130ms ease, box-shadow 180ms ease;
}

.key:active {
  box-shadow: inset 0 2px 5px rgb(0 0 0 / 18%);
  transform: scale(0.96);
  will-change: transform;
}

.key[data-state="correct"] {
  background: var(--green);
}

.key[data-state="present"] {
  background: var(--yellow);
}

.key[data-state="absent"] {
  background: var(--gray);
}

.key-wide {
  flex: 1.5 1 0;
  font-size: clamp(0.59rem, 2.8vw, 0.78rem);
  letter-spacing: 0;
}

.finale {
  align-items: center;
  display: flex;
  flex-direction: column;
  inset: 0;
  justify-content: center;
  overflow: hidden;
  padding: 28px 22px max(28px, env(safe-area-inset-bottom));
  position: absolute;
  text-align: center;
  opacity: 0;
  transition: opacity 1100ms ease;
  will-change: opacity;
}

.finale[hidden] {
  display: none;
}

.finale.is-visible {
  opacity: 1;
}

.poem {
  color: var(--cream);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.05rem, 4.5vw, 1.4rem);
  line-height: 1.7;
  max-width: 620px;
  position: relative;
  z-index: 1;
  width: 100%;
}

.poem-line {
  display: block;
  min-height: 1.7em;
  white-space: pre-wrap;
}

.poem-letter {
  opacity: 0;
}

.poem-line.is-revealing .poem-letter {
  animation: letter-reveal 420ms ease both;
  animation-delay: calc(var(--letter-index) * 28ms);
}

.birthday-message {
  color: var(--gold);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.8rem, 8vw, 3.4rem);
  font-weight: 700;
  margin: 22px 0 0;
  max-width: 12ch;
  position: relative;
  text-shadow: 0 0 24px rgb(243 203 114 / 45%);
  z-index: 1;
  text-wrap: balance;
}

.birthday-message.is-visible {
  animation: birthday-pop 850ms cubic-bezier(0.2, 1.6, 0.4, 1) both;
  will-change: opacity;
}

.confetti {
  height: 100%;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  position: absolute;
}

.confetti-piece {
  animation: confetti-fall var(--duration) linear var(--delay) forwards;
  background: hsl(var(--hue) 75% 67%);
  border-radius: var(--confetti-radius, 2px);
  height: var(--confetti-height, 10px);
  left: 0;
  opacity: 0;
  position: absolute;
  top: 0;
  transform: translate3d(var(--x), -5vh, 0);
  width: var(--confetti-width, 7px);
  will-change: transform, opacity;
}

.sr-only {
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

@keyframes pop {
  50% { transform: scale(1.1); }
}

@keyframes flip-in {
  0% { transform: rotateX(0); }
  45% { transform: rotateX(82deg); }
  100% { transform: rotateX(0); }
}

@keyframes letter-reveal {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes winning-glow {
  to { transform: scale(1.035); }
}

@keyframes shake {
  25%, 75% { transform: translateX(-5px); }
  50% { transform: translateX(5px); }
}

@keyframes birthday-pop {
  0% { opacity: 0; }
  100% { opacity: 1; }
}

@keyframes confetti-fall {
  0% { opacity: 0; }
  8% { opacity: 1; }
  100% {
    opacity: 0.9;
    transform: translate3d(calc(var(--x) + var(--drift)), 110vh, 0) rotate(760deg);
  }
}

@keyframes sparkle {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}

h1 span {
  animation: sparkle 2.4s ease-in-out infinite;
  display: inline-block;
}

@media (max-height: 700px) {
  .game-ui {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .collection {
    margin-top: 6px;
    min-height: 44px;
  }

  .keyboard {
    gap: 6px;
  }

  .key {
    min-height: clamp(37px, 6vh, 48px);
  }

  .board {
    height: min(100%, 350px);
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
