File size: 21,064 Bytes
d6c8fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2addd8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c8fba
 
 
2addd8a
d6c8fba
 
2addd8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c8fba
 
 
2addd8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d6c8fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// MAIN Lab 3D scenes (Three.js r169, ES modules via the import map in index.html).
// Hero: a big block of cubes ("big AI") streams into the Pixel M's blue core ("small AI").
// About: the Pixel M assembles from scattered cubes as the section scrolls into view.
// If WebGL is missing or anything fails, index.html falls back to the 2D canvas.
import * as THREE from 'three';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { UnrealBloomPass } from 'three/addons/postprocessing/UnrealBloomPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';

const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
const small = matchMedia('(max-width: 760px)').matches;
const NAVY = 0x0B1437, BLUE = 0x2F6BFF;
// Neutral tone mapping subtracts a small offset from dark colours; this input comes out as exactly #0B1437.
const BG = new THREE.Color().setRGB(.0233, .0269, .0579);
const P = [[0,0],[4,0],[0,1],[1,1],[3,1],[4,1],[0,2],[2,2],[4,2],[0,3],[4,3],[0,4],[4,4]];
const clamp = (x, a = 0, b = 1) => Math.min(b, Math.max(a, x));
const easeOut = t => 1 - Math.pow(1 - t, 3);
const easeInOut = t => t < .5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
const smooth = t => t * t * (3 - 2 * t);
// Never scale a lit mesh to exactly 0: its normals become NaN and the bloom pass smears NaN over the whole frame.
const MIN = 1e-3; const sc = s => Math.max(MIN, s);

function webglOK() {
  try { const c = document.createElement('canvas'); return !!(c.getContext('webgl2') || c.getContext('webgl')); }
  catch (e) { return false; }
}

/* pointer, shared by both scenes (mouse only, so touch scrolling is never captured) */
const pointer = { x: 0, y: 0 };
addEventListener('pointermove', e => {
  if (e.pointerType !== 'mouse') return;
  pointer.x = e.clientX / innerWidth * 2 - 1;
  pointer.y = e.clientY / innerHeight * 2 - 1;
}, { passive: true });

const geo = new RoundedBoxGeometry(1, 1, 1, 3, .16);
const views = [];

function makeView(el, bloom) {
  const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
  renderer.setPixelRatio(Math.min(devicePixelRatio || 1, small ? 1.5 : 2));
  renderer.toneMapping = THREE.NeutralToneMapping;
  renderer.toneMappingExposure = 1;
  const canvas = renderer.domElement;
  canvas.className = 'gl';
  el.appendChild(canvas);

  const scene = new THREE.Scene();
  scene.background = BG;
  scene.fog = new THREE.Fog(BG, 10, 26);
  const pmrem = new THREE.PMREMGenerator(renderer);
  scene.environment = pmrem.fromScene(new RoomEnvironment(), 0.04).texture;
  scene.environmentIntensity = .3;

  const camera = new THREE.PerspectiveCamera(30, 1, .1, 80);
  const composer = new EffectComposer(renderer);
  composer.addPass(new RenderPass(scene, camera));
  composer.addPass(new UnrealBloomPass(new THREE.Vector2(256, 256), bloom.strength, bloom.radius, bloom.threshold));
  composer.addPass(new OutputPass());

  scene.add(new THREE.HemisphereLight(0xC9D7FF, NAVY, .45));
  const key = new THREE.DirectionalLight(0xffffff, 1.35); key.position.set(4, 6, 8); scene.add(key);
  const rim = new THREE.DirectionalLight(0x6E9BFF, 1.2); rim.position.set(-6, -2, -5); scene.add(rim);

  const v = { el, renderer, scene, camera, composer, w: 1, h: 1, visible: true, baseZ: 10, onResize: null, tick: null };
  const resize = () => {
    const r = el.getBoundingClientRect();
    v.w = Math.max(1, r.width); v.h = Math.max(1, r.height);
    renderer.setSize(v.w, v.h, false); composer.setSize(v.w, v.h);
    camera.aspect = v.w / v.h; camera.updateProjectionMatrix();
    if (v.onResize) v.onResize(v);
    if (reduce) renderOnce(v);
  };
  v.resize = resize;
  v.fit = (W, H) => {
    const half = THREE.MathUtils.degToRad(camera.fov) / 2;
    v.baseZ = Math.max(H / 2 / Math.tan(half), W / 2 / (Math.tan(half) * camera.aspect)) * 1.08;
    scene.fog.near = v.baseZ - 1; scene.fog.far = v.baseZ + 16;
  };
  new ResizeObserver(resize).observe(el);
  new IntersectionObserver(([e]) => { v.visible = e.isIntersecting; }, { rootMargin: '100px' }).observe(el);
  views.push(v);
  return v;
}

function renderOnce(v) { if (v.tick) { v.tick(99, 0); v.composer.render(0); } }

/* soft round sprite for the dust */
function dotTexture() {
  const c = document.createElement('canvas'); c.width = c.height = 64;
  const g = c.getContext('2d'), gr = g.createRadialGradient(32, 32, 0, 32, 32, 32);
  gr.addColorStop(0, 'rgba(255,255,255,1)'); gr.addColorStop(.4, 'rgba(255,255,255,.35)'); gr.addColorStop(1, 'rgba(255,255,255,0)');
  g.fillStyle = gr; g.fillRect(0, 0, 64, 64);
  const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; return t;
}

function dust(scene, n, spread) {
  const pos = new Float32Array(n * 3);
  for (let i = 0; i < n; i++) { pos[i * 3] = (Math.random() - .5) * spread; pos[i * 3 + 1] = (Math.random() - .5) * spread * .6; pos[i * 3 + 2] = -2 - Math.random() * 8; }
  const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.BufferAttribute(pos, 3));
  const pts = new THREE.Points(g, new THREE.PointsMaterial({ size: .09, map: dotTexture(), color: 0x6E9BFF, transparent: true, opacity: .55, depthWrite: false }));
  scene.add(pts); return pts;
}

/* ---------------- hero: distillation ---------------- */
function heroScene() {
  const stage = document.getElementById('stage');
  const v = makeView(stage, { strength: .7, radius: .45, threshold: .96 });
  const { scene, camera } = v;
  const root = new THREE.Group(); scene.add(root);
  const motes = dust(scene, small ? 120 : 260, 22);

  /* big AI: a 7x7x7 block with gaps */
  let seed = 7; const rnd = () => ((seed = (seed * 16807) % 2147483647) / 2147483647);
  const lat = new THREE.Group(); root.add(lat);
  const N = 7, SP = .44, CS = .3, cells = [];
  for (let x = 0; x < N; x++) for (let y = 0; y < N; y++) for (let z = 0; z < N; z++) {
    if (rnd() < .22) continue;
    const p = new THREE.Vector3((x - 3) * SP, (y - 3) * SP, (z - 3) * SP);
    cells.push({ p, d: p.length() / (3 * SP * Math.sqrt(3)), ph: rnd() * 6.28, h: 0, hT: 0, back: 0 });
  }
  const latMesh = new THREE.InstancedMesh(geo, new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: .34, metalness: .12 }), cells.length);
  const col = new THREE.Color(), cA = new THREE.Color(0xDCE3F2), cB = new THREE.Color(0x4A5B88);
  cells.forEach((c, i) => latMesh.setColorAt(i, col.copy(cA).lerp(cB, clamp(rnd() * .7 + c.d * .3))));
  lat.add(latMesh);

  /* small AI: the Pixel M with a glowing core */
  const mG = new THREE.Group(); root.add(mG);
  const MS = .5, MC = .42;
  const outer = P.filter(([x, y]) => !(x === 2 && y === 2));
  const mMesh = new THREE.InstancedMesh(geo, new THREE.MeshStandardMaterial({ color: 0xE9EEF8, roughness: .28, metalness: .12 }), outer.length);
  const mBase = outer.map(([x, y], i) => ({ p: new THREE.Vector3((x - 2) * MS, (2 - y) * MS, 0), delay: .9 + i * .055 }));
  mG.add(mMesh);
  const coreMat = new THREE.MeshStandardMaterial({ color: BLUE, emissive: BLUE, emissiveIntensity: 2, roughness: .3, metalness: .1 });
  const core = new THREE.Mesh(geo, coreMat); mG.add(core);
  const coreLight = new THREE.PointLight(BLUE, 2, 4, 2); mG.add(coreLight);

  /* the stream */
  const PN = 44;
  const pMesh = new THREE.InstancedMesh(geo, new THREE.MeshBasicMaterial({ color: 0xffffff }), PN);
  pMesh.frustumCulled = false; scene.add(pMesh);
  const parts = Array.from({ length: PN }, () => ({ on: false, t: 0, dur: 1, a: new THREE.Vector3(), c: new THREE.Vector3(), cell: -1, s0: CS }));
  const white = new THREE.Color(.86, .89, .96), hot = new THREE.Color(.5, 1.15, 4.6);
  for (let i = 0; i < PN; i++) pMesh.setColorAt(i, white);

  v.onResize = () => {
    const aspect = v.w / v.h;
    if (aspect < .9) {
      lat.position.set(0, 1.3, 0); lat.scale.setScalar(.8);
      mG.position.set(0, -1.75, 0); mG.scale.setScalar(.85); v.fit(4.6, 7.4);
    } else if (aspect < 1.6) {
      lat.position.set(-1.5, 0, 0); lat.scale.setScalar(.82);
      mG.position.set(1.75, 0, 0); mG.scale.setScalar(.8); v.fit(7, 3.9);
    } else {
      lat.position.set(-2.25, 0, 0); lat.scale.setScalar(1);
      mG.position.set(2.4, 0, 0); mG.scale.setScalar(1); v.fit(10.4, 4.6);
    }
    v.mY = mG.position.y;
  };

  const d = new THREE.Object3D(), tmp = new THREE.Vector3(), target = new THREE.Vector3();
  let yaw = 0, pitch = 0, pulse = 0, acc = 0;

  function launch() {
    const free = [];
    for (let i = 0; i < cells.length; i++) if (cells[i].hT === 0 && cells[i].h < .05) free.push(i);
    const slot = parts.find(p => !p.on);
    if (!free.length || !slot) return;
    const ci = free[(Math.random() * free.length) | 0], c = cells[ci];
    c.hT = 1; c.back = 0;
    slot.on = true; slot.t = 0; slot.dur = 1.5 + Math.random() * 1.1; slot.cell = ci; slot.s0 = CS * lat.scale.x;
    slot.a.copy(c.p).applyMatrix4(latMesh.matrixWorld);
    core.getWorldPosition(target);
    slot.c.lerpVectors(slot.a, target, .5).add(tmp.set(0, (Math.random() - .5) * 2.4, .8 + Math.random() * 1.6));
  }

  v.tick = (t, dt) => {
    const r = stage.getBoundingClientRect();
    const sp = reduce ? 0 : clamp(-r.top / (r.height + 80));
    yaw += (pointer.x * .32 - yaw) * Math.min(1, dt * 2.5);
    pitch += (pointer.y * .16 - pitch) * Math.min(1, dt * 2.5);
    root.rotation.set(pitch + sp * .18, yaw + sp * .7, 0);
    camera.position.set(0, .25, v.baseZ * (1 - sp * .14)); camera.lookAt(0, 0, 0);

    lat.rotation.set(.42 + Math.sin(t * .3) * .06, t * .17, 0);
    mG.rotation.y = -.3 + Math.sin(t * .5) * .1;
    mG.position.y = v.mY + Math.sin(t * .9) * .05;
    motes.rotation.y = t * .01;
    root.updateMatrixWorld(true);

    /* block: grow in from the centre, cubes leave and come back */
    for (let i = 0; i < cells.length; i++) {
      const c = cells[i];
      if (c.back > 0) { c.back -= dt; if (c.back <= 0) c.hT = 0; }
      c.h += (c.hT - c.h) * Math.min(1, dt * 12);
      const intro = easeOut(clamp((t - .1 - c.d * .9) / .7));
      d.position.copy(c.p); d.rotation.set(0, 0, 0);
      d.scale.setScalar(sc(CS * intro * (1 - c.h) * (1 + .035 * Math.sin(t * 1.6 + c.ph))));
      d.updateMatrix(); latMesh.setMatrixAt(i, d.matrix);
    }
    latMesh.instanceMatrix.needsUpdate = true;

    /* M: cubes drop into place */
    mBase.forEach((b, i) => {
      const k = easeOut(clamp((t - b.delay) / .8));
      d.position.set(b.p.x, b.p.y + (1 - k) * 2.6, b.p.z); d.rotation.set((1 - k) * 1.2, 0, 0);
      d.scale.setScalar(sc(MC * k)); d.updateMatrix(); mMesh.setMatrixAt(i, d.matrix);
    });
    mMesh.instanceMatrix.needsUpdate = true;
    const kc = easeOut(clamp((t - 1.6) / .8));
    core.scale.setScalar(sc(MC * kc * (1 + pulse * .22)));
    coreMat.emissiveIntensity = 7 + pulse * 8;
    coreLight.intensity = (2 + pulse * 9) * kc;

    /* stream */
    if (!reduce && t > 2.3) { acc += dt * 5.5; while (acc >= 1) { acc--; launch(); } }
    core.getWorldPosition(target);
    for (let i = 0; i < PN; i++) {
      const p = parts[i];
      if (!p.on) { d.position.set(0, -50, 0); d.scale.setScalar(MIN); d.updateMatrix(); pMesh.setMatrixAt(i, d.matrix); continue; }
      p.t += dt / p.dur; const k = clamp(p.t), e = smooth(k), u = 1 - e;
      d.position.set(u * u * p.a.x + 2 * u * e * p.c.x + e * e * target.x, u * u * p.a.y + 2 * u * e * p.c.y + e * e * target.y, u * u * p.a.z + 2 * u * e * p.c.z + e * e * target.z);
      d.rotation.set(e * 3, e * 2, 0);
      d.scale.setScalar(p.s0 * (1 - e * .72));
      d.updateMatrix(); pMesh.setMatrixAt(i, d.matrix);
      pMesh.setColorAt(i, col.copy(white).lerp(hot, easeInOut(e)));
      if (k >= 1) { p.on = false; pulse = 1; cells[p.cell].back = .6 + Math.random() * 1.6; }
    }
    pMesh.instanceMatrix.needsUpdate = true; pMesh.instanceColor.needsUpdate = true;
    pulse = Math.max(0, pulse - dt * 2.4);
  };
  return v;
}

/* ---------------- about: a phone running the small model offline ---------------- */
function roundRect(g, x, y, w, h, r) { g.beginPath(); g.moveTo(x + r, y); g.arcTo(x + w, y, x + w, y + h, r); g.arcTo(x + w, y + h, x, y + h, r); g.arcTo(x, y + h, x, y, r); g.arcTo(x, y, x + w, y, r); g.closePath(); }
function wrap(g, text, maxW) {
  const words = text.split(' '), lines = []; let line = '';
  for (const w of words) { const t = line ? line + ' ' + w : w; if (g.measureText(t).width > maxW && line) { lines.push(line); line = w; } else line = t; }
  if (line) lines.push(line); return lines;
}
/* the chat on the screen: user asks, the on-device model answers (typed out) */
const CHAT = [
  { me: true, text: 'Bakı haqqında bir cümlə yaz.' },
  { me: false, text: 'Bakı Xəzər dənizinin sahilində yerləşən Azərbaycanın paytaxtıdır.' },
  { me: true, text: 'İnternetsiz işləyirsən?' },
  { me: false, text: 'Bəli. Tamamilə oflayn, telefonunda işləyirəm.' }
];
function drawScreen(g, W, H, step, typed) {
  const F = "'Inter', system-ui, sans-serif";
  const bg = g.createLinearGradient(0, 0, 0, H); bg.addColorStop(0, '#111B38'); bg.addColorStop(1, '#0B1330');
  g.clearRect(0, 0, W, H); g.save(); roundRect(g, 0, 0, W, H, 70); g.clip(); g.fillStyle = bg; g.fillRect(0, 0, W, H);
  /* status bar + island */
  g.fillStyle = '#E3E8F3'; g.font = `600 30px ${F}`; g.fillText('9:41', 54, 66);
  g.fillStyle = '#05091A'; roundRect(g, W / 2 - 92, 26, 184, 52, 26); g.fill();
  g.fillStyle = '#1F2B52'; roundRect(g, W - 190, 38, 140, 38, 19); g.fill();
  g.fillStyle = '#8FB0FF'; g.beginPath(); g.arc(W - 166, 57, 7, 0, 6.3); g.fill();
  g.fillStyle = '#C9D4EC'; g.font = `600 22px ${F}`; g.fillText('Offline', W - 150, 65);
  /* header: Pixel M avatar + model name */
  const ax = 54, ay = 118, s = 9.5;
  g.fillStyle = '#E3E8F3'; roundRect(g, ax, ay, 70, 70, 16); g.fill();
  P.forEach(([x, y]) => { g.fillStyle = x === 2 && y === 2 ? '#2F6BFF' : '#0B1437'; g.fillRect(ax + 11 + x * s, ay + 11 + y * s, s - 2, s - 2); });
  g.fillStyle = '#E8EDF7'; g.font = `700 32px ${F}`; g.fillText('MAIN 270M', ax + 92, ay + 32);
  g.fillStyle = '#8C9BB8'; g.font = `500 23px ${F}`; g.fillText('on-device · Azərbaycan dili', ax + 92, ay + 62);
  g.fillStyle = 'rgba(255,255,255,.08)'; g.fillRect(40, ay + 96, W - 80, 2);
  /* bubbles */
  let y = ay + 130; g.font = `500 29px ${F}`;
  for (let i = 0; i <= step && i < CHAT.length; i++) {
    const m = CHAT[i], txt = (i === step && !m.me) ? m.text.slice(0, typed) : m.text;
    if (!txt) { /* typing dots */ g.fillStyle = '#1C2849'; roundRect(g, 44, y, 120, 60, 30); g.fill(); g.fillStyle = '#8C9BB8'; for (let k = 0; k < 3; k++) { g.beginPath(); g.arc(78 + k * 26, y + 30, 6, 0, 6.3); g.fill(); } y += 80; continue; }
    const lines = wrap(g, m.text, W * .6), bw = Math.max(...lines.map(l => g.measureText(l).width)) + 52, bh = lines.length * 40 + 30;
    const bx = m.me ? W - 44 - bw : 44;
    g.fillStyle = m.me ? '#2F6BFF' : '#1C2849'; roundRect(g, bx, y, bw, bh, 28); g.fill();
    g.fillStyle = m.me ? '#F2F5FB' : '#E3E8F3';
    let left = txt.length;
    lines.forEach((l, k) => { if (left <= 0) return; g.fillText(l.slice(0, left), bx + 26, y + 46 + k * 40); left -= l.length + 1; });
    y += bh + 22;
  }
  /* input bar */
  g.fillStyle = '#18234A'; roundRect(g, 40, H - 132, W - 150, 76, 38); g.fill();
  g.fillStyle = '#6F7A90'; g.font = `500 27px ${F}`; g.fillText('Mesaj yaz…', 76, H - 84);
  g.fillStyle = '#2F6BFF'; g.beginPath(); g.arc(W - 76, H - 94, 38, 0, 6.3); g.fill();
  g.strokeStyle = '#F2F5FB'; g.lineWidth = 5; g.lineCap = 'round'; g.beginPath(); g.moveTo(W - 76, H - 78); g.lineTo(W - 76, H - 110); g.moveTo(W - 90, H - 97); g.lineTo(W - 76, H - 111); g.lineTo(W - 62, H - 97); g.stroke();
  g.restore();
}

function aboutScene() {
  const section = document.getElementById('about'), box = document.getElementById('m3d');
  section.classList.add('has3d'); box.hidden = false;
  const v = makeView(box, { strength: .6, radius: .4, threshold: .97 });
  const { scene, camera } = v;
  dust(scene, small ? 60 : 120, 14);
  const phone = new THREE.Group(); scene.add(phone);
  const PW = 2, PH = 4.1, PD = .24;
  /* titanium frame + glass back */
  const frame = new THREE.Mesh(new RoundedBoxGeometry(PW, PH, PD, 8, .3), new THREE.MeshStandardMaterial({ color: 0x9AA6C2, metalness: .85, roughness: .28 }));
  phone.add(frame);
  const back = new THREE.Mesh(new RoundedBoxGeometry(PW - .06, PH - .06, .02, 6, .27), new THREE.MeshStandardMaterial({ color: 0x1A2550, metalness: .3, roughness: .45 }));
  back.position.z = -PD / 2 - .002; phone.add(back);
  /* camera module on the back */
  const bump = new THREE.Mesh(new RoundedBoxGeometry(.72, .72, .06, 4, .16), new THREE.MeshStandardMaterial({ color: 0x232F5E, metalness: .5, roughness: .3 }));
  bump.position.set(-PW / 2 + .5, PH / 2 - .5, -PD / 2 - .03); phone.add(bump);
  const lensMat = new THREE.MeshStandardMaterial({ color: 0x05091A, metalness: .9, roughness: .15 });
  [[-.14, .14], [.14, -.14], [-.14, -.14]].forEach(([x, y]) => {
    const l = new THREE.Mesh(new THREE.CylinderGeometry(.11, .11, .08, 32), lensMat); l.rotation.x = Math.PI / 2;
    l.position.set(bump.position.x + x, bump.position.y + y, -PD / 2 - .07); phone.add(l);
  });
  /* side buttons */
  const btnMat = frame.material;
  [[PW / 2 + .015, .7, .5], [-PW / 2 - .015, .9, .28], [-PW / 2 - .015, .45, .42]].forEach(([x, y, h]) => {
    const b = new THREE.Mesh(new RoundedBoxGeometry(.04, h, .09, 2, .02), btnMat); b.position.set(x, y, 0); phone.add(b);
  });
  /* screen: a live canvas texture */
  const cv = document.createElement('canvas'); cv.width = 600; cv.height = 1260;
  const g = cv.getContext('2d');
  const tex = new THREE.CanvasTexture(cv); tex.colorSpace = THREE.SRGBColorSpace; tex.anisotropy = 4;
  const glassW = PW - .1, glassH = PH - .1;
  const screen = new THREE.Mesh(new THREE.PlaneGeometry(glassW, glassH), new THREE.MeshBasicMaterial({ map: tex, transparent: true }));
  screen.position.z = PD / 2 + .012; phone.add(screen);
  const bezel = new THREE.Mesh(new RoundedBoxGeometry(PW - .04, PH - .04, .01, 6, .005), new THREE.MeshStandardMaterial({ color: 0x03050F, roughness: .2, metalness: .1 }));
  bezel.position.z = PD / 2 + .002; phone.add(bezel);
  /* a soft blue glow behind the phone */
  const glow = new THREE.Sprite(new THREE.SpriteMaterial({ map: dotTexture(), color: 0x2F6BFF, transparent: true, opacity: .35, depthWrite: false }));
  glow.scale.set(7, 7, 1); glow.position.z = -1.5; scene.add(glow);
  v.onResize = () => v.fit(3.2, 4.9);

  let key = '', yaw = 0, pitch = 0, chatT = -1;
  const render = (step, typed) => { const k = step + ':' + typed; if (k === key) return; key = k; drawScreen(g, cv.width, cv.height, step, typed); tex.needsUpdate = true; };
  if (document.fonts && document.fonts.ready) document.fonts.ready.then(() => { key = ''; });
  render(-1, 0);

  v.tick = (t, dt) => {
    const r = section.getBoundingClientRect();
    const q = reduce ? 1 : clamp((innerHeight * .95 - r.top) / (innerHeight * .8)), e = easeInOut(q);
    yaw += (pointer.x * .35 - yaw) * Math.min(1, dt * 2.5);
    pitch += (pointer.y * .18 - pitch) * Math.min(1, dt * 2.5);
    /* turns from showing its back to facing you as the section scrolls in */
    phone.rotation.set(.08 + pitch + (1 - e) * .25, -.28 + (1 - e) * 2.6 + yaw + Math.sin(t * .5) * .06, (1 - e) * -.12);
    phone.position.set(0, (1 - e) * -.8 + Math.sin(t * .9) * .05, 0);
    camera.position.set(0, .1, v.baseZ); camera.lookAt(0, 0, 0);
    /* the chat plays once the phone faces you, then loops */
    if (reduce) { render(CHAT.length - 1, 999); return; }
    if (q > .85 && chatT < 0) chatT = 0;
    if (chatT >= 0) {
      chatT += dt;
      const cyc = chatT % 16; let tt = cyc, step = -1, typed = 0;
      for (let i = 0; i < CHAT.length; i++) {
        const m = CHAT[i], dur = m.me ? .9 : .7 + m.text.length * .035;
        if (tt < 0) break;
        step = i; typed = m.me ? 999 : Math.max(0, Math.floor((tt - .7) / .035));
        tt -= dur + .5;
      }
      render(step, typed);
    }
  };
  return v;
}

/* ---------------- boot ---------------- */
function start() {
  if (!webglOK()) throw new Error('no webgl');
  const hero = heroScene();
  aboutScene();
  views.forEach(v => v.resize());
  if (window.__stop2D) window.__stop2D();
  if (window.ScrollTrigger) window.ScrollTrigger.refresh();
  let last = performance.now(); const t0 = last;
  const loop = now => {
    const dt = Math.min(.05, (now - last) / 1000); last = now; const t = (now - t0) / 1000;
    if (!document.hidden) for (const v of views) if (v.visible) { v.tick(t, dt); v.composer.render(dt); }
    requestAnimationFrame(loop);
  };
  if (reduce) views.forEach(renderOnce); else requestAnimationFrame(loop);
  requestAnimationFrame(() => hero.renderer.domElement.classList.add('on'));
}

try { start(); }
catch (e) { if (window.__start2D) window.__start2D(); }