/* ==========================================================================
   Card Merge — styles
   Mobile-first, dark glam, floating cards (2:3 art), equal grid gaps
   ========================================================================== */

:root {
  --bg: #0a0c12;
  --bg-elevated: rgba(24, 28, 40, 0.72);
  --surface: rgba(36, 42, 58, 0.9);
  --border: rgba(255, 255, 255, 0.08);
  --text: #f3f5f9;
  --text-muted: #9aa3b5;
  --accent: #7ab8f0;
  --accent-strong: #4a9ae0;
  --accent-soft: rgba(122, 184, 240, 0.18);
  --success: #5ee0a0;
  --danger: #ff7b86;
  --glow: rgba(122, 184, 240, 0.55);
  --match-glow: rgba(94, 224, 160, 0.5);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --radius-sm: 12px;
  --gap: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, Cantarell, "Noto Sans", sans-serif;
  --touch-min: 44px;
  --card-radius: 6px;
  --transition-fast: 160ms ease;
  --transition: 260ms cubic-bezier(0.22, 1, 0.36, 1);
  /* Stronger interactive zoom */
  --hover-scale: 1.22;
  --select-scale: 1.2;
  --match-scale: 1.14;
  /* Shared width so header / scores / board stay aligned.
     On short phones, shrink so 4×4 of 2:3 cards fits the viewport. */
  --chrome-height: calc(
    168px + var(--safe-top) + var(--safe-bottom)
  );
  --board-width: min(
    100%,
    480px,
    calc((100dvh - var(--chrome-height)) / 1.52)
  );
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
  overflow-x: hidden;
  /* Soft ambient glow — no hard frame, more “stage” feeling */
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(74, 154, 224, 0.18), transparent 55%),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(160, 90, 220, 0.1), transparent 50%),
    radial-gradient(ellipse 50% 35% at 0% 80%, rgba(60, 180, 160, 0.08), transparent 45%),
    linear-gradient(180deg, #0c0f16 0%, #080a10 100%);
}

button,
select {
  font: inherit;
  color: inherit;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- App layout ---------- */

.app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    calc(10px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(12px + var(--safe-left));
  max-width: 640px;
  margin: 0 auto;
  box-sizing: border-box;
  /* Avoid double-scroll behind fixed overlays on phones */
  overflow-x: hidden;
}

/* ---------- Header (title + circular icon buttons) ---------- */

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: var(--board-width);
  margin: 0 auto 10px;
  flex-shrink: 0;
  box-sizing: border-box;
}

.title {
  margin: 0;
  min-width: 0;
  font-size: clamp(1.05rem, 3.6vw, 1.25rem);
  font-weight: 750;
  letter-spacing: 0.06em;
  line-height: 1.2;
  background: linear-gradient(120deg, #fff 15%, #c4a8f0 55%, #7ab8f0 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

/* ---------- Circular icon buttons (premium) ---------- */

.btn-circle {
  appearance: none;
  position: relative;
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  padding: 0;
  border: none;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #eef3ff;
  isolation: isolate;
  overflow: hidden;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.22), transparent 42%),
    linear-gradient(155deg, #2a3348 0%, #121722 55%, #0c1018 100%);
  box-shadow:
    0 8px 22px rgba(0, 0, 0, 0.4),
    0 0 0 1px rgba(255, 255, 255, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.16),
    inset 0 -6px 14px rgba(0, 0, 0, 0.25);
  transition:
    transform 220ms cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 220ms ease,
    color 180ms ease,
    background 220ms ease,
    filter 220ms ease;
}

/* Soft animated ring */
.btn-circle__ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.1);
  transition: box-shadow 220ms ease, opacity 220ms ease;
}

/* Specular highlight */
.btn-circle__shine {
  position: absolute;
  inset: 2px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 1;
  background: linear-gradient(
    160deg,
    rgba(255, 255, 255, 0.2) 0%,
    transparent 38%,
    transparent 100%
  );
  opacity: 0.85;
}

.btn-circle__glyph {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  line-height: 0;
  transition: transform 220ms cubic-bezier(0.22, 1, 0.36, 1), opacity 160ms ease;
}

.btn-circle__glyph svg {
  display: block;
  width: 26px;
  height: 26px;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.35));
}

.btn-circle:hover {
  transform: translateY(-2px) scale(1.06);
  color: #fff;
}

.btn-circle:hover .btn-circle__glyph {
  transform: scale(1.06);
}

.btn-circle:active {
  transform: translateY(0) scale(0.94);
}

.btn-circle:active .btn-circle__glyph {
  transform: scale(0.92);
}

/* Sound ON — cool cyan / blue energy */
.btn-circle--sound:not([data-muted="true"]) {
  color: #d7efff;
  background:
    radial-gradient(circle at 32% 28%, rgba(160, 220, 255, 0.28), transparent 45%),
    linear-gradient(155deg, #24344a 0%, #101826 55%, #0a1018 100%);
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.4),
    0 0 20px rgba(80, 170, 255, 0.18),
    0 0 0 1px rgba(120, 190, 255, 0.2),
    inset 0 1px 0 rgba(200, 230, 255, 0.22),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

.btn-circle--sound:not([data-muted="true"]) .btn-circle__ring {
  box-shadow:
    inset 0 0 0 1.5px rgba(130, 200, 255, 0.35),
    0 0 12px rgba(90, 180, 255, 0.15);
}

.btn-circle--sound:not([data-muted="true"]):hover {
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.45),
    0 0 28px rgba(90, 180, 255, 0.35),
    0 0 0 1px rgba(150, 210, 255, 0.45),
    inset 0 1px 0 rgba(220, 240, 255, 0.28),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

/* Sound OFF / muted — warm rose, dimmer */
.btn-circle--sound[data-muted="true"] {
  color: #f0c4c8;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 160, 170, 0.14), transparent 45%),
    linear-gradient(155deg, #3a2830 0%, #181218 55%, #100c10 100%);
  box-shadow:
    0 8px 22px rgba(0, 0, 0, 0.42),
    0 0 14px rgba(220, 90, 110, 0.12),
    0 0 0 1px rgba(255, 140, 150, 0.18),
    inset 0 1px 0 rgba(255, 200, 210, 0.12),
    inset 0 -6px 14px rgba(0, 0, 0, 0.3);
  filter: saturate(0.92);
}

.btn-circle--sound[data-muted="true"] .btn-circle__ring {
  box-shadow:
    inset 0 0 0 1.5px rgba(255, 150, 160, 0.28),
    0 0 10px rgba(220, 90, 110, 0.1);
}

.btn-circle--sound[data-muted="true"]:hover {
  box-shadow:
    0 10px 26px rgba(0, 0, 0, 0.48),
    0 0 22px rgba(230, 100, 120, 0.22),
    0 0 0 1px rgba(255, 160, 170, 0.35),
    inset 0 1px 0 rgba(255, 210, 215, 0.16),
    inset 0 -6px 14px rgba(0, 0, 0, 0.3);
  filter: none;
}

/* New game / reload — violet glam */
.btn-circle--reload {
  color: #e8dcff;
  background:
    radial-gradient(circle at 32% 28%, rgba(200, 170, 255, 0.26), transparent 45%),
    linear-gradient(155deg, #322848 0%, #161222 55%, #0e0c16 100%);
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.4),
    0 0 18px rgba(150, 110, 255, 0.16),
    0 0 0 1px rgba(180, 140, 255, 0.22),
    inset 0 1px 0 rgba(230, 210, 255, 0.2),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

.btn-circle--reload .btn-circle__ring {
  box-shadow:
    inset 0 0 0 1.5px rgba(180, 150, 255, 0.32),
    0 0 12px rgba(150, 110, 255, 0.12);
}

.btn-circle--reload:hover {
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.45),
    0 0 28px rgba(160, 120, 255, 0.35),
    0 0 0 1px rgba(200, 170, 255, 0.45),
    inset 0 1px 0 rgba(240, 230, 255, 0.28),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

.btn-circle--reload:hover .btn-circle__glyph {
  animation: reload-spin 560ms cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes reload-spin {
  from { transform: rotate(0deg) scale(1.06); }
  to { transform: rotate(-360deg) scale(1.06); }
}

/* Codex button — warm amber / book glow */
.btn-circle--codex {
  color: #ffe9c8;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 210, 140, 0.28), transparent 45%),
    linear-gradient(155deg, #3a3020 0%, #1a1610 55%, #100e0c 100%);
  box-shadow:
    0 8px 24px rgba(0, 0, 0, 0.4),
    0 0 18px rgba(230, 170, 80, 0.16),
    0 0 0 1px rgba(240, 190, 110, 0.22),
    inset 0 1px 0 rgba(255, 230, 190, 0.2),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

.btn-circle--codex .btn-circle__ring {
  box-shadow:
    inset 0 0 0 1.5px rgba(240, 190, 110, 0.35),
    0 0 12px rgba(230, 170, 80, 0.12);
}

.btn-circle--codex:hover {
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.45),
    0 0 28px rgba(240, 180, 80, 0.32),
    0 0 0 1px rgba(255, 210, 140, 0.45),
    inset 0 1px 0 rgba(255, 240, 210, 0.28),
    inset 0 -6px 14px rgba(0, 0, 0, 0.28);
}

/* ---------- Party select (4 characters) ---------- */

.party {
  position: fixed;
  inset: 0;
  z-index: 55;
  display: flex;
  /* Center shell when short; cap height so grid scrolls when tall */
  align-items: center;
  justify-content: center;
  height: 100dvh;
  max-height: 100dvh;
  width: 100%;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(140, 90, 220, 0.16), transparent 55%),
    radial-gradient(ellipse 50% 40% at 0% 100%, rgba(80, 150, 220, 0.1), transparent 50%),
    linear-gradient(180deg, #0c0e14 0%, #080a10 100%);
  padding:
    max(10px, var(--safe-top))
    max(12px, var(--safe-right))
    max(12px, var(--safe-bottom))
    max(12px, var(--safe-left));
  overflow: hidden;
  animation: backdrop-in 220ms ease both;
  box-sizing: border-box;
}

.party[hidden] {
  display: none;
}

.party__shell {
  width: min(100%, 520px);
  /* Content-sized on desktop (no stretched empty rows); scroll when needed */
  max-height: 100%;
  min-height: 0;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
}

.party__header {
  text-align: center;
  flex-shrink: 0;
}

.party__title {
  margin: 0;
  font-size: clamp(1.15rem, 5vw, 1.4rem);
  font-weight: 800;
  letter-spacing: 0.03em;
  background: linear-gradient(120deg, #fff 15%, #c4a8f0 55%, #7ab8f0 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.party__subtitle {
  margin: 6px 0 0;
  font-size: clamp(0.82rem, 3.4vw, 0.92rem);
  color: var(--text-muted);
}

.party__hint {
  margin: 6px 0 0;
  font-size: clamp(0.7rem, 2.8vw, 0.78rem);
  color: rgba(200, 190, 150, 0.55);
  line-height: 1.35;
  padding: 0 4px;
}

.party__status {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5em;
  font-size: 0.85rem;
  color: rgba(180, 190, 210, 0.65);
  flex-shrink: 0;
}

.party__status strong {
  font-variant-numeric: tabular-nums;
  color: #e8dcff;
  font-size: 1.05rem;
}

.party__grid {
  display: grid;
  /* Stable 3-column roster on phones; 4 cols from tablet up */
  grid-template-columns: repeat(3, minmax(0, 1fr));
  /* Tight, even gutters — rows must NOT stretch to fill leftover height */
  gap: 10px;
  align-content: start;
  align-items: start;
  justify-items: stretch;
  grid-auto-rows: auto;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1 1 auto;
  min-height: 0;
  padding: 4px 2px 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.party-card {
  position: relative;
  appearance: none;
  border: none;
  padding: 0;
  border-radius: 3px;
  /* Clip art; selection ring is drawn inset so it never covers neighbors */
  overflow: hidden;
  cursor: pointer;
  width: 100%;
  height: auto;
  aspect-ratio: 2 / 3;
  background: #141820;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  transition:
    box-shadow 180ms ease,
    filter 180ms ease;
  color: inherit;
  /* No scale by default — zoom was covering cards underneath on mobile */
  transform: none;
  z-index: auto;
  isolation: isolate;
  /* Keep hit target = visual card (no inflated stacking layer) */
  -webkit-tap-highlight-color: transparent;
}

/* Desktop hover only: slight lift, never large enough to block the next row */
@media (hover: hover) and (pointer: fine) {
  .party-card:hover:not(:disabled):not(.party-card--locked) {
    filter: brightness(1.08) saturate(1.06);
    box-shadow:
      0 8px 18px rgba(0, 0, 0, 0.45),
      0 0 0 1px rgba(180, 160, 255, 0.35),
      0 0 16px rgba(140, 110, 255, 0.18);
  }
}

.party-card--selected {
  filter: brightness(1.05);
  box-shadow:
    0 6px 16px rgba(0, 0, 0, 0.4),
    0 0 14px rgba(150, 110, 255, 0.25);
  /* Critical: do NOT raise z-index — selected cards were blocking the row below */
  z-index: auto;
}

/* Full selection overlay stays inside the card (not clipped / cut off) */
.party-card--selected::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow:
    inset 0 0 0 3px rgba(170, 140, 255, 0.95),
    inset 0 0 24px rgba(140, 100, 255, 0.22);
  pointer-events: none;
  z-index: 4;
}

.party-card--locked {
  cursor: not-allowed;
  filter: grayscale(0.85) brightness(0.55);
}

.party-card__art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  display: block;
  pointer-events: none;
}

.party-card__label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px 4px 6px;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.78));
  font-size: clamp(0.58rem, 2.6vw, 0.68rem);
  font-weight: 700;
  text-align: center;
  letter-spacing: 0.03em;
  color: rgba(245, 245, 250, 0.92);
  pointer-events: none;
  z-index: 2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.party-card__check {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: linear-gradient(145deg, #b8a0ff, #7a5ae0);
  color: #0c0a12;
  font-size: 0.7rem;
  font-weight: 800;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.45),
    0 0 0 2px rgba(20, 12, 32, 0.55);
  pointer-events: none;
  z-index: 5;
}

.party-card--selected .party-card__check {
  display: flex;
}

.party-card__lock {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.35);
  color: rgba(220, 220, 230, 0.55);
  pointer-events: none;
}

.party-card__lock svg {
  width: 28px;
  height: 28px;
}

/* Teaser tile for unreleased cast (characters 5–16) */
.party-more {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 52px;
  padding: 14px 16px;
  border-radius: 10px;
  border: 1px dashed rgba(180, 170, 220, 0.28);
  background:
    radial-gradient(ellipse 80% 120% at 50% 0%, rgba(140, 110, 220, 0.12), transparent 65%),
    rgba(20, 22, 32, 0.55);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  pointer-events: none;
  user-select: none;
}

.party-more__text {
  font-size: clamp(0.85rem, 3.2vw, 0.95rem);
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(200, 195, 230, 0.55);
}

.party__start {
  flex-shrink: 0;
  margin-top: 2px;
  min-height: 48px;
  font-size: 1rem;
  box-shadow:
    0 4px 18px rgba(61, 143, 208, 0.4),
    0 -12px 24px rgba(8, 10, 16, 0.85),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.party__start:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: grayscale(0.3);
  box-shadow:
    0 4px 18px rgba(61, 143, 208, 0.15),
    0 -12px 24px rgba(8, 10, 16, 0.85);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 80;
  max-width: min(92vw, 360px);
  padding: 12px 18px;
  border-radius: 999px;
  background: linear-gradient(145deg, rgba(48, 40, 28, 0.95), rgba(20, 16, 12, 0.96));
  border: 1px solid rgba(240, 196, 90, 0.35);
  color: #ffe9c8;
  font-size: 0.9rem;
  font-weight: 650;
  text-align: center;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45), 0 0 24px rgba(240, 180, 80, 0.15);
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease, transform 220ms ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast[hidden] {
  display: block !important; /* allow fade; visibility via opacity */
  opacity: 0;
}

/* ---------- Codex album (full-screen collection) ---------- */

.codex {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(180, 120, 40, 0.14), transparent 50%),
    radial-gradient(ellipse 50% 40% at 100% 100%, rgba(100, 60, 160, 0.1), transparent 50%),
    linear-gradient(180deg, #0c0e14 0%, #080a10 100%);
  padding:
    calc(12px + var(--safe-top))
    calc(14px + var(--safe-right))
    calc(16px + var(--safe-bottom))
    calc(14px + var(--safe-left));
  overflow: hidden;
  animation: backdrop-in 220ms ease both;
}

.codex[hidden] {
  display: none;
}

.codex__shell {
  width: min(100%, 720px);
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.codex__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
  margin-bottom: 12px;
}

.codex__heading {
  min-width: 0;
}

.codex__title {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: linear-gradient(120deg, #fff8e8 10%, #f0c45a 55%, #e8a838 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.codex__subtitle {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.codex__progress {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas:
    "label value"
    "bar bar";
  gap: 4px 12px;
  align-items: baseline;
  margin-bottom: 8px;
}

.codex__progress-label {
  grid-area: label;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: rgba(200, 190, 160, 0.55);
}

.codex__progress-value {
  grid-area: value;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
  color: #f0d090;
  font-weight: 750;
}

.codex__progress-bar {
  grid-area: bar;
  height: 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.codex__progress-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #c4923a, #f0c45a 50%, #ffe9a8);
  box-shadow: 0 0 12px rgba(240, 196, 90, 0.35);
  transition: width 320ms cubic-bezier(0.22, 1, 0.36, 1);
}

.codex__hint {
  flex-shrink: 0;
  margin: 0 0 10px;
  font-size: 0.78rem;
  color: rgba(170, 182, 200, 0.45);
  text-align: center;
}

.codex__album {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scrollbar-width: thin;
  scrollbar-color: rgba(240, 196, 90, 0.35) transparent;
}

.codex__row {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.codex__row-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 0 2px;
}

.codex__row-title {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: rgba(235, 230, 220, 0.85);
}

.codex__row-count {
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  color: rgba(200, 190, 150, 0.5);
}

.codex__more {
  margin: 8px 0 4px;
  padding: 16px 12px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(200, 195, 230, 0.45);
  border-radius: 10px;
  border: 1px dashed rgba(180, 170, 220, 0.22);
  background: rgba(20, 22, 32, 0.4);
}

.codex__levels {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

@media (min-width: 480px) {
  .codex__levels {
    grid-template-columns: repeat(8, 1fr);
    gap: 10px;
  }
}

@media (min-width: 720px) {
  .codex__levels {
    grid-template-columns: repeat(8, 1fr);
  }
}

.codex-card {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  border: none;
  padding: 0;
  cursor: default;
  background: linear-gradient(160deg, #1a1e28, #0e1016);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  transition: transform 180ms ease, box-shadow 180ms ease;
}

.codex-card--unlocked {
  cursor: pointer;
}

.codex-card--unlocked:hover,
.codex-card--unlocked:focus-visible {
  transform: translateY(-3px) scale(1.04);
  box-shadow:
    0 10px 22px rgba(0, 0, 0, 0.45),
    0 0 16px rgba(240, 196, 90, 0.2);
  outline: none;
  z-index: 2;
}

.codex-card__art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  display: block;
}

.codex-card--locked {
  background:
    radial-gradient(circle at 50% 40%, rgba(255, 255, 255, 0.04), transparent 55%),
    linear-gradient(160deg, #161a22, #0a0c10);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.codex-card__lock {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: rgba(140, 150, 170, 0.35);
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  pointer-events: none;
}

.codex-card__lock-icon {
  width: 18px;
  height: 18px;
  opacity: 0.55;
}

.codex-card__level {
  position: absolute;
  top: 5%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2;
  min-width: 1.4em;
  padding: 0.12em 0.35em;
  border-radius: 999px;
  font-size: 0.62rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #fff8e7;
  background: rgba(10, 10, 14, 0.72);
  box-shadow: 0 0 0 1px rgba(255, 220, 140, 0.45);
  pointer-events: none;
}

.codex-card--locked .codex-card__level {
  opacity: 0.35;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
  color: rgba(200, 210, 220, 0.5);
}

/* Full-screen card admirer + level swipe */
.codex-viewer {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.codex-viewer[hidden] {
  display: none;
}

.codex-viewer__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 6, 12, 0.82);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: backdrop-in 200ms ease both;
}

.codex-viewer__panel {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas:
    "prev stage next"
    ". meta ."
    ". dots .";
  align-items: center;
  justify-items: center;
  column-gap: 8px;
  row-gap: 12px;
  width: min(100%, 480px);
  max-height: min(92dvh, 900px);
  animation: modal-in 260ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
  pointer-events: none;
}

.codex-viewer__stage {
  grid-area: stage;
  position: relative;
  width: min(78vw, 360px);
  max-height: min(72dvh, 680px);
  aspect-ratio: 2 / 3;
  pointer-events: auto;
  touch-action: pan-y;
  user-select: none;
}

.codex-viewer__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  border-radius: 8px;
  box-shadow:
    0 28px 60px rgba(0, 0, 0, 0.6),
    0 0 48px rgba(240, 196, 90, 0.14);
  display: block;
  transition: opacity 160ms ease, transform 200ms ease;
}

.codex-viewer__img.is-swap-left {
  animation: codex-swap-from-right 220ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

.codex-viewer__img.is-swap-right {
  animation: codex-swap-from-left 220ms cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes codex-swap-from-right {
  from {
    opacity: 0.35;
    transform: translateX(28px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

@keyframes codex-swap-from-left {
  from {
    opacity: 0.35;
    transform: translateX(-28px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateX(0) scale(1);
  }
}

.codex-viewer__nav {
  appearance: none;
  border: none;
  pointer-events: auto;
  position: relative;
  isolation: isolate;
  overflow: hidden;
  width: 48px;
  height: 48px;
  min-width: 48px;
  min-height: 48px;
  padding: 0;
  margin: 0;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  color: #ffe9c8;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 220, 160, 0.22), transparent 48%),
    linear-gradient(155deg, #2e2830 0%, #141218 55%, #0c0a10 100%);
  box-shadow:
    0 8px 20px rgba(0, 0, 0, 0.45),
    0 0 18px rgba(230, 170, 80, 0.14),
    0 0 0 1px rgba(240, 190, 110, 0.22),
    inset 0 1px 0 rgba(255, 230, 190, 0.18),
    inset 0 -6px 12px rgba(0, 0, 0, 0.28);
  transition:
    transform 180ms cubic-bezier(0.22, 1, 0.36, 1),
    opacity 160ms ease,
    box-shadow 180ms ease,
    color 160ms ease,
    filter 160ms ease;
}

.codex-viewer__nav-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  box-shadow: inset 0 0 0 1.5px rgba(255, 210, 140, 0.28);
}

.codex-viewer__nav-shine {
  position: absolute;
  inset: 2px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 1;
  background: linear-gradient(
    160deg,
    rgba(255, 255, 255, 0.18) 0%,
    transparent 42%
  );
}

.codex-viewer__nav-icon {
  position: relative;
  z-index: 2;
  width: 22px;
  height: 22px;
  display: block;
  /* Optical centering: chevrons read slightly off without this */
  transform: translateX(0);
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.4));
  pointer-events: none;
}

.codex-viewer__nav--prev .codex-viewer__nav-icon {
  transform: translateX(-0.5px);
}

.codex-viewer__nav--next .codex-viewer__nav-icon {
  transform: translateX(0.5px);
}

.codex-viewer__nav:hover:not(:disabled) {
  transform: scale(1.07);
  color: #fff6e0;
  box-shadow:
    0 10px 26px rgba(0, 0, 0, 0.5),
    0 0 26px rgba(240, 180, 80, 0.32),
    0 0 0 1px rgba(255, 220, 150, 0.4),
    inset 0 1px 0 rgba(255, 240, 210, 0.28),
    inset 0 -6px 12px rgba(0, 0, 0, 0.28);
}

.codex-viewer__nav:hover:not(:disabled) .codex-viewer__nav-ring {
  box-shadow:
    inset 0 0 0 1.5px rgba(255, 220, 150, 0.5),
    0 0 12px rgba(240, 180, 80, 0.2);
}

.codex-viewer__nav:active:not(:disabled) {
  transform: scale(0.94);
}

.codex-viewer__nav:disabled {
  opacity: 0.28;
  cursor: default;
  filter: grayscale(0.35) saturate(0.7);
  box-shadow:
    0 4px 12px rgba(0, 0, 0, 0.35),
    0 0 0 1px rgba(255, 255, 255, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.codex-viewer__nav--prev {
  grid-area: prev;
}

.codex-viewer__nav--next {
  grid-area: next;
}

.codex-viewer__meta {
  grid-area: meta;
  margin: 0;
  font-size: 1rem;
  color: rgba(230, 220, 200, 0.85);
  text-align: center;
  letter-spacing: 0.03em;
  pointer-events: none;
}

.codex-viewer__dots {
  grid-area: dots;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 100%;
  pointer-events: none;
}

.codex-viewer__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.codex-viewer__dot.is-active {
  background: linear-gradient(145deg, #ffe9a8, #e0a838);
  box-shadow: 0 0 8px rgba(240, 196, 90, 0.45);
  transform: scale(1.15);
}

@media (max-width: 420px) {
  .codex-viewer__panel {
    column-gap: 4px;
  }

  .codex-viewer__nav {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
  }

  .codex-viewer__nav-icon {
    width: 20px;
    height: 20px;
  }

  .codex-viewer__stage {
    width: min(70vw, 300px);
  }
}

/* ---------- Score HUD — run first, best as soft goal (less pressure) ---------- */

.score-hud {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  width: var(--board-width);
  margin: 0 auto 8px;
  position: relative;
  z-index: 2;
  padding: 2px 0 2px;
  box-sizing: border-box;
  flex-shrink: 0;
}

/* Soft wash behind hero score only */
.score-hud::before {
  content: "";
  position: absolute;
  left: 20%;
  right: 20%;
  top: 30%;
  bottom: -8px;
  background: radial-gradient(
    ellipse at 50% 40%,
    rgba(100, 150, 230, 0.12),
    transparent 70%
  );
  pointer-events: none;
  z-index: 0;
}

/* Heart | Score | Bolt */
.score-hud__row {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(56px, 1fr) auto minmax(56px, 1fr);
  align-items: center;
  width: 100%;
  gap: 8px;
}

.score-hero {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
  min-width: 0;
}

.score-hero__label {
  font-size: 0.62rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: rgba(170, 182, 200, 0.5);
}

.score-hero__value {
  font-size: clamp(2rem, 7vw, 2.6rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: -0.02em;
  background: linear-gradient(180deg, #ffffff 12%, #c5dcf8 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 4px 16px rgba(100, 170, 255, 0.2));
  transition: filter 240ms ease, opacity 240ms ease, transform 240ms ease;
}

/* Idle (score 0): calm, no “zero shame” — soft placeholder energy */
.score-hud[data-phase="idle"] .score-hero__value {
  opacity: 0.42;
  filter: drop-shadow(0 2px 8px rgba(100, 170, 255, 0.08));
  letter-spacing: 0.04em;
}

.score-hud[data-phase="idle"] .score-hero__label {
  color: rgba(170, 182, 200, 0.42);
}

/* Playing: full presence */
.score-hud[data-phase="playing"] .score-hero__value {
  opacity: 1;
}

/* Matching / beating best: warm lift without yelling */
.score-hud[data-phase="record"] .score-hero__value {
  background: linear-gradient(180deg, #fff8e8 5%, #f0c45a 55%, #e0a838 100%);
  -webkit-background-clip: text;
  background-clip: text;
  filter: drop-shadow(0 4px 18px rgba(255, 190, 80, 0.32));
}

.score-hud[data-phase="record"]::before {
  background: radial-gradient(
    ellipse at 50% 40%,
    rgba(240, 190, 90, 0.16),
    transparent 70%
  );
}

/* Best as a quiet goal chip — never competes with the run score */
.score-goal {
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: baseline;
  gap: 0.45em;
  padding: 3px 0;
  max-width: 100%;
  font-size: 0.78rem;
  line-height: 1.2;
  color: rgba(170, 182, 200, 0.48);
  transition: color 240ms ease, opacity 240ms ease;
}

.score-goal[hidden] {
  display: none !important;
}

.score-goal__label {
  font-weight: 550;
  letter-spacing: 0.04em;
}

.score-goal__value {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: rgba(210, 195, 150, 0.72);
  letter-spacing: 0.01em;
}

/* Soften best even more at the start of a run */
.score-hud[data-phase="idle"] .score-goal {
  opacity: 0.85;
}

.score-hud[data-phase="record"] .score-goal {
  color: rgba(240, 210, 140, 0.7);
}

.score-hud[data-phase="record"] .score-goal__value {
  color: rgba(255, 220, 140, 0.9);
}

/* ---------- Power tokens (floating icons, not chips) ---------- */

.power-token {
  appearance: none;
  border: none;
  background: transparent;
  box-shadow: none;
  padding: 4px;
  margin: 0;
  cursor: pointer;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 52px;
  min-height: 56px;
  color: inherit;
  font: inherit;
  transition: transform 200ms cubic-bezier(0.22, 1, 0.36, 1), opacity 180ms ease, filter 180ms ease;
}

.power-token--heart {
  justify-self: start;
}

.power-token--bolt {
  justify-self: end;
}

.power-token__glow {
  position: absolute;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
  opacity: 0.55;
  transition: opacity 200ms ease, transform 200ms ease;
}

.power-token--heart .power-token__glow {
  background: radial-gradient(circle, rgba(255, 80, 130, 0.45) 0%, transparent 68%);
}

.power-token--bolt .power-token__glow {
  background: radial-gradient(circle, rgba(255, 200, 60, 0.42) 0%, transparent 68%);
}

.power-token__svg {
  position: relative;
  z-index: 1;
  width: 42px;
  height: 42px;
  display: block;
  filter:
    drop-shadow(0 4px 10px rgba(0, 0, 0, 0.4))
    drop-shadow(0 0 12px rgba(255, 120, 150, 0.25));
  transition: transform 200ms cubic-bezier(0.22, 1, 0.36, 1), filter 200ms ease;
}

.power-token--bolt .power-token__svg {
  filter:
    drop-shadow(0 4px 10px rgba(0, 0, 0, 0.4))
    drop-shadow(0 0 12px rgba(255, 200, 80, 0.3));
}

.power-token__count {
  position: relative;
  z-index: 1;
  font-size: 0.95rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  letter-spacing: 0.02em;
  color: rgba(245, 240, 255, 0.88);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.55);
  min-width: 1.1em;
  text-align: center;
}

.power-token--heart .power-token__count {
  color: #ffc0d0;
}

.power-token--bolt .power-token__count {
  color: #ffe6a0;
}

.power-token:hover:not(:disabled) {
  transform: translateY(-2px) scale(1.08);
}

.power-token:hover:not(:disabled) .power-token__glow {
  opacity: 0.9;
  transform: scale(1.15);
}

.power-token:hover:not(:disabled) .power-token__svg {
  transform: scale(1.06);
}

.power-token:active:not(:disabled) {
  transform: scale(0.94);
}

.power-token:disabled {
  opacity: 0.28;
  cursor: default;
  filter: grayscale(0.55) brightness(0.85);
}

.power-token:disabled .power-token__glow {
  opacity: 0.15;
}

/* Armed = ready to place on a card */
.power-token.is-armed {
  transform: translateY(-3px) scale(1.12);
}

.power-token.is-armed .power-token__glow {
  opacity: 1;
  transform: scale(1.25);
  animation: power-token-breathe 1.4s ease-in-out infinite;
}

.power-token--heart.is-armed .power-token__svg {
  filter:
    drop-shadow(0 4px 12px rgba(0, 0, 0, 0.45))
    drop-shadow(0 0 18px rgba(255, 90, 140, 0.65));
  animation: power-heart-wiggle 1.1s ease-in-out infinite;
}

.power-token--bolt.is-armed .power-token__svg {
  filter:
    drop-shadow(0 4px 12px rgba(0, 0, 0, 0.45))
    drop-shadow(0 0 18px rgba(255, 200, 60, 0.7));
  animation: power-bolt-crackle 0.9s ease-in-out infinite;
}

@keyframes power-token-breathe {
  0%, 100% { transform: scale(1.15); opacity: 0.85; }
  50% { transform: scale(1.35); opacity: 1; }
}

@keyframes power-heart-wiggle {
  0%, 100% { transform: scale(1.06); }
  30% { transform: scale(1.14); }
  55% { transform: scale(1.02); }
  75% { transform: scale(1.1); }
}

@keyframes power-bolt-crackle {
  0%, 100% { transform: scale(1.06) rotate(0deg); }
  25% { transform: scale(1.12) rotate(-4deg); }
  50% { transform: scale(1.04) rotate(3deg); }
  75% { transform: scale(1.1) rotate(-2deg); }
}

.power-hint {
  margin: 2px 0 0;
  width: 100%;
  text-align: center;
  font-size: 0.78rem;
  color: rgba(200, 190, 220, 0.7);
  min-height: 1.1em;
}

.power-hint[hidden] {
  display: none;
}

.card.is-power-target {
  z-index: 5;
  box-shadow:
    0 0 0 2px rgba(200, 170, 255, 0.75),
    0 0 18px rgba(160, 120, 255, 0.35) !important;
  animation: power-target-pulse 1.4s ease-in-out infinite;
}

/* Love: soft pink bloom + heartbeat swell */
.card.is-power-heart {
  z-index: 12;
  animation: power-love 780ms cubic-bezier(0.18, 1.15, 0.28, 1) both;
  box-shadow:
    0 0 0 3px rgba(255, 120, 160, 0.85),
    0 0 40px rgba(255, 80, 140, 0.55),
    0 0 70px rgba(255, 60, 120, 0.3) !important;
}

.card.is-power-heart::before {
  content: "";
  position: absolute;
  inset: -8%;
  z-index: 6;
  pointer-events: none;
  border-radius: inherit;
  background:
    radial-gradient(circle at 50% 45%, rgba(255, 140, 180, 0.35), transparent 55%),
    radial-gradient(circle at 30% 30%, rgba(255, 200, 220, 0.25), transparent 40%);
  animation: love-aura 780ms ease-out both;
}

.card.is-power-heart::after {
  content: "♥";
  position: absolute;
  left: 50%;
  top: 42%;
  z-index: 7;
  font-size: 1.8rem;
  color: #ff6b9d;
  text-shadow:
    0 0 12px rgba(255, 100, 150, 0.9),
    0 0 24px rgba(255, 60, 120, 0.6);
  pointer-events: none;
  animation: love-hearts 780ms cubic-bezier(0.2, 1.1, 0.3, 1) both;
}

/* Disintegration: shatter / scatter */
.card.is-power-bolt {
  z-index: 12;
  animation: power-disintegrate 720ms cubic-bezier(0.25, 0.1, 0.25, 1) both;
}

.card.is-power-bolt::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  border-radius: inherit;
  background:
    linear-gradient(120deg, transparent 30%, rgba(255, 240, 180, 0.55) 48%, transparent 62%),
    radial-gradient(circle at 50% 40%, rgba(255, 220, 100, 0.4), transparent 50%);
  mix-blend-mode: screen;
  animation: bolt-slash 720ms ease-out both;
}

.card.is-power-bolt::after {
  content: "";
  position: absolute;
  inset: -12%;
  z-index: 7;
  pointer-events: none;
  border-radius: inherit;
  background:
    radial-gradient(circle at 20% 30%, rgba(255, 230, 120, 0.7) 0 2px, transparent 3px),
    radial-gradient(circle at 70% 25%, rgba(255, 200, 80, 0.65) 0 2px, transparent 3px),
    radial-gradient(circle at 40% 70%, rgba(255, 240, 160, 0.6) 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 80% 60%, rgba(255, 180, 60, 0.55) 0 2px, transparent 3px),
    radial-gradient(circle at 55% 40%, rgba(255, 255, 200, 0.8) 0 1.5px, transparent 2.5px),
    radial-gradient(circle at 30% 55%, rgba(255, 210, 100, 0.5) 0 1px, transparent 2px);
  animation: bolt-shards 720ms cubic-bezier(0.2, 0.7, 0.2, 1) both;
}

/* Power-up drop on card */
.power-drop {
  position: absolute;
  left: 50%;
  top: 42%;
  z-index: 30;
  transform: translate(-50%, -50%);
  pointer-events: none;
  animation: power-drop-pop 420ms cubic-bezier(0.18, 1.35, 0.3, 1) both;
  filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.45));
}

.power-drop__glyph {
  display: block;
  font-size: clamp(1.6rem, 5vw, 2.1rem);
  line-height: 1;
  text-shadow: 0 0 16px currentColor;
}

.power-drop--heart .power-drop__glyph {
  color: #ff5a8a;
  filter: drop-shadow(0 0 12px rgba(255, 80, 130, 0.75));
}

.power-drop--bolt .power-drop__glyph {
  color: #ffd24a;
  filter: drop-shadow(0 0 12px rgba(255, 200, 60, 0.75));
}

.power-drop.is-flying {
  animation: power-drop-fly 480ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

.power-drop.is-fade {
  animation: power-drop-fade 360ms ease forwards;
}

@keyframes power-drop-pop {
  0% {
    opacity: 0;
    transform: translate(-50%, -30%) scale(0.2);
  }
  55% {
    opacity: 1;
    transform: translate(-50%, -55%) scale(1.35);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

@keyframes power-drop-fly {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(
        calc(-50% + var(--fly-x, 0px)),
        calc(-50% + var(--fly-y, -40px))
      )
      scale(0.35);
  }
}

@keyframes power-drop-fade {
  to {
    opacity: 0;
    transform: translate(-50%, -80%) scale(0.6);
  }
}

@keyframes power-target-pulse {
  0%, 100% {
    filter: brightness(1.05);
  }
  50% {
    filter: brightness(1.14) saturate(1.1);
  }
}

@keyframes power-love {
  0% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1) saturate(1);
  }
  18% {
    transform: translate(-50%, -50%) scale(0.92);
    filter: brightness(1.15) saturate(1.2);
  }
  42% {
    transform: translate(-50%, -50%) scale(1.22);
    filter: brightness(1.35) saturate(1.35) hue-rotate(-8deg);
  }
  62% {
    transform: translate(-50%, -50%) scale(1.08);
    filter: brightness(1.25) saturate(1.25);
  }
  78% {
    transform: translate(-50%, -50%) scale(1.16);
    filter: brightness(1.3) saturate(1.3);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1.1) saturate(1.15);
  }
}

@keyframes love-aura {
  0% { opacity: 0; transform: scale(0.7); }
  40% { opacity: 1; transform: scale(1.05); }
  100% { opacity: 0; transform: scale(1.35); }
}

@keyframes love-hearts {
  0% {
    opacity: 0;
    transform: translate(-50%, -40%) scale(0.4);
  }
  25% {
    opacity: 1;
    transform: translate(-50%, -55%) scale(1.2);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -120%) scale(0.7);
  }
}

@keyframes power-disintegrate {
  0% {
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
    filter: brightness(1) contrast(1);
    opacity: 1;
  }
  22% {
    transform: translate(-50%, -50%) scale(1.08) rotate(-1deg);
    filter: brightness(1.8) contrast(1.3) saturate(1.4);
  }
  48% {
    transform: translate(-50%, -50%) scale(1.02) rotate(1.5deg);
    filter: brightness(2.2) contrast(1.5) saturate(0.4);
    opacity: 0.85;
  }
  70% {
    transform: translate(-50%, -50%) scale(0.96) rotate(-0.5deg);
    filter: brightness(1.4) contrast(1.2);
    opacity: 0.95;
  }
  100% {
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
    filter: brightness(1.08);
    opacity: 1;
  }
}

@keyframes bolt-slash {
  0% {
    opacity: 0;
    transform: translateX(-40%) skewX(-12deg);
  }
  25% {
    opacity: 1;
    transform: translateX(0) skewX(-8deg);
  }
  55% {
    opacity: 0.7;
    transform: translateX(30%) skewX(-4deg);
  }
  100% {
    opacity: 0;
    transform: translateX(60%) skewX(0deg);
  }
}

@keyframes bolt-shards {
  0% {
    opacity: 0;
    transform: scale(0.9);
    filter: blur(0);
  }
  20% {
    opacity: 1;
    transform: scale(1.05);
  }
  100% {
    opacity: 0;
    transform: scale(1.45);
    filter: blur(1px);
  }
}

.lang-select-wrap {
  display: inline-flex;
}

.lang-select {
  min-height: var(--touch-min);
  min-width: var(--touch-min);
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: var(--bg-elevated);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.lang-select:hover {
  border-color: rgba(122, 184, 240, 0.35);
}

/* ---------- Buttons ---------- */

.btn {
  appearance: none;
  border: none;
  border-radius: 999px;
  min-height: var(--touch-min);
  padding: 0 16px;
  font-weight: 650;
  font-size: 0.88rem;
  cursor: pointer;
  transition:
    transform var(--transition-fast),
    background var(--transition-fast),
    box-shadow var(--transition-fast),
    filter var(--transition-fast);
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn--primary {
  background: linear-gradient(135deg, #8ec6f5 0%, var(--accent-strong) 55%, #3a7fc4 100%);
  color: #071018;
  box-shadow:
    0 4px 18px rgba(61, 143, 208, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.btn--primary:hover {
  filter: brightness(1.08);
  box-shadow:
    0 6px 22px rgba(61, 143, 208, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.btn--secondary {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text);
  backdrop-filter: blur(8px);
}

.btn--secondary:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(122, 184, 240, 0.3);
}

.btn--ghost {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--text-muted);
}

.btn--ghost:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
}

.btn--block {
  width: 100%;
}

.btn--icon {
  min-width: var(--touch-min);
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  font-size: 1.05rem;
  line-height: 1;
}

.btn--icon:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(122, 184, 240, 0.35);
}

.btn--icon[aria-pressed="true"] {
  opacity: 0.7;
}

/* ---------- Main / board ---------- */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  width: 100%;
  /* Prefer fitting the board over scrolling the whole app on phones */
  gap: 0;
}

/* Shell only sizes the grid — no visual frame */
.board-shell {
  width: var(--board-width);
  /* Extra room so zoomed cards aren’t clipped by neighbors’ overflow.
     Smaller on phones so the board doesn’t force page scroll. */
  padding: 12px;
  margin: -12px auto;
  box-sizing: content-box;
  flex-shrink: 0;
}

.board {
  width: 100%;
  /* Cells are 2:3 cards → board height follows content; gap is identical H & V */
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, auto);
  gap: var(--gap);
  padding: 0;
  margin: 0;
  background: transparent;
  border: none;
  border-radius: 0;
  box-shadow: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  /* Allow card zoom to paint outside cell bounds */
  overflow: visible;
}

/* Board meta-level (sum of card levels) */
.board-level {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: var(--board-width);
  margin: 8px auto 0;
  text-align: center;
  flex-shrink: 0;
}

.board-level__title {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.35em 0.4em;
}

/* Same typeface / weight / gradient as the level number */
.board-level__label,
.board-level__value {
  font-size: clamp(1.35rem, 4.5vw, 1.75rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  line-height: 1;
  background: linear-gradient(180deg, #fff 10%, #c4a8f0 55%, #7ab8f0 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 10px rgba(140, 120, 255, 0.25));
}

.board-level__label {
  text-transform: uppercase;
}

.board-level__bar {
  width: min(100%, 220px);
  height: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  box-shadow:
    inset 0 1px 2px rgba(0, 0, 0, 0.35),
    0 0 0 1px rgba(255, 255, 255, 0.05);
  overflow: hidden;
}

.board-level__fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #7a5ae0 0%, #b8a0ff 45%, #8ec6f5 100%);
  box-shadow: 0 0 12px rgba(160, 130, 255, 0.45);
  transition: width 320ms cubic-bezier(0.22, 1, 0.36, 1);
}

.board-level.is-level-up .board-level__label,
.board-level.is-level-up .board-level__value {
  animation: board-level-pop 560ms cubic-bezier(0.18, 1.2, 0.3, 1) both;
}

.board-level.is-level-up .board-level__fill {
  box-shadow: 0 0 18px rgba(200, 160, 255, 0.7);
}

@keyframes board-level-pop {
  0% { transform: scale(1); filter: drop-shadow(0 2px 10px rgba(140, 120, 255, 0.25)); }
  40% { transform: scale(1.22); filter: drop-shadow(0 0 18px rgba(200, 160, 255, 0.55)); }
  100% { transform: scale(1); filter: drop-shadow(0 2px 10px rgba(140, 120, 255, 0.25)); }
}

.hint {
  margin: 10px 8px 0;
  text-align: center;
  font-size: clamp(0.75rem, 3.2vw, 0.84rem);
  color: var(--text-muted);
  max-width: 26rem;
  line-height: 1.4;
  opacity: 0.9;
  padding: 0 4px;
}

/* ---------- Cells & cards ---------- */

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  /* Same aspect as a real playing card → equal visual gaps H/V */
  aspect-ratio: 2 / 3;
  background: transparent;
  border: none;
  border-radius: 0;
  overflow: visible;
}

/* Floating card — portrait via <img.card__art>, overlays above */
.card {
  position: absolute;
  left: 50%;
  top: 50%;
  --card-scale: 1;
  transform: translate(-50%, -50%) scale(var(--card-scale));
  width: 100%;
  height: 100%;
  border-radius: var(--card-radius);
  border: none;
  outline: none;
  overflow: hidden;
  cursor: grab;
  transform-origin: center center;
  transition:
    transform var(--transition),
    box-shadow var(--transition),
    filter var(--transition),
    opacity var(--transition);
  will-change: transform, opacity, filter;
  box-shadow:
    0 6px 16px rgba(0, 0, 0, 0.4),
    0 2px 4px rgba(0, 0, 0, 0.25);
  background-color: var(--card-color, #3a4a5c);
  -webkit-user-drag: none;
}

/* Definitive portrait image (reliable vs CSS url vars) */
.card__art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  border-radius: inherit;
  pointer-events: none;
  z-index: 0;
  -webkit-user-drag: none;
  user-select: none;
}

/* Soft sheen / vignette over the photo */
.card::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.08) 0%,
    transparent 22%,
    transparent 70%,
    rgba(0, 0, 0, 0.22) 100%
  );
  pointer-events: none;
  z-index: 1;
}

.card--has-art::before {
  background: linear-gradient(
    180deg,
    rgba(0, 0, 0, 0.12) 0%,
    transparent 18%,
    transparent 78%,
    rgba(0, 0, 0, 0.28) 100%
  );
}

/*
 * Level chip — bottom-left corner, nestled in the cream frame.
 * Soft opacity so it reads as part of the mat, not a loud HUD.
 */
.card__level {
  position: absolute;
  left: 4.5%;
  bottom: 3.2%;
  top: auto;
  right: auto;
  transform: none;
  z-index: 5;
  min-width: 1.35em;
  height: 1.22em;
  padding: 0.08em 0.34em 0; /* slight top pad — Cinzel sits optically high */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  /* Elegant display numerals (Cinzel) with system fallbacks */
  font-family: "Cinzel", "Cormorant Garamond", "Palatino Linotype", Palatino, "Times New Roman", serif;
  font-size: clamp(0.62rem, 2.3vw, 0.78rem);
  font-weight: 700;
  font-variant-numeric: lining-nums;
  letter-spacing: 0.04em;
  line-height: 1;
  color: rgba(48, 38, 28, 0.78);
  opacity: 0.58;
  background: linear-gradient(
    165deg,
    rgba(247, 241, 230, 0.92) 0%,
    rgba(232, 222, 204, 0.88) 55%,
    rgba(220, 208, 188, 0.9) 100%
  );
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.18),
    0 0 0 1px rgba(120, 95, 60, 0.22),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
  pointer-events: none;
  user-select: none;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.card__level::before {
  display: none;
}

/* Always visible on definitive art */
.card--has-art .card__level {
  display: inline-flex;
}

/* Slightly clearer when the card is focused / matching */
.card.is-selected .card__level,
.card.is-match .card__level {
  opacity: 0.72;
}

.card.is-selected .card__level {
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.2),
    0 0 0 1px rgba(100, 150, 210, 0.4),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.card.is-match .card__level {
  box-shadow:
    0 1px 2px rgba(0, 0, 0, 0.2),
    0 0 0 1px rgba(90, 190, 140, 0.38),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

.card:active {
  cursor: grabbing;
}

.card__face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 2;
}

/* Abstract silhouette / glyph for each type (no level numbers) */
.card__glyph {
  width: 52%;
  height: 52%;
  border-radius: 50%;
  background: var(--glyph-bg, rgba(255, 255, 255, 0.18));
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.2),
    0 4px 14px rgba(0, 0, 0, 0.28);
  position: relative;
  filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.12));
}

.card__glyph::before,
.card__glyph::after {
  content: "";
  position: absolute;
  background: var(--glyph-accent, rgba(255, 255, 255, 0.35));
  border-radius: inherit;
}

/* Level progression: soft inner glow only (no rectangular frame) */
.card::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: var(--level-inner-glow, none);
  pointer-events: none;
  z-index: 1;
}

/* Level marks at corners — visual rank, no digits */
.card__level-mark {
  position: absolute;
  width: 16%;
  height: 10%;
  border-radius: 999px;
  background: var(--level-mark, rgba(255, 255, 255, 0.25));
  opacity: var(--level-mark-opacity, 0.35);
  pointer-events: none;
  z-index: 2;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.15);
}

.card__level-mark--tl { top: 8%; left: 9%; }
.card__level-mark--tr { top: 8%; right: 9%; }
.card__level-mark--bl { bottom: 8%; left: 9%; }
.card__level-mark--br { bottom: 8%; right: 9%; }

/* ---------- Interaction: zoom hover / select ---------- */

/* Desktop / fine pointer: pronounced hover zoom */
@media (hover: hover) and (pointer: fine) {
  .card:hover:not(.is-dragging):not(.is-merging):not(.is-spawn):not(.is-pop) {
    --card-scale: var(--hover-scale);
    /* Explicit transform so hover wins even if an animation class lingers */
    transform: translate(-50%, -50%) scale(var(--hover-scale));
    z-index: 8;
    filter: brightness(1.12) saturate(1.12);
    box-shadow:
      0 20px 42px rgba(0, 0, 0, 0.55),
      0 0 32px rgba(122, 184, 240, 0.28);
  }

  .card.is-selected:hover:not(.is-dragging):not(.is-spawn):not(.is-pop) {
    --card-scale: calc(var(--select-scale) + 0.06);
    transform: translate(-50%, -50%) scale(calc(var(--select-scale) + 0.06));
  }

  .card.is-match:hover:not(.is-dragging):not(.is-spawn):not(.is-pop) {
    --card-scale: calc(var(--match-scale) + 0.06);
    transform: translate(-50%, -50%) scale(calc(var(--match-scale) + 0.06));
  }
}

.card.is-selected {
  --card-scale: var(--select-scale);
  transform: translate(-50%, -50%) scale(var(--select-scale));
  z-index: 10;
  filter: brightness(1.12) saturate(1.1);
  box-shadow:
    0 18px 40px rgba(0, 0, 0, 0.55),
    0 0 0 2px rgba(122, 184, 240, 0.85),
    0 0 32px var(--glow);
}

.card.is-match {
  --card-scale: var(--match-scale);
  transform: translate(-50%, -50%) scale(var(--match-scale));
  z-index: 6;
  filter: brightness(1.1);
  box-shadow:
    0 14px 32px rgba(0, 0, 0, 0.45),
    0 0 0 2px rgba(94, 224, 160, 0.75),
    0 0 26px var(--match-glow);
}

/* Idle hint: soft pulsing halo on mergeable cards (after inactivity) */
.card.is-hint-merge {
  z-index: 4;
  animation: hint-halo 1.6s ease-in-out infinite;
}

.card.is-hint-merge.is-selected,
.card.is-hint-merge.is-match,
.card.is-hint-merge.is-dragging,
.card.is-hint-merge.is-merging {
  animation: none;
}

.card.is-invalid {
  animation: shake 280ms ease;
  box-shadow:
    0 8px 20px rgba(0, 0, 0, 0.4),
    0 0 0 2px var(--danger),
    0 0 16px rgba(255, 123, 134, 0.45);
}

.card.is-dragging {
  opacity: 0.3;
  --card-scale: 0.92;
  cursor: grabbing;
  filter: grayscale(0.15);
}

.card.is-drop-target {
  --card-scale: 1.2;
  z-index: 9;
  box-shadow:
    0 16px 36px rgba(0, 0, 0, 0.5),
    0 0 0 2px rgba(94, 224, 160, 0.9),
    0 0 32px var(--match-glow);
  filter: brightness(1.12);
}

/* Points gained on merge — floats up from the target card */
.card__points {
  position: absolute;
  left: 50%;
  top: 42%;
  z-index: 20;
  transform: translate(-50%, -50%);
  font-size: clamp(1.05rem, 4.2vw, 1.45rem);
  font-weight: 850;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  color: #fff8c8;
  text-shadow:
    0 0 10px rgba(255, 220, 100, 0.85),
    0 2px 6px rgba(0, 0, 0, 0.75),
    0 0 2px rgba(0, 0, 0, 0.9);
  pointer-events: none;
  white-space: nowrap;
  animation: points-float 900ms cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}

.card.is-merging {
  z-index: 12;
  pointer-events: none;
  animation: merge-out 280ms ease forwards;
}

/* Line match — triple (edges pull in, core upgrades) */
.card.is-line-3-edge,
.card.is-line-3-core {
  z-index: 14;
  pointer-events: none;
  animation-delay: var(--line-delay, 0ms);
  animation-fill-mode: both;
}

.card.is-line-3-edge {
  animation-name: line-edge-pull;
  animation-duration: 560ms;
  animation-timing-function: cubic-bezier(0.2, 0.9, 0.2, 1);
  box-shadow:
    0 0 0 2px rgba(120, 200, 255, 0.75),
    0 0 22px rgba(100, 180, 255, 0.4) !important;
}

.card.is-line-3-core {
  z-index: 16;
  animation-name: line-core-burst;
  animation-duration: 560ms;
  animation-timing-function: cubic-bezier(0.18, 1.2, 0.3, 1);
  box-shadow:
    0 0 0 3px rgba(160, 220, 255, 1),
    0 0 40px rgba(120, 200, 255, 0.75),
    0 0 70px rgba(80, 160, 255, 0.35) !important;
}

/* Line match — quad (edges spark, two cores fuse-burst) */
.card.is-line-4-edge,
.card.is-line-4-core {
  z-index: 15;
  pointer-events: none;
  animation-delay: var(--line-delay, 0ms);
  animation-fill-mode: both;
}

.card.is-line-4-edge {
  animation-name: line-edge-spark;
  animation-duration: 680ms;
  animation-timing-function: cubic-bezier(0.2, 0.9, 0.2, 1);
  box-shadow:
    0 0 0 2px rgba(255, 200, 100, 0.7),
    0 0 24px rgba(255, 180, 60, 0.4) !important;
}

.card.is-line-4-core {
  z-index: 17;
  animation-name: line-core-gold;
  animation-duration: 680ms;
  animation-timing-function: cubic-bezier(0.16, 1.25, 0.3, 1);
  box-shadow:
    0 0 0 3px rgba(255, 230, 150, 1),
    0 0 48px rgba(255, 200, 80, 0.85),
    0 0 80px rgba(255, 170, 40, 0.4) !important;
}

/*
 * Use fill-mode "backwards" (not "both"/"forwards"):
 * "forwards" freezes the last keyframe transform and blocks hover/select
 * scale (which relies on --card-scale) until the next full re-render.
 */
.card.is-spawn {
  animation: spawn-in 340ms cubic-bezier(0.2, 1.2, 0.3, 1) backwards;
}

.card.is-pop {
  animation: pop-in 360ms cubic-bezier(0.2, 1.35, 0.3, 1) backwards;
}

/* Ghost while dragging */
.drag-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  border-radius: var(--card-radius);
  border: none;
  overflow: hidden;
  box-shadow:
    0 18px 40px rgba(0, 0, 0, 0.55),
    0 0 24px rgba(122, 184, 240, 0.25);
  transform: translate(-50%, -50%) scale(1.12);
  opacity: 0.96;
  background-color: var(--card-color, #3a4a5c);
  will-change: left, top;
}

.drag-ghost img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
  display: block;
}

/* ---------- Animations ---------- */

@keyframes spawn-in {
  from {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.68);
    filter: brightness(1.4);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1);
  }
}

@keyframes pop-in {
  0% {
    opacity: 0.35;
    transform: translate(-50%, -50%) scale(0.55);
  }
  55% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.14);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

@keyframes merge-out {
  0% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.5);
    filter: brightness(1.5);
  }
}

@keyframes shake {
  0%, 100% { transform: translate(-50%, -50%) scale(var(--card-scale, 1)); }
  25% { transform: translate(calc(-50% - 5px), -50%) scale(var(--card-scale, 1)); }
  75% { transform: translate(calc(-50% + 5px), -50%) scale(var(--card-scale, 1)); }
}

@keyframes points-float {
  0% {
    opacity: 0;
    transform: translate(-50%, -30%) scale(0.55);
  }
  18% {
    opacity: 1;
    transform: translate(-50%, -55%) scale(1.2);
  }
  55% {
    opacity: 1;
    transform: translate(-50%, -95%) scale(1.05);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -140%) scale(0.95);
  }
}

@keyframes hint-halo {
  0%,
  100% {
    box-shadow:
      0 6px 16px rgba(0, 0, 0, 0.4),
      0 0 0 0 rgba(120, 200, 255, 0),
      0 0 0 0 rgba(120, 200, 255, 0);
    filter: brightness(1);
  }
  50% {
    box-shadow:
      0 8px 20px rgba(0, 0, 0, 0.4),
      0 0 0 3px rgba(130, 210, 255, 0.55),
      0 0 22px 4px rgba(100, 180, 255, 0.4);
    filter: brightness(1.08) saturate(1.08);
  }
}

/* Outer cards: compress toward center then settle */
@keyframes line-edge-pull {
  0% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1) saturate(1);
    opacity: 1;
  }
  40% {
    transform: translate(-50%, -50%) scale(0.88);
    filter: brightness(1.2) saturate(1.15);
  }
  70% {
    transform: translate(-50%, -50%) scale(1.06);
    filter: brightness(1.15) saturate(1.1);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1.05) saturate(1.05);
  }
}

/* Triple core: big satisfying pop */
@keyframes line-core-burst {
  0% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1) saturate(1);
  }
  25% {
    transform: translate(-50%, -50%) scale(0.82);
    filter: brightness(1.35) saturate(1.25);
  }
  55% {
    transform: translate(-50%, -50%) scale(1.28);
    filter: brightness(1.4) saturate(1.35);
  }
  78% {
    transform: translate(-50%, -50%) scale(0.96);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1.12) saturate(1.15);
  }
}

/* Quad edges: quick spark / dip */
@keyframes line-edge-spark {
  0% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1) saturate(1);
  }
  30% {
    transform: translate(-50%, -50%) scale(0.86);
    filter: brightness(1.25) saturate(1.2);
  }
  55% {
    transform: translate(-50%, -50%) scale(1.1);
    filter: brightness(1.3) saturate(1.25);
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    filter: brightness(1.08) saturate(1.1);
  }
}

/* Quad cores (the two middles): twin gold burst */
@keyframes line-core-gold {
  0% {
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
    filter: brightness(1) saturate(1);
  }
  20% {
    transform: translate(-50%, -50%) scale(0.78) rotate(-2deg);
    filter: brightness(1.4) saturate(1.35);
  }
  48% {
    transform: translate(-50%, -50%) scale(1.34) rotate(2deg);
    filter: brightness(1.5) saturate(1.4);
  }
  70% {
    transform: translate(-50%, -50%) scale(0.94) rotate(-1deg);
    filter: brightness(1.3) saturate(1.25);
  }
  100% {
    transform: translate(-50%, -50%) scale(1) rotate(0deg);
    filter: brightness(1.15) saturate(1.2);
  }
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes backdrop-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ---------- Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-bottom));
}

.modal[hidden] {
  display: none;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 6, 12, 0.78);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  animation: backdrop-in 240ms ease both;
}

.modal__panel {
  position: relative;
  width: min(100%, 360px);
  background: linear-gradient(165deg, rgba(36, 42, 58, 0.95), rgba(18, 22, 32, 0.96));
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius);
  padding: 26px 22px 20px;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.55),
    0 0 40px rgba(74, 154, 224, 0.08);
  animation: modal-in 300ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
  text-align: center;
}

.modal__panel--small {
  width: min(100%, 320px);
}

.modal__title {
  margin: 0 0 6px;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
  background: linear-gradient(120deg, #fff, #9ec9f5);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.modal__message {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.modal__scores {
  background: rgba(0, 0, 0, 0.28);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 18px;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.modal__score-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 2px;
  font-size: 0.95rem;
  color: var(--text-muted);
}

.modal__score-row strong {
  font-size: 1.2rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.modal__actions {
  display: flex;
  gap: 10px;
  justify-content: stretch;
}

.modal__actions .btn {
  flex: 1;
}

/* ---------- Promo (Game Over only) ---------- */

.promo {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.promo__text {
  margin: 0 0 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.promo__link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-min);
  padding: 0 12px;
  color: var(--accent);
  text-decoration: none;
  font-weight: 650;
  font-size: 0.95rem;
  border-radius: 999px;
  transition: background var(--transition-fast), color var(--transition-fast), box-shadow var(--transition-fast);
}

.promo__link:hover {
  background: var(--accent-soft);
  color: #b6dbfa;
  box-shadow: 0 0 20px rgba(122, 184, 240, 0.15);
}

/* ---------- Mobile / short viewport refinements ---------- */

/* Very short phones / landscape: compact chrome so board stays playable */
@media (max-height: 700px) {
  :root {
    --chrome-height: calc(140px + var(--safe-top) + var(--safe-bottom));
    --gap: 8px;
  }

  .app {
    padding-top: calc(8px + var(--safe-top));
    padding-bottom: calc(10px + var(--safe-bottom));
  }

  .header {
    margin-bottom: 6px;
  }

  .score-hud {
    margin-bottom: 6px;
    gap: 2px;
  }

  .score-hero__value {
    font-size: clamp(1.6rem, 6vw, 2.1rem);
  }

  .board-level {
    margin-top: 8px;
    gap: 5px;
  }

  .board-level__label,
  .board-level__value {
    font-size: clamp(1.05rem, 4vw, 1.35rem);
  }

  .hint {
    margin-top: 6px;
    font-size: 0.72rem;
  }

  .party__hint {
    display: none;
  }

  .party__shell {
    gap: 8px;
  }
}

/* Phones: fill the screen so the roster grid scrolls cleanly */
@media (max-width: 479px) {
  .party {
    align-items: stretch;
  }

  .party__shell {
    height: 100%;
    max-height: 100%;
    width: 100%;
  }

  .party__grid {
    /* Rows pack to card aspect-ratio — never stretch / overlap */
    align-content: start;
    align-items: start;
    gap: 10px;
  }
}

/* Narrow phones: slightly tighter party labels / pads */
@media (max-width: 380px) {
  .party__grid {
    gap: 8px;
    padding: 4px 2px 8px;
  }

  .party-card__check {
    top: 5px;
    right: 5px;
    width: 18px;
    height: 18px;
    font-size: 0.65rem;
  }

  .party-card__lock svg {
    width: 22px;
    height: 22px;
  }

  .score-hud__row {
    grid-template-columns: minmax(48px, 1fr) auto minmax(48px, 1fr);
    gap: 4px;
  }

  .power-token {
    min-width: 44px;
    min-height: 48px;
  }

  .power-token__svg {
    width: 36px;
    height: 36px;
  }
}

/* ---------- Tablet / desktop refinements ---------- */

@media (min-width: 480px) {
  :root {
    --gap: 12px;
    --hover-scale: 1.24;
    --select-scale: 1.22;
    --chrome-height: calc(200px + var(--safe-top) + var(--safe-bottom));
    --board-width: min(
      100%,
      520px,
      calc((100dvh - var(--chrome-height)) / 1.52)
    );
  }

  .app {
    padding-top: calc(20px + var(--safe-top));
  }

  .score-hud {
    margin-bottom: 16px;
    gap: 8px;
  }

  .power-token__svg {
    width: 48px;
    height: 48px;
  }

  .power-token__glow {
    width: 58px;
    height: 58px;
  }

  .board-shell {
    padding: 22px;
    margin: -22px auto;
  }

  .party__grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    /* Compact row spacing on PC — was stretched / too gappy */
    gap: 12px;
    padding: 6px 4px 10px;
    align-content: start;
  }

  .party__shell {
    gap: 12px;
    width: min(100%, 560px);
  }
}

@media (min-width: 768px) {
  :root {
    --gap: 14px;
    --hover-scale: 1.28;
    --select-scale: 1.24;
    --match-scale: 1.16;
    --chrome-height: calc(220px + var(--safe-top) + var(--safe-bottom));
    --board-width: min(
      100%,
      560px,
      calc((100dvh - var(--chrome-height)) / 1.52)
    );
  }

  .app {
    max-width: 720px;
  }
}

/* Coarse pointer (touch): no grab cursor, clearer press feedback */
@media (hover: none), (pointer: coarse) {
  .card {
    cursor: pointer;
  }

  .card:active:not(.is-dragging):not(.is-merging) {
    transform: translate(-50%, -50%) scale(calc(var(--card-scale) * 1.06));
  }

  /* Press feedback without scale (scale was covering the card below) */
  .party-card:active:not(:disabled):not(.party-card--locked) {
    filter: brightness(0.9);
  }

  .party-card--selected:active:not(:disabled) {
    filter: brightness(1);
  }

  .hint {
    /* On touch devices the long drag hint is less relevant */
    font-size: 0.78rem;
  }
}

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