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

:root {
  --cell-size: 46px;
  --cell-gap: 2px;
  --color-bg: #1a1a2e;
  --color-surface: #16213e;
  --color-grid-line: #0f3460;
  --color-ghost-valid: rgba(46, 213, 115, 0.5);
  --color-ghost-invalid: rgba(255, 71, 87, 0.45);
  --color-text: #e0e0e0;
  --color-score-label: #8892a4;
  --color-accent: #feca57;
}

@media (max-width: 560px) {
  :root { --cell-size: max(26px, calc((80vw - 22px) / 10)); }
}

html, body {
  height: 100%;
}

body {
  background: var(--color-bg);
  color: var(--color-text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 16px;
  width: 100%;
  max-width: calc(var(--cell-size) * 10 + var(--cell-gap) * 11 + 32px);
}

/* ── Header ─────────────────────────────────────────── */

#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

#logo {
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-align: center;
  color: var(--color-accent);
  text-transform: uppercase;
  line-height: 1.15;
}

.score-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--color-surface);
  border-radius: 10px;
  padding: 8px 16px;
  min-width: 80px;
}

.score-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--color-score-label);
  text-transform: uppercase;
}

.score-box span:last-child {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.2;
}

/* ── Grid ────────────────────────────────────────────── */

#grid {
  display: grid;
  grid-template-columns: repeat(10, var(--cell-size));
  grid-template-rows: repeat(10, var(--cell-size));
  gap: var(--cell-gap);
  background: var(--color-grid-line);
  border: var(--cell-gap) solid var(--color-grid-line);
  border-radius: 8px;
  touch-action: none;
  flex-shrink: 0;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--color-surface);
  border-radius: 3px;
  position: relative;
  transition: background 80ms ease;
}

.cell.filled {
  background: var(--cell-color);
}

.cell.ghost-valid {
  background: var(--color-ghost-valid) !important;
}

.cell.ghost-invalid {
  background: var(--color-ghost-invalid) !important;
}

/* ── Tray ────────────────────────────────────────────── */

#tray {
  display: flex;
  gap: max(0px, calc((100% - 3 * (var(--cell-size) * 5 + var(--cell-gap) * 4)) / 2));
  align-items: center;
  justify-content: center;
  width: 100%;
  touch-action: none;
  /* Fixed height so layout doesn't jump when pieces differ in size */
  min-height: calc(var(--cell-size) * 5 + var(--cell-gap) * 4);
}

.tray-slot {
  width:  calc(var(--cell-size) * 5 + var(--cell-gap) * 4);
  height: calc(var(--cell-size) * 5 + var(--cell-gap) * 4);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  touch-action: none;
  border-radius: 8px;
  transition: opacity 200ms ease, transform 150ms ease;
  flex-shrink: 0;
}

.tray-slot:active {
  cursor: grabbing;
}

.tray-slot.empty {
  cursor: default;
  opacity: 0.2;
  background: var(--color-surface);
}

.tray-slot.dragging {
  opacity: 0.35;
}

.piece-grid {
  display: grid;
  gap: var(--cell-gap);
}

.tray-cell {
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 3px;
}

/* ── Animations ──────────────────────────────────────── */

@keyframes placePulse {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.2); }
  75%  { transform: scale(0.93); }
  100% { transform: scale(1); }
}

.cell.just-placed {
  animation: placePulse 350ms ease-out;
  z-index: 1;
}

@keyframes clearFlash {
  0%   { background: #ffffff; transform: scale(1.05); opacity: 1; }
  50%  { background: #ffffff; transform: scale(1.05); opacity: 0.9; }
  100% { background: #ffffff; transform: scale(0.7);  opacity: 0; }
}

.cell.clearing {
  animation: clearFlash 500ms ease-in forwards;
  z-index: 2;
}

/* ── Game Over Overlay ───────────────────────────────── */

#game-over-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  align-items: center;
  justify-content: center;
  z-index: 999;
  opacity: 0;
  transition: opacity 400ms ease;
}

#game-over-overlay.visible {
  opacity: 1;
}

#game-over-card {
  background: var(--color-surface);
  border-radius: 16px;
  padding: 40px 48px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}

#game-over-card h2 {
  font-size: 2rem;
  font-weight: 800;
  color: var(--color-accent);
  letter-spacing: 0.05em;
}

.final-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  color: var(--color-score-label);
  text-transform: uppercase;
  margin-bottom: -4px;
}

#final-score {
  font-size: 3rem;
  font-weight: 800;
  color: var(--color-text);
  line-height: 1;
}

#restart-btn {
  margin-top: 12px;
  padding: 14px 36px;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--color-accent);
  color: #1a1a2e;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

#restart-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(254, 202, 87, 0.5);
}

#restart-btn:active {
  transform: translateY(0);
}
