Skip to content
VibeCodeBench
Back to the app

The code

Pelican on a Bike by Claude Sonnet 5.5

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

works43.2 KB · 857 lines · no libraries
Download
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<title>Pelican Promenade Ride</title>
<style>
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:#6ec1f2;overflow:hidden;font-family:system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;-webkit-tap-highlight-color:transparent}
#app{display:flex;flex-direction:column;height:100vh;height:100dvh}
#stage{position:relative;flex:1 1 auto;min-height:0;touch-action:none;-webkit-user-select:none;user-select:none;cursor:pointer;overflow:hidden}
#scene{position:absolute;left:0;top:0;width:100%;height:100%;display:block}
#hint{position:absolute;left:0;right:0;top:10px;text-align:center;pointer-events:none;transition:opacity .6s}
#hint span{display:inline-block;background:rgba(255,255,255,.88);padding:7px 16px;border-radius:999px;font-weight:700;font-size:14px;color:#0f3d52;box-shadow:0 2px 6px rgba(0,0,0,.15)}
#demoBadge{position:absolute;left:10px;top:10px;background:rgba(15,61,82,.85);color:#fff;font-size:12px;font-weight:700;padding:5px 10px;border-radius:999px;display:none;pointer-events:none}
#ui{flex:0 0 auto;display:flex;flex-wrap:wrap;align-items:center;gap:6px 14px;padding:8px 12px calc(8px + env(safe-area-inset-bottom));background:#fff7e6;border-top:3px solid #f0c987;touch-action:manipulation}
.spd{flex:1 1 220px;display:flex;align-items:center;gap:10px;min-width:0}
.spd label{font-weight:800;color:#0f3d52;font-size:14px}
.spd output{min-width:3.2em;text-align:right;font-weight:700;color:#0f3d52;font-size:14px;font-variant-numeric:tabular-nums}
input[type=range]{flex:1 1 auto;min-width:0;height:44px;margin:0;background:transparent;-webkit-appearance:none;appearance:none;cursor:pointer}
input[type=range]::-webkit-slider-runnable-track{height:12px;border-radius:6px;background:linear-gradient(90deg,#8fd3ff,#1f8fd0)}
input[type=range]::-moz-range-track{height:12px;border-radius:6px;background:linear-gradient(90deg,#8fd3ff,#1f8fd0)}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:32px;height:32px;border-radius:50%;background:#ff7a3d;border:3px solid #fff;box-shadow:0 1px 5px rgba(0,0,0,.4);margin-top:-10px}
input[type=range]::-moz-range-thumb{width:28px;height:28px;border-radius:50%;background:#ff7a3d;border:3px solid #fff;box-shadow:0 1px 5px rgba(0,0,0,.4)}
.btns{display:flex;gap:8px;flex:1 1 280px;justify-content:center}
.btns button{flex:1 1 0;min-width:48px;min-height:48px;padding:0 10px;border-radius:24px;border:2px solid #1b6f8a;background:#fff;color:#0f3d52;font:700 15px system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;cursor:pointer;touch-action:manipulation;white-space:nowrap}
.btns button:active,.btns button.on{background:#ffe08a}
.btns button:focus-visible,input[type=range]:focus-visible{outline:3px solid #ff7a3d;outline-offset:2px}
#dp{position:fixed;left:0;top:0;width:30px;height:30px;margin:-15px 0 0 -15px;border-radius:50%;background:rgba(255,80,80,.3);border:3px solid #e5484d;pointer-events:none;z-index:50;display:none}
#dp.press{background:rgba(255,80,80,.65);width:38px;height:38px;margin:-19px 0 0 -19px}
@media (max-height:430px){#ui{padding-top:4px;padding-bottom:4px}.btns button{min-height:44px}input[type=range]{height:44px}}
</style>
</head>
<body>
<div id="app">
  <div id="stage">
    <svg id="scene" viewBox="0 0 800 450" preserveAspectRatio="xMidYMid meet" role="img" aria-label="A pelican riding a bicycle along a seaside promenade">
      <defs>
        <linearGradient id="gSky" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="256"><stop offset="0" stop-color="#58b4ee"/><stop offset="1" stop-color="#ffe8c6"/></linearGradient>
        <linearGradient id="gSea" gradientUnits="userSpaceOnUse" x1="0" y1="255" x2="0" y2="340"><stop offset="0" stop-color="#5cbde4"/><stop offset="1" stop-color="#1c78b8"/></linearGradient>
        <linearGradient id="gPave" gradientUnits="userSpaceOnUse" x1="0" y1="352" x2="0" y2="450"><stop offset="0" stop-color="#e9dcc0"/><stop offset="1" stop-color="#d6c39c"/></linearGradient>
      </defs>
      <g id="world"></g>
    </svg>
    <div id="hint"><span id="hintTxt">Tap anywhere to flap!</span></div>
    <div id="demoBadge">DEMO - touch anything to play</div>
  </div>
  <div id="ui">
    <div class="spd">
      <label for="speed">Speed</label>
      <input id="speed" type="range" min="0" max="100" step="1" value="45" aria-label="Speed">
      <output id="spdOut" for="speed">45%</output>
    </div>
    <div class="btns">
      <button id="bFlap" type="button" aria-label="Flap wings">Flap</button>
      <button id="bBell" type="button" aria-label="Ring the bell">&#128276; Bell</button>
      <button id="bMute" type="button" aria-label="Mute or unmute sound" aria-pressed="false">&#128266; Sound</button>
      <button id="bReset" type="button" aria-label="Restart the ride">&#8635; Reset</button>
    </div>
  </div>
</div>
<div id="dp"></div>

<script>
(function () {
'use strict';

/* ---------- helpers ---------- */
var NS = 'http://www.w3.org/2000/svg';
function el(n, a, p) {
  var e = document.createElementNS(NS, n);
  if (a) for (var k in a) e.setAttribute(k, a[k]);
  if (p) p.appendChild(e);
  return e;
}
function mulberry(seed) {
  var s = seed >>> 0;
  return function () {
    s = (s + 0x6D2B79F5) >>> 0;
    var t = s;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
function $(id) { return document.getElementById(id); }
function fx(n) { return Math.round(n * 100) / 100; }
function clamp(v, a, b) { return v < a ? a : (v > b ? b : v); }
function lerp(a, b, t) { return a + (b - a) * t; }

var rand = mulberry(7);
var svg = $('scene'), world = $('world'), stage = $('stage');
var slider = $('speed'), spdOut = $('spdOut');
var bFlap = $('bFlap'), bBell = $('bBell'), bMute = $('bMute'), bReset = $('bReset');
var hint = $('hint');

/* ---------- constants ---------- */
var PERIOD = 1600;          // scenery tile period
var GROUND_Y = 392;         // y of ground contact in world coords
var WR = 46;                // wheel rolling radius
var N1 = 20, N2 = 11, PITCH = 5.03;
var R1 = N1 * PITCH / (2 * Math.PI);   // chainring radius
var R2 = N2 * PITCH / (2 * Math.PI);   // rear sprocket radius
var VMAX = 520;             // units per second at 100%
var CRANK = 18;
var BB = { x: 0, y: -38 }, RA = { x: -72, y: -46 }, FA = { x: 72, y: -46 };
var GRIP = { x: 59, y: -117 };
var SHOULDER = { x: -4, y: -150 };
var HIP = { x: -14, y: -113 };

/* ---------- layers (parallax) ---------- */
var layers = [];
var gulls = [], boats = [];

function layer(factor, seed, builder, drift) {
  var g = el('g', null, world);
  var inner = el('g', null, g);
  [0, PERIOD].forEach(function (off) {
    rand = mulberry(seed);
    var h = el('g', { transform: 'translate(' + off + ',0)' }, inner);
    builder(h);
  });
  layers.push({ g: g, f: factor, drift: drift || 0 });
  return g;
}

/* sky + sun (static) */
el('rect', { x: -4000, y: -2400, width: 9000, height: 2656, fill: 'url(#gSky)' }, world);
var sun = el('g', null, world);
el('circle', { r: 78, fill: '#fff6c8', opacity: .22 }, sun);
el('circle', { r: 50, fill: '#fff3b0', opacity: .4 }, sun);
el('circle', { r: 30, fill: '#fff9d6' }, sun);

/* clouds */
var cloudLayer = layer(0.04, 11, function (h) {
  for (var i = 0; i < 8; i++) {
    var x = i * 200 + rand() * 100, y = 26 + rand() * 90, s = 0.7 + rand() * 0.7;
    var g = el('g', { transform: 'translate(' + fx(x) + ',' + fx(y) + ') scale(' + fx(s) + ')', fill: '#fff', opacity: .94 }, h);
    el('ellipse', { cx: 14, cy: 8, rx: 44, ry: 12 }, g);
    el('circle', { cx: -18, cy: 2, r: 15 }, g);
    el('circle', { cx: 2, cy: -9, r: 21 }, g);
    el('circle', { cx: 26, cy: -2, r: 17 }, g);
    el('circle', { cx: 44, cy: 4, r: 12 }, g);
  }
});

/* gulls (own drift + flapping) */
var gullLayer = layer(0.1, 23, function (h) {
  for (var i = 0; i < 5; i++) {
    var x = rand() * PERIOD, y = 60 + rand() * 100, s = 0.8 + rand() * 0.7;
    var g = el('g', { transform: 'translate(' + fx(x) + ',' + fx(y) + ') scale(' + fx(s) + ')' }, h);
    var p = el('path', { d: 'M-9,0 Q-4,-4 0,0 Q4,-4 9,0', fill: 'none', stroke: '#3b4756', 'stroke-width': 1.8, 'stroke-linecap': 'round' }, g);
    gulls.push({ p: p, ph: rand() * 6.28 });
  }
}, 14);

/* far headland + lighthouse */
layer(0.06, 31, function (h) {
  var pts = 'M0,256 ';
  for (var x = 0; x <= PERIOD; x += 20) {
    var hh = 12 + 8 * Math.sin(6.2832 * 3 * x / PERIOD + 1) + 5 * Math.sin(6.2832 * 7 * x / PERIOD) + 3 * Math.sin(6.2832 * 13 * x / PERIOD + 2);
    pts += 'L' + x + ',' + fx(256 - Math.max(2, hh)) + ' ';
  }
  pts += 'L' + PERIOD + ',256 Z';
  el('path', { d: pts, fill: '#8fc3d4', opacity: .85 }, h);
  var lh = el('g', { transform: 'translate(700,0)' }, h);
  el('path', { d: 'M-8,248 L8,248 L5,212 L-5,212 Z', fill: '#fafafa', stroke: '#d8d8d8', 'stroke-width': .6 }, lh);
  el('path', { d: 'M-6.6,234 L6.6,234 L5.9,226 L-5.9,226 Z', fill: '#e4574f' }, lh);
  el('rect', { x: -6, y: 206, width: 12, height: 6, fill: '#ffe27a', stroke: '#8a6d1a', 'stroke-width': .6 }, lh);
  el('path', { d: 'M-8,206 L0,198 L8,206 Z', fill: '#e4574f' }, lh);
});

/* sea */
el('rect', { x: -4000, y: 255, width: 9000, height: 86, fill: 'url(#gSea)' }, world);

function wavesBuilder(n, y0, y1, l0, l1, op, w) {
  return function (h) {
    for (var i = 0; i < n; i++) {
      var x = rand() * PERIOD, y = y0 + rand() * (y1 - y0), len = l0 + rand() * (l1 - l0);
      el('path', { d: 'M' + fx(x) + ',' + fx(y) + ' q' + fx(len / 4) + ',-2.2 ' + fx(len / 2) + ',0 t' + fx(len / 2) + ',0', stroke: '#fff', 'stroke-width': w, fill: 'none', opacity: op, 'stroke-linecap': 'round' }, h);
    }
  };
}
var wavesFar = layer(0.12, 41, wavesBuilder(40, 258, 292, 8, 20, .45, 1.2));

function sailboat(h, x, y, sc, c1, c2) {
  var g = el('g', { transform: 'translate(' + fx(x) + ',' + fx(y) + ') scale(' + sc + ')' }, h);
  var b = el('g', null, g);
  el('path', { d: 'M-26,0 L26,0 L17,10 L-17,10 Z', fill: c1, stroke: 'rgba(0,0,0,.25)', 'stroke-width': .8 }, b);
  el('line', { x1: 0, y1: 0, x2: 0, y2: -50, stroke: '#5a4634', 'stroke-width': 1.6 }, b);
  el('path', { d: 'M2.5,-48 L2.5,-3 L30,-3 Z', fill: '#fff', stroke: '#d8dde2', 'stroke-width': .6 }, b);
  el('path', { d: 'M-2.5,-42 L-2.5,-3 L-22,-3 Z', fill: c2, stroke: 'rgba(0,0,0,.15)', 'stroke-width': .6 }, b);
  el('path', { d: 'M0,-50 L9,-47 L0,-44 Z', fill: '#e4574f' }, b);
  boats.push({ g: g, b: b, x: x, y: y, sc: sc, ph: rand() * 6.28 });
}
function motorboat(h, x, y, sc, c1) {
  var g = el('g', { transform: 'translate(' + fx(x) + ',' + fx(y) + ') scale(' + sc + ')' }, h);
  var b = el('g', null, g);
  el('path', { d: 'M-34,-2 L36,-2 L28,11 L-26,11 Z', fill: c1, stroke: 'rgba(0,0,0,.25)', 'stroke-width': .8 }, b);
  el('rect', { x: -14, y: -16, width: 26, height: 14, rx: 2, fill: '#fff', stroke: '#cfd5da', 'stroke-width': .7 }, b);
  el('rect', { x: -10, y: -13, width: 7, height: 6, fill: '#7fc4e8' }, b);
  el('rect', { x: 0, y: -13, width: 7, height: 6, fill: '#7fc4e8' }, b);
  el('rect', { x: -4, y: -26, width: 6, height: 10, fill: '#e4574f' }, b);
  boats.push({ g: g, b: b, x: x, y: y, sc: sc, ph: rand() * 6.28 });
}
var boatsFar = layer(0.15, 51, function (h) {
  sailboat(h, 300, 276, .5, '#e4574f', '#ffd166');
  motorboat(h, 760, 280, .5, '#2f5d8a');
  sailboat(h, 1130, 275, .55, '#2f5d8a', '#ff9f6b');
  sailboat(h, 1480, 277, .45, '#f2a541', '#e4574f');
});
var wavesNear = layer(0.28, 61, wavesBuilder(36, 296, 334, 14, 34, .55, 1.6));
var boatsNear = layer(0.32, 71, function (h) {
  sailboat(h, 160, 302, 1, '#e4574f', '#ffd166');
  motorboat(h, 620, 308, .95, '#1f6f8b');
  sailboat(h, 1010, 303, .9, '#2f5d8a', '#ff9f6b');
  motorboat(h, 1340, 310, .8, '#e4574f');
});

/* seawall + railing */
layer(0.6, 81, function (h) {
  el('rect', { x: 0, y: 338, width: PERIOD, height: 18, fill: '#cdc3b0' }, h);
  el('rect', { x: 0, y: 338, width: PERIOD, height: 3, fill: '#e6ddcc' }, h);
  for (var x = 0; x < PERIOD; x += 50) {
    el('line', { x1: x, y1: 341, x2: x, y2: 356, stroke: '#a89f8c', 'stroke-width': 1 }, h);
  }
  el('rect', { x: 0, y: 316, width: PERIOD, height: 3, fill: '#fff' }, h);
  el('rect', { x: 0, y: 327, width: PERIOD, height: 2, fill: '#fff', opacity: .85 }, h);
  for (var p = 0; p < PERIOD; p += 40) {
    el('rect', { x: p - 1.5, y: 316, width: 3, height: 23, fill: '#fff' }, h);
    el('circle', { cx: p, cy: 315, r: 2.6, fill: '#fff' }, h);
  }
});

/* promenade ground (static base) */
el('rect', { x: -4000, y: 352, width: 9000, height: 2400, fill: 'url(#gPave)' }, world);
el('rect', { x: -4000, y: 374, width: 9000, height: 40, fill: '#cd8668' }, world);
el('rect', { x: -4000, y: 374, width: 9000, height: 2.5, fill: '#fff', opacity: .85 }, world);
el('rect', { x: -4000, y: 411.5, width: 9000, height: 2.5, fill: '#fff', opacity: .85 }, world);

/* lampposts */
layer(0.8, 91, function (h) {
  for (var i = 0; i < 4; i++) {
    var x = i * 400 + 150;
    var g = el('g', { transform: 'translate(' + x + ',0)' }, h);
    el('ellipse', { cx: 0, cy: 363, rx: 9, ry: 2.5, fill: 'rgba(0,0,0,.18)' }, g);
    el('rect', { x: -6, y: 354, width: 12, height: 9, rx: 2, fill: '#2f3a49' }, g);
    el('rect', { x: -2.5, y: 262, width: 5, height: 94, fill: '#3e4a5a' }, g);
    el('path', { d: 'M0,264 Q0,254 14,254', fill: 'none', stroke: '#3e4a5a', 'stroke-width': 4, 'stroke-linecap': 'round' }, g);
    el('circle', { cx: 16, cy: 261, r: 17, fill: '#fff6c0', opacity: .28 }, g);
    el('path', { d: 'M9,254 L23,254 L21,267 L11,267 Z', fill: '#fff4b8', stroke: '#2f3a49', 'stroke-width': 1.8, 'stroke-linejoin': 'round' }, g);
    el('path', { d: 'M7,254 L16,246 L25,254 Z', fill: '#2f3a49' }, g);
    if (i % 2 === 0) {
      el('path', { d: 'M-3,298 q-10,0 -10,8 l3,8 l14,0 l3,-8 q0,-8 -10,-8 z', fill: '#b5663a' }, g);
      el('circle', { cx: -14, cy: 295, r: 4, fill: '#ff6f91' }, g);
      el('circle', { cx: -7, cy: 292, r: 4, fill: '#ffd166' }, g);
      el('circle', { cx: -1, cy: 296, r: 4, fill: '#ff6f91' }, g);
    }
  }
});

/* ground markings (speed 1.0 = exact wheel rolling speed) */
layer(1.0, 101, function (h) {
  var x, i;
  for (x = 0; x < PERIOD; x += 80) {
    el('line', { x1: x, y1: 356, x2: x - 5, y2: 374, stroke: '#bfae8b', 'stroke-width': 1.2 }, h);
    el('line', { x1: x, y1: 414, x2: x - 16, y2: 520, stroke: '#bfae8b', 'stroke-width': 1.6 }, h);
  }
  for (x = 0; x < PERIOD; x += 100) {
    el('rect', { x: x, y: 392, width: 34, height: 3, rx: 1.5, fill: '#fff', opacity: .55 }, h);
  }
  for (i = 0; i < 12; i++) {
    el('circle', { cx: rand() * PERIOD, cy: 420 + rand() * 24, r: 1 + rand() * 1.4, fill: '#bfae8b', opacity: .7 }, h);
  }
});

/* ---------- the bike + pelican ---------- */
var bike = el('g', null, world);
el('ellipse', { cx: 0, cy: 2, rx: 98, ry: 6, fill: '#000', opacity: .22 }, bike);

function buildWheel(parent, c) {
  var g = el('g', null, parent);
  el('circle', { cx: c.x, cy: c.y, r: 43.5, fill: 'none', stroke: '#262a33', 'stroke-width': 5 }, g);
  el('circle', { cx: c.x, cy: c.y, r: 40, fill: 'none', stroke: '#b8bec9', 'stroke-width': 1.6 }, g);
  for (var i = 0; i < 12; i++) {
    var a = i * Math.PI / 6, a2 = a + 0.38, b = a + Math.PI / 12, b2 = b - 0.38;
    el('line', { x1: fx(c.x + 5 * Math.cos(a)), y1: fx(c.y + 5 * Math.sin(a)), x2: fx(c.x + 40 * Math.cos(a2)), y2: fx(c.y + 40 * Math.sin(a2)), stroke: '#8a909b', 'stroke-width': .9 }, g);
    el('line', { x1: fx(c.x + 5 * Math.cos(b)), y1: fx(c.y + 5 * Math.sin(b)), x2: fx(c.x + 40 * Math.cos(b2)), y2: fx(c.y + 40 * Math.sin(b2)), stroke: '#a3a9b4', 'stroke-width': .9 }, g);
  }
  el('line', { x1: fx(c.x + 37 * Math.cos(.7)), y1: fx(c.y + 37 * Math.sin(.7)), x2: fx(c.x + 44 * Math.cos(.7)), y2: fx(c.y + 44 * Math.sin(.7)), stroke: '#e5484d', 'stroke-width': 2.6, 'stroke-linecap': 'round' }, g);
  el('circle', { cx: fx(c.x + 41.5 * Math.cos(3.84)), cy: fx(c.y + 41.5 * Math.sin(3.84)), r: 2.2, fill: '#ffb020' }, g);
  el('circle', { cx: c.x, cy: c.y, r: 5, fill: '#6b7280' }, g);
  el('circle', { cx: c.x, cy: c.y, r: 2, fill: '#e5e7eb' }, g);
  return g;
}
var rearWheel = buildWheel(bike, RA);
var frontWheel = buildWheel(bike, FA);

/* far crank + pedal + leg */
var farCrank = el('line', { stroke: '#6b7280', 'stroke-width': 4, 'stroke-linecap': 'round' }, bike);
var farPedal = el('rect', { width: 12, height: 3, rx: 1, fill: '#3b4250' }, bike);
var farLeg = el('g', null, bike);
var farLegOut = el('path', { fill: 'none', stroke: '#a85a1c', 'stroke-width': 8, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, farLeg);
var farLegIn = el('path', { fill: 'none', stroke: '#d9822b', 'stroke-width': 5.4, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, farLeg);
var farFoot = el('g', null, farLeg);
function buildFoot(g, c1, c2) {
  el('path', { d: 'M-3,-1 L17,2 Q20,5 15,6.5 L12,9 Q6,10 -1,8 Q-5,5 -3,-1 Z', fill: c1, stroke: c2, 'stroke-width': .9, 'stroke-linejoin': 'round' }, g);
  el('path', { d: 'M2,2 L16,3.2 M1,4.6 L14,6.6 M0,7 L10,8.8', stroke: c2, 'stroke-width': .8, fill: 'none', 'stroke-linecap': 'round' }, g);
}
buildFoot(farFoot, '#e89238', '#a85a1c');

/* frame */
var frameCol = '#12a39f';
el('path', { d: 'M0,-38 L-20,-94 L50,-98 L55,-84 Z M0,-38 L-72,-46 L-20,-94', fill: 'none', stroke: frameCol, 'stroke-width': 5, 'stroke-linejoin': 'round', 'stroke-linecap': 'round' }, bike);
el('path', { d: 'M52,-92 L72,-46', stroke: '#9aa3af', 'stroke-width': 4, 'stroke-linecap': 'round' }, bike);
el('path', { d: 'M50,-99 L55,-83', stroke: '#0c7a77', 'stroke-width': 7, 'stroke-linecap': 'round' }, bike);
el('path', { d: 'M-20,-94 L-23,-105', stroke: '#c5cad2', 'stroke-width': 4, 'stroke-linecap': 'round' }, bike);
el('path', { d: 'M-37,-108 Q-35,-112 -22,-111 Q-10,-111 -5,-107 Q-9,-103 -22,-104 Q-34,-104 -37,-108 Z', fill: '#7a4b2a', stroke: '#4d2d17', 'stroke-width': 1 }, bike);
/* stem + handlebars */
el('path', { d: 'M50,-99 L47,-113 L50,-119 Q56,-121 61,-117', fill: 'none', stroke: '#4b5563', 'stroke-width': 3.4, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, bike);
el('line', { x1: 56, y1: -118, x2: 63, y2: -116, stroke: '#1f2937', 'stroke-width': 5, 'stroke-linecap': 'round' }, bike);

/* bell */
var bellG = el('g', { transform: 'translate(57,-108)' }, bike);
var bellInner = el('g', null, bellG);
el('path', { d: 'M-5.5,2 Q-5.5,-6 0,-6 Q5.5,-6 5.5,2 Z', fill: '#f5c542', stroke: '#a67c0f', 'stroke-width': .9 }, bellInner);
el('rect', { x: -6, y: 2, width: 12, height: 1.8, rx: .9, fill: '#d9a21f' }, bellInner);
el('circle', { cx: 0, cy: -7, r: 1.4, fill: '#a67c0f' }, bellInner);
var bellWaves = el('g', { opacity: 0, fill: 'none', stroke: '#f5a300', 'stroke-width': 1.6, 'stroke-linecap': 'round' }, bellG);
el('path', { d: 'M9,-6 Q12,-2 9,2' }, bellWaves);
el('path', { d: 'M13,-9 Q18,-2 13,5' }, bellWaves);
el('path', { d: 'M-9,-6 Q-12,-2 -9,2' }, bellWaves);
el('path', { d: 'M-13,-9 Q-18,-2 -13,5' }, bellWaves);

/* basket + fish */
var fish = el('g', { transform: 'translate(75,-111)' }, bike);
var fishInner = el('g', null, fish);
el('path', { d: 'M0,0 Q-3,-10 4,-17 L1,-22 L10,-19 L13,-24 L13,-13 Q10,-5 6,0 Z', fill: '#6fb6d9', stroke: '#3d7fa3', 'stroke-width': .8, 'stroke-linejoin': 'round' }, fishInner);
el('circle', { cx: 2.4, cy: -13, r: 1.2, fill: '#13293a' }, fishInner);
el('path', { d: 'M62,-111 L88,-111 L85,-95 Q75,-92 65,-95 Z', fill: '#c18a45', stroke: '#8a5a23', 'stroke-width': 1, 'stroke-linejoin': 'round' }, bike);
el('path', { d: 'M64,-105 L87,-105 M65,-100 L86,-100 M70,-111 L69,-94 M76,-111 L76,-93 M82,-111 L83,-94', stroke: '#8a5a23', 'stroke-width': .9, fill: 'none' }, bike);
el('path', { d: 'M56,-117 L63,-111 M61,-117 L87,-111', stroke: '#4b5563', 'stroke-width': 1.2, fill: 'none' }, bike);

/* chain */
var chainGeo = (function () {
  var dx = RA.x - BB.x, dy = RA.y - BB.y, d = Math.sqrt(dx * dx + dy * dy);
  var th = Math.atan2(dy, dx), ph = Math.acos((R1 - R2) / d);
  function pt(c, r, a) { return fx(c.x + r * Math.cos(a)) + ',' + fx(c.y + r * Math.sin(a)); }
  return 'M' + pt(BB, R1, th + ph) + ' L' + pt(RA, R2, th + ph) +
    ' A' + fx(R2) + ',' + fx(R2) + ' 0 0 0 ' + pt(RA, R2, th - ph) +
    ' L' + pt(BB, R1, th - ph) +
    ' A' + fx(R1) + ',' + fx(R1) + ' 0 1 0 ' + pt(BB, R1, th + ph) + ' Z';
})();
el('path', { d: chainGeo, fill: 'none', stroke: '#7b8492', 'stroke-width': 1.2 }, bike);
var chain = el('path', { d: chainGeo, fill: 'none', stroke: '#2d333d', 'stroke-width': 2.6, 'stroke-dasharray': fx(PITCH * .6) + ' ' + fx(PITCH * .4) }, bike);

/* chainring (rotates with crank) and rear sprocket (rotates with rear wheel) */
var ringG = el('g', null, bike);
el('circle', { cx: BB.x, cy: BB.y, r: fx(R1), fill: '#c9ced6', stroke: '#4b5563', 'stroke-width': 3, 'stroke-dasharray': fx(PITCH * .52) + ' ' + fx(PITCH * .48) }, ringG);
el('circle', { cx: BB.x, cy: BB.y, r: fx(R1 - 4.5), fill: 'none', stroke: '#8c94a1', 'stroke-width': 1 }, ringG);
for (var ra = 0; ra < 5; ra++) {
  var ang = ra * 2 * Math.PI / 5;
  el('line', { x1: BB.x, y1: BB.y, x2: fx(BB.x + (R1 - 3) * Math.cos(ang)), y2: fx(BB.y + (R1 - 3) * Math.sin(ang)), stroke: '#6b7280', 'stroke-width': 1.6 }, ringG);
}
var sprG = el('g', null, bike);
el('circle', { cx: RA.x, cy: RA.y, r: fx(R2), fill: '#c9ced6', stroke: '#4b5563', 'stroke-width': 3, 'stroke-dasharray': fx(PITCH * .52) + ' ' + fx(PITCH * .48) }, sprG);
el('line', { x1: RA.x - R2 + 2, y1: RA.y, x2: RA.x + R2 - 2, y2: RA.y, stroke: '#6b7280', 'stroke-width': 1.4 }, sprG);
el('circle', { cx: BB.x, cy: BB.y, r: 3.4, fill: '#374151' }, bike);

/* ---------- pelican ---------- */
var pel = el('g', null, bike);

function mkWing(parent, col) {
  var o = {};
  o.fan = el('path', { fill: col.fan, stroke: col.edge, 'stroke-width': 1, 'stroke-linejoin': 'round' }, parent);
  o.out = el('path', { fill: 'none', stroke: col.edge, 'stroke-width': 15, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, parent);
  o.arm = el('path', { fill: 'none', stroke: col.arm, 'stroke-width': 12, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, parent);
  o.tip = el('path', { fill: 'none', stroke: '#2f343f', 'stroke-width': 9, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, parent);
  return o;
}
var farWing = mkWing(pel, { fan: '#dde2e8', edge: '#b4bcc8', arm: '#e6eaef' });

/* tail */
el('path', { d: 'M-50,-138 L-80,-130 L-77,-121 L-47,-124 Z', fill: '#aab1bc', stroke: '#8d95a2', 'stroke-width': .8, 'stroke-linejoin': 'round' }, pel);
el('path', { d: 'M-76,-130 L-80,-130 L-77,-121 L-73,-122 Z', fill: '#353a45' }, pel);
/* body */
var bodyG = el('g', { transform: 'rotate(14 -22 -141)' }, pel);
el('ellipse', { cx: -22, cy: -141, rx: 36, ry: 28, fill: '#fdfcf7', stroke: '#cfd5dc', 'stroke-width': 2 }, bodyG);
el('ellipse', { cx: -30, cy: -146, rx: 22, ry: 13, fill: '#e1e6ec', stroke: '#c3cad3', 'stroke-width': 1 }, bodyG);
el('path', { d: 'M-46,-143 L-62,-141 L-60,-134 L-44,-137 Z', fill: '#353a45', stroke: '#222', 'stroke-width': .5, 'stroke-linejoin': 'round' }, bodyG);
el('path', { d: 'M-36,-147 q8,3 16,0 M-34,-142 q8,3 14,0', stroke: '#c3cad3', 'stroke-width': 1, fill: 'none', 'stroke-linecap': 'round' }, bodyG);

/* neck + scarf */
var neckOut = el('path', { fill: 'none', stroke: '#cfd5dc', 'stroke-width': 19, 'stroke-linecap': 'round' }, pel);
var neck = el('path', { fill: 'none', stroke: '#fdfcf7', 'stroke-width': 16, 'stroke-linecap': 'round' }, pel);
var scarfTail = el('path', { fill: 'none', stroke: '#e24a4a', 'stroke-width': 5, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, pel);
el('ellipse', { cx: -13, cy: -170, rx: 10.5, ry: 4.6, transform: 'rotate(-16 -13 -170)', fill: '#e24a4a', stroke: '#b12f2f', 'stroke-width': 1 }, pel);

/* head group: origin = head centre */
var headG = el('g', null, pel);
var pouch = el('path', { fill: '#f7bd80', stroke: '#d98c4a', 'stroke-width': 1.2, 'stroke-linejoin': 'round' }, headG);
var pouchVeins = el('path', { fill: 'none', stroke: '#e79c58', 'stroke-width': 1, 'stroke-linecap': 'round' }, headG);
el('path', { d: 'M6,-7 C30,-9 55,-4 76,2 Q81,4.5 76,8 L71,6 C52,3.5 28,3 6,3 Z', fill: '#f4b747', stroke: '#c9822b', 'stroke-width': 1.1, 'stroke-linejoin': 'round' }, headG);
el('path', { d: 'M12,-5.5 C32,-6.5 52,-3 70,1.5', fill: 'none', stroke: '#ffe29a', 'stroke-width': 1.6, 'stroke-linecap': 'round' }, headG);
el('circle', { cx: 75.5, cy: 4.5, r: 2.6, fill: '#e0602a' }, headG);
el('circle', { cx: 0, cy: 0, r: 12.5, fill: '#fdfcf7', stroke: '#cfd5dc', 'stroke-width': 1.2 }, headG);
el('path', { d: 'M-8,-9 Q-19,-14 -21,-4 Q-14,-7 -9,-1 Z', fill: '#fdf2c4', stroke: '#e3d28f', 'stroke-width': .8, 'stroke-linejoin': 'round' }, headG);
el('circle', { cx: 5, cy: -3, r: 5, fill: '#ffe27a' }, headG);
el('circle', { cx: 5.4, cy: -3, r: 3, fill: '#15171c' }, headG);
el('circle', { cx: 6.4, cy: -4.2, r: 1, fill: '#fff' }, headG);

/* near leg etc. (drawn in front of body) */
var nearCrank = el('line', { stroke: '#4b5563', 'stroke-width': 4.4, 'stroke-linecap': 'round' }, bike);
var nearPedal = el('rect', { width: 12, height: 3, rx: 1, fill: '#2a303b' }, bike);
var nearLeg = el('g', null, bike);
var nearLegOut = el('path', { fill: 'none', stroke: '#b9631f', 'stroke-width': 8.4, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, nearLeg);
var nearLegIn = el('path', { fill: 'none', stroke: '#f09a3a', 'stroke-width': 5.8, 'stroke-linecap': 'round', 'stroke-linejoin': 'round' }, nearLeg);
var nearFoot = el('g', null, nearLeg);
buildFoot(nearFoot, '#f6a23f', '#b9631f');
var thigh = el('g', null, bike);
el('circle', { cx: 0, cy: 0, r: 10.5, fill: '#fdfcf7', stroke: '#cfd5dc', 'stroke-width': 1.4 }, thigh);

/* near wing (in front of everything on the pelican) */
var pelFront = el('g', null, bike);
var nearWing = mkWing(pelFront, { fan: '#f6f6f1', edge: '#c9cfd8', arm: '#ffffff' });

/* speech bubble */
var bubble = el('g', { transform: 'translate(82,-168)', opacity: 0 }, bike);
el('path', { d: 'M12,22 L4,34 L26,22 Z', fill: '#fff', stroke: '#0f3d52', 'stroke-width': 1.5, 'stroke-linejoin': 'round' }, bubble);
el('rect', { x: 0, y: 0, width: 70, height: 23, rx: 11.5, fill: '#fff', stroke: '#0f3d52', 'stroke-width': 1.5 }, bubble);
el('rect', { x: 8, y: 20.5, width: 16, height: 4, fill: '#fff' }, bubble);
var bt = el('text', { x: 35, y: 16, 'text-anchor': 'middle', 'font-size': 12, 'font-weight': 700, fill: '#0f3d52', 'font-family': 'system-ui,-apple-system,Segoe UI,Roboto,sans-serif' }, bubble);
bt.textContent = 'ring ring!';

/* foreground (faster than the ground) */
layer(1.4, 111, function (h) {
  for (var i = 0; i < 4; i++) {
    var x = i * 400 + 60;
    var g = el('g', { transform: 'translate(' + x + ',0)' }, h);
    el('rect', { x: -8, y: 420, width: 16, height: 40, rx: 6, fill: '#4b5563' }, g);
    el('rect', { x: -8, y: 428, width: 16, height: 5, fill: '#ffd166' }, g);
    var px = x + 200;
    var g2 = el('g', { transform: 'translate(200,0)' }, g);
    el('path', { d: 'M-20,430 L20,430 L16,456 L-16,456 Z', fill: '#b5663a' }, g2);
    el('rect', { x: -22, y: 427, width: 44, height: 6, rx: 2, fill: '#c8784a' }, g2);
    var cols = ['#ff6f91', '#ffd166', '#ff9f6b', '#ff6f91', '#fff'];
    for (var k = 0; k < 5; k++) {
      el('circle', { cx: -14 + k * 7, cy: 423 - (k % 2) * 5, r: 5, fill: cols[k] }, g2);
      el('circle', { cx: -14 + k * 7, cy: 423 - (k % 2) * 5, r: 1.6, fill: '#f2b705' }, g2);
    }
    void px;
  }
});

/* ---------- maths: IK ---------- */
function ik(ax, ay, tx, ty, l1, l2, dir) {
  var dx = tx - ax, dy = ty - ay, d = Math.sqrt(dx * dx + dy * dy);
  var maxd = l1 + l2 - 0.05;
  if (d > maxd) { dx *= maxd / d; dy *= maxd / d; d = maxd; }
  if (d < 0.01) d = 0.01;
  var ux = dx / d, uy = dy / d;
  var a = (l1 * l1 - l2 * l2 + d * d) / (2 * d);
  var h = Math.sqrt(Math.max(0, l1 * l1 - a * a));
  return [ax + ux * a + uy * h * dir, ay + uy * a - ux * h * dir, ax + dx, ay + dy];
}
function setWing(o, sx, sy, tx, ty, fan) {
  var r = ik(sx, sy, tx, ty, 40, 44, 1);
  var ex = r[0], ey = r[1];
  tx = r[2]; ty = r[3];
  function q(t) {
    var a = (1 - t) * (1 - t), b = 2 * (1 - t) * t, c = t * t;
    return [a * sx + b * ex + c * tx, a * sy + b * ey + c * ty];
  }
  var ux = tx - sx, uy = ty - sy, l = Math.sqrt(ux * ux + uy * uy) || 1;
  ux /= l; uy /= l;
  var px = -uy, py = ux;
  if (px > 0) { px = -px; py = -py; }
  var m1 = q(0.3), m2 = q(0.55), m3 = q(0.8), t6 = q(0.62);
  var main = 'M' + fx(sx) + ',' + fx(sy) + ' Q' + fx(ex) + ',' + fx(ey) + ' ' + fx(tx) + ',' + fx(ty);
  o.out.setAttribute('d', main);
  o.arm.setAttribute('d', main);
  o.tip.setAttribute('d', 'M' + fx(t6[0]) + ',' + fx(t6[1]) + ' L' + fx(m3[0]) + ',' + fx(m3[1]) + ' L' + fx(tx) + ',' + fx(ty));
  o.fan.setAttribute('d', main +
    ' L' + fx(tx + px * fan * .3) + ',' + fx(ty + py * fan * .3) +
    ' L' + fx(m3[0] + px * fan * .95) + ',' + fx(m3[1] + py * fan * .95) +
    ' L' + fx(m2[0] + px * fan * 1.15) + ',' + fx(m2[1] + py * fan * 1.15) +
    ' L' + fx(m1[0] + px * fan) + ',' + fx(m1[1] + py * fan) +
    ' L' + fx(sx + px * fan * .4) + ',' + fx(sy + py * fan * .4) + ' Z');
}
function setLeg(out, inn, foot, ax, ay, hx, hy) {
  var r = ik(hx, hy, ax, ay, 48, 52, 1);
  var d = 'M' + fx(hx) + ',' + fx(hy) + ' L' + fx(r[0]) + ',' + fx(r[1]) + ' L' + fx(ax) + ',' + fx(ay);
  out.setAttribute('d', d);
  inn.setAttribute('d', d);
  foot.setAttribute('transform', 'translate(' + fx(ax) + ',' + fx(ay) + ')');
}

/* ---------- state ---------- */
var S = {
  v: 0, dist: 0, t: 0,
  flap: -1, ring: -1, pouch: 0, bubble: 0,
  q: 2, ready: false
};
var wheelAng0 = 0.3;

function flapK(p) {
  var u;
  if (p < 0.32) { u = p / 0.32; return 1 - (1 - u) * (1 - u); }
  if (p < 0.42) return 1;
  if (p < 0.72) { u = (p - 0.42) / 0.3; return 1 - 1.25 * (u * u * (3 - 2 * u)); }
  u = (p - 0.72) / 0.28;
  return -0.25 * (1 - u * u * (3 - 2 * u));
}

/* ---------- audio ---------- */
var actx = null, master = null, muted = false, noiseBuf = null;
function initAudio() {
  if (actx) { try { if (actx.state === 'suspended') actx.resume(); } catch (e) {} return; }
  try {
    var AC = window.AudioContext || window.webkitAudioContext;
    if (!AC) return;
    actx = new AC();
    master = actx.createGain();
    master.gain.value = muted ? 0 : 0.7;
    master.connect(actx.destination);
    if (actx.state === 'suspended') actx.resume();
  } catch (e) { actx = null; }
}
function strike(t, f) {
  var parts = [[1, 1], [2.76, 0.45], [5.4, 0.22], [8.9, 0.1]];
  parts.forEach(function (p) {
    var o = actx.createOscillator(), g = actx.createGain();
    o.type = 'sine';
    o.frequency.value = f * p[0];
    g.gain.setValueAtTime(0.0001, t);
    g.gain.exponentialRampToValueAtTime(0.32 * p[1], t + 0.004);
    g.gain.exponentialRampToValueAtTime(0.0001, t + 1.1 / Math.pow(p[0], 0.4));
    o.connect(g); g.connect(master);
    o.start(t); o.stop(t + 1.3);
  });
}
function bellSound() {
  if (!actx || muted) return;
  try {
    var t0 = actx.currentTime + 0.01;
    strike(t0, 1760);
    strike(t0 + 0.17, 1568);
  } catch (e) {}
}
function whoosh() {
  if (!actx || muted) return;
  try {
    if (!noiseBuf) {
      var r = mulberry(5);
      noiseBuf = actx.createBuffer(1, Math.floor(actx.sampleRate * 0.5), actx.sampleRate);
      var data = noiseBuf.getChannelData(0);
      for (var i = 0; i < data.length; i++) data[i] = r() * 2 - 1;
    }
    var src = actx.createBufferSource(); src.buffer = noiseBuf;
    var bp = actx.createBiquadFilter(); bp.type = 'bandpass'; bp.Q.value = 1.2;
    var g = actx.createGain();
    var t = actx.currentTime;
    bp.frequency.setValueAtTime(400, t);
    bp.frequency.exponentialRampToValueAtTime(1600, t + 0.3);
    g.gain.setValueAtTime(0.0001, t);
    g.gain.exponentialRampToValueAtTime(0.28, t + 0.12);
    g.gain.exponentialRampToValueAtTime(0.0001, t + 0.45);
    src.connect(bp); bp.connect(g); g.connect(master);
    src.start(t); src.stop(t + 0.5);
  } catch (e) {}
}
function setMuted(m) {
  muted = m;
  if (master) master.gain.value = m ? 0 : 0.7;
  bMute.innerHTML = m ? '&#128263; Muted' : '&#128266; Sound';
  bMute.setAttribute('aria-pressed', m ? 'true' : 'false');
  if (m) bMute.classList.add('on'); else bMute.classList.remove('on');
}

/* ---------- actions ---------- */
function hideHint() { hint.style.opacity = 0; }
function doFlap() {
  if (S.flap < 0 || S.flap > 0.75) {
    S.flap = 0;
    S.pouch = 1;
    whoosh();
  }
}
function doBell() {
  S.ring = 0;
  S.pouch = Math.max(S.pouch, 0.6);
  bellSound();
}
function doReset() {
  S.dist = 0; S.v = 0; S.flap = -1; S.ring = -1; S.pouch = 0;
  slider.value = 45; syncSpeed();
}
function syncSpeed() { spdOut.textContent = slider.value + '%'; }

slider.addEventListener('input', syncSpeed);
bFlap.addEventListener('click', function () { doFlap(); hideHint(); });
bBell.addEventListener('click', doBell);
bMute.addEventListener('click', function () { setMuted(!muted); });
bReset.addEventListener('click', doReset);
stage.addEventListener('pointerdown', function () { doFlap(); hideHint(); });
window.addEventListener('keydown', function (e) {
  var tag = document.activeElement && document.activeElement.tagName;
  if (tag === 'BUTTON' || tag === 'INPUT') return;
  var k = e.key;
  if (k === ' ' || k === 'Enter') { e.preventDefault(); doFlap(); hideHint(); }
  else if (k === 'b' || k === 'B') doBell();
  else if (k === 'm' || k === 'M') setMuted(!muted);
  else if (k === 'r' || k === 'R') doReset();
  else if (k === 'ArrowRight' || k === 'ArrowUp') { slider.value = Math.min(100, +slider.value + 5); syncSpeed(); }
  else if (k === 'ArrowLeft' || k === 'ArrowDown') { slider.value = Math.max(0, +slider.value - 5); syncSpeed(); }
});
/* audio unlock on first real interaction; any real input ends demo */
function realInput() { initAudio(); endDemo(); }
window.addEventListener('pointerdown', realInput, true);
window.addEventListener('keydown', realInput, true);
window.addEventListener('touchstart', function () { endDemo(); }, { capture: true, passive: true });
window.addEventListener('wheel', function () { endDemo(); }, { capture: true, passive: true });

/* ---------- layout ---------- */
function layout() {
  var r = stage.getBoundingClientRect();
  var w = Math.max(1, r.width), h = Math.max(1, r.height), asp = w / h;
  var vw, vh;
  if (asp >= 800 / 450) { vh = Math.max(330, 800 / asp); vw = vh * asp; }
  else { vh = 450; vw = Math.max(330, 450 * asp); }
  vw = Math.min(vw, 1500);
  var y0 = 450 - vh;
  svg.setAttribute('viewBox', '0 ' + fx(y0) + ' ' + fx(vw) + ' ' + fx(vh));
  bike.setAttribute('transform', 'translate(' + fx(vw / 2 - 8) + ',' + GROUND_Y + ')');
  sun.setAttribute('transform', 'translate(' + fx(vw * 0.78) + ',96)');
}
window.addEventListener('resize', layout);
if (window.ResizeObserver) { try { new ResizeObserver(layout).observe(stage); } catch (e) {} }

/* ---------- per-frame update ---------- */
var chainPeriod = PITCH;
function update(dt) {
  S.t += dt;
  var target = (+slider.value) / 100 * VMAX;
  S.v += (target - S.v) * (1 - Math.exp(-dt * 3));
  if (Math.abs(target - S.v) < 0.05) S.v = target;
  S.dist += S.v * dt;
  var t = S.t;

  /* synchronised rotations, all derived from rolled distance */
  var wheelA = (S.dist / WR + wheelAng0) % (Math.PI * 2);
  var crankA = ((S.dist / WR) * (N2 / N1) + 0.6) % (Math.PI * 2);
  var wDeg = wheelA * 180 / Math.PI, cDeg = crankA * 180 / Math.PI;
  rearWheel.setAttribute('transform', 'rotate(' + fx(wDeg) + ' ' + RA.x + ' ' + RA.y + ')');
  frontWheel.setAttribute('transform', 'rotate(' + fx(wDeg) + ' ' + FA.x + ' ' + FA.y + ')');
  sprG.setAttribute('transform', 'rotate(' + fx(wDeg) + ' ' + RA.x + ' ' + RA.y + ')');
  ringG.setAttribute('transform', 'rotate(' + fx(cDeg) + ' ' + BB.x + ' ' + BB.y + ')');
  chain.setAttribute('stroke-dashoffset', fx((R1 * ((S.dist / WR) * (N2 / N1))) % chainPeriod));

  /* scenery parallax */
  for (var i = 0; i < layers.length; i++) {
    var L = layers[i];
    var off = (S.dist * L.f + t * L.drift) % PERIOD;
    L.g.setAttribute('transform', 'translate(' + fx(-off) + ',0)');
  }

  /* flap / ring envelopes */
  var k = 0, hop = 0;
  if (S.flap >= 0) {
    S.flap += dt / 0.95;
    if (S.flap >= 1) S.flap = -1;
    else { k = flapK(S.flap); hop = -3.2 * Math.max(0, Math.sin(Math.PI * clamp((S.flap - 0.3) / 0.45, 0, 1))); }
  }
  var ringK = 0;
  if (S.ring >= 0) {
    S.ring += dt / 1.1;
    if (S.ring >= 1) S.ring = -1; else ringK = Math.sin(Math.PI * S.ring);
  }
  S.pouch = Math.max(0, S.pouch - dt * 1.2);

  var spd = clamp(S.v / VMAX, 0, 1);
  var bob = Math.sin(crankA * 2) * 1.2 * Math.min(1, S.v / 120) + Math.sin(t * 2.2) * 0.35 + hop;
  pel.setAttribute('transform', 'translate(0,' + fx(bob) + ')');
  thigh.setAttribute('transform', 'translate(' + fx(HIP.x) + ',' + fx(HIP.y + bob) + ')');

  /* cranks, pedals, legs */
  var nx = BB.x + CRANK * Math.cos(crankA), ny = BB.y + CRANK * Math.sin(crankA);
  var fxp = BB.x - CRANK * Math.cos(crankA), fyp = BB.y - CRANK * Math.sin(crankA);
  nearCrank.setAttribute('x1', BB.x); nearCrank.setAttribute('y1', BB.y);
  nearCrank.setAttribute('x2', fx(nx)); nearCrank.setAttribute('y2', fx(ny));
  farCrank.setAttribute('x1', BB.x); farCrank.setAttribute('y1', BB.y);
  farCrank.setAttribute('x2', fx(fxp)); farCrank.setAttribute('y2', fx(fyp));
  nearPedal.setAttribute('x', fx(nx - 6)); nearPedal.setAttribute('y', fx(ny - 1.5));
  farPedal.setAttribute('x', fx(fxp - 6)); farPedal.setAttribute('y', fx(fyp - 1.5));
  var hy = HIP.y + bob;
  setLeg(nearLegOut, nearLegIn, nearFoot, nx - 4, ny - 6, HIP.x, hy);
  setLeg(farLegOut, farLegIn, farFoot, fxp - 4, fyp - 6, HIP.x + 4, hy - 1);

  /* neck + head + pouch */
  var hx = 28 + 1.2 * Math.sin(crankA * 2 + 1) - k * 3;
  var hyy = -191 + 1.1 * Math.sin(crankA * 2 + 0.4) + Math.sin(t * 1.6) * 0.8 - Math.max(0, k) * 4;
  var nd = 'M0,-153 C-28,-165 -8,-195 ' + fx(hx - 2) + ',' + fx(hyy + 4);
  neck.setAttribute('d', nd);
  neckOut.setAttribute('d', nd);
  var tilt = 3 * Math.sin(t * 1.7) - Math.max(0, k) * 10 + ringK * 4;
  headG.setAttribute('transform', 'translate(' + fx(hx) + ',' + fx(hyy) + ') rotate(' + fx(tilt) + ')');
  var sag = 7 + 3 * Math.sin(t * 2.1) + spd * 2.5 * Math.sin(crankA * 2) + 17 * S.pouch * (1 + 0.3 * Math.sin(t * 18));
  pouch.setAttribute('d', 'M6,3 L71,6 C60,' + fx(6 + sag) + ' 32,' + fx(8 + sag) + ' 10,' + fx(12 + sag * 0.7) +
    ' Q1,' + fx(18 + sag * 0.5) + ' -2,' + fx(25 + sag * 0.3) + ' L-10,17 Q-6,9 -4,4 Z');
  pouchVeins.setAttribute('d', 'M16,5 Q32,' + fx(5 + sag * 0.7) + ' 54,6.5 M12,6 Q20,' + fx(8 + sag * 0.6) + ' 30,' + fx(8 + sag * 0.7));

  /* scarf */
  var amp = 0.5 + spd * 1.2, ph = t * 14;
  scarfTail.setAttribute('d', 'M-20,-168 Q-34,' + fx(-170 + 4 * amp * Math.sin(ph)) + ' -50,' + fx(-164 + 6 * amp * Math.sin(ph + 1.3)) +
    ' T-68,' + fx(-162 + 7 * amp * Math.sin(ph + 2.4)));

  /* wings: both tips hold the handlebars; near wing flaps once */
  var gx = GRIP.x, gy = GRIP.y - bob;
  var a0 = Math.atan2(gy - SHOULDER.y, gx - SHOULDER.x);
  var r0 = Math.sqrt((gx - SHOULDER.x) * (gx - SHOULDER.x) + (gy - SHOULDER.y) * (gy - SHOULDER.y));
  var ang = a0 + (-1.75 - a0) * k;
  var rr = r0 + (80 - r0) * Math.max(0, k);
  var tx = SHOULDER.x + rr * Math.cos(ang), ty = SHOULDER.y + rr * Math.sin(ang);
  if (S.flap < 0) { tx = gx; ty = gy; }
  var fan = 9 + 26 * Math.max(0, k) + 10 * Math.max(0, -k);
  setWing(nearWing, SHOULDER.x, SHOULDER.y + bob, tx, ty + (S.flap < 0 ? bob : bob), fan);
  setWing(farWing, SHOULDER.x - 6, SHOULDER.y - 2, gx + 4, gy - 1, 8);

  /* bell, bubble, fish */
  bellInner.setAttribute('transform', 'rotate(' + fx(Math.sin(t * 40) * 14 * ringK) + ' 0 -6)');
  bellWaves.setAttribute('opacity', fx(ringK));
  bubble.setAttribute('opacity', fx(clamp(ringK * 2.2, 0, 1)));
  fishInner.setAttribute('transform', 'rotate(' + fx(Math.sin(t * 5 + spd * 3) * (4 + spd * 8)) + ' 3 0)');

  /* gulls and boats (cheaper at low quality) */
  if (S.q >= 2) {
    for (var gI = 0; gI < gulls.length; gI++) {
      var w = Math.sin(t * 6 + gulls[gI].ph) * 4;
      gulls[gI].p.setAttribute('d', 'M-9,' + fx(-w) + ' Q-4,' + fx(-4 - w * 0.4) + ' 0,0 Q4,' + fx(-4 - w * 0.4) + ' 9,' + fx(-w));
    }
  }
  if (S.q >= 1) {
    for (var bI = 0; bI < boats.length; bI++) {
      var b = boats[bI];
      b.b.setAttribute('transform', 'translate(0,' + fx(Math.sin(t * 1.3 + b.ph) * 1.6) + ') rotate(' + fx(Math.sin(t * 1.1 + b.ph) * 2) + ')');
    }
  }
}

function applyQuality() {
  var showFar = S.q >= 2;
  wavesFar.style.display = showFar ? '' : 'none';
  wavesNear.style.display = showFar ? '' : 'none';
  gullLayer.style.display = showFar ? '' : 'none';
}

/* ---------- demo mode ---------- */
var dp = $('dp'), badge = $('demoBadge');
var demo = { on: false, t: 0, idx: 1 };
function sliderPt(v) {
  var r = slider.getBoundingClientRect();
  return { x: r.left + 16 + (r.width - 32) * v / 100, y: r.top + r.height / 2 };
}
function ctr(e) { var r = e.getBoundingClientRect(); return { x: r.left + r.width / 2, y: r.top + r.height / 2 }; }
function stagePt(a, b) { var r = stage.getBoundingClientRect(); return { x: r.left + r.width * a, y: r.top + r.height * b }; }
var DEMO_LEN = 20;
var WP = [
  { t: 0, p: function () { return stagePt(0.5, 0.3); } },
  { t: 1.2, p: function () { return sliderPt(30); }, s: 30 },
  { t: 4.2, p: function () { return sliderPt(85); }, s: 85, drag: true },
  { t: 5.4, p: function () { return ctr(bBell); }, act: doBell },
  { t: 7.2, p: function () { return stagePt(0.72, 0.4); }, act: doFlap },
  { t: 9.2, p: function () { return stagePt(0.3, 0.35); }, act: doFlap },
  { t: 10.8, p: function () { return sliderPt(85); }, s: 85 },
  { t: 13.4, p: function () { return sliderPt(15); }, s: 15, drag: true },
  { t: 14.8, p: function () { return ctr(bBell); }, act: doBell },
  { t: 16.6, p: function () { return stagePt(0.6, 0.45); }, act: doFlap },
  { t: 18.2, p: function () { return sliderPt(15); }, s: 15 },
  { t: 19.5, p: function () { return sliderPt(30); }, s: 30, drag: true },
  { t: 20, p: function () { return stagePt(0.5, 0.3); } }
];
function startDemo() {
  demo.on = true; demo.t = 0; demo.idx = 1;
  slider.value = 30; syncSpeed();
  dp.style.display = 'block'; badge.style.display = 'block';
  hideHint();
}
function endDemo() {
  if (!demo.on) return;
  demo.on = false;
  dp.style.display = 'none'; badge.style.display = 'none';
}
function press() {
  dp.classList.add('press');
  setTimeout(function () { dp.classList.remove('press'); }, 220);
}
function updateDemo(dt) {
  if (!demo.on) return;
  demo.t += dt;
  if (demo.t >= DEMO_LEN) {
    demo.t -= DEMO_LEN; demo.idx = 1;
    slider.value = 30; syncSpeed();
  }
  while (demo.idx < WP.length - 1 && demo.t >= WP[demo.idx].t) {
    if (WP[demo.idx].act) { press(); WP[demo.idx].act(); }
    demo.idx++;
  }
  var b = WP[demo.idx], a = WP[demo.idx - 1];
  var u = clamp((demo.t - a.t) / (b.t - a.t), 0, 1);
  var e = u < 0.5 ? 2 * u * u : 1 - 2 * (1 - u) * (1 - u);
  var pa = a.p(), pb = b.p();
  dp.style.left = fx(lerp(pa.x, pb.x, e)) + 'px';
  dp.style.top = fx(lerp(pa.y, pb.y, e)) + 'px';
  if (b.drag) {
    slider.value = Math.round(lerp(a.s, b.s, e)); syncSpeed();
    dp.classList.add('press');
  } else if (!(WP[demo.idx - 1] && WP[demo.idx - 1].act)) {
    dp.classList.remove('press');
  }
}

/* ---------- main loop ---------- */
var last = 0, fpsAcc = 0, fpsN = 0, firstDone = false;
function frame(ts) {
  var now = ts / 1000;
  var dt = last ? Math.min(0.05, Math.max(0.001, now - last)) : 0.016;
  last = now;
  updateDemo(dt);
  update(dt);

  fpsAcc += dt; fpsN++;
  if (fpsN >= 90) {
    var avg = fpsAcc / fpsN;
    if (avg > 1 / 36 && S.q > 0) { S.q--; applyQuality(); }
    fpsAcc = 0; fpsN = 0;
  }

  if (!firstDone) {
    firstDone = true;
    try { parent.postMessage({ vcb: 'ready' }, '*'); } catch (e) {}
  }
  requestAnimationFrame(frame);
}

/* ---------- init ---------- */
layout();
syncSpeed();
setMuted(false);
applyQuality();
S.v = (+slider.value) / 100 * VMAX;
update(0.016);
if (location.href.slice(-5) === '#demo' || location.hash === '#demo') startDemo();
requestAnimationFrame(frame);
})();
</script>
</body>
</html>