:root {
  color-scheme: dark;
  --bg: #12131a;
  --panel: #1c1e29;
  --cell: #262939;
  --accent: #5eead4;
  --accent-2: #f472b6;
  --text: #e6e7ee;
  --text-dim: #8a8da3;

  /* Colores por tipo de pieza, paleta clásica del guideline de Tetris. */
  --piece-i: #22d3ee;
  --piece-o: #facc15;
  --piece-t: #c084fc;
  --piece-s: #4ade80;
  --piece-z: #f87171;
  --piece-j: #60a5fa;
  --piece-l: #fb923c;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 0%, #1e2130, var(--bg));
  font-family: "Segoe UI", system-ui, sans-serif;
  color: var(--text);
  overflow-x: hidden;
  overflow-y: auto;
}

.app {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(0.5rem, 2vmin, 1rem);
  padding: clamp(0.75rem, 3vmin, 1.5rem);
  width: min(560px, 96vw);
}

h1 {
  margin: 0;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  font-size: clamp(1.1rem, 5.5vmin, 1.6rem);
  color: var(--accent);
}

.hud {
  display: flex;
  width: 100%;
  justify-content: space-between;
  background: var(--panel);
  border-radius: 12px;
  padding: clamp(0.4rem, 2vmin, 0.75rem) clamp(0.5rem, 2.5vmin, 1rem);
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  flex: 1;
}

.hud-label {
  font-size: clamp(0.5rem, 1.7vmin, 0.7rem);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.hud-value {
  font-size: clamp(0.8rem, 2.8vmin, 1.1rem);
  font-weight: 600;
}

.game-area {
  position: relative;
  display: flex;
  gap: clamp(0.5rem, 2.5vmin, 1rem);
  align-items: flex-start;
  justify-content: center;
  width: 100%;
}

.board-wrap {
  position: relative;
  width: min(230px, 46vmin, 34vh, 34dvh);
  aspect-ratio: 1 / 2;
  flex: 0 0 auto;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.board {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(20, 1fr);
  gap: 2px;
  background: var(--panel);
  padding: 4px;
  border-radius: 12px;
}

.cell {
  border-radius: 2px;
  background: var(--cell);
}

.cell[data-state="filled"][data-type="I"],
.cell[data-state="active"][data-type="I"] { background: var(--piece-i); }
.cell[data-state="filled"][data-type="O"],
.cell[data-state="active"][data-type="O"] { background: var(--piece-o); }
.cell[data-state="filled"][data-type="T"],
.cell[data-state="active"][data-type="T"] { background: var(--piece-t); }
.cell[data-state="filled"][data-type="S"],
.cell[data-state="active"][data-type="S"] { background: var(--piece-s); }
.cell[data-state="filled"][data-type="Z"],
.cell[data-state="active"][data-type="Z"] { background: var(--piece-z); }
.cell[data-state="filled"][data-type="J"],
.cell[data-state="active"][data-type="J"] { background: var(--piece-j); }
.cell[data-state="filled"][data-type="L"],
.cell[data-state="active"][data-type="L"] { background: var(--piece-l); }

.cell[data-state="ghost"] {
  background: var(--cell);
  box-shadow: inset 0 0 0 2px rgba(230, 231, 238, 0.25);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: clamp(0.5rem, 2vmin, 0.75rem);
  width: min(150px, 30vmin);
  flex: 0 0 auto;
}

.preview-row {
  display: flex;
  gap: clamp(0.3rem, 1.5vmin, 0.5rem);
}

.next-box {
  display: flex;
  flex: 1;
  min-width: 0;
  flex-direction: column;
  gap: 0.3rem;
  align-items: center;
  background: var(--panel);
  border-radius: 10px;
  padding: clamp(0.4rem, 1.6vmin, 0.6rem);
}

/* Guardada ya usada: no se puede volver a guardar hasta que caiga la pieza. */
#hold-box.used {
  opacity: 0.45;
}

.next-piece {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 2px;
  background: var(--bg);
  padding: 4px;
  border-radius: 8px;
}

.next-cell {
  border-radius: 2px;
  background: transparent;
}

.next-cell.filled[data-type="I"] { background: var(--piece-i); }
.next-cell.filled[data-type="O"] { background: var(--piece-o); }
.next-cell.filled[data-type="T"] { background: var(--piece-t); }
.next-cell.filled[data-type="S"] { background: var(--piece-s); }
.next-cell.filled[data-type="Z"] { background: var(--piece-z); }
.next-cell.filled[data-type="J"] { background: var(--piece-j); }
.next-cell.filled[data-type="L"] { background: var(--piece-l); }

.touch-controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
}

.touch-btn {
  padding: clamp(0.5rem, 2vmin, 0.7rem) 0;
  border: none;
  border-radius: 8px;
  background: var(--cell);
  color: var(--text);
  font-weight: 700;
  font-size: clamp(0.9rem, 3vmin, 1.1rem);
  cursor: pointer;
  touch-action: manipulation;
}

.touch-btn:active {
  background: var(--accent);
  color: #08221d;
}

.touch-btn-wide {
  grid-column: span 3;
  font-size: clamp(0.7rem, 2.4vmin, 0.85rem);
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 11, 16, 0.75);
  border-radius: 16px;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 1.5rem;
  width: min(420px, 85vw);
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.overlay-card h2 {
  margin: 0;
  color: var(--accent);
}

.overlay-card p {
  margin: 0;
  color: var(--text-dim);
  font-size: 0.9rem;
  line-height: 1.4;
}

.auth-box {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.auth-buttons {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

.auth-btn {
  padding: 0.45rem 0.9rem;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  transition: transform 0.1s ease;
}

.auth-btn:hover {
  transform: translateY(-1px);
}

.auth-btn-primary {
  background: var(--accent);
  color: #08221d;
}

.auth-btn-secondary {
  background: var(--cell);
  color: var(--text);
}

.auth-user {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 0.75rem;
  background: var(--cell);
  border-radius: 10px;
  padding: 0.5rem 0.75rem;
  font-size: 0.85rem;
}

.auth-username {
  font-weight: 600;
}

.auth-coins {
  color: var(--accent);
  font-weight: 600;
}

.auth-link-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 0.8rem;
  text-decoration: underline;
  cursor: pointer;
  padding: 0;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  text-align: left;
}

.auth-form input {
  background: var(--cell);
  border: 1px solid #1a1b24;
  border-radius: 8px;
  padding: 0.5rem 0.6rem;
  color: var(--text);
  font-size: 0.9rem;
}

.auth-form input:focus {
  outline: 1px solid var(--accent);
}

.auth-form-error {
  margin: 0;
  color: #ef4444;
  font-size: 0.8rem;
  min-height: 1em;
}

.auth-form-actions {
  display: flex;
  gap: 0.5rem;
}

.auth-form-actions .auth-btn {
  flex: 1;
}

.auth-switch {
  margin: 0;
  font-size: 0.8rem;
  text-align: center;
}

.auth-switch a {
  color: var(--accent);
}

.reward-text {
  margin: 0;
  color: var(--accent);
  font-weight: 700;
}

.reward-text.hidden {
  display: none;
}

#start-btn {
  margin-top: 0.5rem;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #08221d;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.1s ease;
}

#start-btn:hover {
  transform: translateY(-1px);
}

.back-btn {
  margin-top: 0.25rem;
  padding: 0.5rem 1.1rem;
  border-radius: 8px;
  background: var(--cell);
  color: var(--text);
  font-weight: 600;
  font-size: 0.9rem;
  text-decoration: none;
  transition: transform 0.1s ease;
}

.back-btn:hover {
  transform: translateY(-1px);
}

.back-btn.hidden {
  display: none;
}

.leaderboard-box {
  margin-top: 0.5rem;
  padding-top: 0.75rem;
  border-top: 1px solid #1a1b24;
}

.leaderboard-box h3 {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.leaderboard-list {
  list-style: none;
  counter-reset: leaderboard;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  max-height: 220px;
  overflow-y: auto;
}

.leaderboard-list li {
  counter-increment: leaderboard;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--cell);
  border-radius: 8px;
  padding: 0.35rem 0.6rem;
  font-size: 0.85rem;
}

.leaderboard-list li::before {
  content: counter(leaderboard);
  flex: 0 0 auto;
  width: 1.4em;
  color: var(--text-dim);
  font-weight: 700;
  text-align: right;
}

.leaderboard-name {
  flex: 1;
  text-align: left;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-score {
  color: var(--accent);
  font-weight: 700;
}

.leaderboard-lines {
  color: var(--text-dim);
  font-size: 0.8rem;
}

.leaderboard-empty {
  color: var(--text-dim);
  font-size: 0.85rem;
  text-align: center;
  background: none !important;
}
