window.sketch = (runtime) => {
  const { THREE, canvas, startFrame } = runtime;

  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(150, window.innerWidth / window.innerHeight, 0.1, 100);
  const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
  renderer.setSize(window.innerWidth, window.innerHeight);

  // Buildings (Calamity City)
  const group = new THREE.Group();
  for (let i = 0; i < 50; i++) {
    const h = Math.random() * 10 + 2;
    const geo = new THREE.BoxGeometry(1, h, 1);
    const mat = new THREE.MeshBasicMaterial({ color: 0x221111 });
    const b = new THREE.Mesh(geo, mat);
    b.position.set((Math.random() - 0.5) * 40, h / 2 - 5, (Math.random() - 0.5) * 40);
    group.add(b);
  }
  scene.add(group);

  // Brimstone particles
  const particlesGeo = new THREE.BufferGeometry();
  const count = 2000;
  const pos = new Float32Array(count * 3);
  for (let i = 0; i < count * 3; i++) pos[i] = (Math.random() - 0.5) * 50;
  particlesGeo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
  const particlesMat = new THREE.PointsMaterial({ color: 0xffa500, size: 0.1 });
  const particles = new THREE.Points(particlesGeo, particlesMat);
  scene.add(particles);

  // Fire pillars
  const fireGeometry = new THREE.CylinderGeometry(0.5, 2, 5, 8);
  const fireMaterial = new THREE.MeshBasicMaterial({ color: 0xff4500, transparent: true, opacity: 0.8 });
  const fires = [];
  for (let i = 0; i < 5; i++) {
    const f = new THREE.Mesh(fireGeometry, fireMaterial);
    f.position.set((Math.random() - 0.5) * 20, 0, (Math.random() - 0.5) * 20);
    scene.add(f);
    fires.push(f);
  }

  camera.position.z = 25;
  camera.position.y = 0;

  const cleanup = startFrame((time) => {
    const t = time * 0.01;
    
    // Animate camera
    camera.position.x = Math.sin(t * 0.2) * 5;
    camera.lookAt(0, 0, 0);

    // Animate fires
    fires.forEach((f, i) => {
      f.scale.y = 1 + Math.sin(t + i) * 0.5;
      f.material.opacity = 0.5 + Math.sin(t * 2) * 0.2;
    });

    // Animate particles
    particles.rotation.y += 0.002;
    particles.position.y = Math.sin(t * 0.5) * 2;

    renderer.render(scene, camera);
  });

  return cleanup;
};
