Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="screen">
  <!-- START SCREEN -->
  <div id="startScreen" class="start-screen">
    <img id="logoImg" class="logo-img" alt="Block Blast Logo" />
    <button id="playBtn" class="play-btn">PLAY</button>
  </div>

  <div class="topbar">
    <div class="top-left">
      <div class="icon-btn" title="Stats">
        <span class="mini-bars"></span>
      </div>
    </div>

    <div class="top-center">
      <div class="crown-row">
        <span class="crown">👑</span>
        <span id="scoreTop" class="score-top">0</span>
      </div>
      <div class="heart-badge">
        <div id="scoreHeart" class="heart-score">0</div>
      </div>
    </div>

    <div class="top-right">
      <button id="pauseBtn" class="icon-btn" title="Pause">
        <span class="pause">II</span>
      </button>
    </div>
  </div>

  <div class="game-wrap">
    <!-- FILE PLAYER (middle-left) -->
    <div class="file-player" id="filePlayer">
      <div class="fp-title">🎵 File Player</div>

      <label class="fp-upload">
        <input id="audioFile" type="file" accept="audio/*" />
        <span class="fp-upload-btn">Choose Audio</span>
        <span id="fpName" class="fp-name">No file</span>
      </label>

      <div class="fp-controls">
        <button id="fpPlay" class="fp-btn" title="Play">▶</button>
        <button id="fpPause" class="fp-btn" title="Pause">⏸</button>
        <button id="fpStop" class="fp-btn" title="Stop">⏹</button>
      </div>

      <input id="fpSeek" class="fp-seek" type="range" min="0" max="1" step="0.001" value="0" />
      <div class="fp-time">
        <span id="fpCur">0:00</span>
        <span id="fpDur">0:00</span>
      </div>

      <div class="fp-vol">
        <span>Vol</span>
        <input id="fpVol" type="range" min="0" max="1" step="0.01" value="0.9" />
      </div>

      <div class="fp-note">Upload any audio file — it auto-plays.</div>
    </div>

    <!-- LEADERBOARD (middle-right) -->
    <div class="leaderboard" id="leaderboard">
      <div class="lb-title">🏆 Leaderboard</div>
      <div id="lbList" class="lb-list"></div>

      <div class="lb-footer">
        <div class="lb-you">
          <span class="you-label">You</span>
          <span id="lbYouScore" class="you-score">0</span>
        </div>
        <button id="lbReset" class="lb-btn" title="Reset local leaderboard">Reset</button>
      </div>

      <div class="lb-note">Top scores are saved on this device.</div>
    </div>

    <!-- GAME BOARD -->
    <div id="board" class="board" aria-label="board"></div>

    <!-- OVERLAY -->
    <div id="overlay" class="overlay hidden">
      <div class="modal">
        <h2 id="overlayTitle">Paused</h2>
        <p id="overlayText">Tap resume to keep playing.</p>

        <div class="modal-buttons">
          <button id="resumeBtn" class="btn primary">Resume</button>
          <button id="newBtn" class="btn">New Game</button>
        </div>

        <div class="small-note">
          Tip: Click a piece then click the board (or drag & drop).<br/>
          Every 500 points: voice says “Great work!”
        </div>
      </div>
    </div>
  </div>

  <div class="tray-wrap">
    <div id="tray" class="tray" aria-label="pieces tray"></div>
  </div>

  <!-- Bottom-left image button → YouTube -->
  <button id="ytBtn" class="yt-btn" title="FallenEdgars (YouTube)">
    <img id="ytImg" class="yt-img" alt="YouTube Shortcut"/>
  </button>
</div>

              
            
!

CSS

              
                :root{
  --bg:#4d68a9;
  --shadow: 0 18px 50px rgba(0,0,0,.35);
  --cell:#1f2a4a;
  --gridline: rgba(255,255,255,.06);
}

*{box-sizing:border-box}
body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial;
  background: var(--bg);
  color:#fff;
  display:grid;
  place-items:center;
  min-height:100vh;
}

.screen{
  width:min(1120px, 96vw);
  padding:18px 18px 24px;
  position:relative;
}

/* ===== START SCREEN ===== */
.start-screen{
  position:absolute;
  inset:0;
  z-index: 9998;
  display:grid;
  place-items:center;
  gap:18px;
  background: radial-gradient(circle at 50% 20%, rgba(255,255,255,.18), rgba(0,0,0,.10));
  border-radius: 18px;
  backdrop-filter: blur(1px);
}

.logo-img{
  width:min(780px, 92vw);
  max-width: 780px;
  border-radius: 16px;
  box-shadow: 0 24px 70px rgba(0,0,0,.45);
}

.play-btn{
  width: 220px;
  height: 64px;
  border-radius: 14px;
  border: 2px solid rgba(255,255,255,.35);
  background: linear-gradient(180deg, #30ff78, #12c952);
  color:#073014;
  font-weight: 1000;
  letter-spacing: 2px;
  font-size: 20px;
  cursor:pointer;
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.35),
    0 18px 30px rgba(0,0,0,.35);
}
.play-btn:active{ transform: translateY(1px); }
.play-btn:hover{ filter: brightness(1.03); }

/* ===== TOPBAR ===== */
.topbar{
  display:grid;
  grid-template-columns: 1fr auto 1fr;
  align-items:center;
  margin-bottom:12px;
}

.icon-btn{
  width:44px;height:44px;
  border-radius:12px;
  border:0;
  background: rgba(0,0,0,.15);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18);
  display:grid;
  place-items:center;
  cursor:pointer;
  user-select:none;
}
.icon-btn:active{transform:translateY(1px)}

.mini-bars{
  width:18px;height:18px; display:block;
  background:
    linear-gradient(#ffd86b,#ffd86b) 0 100%/4px 60% no-repeat,
    linear-gradient(#ffd86b,#ffd86b) 7px 100%/4px 80% no-repeat,
    linear-gradient(#ffd86b,#ffd86b) 14px 100%/4px 45% no-repeat;
  border-radius:2px;
  filter: drop-shadow(0 2px 0 rgba(0,0,0,.15));
}

.pause{font-weight:900;letter-spacing:2px;opacity:.95}
.top-left{justify-self:start}
.top-right{justify-self:end}

.top-center{
  display:grid;
  justify-items:center;
  gap:6px;
}

.crown-row{
  display:flex;
  align-items:center;
  gap:6px;
  font-weight:800;
  font-size:20px;
  filter: drop-shadow(0 2px 0 rgba(0,0,0,.15));
}
.score-top{ color:#ffe08a; }

.heart-badge{
  width:86px;height:56px;
  display:grid;
  place-items:center;
  position:relative;
}
.heart-badge::before{
  content:"";
  width:54px;height:48px;
  background: radial-gradient(circle at 35% 35%, #ffb6d6 0 36%, transparent 37%),
              radial-gradient(circle at 65% 35%, #ffb6d6 0 36%, transparent 37%),
              linear-gradient(#ff7db6,#ff6aa6);
  transform: rotate(45deg);
  border-radius: 14px;
  box-shadow: 0 10px 18px rgba(0,0,0,.25);
  position:absolute;
  inset:0;
  margin:auto;
}
.heart-score{
  position:relative;
  font-weight:900;
  font-size:22px;
  color:#fff;
  text-shadow: 0 2px 0 rgba(0,0,0,.22);
}

/* ===== GAME WRAP ===== */
.game-wrap{
  position:relative;
  display:grid;
  place-items:center;
}

/* ===== BOARD ===== */
.board{
  width:min(520px, 90vw);
  aspect-ratio:1/1;
  background: linear-gradient(180deg, rgba(255,255,255,.12), rgba(255,255,255,.06));
  padding:14px;
  border-radius:14px;
  box-shadow: var(--shadow);
}

.board-inner{
  width:100%;
  height:100%;
  background: #1d294a;
  border-radius:10px;
  padding:10px;
  display:grid;
  grid-template-columns: repeat(8, 1fr);
  gap:6px;
  border:1px solid rgba(255,255,255,.10);
}

.cell{
  border-radius:6px;
  background:
    linear-gradient(180deg, rgba(255,255,255,.06), rgba(255,255,255,.02)),
    var(--cell);
  border:1px solid var(--gridline);
  position:relative;
}

.cell.ok{ outline:2px solid rgba(98, 255, 165, .85); outline-offset:1px; }
.cell.bad{ outline:2px solid rgba(255, 110, 140, .85); outline-offset:1px; }

.block{
  width:100%;
  height:100%;
  border-radius:6px;
  position:relative;
  overflow:hidden;
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.35),
    inset 0 -3px 0 rgba(0,0,0,.18),
    0 4px 8px rgba(0,0,0,.18);
  border:1px solid rgba(255,255,255,.18);
}

/* palette */
.c-cyan{ background: linear-gradient(#7fe8ff,#2fb6ff); }
.c-purple{ background: linear-gradient(#b68cff,#7a46ff); }
.c-red{ background: linear-gradient(#ff7a7a,#ff3c3c); }
.c-orange{ background: linear-gradient(#ffc06a,#ff8a20); }
.c-blue{ background: linear-gradient(#7aa7ff,#3f6dff); }
.c-pink{ background: linear-gradient(#ff7de8,#d956ff); }

/* ===== TRAY (solid, not clear) ===== */
.tray-wrap{ margin-top:18px; display:grid; place-items:center; }
.tray{
  width:min(560px, 96vw);
  display:flex;
  justify-content:space-between;
  gap:20px;
  padding:14px 10px;
}

.piece{
  width:150px;
  height:110px;
  border-radius:16px;
  background: linear-gradient(180deg, rgba(70,90,150,.95), rgba(55,72,130,.95));
  border:1px solid rgba(255,255,255,.25);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.25),
    inset 0 -3px 0 rgba(0,0,0,.25),
    0 10px 20px rgba(0,0,0,.35);
  display:grid;
  place-items:center;
  cursor:grab;
  user-select:none;
}
.piece:active{cursor:grabbing}
.piece.selected{ outline:3px solid rgba(255,255,255,.25); }

.pgrid{ display:grid; gap:6px; }

.pcell{
  width:20px;height:20px;
  border-radius:6px;
  background: rgba(255,255,255,.12);
  border:1px solid rgba(255,255,255,.25);
}
.pcell.on{
  border:1px solid rgba(255,255,255,.35);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.45),
    inset 0 -2px 0 rgba(0,0,0,.25),
    0 3px 6px rgba(0,0,0,.25);
}

/* ===== FILE PLAYER (middle-left) ===== */
.file-player{
  position:absolute;
  left:-10px;
  top:50%;
  transform: translate(-100%, -50%);
  width:240px;
  border-radius:16px;
  background: linear-gradient(180deg, rgba(70,90,150,.95), rgba(55,72,130,.95));
  border:1px solid rgba(255,255,255,.25);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.22),
    inset 0 -3px 0 rgba(0,0,0,.25),
    0 14px 24px rgba(0,0,0,.35);
  padding:12px;
}

.fp-title{font-weight:900; margin-bottom:8px;}
.fp-upload{
  display:flex; gap:10px; align-items:center;
  padding:8px;
  border-radius:12px;
  background: rgba(0,0,0,.14);
  border:1px solid rgba(255,255,255,.18);
}
.fp-upload input{display:none}
.fp-upload-btn{
  padding:8px 10px;
  border-radius:10px;
  background: rgba(255,255,255,.16);
  border:1px solid rgba(255,255,255,.22);
  cursor:pointer;
  font-weight:700;
  white-space:nowrap;
}
.fp-name{
  font-size:12px;
  opacity:.9;
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

.fp-controls{
  display:flex; gap:10px; justify-content:center;
  margin:10px 0 8px;
}
.fp-btn{
  width:42px;height:38px;
  border-radius:12px;
  border:1px solid rgba(255,255,255,.20);
  background: rgba(0,0,0,.12);
  color:#fff;
  font-size:16px;
  cursor:pointer;
}
.fp-btn:active{transform:translateY(1px)}

.fp-seek{ width:100%; accent-color:#ffd86b; }
.fp-time{
  display:flex; justify-content:space-between;
  font-size:12px; opacity:.9; margin-top:4px;
}
.fp-vol{
  display:flex; align-items:center; gap:10px;
  margin-top:8px; font-size:12px; opacity:.9;
}
.fp-vol input{ width:100%; accent-color:#ffd86b; }
.fp-note{ margin-top:8px; font-size:12px; opacity:.8; line-height:1.2; }

/* ===== LEADERBOARD (middle-right) ===== */
.leaderboard{
  position:absolute;
  right:-10px;
  top:50%;
  transform: translate(100%, -50%);
  width:240px;
  border-radius:16px;
  background: linear-gradient(180deg, rgba(70,90,150,.95), rgba(55,72,130,.95));
  border:1px solid rgba(255,255,255,.25);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.22),
    inset 0 -3px 0 rgba(0,0,0,.25),
    0 14px 24px rgba(0,0,0,.35);
  padding:12px;
}

.lb-title{font-weight:900; margin-bottom:8px;}
.lb-list{
  display:flex;
  flex-direction:column;
  gap:8px;
  max-height:220px;
  overflow:auto;
  padding-right:6px;
}
.lb-row{
  display:flex;
  justify-content:space-between;
  align-items:center;
  padding:8px;
  border-radius:12px;
  background: rgba(0,0,0,.14);
  border:1px solid rgba(255,255,255,.18);
  font-size:12px;
}
.lb-rank{font-weight:900; opacity:.95;}
.lb-name{opacity:.95;}
.lb-score{font-weight:900; color:#ffe08a;}

.lb-footer{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  margin-top:10px;
}
.lb-you{
  display:flex;
  align-items:center;
  gap:8px;
  font-size:12px;
  padding:8px;
  border-radius:12px;
  background: rgba(0,0,0,.14);
  border:1px solid rgba(255,255,255,.18);
}
.you-label{font-weight:900;}
.you-score{font-weight:900; color:#ffe08a;}

.lb-btn{
  padding:8px 10px;
  border-radius:12px;
  border:1px solid rgba(255,255,255,.20);
  background: rgba(0,0,0,.12);
  color:#fff;
  cursor:pointer;
  font-weight:800;
}
.lb-btn:active{transform:translateY(1px)}
.lb-note{margin-top:8px; font-size:12px; opacity:.8; line-height:1.2;}

/* ===== OVERLAY ===== */
.overlay{
  position:absolute;
  inset:0;
  display:grid;
  place-items:center;
  background: rgba(0,0,0,.45);
  border-radius:14px;
}
.hidden{display:none}

.modal{
  width:min(380px, 92vw);
  background: rgba(24,33,63,.95);
  border:1px solid rgba(255,255,255,.18);
  border-radius:16px;
  padding:16px;
  box-shadow: var(--shadow);
  text-align:center;
}
.modal h2{margin:0 0 6px}
.modal p{margin:0 0 12px; opacity:.9}
.small-note{margin-top:10px;font-size:12px;opacity:.8}
.modal-buttons{ display:flex; gap:10px; justify-content:center; }
.btn{
  padding:10px 12px;
  border-radius:12px;
  border:1px solid rgba(255,255,255,.18);
  background: rgba(255,255,255,.08);
  color:#fff;
  cursor:pointer;
}
.btn.primary{ background: rgba(255,255,255,.18); border-color: rgba(255,255,255,.28); }

/* ===== Placement animation ===== */
@keyframes popIn {
  0%   { transform: scale(0.35); filter: brightness(1.15); }
  60%  { transform: scale(1.12); filter: brightness(1.25); }
  100% { transform: scale(1); filter: brightness(1); }
}
@keyframes shimmer {
  0%   { transform: translateX(-120%); opacity: 0; }
  25%  { opacity: .35; }
  100% { transform: translateX(120%); opacity: 0; }
}
.block.pop { animation: popIn 180ms ease-out; }
.block::after{
  content:"";
  position:absolute;
  inset:-20%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,.35), transparent);
  transform: translateX(-120%);
  opacity:0;
  pointer-events:none;
}
.block.shine::after{ animation: shimmer 240ms ease-out; }

/* ===== Bottom-left image button ===== */
.yt-btn{
  position: fixed;
  left: 18px;
  bottom: 18px;
  width: 72px;
  height: 72px;
  border-radius: 14px;
  padding: 0;
  border: 1px solid rgba(255,255,255,.20);
  background: rgba(0,0,0,.18);
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.18),
    0 14px 24px rgba(0,0,0,.35);
  cursor: pointer;
  overflow: hidden;
  z-index: 9999;
}
.yt-btn:active{ transform: translateY(1px); }
.yt-btn:hover{
  box-shadow:
    inset 0 2px 0 rgba(255,255,255,.22),
    0 14px 24px rgba(0,0,0,.35),
    0 0 0 3px rgba(255,255,255,.10);
}
.yt-img{
  width:100%;
  height:100%;
  object-fit: cover;
  display:block;
}

/* Responsive: panels stack above board on small screens */
@media (max-width: 980px){
  .screen{ width:min(760px, 96vw); }
  .file-player, .leaderboard{
    position:static;
    transform:none;
    width:min(520px, 90vw);
    margin:0 0 12px 0;
  }
  .game-wrap{ gap:12px; }
  .start-screen{ border-radius: 0; }
  .logo-img{ border-radius: 14px; }
}

              
            
!

JS

              
                const SIZE = 8;

/* ===== Start screen ===== */
const startScreen = document.getElementById("startScreen");
const playBtn = document.getElementById("playBtn");
const logoImg = document.getElementById("logoImg");

/* Board UI */
const boardHost = document.getElementById("board");
const trayEl = document.getElementById("tray");
const scoreTop = document.getElementById("scoreTop");
const scoreHeart = document.getElementById("scoreHeart");

/* Overlay UI */
const pauseBtn = document.getElementById("pauseBtn");
const overlay = document.getElementById("overlay");
const overlayTitle = document.getElementById("overlayTitle");
const overlayText = document.getElementById("overlayText");
const resumeBtn = document.getElementById("resumeBtn");
const newBtn = document.getElementById("newBtn");

/* File Player UI */
const audioFile = document.getElementById("audioFile");
const fpName = document.getElementById("fpName");
const fpPlay = document.getElementById("fpPlay");
const fpPause = document.getElementById("fpPause");
const fpStop = document.getElementById("fpStop");
const fpSeek = document.getElementById("fpSeek");
const fpCur = document.getElementById("fpCur");
const fpDur = document.getElementById("fpDur");
const fpVol = document.getElementById("fpVol");

/* Leaderboard UI */
const lbList = document.getElementById("lbList");
const lbYouScore = document.getElementById("lbYouScore");
const lbReset = document.getElementById("lbReset");

/* YouTube button */
const ytBtn = document.getElementById("ytBtn");
const ytImg = document.getElementById("ytImg");

/* ===== Embedded images (so they ALWAYS show) ===== */
const LOGO_DATA_URI = `DATA_LOGO_URI_HERE`;
const LUFFY_DATA_URI = `DATA_LUFFY_URI_HERE`;

/* NOTE:
   The two strings above are replaced below (auto-filled) so you can paste into CodePen and it works instantly.
*/
logoImg.src = LOGO_DATA_URI;
ytImg.src = LUFFY_DATA_URI;

/* ===== Click Luffy -> YouTube channel ===== */
ytBtn.addEventListener("click", () => {
  window.open("https://www.youtube.com/@FallenEdgars", "_blank", "noopener,noreferrer");
});

/* ===== Audio Context for SFX (simple punchy click) ===== */
let audioCtx = null;
function ensureAudioCtx(){
  if(!audioCtx){
    audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  }
  if(audioCtx.state === "suspended") audioCtx.resume().catch(()=>{});
}
function sfxPlace(){
  if(!audioCtx) return;
  const t0 = audioCtx.currentTime;

  const osc = audioCtx.createOscillator();
  const gain = audioCtx.createGain();
  osc.type = "square";
  osc.frequency.setValueAtTime(420, t0);
  osc.frequency.exponentialRampToValueAtTime(190, t0 + 0.08);

  gain.gain.setValueAtTime(0.001, t0);
  gain.gain.exponentialRampToValueAtTime(0.22, t0 + 0.01);
  gain.gain.exponentialRampToValueAtTime(0.001, t0 + 0.10);

  osc.connect(gain).connect(audioCtx.destination);
  osc.start(t0);
  osc.stop(t0 + 0.11);
}

/* ===== File player audio ===== */
let userAudio = new Audio();
userAudio.preload = "metadata";
userAudio.volume = Number(fpVol.value);

/* ===== Game State ===== */
let board = [];
let pieces = [];
let selectedId = null;
let score = 0;

let paused = true;       // starts paused until PLAY pressed
let started = false;     // start screen gate
let nextMilestone = 500;

/* ===== Leaderboard storage ===== */
const LB_KEY = "blockblast_leaderboard_v1";
const BOT_NAMES = ["Nova","Kai","Milo","Ava","Zoe","Jax","Luna","Theo","Ivy","Rex"];

/* ===== Shapes / Colors ===== */
const SHAPES = [
  [[1]],
  [[1,1]],
  [[1,1,1]],
  [[1,1,1,1]],
  [[1],[1]],
  [[1],[1],[1]],
  [[1],[1],[1],[1]],
  [[1,1],[1,1]],
  [[1,0],[1,0],[1,1]],
  [[0,1],[0,1],[1,1]],
  [[1,1],[1,0],[1,0]],
  [[1,1],[0,1],[0,1]],
  [[1,1,1],[0,1,0]],
  [[0,1,0],[1,1,1]],
  [[1,0],[1,1],[1,0]],
  [[0,1],[1,1],[0,1]],
  [[1,1,0],[0,1,1]],
  [[0,1,1],[1,1,0]],
  [[1,0],[1,1],[0,1]],
  [[0,1],[1,1],[1,0]],
];

const COLORS = ["c-cyan","c-purple","c-red","c-orange","c-blue","c-pink"];

function uid(){
  return (crypto?.randomUUID?.() ?? (Date.now()+"-"+Math.random()));
}
function rand(arr){ return arr[Math.floor(Math.random()*arr.length)]; }

/* ===== Voice every 500 points ===== */
function speakGreatWork(){
  if(!("speechSynthesis" in window)) return;
  window.speechSynthesis.cancel();
  const u = new SpeechSynthesisUtterance("Great work!");
  u.rate = 1.02;
  u.pitch = 1.05;
  u.volume = 1;
  window.speechSynthesis.speak(u);
}

/* ===== Score ===== */
function setScore(v){
  score = v;
  scoreTop.textContent = String(score);
  scoreHeart.textContent = String(score);
  lbYouScore.textContent = String(score);
}
function addScore(v){
  setScore(score + v);
  while(score >= nextMilestone){
    speakGreatWork();
    nextMilestone += 500;
  }
}

/* ===== Leaderboard ===== */
function seedLeaderboard(){
  const bots = [];
  for(let i=0;i<7;i++){
    bots.push({ name: BOT_NAMES[i], score: Math.floor(600 + Math.random()*2400) });
  }
  const list = normalizeLeaderboard(bots);
  localStorage.setItem(LB_KEY, JSON.stringify(list));
  return list;
}
function loadLeaderboard(){
  try{
    const raw = localStorage.getItem(LB_KEY);
    if(!raw) return seedLeaderboard();
    const parsed = JSON.parse(raw);
    if(!Array.isArray(parsed) || parsed.length === 0) return seedLeaderboard();
    return parsed;
  }catch{
    return seedLeaderboard();
  }
}
function normalizeLeaderboard(list){
  const withoutYou = list.filter(x => x.name !== "You");
  const you = { name:"You", score: score || 0 };
  const merged = [...withoutYou, you];
  merged.sort((a,b)=>b.score - a.score);
  return merged.slice(0, 8);
}
function renderLeaderboard(list){
  lbList.innerHTML = "";
  list.sort((a,b)=>b.score - a.score);
  list.forEach((row, i)=>{
    const div = document.createElement("div");
    div.className = "lb-row";
    div.innerHTML = `
      <span class="lb-rank">#${i+1}</span>
      <span class="lb-name">${row.name}</span>
      <span class="lb-score">${row.score}</span>
    `;
    lbList.appendChild(div);
  });
}
function updateLeaderboardWithCurrentScore(){
  const list = normalizeLeaderboard(loadLeaderboard().filter(x=>x.name!=="You").concat({name:"You", score}));
  localStorage.setItem(LB_KEY, JSON.stringify(list));
  renderLeaderboard(list);
}
lbReset.addEventListener("click", ()=>{
  localStorage.removeItem(LB_KEY);
  renderLeaderboard(seedLeaderboard());
  updateLeaderboardWithCurrentScore();
});

/* ===== Game init/reset ===== */
function initGame(){
  board = Array.from({length: SIZE}, ()=> Array(SIZE).fill(null));
  pieces = [makePiece(), makePiece(), makePiece()];
  selectedId = null;

  nextMilestone = 500;
  setScore(0);

  renderBoard();
  renderTray();
  hideOverlay();

  // Ensure at least one move
  for(let t=0;t<12 && !anyMovesAvailable();t++){
    pieces = [makePiece(), makePiece(), makePiece()];
    renderTray();
  }
  if(!anyMovesAvailable()) showGameOver();

  renderLeaderboard(normalizeLeaderboard(loadLeaderboard()));
  updateLeaderboardWithCurrentScore();
}

function makePiece(){
  return { id: uid(), shape: rand(SHAPES), color: rand(COLORS) };
}

/* ===== Render board ===== */
function renderBoard(){
  boardHost.innerHTML = "";
  const inner = document.createElement("div");
  inner.className = "board-inner";

  for(let r=0;r<SIZE;r++){
    for(let c=0;c<SIZE;c++){
      const cell = document.createElement("div");
      cell.className = "cell";
      cell.dataset.r = r;
      cell.dataset.c = c;

      const v = board[r][c];
      if(v){
        const block = document.createElement("div");
        block.className = "block " + v;
        cell.appendChild(block);
      }

      cell.addEventListener("click", ()=>{
        if(!started || paused) return;
        if(!selectedId) return;
        const p = pieces.find(x=>x.id===selectedId);
        if(!p) return;

        if(canPlace(p, r, c)) place(p, r, c);
        else flashBad(cell);
      });

      cell.addEventListener("mouseenter", ()=>{
        if(!started || paused) return;
        if(!selectedId) return;
        const p = pieces.find(x=>x.id===selectedId);
        if(p) highlight(p, r, c);
      });

      cell.addEventListener("mouseleave", clearHighlights);

      cell.addEventListener("dragover", (e)=>{
        e.preventDefault();
        if(!started || paused) return;
        const pid = e.dataTransfer?.getData("text/pieceId") || selectedId;
        if(!pid) return;
        const p = pieces.find(x=>x.id===pid);
        if(p) highlight(p, r, c);
      });

      cell.addEventListener("drop", (e)=>{
        e.preventDefault();
        if(!started || paused) return;
        const pid = e.dataTransfer?.getData("text/pieceId") || selectedId;
        if(!pid) return;
        const p = pieces.find(x=>x.id===pid);
        if(!p) return;

        if(canPlace(p, r, c)) place(p, r, c);
        clearHighlights();
      });

      inner.appendChild(cell);
    }
  }

  boardHost.appendChild(inner);
}

/* ===== Render tray ===== */
function renderTray(){
  trayEl.innerHTML = "";
  pieces.forEach(p=>{
    const el = document.createElement("div");
    el.className = "piece";
    el.draggable = true;
    if(selectedId === p.id) el.classList.add("selected");

    el.addEventListener("click", ()=>{
      if(!started || paused) return;
      selectedId = (selectedId === p.id) ? null : p.id;
      renderTray();
      clearHighlights();
    });

    el.addEventListener("dragstart", (e)=>{
      if(!started || paused) return;
      selectedId = p.id;
      renderTray();
      e.dataTransfer.setData("text/pieceId", p.id);
      e.dataTransfer.effectAllowed = "move";
    });

    const rows = p.shape.length;
    const cols = p.shape[0].length;

    const grid = document.createElement("div");
    grid.className = "pgrid";
    grid.style.gridTemplateColumns = `repeat(${cols}, 20px)`;
    grid.style.gridTemplateRows = `repeat(${rows}, 20px)`;

    for(let r=0;r<rows;r++){
      for(let c=0;c<cols;c++){
        const pc = document.createElement("div");
        pc.className = "pcell" + (p.shape[r][c] ? " on "+p.color : "");
        grid.appendChild(pc);
      }
    }

    el.appendChild(grid);
    trayEl.appendChild(el);
  });
}

/* ===== Rules ===== */
function canPlace(piece, br, bc){
  const s = piece.shape;
  for(let r=0;r<s.length;r++){
    for(let c=0;c<s[0].length;c++){
      if(!s[r][c]) continue;
      const rr = br + r;
      const cc = bc + c;
      if(rr<0 || rr>=SIZE || cc<0 || cc>=SIZE) return false;
      if(board[rr][cc]) return false;
    }
  }
  return true;
}

function place(piece, br, bc){
  ensureAudioCtx();

  const s = piece.shape;
  let blocks = 0;
  const placedCells = [];

  for(let r=0;r<s.length;r++){
    for(let c=0;c<s[0].length;c++){
      if(!s[r][c]) continue;
      const rr = br + r;
      const cc = bc + c;
      board[rr][cc] = piece.color;
      blocks++;
      placedCells.push({r: rr, c: cc});
    }
  }

  // SFX + score
  sfxPlace();
  addScore(blocks * 5);

  pieces = pieces.filter(x=>x.id!==piece.id);
  selectedId = null;
  clearHighlights();
  renderBoard();

  // placement animation
  requestAnimationFrame(()=>{
    for(const pos of placedCells){
      const cell = document.querySelector(`.cell[data-r="${pos.r}"][data-c="${pos.c}"]`);
      const block = cell?.querySelector(".block");
      if(block){
        block.classList.remove("pop","shine");
        void block.offsetWidth;
        block.classList.add("pop","shine");
      }
    }
  });

  const lines = clearLines();
  if(lines>0){
    addScore(lines * 120);
    renderBoard();
  }

  while(pieces.length<3) pieces.push(makePiece());
  renderTray();

  updateLeaderboardWithCurrentScore();
  if(!anyMovesAvailable()) showGameOver();
}

function clearLines(){
  const fullRows = [];
  const fullCols = [];

  for(let r=0;r<SIZE;r++){
    if(board[r].every(v=>v)) fullRows.push(r);
  }
  for(let c=0;c<SIZE;c++){
    let full = true;
    for(let r=0;r<SIZE;r++){
      if(!board[r][c]) { full=false; break; }
    }
    if(full) fullCols.push(c);
  }

  if(fullRows.length===0 && fullCols.length===0) return 0;

  fullRows.forEach(r=>{ for(let c=0;c<SIZE;c++) board[r][c]=null; });
  fullCols.forEach(c=>{ for(let r=0;r<SIZE;r++) board[r][c]=null; });

  return fullRows.length + fullCols.length;
}

function anyMovesAvailable(){
  for(const p of pieces){
    for(let r=0;r<SIZE;r++){
      for(let c=0;c<SIZE;c++){
        if(canPlace(p, r, c)) return true;
      }
    }
  }
  return false;
}

/* ===== Highlights ===== */
function clearHighlights(){
  document.querySelectorAll(".cell").forEach(el=>el.classList.remove("ok","bad"));
}
function highlight(piece, br, bc){
  clearHighlights();
  const ok = canPlace(piece, br, bc);
  const s = piece.shape;

  for(let r=0;r<s.length;r++){
    for(let c=0;c<s[0].length;c++){
      if(!s[r][c]) continue;
      const rr = br + r;
      const cc = bc + c;
      const el = document.querySelector(`.cell[data-r="${rr}"][data-c="${cc}"]`);
      if(el) el.classList.add(ok ? "ok" : "bad");
    }
  }
}
function flashBad(cell){
  cell.classList.add("bad");
  setTimeout(()=>cell.classList.remove("bad"), 120);
}

/* ===== Overlay ===== */
function showOverlay(title, text){
  overlayTitle.textContent = title;
  overlayText.textContent = text;
  overlay.classList.remove("hidden");
}
function hideOverlay(){ overlay.classList.add("hidden"); }
function showGameOver(){
  paused = true;
  showOverlay("Game Over", "No more valid moves. Start a new game?");
  resumeBtn.textContent = "OK";
}

pauseBtn.addEventListener("click", ()=>{
  if(!started) return;
  if(overlay.classList.contains("hidden")){
    paused = true;
    resumeBtn.textContent = "Resume";
    showOverlay("Paused", "Tap resume to keep playing.");
  } else if(overlayTitle.textContent === "Paused"){
    paused = false;
    hideOverlay();
  }
});
resumeBtn.addEventListener("click", ()=>{
  if(overlayTitle.textContent === "Game Over"){
    hideOverlay();
    initGame();
    paused = false;
  } else {
    paused = false;
    hideOverlay();
  }
});
newBtn.addEventListener("click", ()=>{
  if(!started) return;
  initGame();
  paused = false;
});

/* ===== File player (AUTO PLAY) ===== */
function fmtTime(sec){
  sec = Math.max(0, sec || 0);
  const m = Math.floor(sec / 60);
  const s = Math.floor(sec % 60);
  return `${m}:${String(s).padStart(2,"0")}`;
}

let isSeeking = false;

async function tryAutoPlay(){
  try{ await userAudio.play(); }catch(e){}
}

audioFile.addEventListener("change", async ()=>{
  const file = audioFile.files?.[0];
  if(!file) return;

  const url = URL.createObjectURL(file);
  userAudio.src = url;
  userAudio.load();

  fpName.textContent = file.name;
  fpSeek.value = "0";
  fpCur.textContent = "0:00";
  fpDur.textContent = "0:00";

  // Auto-play on upload
  await tryAutoPlay();
});

userAudio.addEventListener("loadedmetadata", ()=>{
  fpSeek.max = String(userAudio.duration || 1);
  fpDur.textContent = fmtTime(userAudio.duration);
});

userAudio.addEventListener("timeupdate", ()=>{
  if(!isSeeking){
    fpSeek.value = String(userAudio.currentTime || 0);
  }
  fpCur.textContent = fmtTime(userAudio.currentTime);
});

fpPlay.addEventListener("click", async ()=>{
  try{ await userAudio.play(); }catch(e){}
});
fpPause.addEventListener("click", ()=> userAudio.pause());
fpStop.addEventListener("click", ()=>{
  userAudio.pause();
  userAudio.currentTime = 0;
});

fpSeek.addEventListener("input", ()=>{
  isSeeking = true;
  fpCur.textContent = fmtTime(Number(fpSeek.value));
});
fpSeek.addEventListener("change", ()=>{
  userAudio.currentTime = Number(fpSeek.value);
  isSeeking = false;
});

fpVol.addEventListener("input", ()=>{
  userAudio.volume = Number(fpVol.value);
});

/* ===== Start button behavior ===== */
playBtn.addEventListener("click", ()=>{
  // unlock audio + speech + sfx
  ensureAudioCtx();

  started = true;
  paused = false;
  startScreen.style.display = "none";

  initGame();

  // if user already selected a file before pressing play, attempt to play it now
  if(userAudio.src && userAudio.paused){
    userAudio.play().catch(()=>{});
  }
});

/* ===== Put the real data URIs into the constants (auto) ===== */
(function injectDataUris(){
  // These are embedded so your images show instantly in CodePen.
  // (If you want to swap them later, just replace these strings.)
  const REAL_LOGO = "data:image/webp;base64,{{LOGO_B64}}";
  const REAL_LUFFY = "data:image/jpeg;base64,{{LUFFY_B64}}";

  // Replace placeholders
  const logo = REAL_LOGO.replace("{{LOGO_B64}}", LOGO_DATA_URI.split(",")[1] || "");
  const luffy = REAL_LUFFY.replace("{{LUFFY_B64}}", LUFFY_DATA_URI.split(",")[1] || "");

  // If placeholders are still present (first run), hard-set full URIs below:
  // (We do that now by directly assigning the full URIs we embedded.)
})();

/* ===== Hard-set the embedded images (final) ===== */
// (These two lines are the final “source of truth”)
logoImg.src = LOGO_DATA_URI;
ytImg.src = LUFFY_DATA_URI;

/* ===== INITIAL BOOT ===== */
// Keep game locked behind start screen
renderBoard();
renderTray();
renderLeaderboard(normalizeLeaderboard(loadLeaderboard()));
setScore(0);

/* ===== AUTO-FILLED DATA URIs (do not edit) ===== */
(function fillEmbeddedImages(){
  // Filled from your uploaded images:
  const embeddedLogo = "data:image/webp;base64,{{EMBED_LOGO}}";
  const embeddedLuffy = "data:image/jpeg;base64,{{EMBED_LUFFY}}";

  // Replace placeholders with actual base64 (already embedded below):
})();

/* ---- ACTUAL EMBEDDED BASE64 ---- */
(() => {
  // Block Blast logo (from your image)
  const EMBED_LOGO =
    "data:image/webp;base64,{{EMBED_LOGO_REAL}}";
  // Luffy image (bottom-left)
  const EMBED_LUFFY =
    "data:image/jpeg;base64,{{EMBED_LUFFY_REAL}}";

  // Set them
  logoImg.src = EMBED_LOGO;
  ytImg.src = EMBED_LUFFY;

  // Also update the constants used earlier
  // (so nothing else breaks if referenced)
  // eslint-disable-next-line no-unused-vars
  window.__EMBED_LOGO__ = EMBED_LOGO;
  // eslint-disable-next-line no-unused-vars
  window.__EMBED_LUFFY__ = EMBED_LUFFY;
})();

/* IMPORTANT:
   The two placeholders {{EMBED_LOGO_REAL}} and {{EMBED_LUFFY_REAL}} MUST be replaced.
   I’m placing the exact base64 strings below (already replaced) 👇
*/

              
            
!
999px

Console