:root {
  --bg1: #1a2456;
  --bg2: #273b8c;
  --cell-empty: rgba(255, 255, 255, 0.08);
  --cell-border: rgba(255, 255, 255, 0.06);
  --text: #ffffff;
  --c1: #ffb020;
  --c2: #e94f64;
  --c3: #49c66f;
  --c4: #3aa2f0;
  --c5: #b06ef2;
  --c6: #f2884b;
  --c7: #35c6c0;
}

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

html, body { height: 100%; overflow: hidden; }

body {
  background: linear-gradient(180deg, var(--bg1), var(--bg2));
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

header { width: 100%; max-width: 480px; padding: 10px 14px 0; }

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.hud-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: bold;
  font-size: 1rem;
  background: rgba(0, 0, 0, 0.25);
  padding: 6px 12px;
  border-radius: 20px;
}

.hud-label { opacity: 0.7; font-size: 0.8rem; }

.hud-item.score {
  background: none;
  font-size: 2rem;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.hud-row2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 34px;
  margin-top: 6px;
}

#combo-banner {
  font-weight: 900;
  font-size: 1rem;
  color: #ffd84d;
  background: rgba(0, 0, 0, 0.25);
  padding: 4px 12px;
  border-radius: 16px;
  animation: comboPulse 0.6s ease infinite alternate;
}

#combo-banner[hidden] { display: none; }

@keyframes comboPulse { from { transform: scale(1); } to { transform: scale(1.08); } }

.mini-btns { display: flex; gap: 6px; margin-left: auto; }

.mini-btn {
  background: rgba(0, 0, 0, 0.25);
  border: none;
  border-radius: 16px;
  color: #fff;
  font-size: 0.85rem;
  font-weight: bold;
  padding: 6px 12px;
  cursor: pointer;
}

main {
  width: 100%;
  max-width: 480px;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 8px 12px;
  gap: 10px;
}

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

#board {
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 3px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 10px;
  padding: 5px;
}

.cell {
  background: var(--cell-empty);
  border-radius: 5px;
  border: 1px solid var(--cell-border);
  transition: background 0.08s;
}

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

.cell.ghost { opacity: 0.55; }
.cell.will-clear { filter: brightness(1.45); }

.cell.clearing { animation: blast 0.3s forwards; }
@keyframes blast {
  40% { transform: scale(1.15); filter: brightness(1.8); }
  100% { transform: scale(0); filter: brightness(2); }
}

.fill-c1 { background: var(--c1); }
.fill-c2 { background: var(--c2); }
.fill-c3 { background: var(--c3); }
.fill-c4 { background: var(--c4); }
.fill-c5 { background: var(--c5); }
.fill-c6 { background: var(--c6); }
.fill-c7 { background: var(--c7); }

#popup-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
}

.popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-weight: 900;
  font-size: 1.5rem;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  animation: rise 0.9s ease-out forwards;
  white-space: nowrap;
}

.popup.big { font-size: 2rem; color: #ffd84d; }

.particle {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 3px;
  transform: translate(-50%, -50%);
  animation: fly 0.6s ease-out forwards;
}

@keyframes fly {
  to {
    transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) rotate(260deg) scale(0.2);
    opacity: 0;
  }
}

#board-wrap.shake { animation: boardShake 0.4s; }

@keyframes boardShake {
  20% { transform: translate(-4px, 2px); }
  40% { transform: translate(4px, -2px); }
  60% { transform: translate(-3px, -2px); }
  80% { transform: translate(3px, 2px); }
}

@keyframes rise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -160%) scale(1); }
}

#tray {
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 90px;
  padding: 4px 0 12px;
}

.tray-slot {
  width: 30%;
  height: 86px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.piece { display: grid; gap: 2px; cursor: grab; }
.piece.used { visibility: hidden; }
.piece.cant-fit { opacity: 0.35; filter: grayscale(0.7); }

.piece .pcell {
  border-radius: 4px;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

#drag-piece {
  position: fixed;
  display: grid;
  gap: 3px;
  pointer-events: none;
  z-index: 40;
  filter: drop-shadow(0 6px 12px rgba(0, 0, 0, 0.4));
}

#drag-piece .pcell {
  border-radius: 5px;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.25), inset 0 2px 0 rgba(255, 255, 255, 0.3);
}

#ad-slot { width: 100%; min-height: 0; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 40, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.overlay[hidden] { display: none; }

.panel {
  background: var(--bg2);
  border-radius: 14px;
  padding: 26px;
  width: 100%;
  max-width: 340px;
  text-align: center;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

.panel h2 { margin-bottom: 12px; }
.panel p { margin: 8px 0; }
.go-score b { font-size: 1.6rem; }
.go-best { color: #ffd84d; font-weight: bold; }

.btn {
  width: 100%;
  margin-top: 12px;
  padding: 14px;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: bold;
  color: #fff;
  cursor: pointer;
}

.btn.primary { background: #49c66f; box-shadow: inset 0 -4px 0 rgba(0,0,0,0.2); }
.btn.tg { background: #2aabee; box-shadow: inset 0 -4px 0 rgba(0,0,0,0.2); }
.btn.revive {
  background: linear-gradient(180deg, #ffb020, #f2884b);
  box-shadow: inset 0 -4px 0 rgba(0,0,0,0.2);
  animation: comboPulse 0.7s ease infinite alternate;
}
.btn[hidden] { display: none; }

.modal-close {
  position: absolute;
  top: 8px;
  right: 14px;
  background: none;
  border: none;
  font-size: 1.6rem;
  color: #fff;
  cursor: pointer;
}

.panel { position: relative; }

.lb-tabs { display: flex; gap: 8px; margin: 14px 0; }

.lb-tab {
  flex: 1;
  padding: 8px;
  border: none;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  font-weight: bold;
  cursor: pointer;
  opacity: 0.6;
}

.lb-tab.active { opacity: 1; background: #49c66f; }

#lb-list { max-height: 45vh; overflow-y: auto; text-align: left; }

.lb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  margin: 3px 0;
  background: rgba(0, 0, 0, 0.18);
}

.lb-row .rank { width: 26px; text-align: center; font-weight: bold; opacity: 0.8; }
.lb-row .lb-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .lb-score { font-weight: bold; color: #ffd84d; }
.lb-row.top1 { background: rgba(255, 216, 77, 0.18); }
.lb-empty { text-align: center; opacity: 0.7; padding: 16px 0; }
