/* Merge board: grid, pieces, chain tracing and the fusion ladder panel.
   Colours fall back to the palette so the file works on its own. */

.board-wrap { position: relative; width: 100%; }

.board {
  --cols: 6;
  --rows: 7;
  box-sizing: border-box;
  display: grid;
  gap: 6px;
  padding: 8px;
  margin: 0 auto;
  /* Fill the container width, but never grow taller than --board-max-h
     (so the whole screen still fits on a 360×640 phone). */
  width: min(100%, calc(54vh * var(--cols) / var(--rows)));
  width: min(100%, calc(54dvh * var(--cols) / var(--rows)));
  background: var(--board, #121A2E);
  border: 1px solid var(--border, #2A3654);
  border-radius: 16px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  position: relative;
  container-type: inline-size; /* piece text and glyph scale with the cell */
  min-width: 0;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  background: var(--cell, #1A2238);
}

/* Pieces: a flat tinted tile with a glyph, label and tier number. */
.piece {
  color: #0E1424;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0 2px;
  text-align: center;
  border-radius: 10px;
  overflow: hidden;
}
.item { position: absolute; inset: 0; cursor: grab; transition: transform .12s ease, box-shadow .12s ease; }
.piece .glyph {
  flex: none;
  width: 42cqi;
  height: 42cqi;
  min-width: 14px;
  max-width: 26px;
  max-height: 26px;
  fill: #0E1424;
  opacity: .55;
}
.piece .label {
  max-width: 100%;
  font-size: clamp(7px, 17cqi, 10px);
  font-weight: 700;
  line-height: 1.05;
  white-space: nowrap;
  overflow: hidden;
}
.piece .tier {
  position: absolute;
  top: 3px;
  right: 4px;
  font-size: clamp(7px, 15cqi, 9px);
  font-weight: 700;
  opacity: .75;
}

.item.dragging-src { opacity: .25; }
.cell.target .item { box-shadow: 0 0 0 2px var(--board, #121A2E), 0 0 0 4px var(--accent, #FF8A3D); }
.cell.can-extend .item { box-shadow: 0 0 0 2px var(--board, #121A2E), 0 0 0 4px var(--gold, #FFD166); }

/* chain tracing: pressed/traced pieces shrink slightly and get an orange ring */
.cell.in-chain .item {
  transform: scale(.94);
  box-shadow: 0 0 0 2px var(--board, #121A2E), 0 0 0 4px var(--accent, #FF8A3D);
}
.step {
  position: absolute;
  z-index: 5;
  box-sizing: border-box;
  width: 20px;
  height: 20px;
  border-radius: 999px;
  background: #0E1424; /* solid, so the step stays legible over the line */
  border: 2px solid var(--accent, #FF8A3D);
  color: var(--accent, #FF8A3D);
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.chain-line { position: absolute; left: 0; top: 0; pointer-events: none; z-index: 2; }
.chain-badge {
  /* Below the board, over the legend area: the finger covers the board, the panel the top. */
  position: absolute;
  left: 50%;
  top: 100%;
  margin-top: 8px;
  transform: translateX(-50%);
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2, #1B2440);
  border: 1px solid var(--border, #2A3654);
  font-size: 12px;
  color: var(--text-2, #C3CCE0);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s;
}
.chain-badge.show { opacity: 1; }
.chain-badge.ready { border-color: var(--accent, #FF8A3D); }
.chain-badge strong { font-family: 'Space Grotesk', Inter, sans-serif; font-size: 15px; color: var(--text, #E8ECF5); }
.chain-badge.ready strong { color: var(--accent, #FF8A3D); }

/* Ladder panel: a single compact row over the order card while tracing
   (main.js places it in a position:relative wrapper around the card). */
.chain-panel {
  position: absolute;
  inset: 0;
  z-index: 6;
  box-sizing: border-box;
  min-height: 100%;
  max-height: 64px;
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 8px;
  background: var(--surface-2, #1B2440);
  border: 1px solid var(--accent, #FF8A3D);
  border-radius: 14px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 120ms ease, visibility 120ms;
}
.chain-panel.show { opacity: 1; visibility: visible; z-index: 20; }
/* The chain badge sits below the board, where the legend is: hide the legend while tracing. */
.screen-game:has(.chain-panel.show) .legend { visibility: hidden; }
.chain-count {
  flex: none;
  height: 28px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--accent, #FF8A3D);
  color: #0E1424;
  font-family: 'Space Grotesk', Inter, sans-serif;
  font-weight: 700;
  font-size: 15px;
  display: flex;
  align-items: center;
  white-space: nowrap;
}
.ladder { flex: 1; min-width: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
.rung {
  min-width: 0;
  padding: 2px;
  border-radius: 8px;
  text-align: center;
  overflow: hidden;
  background: var(--surface, #151D33);
  border: 1px solid var(--border, #2A3654);
  color: var(--text-2, #C3CCE0);
}
.rung b { display: block; font-family: 'Space Grotesk', Inter, sans-serif; font-size: 13px; line-height: 1.1; }
.rung small { display: block; font-size: 10px; line-height: 1.1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: clip; }
.rung.current { background: var(--accent, #FF8A3D); border-color: var(--accent, #FF8A3D); color: #0E1424; }

/* Hint ring (tutorial) and flash on cleared cells. */
.cell.hint::after,
.cell.removed::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 13px;
  border: 2px solid var(--gold, #FFD166);
  pointer-events: none;
}
.cell.hint::after { animation: hint-pulse 1.2s ease-in-out infinite; }
.cell.removed::after { animation: removed-flash .7s ease-out forwards; }
@keyframes hint-pulse {
  0%, 100% { opacity: .35; transform: scale(.96); }
  50% { opacity: 1; transform: scale(1.02); }
}
@keyframes removed-flash {
  from { opacity: 1; transform: scale(1.1); }
  to { opacity: 0; transform: scale(1.25); }
}

/* Spawn, merge squash-and-glow. */
.item.pop { animation: pop .28s ease-out; }
.item.merged { animation: merged .4s ease-out; }
@keyframes pop {
  from { transform: scale(.4); }
  70% { transform: scale(1.08); }
  to { transform: scale(1); }
}
@keyframes merged {
  0% { transform: scale(.6); box-shadow: 0 0 0 0 rgba(255, 138, 61, .8); }
  60% { transform: scale(1.12); box-shadow: 0 0 18px 6px rgba(255, 138, 61, .5); }
  100% { transform: scale(1); box-shadow: none; }
}

/* Piece following the finger while moving. */
.ghost {
  position: fixed;
  z-index: 10;
  pointer-events: none;
  container-type: inline-size;
  border-radius: 12px;
  font-size: 10px;
  transform: translate(-50%, -60%) rotate(-6deg) scale(1.15);
  box-shadow: 0 12px 24px rgba(0, 0, 0, .5);
}

/* Floating "+12 XP" over a cell. */
.float {
  position: absolute;
  z-index: 5;
  pointer-events: none;
  white-space: nowrap;
  font-weight: 700;
  font-size: 13px;
  color: var(--gold, #FFD166);
  animation: rise .9s ease-out forwards;
}
@keyframes rise {
  from { opacity: 1; transform: translate(-50%, 0); }
  to { opacity: 0; transform: translate(-50%, -36px); }
}

@media (prefers-reduced-motion: reduce) {
  .item.pop,
  .item.merged,
  .float,
  .cell.hint::after { animation: none; }
  .cell.removed::after { display: none; }
  .item,
  .chain-badge,
  .chain-panel { transition: none; }
}
