window.sketch = (p) => {
  let time = 0;
  let flowers = [];
  let petals = [];
  let trails = [];

  p.setup = () => {
    p.createCanvas(p.windowWidth, p.windowHeight);
    p.colorMode(p.RGB, 255);
    // initialize some dream flowers
    for (let i = 0; i < 60; i++) {
      flowers.push({
        x: p.random(p.width),
        y: p.random(p.height),
        size: p.random(8, 24),
        hueShift: p.random(p.TWO_PI),
        phase: p.random(p.TWO_PI),
        petalCount: p.floor(p.random(5, 12)),
        sway: p.random(0.005, 0.02),
        floatSpeed: p.random(0.002, 0.008),
        driftX: p.random(-0.3, 0.3),
        driftY: p.random(-0.2, 0.2),
        life: 1
      });
    }
    // floating petals
    for (let i = 0; i < 40; i++) {
      petals.push({
        x: p.random(p.width),
        y: p.random(p.height),
        size: p.random(4, 12),
        angle: p.random(p.TWO_PI),
        speed: p.random(0.2, 1),
        wobble: p.random(0.01, 0.04),
        life: 1,
        hue: p.random(260, 320)
      });
    }
  };

  p.draw = () => {
    // dreamy sky gradient – lavender to amber with slow drifting
    for (let y = 0; y < p.height; y++) {
      let inter = p.map(y, 0, p.height, 0, 1);
      let r = p.lerp(180, 240, inter) + 10 * p.sin(p.frameCount * 0.002 + y * 0.005);
      let g = p.lerp(160, 180, inter) + 10 * p.cos(p.frameCount * 0.003 + y * 0.004);
      let b = p.lerp(220, 120, inter) + 15 * p.sin(p.frameCount * 0.0015 + y * 0.006);
      p.stroke(r, g, b, 40);
      p.line(0, y, p.width, y);
    }

    // soft atmospheric glow
    p.noStroke();
    for (let i = 0; i < 15; i++) {
      let x = p.width * 0.5 + 100 * p.sin(p.frameCount * 0.002 + i * 2.1);
      let y = p.height * 0.4 + 80 * p.cos(p.frameCount * 0.0018 + i * 1.7);
      let rad = 120 + 30 * p.sin(p.frameCount * 0.005 + i);
      p.fill(255, 220, 180, 6);
      p.circle(x, y, rad);
    }

    // drifting light orbs
    for (let i = 0; i < 12; i++) {
      let x = (p.width * 0.2 + i * 70 + p.frameCount * 0.3) % p.width;
      let y = p.height * 0.2 + 40 * p.sin(p.frameCount * 0.01 + i * 0.9);
      let sz = 8 + 6 * p.sin(p.frameCount * 0.02 + i);
      p.fill(255, 240, 180, 20 + 10 * p.sin(p.frameCount * 0.01 + i));
      p.circle(x, y, sz);
    }

    // update and draw dream flowers
    for (let i = flowers.length - 1; i >= 0; i--) {
      let f = flowers[i];
      // sway and drift
      f.x += f.driftX + 0.3 * p.sin(p.frameCount * f.sway + f.phase);
      f.y += f.driftY + 0.2 * p.cos(p.frameCount * f.floatSpeed + f.phase);
      // wrap around
      if (f.x < -50) f.x = p.width + 50;
      if (f.x > p.width + 50) f.x = -50;
      if (f.y < -50) f.y = p.height + 50;
      if (f.y > p.height + 50) f.y = -50;

      // soft glow behind flower
      p.noStroke();
      for (let g = 0; g < 3; g++) {
        let glowSize = f.size * 2.5 + 10 * p.sin(p.frameCount * 0.01 + f.phase);
        p.fill(255, 220, 200, 12);
        p.circle(f.x, f.y, glowSize);
      }

      // draw petals
      let petalCount = f.petalCount;
      let hueBase = 280 + 40 * p.sin(p.frameCount * 0.003 + f.hueShift);
      for (let j = 0; j < petalCount; j++) {
        let angle = p.TWO_PI * j / petalCount + p.frameCount * 0.002 + f.phase;
        let petalLen = f.size * (0.7 + 0.3 * p.sin(p.frameCount * 0.01 + j + f.phase));
        let px = f.x + petalLen * p.cos(angle);
        let py = f.y + petalLen * p.sin(angle);
        let petalSize = f.size * 0.5 + 3 * p.sin(p.frameCount * 0.015 + j * 0.7);
        p.fill(hueBase + 20 * p.sin(j), 180, 220, 160);
        p.noStroke();
        p.circle(px, py, petalSize);
      }
      // center
      p.fill(255, 240, 200, 200);
      p.circle(f.x, f.y, f.size * 0.5);
    }

    // floating petals
    for (let i = petals.length - 1; i >= 0; i--) {
      let pt = petals[i];
      pt.x += 0.4 * p.sin(pt.angle + p.frameCount * 0.005);
      pt.y += 0.3 * p.cos(pt.angle + p.frameCount * 0.003) - 0.2;
      pt.angle += 0.01;
      // wrap
      if (pt.x < -40) pt.x = p.width + 40;
      if (pt.x > p.width + 40) pt.x = -40;
      if (pt.y < -40) pt.y = p.height + 40;
      if (pt.y > p.height + 40) pt.y = -40;

      p.noStroke();
      let alpha = 80 + 40 * p.sin(p.frameCount * 0.01 + i);
      p.fill(pt.hue, 180, 230, alpha);
      p.circle(pt.x, pt.y, pt.size);

      // tiny glow
      p.fill(255, 200, 200, 15);
      p.circle(pt.x, pt.y, pt.size * 2.5);
    }

    // gentle shimmering lines (dreamy horizon)
    p.noFill();
    for (let i = 0; i < 8; i++) {
      let yBase = p.height * 0.7 + i * 18 + 5 * p.sin(p.frameCount * 0.004 + i * 0.5);
      p.stroke(240, 200, 180, 20 + 10 * p.sin(p.frameCount * 0.008 + i));
      p.strokeWeight(1.2);
      p.beginShape();
      for (let x = 0; x <= p.width; x += 20) {
        let y = yBase + 6 * p.sin(x * 0.01 + p.frameCount * 0.003 + i);
        p.vertex(x, y);
      }
      p.endShape();
    }

    // occasionally respawn flowers to keep infinite
    if (p.frameCount % 120 === 0 && flowers.length < 80) {
      flowers.push({
        x: p.random(p.width),
        y: p.random(p.height),
        size: p.random(10, 28),
        hueShift: p.random(p.TWO_PI),
        phase: p.random(p.TWO_PI),
        petalCount: p.floor(p.random(5, 14)),
        sway: p.random(0.004, 0.025),
        floatSpeed: p.random(0.001, 0.01),
        driftX: p.random(-0.4, 0.4),
        driftY: p.random(-0.3, 0.3),
        life: 1
      });
    }
    if (p.frameCount % 150 === 0 && petals.length < 70) {
      petals.push({
        x: p.random(p.width),
        y: p.random(p.height),
        size: p.random(4, 14),
        angle: p.random(p.TWO_PI),
        speed: p.random(0.2, 1.2),
        wobble: p.random(0.01, 0.05),
        life: 1,
        hue: p.random(260, 330)
      });
    }

    time += 0.01;
  };

  p.windowResized = () => {
    p.resizeCanvas(p.windowWidth, p.windowHeight);
  };
};
