:root {
  --bg: #1a1c2c;
  --panel: #2a2d44;
  --cell-empty: #33374f;
  --cell-line: #292c41;
  --text: #f4f4f8;
  --accent: #ffd23f;
  --board-size: min(92vw, 70vh, 520px);
  --gap: 4px;
  --radius: 8px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(circle at 50% 0%, #2a2d52 0%, var(--bg) 70%);
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 14px 10px 24px;
}

.topbar {
  width: var(--board-size);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.title {
  font-size: clamp(18px, 5vw, 28px);
  margin: 0;
  text-align: center;
  color: var(--accent);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.35);
  letter-spacing: 0.5px;
  flex: 1;
}

.score-box {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 6px 12px;
  min-width: 74px;
  text-align: center;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25);
}

.score-box .label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.7;
}

.score-box .value {
  display: block;
  font-size: 22px;
  font-weight: 700;
}

.score-box.best .value { color: var(--accent); }

.right-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  flex: 0 0 auto;
  border: none;
  border-radius: 10px;
  background: var(--panel);
  color: var(--accent);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25);
  transition: transform 0.08s, filter 0.08s;
}

.icon-btn:active {
  transform: translateY(2px);
  filter: brightness(1.2);
}

.combo {
  height: 24px;
  font-size: 18px;
  font-weight: 800;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  transform: scale(0.6);
}

.combo.show {
  opacity: 1;
  transform: scale(1);
}

.game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.board {
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: var(--gap);
  background: var(--cell-line);
  padding: var(--gap);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4), inset 0 0 0 2px rgba(255,255,255,0.04);
  touch-action: none; /* don't let the board scroll while dragging over it */
}

.cell {
  background: var(--cell-empty);
  border-radius: 6px;
  position: relative;
}

.cell.filled {
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25), inset 0 2px 0 rgba(255, 255, 255, 0.25);
}

.cell.preview {
  outline: 2px solid rgba(255, 255, 255, 0.5);
  outline-offset: -2px;
}

.cell.preview-bad {
  background: rgba(255, 80, 80, 0.25);
}

.cell.clearing {
  animation: pop 0.32s ease forwards;
}

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.18); filter: brightness(1.6); }
  100% { transform: scale(0); opacity: 0; }
}

.tray {
  width: var(--board-size);
  display: flex;
  justify-content: space-around;
  align-items: center;
  gap: 8px;
  min-height: calc(var(--board-size) / 4.5);
  background: var(--panel);
  border-radius: 12px;
  padding: 12px 8px;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.2);
}

.piece {
  display: grid;
  gap: 3px;
  cursor: grab;
  transition: transform 0.15s;
  touch-action: none; /* critical: prevents touch-drag being treated as a scroll */
}

.piece.used {
  visibility: hidden;
}

.piece.dragging {
  opacity: 0.25;
}

.piece-cell {
  border-radius: 5px;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25), inset 0 2px 0 rgba(255, 255, 255, 0.25);
}

.piece-cell.empty {
  background: transparent !important;
  box-shadow: none;
}

.ghost {
  position: fixed;
  top: 0;
  left: 0;
  display: grid;
  gap: 3px;
  pointer-events: none;
  z-index: 1000;
  transform: translate(-9999px, -9999px);
  opacity: 0.9;
}

.ghost .piece-cell {
  border-radius: 6px;
}

.btn {
  background: var(--accent);
  color: #2a2418;
  border: none;
  border-radius: 10px;
  padding: 12px 22px;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 4px 0 #c9a31f;
  transition: transform 0.08s, box-shadow 0.08s;
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #c9a31f;
}

.btn.big {
  font-size: 20px;
  padding: 14px 32px;
}

.btn.secondary {
  background: var(--cell-empty);
  color: var(--text);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
}

.confirm-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 11, 20, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  backdrop-filter: blur(3px);
}

.overlay-card {
  background: var(--panel);
  border-radius: 18px;
  padding: 32px 28px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  animation: rise 0.3s ease;
}

@keyframes rise {
  from { transform: translateY(20px) scale(0.95); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

.overlay-card h2 {
  margin: 0 0 12px;
  font-size: 30px;
  color: var(--accent);
}

.overlay-card p { font-size: 18px; margin: 8px 0 18px; }
.new-best { color: var(--accent); font-weight: 800; }

.hidden { display: none !important; }
