:root {
  color-scheme: light dark;
  --bg: #0e172a;
  --bg-soft: #111827;
  --bg-softer: #020617;

  /* Wood tokens (Phase A). Used by tile/board/tray in Phase B+. */
  --wood-highlight: #ebd5b8;
  --wood-face: #b8895e;
  --wood-shadow: #6f4e37;
  --wood-edge: rgb(62 44 32 / 0.55);
  --wood-grain: rgb(62 44 32 / 0.08);
  --ink: #251810;

  /* Single interactive accent (honey — pairs with wood). --accent aliases for existing rules. */
  --accent-rgb: 220 165 70;
  --accent-warm: rgb(var(--accent-rgb));
  --accent: var(--accent-warm);
  --accent-soft: rgb(var(--accent-rgb) / 0.15);

  --danger: #f97373;
  --text: #e5e7eb;
  --muted: #9ca3af;
  --tile-size: 64px;
  /* Floor for board cell: width uses gridWidth + horizontal margin cells (2 desktop, tighter on narrow — see game.js). */
  --board-cell-min: 40px;
  /* Rounded-square silhouette: 28.125% of side (18/64). `.board` sets --tile-size for playfield; `.tray-wrapper` sets it for the tray rail. */
  --tile-corner-radius: calc(var(--tile-size) * 0.28125);
  /* Icon size: same on board and tray when tile size matches */
  --tile-icon-size: 2.4rem;

  /* Phase B: wooden tile face (shared by .tile face, .tray-tile) */
  --tile-bg-plank: linear-gradient(
    132deg,
    var(--wood-highlight) 0%,
    var(--wood-face) 46%,
    var(--wood-shadow) 100%
  );
  --tile-bg-grain: repeating-linear-gradient(
    95deg,
    rgb(0 0 0 / 0) 0 6px,
    var(--wood-grain) 6px 6.5px
  );
  /* Seamless neutral grain (assets/wood-grain-noise.webp); regenerate: npm run generate:grain */
  --tile-bg-noise: url('assets/wood-grain-noise.webp');
  --tile-noise-tile-size: 64px;
  --tile-inset-light: inset 1px 1px 3px rgb(255 255 255 / 0.38);
  --tile-inset-dark: inset -2px -3px 6px rgb(0 0 0 / 0.28);
  --tile-drop-board: 0 10px 20px rgb(22 14 10 / 0.72);
  --tile-drop-tray: 0 8px 18px rgb(22 14 10 / 0.68);

  /* Phase C: board mat + tray trough (warm, desaturated — family of --wood-shadow) */
  /* Felt mid-tones lifted vs --chrome-bg-page; top ellipse slightly cooler for separation without a new hue. */
  --board-felt: radial-gradient(
    ellipse 100% 88% at 50% -8%,
    rgb(72 62 58) 0%,
    rgb(46 40 36) 52%,
    rgb(30 26 23) 100%
  );
  --board-felt-inset:
    inset 0 1px 0 rgb(255 255 255 / 0.11),
    inset 0 -6px 18px rgb(0 0 0 / 0.38);
  --board-guide: rgb(235 213 184 / 0.24);
  /* Dashed inner guide inset from the mat’s outer edge (wood ring); same on all sides */
  --board-guide-inset: 5px;
  /* Soft outer ring so the playfield reads as a mat against the page gradient */
  --board-mat-ambient: 0 0 44px 14px rgb(0 0 0 / 0.28);

  /* Phase D: UI chrome (page shell, header, dialogs) — warm brown glass, wood-adjacent */
  --chrome-bg-page: radial-gradient(
    circle at top,
    rgb(52 44 38) 0%,
    rgb(22 18 15) 45%,
    rgb(5 4 3) 100%
  );
  --chrome-bar-bg: linear-gradient(to right, rgb(36 30 26 / 0.96), rgb(24 20 17 / 0.94));
  --chrome-bar-border: rgb(235 213 184 / 0.12);
  --chrome-scrim: rgb(8 6 5 / 0.88);
  --chrome-panel-bg: linear-gradient(
    to bottom right,
    rgb(42 36 30 / 0.98),
    rgb(26 22 18 / 0.98)
  );
  --chrome-panel-edge: var(--wood-edge);
  --chrome-panel-inset: inset 0 1px 0 rgb(255 255 255 / 0.07);
  --chrome-panel-shadow: 0 24px 60px rgb(6 4 3 / 0.86);
  --chrome-line: rgb(235 213 184 / 0.16);
  --chrome-line-soft: rgb(235 213 184 / 0.22);
  --chrome-ghost-hover: rgb(32 28 24 / 0.88);
  --btn-ring: 0 0 0 1px rgb(48 38 30 / 0.65);
  --btn-drop: 0 10px 18px rgb(12 8 6 / 0.6);
  --btn-drop-hover: 0 14px 28px rgb(10 7 5 / 0.72);
  --btn-drop-active: 0 8px 14px rgb(12 8 6 / 0.55);
}

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

html {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  /* Column flex + #app flex:1 gives Chrome a definite height for nested flex (main → board-scroll). */
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--chrome-bg-page);
  color: var(--text);
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  width: 100%;
  /* Avoid WebKit tap flash on game surface; tile faces are decorative, not text to copy */
  -webkit-tap-highlight-color: transparent;
}

.openmoji-credit {
  margin: 0;
  padding: 0.5rem 1rem 1rem;
  text-align: center;
  font-size: 0.68rem;
  color: var(--muted);
}

.openmoji-credit a {
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.openmoji-credit a:hover {
  color: var(--text);
}

.top-bar {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
  min-width: 0;
  padding: calc(0.5rem + env(safe-area-inset-top, 0px)) calc(1rem + env(safe-area-inset-right, 0px)) 0.5rem
    calc(1rem + env(safe-area-inset-left, 0px));
  background: var(--chrome-bar-bg);
  border-bottom: 1px solid var(--chrome-bar-border);
  box-shadow: 0 1px 0 rgb(0 0 0 / 0.2);
  backdrop-filter: blur(16px);
  position: sticky;
  top: 0;
  z-index: 10;
}

.top-bar-left {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-width: 0;
  flex: 1;
}

.top-bar-right {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  flex-shrink: 1;
}

.header-fullscreen-wrap {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.display-mode-toolbar {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.display-mode-btn {
  flex-shrink: 0;
}

.audio-master-strip {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
  min-height: 1.75rem;
}

.audio-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.2rem;
  max-width: min(58vw, 16rem);
}

.audio-row {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: nowrap;
  justify-content: flex-end;
}

.audio-music-toggle,
.audio-sfx-toggle,
.audio-haptics-toggle,
.audio-master-toggle {
  flex-shrink: 0;
  font-size: 0.75rem;
  padding: 0.25rem 0.55rem;
}

.audio-master-row {
  min-height: 1.75rem;
}

.audio-row-haptics {
  /* Icon-only row: keep label sr-only like volume rows. */
  min-height: 1.75rem;
}

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

.audio-volume {
  width: min(4.5rem, 22vw);
  min-width: 0;
  accent-color: var(--accent);
  vertical-align: middle;
}

.audio-volume:disabled {
  opacity: 0.45;
}

.locale-select {
  flex-shrink: 0;
  font-size: 0.75rem;
  padding: 0.25rem 0.4rem;
  max-width: min(28vw, 7.5rem);
  min-width: 0;
  border-radius: 0.35rem;
  border: 1px solid rgb(255 255 255 / 0.12);
  background: rgb(0 0 0 / 0.2);
  color: var(--text);
  cursor: pointer;
}

.settings-locale-select {
  display: block;
  width: 100%;
  max-width: 100%;
  margin-top: 0.35rem;
  box-sizing: border-box;
  padding: 0.4rem 0.5rem;
  font-size: 0.85rem;
}

.locale-select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/*
 * RTL: rely on dir="rtl" on <html> (set from lib/i18n.js). Flex rows already
 * flow main-start → main-end on the right, so do NOT use flex-direction:
 * row-reverse here — it would cancel mirroring and keep the bar LTR.
 */

.game-title {
  font-size: 1.2rem;
  margin: 0;
  flex-shrink: 0;
}

.level-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.9rem;
  min-width: 0;
}

#level-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: min(42vw, 16rem);
}

.score-info {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.1rem;
}

.score-info span:first-child {
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.06em;
}

.score-info span:last-child {
  font-weight: 600;
}

.powerups {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  align-self: center;
  width: 100%;
  max-width: min(448px, 100%);
  box-sizing: border-box;
  padding-inline: max(0.75rem, env(safe-area-inset-left, 0px)) max(0.75rem, env(safe-area-inset-right, 0px));
  flex-shrink: 0;
}

.btn {
  border: none;
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  cursor: pointer;
  background: var(--accent);
  color: var(--ink);
  font-weight: 600;
  box-shadow:
    var(--btn-ring),
    var(--btn-drop),
    inset 0 1px 0 rgb(255 255 255 / 0.35);
  transition: transform 0.1s ease, box-shadow 0.1s ease, background 0.1s ease, opacity 0.1s ease;
}

.btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow:
    var(--btn-ring),
    var(--btn-drop-hover),
    inset 0 1px 0 rgb(255 255 255 / 0.42);
}

.btn:active:not(:disabled) {
  transform: translateY(0);
  box-shadow:
    var(--btn-ring),
    var(--btn-drop-active),
    inset 0 1px 0 rgb(255 255 255 / 0.25);
}

.btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  box-shadow: none;
  border: 1px solid var(--chrome-line);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--chrome-ghost-hover);
  border-color: var(--chrome-line-soft);
}

.btn-small {
  padding: 0.2rem 0.7rem;
  font-size: 0.75rem;
}

/* Phosphor (Regular): icon-only and icon + label */
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.5rem;
  min-height: 2.5rem;
  padding: 0.35rem;
  line-height: 1;
}

.btn-icon.btn-small {
  min-width: 2.25rem;
  min-height: 2.25rem;
  padding: 0.25rem;
}

.btn-icon .ph {
  font-size: 1.25rem;
  color: inherit;
}

.btn-icon.btn-small .ph {
  font-size: 1.1rem;
}

.btn-with-icon {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.btn-with-icon .ph {
  font-size: 1.05rem;
  flex-shrink: 0;
  color: inherit;
}

.powerup-count {
  font-variant-numeric: tabular-nums;
}

.main-layout {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Top-align chrome; #board-scroll flex:1 owns the middle — avoids empty bands above power-ups */
  justify-content: flex-start;
  /* Horizontal inset only on power-ups / tray; #board-scroll is full-bleed for the playfield */
  padding: 0.65rem 0;
  gap: 0.85rem;
  min-height: 0;
}

/*
 * Scrollport for the squared playfield. `.board` may grow past this area when
 * gridSize is large and --board-cell-min forces bigger cells; users pan with overflow auto.
 */
/*
 * Do not justify-content:center this scrollport horizontally: browsers under-report scrollWidth /
 * max scrollLeft, so a wider-than-viewport board can leave the first column unreachable.
 * Horizontal placement stays on .board-scroll-align (width max-content + margin-inline auto).
 * Column flex + margin-top:auto on that wrapper bottom-aligns the board when the scrollport is taller
 * than the playfield (min-height:100% on the inner box is unreliable inside overflow:auto).
 */
.board-scroll {
  flex: 1 1 0;
  /* Keep a floor when the header gains rows (e.g. full-screen + audio on narrow phones). */
  min-height: clamp(100px, 26dvh, 280px);
  width: 100%;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
  /* Edge-to-edge with #app; header/tray keep their own safe-area padding */
  /* Lets .board use cqw/cqh for no-JS / pre-hydration cap that matches the real scrollport */
  container-type: size;
  container-name: board-scroll;
}

.board-scroll-align {
  box-sizing: border-box;
  width: max-content;
  max-width: 100%;
  margin-inline: auto;
  margin-top: auto;
  margin-bottom: 0;
  flex-shrink: 0;
  align-self: center;
  /* Vertical room for mat drop-shadow; horizontal flush so #board spans the scrollport */
  padding: 12px 0;
  /* Clicks / focus on padding hit #board-scroll underneath; #board re-enables interaction */
  pointer-events: none;
}

.board {
  /* Board tile geometry; JS updates --tile-size from layout cellSize. */
  --tile-size: 64px;
  --tile-corner-radius: calc(var(--tile-size) * 0.28125);
  --tile-noise-tile-size: var(--tile-size);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  /* Restore hit-testing for tiles and board focus (parent .board-scroll-align is pointer-events: none) */
  pointer-events: auto;
  /* Intrinsic size is set in JS (fit cap × cell floor); fallback for no-JS / first paint. */
  box-sizing: border-box;
  /* Tighter chrome budget than 16.75rem; scrollport-sized cap when container queries run.
     Runtime sets width/height from gridWidth × gridHeight; fallback is square until hydration. */
  /* No 448 cap here: narrow layouts use full width (JS); ≥768px overrides below */
  width: min(100%, calc(100dvh - 14.25rem));
  height: min(448px, 100%, calc(100dvh - 14.25rem));
  max-width: none;
  flex-shrink: 0;
  background: var(--board-felt);
  border-radius: 22px;
  box-shadow:
    0 20px 52px rgb(12 8 6 / 0.78),
    0 0 0 1px var(--wood-edge),
    0 0 0 3px rgb(235 213 184 / 0.14),
    var(--board-felt-inset),
    var(--board-mat-ambient);
  /* Clip tiles and shadows to the rounded playfield; positioning uses insetY so content fits. */
  overflow: hidden;
}

@supports (width: 1cqw) {
  .board {
    width: min(100cqw, calc(100cqh - 24px));
    height: min(448px, calc(100cqh - 24px));
  }
}

.board::before {
  content: '';
  position: absolute;
  inset: var(--board-guide-inset);
  border-radius: 17px;
  border: none;
  border-top: 1px dashed var(--board-guide);
  border-bottom: 1px dashed var(--board-guide);
  /* Match prior full-frame inset; only top/bottom so sides stay clean next to removed vertical dashes */
  box-shadow:
    inset 0 1px 0 0 rgb(235 213 184 / 0.06),
    inset 0 -1px 0 0 rgb(235 213 184 / 0.06);
  pointer-events: none;
}

/*
 * Phones (≤767px): stretch the playfield column edge-to-edge. Base `.main-layout` uses
 * `align-items: center`, which lets `#board-scroll` shrink to the board width and leaves gaps;
 * `.powerups` / `.tray-wrapper` keep their own `align-self` / width where needed.
 */
@media (max-width: 767px) {
  .main-layout {
    align-items: stretch;
  }

  .board-scroll {
    scrollbar-gutter: auto;
    min-width: 0;
    align-self: stretch;
  }

  .board-scroll-align {
    align-self: stretch;
    width: 100%;
    max-width: 100%;
    margin-inline: 0;
  }
}

/*
 * Tablet/desktop (≥768px): inset shell + 92vw-style cap; phones keep full-bleed board above.
 */
@media (min-width: 768px) {
  .main-layout {
    padding: 0.65rem max(0.75rem, env(safe-area-inset-left, 0px)) 0.65rem
      max(0.75rem, env(safe-area-inset-right, 0px));
  }

  .powerups {
    max-width: min(448px, 92vw);
    padding-inline: 0;
  }

  .board-scroll {
    padding-inline: env(safe-area-inset-left, 0px) env(safe-area-inset-right, 0px);
  }

  .board-scroll-align {
    padding: 12px;
  }

  .board {
    width: min(448px, 92%, calc(100dvh - 14.25rem));
    height: min(448px, 92%, calc(100dvh - 14.25rem));
  }

  @supports (width: 1cqw) {
    .board {
      width: min(448px, 92vw, calc(100cqw - 24px));
      height: min(448px, calc(100cqh - 24px));
    }
  }

  /* Full stitch frame + inner ring (phones keep top/bottom dashes only) */
  .board::before {
    border: 1px dashed var(--board-guide);
    box-shadow: inset 0 0 0 1px rgb(235 213 184 / 0.06);
  }
}

/* Plank + seamless noise + line grain (top). Noise uses soft-light so mid-gray stays neutral on wood. */
.tile,
.tray-tile {
  background-image: var(--tile-bg-grain), var(--tile-bg-noise), var(--tile-bg-plank);
  background-size: auto, var(--tile-noise-tile-size) var(--tile-noise-tile-size), auto;
  background-repeat: repeat, repeat, no-repeat;
  background-blend-mode: normal, soft-light, normal;
}

.tile .tile-icon,
.tray-tile .tile-icon {
  width: 1em;
  height: 1em;
  display: block;
  object-fit: contain;
  flex-shrink: 0;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-user-drag: none;
}

.board .tile {
  position: absolute;
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: var(--tile-corner-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--tile-icon-size);
  cursor: pointer;
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  box-shadow:
    var(--tile-drop-board),
    0 0 0 1px var(--wood-edge),
    var(--tile-inset-light),
    var(--tile-inset-dark);
  transform-origin: center center;
  /*
   * box-shadow is intentionally excluded from the transition: animating box-shadow forces a
   * main-thread repaint every frame and tanks frame rates when the mouse crosses multiple tiles
   * (same reason the board-settle animation excludes it). Shadow changes are instant.
   */
  transition: transform 0.12s ease, filter 0.1s ease, opacity 0.15s ease;
}

/*
 * Hover lift only on fine pointers — touch devices often synthesize :hover, which replaced
 * the inline centering transform on some engines and stacked all tiles visually (e.g. Pixel / Android).
 */
@media (hover: hover) and (pointer: fine) {
  .board .tile.tappable:hover {
    /*
     * will-change promotes only this one tile to its own compositor layer so the transform
     * runs entirely on the GPU without touching the main thread.
     */
    will-change: transform;
    transform: translateY(-3px) scale(1.03);
    box-shadow:
      0 16px 30px rgb(22 14 10 / 0.85),
      0 0 0 1px rgb(var(--accent-rgb) / 0.55),
      var(--tile-inset-light),
      var(--tile-inset-dark);
  }
}

.board .tile.blocked {
  filter: grayscale(0.4) brightness(0.9);
  cursor: default;
}

.board .tile.tile-queued {
  cursor: default;
  transform: translate(-50%, -50%) translateY(-10px);
  box-shadow:
    0 16px 32px rgba(0, 0, 0, 0.5),
    0 6px 16px rgb(22 14 10 / 0.55),
    0 0 0 2px rgb(var(--accent-rgb) / 0.8),
    var(--tile-inset-light),
    var(--tile-inset-dark);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

/* Promote fly to its own layer; WAAPI animates transform only (see animateTileToTray). */
.tile-flying {
  pointer-events: none;
  transition: none;
  will-change: transform;
  backface-visibility: hidden;
}

/*
 * Board settle: animate transform + opacity only. Animating box-shadow on hundreds of tiles
 * tanks mobile GPUs (was a major source of jank on large levels).
 */
.board-settle .board .tile {
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
}

.tile-settle-in {
  animation: tileSettleIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.tile-combining {
  pointer-events: none;
}

.tile-pop-in {
  animation: tilePopIn 0.18s ease-out;
}

.tray-wrapper {
  /* Pin to bottom of .main-layout when Chrome leaves spare flex space above the tray. */
  margin-top: auto;
  flex-shrink: 0;
  width: min(520px, 100vw);
  max-width: 100%;
  overflow-x: hidden;
  overflow-y: visible;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
  /* Match board mat treatment; tray row uses the same dashed guide as `.board::before`. */
  background: var(--board-felt);
  border-radius: 22px;
  padding: 0.55rem calc(0.85rem + env(safe-area-inset-right, 0px)) calc(0.7rem + env(safe-area-inset-bottom, 0px))
    calc(0.85rem + env(safe-area-inset-left, 0px));
  box-shadow:
    0 20px 52px rgb(12 8 6 / 0.78),
    0 0 0 1px var(--wood-edge),
    0 0 0 3px rgb(235 213 184 / 0.14),
    var(--board-felt-inset),
    0 0 36px 10px rgb(0 0 0 / 0.22);
  --tile-size: 64px;
  --tile-corner-radius: calc(var(--tile-size) * 0.28125);
}

/* After base `width: min(520px, 100vw)` above — full-bleed tray on phones (601–767px, not only ≤600). */
@media (max-width: 767px) {
  .tray-wrapper {
    width: 100%;
    max-width: 100%;
    align-self: stretch;
  }
}

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

.tray {
  user-select: none;
  -webkit-user-select: none;
  display: grid;
  grid-template-columns: repeat(7, var(--tile-size));
  gap: 0;
  width: max-content;
  max-width: 100%;
  margin-inline: auto;
  box-sizing: border-box;
  padding: 0.5rem 0.45rem;
  border-radius: 14px;
  border-top: 1px dashed var(--board-guide);
  border-bottom: 1px dashed var(--board-guide);
  overflow: visible;
}

@media (min-width: 768px) {
  .tray {
    border: 1px dashed var(--board-guide);
    box-shadow: inset 0 0 0 1px rgb(235 213 184 / 0.06);
  }
}

.tray-making-room .tray-slot-shift-right .tray-slot-inner {
  transition: transform 0.32s cubic-bezier(0.25, 0.1, 0.25, 1);
  transform: translateX(var(--tray-shift-x, 0));
}

.tray-compacting .tray-slot-shift-left .tray-slot-inner {
  transition: transform 0.32s cubic-bezier(0.25, 0.1, 0.25, 1);
  transform: translateX(var(--tray-shift-left, 0));
}

.tray-compacting .tray-slot-shift-left {
  /* Vacated column: keep divider lines only (no pocket fill). */
  background: transparent;
  box-shadow: none;
}

.tray-compacting {
  position: relative;
  z-index: 15;
}

.tray-slot {
  position: relative;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  min-height: var(--tile-size);
  overflow: visible;
}

.tray-slot + .tray-slot {
  border-inline-start: 1px dashed var(--board-guide);
}

.tray-slot-inner {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: var(--tile-size);
  z-index: 0;
}

.tray-tile {
  --tile-noise-tile-size: var(--tile-size);
  position: relative;
  z-index: 1;
  width: var(--tile-size);
  height: var(--tile-size);
  border-radius: var(--tile-corner-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile-size) * 0.6);
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  box-shadow:
    var(--tile-drop-tray),
    0 0 0 1px var(--wood-edge),
    var(--tile-inset-light),
    var(--tile-inset-dark);
  transform-origin: center center;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.tray-tile.matching {
  box-shadow:
    var(--tile-drop-tray),
    0 0 0 2px rgb(var(--accent-rgb) / 0.9),
    0 0 30px rgb(var(--accent-rgb) / 0.7),
    var(--tile-inset-light),
    var(--tile-inset-dark);
}

.tray-tile.selectable-type {
  cursor: pointer;
  box-shadow:
    var(--tile-drop-tray),
    0 0 0 2px rgb(var(--accent-rgb) / 0.85),
    0 0 20px rgb(var(--accent-rgb) / 0.55),
    var(--tile-inset-light),
    var(--tile-inset-dark);
}

/* Remove-type mode: roving focus on #tray */
#tray:focus-visible .tray-tile.tray-keyboard-focus.selectable-type {
  outline: 2px solid rgb(var(--accent-rgb) / 0.92);
  outline-offset: 3px;
}

.overlay {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
  background: radial-gradient(circle at 50% 0%, rgb(24 20 17 / 0.82), var(--chrome-scrim));
  backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
}

/* Win celebration: canvas fills dialog; drawn above scrim, below `.overlay-content` */
.win-star-fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  z-index: 0;
}

.overlay.hidden {
  display: none;
}

.overlay-content {
  position: relative;
  z-index: 1;
  background: var(--chrome-panel-bg);
  border-radius: 24px;
  padding: 1.5rem 1.7rem;
  max-width: min(340px, 92vw);
  width: 90vw;
  box-shadow:
    var(--chrome-panel-shadow),
    0 0 0 1px var(--chrome-panel-edge),
    var(--chrome-panel-inset);
}

.overlay-content h2 {
  margin-top: 0;
  margin-bottom: 0.5rem;
}

.overlay-content p {
  margin-top: 0;
  margin-bottom: 1.2rem;
  color: var(--muted);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.overlay-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  justify-content: flex-end;
}

.overlay-actions .btn {
  min-width: 110px;
}

.overlay-actions .btn.btn-icon {
  min-width: 2.75rem;
}

.hidden {
  display: none !important;
}

/* Level select overlay & carousel */
.level-select-overlay {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
  background: var(--chrome-scrim);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 25;
}

.level-select-overlay.hidden {
  display: none !important;
}

/* Settings dialog (shell: display, granular audio, haptics) */
.settings-overlay {
  position: fixed;
  inset: 0;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
  background: var(--chrome-scrim);
  backdrop-filter: blur(12px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 28;
  animation: settings-overlay-in 0.18s ease-out;
}

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

@media (prefers-reduced-motion: reduce) {
  .settings-overlay {
    animation: none;
  }
}

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

.settings-panel {
  background: var(--chrome-panel-bg);
  border-radius: 24px;
  padding: 1.25rem 1.5rem;
  max-width: min(420px, 96vw);
  width: 96vw;
  max-height: min(92dvh, 640px);
  display: flex;
  flex-direction: column;
  box-shadow:
    var(--chrome-panel-shadow),
    0 0 0 1px var(--chrome-panel-edge),
    var(--chrome-panel-inset);
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.settings-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
}

.settings-close {
  flex-shrink: 0;
  padding: 0.25rem 0.5rem;
  font-size: 1.15rem;
  line-height: 1;
}

.settings-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 0.2rem;
  margin-right: -0.2rem;
}

.settings-section {
  margin-bottom: 1.25rem;
}

.settings-section:last-of-type {
  margin-bottom: 0.35rem;
}

.settings-section-title {
  margin: 0 0 0.5rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--muted);
}

.settings-hint {
  margin: 0 0 0.65rem;
  font-size: 0.8rem;
  line-height: 1.35;
  color: var(--muted);
}

.settings-install-btn {
  justify-content: flex-start;
  width: 100%;
  max-width: 100%;
}

.settings-audio-stack {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
}

.settings-sound-block {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  width: 100%;
}

.settings-sound-label {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}

.settings-sound-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.settings-audio-volume {
  width: min(14rem, 100%);
  flex: 1 1 8rem;
  min-width: 5rem;
}

.settings-display-tools {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.settings-footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid var(--chrome-line);
}

.settings-reset-btn {
  width: 100%;
  justify-content: center;
  min-height: 2.75rem;
}

.level-select-panel {
  background: var(--chrome-panel-bg);
  border-radius: 24px;
  padding: 1.25rem 1.5rem;
  max-width: min(960px, 96vw);
  width: 96vw;
  box-shadow:
    var(--chrome-panel-shadow),
    0 0 0 1px var(--chrome-panel-edge),
    var(--chrome-panel-inset);
}

.level-select-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
}

.level-select-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.level-select-close {
  padding: 0.25rem 0.5rem;
  font-size: 1.2rem;
  line-height: 1;
}

.level-select-debug {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.level-select-debug-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.level-select-group-by {
  font-size: 0.85rem;
  padding: 0.35rem 0.6rem;
  border-radius: 10px;
  border: 1px solid var(--chrome-line-soft);
  background: rgb(24 20 17 / 0.85);
  color: var(--text);
  max-width: 100%;
}

.level-select-difficulty {
  display: flex;
  gap: 0.35rem;
  margin-bottom: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
}

.level-select-difficulty .btn {
  font-size: 0.8rem;
  padding: 0.35rem 0.65rem;
}

.level-select-band.is-active {
  border-color: var(--accent);
  color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.level-select-shape-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.level-select-shape-row.hidden {
  display: none !important;
}

.level-select-shape-label {
  font-size: 0.75rem;
  color: var(--muted);
}

.level-select-shape-select {
  font-size: 0.85rem;
  padding: 0.35rem 0.6rem;
  border-radius: 10px;
  border: 1px solid var(--chrome-line-soft);
  background: rgb(24 20 17 / 0.85);
  color: var(--text);
  min-width: 12rem;
  max-width: 100%;
}

.level-select-scroll {
  max-height: min(62vh, 520px);
  overflow-x: hidden;
  overflow-y: auto;
  margin-bottom: 0.75rem;
  padding: 0.15rem;
  border-radius: 14px;
  border: 1px solid rgb(235 213 184 / 0.1);
  background: rgb(12 10 8 / 0.35);
  -webkit-overflow-scrolling: touch;
}

.level-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 0.65rem;
  justify-items: center;
  align-items: start;
  min-height: 80px;
}

.level-select-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem;
  border-radius: 12px;
  border: 2px solid rgb(235 213 184 / 0.14);
  background: rgb(24 20 17 / 0.55);
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease,
    transform 0.28s cubic-bezier(0.25, 0.1, 0.25, 1),
    opacity 0.28s cubic-bezier(0.25, 0.1, 0.25, 1);
  min-width: 88px;
}

.level-select-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgb(var(--accent-rgb) / 0.2);
}

.level-select-card-current {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.level-select-card-completed:not(.level-select-card-current) {
  border-color: rgb(var(--accent-rgb) / 0.35);
}

.level-select-card-check {
  position: absolute;
  top: 4px;
  right: 4px;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 1.15rem;
  height: 1.15rem;
  border-radius: 999px;
  background: rgb(var(--accent-rgb) / 0.92);
  color: rgb(12 10 8);
  font-size: 0.65rem;
  line-height: 1;
  box-shadow:
    0 1px 2px rgb(0 0 0 / 0.35),
    inset 0 1px 0 rgb(255 255 255 / 0.25);
  pointer-events: none;
}

.level-select-mini-wrap {
  position: relative;
  flex-shrink: 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--board-felt);
  box-shadow:
    inset 0 0 0 1px var(--wood-edge),
    var(--board-felt-inset);
}

.level-select-mini-board {
  position: relative;
  transform-origin: top left;
}

/* Level picker: filled footprint only (no per-tile icons) */
.level-select-mini-silhouette-cell {
  position: absolute;
  border-radius: 22%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  background: rgb(var(--accent-rgb) / 0.42);
  box-shadow:
    inset 0 1px 0 rgb(255 255 255 / 0.22),
    inset 0 -1px 2px rgb(0 0 0 / 0.35),
    0 0 0 1px rgb(235 213 184 / 0.2);
}

.level-select-card-label {
  font-size: 0.7rem;
  color: var(--text);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.level-select-card-difficulty {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.level-select-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  overflow-wrap: anywhere;
}

/* Roving tabindex: focus stays on #board / #tray; child shows --accent — hide UA blue on container */
#board:focus,
#board:focus-visible,
#tray:focus,
#tray:focus-visible {
  outline: none;
}

/* Active tile / tray slot (keyboard): warm accent, softer than a full 3px solid slab */
#board:focus-visible .tile.tile-keyboard-focus.tappable {
  outline: 2px solid rgb(var(--accent-rgb) / 0.92);
  outline-offset: 3px;
}

.level-select-card:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

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

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

@keyframes tileSettleIn {
  from {
    transform: translate3d(-50%, -50%, 0) scale(0.96);
  }
  to {
    transform: translate3d(-50%, -50%, 0) scale(1);
  }
}

@media (max-width: 600px) {
  .game-title {
    display: none;
  }

  .top-bar {
    align-items: center;
    gap: 0.25rem;
    padding: calc(0.35rem + env(safe-area-inset-top, 0px)) calc(0.45rem + env(safe-area-inset-right, 0px)) 0.35rem
      calc(0.45rem + env(safe-area-inset-left, 0px));
  }

  .top-bar-left {
    flex: 1 1 auto;
    min-width: 0;
    gap: 0.45rem;
  }

  .level-info {
    gap: 0.35rem;
    font-size: 0.82rem;
  }

  .top-bar-right {
    flex: 0 1 auto;
    justify-content: flex-end;
    gap: 0.2rem;
  }

  #level-label {
    max-width: min(28vw, 9rem);
  }

  .score-info span:first-child {
    font-size: 0.62rem;
  }

  .score-info span:last-child {
    font-size: 0.85rem;
  }

  .powerups .btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.7rem;
  }

  .main-layout {
    padding: 0.45rem 0;
    gap: 0.55rem;
  }

  .powerups {
    gap: 0.35rem;
    padding-inline: max(0.55rem, env(safe-area-inset-left, 0px)) max(0.55rem, env(safe-area-inset-right, 0px));
  }

  .tray-wrapper {
    width: 100%;
    border-radius: 22px;
    padding: 0.45rem calc(0.75rem + env(safe-area-inset-right, 0px)) calc(0.55rem + env(safe-area-inset-bottom, 0px))
      calc(0.75rem + env(safe-area-inset-left, 0px));
  }

  .tray-label {
    margin-bottom: 0.22rem;
  }
}

/* Narrow + short: still no room above the board without clipping — match mobile-first clarity */
@media (max-width: 600px) and (max-height: 560px) {
  .powerups {
    display: none;
  }
}

