:root {
  --bg: #12142B;
  --surface: #1B1F44;
  --surface-2: #232853;
  --cell-empty: #262B58;
  --cell-border: #33396b;
  --text: #F4F2FF;
  --muted: #8D91C4;

  --amber: #FFB454;
  --coral: #FF6B7A;
  --teal: #4ED9C4;
  --violet: #A78BFA;
  --sky: #5FB8FF;
  --lime: #C6E36B;

  --font-display: "Space Grotesk", ui-sans-serif, system-ui, sans-serif;
  --font-body: "Inter", ui-sans-serif, system-ui, sans-serif;

  --grid-size: 8;
  --board-max: 480px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  background: radial-gradient(120% 100% at 50% -10%, #1a1e47 0%, var(--bg) 55%);
  color: var(--text);
  font-family: var(--font-body);
  display: flex;
  justify-content: center;
  min-height: 100vh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  user-select: none;
}

#app {
  width: 100%;
  max-width: var(--board-max);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 14px 16px 20px;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

/* Pushes the icon buttons to the right edge as a pair. */
.brand { margin-right: auto; }

.brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 22px;
  letter-spacing: 0.02em;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-mark { display: inline-flex; }

.icon-btn {
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.06);
  color: var(--muted);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: color 0.15s, transform 0.1s;
}
.icon-btn:active { transform: scale(0.92); color: var(--text); }

/* The speaker icon carries both states in one SVG: unmuted shows the two sound
   waves, muted swaps them for the slash. */
.sound-btn .mute-slash { opacity: 0; }
.sound-btn.is-muted .wave-outer,
.sound-btn.is-muted .wave-inner { opacity: 0; }
.sound-btn.is-muted .mute-slash { opacity: 1; }
.sound-btn svg path { transition: opacity 0.15s; }

/* ---------- Scoreboard ---------- */

.scoreboard {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}

.score-block {
  flex: 1;
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 16px;
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
  overflow: hidden;
}

.score-block--ghost { opacity: 0.7; }

.score-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  font-weight: 600;
}

.score-value {
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.score-value.bump { animation: bump 0.28s ease; }
@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.18); color: var(--amber); }
  100% { transform: scale(1); }
}

/* ---------- Board ---------- */

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  margin: 0 auto;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), 1fr);
  grid-template-rows: repeat(var(--grid-size), 1fr);
  gap: 3%;
  background: var(--surface);
  border-radius: 18px;
  padding: 3%;
  border: 1px solid rgba(255,255,255,0.06);
}

.cell {
  position: relative;
  border-radius: 6px;
  background: var(--cell-empty);
  /* No background/box-shadow transition. Neither can be composited, so every
     preview toggle during a drag scheduled 0.12s of repainting across up to 64
     cells. An instant preview also feels more responsive in a game. */
  transition: transform 0.12s;
}

.cell.filled {
  box-shadow: inset 0 -3px 0 rgba(0,0,0,0.22), inset 0 2px 0 rgba(255,255,255,0.25);
}

.cell.preview-ok {
  background: color-mix(in srgb, var(--preview-color, var(--teal)) 55%, var(--cell-empty));
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--preview-color, var(--teal)) 70%, transparent) inset;
}

.cell.preview-bad {
  background: color-mix(in srgb, var(--coral) 30%, var(--cell-empty));
}

/* Cells carry their stagger in --anim-delay, set per cell from JS. */

.cell.clearing {
  animation: clearPulse 0.32s ease var(--anim-delay, 0ms) forwards;
  z-index: 2;
}

/* transform + opacity only — both run on the compositor. The previous version
   animated filter: brightness(), which forces a repaint per frame on every
   animating cell, up to 64 at once during a big clear. */
@keyframes clearPulse {
  0% { transform: scale(1); opacity: 1; }
  60% { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(0.2); opacity: 0; }
}

/* The brightness flash, recreated with a composited opacity fade on an overlay
   instead of a filter on the cell itself. */
.cell.clearing::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: #fff;
  animation: clearFlash 0.32s ease var(--anim-delay, 0ms) backwards;
}

@keyframes clearFlash {
  0% { opacity: 0.45; }
  60% { opacity: 0.75; }
  100% { opacity: 0; }
}

.cell.just-placed {
  animation: placePop 0.26s cubic-bezier(.34,1.56,.64,1) var(--anim-delay, 0ms) backwards;
  z-index: 1;
}

/* `backwards` holds the 0% frame during the delay, so staggered cells don't
   flash at full size before their turn. */
@keyframes placePop {
  0% { transform: scale(0.55); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.board.shake { animation: boardShake 0.34s cubic-bezier(.36,.07,.19,.97); }

@keyframes boardShake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}

.combo-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

/* Absolutely positioned rather than flex-centered: the score popup is a
   sibling that needs its own coordinates within this layer. */
.combo-text {
  position: absolute;
  left: 50%;
  top: 50%;
  white-space: nowrap;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 30px;
  color: var(--amber);
  text-shadow: 0 2px 12px rgba(255,180,84,0.6);
  animation: comboPop 0.7s cubic-bezier(.2,1.4,.4,1) forwards;
}

@keyframes comboPop {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(-4deg) scale(0.5); }
  25% { opacity: 1; transform: translate(-50%, -50%) rotate(-4deg) scale(1.08); }
  75% { opacity: 1; transform: translate(-50%, -50%) rotate(-4deg) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -140%) rotate(-4deg) scale(1); }
}

.score-pop {
  position: absolute;
  white-space: nowrap;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 22px;
  color: var(--teal);
  /* Tight dark halo first, then the glow — the popup starts life on top of
     brightly lit clearing cells and needs to read against them. */
  text-shadow:
    0 0 4px rgba(10,11,26,0.95),
    0 0 8px rgba(10,11,26,0.8),
    0 2px 10px rgba(78,217,196,0.55);
  animation: scoreFloat 0.9s cubic-bezier(.22,.9,.3,1) forwards;
}

/* Rises clear of the line it came from rather than sitting on it. Offsets are
   percentages of the popup's own height, so they scale with the font. */
@keyframes scoreFloat {
  0% { opacity: 0; transform: translate(-50%, -70%) scale(0.7); }
  20% { opacity: 1; transform: translate(-50%, -115%) scale(1.1); }
  55% { opacity: 1; transform: translate(-50%, -145%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -220%) scale(0.95); }
}

/* ---------- Tray ---------- */

.tray {
  margin-top: 18px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  min-height: 116px;
}

.tray-slot {
  background: var(--surface);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  touch-action: none;
}

/* The whole slot is the grab handle, so it carries the cursor. */
.tray-slot--filled { cursor: grab; }
.tray-slot--filled:active { cursor: grabbing; }

.tray-piece {
  display: grid;
  gap: 4px;
  transition: opacity 0.15s;
}

.tray-piece.dragging-source { opacity: 0.25; }

.tray-piece.tray-enter {
  animation: trayEnter 0.34s cubic-bezier(.34,1.4,.64,1) var(--anim-delay, 0ms) backwards;
}

@keyframes trayEnter {
  0% { opacity: 0; transform: translateY(14px) scale(0.7); }
  60% { opacity: 1; transform: translateY(0) scale(1.06); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

.piece-cell {
  border-radius: 5px;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,0.25), inset 0 2px 0 rgba(255,255,255,0.28);
}

.piece-cell.empty { visibility: hidden; }

/* floating drag ghost */

.drag-ghost {
  position: fixed;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 50;
  display: grid;
  gap: 4px;
  opacity: 0.95;
  /* Promoted to its own compositor layer so moving it is a GPU transform
     rather than a repaint. Safari is far less willing than Chrome to promote
     this on its own. */
  will-change: transform;
}

/* Was filter: drop-shadow() on the container — that re-renders the filter as
   the element moves. A per-cell box-shadow is composited instead and costs
   nothing per frame. */
.drag-ghost .piece-cell {
  border-radius: 6px;
  box-shadow:
    inset 0 -2px 0 rgba(0,0,0,0.25),
    inset 0 2px 0 rgba(255,255,255,0.28),
    0 6px 14px rgba(0,0,0,0.45);
}

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

.modal {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 26, 0.72);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal.hidden { display: none; }

.modal-card {
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 22px;
  padding: 32px 28px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  animation: cardIn 0.25s ease;
}

@keyframes cardIn {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-card h2 {
  font-family: var(--font-display);
  margin: 0 0 4px;
  font-size: 22px;
}

.modal-sub {
  color: var(--muted);
  margin: 12px 0 2px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.modal-score {
  font-family: var(--font-display);
  font-size: 44px;
  font-weight: 700;
  margin: 0;
  color: var(--amber);
}

.new-best {
  color: var(--teal);
  font-weight: 600;
  font-size: 13px;
  margin: 6px 0 0;
}

.new-best.hidden { display: none; }

.primary-btn {
  margin-top: 22px;
  background: linear-gradient(160deg, var(--amber), #e89a3a);
  color: #201200;
  border: none;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 16px;
  padding: 13px 28px;
  border-radius: 14px;
  cursor: pointer;
  width: 100%;
}
.primary-btn:active { transform: scale(0.98); }

/* ---------- Install toast ---------- */

.install-toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  background: var(--surface-2);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 14px;
  padding: 12px 12px 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.35);
  z-index: 80;
}

.install-toast.hidden { display: none; }
.install-toast span { flex: 1; color: var(--text); }

#installBtn {
  background: var(--teal);
  color: #052922;
  border: none;
  font-weight: 700;
  padding: 8px 14px;
  border-radius: 10px;
  cursor: pointer;
}

.ghost-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  padding: 4px;
}

@media (max-width: 360px) {
  .score-value { font-size: 24px; }
  .brand { font-size: 19px; }
}

/* Motion is decoration here, never the mechanism: every state change is driven
   by JS timers and class toggles, so collapsing these to near-zero duration
   leaves the game fully playable. Durations are kept non-zero so animationend
   still fires and elements aren't left mid-transform. */
@media (prefers-reduced-motion: reduce) {
  .cell.clearing,
  .cell.clearing::after,
  .cell.just-placed,
  .tray-piece.tray-enter,
  .board.shake,
  .score-value.bump,
  .modal-card {
    animation-duration: 0.01ms !important;
    animation-delay: 0ms !important;
  }
  /* Centering lives in these elements' keyframes, so it has to be restated
     here or they jump off-position once the animation is swapped out. */
  .combo-text, .score-pop {
    transform: translate(-50%, -50%);
    animation: fadeOnly 0.7s ease forwards;
  }
  .cell { transition: none; }
  @keyframes fadeOnly {
    0%, 70% { opacity: 1; }
    100% { opacity: 0; }
  }
}
