:root {
  --bg-a: #f8fbf8;
  --bg-b: #e5f5ef;
  --card: rgba(255, 255, 255, 0.84);
  --line: rgba(16, 42, 47, 0.14);
  --text: #15292e;
  --muted: #496169;
  --accent: #ff8e6e;
  --accent-2: #46bfa7;
  --danger: #e25962;
  --shadow: 0 18px 42px rgba(28, 77, 88, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Space Grotesk", sans-serif;
  color: var(--text);
  min-height: 100vh;
  background: radial-gradient(circle at 12% 10%, #d0ece2 0%, transparent 45%),
    radial-gradient(circle at 80% 88%, #ffe8cf 0%, transparent 35%),
    linear-gradient(160deg, var(--bg-a), var(--bg-b));
  padding-top: calc(1rem + env(safe-area-inset-top));
  padding-right: calc(1rem + env(safe-area-inset-right));
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  padding-left: calc(1rem + env(safe-area-inset-left));
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

.background-glow {
  position: fixed;
  inset: auto auto -120px -80px;
  width: 260px;
  height: 260px;
  background: radial-gradient(circle, rgba(255, 143, 107, 0.26), transparent 72%);
  pointer-events: none;
}

.app {
  margin: 0 auto;
  max-width: 1040px;
  display: grid;
  gap: 0.68rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(6px);
}

.topbar,
.board-wrap,
.controls,
.tray-wrap,
.leaderboard-wrap,
.legend {
  padding: 0.72rem 0.82rem;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

h1,
h2,
h3 {
  margin: 0;
  font-family: "Bungee", cursive;
  letter-spacing: 0.03em;
}

h1 {
  font-size: 1.9rem;
}

h2 {
  font-size: 1rem;
}

.scores {
  display: flex;
  gap: 1rem;
}

.score-label {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
}

.score-value {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 700;
}

.player-line {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.86rem;
}

.board-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.8rem;
  margin-bottom: 0.55rem;
}

.board-wrap {
  position: relative;
}

#status {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  width: min(86vw, 58dvh, 560px);
  margin: 0 auto;
}

.cell {
  aspect-ratio: 1 / 1;
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(21, 41, 46, 0.08);
  transition: transform 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.cell.filled {
  background: linear-gradient(145deg, #ffb89a, #ff8f70);
  border-color: rgba(255, 143, 112, 0.55);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.cell.preview-ok {
  background: linear-gradient(145deg, #a4f2dd, #52d4b7);
}

.cell.preview-bad {
  background: rgba(226, 89, 98, 0.55);
}

.cell.line-clear {
  animation: clearBlast 250ms ease forwards;
}

.controls {
  display: grid;
  gap: 0.4rem;
}

.rotate-row {
  display: flex;
  justify-content: space-between;
  gap: 0.8rem;
  align-items: center;
}

#rotation-state {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.btn {
  border: none;
  border-radius: 12px;
  background: rgba(21, 41, 46, 0.08);
  color: var(--text);
  padding: 0.45rem 0.74rem;
  font-family: "Space Grotesk", sans-serif;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, filter 120ms ease;
}

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

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn.accent {
  background: linear-gradient(140deg, var(--accent), #ffb088);
  color: #102a2f;
}

.tray-head {
  margin-bottom: 0.45rem;
}

.tray {
  display: grid;
  gap: 0.55rem;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
}

.piece-card {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0.62rem;
  background: rgba(255, 255, 255, 0.62);
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background-color 120ms ease;
  touch-action: none;
  user-select: none;
}

.piece-card:hover {
  transform: translateY(-2px);
}

.piece-card.selected {
  border-color: var(--accent-2);
  background: rgba(79, 209, 162, 0.18);
}

.piece-card.used {
  opacity: 0.34;
  cursor: default;
}

.piece-grid {
  display: grid;
  gap: 4px;
  justify-content: start;
}

.piece-cell {
  width: 19px;
  height: 19px;
  border-radius: 6px;
  background: transparent;
  border: 1px solid transparent;
}

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  pointer-events: none;
  padding: 0.5rem;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  background: rgba(9, 20, 27, 0.88);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
}

.piece-cell.on {
  background: linear-gradient(140deg, #ffd3bf, #ff9e80);
  border-color: rgba(255, 143, 112, 0.3);
}

.piece-meta {
  margin-top: 0.35rem;
  color: var(--muted);
  font-size: 0.72rem;
}

.hint,
.legend p {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.leaderboard-status {
  margin: 0 0 0.35rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.leaderboard {
  margin: 0;
  padding-left: 1.2rem;
  display: grid;
  gap: 0.32rem;
  max-height: 150px;
  overflow: auto;
}

.leaderboard li {
  color: var(--text);
  font-size: 0.84rem;
}

.lb-name {
  font-weight: 700;
}

.lb-score {
  color: var(--accent);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(21, 41, 46, 0.48);
  display: grid;
  place-items: center;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-content {
  width: min(340px, 100%);
  text-align: center;
  padding: 0.85rem;
}

.register-form {
  width: min(360px, 100%);
  text-align: left;
  display: grid;
  gap: 0.4rem;
}

.register-copy {
  margin: 0;
  color: var(--muted);
  font-size: 0.84rem;
}

.field-label {
  font-size: 0.82rem;
  color: var(--muted);
}

.name-input {
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  color: var(--text);
  font-size: 0.92rem;
  padding: 0.56rem 0.62rem;
}

.name-input:focus {
  outline: 2px solid rgba(79, 209, 162, 0.5);
  border-color: var(--accent-2);
}

.register-error {
  margin: 0;
  color: var(--danger);
  min-height: 1.2em;
  font-size: 0.86rem;
}

#final-score {
  color: var(--muted);
}

.fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.clear-toast {
  position: absolute;
  left: 50%;
  bottom: 10%;
  transform: translate(-50%, 14px) scale(0.96);
  opacity: 0;
  background: rgba(21, 41, 46, 0.92);
  color: #fff6ef;
  border-radius: 999px;
  padding: 0.45rem 0.85rem;
  font-size: 0.84rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.clear-toast.show {
  animation: toastFloat 1.1s cubic-bezier(0.23, 1, 0.32, 1) forwards;
}

.reveal-1,
.reveal-2,
.reveal-3,
.reveal-4,
.reveal-5 {
  opacity: 0;
  transform: translateY(8px);
  animation: enter 420ms ease forwards;
}

.reveal-2 {
  animation-delay: 70ms;
}

.reveal-3 {
  animation-delay: 140ms;
}

.reveal-4 {
  animation-delay: 210ms;
}

.reveal-5 {
  animation-delay: 280ms;
}

body.dragging {
  overflow: hidden;
}

@keyframes enter {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes clearBlast {
  0% {
    transform: scale(1);
    filter: brightness(1);
  }
  55% {
    transform: scale(0.86);
    filter: brightness(1.5);
  }
  100% {
    transform: scale(0.2);
    opacity: 0;
  }
}

@keyframes toastFloat {
  0% {
    opacity: 0;
    transform: translate(-50%, 14px) scale(0.96);
  }
  14% {
    opacity: 1;
    transform: translate(-50%, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -34px) scale(1);
  }
}

@media (max-width: 640px) {
  .topbar {
    flex-direction: column;
    gap: 0.8rem;
  }

  .scores {
    width: 100%;
    justify-content: flex-start;
  }

  .board {
    width: min(91vw, 43dvh, 350px);
    gap: 5px;
  }

  .board-head {
    flex-direction: column;
    align-items: flex-start;
  }

  .rotate-row {
    flex-direction: column;
    align-items: flex-start;
  }

  h1 {
    font-size: 1.5rem;
  }

  .score-value {
    font-size: 1.35rem;
  }

  .tray {
    display: flex;
    overflow-x: auto;
    padding-bottom: 0.12rem;
    gap: 0.45rem;
    max-height: 112px;
  }

  .piece-card {
    min-width: 116px;
    flex: 0 0 auto;
  }

  .piece-cell {
    width: 16px;
    height: 16px;
  }

  .leaderboard {
    max-height: 100px;
  }

  .clear-toast {
    font-size: 0.78rem;
  }

  .legend {
    display: none;
  }
}

@media (min-width: 641px) and (max-width: 1024px) {
  .app {
    max-width: 900px;
  }

  .board {
    width: min(74vw, 46dvh, 470px);
  }

  .tray {
    display: flex;
    overflow-x: auto;
    gap: 0.55rem;
    max-height: 122px;
  }

  .piece-card {
    min-width: 128px;
    flex: 0 0 auto;
  }

  h1 {
    font-size: 1.6rem;
  }

  .score-value {
    font-size: 1.4rem;
  }

  .leaderboard {
    max-height: 114px;
  }

  .legend {
    display: none;
  }
}

@media (orientation: landscape) and (max-width: 1024px) {
  .board {
    width: min(66vw, 39dvh, 400px);
  }
}
