:root {
  --bg: #f4f1ff;
  --panel: #ffffff;
  --cell-empty: #ece8fb;
  --accent: #8b7bff;
  --text: #4a4666;
  --muted: #9a96bb;
  --border: #e6e1f7;
  --border-soft: #f0ecfb;
  --shadow: 0 8px 24px rgba(120, 100, 220, 0.14);
  --cell: 44px;
  --tcell: 24px;
  --gap: 4px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: linear-gradient(160deg, #ffd6ef 0%, #ffe9c7 28%, #d6e4ff 60%, #c9f7e3 100%);
  background-attachment: fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  touch-action: manipulation;
  /* Stop dragging pieces from selecting text or popping the iOS copy/paste
     callout on touch devices. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Re-enable selection where it's actually wanted (e.g. the name field). */
input,
textarea {
  user-select: text;
  -webkit-user-select: text;
}

/* The game fills exactly one screen; the leaderboard sits below the fold. */
.screen {
  width: 100%;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0 0.75rem;
}

.game {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  position: relative;
}

.controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.hud {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0 0.3rem;
}

.hud h1 {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: 0.05em;
}

.stats {
  display: flex;
  gap: 1.25rem;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.1;
}

.stat .label {
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.stat strong {
  color: var(--accent);
  font-size: 1.3rem;
  font-variant-numeric: tabular-nums;
}

/* ---- Combo banner ------------------------------------------------------ */
.combo {
  height: 1.4rem;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--accent);
  opacity: 0;
  pointer-events: none;
}

.combo.show {
  animation: combo 1.3s cubic-bezier(0.22, 0.9, 0.27, 1);
}

@keyframes combo {
  0% { opacity: 0; transform: translateY(8px); }
  18% { opacity: 1; transform: translateY(0); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-6px); }
}

/* ---- Combo celebration popup ------------------------------------------ */
/* Outer is positioned by JS to sit centered over the board. */
.combo-pop {
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  text-align: center;
  pointer-events: none;
  z-index: 1600;
}

.combo-inner {
  opacity: 0;
  position: relative;
}

/* Casino "spotlight" glow blooming behind the text. */
.combo-inner::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 150%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle, rgba(255, 214, 92, 0.55) 0%, rgba(255, 122, 214, 0.28) 38%, rgba(255, 255, 255, 0) 70%);
  z-index: -1;
  pointer-events: none;
}

.combo-pop.show .combo-inner {
  animation: comboPop 2.3s cubic-bezier(0.18, 0.9, 0.25, 1);
}

@keyframes comboPop {
  0% { opacity: 0; transform: scale(0.5) rotate(-6deg) translateY(14px); }
  8% { opacity: 1; transform: scale(1.25) rotate(2deg) translateY(0); }
  14% { transform: scale(0.96) rotate(-2deg); }
  20% { transform: scale(1.08) rotate(1deg); }
  26% { transform: scale(1) rotate(0); }
  85% { opacity: 1; transform: scale(1) translateY(-4px); }
  100% { opacity: 0; transform: scale(1.04) translateY(-18px); }
}

/* Shiny gold "jackpot" lettering with a metallic gradient + glow. */
.combo-pop .big {
  font-size: clamp(1.9rem, 9vw, 3rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: linear-gradient(180deg, #fff6c2 0%, #ffd84d 38%, #ffae21 60%, #ff7ad6 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-stroke: 1px rgba(120, 60, 10, 0.35);
  filter: drop-shadow(0 2px 0 rgba(155, 90, 0, 0.45)) drop-shadow(0 0 16px rgba(255, 196, 70, 0.85));
}

.combo-pop.show .big {
  animation: jackpotFlash 0.42s steps(1) 0s 4;
}

@keyframes jackpotFlash {
  0%, 100% { filter: drop-shadow(0 2px 0 rgba(155, 90, 0, 0.45)) drop-shadow(0 0 16px rgba(255, 196, 70, 0.85)); }
  50% { filter: drop-shadow(0 2px 0 rgba(155, 90, 0, 0.45)) drop-shadow(0 0 26px rgba(255, 255, 255, 0.95)); }
}

.combo-pop .sub {
  margin-top: 0.2rem;
  font-size: clamp(0.95rem, 4.4vw, 1.2rem);
  font-weight: 800;
  color: #c0392b;
  text-shadow: 0 1px 0 #fff;
}

/* Big combos (≥5): hotter, faster, pink-gold casino energy. */
.combo-pop.jackpot .combo-inner::before {
  background: radial-gradient(circle, rgba(255, 230, 120, 0.7) 0%, rgba(255, 80, 200, 0.4) 40%, rgba(255, 255, 255, 0) 72%);
  animation: jackpotSpin 0.9s linear infinite;
}

@keyframes jackpotSpin {
  0% { transform: translate(-50%, -50%) rotate(0) scale(1); }
  50% { transform: translate(-50%, -50%) rotate(180deg) scale(1.12); }
  100% { transform: translate(-50%, -50%) rotate(360deg) scale(1); }
}

/* ---- Board ------------------------------------------------------------- */
.board {
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  gap: var(--gap);
  padding: calc(var(--gap) * 1.5);
  background: var(--panel);
  border-radius: 16px;
  box-shadow: var(--shadow);
  touch-action: none;
}

/* New level: the resized board eases into place rather than snapping. */
.board.enter {
  animation: boardEnter 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes boardEnter {
  from { opacity: 0.3; transform: scale(0.88); }
  to { opacity: 1; transform: scale(1); }
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border-radius: 6px;
  background: var(--cell-empty);
  transition: background 0.2s ease, width 0.3s ease, height 0.3s ease;
}

.cell.placed {
  animation: placePop 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes placePop {
  0% { transform: scale(0.7); }
  55% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

.cell.filled {
  border-radius: 7px;
}

/* Glossy, beveled 3D look shared by board cells and tray pieces. Each element
   supplies its base color via the --c custom property. */
.cell.filled,
.blk:not(.empty) {
  background-image: linear-gradient(
    155deg,
    color-mix(in srgb, var(--c, #888) 82%, #fff),
    var(--c, #888) 46%,
    color-mix(in srgb, var(--c, #888) 70%, #000)
  );
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.32),
    inset 3px 0 0 rgba(255, 255, 255, 0.13),
    inset 0 -4px 0 rgba(0, 0, 0, 0.30),
    inset -3px 0 0 rgba(0, 0, 0, 0.16),
    0 1px 2px rgba(0, 0, 0, 0.30);
}

/* Where the piece will land: a bright "ghost" in the piece's own colour with
   a crisp white ring + glow so it stands out clearly against the board. */
.cell.preview-valid {
  background: var(--pc, var(--accent));
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.95),
    0 0 12px 2px color-mix(in srgb, var(--pc, var(--accent)) 70%, transparent);
  transform: scale(1.04);
  z-index: 1;
}

.cell.preview-invalid {
  background: rgba(239, 68, 68, 0.55);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.85);
}

/* Whole rows/columns that WILL clear if the player drops the piece here —
   a bright, pulsing gold so it's obvious what's about to pop. */
.cell.clear-preview {
  background: #ffd84d;
  box-shadow: 0 0 10px 2px rgba(255, 196, 70, 0.85), inset 0 0 0 2px #fff3bf;
  animation: clearPulse 0.6s ease-in-out infinite;
  z-index: 2;
}

@keyframes clearPulse {
  0%, 100% { filter: brightness(1); transform: scale(1); }
  50% { filter: brightness(1.25); transform: scale(1.08); }
}

.cell.cleared {
  animation: flash 0.45s ease;
}

@keyframes flash {
  0% { filter: brightness(2.2); transform: scale(1.04); }
  100% { filter: brightness(1); transform: scale(1); }
}

/* ---- Tray -------------------------------------------------------------- */
.tray {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 100%;
  min-height: calc(var(--tcell) * 4);
  gap: 0.5rem;
  touch-action: none;
}

.slot {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(var(--tcell) * 4);
  touch-action: none;
}

.slot.used {
  opacity: 0.25;
}

.piece {
  display: grid;
  gap: var(--gap);
  touch-action: none;
  cursor: grab;
}

.piece.dragging {
  opacity: 0.3;
}

.blk {
  width: var(--tcell);
  height: var(--tcell);
  border-radius: 5px;
  touch-action: none;
}

.blk.empty {
  background: transparent !important;
  box-shadow: none;
}

/* ---- Drag ghost -------------------------------------------------------- */
/* A picked-up piece reads as a solid object lifted off the board: bigger,
   casting a soft shadow, and tilting toward the direction of motion. Position
   (left/top) follows the finger instantly; only the tilt/scale transform is
   eased, which gives the springy, physical feel. */
.ghost {
  position: fixed;
  z-index: 1000;
  display: grid;
  gap: var(--gap);
  pointer-events: none;
  transform-origin: center center;
  transition: transform 130ms cubic-bezier(0.34, 1.56, 0.64, 1);
  filter: drop-shadow(0 12px 16px rgba(70, 50, 130, 0.32));
  will-change: left, top, transform;
}

.ghost.no-motion {
  transition: none;
  filter: drop-shadow(0 6px 8px rgba(70, 50, 130, 0.28));
}

.ghost .blk {
  width: var(--cell);
  height: var(--cell);
}

/* ---- Buttons ----------------------------------------------------------- */
.btn {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.55rem 1.1rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(120, 100, 220, 0.1);
  transition: transform 0.1s ease, box-shadow 0.12s ease;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
  transform: none;
}

.btn-primary {
  background: linear-gradient(160deg, #a99bff, var(--accent));
  color: #fff;
  border-color: transparent;
  box-shadow: 0 6px 16px rgba(139, 123, 255, 0.4);
}

.btn-secondary {
  background: transparent;
  color: var(--muted);
  margin-top: 0.5rem;
}

.overlay-card > .btn {
  display: block;
  width: 100%;
}

.hint {
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
  margin: 0;
}

/* ---- Confetti layer ---------------------------------------------------- */
.fx {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1500;
}

/* ---- Game over overlay ------------------------------------------------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(74, 70, 102, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 2000;
  animation: overlayIn 0.4s ease;
}

@keyframes overlayIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay.hidden {
  display: none !important;
}

.overlay-card {
  background: var(--panel);
  border-radius: 20px;
  padding: 1.6rem 1.75rem;
  text-align: center;
  width: 100%;
  max-width: 340px;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: 0 16px 50px rgba(90, 70, 180, 0.28);
  animation: cardIn 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(14px) scale(0.96); }
  to { opacity: 1; transform: none; }
}

.overlay-card h2 {
  margin: 0 0 0.5rem;
}

.final {
  color: var(--muted);
  margin: 0.25rem 0 1rem;
}

.final strong {
  color: var(--accent);
  font-size: 1.6rem;
  margin-left: 0.4rem;
}

/* The player's own personal best — the headline stat now the leaderboard
   is gone. Big, gold, and impossible to miss. */
.best-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  margin: 0.25rem auto 1rem;
  padding: 0.7rem 1.4rem;
  border-radius: 16px;
  background: linear-gradient(160deg, #fff6d6, #ffe9a8);
  box-shadow: inset 0 0 0 2px #ffd84d, 0 6px 16px rgba(255, 190, 60, 0.35);
}

.best-box .best-label {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #b07900;
}

.best-box strong {
  font-size: 2.1rem;
  font-weight: 900;
  line-height: 1;
  color: #d68a00;
  font-variant-numeric: tabular-nums;
}

.newbest {
  color: #e8920a;
  font-weight: 800;
  font-size: 1.1rem;
  margin: 0 0 1rem;
}

.newbest:not(.hidden) {
  animation: newbestPop 0.6s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}

@keyframes newbestPop {
  0% { opacity: 0; transform: scale(0.5); }
  60% { transform: scale(1.15); }
  100% { opacity: 1; transform: scale(1); }
}

.hidden {
  display: none !important;
}

/* ---- Title screen ------------------------------------------------------ */
.title {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(165deg, #ffe7f6 0%, #eaf0ff 50%, #e4fbf0 100%);
  z-index: 2500;
}

.title-card {
  text-align: center;
  padding: 2rem;
}

.title-logo {
  margin: 0;
  font-size: clamp(2.6rem, 14vw, 4.5rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 0.95;
  color: var(--text);
}

.title-logo span {
  display: block;
  color: var(--accent);
  text-shadow: 0 4px 30px rgba(139, 123, 255, 0.6);
}

.title-sub {
  color: var(--muted);
  margin: 0.75rem 0 1.5rem;
}

.title-status {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 1rem 0 0;
  min-height: 1rem;
}

.btn.big {
  font-size: 1.2rem;
  padding: 0.7rem 2.5rem;
}

/* ---- Settings ---------------------------------------------------------- */
#settings, #progress {
  z-index: 3000; /* above the title (2500) screen */
}

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 0.2rem;
  border-bottom: 1px solid var(--border-soft);
  font-weight: 600;
}

.setting input {
  width: 1.4rem;
  height: 1.4rem;
  accent-color: var(--accent);
}

/* ---- HUD icon button --------------------------------------------------- */
.icon-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  width: 2.1rem;
  height: 2.1rem;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(120, 100, 220, 0.1);
}

.hud h1 {
  flex: 1;
  margin-left: 0.5rem;
}

/* coachBob is reused by the "Update app" pill below. */
@keyframes coachBob {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(6px); }
}

/* ---- "Update app" pill ------------------------------------------------- */
.update-pill {
  position: fixed;
  left: 50%;
  bottom: 1.1rem;
  transform: translateX(-50%);
  z-index: 3500;
  background: linear-gradient(160deg, #a99bff, var(--accent));
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 0.6rem 1.3rem;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(139, 123, 255, 0.5);
  animation: updateIn 0.35s ease, coachBob 1.6s ease-in-out infinite 0.35s;
}

@keyframes updateIn {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ---- Live percentile pill --------------------------------------------- */
.percentile {
  font-weight: 800;
  font-size: 0.95rem;
  color: #b07900;
  background: linear-gradient(160deg, #fff6d6, #ffe9a8);
  border-radius: 999px;
  padding: 0.2rem 0.8rem;
  box-shadow: inset 0 0 0 1.5px #ffd84d;
}

/* ---- Progress screen --------------------------------------------------- */
.prog-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  margin: 0.5rem 0 1rem;
}

.prog-cell {
  background: var(--cell-empty);
  border-radius: 14px;
  padding: 0.7rem 0.4rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
}

.prog-cell strong {
  font-size: 1.5rem;
  font-weight: 900;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}

.prog-cell span {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
