Skip to content
VibeCodeBench
Back to the app

The code

Night Courier by GPT-6.2 Astra

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

index.html

works · auto-checked (Checked by an automated pass. Not hand-played yet.)88.6 KB · 1,228 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">
<link rel="icon" href="data:,">
<meta name="theme-color" content="#192e36">
<title>Whisker Air · Sunset fish deliveries</title>
<style>
:root{color-scheme:dark;--ink:#192e36;--deep:#13272e;--cream:#fff0d3;--gold:#ffbd77;--muted:#afc0bc;--edge:#3c5156;--mono:ui-monospace,"Cascadia Mono","SFMono-Regular",Consolas,monospace}
*{box-sizing:border-box}
html,body{margin:0;width:100%;height:100%;overflow:hidden;background:var(--ink);font-family:"Trebuchet MS",Arial,sans-serif;color:var(--cream);overscroll-behavior:none}
button{font:inherit;color:inherit;cursor:pointer;-webkit-tap-highlight-color:transparent;touch-action:manipulation}
button:focus-visible{outline:3px solid #fff2c8;outline-offset:3px}
button:active{transform:translateY(1px)}
svg{display:block}
.icons{position:absolute;width:0;height:0;overflow:hidden}
.icon{width:22px;height:22px;fill:none;stroke:currentColor;stroke-width:1.65;stroke-linecap:round;stroke-linejoin:round;flex:none}
.app{height:100vh;height:100dvh;display:grid;grid-template-rows:76px minmax(0,1fr) 79px;isolation:isolate}
.top,.controls{display:flex;align-items:center;padding:0 clamp(16px,3.2vw,60px);background:var(--ink);position:relative;z-index:2}
.top{gap:clamp(14px,2.4vw,44px);border-bottom:2px solid #da9364;box-shadow:0 3px 16px #26233128}
.brand{display:flex;align-items:center;gap:12px;margin-right:auto;white-space:nowrap}
.brandmark{width:73px;height:51px;color:var(--gold)}
.wordmark{font-family:Georgia,"Times New Roman",serif;font-size:clamp(27px,2.6vw,39px);font-weight:700;font-style:italic;line-height:1.05;letter-spacing:-1.8px}
.brand small,.eyebrow,.stat-label,.cam-label,.post-kicker{display:block;font:700 10px/1.2 var(--mono);letter-spacing:1.5px;text-transform:uppercase}
.brand small{font-size:8.5px;letter-spacing:2px;margin-top:4px;color:#e4b381}
.stats{display:flex;align-items:center;gap:clamp(17px,2.8vw,50px)}
.stat{display:flex;align-items:center;gap:10px;white-space:nowrap}
.stat>.icon{color:#ebaf79;width:23px;height:23px}
.stat-label{font-size:9px;letter-spacing:1.3px;color:#b6c3bd;margin-bottom:3px}
.stat-value{font:700 22px/1.08 var(--mono);letter-spacing:.6px;font-variant-numeric:tabular-nums}
.stat-value small{font-size:13px;letter-spacing:0;color:#8fa6a4}
.light-line{display:flex;align-items:center;gap:10px}
.light-track{width:clamp(44px,6vw,98px);height:5px;background:#405158;overflow:hidden;border-radius:4px}
.light-fill{display:block;width:100%;height:100%;border-radius:inherit;background:linear-gradient(90deg,#e98159,#ffd991);transform-origin:left}
#timeLeft{font:700 16px/1.4 var(--mono)}
.tools{display:flex;gap:5px;align-items:center;padding-left:clamp(8px,1.6vw,23px);border-left:1px solid var(--edge)}
.tool{width:43px;height:44px;background:transparent;border:1px solid transparent;border-radius:9px;display:grid;place-items:center;color:#e6e7d8}
.tool:hover,.tool[aria-pressed="true"]{background:#2b424a}
.scene{position:relative;min-height:0;overflow:hidden;isolation:isolate;background:linear-gradient(#dd9b99,#ffd599 41%,#b19091 47%,#679098 63%,#557e8a);touch-action:none;user-select:none}
#world{display:block;width:100%;height:100%;touch-action:none;outline:none}
.scene:after{content:"";position:absolute;inset:0;pointer-events:none;z-index:1;box-shadow:inset 0 -36px 54px -46px #302e46aa,inset 0 22px 32px -32px #6c4f5c70}
.dispatch{position:absolute;z-index:2;left:clamp(15px,2.8vw,48px);top:clamp(16px,3.1vh,35px);background:#fff0d5;color:#293a3b;min-width:205px;padding:14px 18px 13px 23px;border:1px solid #fff1d6;border-radius:5px 12px 12px 5px;box-shadow:0 4px 0 #624d3f15,0 8px 25px #372b3924;pointer-events:none;overflow:hidden}
.dispatch:before{content:"";position:absolute;inset:0 auto 0 0;width:5px;background:repeating-linear-gradient(-42deg,#c8795d 0 8px,#f5dfba 8px 15px,#617a7e 15px 23px,#f5dfba 23px 30px)}
.dispatch-head{display:flex;gap:18px;align-items:center;justify-content:space-between}
.dispatch .eyebrow{font-size:9px;letter-spacing:1.45px;color:#6c6250}
.dispatch .serial{font:700 10px var(--mono);color:#93644a}
.dispatch-name{font:700 clamp(20px,1.65vw,26px)/1.15 Georgia,serif;letter-spacing:-.7px;margin:5px 0 9px}
.legs{display:flex;gap:4px}
.legs i{width:22px;height:3px;border-radius:3px;background:#d9c7a9}
.legs i.here{background:#c98050}.legs i.done{background:#5e8c80}
.mapframe{position:absolute;bottom:clamp(15px,2.8vh,32px);right:clamp(15px,2.6vw,48px);width:clamp(100px,10.6vw,150px);aspect-ratio:1;border-radius:50%;border:2px solid #fae6b9;background:#223e49e8;box-shadow:0 2px 0 2px #263e46aa,0 4px 20px #232d3b3d;z-index:2;pointer-events:none}
#chart{width:100%;height:100%;display:block;border-radius:50%}
.instruments{position:absolute;bottom:clamp(19px,3.1vh,34px);left:clamp(15px,2.8vw,48px);display:flex;align-items:center;gap:13px;background:#1c343ddc;border:1px solid #efe5ca55;border-radius:7px;padding:8px 12px;color:#f8eacf;z-index:2;pointer-events:none}
.instrument{display:flex;align-items:center;gap:6px;font:600 11px/1 var(--mono)}
.instrument .icon{width:16px;height:16px;color:#f7ba7b}.instrument em{font-size:9px;color:#c9d4c9;font-style:normal}.inst-divider{height:14px;width:1px;background:#adc9c647}
.waypoint{position:absolute;pointer-events:none;z-index:2;display:flex;align-items:center;gap:5px;background:#263b3edd;color:#fff0ba;border:1px solid #f1cf8c;border-radius:5px;padding:3px 7px;font:700 10px var(--mono);white-space:nowrap;transform:translate(-50%,-100%);box-shadow:0 2px 6px #463d4720}
.waypoint:after{content:"";position:absolute;left:calc(50% - 3px);bottom:-4px;width:5px;height:5px;transform:rotate(45deg);background:#304143;border-right:1px solid #f1cf8c;border-bottom:1px solid #f1cf8c}
.waypoint .icon{width:13px;height:13px}
.notice{position:absolute;z-index:4;top:12px;left:50%;transform:translate(-50%,-8px);opacity:0;transition:opacity .2s,transform .25s;background:#20363de8;border:1px solid #f6d599aa;padding:9px 15px;border-radius:20px;color:#fff0cb;font:700 13px/1.25 "Trebuchet MS",sans-serif;pointer-events:none;text-align:center}
.notice.show{opacity:1;transform:translate(-50%,0)}
.parcel{position:absolute;z-index:3;bottom:64px;left:clamp(15px,2.8vw,48px);width:clamp(165px,18vw,248px);border:2px solid #f6dfb7;border-radius:8px;box-shadow:0 4px 16px #222f3b55;overflow:hidden;pointer-events:none}
.parcel-head{display:flex;align-items:center;gap:7px;padding:7px 9px;background:#20363e;color:#ffdb9f;font:700 9px var(--mono);letter-spacing:1.2px}
.parcel-head i{width:6px;height:6px;border-radius:50%;background:#fab46d}
#parcelWindow{width:100%;aspect-ratio:1.85}
.demoflag{position:absolute;top:18px;right:clamp(15px,2.6vw,48px);background:#20363dd9;border:1px solid #f5d598aa;border-radius:20px;color:#ffe7bc;font:600 11px var(--mono);letter-spacing:.3px;display:flex;align-items:center;gap:7px;padding:9px 12px;pointer-events:none;z-index:3}
.demoflag i{width:6px;height:6px;border-radius:50%;background:#ffb875}
.controls{justify-content:space-between;gap:18px;border-top:2px solid #d09a70;box-shadow:0 -3px 15px #3a353b19}
.camera-bar,.camera-buttons,.help{display:flex;align-items:center}
.camera-bar{gap:17px}
.cam-label{font-size:9px;color:#acbbb7;letter-spacing:1.7px}
.camera-buttons{gap:7px}
.cam{height:44px;padding:0 clamp(11px,1.5vw,23px);display:flex;align-items:center;justify-content:center;gap:8px;border:1px solid #4b5c5f;border-radius:8px;background:#253a42;font-weight:bold;font-size:14px;color:#ece6d7;transition:background .15s}
.cam .icon{width:20px;height:20px}
.cam[aria-pressed="true"]{background:#ffc387;color:#27353a;border-color:#ffd4a1;box-shadow:inset 0 -2px 0 #c48b57}
.cam:hover:not([aria-pressed="true"]){background:#344950}
.help{gap:11px}.help>.icon{color:#edbc85;width:25px;height:29px}.help strong,.help small{display:block}.help strong{font-size:13px;font-weight:700;color:#f0e7d5}.help small{font-size:11px;color:#b1c0bd;margin-top:3px}
.touch-help{display:none}
.modal{position:absolute;z-index:8;inset:0;background:#13273468;display:grid;place-items:center;padding:12px;backdrop-filter:blur(3px)}
.ticket{color:#263b3e;background:#fff0d5;border:1px solid #fff6e3;width:min(355px,100%);border-radius:16px;padding:24px clamp(22px,3vw,36px);text-align:center;box-shadow:0 16px 60px #10293160;position:relative;overflow:hidden}
.ticket:before{content:"";height:5px;position:absolute;inset:0 0 auto;background:repeating-linear-gradient(-45deg,#c5775c 0 12px,#fff0d5 12px 24px,#567378 24px 36px,#fff0d5 36px 48px)}
.ticket-mark{width:81px;height:47px;margin:0 auto 6px;color:#b96d4c}.post-kicker{font-size:9px;color:#906c50}
.ticket h1{font:700 italic 36px/1.05 Georgia,serif;margin:8px 0 10px;letter-spacing:-1.6px}
.ticket p{color:#52605b;font-size:14px;line-height:1.45;margin:5px 0 15px}
.receipt{display:flex;justify-content:space-around;gap:15px;border-block:1px dashed #baaa91;margin:16px 0;padding:11px 0}
.receipt span{display:block;font:700 9px var(--mono);letter-spacing:1px;text-transform:uppercase;color:#716d5d}
.receipt b{display:block;font:700 22px var(--mono);margin-top:4px}
.play{background:#233d43;color:#fff0ce;border:0;border-radius:8px;min-height:45px;width:100%;font-weight:bold;display:flex;align-items:center;justify-content:center;gap:10px}
[hidden]{display:none!important}
@media(pointer:coarse){.mouse-help{display:none!important}.touch-help{display:block}}
@media(max-width:900px){.top,.controls{padding-inline:18px}.stats{gap:20px}.brandmark{width:58px}.brand{gap:8px}.top{gap:15px}.tools{padding-left:8px}.cam-label{display:none}.light-track{width:47px}.controls{gap:12px}.help strong{font-size:12px}}
@media(max-width:680px) and (min-height:501px){.app{grid-template-rows:99px minmax(0,1fr) calc(63px + env(safe-area-inset-bottom))}.top{display:grid;grid-template-columns:1fr auto;grid-template-rows:52px 43px;gap:0;padding-inline:15px}.brandmark{width:47px;height:39px}.brand{gap:7px}.brand small{font-size:7px;letter-spacing:1.5px;margin-top:1px}.wordmark{font-size:25px}.tools{grid-column:2;grid-row:1;border:0;padding:0}.stats{grid-row:2;grid-column:1/-1;border-top:1px solid #374b50;justify-content:space-between;gap:6px;padding-top:5px}.stat{gap:7px}.stat>.icon{width:19px;height:19px}.stat-label{font-size:7.5px;letter-spacing:1px;margin-bottom:1px}.stat-value{font-size:17px}.stat-value small{font-size:10px}#timeLeft{font-size:14px}.light-track{width:clamp(23px,9vw,64px)}.light-line{gap:6px}.controls{padding:0 12px env(safe-area-inset-bottom)}.camera-bar,.camera-buttons{width:100%}.camera-buttons{gap:6px}.cam{flex:1;padding:0 6px;font-size:13px;gap:5px}.controls .help{display:none}.dispatch{left:12px;top:12px;padding:10px 12px 10px 16px;min-width:158px}.dispatch-name{font-size:19px;margin:4px 0 6px}.dispatch .eyebrow{font-size:8px}.dispatch .serial{font-size:9px}.legs i{width:17px}.mapframe{width:83px;right:12px;bottom:12px}.instruments{left:12px;bottom:12px;padding:6px 9px;gap:7px}.parcel{left:12px;bottom:47px;width:145px}.notice{top:91px;font-size:11px;padding:7px 12px;white-space:nowrap}.demoflag{font-size:9px;padding:6px 8px;top:13px;right:10px}.tool{width:44px}.mobile-hint:not([hidden]){display:block!important}}
.mobile-hint{position:absolute;left:13px;bottom:47px;font:600 10px var(--mono);color:#fff3d6;text-shadow:0 1px 4px #152a34,0 0 8px #1f343a;pointer-events:none;z-index:2;display:none}
@media(max-height:500px){.app{grid-template-rows:46px minmax(0,1fr) 49px}.brandmark{width:48px;height:35px}.brand small{display:none}.wordmark{font-size:25px}.stat>.icon{width:17px}.stat-value{font-size:15px}.stat-label{font-size:7px;margin-bottom:0}.top{gap:10px}.stats{gap:15px}.controls,.top{padding-inline:12px}.tool{height:42px;width:41px}.cam{height:42px;font-size:12px;padding:0 12px}.help small{display:none}.dispatch{top:8px;left:10px;min-width:141px;padding:8px 12px 8px 15px}.dispatch-name{font-size:17px;margin:2px 0 4px}.dispatch .eyebrow{font-size:7px}.mapframe{width:70px;bottom:8px;right:10px}.instruments{bottom:8px;left:10px;padding:4px 8px}.parcel{left:10px;bottom:38px;width:121px}.parcel-head{padding:3px 5px;font-size:7px}.ticket{padding:13px 22px}.ticket-mark{display:none}.ticket h1{font-size:27px}.ticket p{margin:4px 0 8px}.receipt{margin:8px 0;padding:5px}.notice{padding:6px 10px;font-size:11px}.brand{gap:3px}.demoflag{top:8px}}
@media(max-width:500px) and (max-height:500px){.brandmark,.help,.light-track,.tools #soundButton{display:none}.stat>.icon{display:none}.stats{gap:10px}.wordmark{font-size:20px}.top,.controls{padding-inline:6px}.camera-bar,.camera-buttons{width:100%}.cam{flex:1}.cam-label{display:none}}
</style>
</head>
<body>
<svg class="icons" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><defs>
<symbol id="badge" viewBox="0 0 92 58">
<g fill="currentColor"><path d="M2 17q13 3 25 2l5 15-13-2-5-5 12 2-2-3-13-2zM90 17q-13 3-25 2l-5 15 13-2 5-5-12 2 2-3 13-2z"/><path d="M36 44l10 9 10-9-10 3z"/></g>
<path d="M28 21 30 4 40 12Q46 9 53 12L64 4l1 19q6 20-18 25-23-4-19-27Z" fill="#ef9c64" stroke="#222f35" stroke-width="2.3" stroke-linejoin="round"/>
<path d="m32 10 1 10 5-6zm29 0-7 5 6 5z" fill="#9e5748"/><path d="m41 13 3 7 2-8 2 8 3-7" fill="#a45e3f"/>
<path d="M28 24q18-7 37 0" fill="none" stroke="#293b3c" stroke-width="4"/>
<g stroke="#293b3c" stroke-width="2.8" fill="#f6d399"><path d="M31 22q5-3 12-1l1 5q-2 6-8 4-5-1-5-8Zm19-1q6-2 12 1-1 9-7 9-6 0-6-6Z"/><path d="M44 24q2-2 5 0" fill="none"/></g>
<path d="m33 25 5-2m14 2 5-2" stroke="#fff0c9" stroke-width="2" stroke-linecap="round"/><path d="M33 32q4 2 7 0 5-3 7 1 4-4 7 0 3 2 7-1-4 10-14 12-11-2-14-12Z" fill="#ffe4b3"/><path d="m43 33 7 0-4 4z" fill="#493e3b"/><path d="M46 36q0 5-4 3m4-3q0 5 4 3" fill="none" stroke="#604c43" stroke-width="1.2" stroke-linecap="round"/>
</symbol>
<symbol id="i-fish" viewBox="0 0 24 24"><path d="M3 12s5-7 11-6q4 .6 6 6-2 5-6 6-6 1-11-6Z"/><path d="m4 11-3-4v10l3-4m9-7-2-3 6 3m-2 1q-3 5 0 10"/><circle cx="17" cy="11" r=".85" fill="currentColor" stroke="none"/></symbol>
<symbol id="i-box" viewBox="0 0 24 24"><path d="m3 7 9-4 9 4v11l-9 4-9-4zm0 0 9 4 9-4m-9 4v11M7.5 5l9 4v4"/></symbol>
<symbol id="i-sun" viewBox="0 0 24 24"><path d="M3 17h18M6 20h12M6.5 14a5.5 5.5 0 1 1 11 0M12 2v3m-9 5 3 1m12 0 3-1M5 5l2 2m12-2-2 2"/></symbol>
<symbol id="i-mute" viewBox="0 0 24 24"><path d="M3 9h4l5-5v16l-5-5H3zM17 9l5 6m0-6-5 6"/></symbol>
<symbol id="i-sound" viewBox="0 0 24 24"><path d="M3 9h4l5-5v16l-5-5H3zM16 8q4 4 0 8m3-11q7 7 0 14"/></symbol>
<symbol id="i-pause" viewBox="0 0 24 24"><path d="M8 5v14m8-14v14" stroke-width="3"/></symbol>
<symbol id="i-play" viewBox="0 0 24 24"><path d="m7 4 13 8-13 8z" fill="currentColor" stroke="none"/></symbol>
<symbol id="i-behind" viewBox="0 0 24 24"><path d="m2 13 8-1 1-6h2l1 6 8 1v2l-8-1-2 4-2-4-8 1z"/><path d="M9 20h6"/></symbol>
<symbol id="i-cockpit" viewBox="0 0 24 24"><path d="M2 18v-5a10 10 0 0 1 20 0v5zM6 12l1 1m5-6v2m6 3-1 1m-5 2 3-4"/><circle cx="12" cy="15" r="1.3"/></symbol>
<symbol id="i-above" viewBox="0 0 24 24"><path d="M12 2q2 0 2 7l8 5v2l-8-2-1 5 3 2v1l-4-1-4 1v-1l3-2-1-5-8 2v-2l8-5q0-7 2-7Z"/></symbol>
<symbol id="i-pointer" viewBox="0 0 24 26"><path d="m4 3 15 11-7 .8 3.5 7-3.5 1.7-3.4-7L4 21z"/></symbol>
<symbol id="i-alt" viewBox="0 0 24 24"><path d="m2 19 8-14 5 9 3-5 5 10zM7 10l3 2 2-3"/></symbol>
<symbol id="i-speed" viewBox="0 0 24 24"><path d="M3 18a10 10 0 1 1 18 0M12 15l5-6M6 12h1m4-6v1m7 7h1"/><circle cx="12" cy="16" r="1"/></symbol>
<symbol id="i-again" viewBox="0 0 24 24"><path d="M4 10a8 8 0 1 1 1 8M4 4v6h6"/></symbol>
</defs></svg>
<div class="app">
<header class="top">
<div class="brand"><svg class="brandmark" aria-hidden="true"><use href="#badge"/></svg><div><div class="wordmark">Whisker Air</div><small>Fish delivery service</small></div></div>
<div class="stats">
<div class="stat"><svg class="icon"><use href="#i-fish"/></svg><div><span class="stat-label">Score</span><span class="stat-value" id="score">0000</span></div></div>
<div class="stat"><svg class="icon"><use href="#i-box"/></svg><div><span class="stat-label">Crates left</span><span class="stat-value"><span id="stock">5</span><small> / 5</small></span></div></div>
<div class="stat"><svg class="icon"><use href="#i-sun"/></svg><div><span class="stat-label">Daylight</span><div class="light-line"><div class="light-track"><i class="light-fill" id="daybar"></i></div><span id="timeLeft">1:30</span></div></div></div>
</div>
<div class="tools"><button class="tool" id="soundButton" aria-label="Turn sound on" title="Turn sound on" aria-pressed="false"><svg class="icon"><use id="soundGlyph" href="#i-mute"/></svg></button><button class="tool" id="pauseButton" aria-label="Pause flight" title="Pause flight"><svg class="icon"><use id="pauseGlyph" href="#i-pause"/></svg></button></div>
</header>
<main class="scene" id="scene" aria-label="Flying over Bramble Lake at sunset">
<canvas id="world" tabindex="0" aria-label="Flight view. Move your pointer or drag one finger to steer through the golden delivery rings."></canvas>
<div class="dispatch"><div class="dispatch-head"><span class="eyebrow" id="dispatchLabel">Next delivery</span><span class="serial" id="serial">01 / 05</span></div><div class="dispatch-name" id="place">Lighthouse Pier</div><div class="legs" id="legs"><i class="here"></i><i></i><i></i><i></i><i></i></div></div>
<div class="waypoint" id="waypoint" hidden><svg class="icon"><use href="#i-box"/></svg><span id="distance"></span></div>
<div class="mapframe" aria-label="Route map"><canvas id="chart" width="280" height="280"></canvas></div>
<div class="instruments"><div class="instrument"><svg class="icon"><use href="#i-alt"/></svg><span id="alt">17</span><em>m</em></div><i class="inst-divider"></i><div class="instrument"><svg class="icon"><use href="#i-speed"/></svg><span id="speed">38</span><em>km/h</em></div></div>
<div class="mobile-hint" id="mobileHint">Drag to steer through the rings</div>
<div class="parcel" id="parcelCam" hidden><div class="parcel-head"><i></i><span id="parcelTitle">PARCEL CAM</span></div><div id="parcelWindow"></div></div>
<div class="demoflag" id="demoFlag" hidden><i></i>Demo · tap to fly</div>
<div class="notice" id="notice" role="status"></div>
<div class="modal" id="modal" hidden><section class="ticket" role="dialog" aria-modal="true" aria-labelledby="modalTitle"><svg class="ticket-mark" aria-hidden="true"><use href="#badge"/></svg><span class="post-kicker">Whisker Air · Bramble Lake</span><h1 id="modalTitle">Route complete!</h1><p id="modalCopy"></p><div class="receipt" id="receipt"><div><span>Delivered</span><b id="finalCount">5 / 5</b></div><div><span>Score</span><b id="finalScore">0500</b></div></div><button class="play" id="playButton"><svg class="icon"><use id="playGlyph" href="#i-again"/></svg><span id="playText">Fly again</span></button></section></div>
</main>
<footer class="controls"><div class="camera-bar"><span class="cam-label">Camera</span><div class="camera-buttons" role="group" aria-label="Camera view"><button class="cam" data-cam="0" aria-pressed="true"><svg class="icon"><use href="#i-behind"/></svg>Behind</button><button class="cam" data-cam="1" aria-pressed="false"><svg class="icon"><use href="#i-cockpit"/></svg>Cockpit</button><button class="cam" data-cam="2" aria-pressed="false"><svg class="icon"><use href="#i-above"/></svg>Above</button></div></div><div class="help"><svg class="icon"><use href="#i-pointer"/></svg><div><strong class="mouse-help">Move your pointer to steer</strong><strong class="touch-help">Drag one finger to steer</strong><small>Fly through the rings to deliver fish.</small></div></div></footer>
</div>
<script id="sky-vs" type="x-shader/x-vertex">
attribute vec2 aXY;
varying vec2 vXY;
void main(){vXY=aXY;gl_Position=vec4(aXY,0.,1.);}
</script>
<script id="sky-fs" type="x-shader/x-fragment">
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
varying vec2 vXY;
uniform vec3 uEye,uRight,uUp,uFront,uPlane;
uniform vec2 uLens;
uniform float uTime,uDusk;
float hash(vec2 p){return fract(sin(dot(mod(p,701.),vec2(127.1,311.7)))*4375.5453);}
float dashes(vec2 p,float seed){
 float row=floor(p.y);
 p.x+=hash(vec2(row,seed))*17.;
 vec2 id=floor(p), f=fract(p)-.5;
 float h=hash(id+seed);
 float width=.14+.32*h;
 return (1.-smoothstep(width*.62,width,abs(f.x)))*(1.-smoothstep(.018,.105+(.07*h),abs(f.y+.065*sin(f.x*5.+h*4.))))*smoothstep(.22,.81,h);
}
void main(){
 vec3 dir=normalize(uFront+uRight*vXY.x*uLens.x+uUp*vXY.y*uLens.y);
 vec3 sun=normalize(vec3(mix(.44,.68,smoothstep(.62,1.40,uLens.x/uLens.y)),.196-uDusk*.29,.86));
 float elev=max(0.,dir.y);
 vec3 horizon=mix(vec3(1.,.81,.635),vec3(.395,.339,.451),uDusk);
 vec3 middle=mix(vec3(.951,.648,.621),vec3(.175,.207,.339),uDusk);
 vec3 high=mix(vec3(.516,.588,.714),vec3(.055,.096,.189),uDusk);
 vec3 sky=mix(horizon,middle,smoothstep(-.02,.34,elev));
 sky=mix(sky,high,smoothstep(.18,.81,elev));
 float sd=length(dir-sun), warm=1.-uDusk*.84;
 sky+=vec3(.19,.128,.035)*exp(-sd*4.7)*warm;
 sky=mix(sky,vec3(1.,.833,.54),exp(-sd*14.)*.70*warm);
 sky=mix(sky,vec3(1.,.953,.749),(1.-smoothstep(.062,.0645,sd))*warm);
 vec2 stars=vec2(atan(dir.x,dir.z)*92.,dir.y*131.);
 float star=hash(floor(stars));
 sky+=smoothstep(.972,1.,star)*(1.-smoothstep(.035,.15,length(fract(stars)-.5)))*smoothstep(.04,.2,elev)*uDusk*vec3(.85,.84,.76);
 vec3 color=sky;
 if(dir.y<-.0003){
  float dist=min(4000.,(uEye.y-.015)/(-dir.y));
  vec2 p=uEye.xz+dist*dir.xz;
  float haze=exp(-max(0.,dist)*.0051);
  vec3 nearSea=mix(vec3(.262,.509,.555),vec3(.084,.183,.236),uDusk);
  vec3 farSea=mix(vec3(.814,.687,.656),vec3(.273,.283,.373),uDusk);
  vec3 sea=mix(farSea,nearSea,pow(haze,.70));
  float und=sin(p.x*.067+p.y*.137+uTime*.42)*.5+sin(p.y*.34-p.x*.023-uTime*.47)*.5;
  sea+=vec3(.025,.026,.025)*und*haze;
  float nearMask=smoothstep(.01,.09,-dir.y);
  float streak=dashes(p*vec2(.109,.77)+vec2(uTime*.065,-uTime*.09),2.);
  float streak2=dashes(p*vec2(.073,.34)+vec2(-uTime*.027,uTime*.034),9.);
  sea=mix(sea,vec3(.75,.837,.806)*(1.-uDusk*.63),(.26*streak+.16*streak2)*nearMask);
  sea-=vec3(.034,.041,.038)*dashes(p*vec2(.095,.55)+uTime*.025,13.)*nearMask;
  float sunX=dot(normalize(dir.xz+.00001),normalize(sun.xz));
  float width=.0052+max(0.,-dir.y)*.033;
  float trail=exp(-max(0.,1.-sunX)/width);
  float waves=.48+.26*sin(p.y*1.27+sin(p.x*.49+uTime*.4)*1.6-uTime*.92)+.26*sin(p.y*.54+p.x*.09+uTime*.55);
  float sparkle=smoothstep(.44,.77,waves);
  sea=mix(sea,vec3(1.,.749,.473),trail*(.31+sparkle*.48)*warm);
  sea=mix(sea,vec3(1.,.903,.662),trail*(streak*.68+streak2*.25)*warm);
  vec2 shadow=p-(uPlane.xz-vec2(.58,.43)*uPlane.y);
  float shade=exp(-dot(shadow/vec2(4.8,1.55),shadow/vec2(4.8,1.55)));
  sea*=1.-.13*shade;
  color=mix(sea,horizon,1.-smoothstep(.0003,.009,-dir.y));
 }
 float grain=(hash(gl_FragCoord.xy)-.5)*.011;
 gl_FragColor=vec4(color+grain,1.);
}
</script>
<script id="mesh-vs" type="x-shader/x-vertex">
attribute vec3 aPos,aNormal,aColor;
uniform mat4 uModel;
uniform vec3 uEye,uRight,uUp,uFront;
uniform vec2 uLens;
uniform float uMirror,uTime,uDusk;
varying vec3 vColor;
varying float vDepth,vHeight;
void main(){
 vec3 w=(uModel*vec4(aPos,1.)).xyz;
 vHeight=w.y;
 float glowing=1.-step(.1,dot(aNormal,aNormal));
 vec3 n=mat3(uModel)*aNormal;
 n=n*inversesqrt(max(dot(n,n),.0001));
 float sun=max(0.,dot(n,normalize(vec3(.64,.81,.37))));
 float lit=.74+.15*n.y+.23*sun;
 vec3 day=aColor*lit+vec3(.05,.004,-.021)*sun+vec3(.018,.019,.034)*(1.-sun);
 vColor=mix(day,day*vec3(.335,.409,.543)+vec3(.013,.018,.028),uDusk);
 vColor=mix(vColor,aColor*(1.+.12*uDusk),glowing);
 if(uMirror>.5){w.y=.015-w.y;w.x+=.12*sin(w.z*.42+uTime*1.1)+.06*sin(w.y*1.4+uTime*.7);}
 vec3 d=w-uEye;
 float z=dot(d,uFront); vDepth=z;
 gl_Position=vec4(dot(d,uRight)/uLens.x,dot(d,uUp)/uLens.y,z*1.00011-.24002,z);
}
</script>
<script id="mesh-fs" type="x-shader/x-fragment">
precision highp float;
uniform float uMirror,uDusk,uTime;
varying vec3 vColor;
varying float vDepth,vHeight;
void main(){
 vec3 mist=mix(vec3(.995,.786,.675),vec3(.321,.301,.392),uDusk);
 float fog=1.-exp(-max(0.,vDepth-12.)*.00172);
 vec3 c=mix(vColor,mist,min(.83,fog));
 if(uMirror>.5){
  if(vHeight<.055)discard;
  float ripple=.5+.5*sin(gl_FragCoord.y*1.11+sin(gl_FragCoord.x*.027+uTime*.56)*1.5+uTime*.86);
  c=mix(c,vec3(.37,.538,.585)*(1.-uDusk*.52),.30);
  gl_FragColor=vec4(c,(.43+.27*exp(-vHeight*.043))*(.75+.23*ripple));
 }else{gl_FragColor=vec4(c,1.);}
}
</script>
<script id="ring-vs" type="x-shader/x-vertex">
attribute vec2 aXY;
uniform vec3 uCenter,uEye,uRight,uUp,uFront;
uniform vec2 uLens;
uniform float uRadius;
varying vec2 vUV;
void main(){
 vUV=aXY*1.38;
 vec3 d=uCenter+vec3(vUV*uRadius,0.)-uEye;
 float z=dot(d,uFront);
 gl_Position=vec4(dot(d,uRight)/uLens.x,dot(d,uUp)/uLens.y,z*1.00011-.24002,z);
}
</script>
<script id="ring-fs" type="x-shader/x-fragment">
precision highp float;
varying vec2 vUV;
uniform float uTime,uStrength;
void main(){
 float r=length(vUV),a=atan(vUV.y,vUV.x),d=abs(r-1.);
 float halo=exp(-d*14.)*.59;
 float edge=1.-smoothstep(.035,.057,d);
 float core=1.-smoothstep(.006,.023,d);
 float arcs=(1.-smoothstep(.004,.012,abs(r-1.105)))*smoothstep(.72,.83,cos(a*4.+uTime*.38))*.74;
 float marks=0.;
 for(int i=0;i<4;i++){
  float q=float(i)*1.570796+.7854+sin(uTime*.4)*.07;
  vec2 off=vUV-vec2(cos(q),sin(q))*1.015;
  marks=max(marks,1.-smoothstep(.075,.095,abs(off.x)*1.7+abs(off.y)*.8));
 }
 float alpha=max(max(halo,edge),max(arcs,marks));
 if(alpha*uStrength<.012)discard;
 vec3 col=mix(vec3(1.,.69,.218),vec3(1.,.846,.34),edge);
 col=mix(col,vec3(1.,.987,.773),max(core,marks));
 gl_FragColor=vec4(col,min(.985,alpha)*uStrength);
}
</script>
<script>
(()=>{
"use strict";
const el=id=>document.getElementById(id),canvas=el("world"),scene=el("scene");
const TAU=Math.PI*2, PI=Math.PI, sin=Math.sin, cos=Math.cos, abs=Math.abs;
const clamp=(x,a,b)=>Math.max(a,Math.min(b,x)),mix=(a,b,t)=>a+(b-a)*t;
const ease=t=>t*t*(3-2*t),approach=(a,b,k,dt)=>mix(a,b,1-Math.exp(-k*dt));
const norm=v=>{const l=Math.hypot(v[0],v[1],v[2])||1;return [v[0]/l,v[1]/l,v[2]/l]};
const dot=(a,b)=>a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
const sub=(a,b)=>[a[0]-b[0],a[1]-b[1],a[2]-b[2]];
const cross=(a,b)=>[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]];
function random(seed){let n=seed>>>0;return ()=>{n=(Math.imul(n,1664525)+1013904223)>>>0;return n/4294967296}}
const colors=new Map();
function rgb(s){if(Array.isArray(s))return s;if(!colors.has(s)){const n=parseInt(s.slice(1),16);colors.set(s,[(n>>16)/255,((n>>8)&255)/255,(n&255)/255])}return colors.get(s)}
function tone(s,k){return rgb(s).map(a=>clamp(a*k,0,1))}
function frame(x=0,y=0,z=0,yaw=0,pitch=0,roll=0,sx=1,sy=sx,sz=sx){
 const a=cos(yaw),b=sin(yaw),c=cos(pitch),d=sin(pitch),e=cos(roll),f=sin(roll);
 return new Float32Array([(a*e-b*d*f)*sx,c*f*sx,(-b*e-a*d*f)*sx,0,(-a*f-b*d*e)*sy,c*e*sy,(b*f-a*d*e)*sy,0,b*c*sz,d*sz,a*c*sz,0,x,y,z,1]);
}
const ID=frame();
function point(m,p){return [m[0]*p[0]+m[4]*p[1]+m[8]*p[2]+m[12],m[1]*p[0]+m[5]*p[1]+m[9]*p[2]+m[13],m[2]*p[0]+m[6]*p[1]+m[10]*p[2]+m[14]]}
function product(a,b){
 const m=new Float32Array(16);
 for(let j=0;j<4;j++)for(let i=0;i<4;i++)m[j*4+i]=a[i]*b[j*4]+a[4+i]*b[j*4+1]+a[8+i]*b[j*4+2]+a[12+i]*b[j*4+3];
 return m;
}
function tri(b,a,p,q,col,out){
 let n=norm(cross(sub(p,a),sub(q,a)));
 if(out===0)n=[0,0,0];else if(out&&dot(n,out)<0)n=[-n[0],-n[1],-n[2]];
 const c=rgb(col);for(const v of [a,p,q])b.push(v[0],v[1],v[2],n[0],n[1],n[2],c[0],c[1],c[2]);
}
function quad(b,a,p,q,r,c,n){tri(b,a,p,q,c,n);tri(b,a,q,r,c,n)}
function box(b,x,y,z,w,h,d,col){
 const l=x-w/2,r=x+w/2,u=y+h,v=z+d/2,t=z-d/2;
 quad(b,[l,y,t],[l,u,t],[r,u,t],[r,y,t],col,[0,0,-1]);quad(b,[l,y,v],[r,y,v],[r,u,v],[l,u,v],col,[0,0,1]);
 quad(b,[l,y,t],[l,y,v],[l,u,v],[l,u,t],col,[-1,0,0]);quad(b,[r,y,t],[r,u,t],[r,u,v],[r,y,v],col,[1,0,0]);
 quad(b,[l,u,t],[l,u,v],[r,u,v],[r,u,t],col,[0,1,0]);quad(b,[l,y,t],[r,y,t],[r,y,v],[l,y,v],col,[0,-1,0]);
}
function tube(b,a,p,r0,r1,col,n=9,caps=true,lit=true){
 const axis=norm(sub(p,a)),v=norm(cross(axis,abs(axis[1])>.83?[0,0,1]:[0,1,0])),w=cross(axis,v);
 const ring=(base,rad,k)=>[base[0]+rad*(v[0]*cos(k)+w[0]*sin(k)),base[1]+rad*(v[1]*cos(k)+w[1]*sin(k)),base[2]+rad*(v[2]*cos(k)+w[2]*sin(k))];
 for(let i=0;i<n;i++){
  const k=TAU*i/n,j=TAU*(i+1)/n,A=ring(a,r0,k),B=ring(a,r0,j),C=ring(p,r1,j),D=ring(p,r1,k),dir=sub(A,a);
  if(r1<.0001)tri(b,A,B,p,col,lit?dir:0);else quad(b,A,B,C,D,col,lit?dir:0);
  if(caps&&r0>0)tri(b,a,B,A,col,lit?axis.map(h=>-h):0);
  if(caps&&r1>0)tri(b,p,D,C,col,lit?axis:0);
 }
}
function oval(b,x,y,z,rx,ry,rz,col,n=12,k=7){
 const v=(i,j)=>{const a=TAU*i/n,h=-PI/2+PI*j/k;return [x+rx*cos(h)*cos(a),y+ry*sin(h),z+rz*cos(h)*sin(a)]};
 for(let j=0;j<k;j++)for(let i=0;i<n;i++){
  const A=v(i,j),B=v(i+1,j),C=v(i+1,j+1),D=v(i,j+1),out=sub(v(i+.5,j+.5),[x,y,z]);
  if(j===0)tri(b,A,C,D,col,out);else if(j===k-1)tri(b,A,B,D,col,out);else quad(b,A,B,C,D,col,out);
 }
}
function disk(b,x,y,z,rx,rz,col,n=14){for(let i=0;i<n;i++){const a=i*TAU/n,d=(i+1)*TAU/n;tri(b,[x,y,z],[x+cos(a)*rx,y,z+sin(a)*rz],[x+cos(d)*rx,y,z+sin(d)*rz],col,[0,1,0])}}
function stamp(b,a,m){
 const ix=1/(m[0]*m[0]+m[1]*m[1]+m[2]*m[2]||1),iy=1/(m[4]*m[4]+m[5]*m[5]+m[6]*m[6]||1),iz=1/(m[8]*m[8]+m[9]*m[9]+m[10]*m[10]||1);
 for(let i=0;i<a.length;i+=9){
  const x=a[i],y=a[i+1],z=a[i+2],nx=a[i+3]*ix,ny=a[i+4]*iy,nz=a[i+5]*iz;
  const tx=m[0]*nx+m[4]*ny+m[8]*nz,ty=m[1]*nx+m[5]*ny+m[9]*nz,tz=m[2]*nx+m[6]*ny+m[10]*nz;
  const l=Math.hypot(tx,ty,tz)||1;
  b.push(m[0]*x+m[4]*y+m[8]*z+m[12],m[1]*x+m[5]*y+m[9]*z+m[13],m[2]*x+m[6]*y+m[10]*z+m[14],tx/l,ty/l,tz/l,a[i+6],a[i+7],a[i+8]);
 }
}
function mesh(b){return {data:new Float32Array(b),count:b.length/9,buffer:null}}
const scenery=[],islands=[],shoreEdges=[[],[]];
function sceneryPart(b,x,y,z,r,kind="land"){if(b.length)scenery.push({g:mesh(b),x,y,z,r,kind})}
const stops=[
 {name:"Lighthouse Pier",x:5,y:18,z:39,side:1,base:2.05,roof:false},
 {name:"Copper Rooftop",x:-17,y:20.5,z:111,side:-1,base:8.2,roof:true},
 {name:"Willow Landing",x:18,y:15.8,z:192,side:1,base:2.05,roof:false},
 {name:"The Old Fishery",x:-18,y:20,z:265,side:-1,base:8.2,roof:true},
 {name:"Sunset Wharf",x:10,y:17,z:344,side:1,base:2.05,roof:false}
];
const RAD=5.2;
function fishMark(b,x,y,z,size,color,vertical=false){
 const pts=[[-.53,0],[-.22,.24],[.18,.24],[.43,.07],[.67,.29],[.67,-.28],[.42,-.06],[.15,-.25],[-.23,-.22]];
 const cv=p=>vertical?[x+p[0]*size,y+p[1]*size,z]:[x+p[0]*size,y,z+p[1]*size];
 for(let i=0;i<pts.length;i++)tri(b,cv([0,0]),cv(pts[i]),cv(pts[(i+1)%pts.length]),color,vertical?[0,0,-1]:[0,1,0]);
}
function loftZ(b,sections,n=22){
 for(let j=1;j<sections.length;j++){
  const A=sections[j-1],B=sections[j],pt=(s,a)=>[s[1]*cos(a),s[3]+s[2]*sin(a),s[0]];
  for(let i=0;i<n;i++){
   const a=i*TAU/n,d=(i+1)*TAU/n,mid=(a+d)*.5;
   quad(b,pt(A,a),pt(A,d),pt(B,d),pt(B,a),B[4],[cos(mid),sin(mid),0]);
  }
 }
}
function wingHalf(b,side){
 const xs=[0,1.14,2.85,3.25,3.39,3.70,3.88,4.35,4.64,4.83,4.87],le=[1.03,1.02,.94,.91,.90,.87,.855,.79,.64,.37,.06],te=[-1.02,-.96,-.76,-.71,-.69,-.655,-.63,-.54,-.42,-.18,.015];
 const paint=["","#ce513b","#d4563e","#d4563e","#ffe4b6","#d4563e","#ffe4b6","#d4563e","#c74f39","#c74f39","#b34b37"];
 const shape=i=>{const x=xs[i]*side,h=-.22+xs[i]*.048;return [[x,h+.05,le[i]],[x,h+.17,mix(le[i],te[i],.34)],[x,h,te[i]],[x,h-.095,mix(le[i],te[i],.35)]]};
 for(let i=1;i<xs.length;i++){const a=shape(i-1),v=shape(i);for(let j=0;j<4;j++)quad(b,a[j],v[j],v[(j+1)%4],a[(j+1)%4],j<2?paint[i]:"#a04436",[0,j<2?1:-1,j===0? .15:0])}
 const x=side*2.43;
 disk(b,x,.087,.27,.48,.39,"#f8e3b8",22);disk(b,x,.093,.27,.36,.29,"#3a5356",20);fishMark(b,x,.10,.27,.48,"#ffe5b1");
 tube(b,[side*1.65,-.107,-.52],[side*4.26,.020,-.35],.014,.014,"#984b3c",5);
 for(const pos of [2.05,3.17,4.08])tube(b,[side*pos,-.198+pos*.048,-.54],[side*pos,-.215+pos*.048,-.72],.014,.014,"#984b3c",4);
}
function foil(b,outline,thickness,color,axis="y"){
 const av=axis==="y"?[0,1,0]:[1,0,0],up=p=>p.map((v,i)=>v+av[i]*thickness*.5),dn=p=>p.map((v,i)=>v-av[i]*thickness*.5);
 const center=outline.reduce((a,p)=>a.map((v,i)=>v+p[i]/outline.length),[0,0,0]);
 for(let i=0;i<outline.length;i++){const p=outline[i],q=outline[(i+1)%outline.length];tri(b,up(center),up(p),up(q),color,av);tri(b,dn(center),dn(q),dn(p),color,av.map(v=>-v));quad(b,up(p),up(q),dn(q),dn(p),tone(color,.84),sub(p,center))}
}
function makePlane(){
 const b=[],red="#cc513b",cream="#f7dbab",engine="#344c50";
 wingHalf(b,-1);wingHalf(b,1);
 loftZ(b,[[-3.32,.065,.13,.14,red],[-3.08,.17,.25,.13,red],[-2.61,.28,.34,.11,red],[-1.65,.47,.49,.10,red],[-1.44,.49,.51,.10,cream],[-1.32,.52,.525,.1,red],[-1.24,.535,.54,.1,cream],[-.42,.62,.63,.07,red],[.6,.68,.64,.035,red],[1.27,.663,.61,.03,red],[1.42,.67,.615,.035,cream],[1.57,.68,.62,.03,cream],[1.70,.72,.66,.03,engine],[1.92,.722,.66,.03,engine],[2.03,.70,.645,.035,"#d79365"],[2.20,.62,.58,.05,red],[2.29,.54,.50,.06,red],[2.30,.015,.02,.06,engine]]);
 for(let i=0;i<12;i++){const a=i*TAU/12;tube(b,[cos(a)*.65,.035+sin(a)*.59,1.73],[cos(a)*.66,.035+sin(a)*.60,1.92],.036,.036,"#82908a",6)}
 foil(b,[[-1.56,.12,-2.85],[-1.47,.12,-2.61],[-.32,.12,-2.19],[.33,.12,-2.19],[1.47,.12,-2.61],[1.57,.12,-2.85],[1.35,.12,-3.03],[.25,.12,-3.2],[-.25,.12,-3.20],[-1.35,.12,-3.03]],.08,red);
 for(const s of [-1,1])quad(b,[s*.97,.167,-2.43],[s*1.14,.167,-2.50],[s*1.14,.167,-3.066],[s*.97,.167,-3.094],cream,[0,1,0]);
 foil(b,[[0,.17,-2.18],[0,.63,-2.43],[0,1.13,-2.75],[0,1.31,-3.03],[0,1.29,-3.25],[0,1.14,-3.33],[0,.19,-3.34]],.124,red,"x");
 for(const s of [-1,1]){quad(b,[s*.064,.71,-2.48],[s*.064,.88,-2.60],[s*.064,.88,-3.33],[s*.064,.71,-3.34],cream,[s,0,0]);}
 oval(b,0,.583,-.21,.55,.11,.69,"#714538",22,6);oval(b,0,.642,-.23,.48,.085,.59,"#253c3e",20,5);
 for(const side of [-1,1]){
  tube(b,[side*.49,-.25,.87],[side*.88,-.93,.48],.045,.045,engine,7);
  tube(b,[side*.48,-.24,-.54],[side*.88,-.93,.48],.045,.045,engine,7);
  tube(b,[side*.72,-.95,.48],[side*1.01,-.95,.48],.332,.332,"#333e40",16);
  tube(b,[side*1.009,-.95,.48],[side*1.03,-.95,.48],.157,.157,"#c0a683",14);
  tube(b,[side*.47,.16,-1.72],[side*.59,.05,-.65],.035,.035,cream,6);
 }
 tube(b,[0,-.08,-2.81],[0,-.54,-3.01],.05,.05,engine,6);tube(b,[-.085,-.55,-3.02],[.085,-.55,-3.02],.145,.145,"#364344",11);
 oval(b,0,.79,-.19,.40,.48,.37,"#bd733f",14,7);oval(b,0,.96,-.21,.43,.16,.38,"#3d7974",14,6);
 for(const s of [-1,1]){oval(b,s*.40,.77,.03,.15,.16,.23,"#df9958",10,5);oval(b,s*.42,.76,.19,.135,.11,.115,"#ffe0a0",8,5);}
 // Low windscreen, with a brass rim and a clear view over the nose.
 for(let i=0;i<14;i++){
  const a=PI*i/14,d=PI*(i+1)/14;
  const p=q=>[.48*cos(q),.605+.36*sin(q),.40+.08*sin(q)];
  tube(b,p(a),p(d),.024,.024,"#c6b692",6);
 }
 tube(b,[-.47,.606,.39],[.47,.606,.39],.025,.025,"#b49871",7);
 return mesh(b);
}
function makeHead(fur="#e69a54",pilot=true,patch=false){
 const b=[],cream="#ffe1aa",nose="#75493d",stripe=patch?"#70615a":"#a95735";
 oval(b,0,.06,0,.61,.53,.52,fur,22,12);
 for(const s of [-1,1]){
  const a=[s*.58,.24,.13],d=[s*.26,.44,.16],p=[s*.51,.88,.07],r=[s*.46,.34,-.23];
  tri(b,a,d,p,fur,[0,.1,1]);tri(b,a,p,r,tone(fur,.87),[s,0,-.2]);tri(b,d,r,p,fur,[-s,0,-.4]);
  tri(b,[s*.531,.36,.143],[s*.346,.469,.161],[s*.491,.731,.095],"#b97061",[0,0,1]);
  oval(b,s*.22,-.032,.436,.232,.186,.134,cream,12,7);
  oval(b,s*.263,.139,.44,.073,.09,.042,"#313c39",12,7);oval(b,s*.242,.166,.478,.022,.024,.009,"#fff2ce",8,5);
  for(const k of [0,1])tube(b,[s*.36,-.035-k*.062,.49],[s*(.83-k*.025),.002-k*.13,.46],.013,.005,cream,5);
 }
 tri(b,[-.083,.021,.566],[.083,.021,.566],[0,-.066,.571],nose,[0,0,1]);
 for(const a of [-.58,0,.58]){
  const at=(ang,ht)=>[.615*cos(ht)*sin(ang),.06+.533*sin(ht),-.525*cos(ht)*cos(ang)];
  tri(b,at(a-.12,.87),at(a+.115,.87),at(a+.015,.17),stripe,[sin(a),.3,-cos(a)]);
  tri(b,at(a-.12,.87),at(a+.015,.17),at(a-.084,.49),stripe,[sin(a),.3,-cos(a)]);
 }
 if(pilot){
  const belt=t=>[.615*sin(t),.178+.040*cos(t),.52*cos(t)];
  for(let i=0;i<32;i++){const a=belt(i*TAU/32),d=belt((i+1)*TAU/32);quad(b,a,d,[d[0],d[1]+.113,d[2]],[a[0],a[1]+.113,a[2]],"#73513b",[a[0],0,a[2]])}
  for(const s of [-1,1]){
   oval(b,s*.249,.29,.421,.218,.166,.073,"#644835",16,8);
   oval(b,s*.249,.293,.461,.189,.139,.063,"#e8c285",16,8);
   oval(b,s*.249,.298,.490,.148,.102,.051,"#3c6269",16,8);
   tube(b,[s*.27-.038,.320,.533],[s*.27+.044,.335,.531],.011,.013,"#c4e2d9",5);
  }
  tube(b,[-.078,.292,.485],[.078,.292,.485],.023,.023,"#b78e60",7);
 }
 return b;
}
function makeProp(){
 const b=[];
 for(const s of [-1,1]){
  foil(b,[[0,.02*s,0],[-.112*s,.32*s,0],[-.145*s,.91*s,.015],[-.084*s,1.21*s,.018],[.027*s,1.28*s,.018],[.137*s,1.12*s,0],[.129*s,.71*s,-.01],[.05*s,.18*s,0]],.008,"#865b3d","x");
  quad(b,[-.13*s,.982*s,.026],[.138*s,.982*s,.026],[.098*s,1.13*s,.026],[-.103*s,1.13*s,.026],"#f4ca84",[0,0,1]);
 }
 oval(b,0,0,.065,.18,.18,.23,"#e2bd81",16,8);return mesh(b);
}
function makeScarf(){
 const b=[],pc=[[0,0,0],[-.26,.042,-.27],[-.43,.10,-.53],[-.46,.055,-.77],[-.60,.12,-1.09],[-.69,.13,-1.30]];
 for(let i=1;i<pc.length;i++){
  const a=pc[i-1],p=pc[i],edge=(q,s)=>[q[0]+s*.09,q[1]+s*.026,q[2]];
  quad(b,edge(a,-1),edge(a,1),edge(p,1),edge(p,-1),i===4?"#efbd77":"#36857c",[0,1,0]);
 }
 return mesh(b);
}
function catModel(i){
 const fur=["#da9857","#dfc29b","#717679","#c98151","#ddc9a8"][i],b=[];
 oval(b,0,.48,-.08,.43,.62,.38,fur,12,7);
 oval(b,0,.54,.194,.29,.43,.16,"#f8deb4",10,6);
 for(const s of [-1,1]){oval(b,s*.34,.275,-.055,.21,.25,.28,fur,10,6);oval(b,s*.19,.12,.23,.15,.117,.21,"#f5dbad",10,5)}
 const tail=[[.24,.15,-.21],[.48,.18,-.44],[.64,.32,-.45],[.75,.53,-.42],[.72,.69,-.37]];
 for(let k=1;k<tail.length;k++)tube(b,tail[k-1],tail[k],.13-k*.009,.126-k*.01,k===4?"#f0c895":fur,8);
 stamp(b,makeHead(fur,false,i===2),frame(0,1.11,0,.08,0,0,.85));return b;
}
function crateModel(){
 const b=[];box(b,0,0,0,1.25,.92,1.03,"#a5734b");
 for(let i=0;i<3;i++)box(b,0,.045+i*.287,0,1.285,.25,1.055,i%2?"#c49763":"#d2a46d");
 for(const s of [-1,1]){box(b,s*.54,.005,0,.12,.92,1.09,"#edc58a");box(b,s*.50,.927,0,.092,.020,1.05,"#e5be84")}
 box(b,0,.88,0,1.30,.10,1.09,"#e2b67c");box(b,0,.02,0,1.30,.09,1.09,"#deb282");
 box(b,0,.24,-.541,.62,.42,.021,"#eee1b5");fishMark(b,-.018,.447,-.554,.428,"#395a5c",true);
 return b;
}
function canopyModel(){
 const b=[],N=16,rows=5;
 const v=(i,j)=>{const a=i*TAU/N,t=(j/rows)*PI*.5;return [2.05*sin(t)*cos(a),1.05*cos(t)+(j===rows?(i%2? .075:0):0),2.05*sin(t)*sin(a)]};
 for(let i=0;i<N;i++)for(let j=0;j<rows;j++){
  const a=v(i,j),p=v(i+1,j),q=v(i+1,j+1),r=v(i,j+1),col=(Math.floor(i/2)%2)?"#fff0ce":"#d9704d";
  if(!j)tri(b,a,q,r,col,[r[0],1,r[2]]);else quad(b,a,p,q,r,col,[r[0],.5,r[2]]);
 }
 for(let i=0;i<8;i++){const a=i*TAU/8;tube(b,[cos(a)*2.05,.035,sin(a)*2.05],[cos(a)*.54,-2.45,sin(a)*.40],.018,.011,"#675848",5,false)}
 return mesh(b);
}
const aircraft=makePlane(),pilotHead=mesh(makeHead()),prop=makeProp(),scarf=makeScarf(),crateRaw=crateModel(),crate=mesh(crateRaw),canopy=canopyModel();
function pineTree(k){
 const b=[],r=random(61+k*811),palette=[["#3d6860","#4e7564","#638570"],["#516f60","#65816a","#7e9271"],["#49625c","#5c7464","#738570"]];
 tube(b,[0,0,0],[.04,3,0],.17,.10,"#856b50",6);
 for(let j=0;j<3;j++){
  const n=7,rad=1.26-.30*j,base=.68+j*1.14,top=3.39+j*.97,pts=[];
  for(let i=0;i<n;i++){const a=i*TAU/n+j*.44,rr=rad*(.87+.23*r());pts.push([rr*cos(a),base+r()*.25,rr*sin(a)])}
  for(let i=0;i<n;i++){const a=pts[i],p=pts[(i+1)%n];tri(b,a,p,[.045*j,top,-.03*j],palette[k%3][j],[a[0],.4,a[2]]);tri(b,[0,base+.04,0],p,a,tone(palette[k%3][j],.88),[0,-1,0])}
 }
 return b;
}
function broadTree(k){
 const b=[],c=["#9b9b68","#bc935c","#809270"][k%3];
 tube(b,[0,0,0],[.04,2.87,0],.19,.103,"#876e51",7);
 tube(b,[0,1.39,0],[-.65,2.65,.18],.095,.044,"#85694e",6);
 oval(b,-.54,2.95,.14,1.02,1.12,.98,c,8,5);oval(b,.51,2.9,-.06,1.1,1.16,.97,tone(c,.96),8,5);oval(b,.02,3.69,.07,1.03,1.05,1.07,tone(c,1.05),8,5);return b;
}
const trees=[pineTree(0),pineTree(1),pineTree(2),broadTree(0),broadTree(1),broadTree(2)];
function houseModel(k){
 const b=[],r=random(552+k*41),w=3.7+r()*1.35,d=3.2+r()*1.85,h=2.85+r()*1.12,g=.95+r()*.98;
 const wall=["#e8cfaa","#e0b389","#d8a88c","#edcfaa","#c2bab0","#e1c398","#e3b699","#a9b6ac"][k%8];
 const roof=["#ab6454","#b9684e","#5e7474","#9f594c","#ba7659","#777976","#9e6056","#c68761"][k%8],beam="#7b6a56",glass="#485e61",framecol="#ebd6b0";
 const hw=w/2,hd=d/2,W=hw+.31,D=hd+.30,H=h+.13;
 box(b,0,0,0,w+.16,.34,d+.14,"#9d8c73");box(b,0,.28,0,w,h-.28,d,wall);
 const win=(cx,cy,z,lit)=>{
  box(b,cx,cy,z,.72,.89,.069,beam);
  quad(b,[cx-.275,cy+.10,z-.039],[cx+.275,cy+.10,z-.039],[cx+.275,cy+.78,z-.039],[cx-.275,cy+.78,z-.039],lit?"#f8c37e":glass,lit?0:[0,0,-1]);
  box(b,cx,cy+.41,z-.048,.56,.042,.025,framecol);box(b,cx,cy+.06,z-.049,.037,.76,.024,framecol);
  box(b,cx,cy-.035,z-.04,.88,.085,.16,framecol);
 };
 for(const a of [-1,1])win(a*w*.291,.99,-hd-.038,(k+(a>0?1:0))%3!==1);
 box(b,0,.21,-hd-.04,.69,1.67,.095,k%2?"#657c72":"#855e4e");
 box(b,0,.24,-hd-.12,.96,.13,.39,"#c5b397");
 for(const s of [-1,1])for(const z of [-d*.238,d*.238]){
  box(b,s*(hw+.02),1.04,z,.055,.88,.77,beam);
  const x=s*(hw+.054);quad(b,[x,1.13,z-.294],[x,1.13,z+.294],[x,1.80,z+.294],[x,1.80,z-.294],k%3===0?"#f6c584":glass,k%3===0?0:[s,0,0]);
  box(b,x,1.435,z,.018,.05,.59,framecol);
 }
 for(const s of [-1,1]){
  tri(b,[-hw,h,s*hd],[hw,h,s*hd],[0,h+g,s*hd],wall,[0,0,s]);
  quad(b,[s*W,H-.11,-D],[s*W,H-.11,D],[0,H+g,D],[0,H+g,-D],roof,[s,.7,0]);
  quad(b,[s*W,H-.11,-D],[s*W,H-.23,-D],[s*W,H-.23,D],[s*W,H-.11,D],tone(roof,.78),[s,0,0]);
  tube(b,[s*W,H-.145,-D],[0,H+g-.031,-D],.084,.084,tone(roof,.85),5);
  box(b,s*(hw-.04),.3,-hd-.008,.11,h-.3,.10,tone(wall,.84));
 }
 tube(b,[0,H+g,-D-.035],[0,H+g,D+.035],.095,.095,tone(roof,1.09),8);
 box(b,w*.23,h+.36,d*.19,.49,g+.49,.51,k%2?"#a78269":"#b1927a");box(b,w*.23,h+g+.79,d*.19,.60,.17,.61,tone(roof,.90));
 if(k%3===1){
  quad(b,[-1.44,2.34,-hd],[1.44,2.34,-hd],[1.44,2.01,-hd-.84],[-1.44,2.01,-hd-.84],"#778f7e",[0,1,-.15]);
  for(let n=0;n<3;n++)quad(b,[-1.42+n*.96,2.35,-hd],[-.96+n*.96,2.35,-hd],[-.96+n*.96,2.02,-hd-.845],[-1.42+n*.96,2.02,-hd-.845],"#f0d8a4",[0,1,0]);
 }
 return b;
}
const houses=Array.from({length:8},(_,i)=>houseModel(i));
function makeIsland(b,cx,cz,rx,rz,h,seed){
 const r=random(seed),n=25,ring=[],top=[],beach=[],wash=[];
 for(let i=0;i<n;i++){
  const a=TAU*i/n,rad=.97+.07*sin(3*a+seed)+.055*r(),vx=rx*rad*cos(a),vz=rz*rad*sin(a);
  ring.push([cx+vx*.94,h,cz+vz*.94]);top.push([cx+vx*.51,h+.013,cz+vz*.51]);
  beach.push([cx+vx,.43+r()*.19,cz+vz]);wash.push([cx+vx*1.052,-.18,cz+vz*1.052]);
 }
 const info={cx,cz,rx,rz,points:beach.map(p=>[p[0],p[2]])};islands.push(info);
 for(let i=0;i<n;i++){
  const j=(i+1)%n,dir=sub(ring[i],[cx,0,cz]);
  tri(b,[cx-.15*rx,h+.02,cz+.08*rz],top[i],top[j],tone("#a4a779",.98+r()*.043),[0,1,0]);
  quad(b,top[i],ring[i],ring[j],top[j],tone("#a2a477",.96+r()*.07),[0,1,0]);
  quad(b,ring[i],beach[i],beach[j],ring[j],tone("#b8a483",.90+r()*.13),dir);
  quad(b,beach[i],wash[i],wash[j],beach[j],tone("#dcc5a0",.96+r()*.06),dir);
  if(i%4!==1){
   const p=wash[i],q=wash[j],outer=v=>[cx+(v[0]-cx)*1.014,.055,cz+(v[2]-cz)*1.014];
   quad(b,[p[0],.06,p[2]],[q[0],.06,q[2]],outer(q),outer(p),"#bdd0be",[0,1,0]);
  }
 }
 return info;
}
function jetty(b,from,to,z,h=2.05,w=3.8){
 const lo=Math.min(from,to),hi=Math.max(from,to),n=Math.ceil((hi-lo)/.70),step=(hi-lo)/n;
 for(let i=0;i<n;i++)box(b,lo+(i+.5)*step,h-.22,z,step-.035,.22,w,i%4===0?"#be956c":i%3===0?"#ad845f":"#c29b70");
 for(const s of [-1,1]){
  box(b,(lo+hi)/2,h-.56,z+s*(w*.5-.26),hi-lo,.34,.27,"#8a7158");
  for(let x=lo+.38;x<hi;x+=3.6){tube(b,[x,-.15,z+s*(w*.5-.15)],[x,h+.42,z+s*(w*.5-.15)],.165,.195,"#937657",7);tube(b,[x,h+.25,z+s*(w*.5-.15)],[x,h+.39,z+s*(w*.5-.15)],.205,.205,"#d1b994",7);}
 }
}
function lamp(b,x,y,z){
 tube(b,[x,y,z],[x,y+2.56,z],.083,.069,"#4e615a",7);
 tube(b,[x,y+2.54,z],[x,y+2.70,z],.12,.22,"#52645f",6);
 tube(b,[x,y+2.70,z],[x,y+3.10,z],.17,.205,"#ffe0a0",6,true,false);
 tube(b,[x,y+3.09,z],[x,y+3.36,z],.27,.03,"#53655d",6);
}
function lighthouse(b,x,y,z){
 const seg=(a,d,ra,rd,c)=>tube(b,[x,y+a,z],[x,y+d,z],ra,rd,c,16);
 seg(0,.64,2.41,2.3,"#a9957b");seg(.64,4.2,1.98,1.68,"#efe4c4");seg(4.2,6.38,1.68,1.51,"#bf6552");seg(6.38,9.25,1.51,1.28,"#f6e7c7");seg(9.25,10.28,1.28,1.22,"#c26b55");
 seg(10.28,10.56,1.65,1.69,"#536669");seg(10.56,10.72,1.69,1.62,"#d5bf98");
 tube(b,[x,y+10.70,z],[x,y+12.26,z],1.01,.99,"#ffdf9b",12,true,false);
 for(let i=0;i<10;i++){const a=i*TAU/10;tube(b,[x+1.05*cos(a),y+10.7,z+1.05*sin(a)],[x+1.05*cos(a),y+12.32,z+1.05*sin(a)],.062,.061,"#455a5c",6);const v=[x+1.57*cos(a),y+11.25,z+1.57*sin(a)],q=[x+1.57*cos(a+TAU/10),y+11.25,z+1.57*sin(a+TAU/10)];tube(b,[v[0],y+10.7,v[2]],v,.033,.033,"#607170",5);tube(b,v,q,.038,.038,"#65736d",5);}
 seg(12.25,12.46,1.37,1.38,"#465f62");seg(12.46,13.64,1.51,.14,"#a75f4f");seg(13.64,14.25,.085,.035,"#596664");
 for(const level of [2.8,7.55]){box(b,x,y+level,z-(level<4?1.79:1.42),.50,.83,.18,"#6b7971");box(b,x,y+level+.09,z-(level<4?1.89:1.52),.34,.61,.025,"#ead3a1")}
 box(b,x,y+.62,z-1.89,.90,1.7,.21,"#6c6555");
}
function boatModel(sail,variant){
 const b=[],hull=variant?"#486c71":"#a96852",stations=[[-2.22,.04],[-1.48,.76],[-.43,.94],[1.03,.81],[1.93,.32],[2.26,.03]];
 for(let i=1;i<stations.length;i++){
  const [z0,w0]=stations[i-1],[z1,w1]=stations[i];
  for(const s of [-1,1]){
   quad(b,[s*w0*.59,.06,z0],[s*w1*.59,.06,z1],[s*w1,.73,z1],[s*w0,.73,z0],hull,[s,0,0]);
   quad(b,[s*w0*.95,.625,z0],[s*w1*.95,.625,z1],[s*w1,.72,z1],[s*w0,.72,z0],"#f4d7a6",[s,.05,0]);
  }
  quad(b,[-w0,.728,z0],[w0,.728,z0],[w1,.728,z1],[-w1,.728,z1],"#ba9369",[0,1,0]);
 }
 if(sail){
  tube(b,[0,.7,.3],[0,5.75,.3],.075,.042,"#826d53",7);
  tri(b,[-.022,5.57,.28],[-.24,1.60,-1.77],[-.018,1.52,.28],"#f4dfb6",[-1,.1,0]);
  tri(b,[.035,4.92,.38],[.055,1.50,.46],[.18,1.27,2.02],variant?"#c78e65":"#e7b982",[1,.1,0]);
  tube(b,[0,1.44,.31],[-.25,1.53,-1.8],.042,.040,"#927657",6);
  tri(b,[0,5.71,.30],[0,5.46,.30],[-.07,5.57,-.4],"#bc6755",[1,0,0]);
 }else{box(b,0,.72,.35,1.02,.82,1.32,"#e4d0ac");box(b,0,1.52,.35,1.22,.13,1.54,"#526d6c");box(b,0,1.02,-.32,.77,.32,.037,"#5f8388");}
 return b;
}
const boats=[boatModel(false,true),boatModel(true,false),boatModel(true,true)];
function addBoat(b,x,z,theta,s,kind){stamp(b,boats[kind],frame(x,.016,z,theta,0,0,s))}
function roofDepot(b,t){
 const x=t.x,z=t.z,H=t.base;
 box(b,x,2.02,z,6.8,H-2.03,5.8,"#d7c5a5");box(b,x,H-.39,z,7.13,.29,6.1,"#587570");box(b,x,H-.11,z,7.18,.11,6.13,"#bcb99b");
 for(const s of [-1,1]){box(b,x+s*3.52,H,z,.14,.28,6.12,"#d6c7a3");box(b,x,H,z+s*3.0,7.18,.26,.14,"#d5c5a2")}
 for(const k of [-1,0,1]){box(b,x+k*2.02,4.25,z-2.92,1.18,1.61,.13,"#977e66");box(b,x+k*2.02,4.39,z-2.997,.94,1.33,.04,"#e6b682");box(b,x+k*2.02,4.39,z-3.027,.07,1.32,.023,"#7e8170")}
 box(b,x+t.side*2.45,H,z+1.97,.67,1.29,.62,"#ac8870");tube(b,[x+t.side*2.46,H+1.28,z+1.96],[x+t.side*2.46,H+1.72,z+1.96],.25,.24,"#716e61",9);
}
function addTown(t,i){
 const b=[],r=random(907+i*379),s=t.side,cx=t.x+s*(t.roof?13.5:24.5),cz=t.z+6.3,h=2.35;
 makeIsland(b,cx,cz,t.roof?22:18.8,22.5,h,471+i*37);
 if(t.roof){roofDepot(b,t);jetty(b,cx-s*24,cx-s*17,cz+10,1.87,2.45);}
 else{jetty(b,t.x-s*2.9,cx-s*11.8,t.z);lamp(b,t.x+s*5.2,2.05,t.z+1.64);stamp(b,crateRaw,frame(t.x+s*3.4,2.05,t.z-.79,.2,0,0,.78))}
 const occ=[];
 const avoid=(x,z,d)=>occ.some(p=>Math.hypot(x-p[0],z-p[1])<d+p[2]);
 if(i===0){lighthouse(b,cx-4,h,cz-4.8);occ.push([cx-4,cz-4.8,3.2]);}
 if(t.roof)occ.push([t.x,t.z,4.5]);
 for(const [a,d] of [[-8,-8],[1,-12],[8,-8],[-7,2],[2,-1],[8,3],[-6,11],[3,12],[11,-2]]){
  const x=cx+s*a,z=cz+d;if(avoid(x,z,2.63))continue;const sc=.83+r()*.26;
  stamp(b,houses[(i*2+occ.length)%8],frame(x,h+.022,z,r()*.75-.375,0,0,sc));occ.push([x,z,2.8]);
 }
 for(let k=0;k<48;k++){
  const a=r()*TAU,rad=Math.sqrt(r())*.87,x=cx+cos(a)*rad*(t.roof?22:18.8),z=cz+sin(a)*rad*22.5;
  if(avoid(x,z,1.14))continue;const idx=r()<.77?Math.floor(r()*3):3+Math.floor(r()*3);
  stamp(b,trees[idx],frame(x,h+.021,z,r()*TAU,0,0,.58+r()*.70));occ.push([x,z,.63]);
 }
 disk(b,t.x,t.base+.014,t.z,.96,.82,"#e8cea1",22);
 disk(b,t.x,t.base+.020,t.z,.79,.66,"#688781",22);
 fishMark(b,t.x-.037,t.base+.028,t.z,.88,"#f0dbaf");
 addBoat(b,t.x+s*(t.roof?0:6),t.z-(t.roof?13:3.50),s*1.34,.86,0);
 sceneryPart(b,cx,6,cz,43);
}
function villageIsland(cx,cz,rx,rz,seed){
 const b=[],r=random(seed);makeIsland(b,cx,cz,rx,rz,2.0,seed);
 const sites=[[-.39,-.35],[.26,-.42],[-.09,.025],[.49,.16],[-.41,.4],[.13,.51]];
 for(let i=0;i<sites.length;i++){const p=sites[i];stamp(b,houses[(seed+i)%8],frame(cx+p[0]*rx,2.03,cz+p[1]*rz,r()*.83-.415,0,0,.85+r()*.27))}
 for(let i=0;i<33;i++){const a=r()*TAU,rad=.58+r()*.30;stamp(b,trees[Math.floor(r()*6)],frame(cx+rx*cos(a)*rad,2.025,cz+rz*sin(a)*rad,r()*TAU,0,0,.55+r()*.60))}
 const sign=cx>0?-1:1;jetty(b,cx+rx*.78*sign,cx+rx*1.22*sign,cz-.14*rz,1.85,2.4);
 addBoat(b,cx+rx*1.12*sign,cz-.14*rz-2.42,sign*1.27,.72,0);
 sceneryPart(b,cx,4,cz,Math.max(rx,rz)+10);
}
function shoreX(z,s){return s*(73+9*sin(z*.025+s*.5)+6.5*sin(z*.051+2*s)+4.1*cos(z*.079))}
function shoreHeight(z,k,s){return k===0? .55:k===1?2.6+.48*sin(z*.034):k===2?5.1+2.3*sin(z*.025+s):k===3?14+5.4*sin(z*.032+s):31+13*sin(z*.019+s*1.6)+5*sin(z*.053)}
const shoreWs=[0,5.2,18,42,87];
function ground(z,d,s){let k=0;while(k<3&&d>shoreWs[k+1])k++;return mix(shoreHeight(z,k,s),shoreHeight(z,k+1,s),clamp((d-shoreWs[k])/(shoreWs[k+1]-shoreWs[k]),0,1))}
function addBanks(){
 for(const s of [-1,1]){
  const idx=s>0?1:0;
  for(let z=-174;z<=522;z+=6)shoreEdges[idx].push([shoreX(z,s),z]);
  for(let chunk=0;chunk<8;chunk++){
   const b=[],r=random(8812+chunk*351+s*90),base=-174+chunk*90;
   const row=z=>shoreWs.map((w,k)=>[shoreX(z,s)+s*w,shoreHeight(z,k,s),z]);
   for(let j=0;j<10;j++){
    const z=base+j*9,A=row(z),B=row(z+9);
    quad(b,[A[0][0]-s*.53,-.23,z],[B[0][0]-s*.53,-.23,z+9],B[0],A[0],"#d5bf9d",[-s,1,0]);
    for(let k=1;k<5;k++)quad(b,A[k-1],B[k-1],B[k],A[k],tone(["","#cab99a","#a5a47c","#959e78","#84957b"][k],.973+r()*.050),[0,1,0]);
    // A narrow shore road catches the last light.
    const rd=(zz,d)=>[shoreX(zz,s)+s*d,ground(zz,d,s)+.08,zz];
    quad(b,rd(z,9),rd(z+9,9),rd(z+9,10.8),rd(z,10.8),"#c7bb94",[0,1,0]);
   }
   for(let j=0;j<44;j++){
    const z=base+r()*89,d=14+r()*59,idxTree=(r()<.83?Math.floor(r()*3):3+Math.floor(r()*3));
    stamp(b,trees[idxTree],frame(shoreX(z,s)+s*d,ground(z,d,s)-.08,z,r()*TAU,0,0,.72+r()*1.21));
   }
   if(chunk>0&&chunk<7)for(let j=0;j<4;j++){
    const z=base+9+j*17+r()*3,d=6.2+r()*1.9;
    stamp(b,houses[(j+chunk+(s>0?3:0))%8],frame(shoreX(z,s)+s*d,ground(z,d,s)-.18,z,-s*(1.2+r()*.46),0,0,.91+r()*.39));
   }
   sceneryPart(b,s*110,12,base+45,85,"bank");
  }
 }
}
function mountains(){
 for(let layer=0;layer<3;layer++){
  const b=[],r=random(919+layer*843),n=[99,106,93][layer],rad=[930,676,479][layer],colors=["#bd9eae","#a38b9c","#858694"],rows=[];
  for(let i=0;i<n;i++){
   const a=i*TAU/n,wd=.99+.039*cos(3*a+layer)+r()*.022;
   const h=rad*(.050+.067*Math.pow(.5+.5*sin(a*7+layer*3.2),1.2)+.068*(.5+.5*cos(a*13+2*layer))+.064*r())*(1-.33*Math.exp(-Math.pow((a-.7)/.41,2)));
   const v=(off,rr,yy)=>[sin(a+off)*rad*rr,yy,109+cos(a+off)*rad*rr];
   rows.push([v(0,wd*.69,-3.3),v(.009*(r()-.5),wd*(.788+r()*.04),h*(.29+.15*r())),v(.014*(r()-.5),wd*(.91+r()*.035),h*(.69+.1*r())),v(0,wd,h)]);
  }
  for(let i=0;i<n;i++){
   const a=rows[i],p=rows[(i+1)%n],out=[-a[0][0],180,109-a[0][2]];
   for(let j=0;j<3;j++){
    tri(b,a[j],p[j],(i%2?p[j+1]:a[j+1]),tone(colors[layer],.934+r()*.073),out);
    if(i%2)tri(b,a[j],p[j+1],a[j+1],tone(colors[layer],.948+r()*.075),out);
    else tri(b,p[j],p[j+1],a[j+1],tone(colors[layer],.95+r()*.077),out);
   }
  }
  sceneryPart(b,0,34,109,rad*1.3,"mountain");
 }
}
function cloudCluster(x,y,z,scale,seed){
 const b=[],r=random(seed);
 for(let i=0;i<7;i++){const f=i-3,h=(1-abs(f)/4)*1.42+r()*.6;oval(b,x+f*4.75*scale,y+h*scale*.65,z+r()*2.5*scale,(4.9+r()*3.1)*scale,h*1.8*scale,(2.7+r()*2.2)*scale,i%2?"#ffe0bf":"#f8d2b9",10,5)}
 oval(b,x+3.5*scale,y-.91*scale,z+scale,19.6*scale,1.35*scale,4.4*scale,"#f2c4b1",13,5);
 sceneryPart(b,x,y,z,27*scale,"cloud");
}
function buildWorld(){
 mountains();addBanks();stops.forEach(addTown);
 villageIsland(-35,13,14.8,20.1,182);villageIsland(44,-6,14.1,17.2,277);villageIsland(46,132,15.4,18.2,353);villageIsland(-47,202,14.4,21.2,461);villageIsland(48,280,14.0,19.1,573);villageIsland(-40,368,18.2,22.8,641);
 const b=[];for(const [x,z,a,s,k] of [[-8,15,-.82,1.1,1],[25,7,.95,.88,0],[-18,67,.66,1.03,2],[16,91,-.55,.88,1],[-2,149,1.16,.87,2],[-11,231,.57,.91,1],[19,252,-.65,1.03,2],[2,312,.41,.96,1],[-12,390,.77,1.03,2]])addBoat(b,x,z,a,s,k);
 sceneryPart(b,0,3,194,225,"boats");
 [[-108,48,195,1.53,201],[-53,94,273,2,310],[51,82,249,1.22,16],[171,72,316,1.44,181],[283,104,269,2.1,91],[-188,52,438,2.2,38],[64,85,526,1.7,944],[249,131,558,2.35,276],[-269,84,122,2.23,159],[-74,67,-250,2.86,743],[296,83,-175,2.13,559]].forEach(a=>cloudCluster(...a));
}
buildWorld();
const receivers=stops.map((t,i)=>mesh(catModel(i)));
const birdRaw=[];
for(const side of [-1,1]){
 tri(birdRaw,[0,0,.12],[side*.69,.22,.17],[side*.83,.16,-.08],"#f2d8bd",[0,1,0]);
 tri(birdRaw,[0,0,.12],[side*.83,.16,-.08],[0,-.02,-.20],"#dfc7b2",[0,-1,0]);
 tri(birdRaw,[side*.69,.22,.17],[side*1.31,.04,-.16],[side*.83,.16,-.08],"#766e6d",[0,1,0]);
}
const flockRaw=[];[[-8,.4,1],[-5,1.36,2],[-2.6,1.6,3.5],[1,1,1.9],[3.9,.79,1.1],[7.2,-.1,-.72]].forEach((p,i)=>stamp(flockRaw,birdRaw,frame(p[0],p[1],p[2],-.52,.04,sin(i)*.18,.74+i*.04)));
const flock=mesh(flockRaw),sparkRaw=[];
for(let i=0;i<4;i++){const a=i*PI/2,d=a+PI/2,p=[cos(a)*.11,0,sin(a)*.11],q=[cos(d)*.11,0,sin(d)*.11];tri(sparkRaw,[0,.22,0],p,q,"#ffdf92",0);tri(sparkRaw,[0,-.19,0],p,q,"#fff2aa",0)}
const spark=mesh(sparkRaw);
const pilot={x:-4.4,y:16.8,z:-16,vx:0,vy:0,yaw:0,pitch:0,bank:0,pace:10.7};
const input={kind:"none",nx:0,ny:0,tx:0,ty:17,drag:null,sx:0,sy:0,startX:0,startY:17};
const flightTime=90;
let demo=location.hash==="#demo",demoClock=0,phase="ready",resumePhase="ready",view=0,remaining=flightTime,sim=0,released=0,landed=0,points=0,parcels=[],bursts=[],around=null,noticeAge=0,dusk=0,activeCam=null;
let width=1,height=1,pixelRatio=1,frameTime=0,clockStamp=0,radarClock=0,uiClock=0,softMode=false;
let audioCtx=null,sound=false,motor=null,motorGain=null,filter=null;
const cameraButtons=Array.from(document.querySelectorAll(".cam"));
const keys=new Set();
function ambient(){
 if(!audioCtx||!motorGain)return;
 const fly=sound&&(phase==="ready"||phase==="running"||phase==="coast");
 motorGain.gain.setTargetAtTime(fly? .037:0,audioCtx.currentTime,.10);
 if(motor)motor.frequency.setTargetAtTime(38+pilot.pace*.5+pilot.pitch*12,audioCtx.currentTime,.22);
}
function soundSwitch(){
 try{
  if(!audioCtx){const A=window.AudioContext||window.webkitAudioContext;if(!A)return;audioCtx=new A();motor=audioCtx.createOscillator();motor.type="sawtooth";filter=audioCtx.createBiquadFilter();filter.type="lowpass";filter.frequency.value=186;motorGain=audioCtx.createGain();motorGain.gain.value=0;motor.connect(filter).connect(motorGain).connect(audioCtx.destination);motor.start();}
  if(audioCtx.state==="suspended"){const op=audioCtx.resume();if(op&&op.catch)op.catch(()=>{})}
  sound=!sound;ambient();el("soundGlyph").setAttribute("href",sound?"#i-sound":"#i-mute");
  const label=sound?"Mute sound":"Turn sound on";el("soundButton").setAttribute("aria-label",label);el("soundButton").title=label;el("soundButton").setAttribute("aria-pressed",String(sound));
 }catch(_){sound=false}
}
function chime(notes){
 if(!sound||!audioCtx)return;
 notes.forEach((hz,i)=>{const t=audioCtx.currentTime+i*.086,o=audioCtx.createOscillator(),g=audioCtx.createGain();o.type="sine";o.frequency.value=hz;g.gain.setValueAtTime(.001,t);g.gain.exponentialRampToValueAtTime(.09,t+.023);g.gain.exponentialRampToValueAtTime(.001,t+.40);o.connect(g).connect(audioCtx.destination);o.start(t);o.stop(t+.41)});
}
function notice(s,seconds=2){
 el("notice").textContent=s;el("notice").classList.add("show");noticeAge=seconds;
}
function setCamera(n){
 view=n;cameraButtons.forEach((b,i)=>b.setAttribute("aria-pressed",String(i===n)));
 document.querySelector(".instruments").style.visibility=n===1?"hidden":"visible";
 if(width>1)activeCam=flightCamera();
}
function renderHUD(){
 const t=stops[released],secs=Math.ceil(Math.max(0,remaining));
 el("timeLeft").textContent=Math.floor(secs/60)+":"+String(secs%60).padStart(2,"0");
 el("daybar").style.transform="scaleX("+clamp(remaining/flightTime,0,1)+")";
 el("stock").textContent=String(stops.length-released);el("score").textContent=String(points).padStart(4,"0");
 el("dispatchLabel").textContent=t?"Next delivery":"Bramble Lake";el("place").textContent=t?t.name:"Route flown";el("serial").textContent=String(Math.min(released+1,5)).padStart(2,"0")+" / 05";
 Array.from(el("legs").children).forEach((b,i)=>b.className=i<released?"done":i===released?"here":"");
 el("alt").textContent=String(Math.round(pilot.y));el("speed").textContent=String(Math.round(Math.hypot(pilot.vx,pilot.vy,pilot.pace)*3.6));
 el("mobileHint").hidden=phase!=="ready"||demo;
}
function reset(toDemo=false,ready=true){
 demo=toDemo;demoClock=0;phase=ready&&!demo?"ready":"running";remaining=flightTime;released=0;landed=0;points=0;parcels=[];bursts=[];around=null;dusk=0;
 Object.assign(pilot,{x:-4.4,y:16.8,z:-16,vx:0,vy:0,yaw:0,pitch:0,bank:.015,pace:10.7});
 Object.assign(input,{kind:"none",tx:pilot.x,ty:pilot.y,drag:null});keys.clear();noticeAge=0;el("notice").classList.remove("show");
 el("modal").hidden=true;el("parcelCam").hidden=true;el("demoFlag").hidden=!demo;el("pauseButton").disabled=false;
 el("pauseButton").setAttribute("aria-label","Pause flight");el("pauseButton").title="Pause flight";el("pauseGlyph").setAttribute("href","#i-pause");
 setCamera(0);renderHUD();ambient();
}
function pauseGame(){
 if(phase==="paused"){phase=resumePhase;el("modal").hidden=true;el("pauseGlyph").setAttribute("href","#i-pause");el("pauseButton").setAttribute("aria-label","Pause flight");el("pauseButton").title="Pause flight";clockStamp=performance.now();ambient();return}
 if(phase==="won"||phase==="lost")return;
 resumePhase=phase;phase="paused";keys.clear();input.drag=null;
 el("modalTitle").textContent="Flight paused";el("modalCopy").textContent="Your route will be here when you’re ready.";
 el("receipt").hidden=true;el("playText").textContent="Resume flight";el("playGlyph").setAttribute("href","#i-play");
 el("modal").hidden=false;el("pauseGlyph").setAttribute("href","#i-play");el("pauseButton").setAttribute("aria-label","Resume flight");el("pauseButton").title="Resume flight";ambient();
}
function finish(win){
 phase=win?"won":"lost";el("modalTitle").textContent=win?"Route complete!":"Night has fallen";
 el("modalCopy").textContent=win?"Five happy cats have their supper. Good flying, courier.":"The last light has faded over Bramble Lake.";
 el("finalCount").textContent=landed+" / 5";el("finalScore").textContent=String(points).padStart(4,"0");
 el("receipt").hidden=false;el("playText").textContent="Fly again";el("playGlyph").setAttribute("href","#i-again");
 el("modal").hidden=false;el("pauseButton").disabled=true;el("waypoint").hidden=true;el("parcelCam").hidden=true;
 if(win)chime([523.25,659.25,783.99,1046.5]);ambient();renderHUD();
}
function camera(eye,forward,tan,aspect,roll=0){
 const f=norm(forward),rt=norm([f[2],0,-f[0]]),up=cross(f,rt),c=cos(roll),s=sin(roll);
 return {eye,f,r:rt.map((v,i)=>v*c+up[i]*s),u:up.map((v,i)=>v*c-rt[i]*s),lx:tan*aspect,ly:tan};
}
function planeY(){return pilot.y+.11*sin(sim*1.58)+.035*sin(sim*2.91)}
function flightCamera(){
 const p=pilot,aspect=width/height,k=clamp(1.03/aspect,1,2.1),y=planeY();
 if(view===1){
  const m=frame(p.x,y,p.z,p.yaw,p.pitch,p.bank),eye=point(m,[0,1.67,.02]),ya=p.yaw,pi=p.pitch*.86-.045;
  return camera(eye,[sin(ya)*cos(pi),sin(pi),cos(ya)*cos(pi)],.646,aspect,p.bank*.80);
 }
 if(view===2){
  const ya=p.yaw*.67,fw=[sin(ya),0,cos(ya)],alt=(48+5*(k-1));
  return camera([p.x-fw[0]*1.4,y+alt,p.z-fw[2]*1.4],[fw[0]*.366,-.93,fw[2]*.366],.685,aspect,p.bank*.045);
 }
 const heading=around?p.yaw:p.yaw*.47,cs=cos(heading),sn=sin(heading);
 const safeLag=clamp(aspect*2.24,1,5),cx=p.x-clamp(p.x*.235,-safeLag,safeLag),cy=y-clamp((p.y-16.8)*.26,-1.6,1.6);
 const eye=[cx-(cs*5.25+sn*11.72)*k,cy+4.05*k,p.z+(sn*5.25-cs*11.72)*k];
 const ya=heading+.246,pi=-.140+p.pitch*.09;
 return camera(eye,[sin(ya)*cos(pi),sin(pi),cos(ya)*cos(pi)],aspect<.9? .55:.50,aspect,p.bank*.072);
}
function project(c,x,y,z){
 const dx=x-c.eye[0],dy=y-c.eye[1],dz=z-c.eye[2],f=dx*c.f[0]+dy*c.f[1]+dz*c.f[2],d=Math.max(.12,f);
 return {nx:(dx*c.r[0]+dy*c.r[1]+dz*c.r[2])/(d*c.lx),ny:(dx*c.u[0]+dy*c.u[1]+dz*c.u[2])/(d*c.ly),z:f};
}
function targetAim(t){
 if(demo)return [t.x,t.y];
 if(keys.size&&input.kind==="keys")return [input.tx,input.ty];
 if(input.kind!=="mouse"||!activeCam)return [input.tx,input.ty];
 const c=activeCam,dir=c.f.map((v,i)=>v+c.r[i]*input.nx*c.lx+c.u[i]*input.ny*c.ly);
 let x,y;
 if(view===2){
  const f=(t.y-c.eye[1])/Math.min(-.12,dir[1]);x=c.eye[0]+dir[0]*f;
  const g=project(c,t.x,t.y,t.z);y=t.y+(input.ny-clamp(g.ny,-1,1))*3.8;
 }else{
  const lead=clamp(t.z-pilot.z,2.3,view===1?22:24),travel=(pilot.z+lead-c.eye[2])/Math.max(.22,dir[2]);
  x=c.eye[0]+dir[0]*travel;y=c.eye[1]+dir[1]*travel;
 }
 return [clamp(x,-48,48),clamp(y,8.5,33)];
}
function release(t){
 const i=released++;
 parcels.push({i,t,sx:pilot.x,sz:pilot.z,start:planeY()-.67,age:0,duration:demo?2.48:3.25,scored:false});
 bursts.push({x:t.x,y:t.y,z:t.z,age:0});
 chime([660,880]);renderHUD();
 if(released===stops.length)phase="coast";
}
function dropPose(d){
 const u=clamp(d.age/d.duration,0,1),fall=u*(.81+.19*u),sway=sin(u*PI*2.4)*sin(PI*u);
 const x=mix(d.sx,d.t.x,ease(u))+sway*.70,z=mix(d.sz,d.t.z,ease(u))+sin(PI*u)*.47,y=mix(d.start,d.t.base,fall);
 const open=ease(clamp(d.age/.36,0,1)),fold=clamp((d.age-d.duration)/.64,0,1);
 const scale=(.18+.82*open)*(1-.91*ease(fold));
 return {x,y,z,scale,roll:sway*.1,cy:y+.91+2.45*scale,cx:x+.14*sway+fold*.5};
}
function animateDrops(dt){
 for(const d of parcels){
  d.age+=dt;
  if(!d.scored&&d.age>=d.duration){d.scored=true;landed++;points+=100;chime([784,988,1175]);notice("Fish delivered!  +100",demo?1.05:1.75);renderHUD();}
 }
 parcels=parcels.filter(d=>d.age<d.duration+1.1);
 for(const b of bursts)b.age+=dt;bursts=bursts.filter(b=>b.age<1.1);
}
function fly(dt){
 const p=pilot,t=stops[released];
 if(around){
  const a=around;a.age+=dt;const u=clamp(a.age/5.7,0,1),th=TAU*u,e=ease(u),de=6*u*(1-u),r=13,s=a.side;
  p.x=mix(a.x,a.endX,e)+s*r*(1-cos(th));p.z=a.z+r*sin(th)-29*e;p.y=mix(a.y,t.y,e)+1.4*sin(PI*u);
  const dx=(a.endX-a.x)*de+s*r*TAU*sin(th),dz=r*TAU*cos(th)-29*de;
  p.yaw=Math.atan2(dx,dz);p.bank=-s*.34*sin(PI*u);p.pitch=.062*cos(th);p.vx=dx/5.7;p.vy=0;p.pace=abs(dz)/5.7;
  if(u>=1){around=null;p.yaw=0;p.bank=0;p.vx=0;p.pitch=0;if(input.kind!=="mouse"){input.tx=p.x;input.ty=p.y}}
  return;
 }
 if(!t||phase==="night"){
  p.pace=approach(p.pace,phase==="night"?2.1:7,1.4,dt);p.z+=p.pace*dt;p.bank=approach(p.bank,0,2,dt);p.pitch=approach(p.pitch,.03,2,dt);return;
 }
 if(input.kind==="keys"){
  input.tx=clamp(input.tx+((keys.has("ArrowRight")?1:0)-(keys.has("ArrowLeft")?1:0))*13*dt,-48,48);
  input.ty=clamp(input.ty+((keys.has("ArrowUp")?1:0)-(keys.has("ArrowDown")?1:0))*8.5*dt,8.5,33);
 }
 let [wantX,wantY]=targetAim(t);
 const range=t.z-p.z,offset=Math.hypot(wantX-t.x,wantY-t.y);
 if(range<17&&offset<RAD*1.15){const help=.28*(1-range/17);wantX=mix(wantX,t.x,help);wantY=mix(wantY,t.y,help)}
 p.pace=approach(p.pace,demo?22.2:10.7,6,dt);
 const vx=clamp((wantX-p.x)*(demo?3.6:2.25),demo?-21:-15,demo?21:15),vy=clamp((wantY-p.y)*2.35,-8.5,8.5);
 p.vx=approach(p.vx,vx,5.15,dt);p.vy=approach(p.vy,vy,5.3,dt);
 const prev=[p.x,p.y,p.z];p.x=clamp(p.x+p.vx*dt,-49,49);p.y=clamp(p.y+p.vy*dt,8.3,33.3);p.z+=p.pace*dt;
 p.yaw=approach(p.yaw,clamp(p.vx*.038,-.42,.42),3.2,dt);p.bank=approach(p.bank,clamp(-p.vx*.038,-.48,.48),3.9,dt);p.pitch=approach(p.pitch,clamp(p.vy*.036,-.22,.22),3.5,dt);
 if(prev[2]<=t.z&&p.z>=t.z){
  const f=clamp((t.z-prev[2])/(p.z-prev[2]||1),0,1),dx=mix(prev[0],p.x,f)-t.x,dy=mix(prev[1],p.y,f)-t.y;
  if(Math.hypot(dx,dy)<=RAD+.32)release(t);
  else{around={age:0,x:p.x,y:p.y,z:t.z,side:p.x>t.x?-1:1,endX:mix(p.x,t.x,.53)};notice("Missed the ring · coming around",3);}
 }
}
function update(dt){
 if(phase==="paused")return;
 sim+=dt;
 if(noticeAge>0){noticeAge-=dt;if(noticeAge<=0)el("notice").classList.remove("show")}
 if(demo){
  demoClock+=dt;if(demoClock>=20.4){reset(true,false);return;}
  const n=demoClock<5.9?0:demoClock<10.5?1:demoClock<15.0?2:0;if(view!==n)setCamera(n);
 }
 if(phase==="ready"||phase==="won"||phase==="lost")return;
 remaining=demo?Math.max(0,flightTime*(1-demoClock/20.4)):Math.max(0,remaining-dt);
 if(remaining<=0&&released<5)phase="night";
 dusk=demo? .75*Math.pow(clamp(demoClock/20.4,0,1),1.72):Math.pow(1-remaining/flightTime,1.62);
 fly(dt);animateDrops(dt);
 if(phase==="coast"&&landed===5){const last=parcels[parcels.length-1];if(!last||last.age>=last.duration+.48)finish(true)}
 if(phase==="night"&&!parcels.some(d=>!d.scored))finish(false);
}
let gl=null,soft=null,surface=canvas,skyP=null,geoP=null,ringP=null,flatBuffer=null,boundGeo=null;
function program(vsId,fsId,attrs,uniforms){
 const make=(type,id)=>{const s=gl.createShader(type);gl.shaderSource(s,el(id).textContent);gl.compileShader(s);if(!gl.getShaderParameter(s,gl.COMPILE_STATUS))throw Error(gl.getShaderInfoLog(s));return s};
 const p=gl.createProgram(),v=make(gl.VERTEX_SHADER,vsId),f=make(gl.FRAGMENT_SHADER,fsId);
 gl.attachShader(p,v);gl.attachShader(p,f);attrs.forEach((key,i)=>gl.bindAttribLocation(p,i,key));gl.linkProgram(p);gl.deleteShader(v);gl.deleteShader(f);
 if(!gl.getProgramParameter(p,gl.LINK_STATUS))throw Error(gl.getProgramInfoLog(p));
 const obj={p};uniforms.forEach(key=>obj[key]=gl.getUniformLocation(p,key));return obj;
}
function useSoftware(){
 if(softMode)return;softMode=true;gl=null;
 surface=document.createElement("canvas");surface.id="world";surface.tabIndex=0;surface.setAttribute("aria-label",canvas.getAttribute("aria-label"));canvas.replaceWith(surface);soft=surface.getContext("2d",{alpha:false});
}
function initGraphics(){
 try{
  gl=canvas.getContext("webgl",{alpha:false,antialias:true,depth:true,powerPreference:"high-performance",preserveDrawingBuffer:false});
  if(!gl){useSoftware();return}
  const camU=["uEye","uRight","uUp","uFront","uLens"];
  skyP=program("sky-vs","sky-fs",["aXY"],[...camU,"uTime","uDusk","uPlane"]);
  geoP=program("mesh-vs","mesh-fs",["aPos","aNormal","aColor"],[...camU,"uModel","uMirror","uTime","uDusk"]);
  ringP=program("ring-vs","ring-fs",["aXY"],[...camU,"uCenter","uRadius","uTime","uStrength"]);
  flatBuffer=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,flatBuffer);gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);
  gl.disable(gl.CULL_FACE);gl.depthFunc(gl.LEQUAL);gl.clearColor(.65,.65,.68,1);
  canvas.addEventListener("webglcontextlost",e=>{e.preventDefault();useSoftware();resize(true)});
 }catch(_){useSoftware()}
}
function camUniform(pr,c){
 gl.useProgram(pr.p);gl.uniform3fv(pr.uEye,c.eye);gl.uniform3fv(pr.uFront,c.f);gl.uniform3fv(pr.uRight,c.r);gl.uniform3fv(pr.uUp,c.u);gl.uniform2f(pr.uLens,c.lx,c.ly);
}
function flat(){
 gl.bindBuffer(gl.ARRAY_BUFFER,flatBuffer);gl.enableVertexAttribArray(0);gl.disableVertexAttribArray(1);gl.disableVertexAttribArray(2);gl.vertexAttribPointer(0,2,gl.FLOAT,false,8,0);boundGeo=null;
}
function drawGeo(g,m=ID){
 if(!g.buffer){g.buffer=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,g.buffer);gl.bufferData(gl.ARRAY_BUFFER,g.data,gl.STATIC_DRAW);boundGeo=null}
 if(boundGeo!==g){gl.bindBuffer(gl.ARRAY_BUFFER,g.buffer);gl.vertexAttribPointer(0,3,gl.FLOAT,false,36,0);gl.vertexAttribPointer(1,3,gl.FLOAT,false,36,12);gl.vertexAttribPointer(2,3,gl.FLOAT,false,36,24);boundGeo=g}
 gl.uniformMatrix4fv(geoP.uModel,false,m);gl.drawArrays(gl.TRIANGLES,0,g.count);
}
function visible(c,o){
 if(o.kind==="mountain")return true;
 const v=[o.x-c.eye[0],o.y-c.eye[1],o.z-c.eye[2]],z=dot(v,c.f),r=o.r;
 return z+r>.13&&z-r<1350&&abs(dot(v,c.r))<Math.max(0,z)*c.lx+r*(1+c.lx)&&abs(dot(v,c.u))<Math.max(z,0)*c.ly+r*(1+c.ly);
}
function movingObjects(inset){
 const result=[],add=(g,m)=>result.push({g,m});
 if(!inset){
  const p=pilot,m=frame(p.x,planeY(),p.z,p.yaw,p.pitch,p.bank+.012*sin(sim*1.4));
  add(aircraft,m);add(prop,product(m,frame(0,.061,2.38,0,0,sim*38.2)));
  if(view!==1){
   const look=phase==="ready"?-1.24+.13*sin(sim*.79):-.22+.26*sin(sim*1.02)-.70*Math.pow(Math.max(0,cos(sim*.61)),7);
   add(pilotHead,product(m,frame(0,1.32,-.20,look,.025,.038*sin(sim*1.22),1.05)));
   add(scarf,product(m,frame(-.16,1.01,-.52,.095*sin(sim*5.1),.11*sin(sim*7.4),.12*sin(sim*5.4),1.12)));
   if(released<5)add(crate,product(m,frame(0,.527,-1.41,0,0,0,.58)));
  }
  add(flock,frame(-8+sin(sim*.094)*4,29+sin(sim*.43)*.54,80+sin(sim*.075)*6,.42,0,cos(sim*.49)*.09,1.02));
 }
 for(let i=0;i<stops.length;i++){
  const t=stops[i],d=parcels.find(v=>v.i===i);
  if(inset?!d:abs(t.z-pilot.z)>145)continue;
  const hop=d&&d.scored?Math.max(0,sin(PI*clamp((d.age-d.duration)/.65,0,1)))*.62:0;
  const turn=PI+.34*t.side+.12*sin(sim*.98+i),look=d&&!d.scored? .10:0;
  add(receivers[i],frame(t.x+t.side*1.60,t.base+hop,t.z+.53,turn,look,-t.side*hop*.12,1.13));
 }
 for(const d of parcels){
  const p=dropPose(d);add(crate,frame(p.x,p.y,p.z,.08*sin(d.age*2.4),0,p.roll));
  if(d.age<d.duration+.65)add(canopy,frame(p.cx,p.cy,p.z,0,0,p.roll*.54,p.scale));
 }
 if(!inset)for(const b of bursts)for(let i=0;i<14;i++){
  const a=i*TAU/14+.2,big=RAD+b.age*(1.6+i%3*.48),s=1.46*(1-b.age/1.1);
  add(spark,frame(b.x+cos(a)*big,b.y+sin(a)*big-b.age*b.age*.68,b.z+.6*sin(i*2.59)*b.age,0,0,a+sim,s));
 }
 return result;
}
function gates(inset){
 if(inset||released>=5)return [];
 let a=[{t:stops[released],strength:1}];if(released+1<5)a.push({t:stops[released+1],strength:.64});return a.reverse();
}
function gpuScene(c,rect,inset){
 const x=Math.round(rect.x*pixelRatio),y=surface.height-Math.round((rect.y+rect.h)*pixelRatio),w=Math.round(rect.w*pixelRatio),h=Math.round(rect.h*pixelRatio);
 gl.enable(gl.SCISSOR_TEST);gl.scissor(x,y,w,h);gl.viewport(x,y,w,h);gl.depthMask(true);gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
 gl.disable(gl.BLEND);gl.disable(gl.DEPTH_TEST);gl.depthMask(false);camUniform(skyP,c);flat();
 gl.uniform1f(skyP.uTime,sim%1500);gl.uniform1f(skyP.uDusk,dusk);gl.uniform3f(skyP.uPlane,pilot.x,pilot.y,pilot.z);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
 gl.enable(gl.DEPTH_TEST);gl.depthMask(true);camUniform(geoP,c);gl.uniform1f(geoP.uTime,sim%1500);gl.uniform1f(geoP.uDusk,dusk);
 gl.enableVertexAttribArray(0);gl.enableVertexAttribArray(1);gl.enableVertexAttribArray(2);boundGeo=null;
 const objects=scenery.filter(o=>visible(c,o));
 if(c.f[1]>-.85){
  gl.enable(gl.BLEND);gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA);gl.uniform1f(geoP.uMirror,1);
  for(const o of objects)if(o.kind!=="cloud")drawGeo(o.g);
  gl.disable(gl.BLEND);gl.clear(gl.DEPTH_BUFFER_BIT);
 }
 gl.uniform1f(geoP.uMirror,0);for(const o of objects)drawGeo(o.g);
 for(const o of movingObjects(inset))drawGeo(o.g,o.m);
 const goals=gates(inset);
 if(goals.length){
  camUniform(ringP,c);flat();gl.enable(gl.BLEND);gl.blendFunc(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA);gl.depthMask(false);
  for(const {t,strength} of goals){
   gl.uniform3f(ringP.uCenter,t.x,t.y,t.z);gl.uniform1f(ringP.uRadius,RAD);gl.uniform1f(ringP.uTime,sim%1500);gl.uniform1f(ringP.uStrength,strength);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
  }
  gl.depthMask(true);gl.disable(gl.BLEND);
 }
 gl.disable(gl.SCISSOR_TEST);
}
function skyColors(night){const blend=(a,b)=>"rgb("+a.map((v,i)=>Math.round(mix(v,b[i],night))).join(",")+")";return {high:blend([171,161,180],[31,38,64]),mid:blend([242,169,155],[74,68,98]),low:blend([255,213,166],[106,86,116]),far:blend([209,178,163],[69,70,95]),near:blend([86,146,153],[24,55,73])}}
function softBackdrop(ctx,c,w,h){
 const horizon=h*(.5+c.f[1]/(Math.max(.1,c.u[1])*c.ly)*.5),hz=clamp(horizon,0,h),colors=skyColors(dusk);
 let sky=ctx.createLinearGradient(0,-h*.05,0,Math.max(h*.18,horizon+.5));sky.addColorStop(0,colors.high);sky.addColorStop(.52,colors.mid);sky.addColorStop(1,colors.low);ctx.fillStyle=sky;ctx.fillRect(0,0,w,h);
 const sun=[mix(.44,.68,ease(clamp((c.lx/c.ly-.62)/.78,0,1))),.196-dusk*.29,.86],dir=norm(sun),pr=project(c,c.eye[0]+dir[0]*800,c.eye[1]+dir[1]*800,c.eye[2]+dir[2]*800),sx=(.5+.5*pr.nx)*w,sy=(.5-.5*pr.ny)*h,sr=h*.064/(2*c.ly*Math.max(.25,dot(c.f,dir)));
 if(pr.z>0&&sx>-sr*5&&sx<w+sr*5){
  const glow=ctx.createRadialGradient(sx,sy,0,sx,sy,sr*4.1);glow.addColorStop(0,"rgba(255,218,139,.7)");glow.addColorStop(.35,"rgba(255,192,110,.29)");glow.addColorStop(1,"rgba(255,184,122,0)");ctx.fillStyle=glow;ctx.fillRect(sx-sr*4.1,sy-sr*4.1,sr*8.2,sr*8.2);ctx.fillStyle="#fff2c7";ctx.beginPath();ctx.arc(sx,sy,sr,0,TAU);ctx.fill();
 }
 if(hz<h){const water=ctx.createLinearGradient(0,hz,0,h);water.addColorStop(0,colors.far);water.addColorStop(.51,colors.near);water.addColorStop(1,colors.near);ctx.fillStyle=water;ctx.fillRect(0,hz,w,h-hz);
  const r=random(777);
  for(let i=0;i<(w<310?90:490);i++){
   const v=Math.pow(r(),1.62),y=hz+v*(h-hz),x=r()*w+sin(sim*.34+i)*3,ww=(.9+r()*13)*(v+.22)*w/720;
   const inSun=abs(x-sx)<(sr*.8+v*sr*2.5)*(.4+r());
   ctx.fillStyle=inSun?"rgba(255,219,153,"+(.23+r()*.44)*(1-dusk*.69)+")":i%4?"rgba(216,231,208,.24)":"rgba(49,99,116,.16)";
   ctx.fillRect(x,y,ww*(inSun?2.2:1),.42+v*1.6);
  }
 }
}
const colorCache=new Map();let paletteDusk=-1;
function softColors(g){
 if(g.ink)return g.ink;const data=g.data,n=data.length/27,ink=new Uint32Array(n),lit=norm([.64,.81,.37]);
 for(let i=0;i<n;i++){const j=i*27,nx=data[j+3],ny=data[j+4],nz=data[j+5],sun=Math.max(0,nx*lit[0]+ny*lit[1]+nz*lit[2]),light=.74+.15*ny+.23*sun,glow=nx===0&&ny===0&&nz===0;
  const rr=clamp(Math.round((glow?data[j+6]:data[j+6]*light+.05*sun+.018*(1-sun))*255),0,255),gg=clamp(Math.round((glow?data[j+7]:data[j+7]*light+.004*sun+.019*(1-sun))*255),0,255),bb=clamp(Math.round((glow?data[j+8]:data[j+8]*light-.021*sun+.034*(1-sun))*255),0,255);
  ink[i]=(glow?16777216:0)+rr*65536+gg*256+bb;
 }
 g.ink=ink;return ink;
}
function softColor(code,z,mirror){
 const stage=Math.round(dusk*22);if(stage!==paletteDusk){colorCache.clear();paletteDusk=stage}
 const f=Math.round(clamp(1-Math.exp(-Math.max(0,z-12)*.00172),0,.83)*20),key=code*64+f*2+(mirror?1:0);
 if(colorCache.has(key))return colorCache.get(key);
 const n=stage/22,k=f/20,glow=code>16777215;
 let r=(code>>16)&255,g=(code>>8)&255,b=code&255;
 if(!glow){r=r*(1-.665*n)+3*n;g=g*(1-.59*n)+5*n;b=b*(1-.457*n)+7*n}
 r=mix(r,254-172*n,k);g=mix(g,200-123*n,k);b=mix(b,172-72*n,k);
 if(mirror){r=mix(r,99-40*n,.4);g=mix(g,143-70*n,.40);b=mix(b,150-60*n,.4)}
 const color="rgb("+Math.round(r)+","+Math.round(g)+","+Math.round(b)+")";colorCache.set(key,color);return color;
}
function collectSoft(out,g,m,c,w,h,mirror,detailed){
 const data=g.data,ink=softColors(g),my=mirror?-1:1,eye=c.eye;
 const eyeIn=[m[12]-eye[0],m[13]*my-eye[1],m[14]-eye[2]];
 const coeff=v=>[m[0]*v[0]+m[1]*my*v[1]+m[2]*v[2],m[4]*v[0]+m[5]*my*v[1]+m[6]*v[2],m[8]*v[0]+m[9]*my*v[1]+m[10]*v[2],dot(eyeIn,v)];
 const A=coeff(c.r),B=coeff(c.u),C=coeff(c.f),hw=w*.5,hh=h*.5,fx=hw/c.lx,fy=-hh/c.ly;
 const coord=(i,M)=>data[i]*M[0]+data[i+1]*M[1]+data[i+2]*M[2]+M[3];
 for(let i=0;i<data.length;i+=27){
  const z0=coord(i,C),z1=coord(i+9,C),z2=coord(i+18,C);if(Math.min(z0,z1,z2)<.14)continue;
  const z=(z0+z1+z2)/3;if(z>(detailed?1750:450))continue;
  const x0=hw+fx*coord(i,A)/z0,x1=hw+fx*coord(i+9,A)/z1,x2=hw+fx*coord(i+18,A)/z2;
  if((x0<0&&x1<0&&x2<0)||(x0>w&&x1>w&&x2>w))continue;
  const y0=hh+fy*coord(i,B)/z0,y1=hh+fy*coord(i+9,B)/z1,y2=hh+fy*coord(i+18,B)/z2;
  if((y0<0&&y1<0&&y2<0)||(y0>h&&y1>h&&y2>h))continue;
  if(abs((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0))<(detailed? .11:.47))continue;
  out.push([z,softColor(ink[i/27],z,mirror),x0,y0,x1,y1,x2,y2]);
 }
}
function softRing(ctx,t,strength,c,w,h){
 const pp=project(c,t.x,t.y,t.z);if(pp.z<.4)return;
 const r=RAD*h/(pp.z*c.ly*2);if(r<1||r>w*2.6)return;
 const draw=(scale,start,end)=>{ctx.beginPath();const n=Math.max(8,Math.ceil(62*(end-start)/TAU));for(let i=0;i<=n;i++){const a=start+(end-start)*i/n,p=project(c,t.x+RAD*scale*cos(a),t.y+RAD*scale*sin(a),t.z);if(p.z<.16)continue;const x=(.5+p.nx*.5)*w,y=(.5-p.ny*.5)*h;if(!i)ctx.moveTo(x,y);else ctx.lineTo(x,y)}ctx.stroke()};
 ctx.save();ctx.globalAlpha=strength;ctx.lineJoin="round";ctx.strokeStyle="rgba(255,195,69,.25)";ctx.lineWidth=Math.max(3,r*.16);draw(1,0,TAU);
 ctx.strokeStyle="#ffc44e";ctx.lineWidth=Math.max(1.7,r*.079);ctx.shadowBlur=Math.min(18,r*.23);ctx.shadowColor="#ffcc65";draw(1,0,TAU);
 ctx.shadowBlur=0;ctx.lineWidth=Math.max(.75,r*.028);ctx.strokeStyle="#fff7bf";draw(1,0,TAU);
 ctx.strokeStyle="#ffe6a0";ctx.lineWidth=Math.max(.7,r*.014);for(let i=0;i<4;i++){const a=i*PI/2-sim*.08;draw(1.105,a-.16,a+.16)}ctx.restore();
}
function paintSoft(ctx,items,c,w,h){
 items.sort((a,b)=>b[0]-a[0]);let paint="";
 for(const p of items){
  if(p[1]===null){softRing(ctx,p[2],p[3],c,w,h);paint="";continue}
  if(paint!==p[1]){paint=p[1];ctx.fillStyle=paint}ctx.beginPath();ctx.moveTo(p[2],p[3]);ctx.lineTo(p[4],p[5]);ctx.lineTo(p[6],p[7]);ctx.closePath();ctx.fill();
 }
}
function softScene(c,rect,inset){
 const ctx=soft,w=rect.w,h=rect.h;ctx.save();ctx.setTransform(pixelRatio,0,0,pixelRatio,rect.x*pixelRatio,rect.y*pixelRatio);ctx.beginPath();ctx.rect(0,0,w,h);ctx.clip();
 softBackdrop(ctx,c,w,h);const objects=scenery.filter(o=>visible(c,o)),list=[];
 if(c.f[1]>-.82){
  ctx.save();const hz=clamp(h*(.5+c.f[1]/(Math.max(.1,c.u[1])*c.ly)*.5),0,h);ctx.beginPath();ctx.rect(0,hz,w,h-hz);ctx.clip();
  for(const o of objects)if(o.kind!=="cloud"&&(o.kind==="mountain"||Math.hypot(o.x-c.eye[0],o.z-c.eye[2])<135))collectSoft(list,o.g,ID,c,w,h,true,o.kind==="mountain");ctx.globalAlpha=.41;paintSoft(ctx,list,c,w,h);list.length=0;ctx.restore();
 }
 for(const o of objects)collectSoft(list,o.g,ID,c,w,h,false,o.kind==="mountain"||o.kind==="cloud");
 for(const o of movingObjects(inset))collectSoft(list,o.g,o.m,c,w,h,false,true);
 for(const {t,strength} of gates(inset)){const p=project(c,t.x,t.y,t.z);if(p.z>.13)list.push([p.z,null,t,strength]);}
 paintSoft(ctx,list,c,w,h);ctx.restore();
}
const chart=el("chart"),mapPen=chart.getContext("2d");
function drawMap(){
 if(!mapPen)return;const ctx=mapPen,n=chart.width,R=n*.5,edge=R-3,scale=n/167,cx=pilot.x*.85,cz=pilot.z+20;
 const xx=x=>R+(x-cx)*scale,yy=z=>R-(z-cz)*scale;
 ctx.clearRect(0,0,n,n);ctx.save();ctx.beginPath();ctx.arc(R,R,edge,0,TAU);ctx.clip();
 ctx.fillStyle="#2c4d58";ctx.fillRect(0,0,n,n);
 ctx.lineWidth=.85;ctx.strokeStyle="#c8d2bd22";ctx.beginPath();
 for(let z=Math.floor((cz-95)/15)*15;z<cz+95;z+=15){ctx.moveTo(0,yy(z));ctx.lineTo(n,yy(z))}
 for(let x=Math.floor((cx-95)/15)*15;x<cx+95;x+=15){ctx.moveTo(xx(x),0);ctx.lineTo(xx(x),n)}ctx.stroke();
 function shape(points,bank=0){
  if(!points.length)return;ctx.beginPath();if(bank)ctx.moveTo(bank<0?-20:n+20,yy(points[0][1]));else ctx.moveTo(xx(points[0][0]),yy(points[0][1]));
  for(const p of points)ctx.lineTo(xx(p[0]),yy(p[1]));if(bank)ctx.lineTo(bank<0?-20:n+20,yy(points[points.length-1][1]));ctx.closePath();
  ctx.fillStyle="#668077";ctx.fill();ctx.strokeStyle="#a3ae8d";ctx.lineWidth=1.65;ctx.stroke();
 }
 shoreEdges.forEach((p,i)=>shape(p.filter(v=>abs(v[1]-cz)<100),i===0?-1:1));
 for(const island of islands){if(abs(island.cz-cz)>110)continue;shape(island.points);ctx.beginPath();island.points.forEach((p,i)=>{const x=xx(mix(p[0],island.cx,.21)),y=yy(mix(p[1],island.cz,.21));if(i)ctx.lineTo(x,y);else ctx.moveTo(x,y)});ctx.closePath();ctx.strokeStyle="#ccd0a43d";ctx.lineWidth=1;ctx.stroke()}
 ctx.setLineDash([3.5,5]);ctx.lineWidth=2;ctx.strokeStyle="#ebc68cb5";ctx.beginPath();ctx.moveTo(xx(-4.4),yy(-16));stops.forEach(t=>ctx.lineTo(xx(t.x),yy(t.z)));ctx.stroke();ctx.setLineDash([]);
 stops.forEach((t,i)=>{const x=xx(t.x),y=yy(t.z);if(y<0||y>n)return;ctx.beginPath();ctx.arc(x,y,i===released?8.1:4.7,0,TAU);ctx.strokeStyle=i===released?"#ffdb89":"#b6bc9f";ctx.lineWidth=i===released?2.7:1.75;ctx.stroke();ctx.beginPath();ctx.arc(x,y,i===released?3.2:1.85,0,TAU);ctx.fillStyle=i<released?"#95cfb4":"#ffd38b";ctx.fill()});
 ctx.save();ctx.translate(xx(pilot.x),yy(pilot.z));ctx.rotate(pilot.yaw);ctx.shadowColor="#122b36";ctx.shadowBlur=4;ctx.beginPath();[[0,-12],[2.2,-8.8],[2.6,-2.8],[12,3.7],[11.5,6.0],[2,3.6],[1.8,8.1],[5.3,10.6],[5.1,12],[0,10.9],[-5.1,12],[-5.3,10.6],[-1.8,8.1],[-2,3.6],[-11.5,6],[-12,3.7],[-2.6,-2.8],[-2.2,-8.8]].forEach((p,i)=>i?ctx.lineTo(p[0],p[1]):ctx.moveTo(p[0],p[1]));ctx.closePath();ctx.fillStyle="#ffbf7c";ctx.fill();ctx.lineWidth=1.3;ctx.strokeStyle="#4b5148";ctx.stroke();ctx.restore();
 ctx.restore();ctx.save();ctx.translate(R,R);
 for(let i=0;i<60;i++){const a=i*TAU/60,len=i%5?3.7:8;ctx.strokeStyle=i%5?"#d1d2b774":"#e8d7b5";ctx.lineWidth=i%5?1:1.8;ctx.beginPath();ctx.moveTo(sin(a)*(edge-2),-cos(a)*(edge-2));ctx.lineTo(sin(a)*(edge-2-len),-cos(a)*(edge-2-len));ctx.stroke()}
 ctx.font="bold 19px monospace";ctx.textAlign="center";ctx.textBaseline="middle";ctx.lineWidth=3;ctx.strokeStyle="#36515a";ctx.fillStyle="#ffe0a9";ctx.strokeText("N",0,-R+26);ctx.fillText("N",0,-R+26);ctx.restore();
}
const visor=document.createElement("canvas");visor.setAttribute("aria-hidden","true");visor.style.cssText="position:absolute;inset:0;width:100%;height:100%;z-index:1;pointer-events:none;display:none";scene.insertBefore(visor,scene.firstElementChild.nextSibling);
const dash=visor.getContext("2d");
function dial(ctx,x,y,r,label,value,units,frac){
 ctx.save();ctx.translate(x,y);const circ=(rad,color)=>{ctx.beginPath();ctx.arc(0,0,rad,0,TAU);ctx.fillStyle=color;ctx.fill()};
 circ(r+3,"#112a30");circ(r+1.8,"#c2aa83");circ(r-.4,"#405454");circ(r-2,"#21383e");
 const start=PI*.76,angle=PI*1.49;
 for(let i=0;i<=20;i++){const a=start+angle*i/20,major=i%5===0;ctx.strokeStyle=major?"#f4ddae":"#b7baaa";ctx.lineWidth=major?1.5:.8;ctx.beginPath();ctx.moveTo(cos(a)*(r-4),sin(a)*(r-4));ctx.lineTo(cos(a)*(r-(major?9:6.6)),sin(a)*(r-(major?9:6.6)));ctx.stroke()}
 ctx.textAlign="center";ctx.fillStyle="#deceb0";ctx.font="bold "+Math.max(6.5,r*.185)+"px monospace";ctx.fillText(label,0,-r*.29);
 ctx.fillStyle="#f4e4bf";ctx.font="bold "+Math.max(8,r*.29)+"px monospace";ctx.fillText(value+"",0,r*.56);
 ctx.font=Math.max(6,r*.16)+"px monospace";ctx.fillStyle="#c0c8b6";ctx.fillText(units,0,r*.77);
 const a=start+angle*clamp(frac,0,1);ctx.rotate(a);ctx.beginPath();ctx.moveTo(r*.78,0);ctx.lineTo(-r*.12,2.4);ctx.lineTo(-r*.18,0);ctx.lineTo(-r*.12,-2.4);ctx.closePath();ctx.fillStyle="#f7a269";ctx.fill();circ(2.9,"#dec79a");ctx.restore();
}
function drawCockpit(){
 if(view!==1||!dash){visor.style.display="none";return}
 visor.style.display="block";const ctx=dash,w=width,h=height,pr=pixelRatio;ctx.setTransform(pr,0,0,pr,0,0);ctx.clearRect(0,0,w,h);
 const ph=clamp(h*.245,47,155),pw=Math.min(w*.96,985),cx=w*.5,cy=h+ph*.28;
 ctx.save();ctx.beginPath();ctx.moveTo(cx-pw*.58,h+3);ctx.lineTo(cx-pw*.385,h-ph*1.43);ctx.quadraticCurveTo(cx,h-ph*2.00,cx+pw*.385,h-ph*1.43);ctx.lineTo(cx+pw*.58,h+3);ctx.closePath();
 const glass=ctx.createLinearGradient(0,h-ph*2,0,h);glass.addColorStop(0,"rgba(235,247,213,.105)");glass.addColorStop(.6,"rgba(112,181,181,.053)");glass.addColorStop(1,"rgba(99,151,155,.13)");ctx.fillStyle=glass;ctx.fill();ctx.strokeStyle="#4d635dcc";ctx.lineWidth=4;ctx.stroke();ctx.strokeStyle="#e4d8b3";ctx.lineWidth=1.4;ctx.stroke();ctx.restore();
 const arch=(radX,radY)=>{ctx.beginPath();ctx.ellipse(cx,cy,radX,radY,0,PI,TAU);ctx.lineTo(cx+radX,h+ph);ctx.lineTo(cx-radX,h+ph);ctx.closePath()};
 arch(pw*.605,ph*1.29);ctx.fillStyle="#533c33";ctx.fill();ctx.lineWidth=5;ctx.strokeStyle="#bb8b69";ctx.stroke();
 arch(pw*.588,ph*1.195);const panel=ctx.createLinearGradient(0,h-ph,0,h);panel.addColorStop(0,"#455751");panel.addColorStop(1,"#253d40");ctx.fillStyle=panel;ctx.fill();ctx.strokeStyle="#b49470";ctx.lineWidth=1.8;ctx.stroke();
 const cyDial=h-ph*.36,r=Math.min(ph*.37,pw*.078);
 dial(ctx,cx-r*2.53,cyDial,r,"AIR SPEED",Math.round(Math.hypot(pilot.pace,pilot.vx)*3.6),"km/h",Math.hypot(pilot.pace,pilot.vx)*3.6/85);
 dial(ctx,cx,cyDial-r*.13,r*1.07,"ALTITUDE",Math.round(pilot.y),"m",pilot.y/40);
 const deg=(Math.round(pilot.yaw*180/PI)+360)%360;dial(ctx,cx+r*2.53,cyDial,r,"HEADING",String(deg).padStart(3,"0"),"deg",.5+Math.sin(pilot.yaw)*.40);
 for(const s of [-1,1]){const x=cx+s*pw*.36;ctx.fillStyle="#b4a483";ctx.beginPath();ctx.arc(x,h-ph*.23,2.35,0,TAU);ctx.fill();ctx.strokeStyle="#53605a";ctx.beginPath();ctx.moveTo(x-1.1,h-ph*.23+1);ctx.lineTo(x+1.1,h-ph*.23-1);ctx.stroke()}
}
function resize(force=false){
 const rect=scene.getBoundingClientRect(),w=Math.max(1,rect.width),h=Math.max(1,rect.height),pr=softMode?Math.min(1.18,window.devicePixelRatio||1):Math.min(1.65,window.devicePixelRatio||1,Math.sqrt(3200000/(w*h)));
 if(!force&&abs(width-w)<.05&&abs(height-h)<.05&&pr===pixelRatio)return;
 width=w;height=h;pixelRatio=pr;surface.width=Math.max(1,Math.round(w*pr));surface.height=Math.max(1,Math.round(h*pr));visor.width=surface.width;visor.height=surface.height;activeCam=flightCamera();
}
function drawTarget(c){
 const mark=el("waypoint"),t=stops[released];
 if(!t||around||phase==="won"||phase==="lost"||phase==="night"){mark.hidden=true;return}
 const p=project(c,t.x,t.y+RAD+1.1,t.z),edgeX=clamp((p.nx*.5+.5)*width,34,width-34),edgeY=clamp((.5-p.ny*.5)*height,28,height-14);
 if(p.z<2.2){mark.hidden=true;return}
 const arrow=p.nx>1?" →":p.nx<-1?" ←":"";
 const dist=Math.max(1,Math.round(Math.hypot(pilot.x-t.x,pilot.y-t.y,pilot.z-t.z)));
 el("distance").textContent=dist+" m"+arrow;mark.style.left=edgeX+"px";mark.style.top=edgeY+"px";mark.hidden=false;
}
function parcelCamera(d,aspect){
 const p=dropPose(d),span=Math.max(1.9,p.cy+p.scale-d.t.base),range=clamp(4.3+span*1.22,8.1,32.5);
 const focus=[mix(d.t.x+d.t.side*.66,p.x,.30),d.t.base+span*.49,d.t.z+.22];
 const eye=[focus[0]-.43*range,focus[1]+.24*range,focus[2]-.87*range];
 return camera(eye,sub(focus,eye),.49,aspect);
}
function draw(){
 const c=flightCamera();activeCam=c;const render=softMode?softScene:gpuScene;render(c,{x:0,y:0,w:width,h:height},false);
 drawCockpit();drawTarget(c);
 const d=parcels[parcels.length-1],panel=el("parcelCam");
 if(d&&phase!=="won"&&phase!=="lost"){
  panel.hidden=false;el("parcelTitle").textContent=d.scored?"PARCEL DELIVERED":"PARCEL CAM";
  const a=scene.getBoundingClientRect(),r=el("parcelWindow").getBoundingClientRect(),v={x:r.left-a.left,y:r.top-a.top,w:r.width,h:r.height};
  if(r.width>2&&r.height>2){render(parcelCamera(d,v.w/v.h),v,true);if(view===1&&dash)dash.clearRect(v.x-2,v.y-22,v.w+4,v.h+24)}
 }else panel.hidden=true;
}
function begin(){if(phase==="ready"){phase="running";renderHUD();ambient()}}
function positionInput(e){
 const r=scene.getBoundingClientRect();input.nx=clamp(2*(e.clientX-r.left)/r.width-1,-1,1);input.ny=clamp(1-2*(e.clientY-r.top)/r.height,-1,1);
}
function endDemo(){if(demo)reset(false,true)}
window.addEventListener("pointerdown",endDemo,true);window.addEventListener("touchstart",endDemo,{passive:true,capture:true});
scene.addEventListener("pointerdown",e=>{
 if(!e.isPrimary||el("modal").contains(e.target)||!(phase==="ready"||phase==="running"))return;
 begin();positionInput(e);
 if(e.pointerType==="mouse"){input.kind="mouse";return}
 input.kind="touch";input.drag=e.pointerId;input.sx=e.clientX;input.sy=e.clientY;input.startX=pilot.x;input.startY=pilot.y;input.tx=pilot.x;input.ty=pilot.y;
 try{scene.setPointerCapture(e.pointerId)}catch(_){}
});
scene.addEventListener("pointermove",e=>{
 if(demo||!e.isPrimary||!(phase==="ready"||phase==="running")||el("modal").contains(e.target))return;
 if(e.pointerType==="mouse"){positionInput(e);input.kind="mouse";begin();return}
 if(input.drag!==e.pointerId)return;positionInput(e);
 input.tx=clamp(input.startX+(e.clientX-input.sx)/width*69,-48,48);input.ty=clamp(input.startY-(e.clientY-input.sy)/height*34,8.5,33);
 const t=stops[released];if(activeCam&&t){const p=project(activeCam,t.x,t.y,t.z),rr=RAD*height/(2*activeCam.ly*Math.max(1,p.z));if(p.z>1&&Math.hypot((p.nx-input.nx)*width*.5,(p.ny-input.ny)*height*.5)<Math.max(21,rr*.93)){input.tx=t.x;input.ty=t.y;}}
});
const pointerUp=e=>{if(input.drag===e.pointerId)input.drag=null};
scene.addEventListener("pointerup",pointerUp);scene.addEventListener("pointercancel",pointerUp);scene.addEventListener("lostpointercapture",pointerUp);
cameraButtons.forEach((b,i)=>b.addEventListener("click",()=>{endDemo();setCamera(i)}));
el("soundButton").addEventListener("click",soundSwitch);el("pauseButton").addEventListener("click",pauseGame);
el("playButton").addEventListener("click",()=>{if(phase==="paused")pauseGame();else reset(false,false);surface.focus({preventScroll:true})});
window.addEventListener("keydown",e=>{
 if(e.altKey||e.ctrlKey||e.metaKey)return;
 if(["ArrowLeft","ArrowRight","ArrowUp","ArrowDown"].includes(e.key)){
  if(phase==="paused"||phase==="won"||phase==="lost")return;e.preventDefault();endDemo();begin();if(input.kind!=="keys"){input.tx=pilot.x;input.ty=pilot.y}input.kind="keys";keys.add(e.key);
 }else if(!e.repeat&&(e.code==="Escape"||e.code==="KeyP")){endDemo();pauseGame()}
 else if(!e.repeat&&["Digit1","Digit2","Digit3"].includes(e.code)){endDemo();setCamera(Number(e.code.slice(-1))-1)}
});
window.addEventListener("keyup",e=>keys.delete(e.key));window.addEventListener("blur",()=>{keys.clear();input.drag=null});
document.addEventListener("visibilitychange",()=>{clockStamp=performance.now();if(document.hidden&&!demo&&(phase==="running"||phase==="coast"))pauseGame()});
initGraphics();resize(true);reset(demo,true);drawMap();
function tick(ts){
 const dt=clamp((ts-(clockStamp||ts))/1000,0,demo? .65:.30);clockStamp=ts;
 // Small physics steps keep ring crossings and falling parcels consistent across frame rates.
 let left=dt;while(left>.00001){const step=Math.min(left,1/45);update(step);left-=step}
 radarClock+=dt;uiClock+=dt;
 if(uiClock>=.10){renderHUD();ambient();uiClock=0}
 if(radarClock>=.085){drawMap();radarClock=0}
 if(ts-frameTime>=(softMode?1000/30-.7:0)){resize();draw();frameTime=ts}
 requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();
</script>
</body>
</html>