/* In-game stage loading + playfield */

#game-screen.overlay-screen {
  padding: 0;
  background-color: var(--color-bg);
}

#game-screen.overlay-screen.open {
  display: flex;
}

#game-root {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
  position: relative;
  overflow: hidden;
  overscroll-behavior: none;
}

#game-root.is-drawing-letters,
#game-root.is-drawing-letters * {
  touch-action: none !important;
  overscroll-behavior: none;
}

/* —— Stage loading —— */
.stage-loading {
  position: absolute;
  inset: 0;
  z-index: 5;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.35s ease, transform 0.35s ease;
}

.stage-loading.done {
  opacity: 0;
  transform: scale(1.04);
  pointer-events: none;
}

.stage-loading-bg {
  position: absolute;
  inset: -8%;
  background-size: cover;
  background-position: center;
  animation: stage-kenburns 8s ease-in-out infinite alternate;
}

.stage-loading-veil {
  position: absolute;
  inset: 0;
  background: rgba(18, 10, 5, 0.78);
}

.stage-loading-content {
  position: relative;
  z-index: 1;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.stage-loading-season {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 900;
  color: var(--color-primary-light);
  text-shadow: 0 3px 14px rgba(0, 0, 0, 0.55);
  animation: stage-fade-up 0.7s ease both;
}

.stage-loading-stage {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--color-text);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.55);
  animation: stage-fade-up 0.7s ease 0.12s both;
}

.stage-loading-orbit {
  width: 52px;
  height: 52px;
  margin-top: 12px;
  border-radius: 50%;
  border: 3px solid rgba(255, 199, 44, 0.2);
  border-top-color: var(--color-primary);
  animation: stage-spin 1.1s linear infinite;
}

@keyframes stage-kenburns {
  from { transform: scale(1) translate(0, 0); }
  to { transform: scale(1.12) translate(-2%, 1.5%); }
}

@keyframes stage-spin {
  to { transform: rotate(360deg); }
}

@keyframes stage-fade-up {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* —— Playfield —— */
.game-play {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  padding:
    max(var(--safe-top), 8px)
    var(--space-3)
    max(var(--safe-bottom), 8px);
  gap: var(--space-2);
  overflow: hidden;
}

.game-play[hidden] {
  display: none !important;
}

.game-play-top {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-1) 0;
  flex-shrink: 0;
}

.game-nav-back {
  width: 44px;
  height: 44px;
  border: none;
  background: transparent;
  box-shadow: none;
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  flex-shrink: 0;
}

.game-nav-back img {
  width: 26px;
  height: 26px;
  object-fit: contain;
  transform: none;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.45));
}

.game-nav-back:active {
  transform: scale(0.92);
}

.game-music-btn {
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
}

.game-music-btn img {
  width: 24px;
  height: 24px;
  object-fit: contain;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.45));
}

.game-music-btn:active {
  transform: scale(0.92);
}

.game-play-meta {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.game-play-meta strong {
  font-size: 1.02rem;
  color: var(--color-primary-light);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-play-meta span {
  font-size: 0.78rem;
  color: var(--color-text);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-tool-btn {
  position: relative;
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  border: var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--glass-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
}

.game-tool-btn img {
  width: 26px;
  height: 26px;
  object-fit: contain;
}

.game-words {
  position: relative;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: var(--space-1) 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
  min-height: 72px;
  max-height: none;
}

.game-word-row {
  display: flex;
  flex-direction: row;
  gap: 5px;
  justify-content: center;
  flex-shrink: 0;
}

.game-slot {
  position: relative;
  width: clamp(40px, 11vw, 55px);
  height: clamp(40px, 11vw, 55px);
  flex-shrink: 0;
  overflow: visible;
}

.game-slot-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.game-slot-letter {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: visible;
  line-height: 1.45;
  padding-block: 0.12em;
  font-size: clamp(1rem, 4.2vw, 1.35rem);
  font-weight: 900;
  color: var(--color-primary-light);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
}

.game-slot-letter.is-digraph {
  font-size: clamp(0.72rem, 3.2vw, 1rem);
  letter-spacing: -0.06em;
  white-space: nowrap;
}

.game-word-row.solved .game-slot-letter {
  animation: slot-pop 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes slot-pop {
  0% { transform: scale(0.7); opacity: 0.35; }
  65% { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

.game-letter-flyer {
  position: fixed;
  z-index: 1200;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  will-change: transform, opacity;
  transform: translate(0, 0) scale(1);
  transition:
    transform 0.38s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.38s ease;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.35));
}

.game-letter-flyer .game-letter-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.game-letter-flyer span {
  position: relative;
  z-index: 1;
  overflow: visible;
  font-weight: 900;
  color: var(--color-primary-light);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.55);
  line-height: 1.45;
  padding-block: 0.12em;
}

.game-letter-flyer.is-flying {
  opacity: 0.92;
}

.game-preview {
  position: relative;
  z-index: 1;
  align-self: center;
  min-width: 4.5rem;
  min-height: 1.85rem;
  max-width: min(90vw, 260px);
  padding: 5px 10px 7px;
  font-size: 0.85rem;
  font-weight: 800;
  line-height: 1.45;
  color: var(--color-primary-light);
  text-align: center;
  letter-spacing: 0;
  flex-shrink: 0;
  pointer-events: none;
  border-radius: var(--radius-md);
  overflow-x: hidden;
  overflow-y: visible;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Keep a thin slot so layout doesn't jump; stay out of the word grid */
.game-preview[hidden] {
  display: none !important;
}

.game-preview.ok {
  color: #b8f5c0;
  border-color: rgba(67, 160, 71, 0.45);
}

.game-preview.bad {
  color: #ffb4a8;
  border-color: rgba(139, 29, 29, 0.45);
}

.game-preview.dup {
  color: var(--color-text-muted);
}

.game-board-bottom {
  margin-top: 0;
  position: relative;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
  padding-bottom: 2px;
  touch-action: none;
  overscroll-behavior: none;
}

.game-tray-wrap {
  position: relative;
  z-index: 2;
  /* Cap by viewport height so tray never crushes the word grid on short phones */
  --tray-size: min(68vw, 32dvh, 248px);
  width: var(--tray-size);
  height: var(--tray-size);
  margin: 0;
  flex-shrink: 0;
  touch-action: none;
  overscroll-behavior: none;
  isolation: isolate;
  --letter-size: clamp(34px, calc(var(--tray-size) * 0.2), 54px);
}

.game-play-tools {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
  flex-shrink: 0;
  width: 100%;
  padding: 2px 0 6px;
}

.game-tray-lines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}

.game-line-path {
  fill: none;
  stroke: var(--color-primary);
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 6px rgba(255, 199, 44, 0.55));
}

.game-tray {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.game-letter {
  position: absolute;
  width: var(--letter-size, 56px);
  height: var(--letter-size, 56px);
  transform: translate(-50%, -50%);
  border: none;
  background: transparent;
  padding: 0;
  overflow: visible;
  cursor: pointer;
  /* Override global touch-action: manipulation — must block WebView pan */
  touch-action: none !important;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition:
    left 0.45s cubic-bezier(0.34, 1.4, 0.64, 1),
    top 0.45s cubic-bezier(0.34, 1.4, 0.64, 1),
    transform 0.22s ease,
    filter 0.22s ease;
  will-change: left, top, transform;
}

.game-letter-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  transition: filter 0.22s ease, transform 0.22s ease;
}

.game-letter span {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow: visible;
  line-height: 1.45;
  padding-block: 0.14em;
  font-size: clamp(1rem, calc(var(--letter-size) * 0.42), 1.35rem);
  font-weight: 900;
  color: var(--color-primary-light);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  pointer-events: none;
  transition: color 0.2s ease, text-shadow 0.2s ease, transform 0.22s ease;
}

.game-letter span.is-digraph {
  font-size: clamp(0.7rem, calc(var(--letter-size) * 0.3), 1rem);
  letter-spacing: -0.06em;
  white-space: nowrap;
}

.game-letter.selected {
  transform: translate(-50%, -50%) scale(1.16);
  filter: drop-shadow(0 0 12px rgba(255, 199, 44, 0.75));
  z-index: 5;
}

.game-letter.selected .game-letter-bg {
  filter: brightness(1.45) saturate(1.5) sepia(0.35) hue-rotate(-8deg);
  transform: scale(1.04);
}

.game-letter.selected span {
  color: #ffd84a;
  text-shadow:
    0 0 10px rgba(255, 199, 44, 0.95),
    0 0 18px rgba(255, 200, 60, 0.55),
    0 1px 2px rgba(0, 0, 0, 0.45);
  transform: scale(1.06);
}

.game-letter.just-picked {
  animation: letter-pick 0.32s cubic-bezier(0.34, 1.5, 0.64, 1);
}

.game-letter.is-shuffling {
  pointer-events: none;
  z-index: 6;
  animation: letter-shuffle-spin 0.55s ease-in-out;
}

.game-letter.is-shuffling .game-letter-bg {
  filter: brightness(1.2) saturate(1.2);
}

@keyframes letter-pick {
  0% { transform: translate(-50%, -50%) scale(1); }
  45% { transform: translate(-50%, -50%) scale(1.28); }
  100% { transform: translate(-50%, -50%) scale(1.16); }
}

@keyframes letter-shuffle-spin {
  0% { transform: translate(-50%, -50%) scale(1) rotate(0deg); }
  40% { transform: translate(-50%, -50%) scale(0.55) rotate(180deg); }
  100% { transform: translate(-50%, -50%) scale(1) rotate(360deg); }
}

/* Praise text pops */
.game-praise {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.game-praise[hidden] {
  display: none !important;
}

.game-praise img {
  width: min(42vw, 168px);
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 8px 20px rgba(0, 0, 0, 0.45));
  opacity: 0;
  transform: scale(0.55) translateY(18px) rotate(-4deg);
}

.game-praise.in img {
  animation: praise-in 0.28s cubic-bezier(0.34, 1.55, 0.64, 1) forwards;
}

.game-praise.out img {
  animation: praise-out 0.18s ease-in forwards;
}

@keyframes praise-in {
  0% {
    opacity: 0;
    transform: scale(0.55) translateY(18px) rotate(-4deg);
  }
  100% {
    opacity: 1;
    transform: scale(1) translateY(0) rotate(0deg);
  }
}

@keyframes praise-out {
  0% {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
  100% {
    opacity: 0;
    transform: scale(1.12) translateY(-14px);
  }
}

/* Stage complete card */
.game-win {
  position: absolute;
  inset: 0;
  z-index: 25;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgba(18, 10, 5, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  transition: opacity 0.28s ease;
}

.game-win[hidden] {
  display: none !important;
}

.game-win.open {
  opacity: 1;
}

.game-win-card {
  width: 100%;
  max-width: 340px;
  padding: var(--space-5) var(--space-4);
  text-align: center;
  transform: scale(0.88) translateY(16px);
  transition: transform 0.35s cubic-bezier(0.34, 1.4, 0.64, 1);
}

.game-win.open .game-win-card {
  transform: scale(1) translateY(0);
}

.game-coin-wrap {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.game-coin-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: var(--radius-full);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border: var(--glass-border);
  box-shadow: var(--glass-shadow);
  font-weight: 800;
  font-size: 0.8rem;
  color: var(--color-primary-light);
}

.game-coin-pill img {
  width: 16px;
  height: 16px;
  object-fit: contain;
}

.game-coin-delta {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, 0);
  font-size: 0.85rem;
  font-weight: 900;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.55);
  pointer-events: none;
  white-space: nowrap;
  opacity: 0;
  z-index: 6;
}

.game-coin-delta.minus {
  color: #ff8a80;
}

.game-coin-delta.plus {
  color: #9be7a4;
}

.game-coin-delta.show {
  animation: coin-spend-fade 0.95s ease-out forwards;
}

@keyframes coin-spend-fade {
  0% {
    opacity: 0;
    transform: translate(-50%, 4px) scale(0.9);
  }
  18% {
    opacity: 1;
    transform: translate(-50%, -6px) scale(1.05);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -28px) scale(1);
  }
}

.game-tool-cost {
  position: absolute;
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%);
  min-width: 22px;
  padding: 1px 5px;
  border-radius: var(--radius-full);
  background: rgba(18, 10, 5, 0.85);
  border: 1px solid rgba(255, 199, 44, 0.4);
  color: var(--color-primary);
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1.2;
  pointer-events: none;
  white-space: nowrap;
}

.game-tool-cost.is-free {
  color: #9be7a4;
  border-color: rgba(67, 160, 71, 0.45);
}

.game-slot-letter.hinted {
  color: var(--color-primary);
}

.game-slot-letter.bombed {
  color: #ffb347;
  text-shadow: 0 0 8px rgba(255, 140, 40, 0.7);
}

.game-win-stars {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 0 auto 14px;
}

.game-win-stars img {
  width: 40px;
  height: 40px;
  object-fit: contain;
  filter: drop-shadow(0 4px 10px rgba(255, 199, 44, 0.35));
  opacity: 0.28;
  transform: scale(0.85);
  transition: opacity 0.25s ease, transform 0.25s ease, filter 0.25s ease;
}

.game-win-stars img.earned {
  opacity: 1;
  transform: scale(1);
  animation: win-star-pop 0.5s cubic-bezier(0.34, 1.5, 0.64, 1) both;
}

.game-win-stars img.earned:nth-child(2) { animation-delay: 0.08s; }
.game-win-stars img.earned:nth-child(3) { animation-delay: 0.16s; }

.game-win-stars img.missed {
  opacity: 0.22;
  filter: grayscale(1) brightness(0.7);
}

.game-win-share {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 2px 0 12px;
  padding: 8px 16px;
  border: 1px solid rgba(255, 199, 44, 0.35);
  border-radius: var(--radius-full);
  background: rgba(255, 199, 44, 0.12);
  color: var(--color-primary-light);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease;
}

.game-win-share:active {
  transform: scale(0.96);
  background: rgba(255, 199, 44, 0.22);
}

.game-win-card h2 {
  margin: 0 0 8px;
  font-size: 1.55rem;
  font-weight: 900;
  color: var(--color-primary-light);
}

.game-win.is-season-clear .game-win-card h2 {
  font-size: 1.7rem;
  color: #ffe08a;
  text-shadow: 0 0 18px rgba(255, 199, 44, 0.45);
}

.game-win-card p {
  margin: 0 0 var(--space-5);
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--color-text-muted);
}

.game-win-actions {
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 10px;
}

.game-win-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 8px;
  border-radius: var(--radius-lg);
  border: var(--glass-border);
  background: rgba(18, 10, 5, 0.35);
  color: var(--color-text);
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.15s ease, background 0.15s ease;
}

.game-win-btn:active {
  transform: scale(0.94);
}

.game-win-btn img {
  width: 36px;
  height: 36px;
  object-fit: contain;
}

#win-home img {
  width: 40px;
  height: 40px;
}

.game-win-btn-primary {
  background: linear-gradient(
    180deg,
    rgba(255, 199, 44, 0.28) 0%,
    rgba(216, 135, 18, 0.22) 100%
  );
  border-color: rgba(255, 199, 44, 0.5);
  color: var(--color-primary-light);
}

@keyframes win-star-pop {
  0% { transform: scale(0.4) rotate(-20deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* Short phones (iPhone SE / 8 / small Android) — keep tray under the word grid */
@media (max-height: 780px) {
  .game-tray-wrap {
    --tray-size: min(62vw, 30dvh, 220px);
  }

  .game-tool-btn {
    width: 44px;
    height: 44px;
  }

  .game-tool-btn img {
    width: 22px;
    height: 22px;
  }

  .game-play-tools {
    gap: 12px;
    padding: 2px 0 4px;
  }

  .game-words {
    gap: 5px;
    min-height: 56px;
  }

  .game-slot {
    width: clamp(36px, 9.5vw, 48px);
    height: clamp(36px, 9.5vw, 48px);
  }
}

@media (max-height: 700px) {
  .game-tray-wrap {
    --tray-size: min(56vw, 28dvh, 200px);
  }
}

@media (max-height: 640px) {
  .game-tray-wrap {
    --tray-size: min(50vw, 24dvh, 176px);
  }

  .game-play-meta strong {
    font-size: 0.95rem;
  }

  .game-play-meta span {
    font-size: 0.7rem;
  }

  .game-play-top {
    padding: 0;
  }
}

/* Narrow phones */
@media (max-width: 380px) {
  .game-tray-wrap {
    --tray-size: min(72vw, 34dvh, 240px);
  }

  .game-slot {
    width: clamp(34px, 9vw, 44px);
    height: clamp(34px, 9vw, 44px);
  }
}

/* Fallback when dvh is unsupported */
@supports not (height: 1dvh) {
  .game-tray-wrap {
    --tray-size: min(70vw, 38vh, 280px);
  }

  @media (max-height: 780px) {
    .game-tray-wrap {
      --tray-size: min(58vw, 30vh, 220px);
    }
  }

  @media (max-height: 640px) {
    .game-tray-wrap {
      --tray-size: min(50vw, 24vh, 176px);
    }
  }
}
