// Menu — title + Play button. Keyboard (Space/Enter) or tap to start.
window.MenuScene = class MenuScene extends Phaser.Scene {
  constructor() { super('Menu'); }

  create() {
    const W = window.GAME.WIDTH, H = window.GAME.HEIGHT;

    if (this.textures.exists('bg')) {
      this.add.image(W / 2, H / 2, 'bg').setDisplaySize(W, H).setAlpha(0.85);
      this.add.rectangle(W / 2, H / 2, W, H, 0x0b0b14, 0.35);
    } else {
      this.cameras.main.setBackgroundColor(window.GAME.BG_COLOR);
    }

    const title = this.add.text(W / 2, H * 0.32, window.GAME.TITLE, {
      fontFamily: 'Arial', fontSize: '46px', fontStyle: 'bold', color: '#ffffff',
      stroke: '#000', strokeThickness: 6, align: 'center',
    }).setOrigin(0.5);
    this.tweens.add({ targets: title, y: title.y - 12, duration: 1400, yoyo: true, repeat: -1, ease: 'Sine.easeInOut' });

    this.add.text(W / 2, H * 0.42, window.GAME.SUBTITLE, {
      fontFamily: 'Arial', fontSize: '18px', color: '#cdd',
    }).setOrigin(0.5);

    const best = parseInt(localStorage.getItem(window.GAME.BEST_KEY) || '0', 10);
    this.add.text(W / 2, H * 0.5, 'BEST: ' + best, {
      fontFamily: 'Arial', fontSize: '20px', color: window.GAME.ACCENT, fontStyle: 'bold',
    }).setOrigin(0.5);

    // Play button
    const btn = this.add.rectangle(W / 2, H * 0.66, 220, 64, 0xffffff, 0.08)
      .setStrokeStyle(3, Phaser.Display.Color.HexStringToColor(window.GAME.ACCENT).color)
      .setInteractive({ useHandCursor: true });
    const btnText = this.add.text(W / 2, H * 0.66, 'PLAY', {
      fontFamily: 'Arial', fontSize: '28px', fontStyle: 'bold', color: '#ffffff',
    }).setOrigin(0.5);
    this.tweens.add({ targets: [btn, btnText], scale: 1.06, duration: 800, yoyo: true, repeat: -1, ease: 'Sine.easeInOut' });

    this.add.text(W / 2, H * 0.82, 'Arrow keys / drag / tap to move', {
      fontFamily: 'Arial', fontSize: '14px', color: '#889',
    }).setOrigin(0.5);

    const start = () => {
      window.Audio.unlock();
      window.Audio.sfx('start');
      this.cameras.main.fade(220, 0, 0, 0);
      this.time.delayedCall(230, () => this.scene.start('Game'));
    };
    btn.on('pointerdown', start);
    this.input.keyboard.on('keydown-SPACE', start);
    this.input.keyboard.on('keydown-ENTER', start);
  }
};
