File size: 22,892 Bytes
1f56a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
383
384
385
386
387
388
389
390
// Gummy Jar: game screen (fixed-tick loop, crisp canvas, sound, haptics, daily/practice/archive, share card, Fun Pass).
// (c) 2026 CyberMax. All rights reserved.
import { makeGame, shareText as coreShare, botX, HZ, W, H, L, R, FLOOR, LINE, DROP_Y, RADII, COLORS, NAMES, EMOJI, MAX_TIER, DAILY_DROPS, clampX } from './core.js';
import { dayNumber, dateOf, count, store, toast, share, intents, download, canvasBlob, pwa, themeToggle, rr } from './cmx.js';
import * as pass from './funpass.js';

const GAME = 'plopwick';
const PATH = '/games/gummy-jar/';
const $ = (id) => document.getElementById(id);
const cv = $('c');
const ctx = cv.getContext('2d');
const TAU = Math.PI * 2;
// Looks: the jar's backdrop. Bright by default (owner, 7 Nov 2026: "bright, clean, readable on a phone").
const LOOKS = {
  sunny: { name: 'Sunny', tier: 'free', top: '#DDF4FF', bot: '#FFF3DF', jar: 'rgba(255,255,255,.62)', rim: '#BFDDF0', ink: '#2B2340', line: '#FF6FAE' },
  mint: { name: 'Mint', tier: 'pass', top: '#D9FBEF', bot: '#F4FFE6', jar: 'rgba(255,255,255,.6)', rim: '#A8E6CF', ink: '#173A33', line: '#FF7A59' },
  peach: { name: 'Peach', tier: 'pass', top: '#FFE6DA', bot: '#FFF8D9', jar: 'rgba(255,255,255,.6)', rim: '#F5C6A5', ink: '#3E2418', line: '#E2477A' },
};
const todayIso = new Date().toISOString().slice(0, 10); // UTC day: the same jar for everyone on Earth
const todayNum = dayNumber(todayIso);

pass.init({ game: GAME, name: 'Gummy Jar', perks: ['Every past Daily Jar in the archive', 'Mint and Peach jar themes'],
  free: 'The Daily Jar and endless practice stay free for everyone.',
  extra: 'The Fun Pass adds every past Daily Jar and 2 jar themes.',
  preview: ['past daily jars', '2 themes'] });
const canUse = (s) => s.tier === 'free' || pass.isPro();
let look = LOOKS[store('plopwick:look')] && canUse(LOOKS[store('plopwick:look')]) ? store('plopwick:look') : 'sunny';
let muted = !!store('plopwick:mute');
let mode = { kind: 'daily', date: todayIso };
let game, acc = 0, last = performance.now(), shake = 0, aimX = W / 2, aiming = false, hint = 1, endShown = false, autoBot = false;
let fx = [];      // particles
let texts = [];   // floating "+15" texts
let rings = [];   // merge shock rings
let pops = new Map(); // ball id -> born time (squash)
let deco = Array.from({ length: 12 }, (_, i) => ({ x: (i * 67) % W, y: (i * 131) % H, r: 4 + (i % 4) * 3, s: 6 + (i % 5) * 3 }));
const install = pwa(GAME, $('install'));
themeToggle($('theme'));

// ------------------------------------------------------------------ sound (WebAudio, no files) + haptics
let ac;
function tone(f, d = 0.08, type = 'sine', vol = 0.08, to = 1, delay = 0) {
  if (muted) return;
  try {
    ac = ac || new (window.AudioContext || window.webkitAudioContext)();
    const t0 = ac.currentTime + delay;
    const o = ac.createOscillator(), v = ac.createGain();
    o.type = type; o.frequency.setValueAtTime(f, t0);
    o.frequency.exponentialRampToValueAtTime(Math.max(40, f * to), t0 + d);
    v.gain.setValueAtTime(0.0001, t0); v.gain.exponentialRampToValueAtTime(vol, t0 + 0.008); v.gain.exponentialRampToValueAtTime(0.0001, t0 + d);
    o.connect(v); v.connect(ac.destination); o.start(t0); o.stop(t0 + d + 0.03);
  } catch { /* no audio */ }
}
const NOTES = [523, 587, 659, 784, 880, 988, 1047, 1175, 1319, 1568, 1760];
function plop(tier, chain) { const f = 900 - tier * 55; tone(f, 0.12, 'sine', 0.11, 0.45); tone(NOTES[Math.min(10, tier + Math.min(chain - 1, 4))], 0.16, 'triangle', 0.06, 1.01, 0.05); }
const buzz = (ms) => { try { if (navigator.vibrate) navigator.vibrate(ms); } catch { /* */ } };
const setMuteLabel = () => { $('mute').textContent = muted ? '🔇' : '🔊'; };
setMuteLabel();
$('mute').onclick = () => { muted = !muted; store('plopwick:mute', muted); setMuteLabel(); };

// ------------------------------------------------------------------ crisp canvas (device pixels, no blur on iPhone)
let scale = 1;
function fit() {
  const r = cv.getBoundingClientRect();
  const dpr = Math.min(3, window.devicePixelRatio || 1);
  const w = Math.max(1, Math.round(r.width * dpr)), h = Math.max(1, Math.round(r.height * dpr));
  if (cv.width !== w || cv.height !== h) { cv.width = w; cv.height = h; }
  scale = cv.width / W;
}
new ResizeObserver(fit).observe(cv);
fit();

// ------------------------------------------------------------------ modes + stats (one small menu: calm screen)
const seedOf = (m) => (m.kind === 'practice' ? m.seed : `daily:${m.date}`);
const dayKey = (m) => `plopwick:day:${m.date}`;
const dayStats = (m) => store(dayKey(m)) || { best: 0, tries: 0, tier: 0 };
function renderModes() {
  const el = $('modes'); el.innerHTML = '';
  const items = [
    { t: `Daily Jar #${todayNum}`, on: mode.kind === 'daily' && mode.date === todayIso, go: () => setMode({ kind: 'daily', date: todayIso }) },
    { t: 'Endless', on: mode.kind === 'practice', go: () => setMode({ kind: 'practice', seed: `p${Date.now()}` }) },
    { t: 'Archive', on: mode.kind === 'daily' && mode.date !== todayIso, lock: !pass.isPro(), go: archive },
  ];
  for (const it of items) {
    const b = document.createElement('button');
    b.className = 'chip'; b.type = 'button'; b.setAttribute('aria-pressed', String(it.on));
    b.innerHTML = `${it.t}${it.lock ? ' <span class="lock">🔒</span>' : ''}`;
    b.onclick = it.go; el.appendChild(b);
  }
}
function archive() {
  if (!pass.isPro()) { pass.open('archive'); return; }
  const n = todayNum - 1;
  const k = Number(prompt(`Play a past Daily Jar: enter a number from 1 to ${n}`, String(n)));
  if (k >= 1 && k <= n) setMode({ kind: 'daily', date: dateOf(k) });
}
function setMode(m) {
  mode = m;
  $('num').textContent = m.kind === 'practice' ? 'endless' : `#${dayNumber(m.date)}`;
  $('menu').open = false;
  renderModes(); start();
}
function hud() {
  const s = mode.kind === 'daily' ? dayStats(mode) : { best: store('plopwick:endless-best') || 0, tries: 0 };
  $('best').textContent = s.best.toLocaleString('en-US'); $('tries').textContent = s.tries || 0;
}
const shopVis = (on) => { const b = $('shop-btn'); if (b) b.hidden = !on || pass.noShop(); };

// ------------------------------------------------------------------ run
function start() {
  if (mode.kind === 'practice') mode.seed = `p${Date.now()}`;
  game = makeGame(seedOf(mode), { limit: mode.kind === 'practice' ? 0 : DAILY_DROPS });
  fx = []; texts = []; rings = []; pops = new Map(); shake = 0; endShown = false; acc = 0; last = performance.now();
  $('over').hidden = true; shopVis(false); hud();
  count(GAME, 'play', mode.kind === 'daily' ? (mode.date === todayIso ? 'daily' : 'archive') : 'practice');
}
function release(x) {
  if (!game || game.state !== 'run') return;
  if (game.drop(x)) { tone(660, 0.05, 'triangle', 0.04, 0.8); hint = Math.min(hint, 0.99); }
}
function finish() {
  endShown = true;
  const done = game.state === 'done';
  if (done) { tone(784, 0.15, 'triangle', 0.08); tone(1047, 0.25, 'triangle', 0.08, 1, 0.12); } else { tone(300, 0.35, 'sawtooth', 0.06, 0.4); buzz([30, 40, 30]); shake = 10; }
  let newBest = false;
  if (mode.kind === 'daily') {
    const s = dayStats(mode); s.tries = (s.tries || 0) + 1;
    if (game.score > (s.best || 0)) { s.best = game.score; s.tier = game.best; s.chain = game.bigChain; s.bursts = game.bursts; newBest = game.score > 0; }
    store(dayKey(mode), s);
  } else if (game.score > (store('plopwick:endless-best') || 0)) { store('plopwick:endless-best', game.score); newBest = true; }
  hud();
  setTimeout(() => showOver(newBest, done), done ? 500 : 900);
  if (game.score >= 300) install.offer();
}

// ------------------------------------------------------------------ loop
function frame(now) {
  requestAnimationFrame(frame);
  const dt = Math.min(0.1, (now - last) / 1000); last = now;
  if (game && (game.state === 'run' || game.state === 'settle')) {
    acc += dt;
    let n = 0;
    while (acc >= 1 / HZ && n++ < 6) {
      acc -= 1 / HZ;
      if (autoBot && game.canDrop()) game.drop(botX(game));
      game.tick();
      for (const e of game.events) onEvent(e);
      game.events.length = 0;
    }
    if (acc > 1 / HZ) acc = 0;
  }
  if (game && (game.state === 'over' || game.state === 'done') && !endShown) { for (const e of game.events) onEvent(e); game.events.length = 0; finish(); }
  step(dt);
  draw(now);
}
function onEvent(e) {
  if (e.type === 'merge' || e.type === 'burst') {
    const big = e.type === 'burst' ? 14 : e.tier;
    burst(e.x, e.y, COLORS[e.tier], 10 + big * 2, 60 + big * 22);
    rings.push({ x: e.x, y: e.y, r: RADII[Math.min(e.tier, MAX_TIER)] * 0.6, life: 1, c: COLORS[e.tier] });
    texts.push({ x: e.x, y: e.y - 10, t: `+${e.pts}`, life: 1, big: e.chain >= 2 ? `${e.chain}x chain!` : '' });
    plop(e.tier, e.chain);
    if (e.type === 'burst') { tone(1568, 0.3, 'triangle', 0.08, 1, 0.08); tone(2093, 0.35, 'triangle', 0.07, 1, 0.16); }
    shake = Math.max(shake, e.tier >= 6 ? e.tier * 0.9 : e.chain >= 3 ? 3 : 0);
    buzz(e.tier >= 6 ? 25 : 10);
    if (e.id) pops.set(e.id, performance.now());
  } else if (e.type === 'land' && e.v > 300) {
    tone(220 + (10 - e.tier) * 25, 0.05, 'sine', Math.min(0.05, e.v / 20000), 0.7);
  }
}
function burst(x, y, c, n, sp) {
  for (let i = 0; i < n; i++) {
    const a = Math.random() * TAU, v = sp * (0.4 + Math.random() * 0.8);
    fx.push({ x, y, vx: Math.cos(a) * v, vy: Math.sin(a) * v - 60, life: 1, r: 2 + Math.random() * 3.5, c: i % 4 === 0 ? '#FFFFFF' : c, star: i % 5 === 0 });
  }
}
function step(dt) {
  for (const p of fx) { p.x += p.vx * dt; p.y += p.vy * dt; p.vy += 600 * dt; p.vx *= 0.985; p.life -= dt * 1.4; }
  fx = fx.filter((p) => p.life > 0);
  for (const t of texts) { t.y -= 40 * dt; t.life -= dt * 0.9; }
  texts = texts.filter((t) => t.life > 0);
  for (const r of rings) { r.r += 160 * dt; r.life -= dt * 2.6; }
  rings = rings.filter((r) => r.life > 0);
  for (const d of deco) { d.y -= d.s * dt; if (d.y < -20) { d.y = H + 20; d.x = Math.random() * W; } }
  if (game && game.drops > 0) hint = Math.max(0, hint - dt * 0.8);
  shake *= Math.pow(0.02, dt); if (shake < 0.2) shake = 0;
}

// ------------------------------------------------------------------ drawing
const shade = (hex, k) => { // k<0 darker, k>0 lighter
  const n = parseInt(hex.slice(1), 16); let r = n >> 16, g = (n >> 8) & 255, b = n & 255;
  const t = k < 0 ? 0 : 255, p = Math.abs(k);
  r = Math.round(r + (t - r) * p); g = Math.round(g + (t - g) * p); b = Math.round(b + (t - b) * p);
  return `rgb(${r},${g},${b})`;
};
function gummy(c, x, y, tier, sx = 1, sy = 1, blink = false, alpha = 1) {
  const r = RADII[tier], col = COLORS[tier];
  c.save(); c.translate(x, y); c.scale(sx, sy); c.globalAlpha = alpha;
  if (tier === MAX_TIER) { // Sunny: soft rays
    c.fillStyle = 'rgba(255,200,40,.35)';
    const t = performance.now() / 1400;
    for (let i = 0; i < 12; i++) { const a = t + (i * TAU) / 12; c.beginPath(); c.moveTo(Math.cos(a - 0.12) * r, Math.sin(a - 0.12) * r); c.lineTo(Math.cos(a) * r * 1.25, Math.sin(a) * r * 1.25); c.lineTo(Math.cos(a + 0.12) * r, Math.sin(a + 0.12) * r); c.fill(); }
  }
  const gr = c.createRadialGradient(-r * 0.35, -r * 0.4, r * 0.1, 0, 0, r);
  gr.addColorStop(0, shade(col, 0.45)); gr.addColorStop(0.55, col); gr.addColorStop(1, shade(col, -0.12));
  c.fillStyle = gr; c.beginPath(); c.arc(0, 0, r, 0, TAU); c.fill();
  c.lineWidth = Math.max(1.6, r * 0.07); c.strokeStyle = shade(col, -0.28); c.stroke();
  c.fillStyle = 'rgba(255,255,255,.75)'; c.beginPath(); c.ellipse(-r * 0.38, -r * 0.45, r * 0.22, r * 0.13, -0.6, 0, TAU); c.fill();
  // face
  const ink = '#2B2340', ex = r * 0.3, ey = -r * 0.04, er = Math.max(1.4, r * 0.085);
  c.fillStyle = ink;
  if (blink) { c.lineWidth = Math.max(1.2, r * 0.05); c.strokeStyle = ink; c.beginPath(); c.moveTo(-ex - er, ey); c.lineTo(-ex + er, ey); c.moveTo(ex - er, ey); c.lineTo(ex + er, ey); c.stroke(); }
  else { c.beginPath(); c.arc(-ex, ey, er, 0, TAU); c.arc(ex, ey, er, 0, TAU); c.fill(); }
  if (tier >= 2) { c.fillStyle = 'rgba(255,90,120,.28)'; c.beginPath(); c.arc(-r * 0.52, r * 0.16, r * 0.13, 0, TAU); c.arc(r * 0.52, r * 0.16, r * 0.13, 0, TAU); c.fill(); }
  c.lineWidth = Math.max(1.2, r * 0.055); c.strokeStyle = ink; c.lineCap = 'round';
  c.beginPath(); c.arc(0, r * 0.12, r * 0.16, 0.15 * Math.PI, 0.85 * Math.PI); c.stroke();
  c.restore();
}
function backdrop(c, S, w, h, now) {
  const gr = c.createLinearGradient(0, 0, 0, h); gr.addColorStop(0, S.top); gr.addColorStop(1, S.bot);
  c.fillStyle = gr; c.fillRect(-30, -30, w + 60, h + 60);
  c.fillStyle = 'rgba(255,255,255,.55)';
  for (const d of deco) { c.beginPath(); c.arc(d.x * (w / W), d.y * (h / H), d.r, 0, TAU); c.fill(); }
}
function jar(c, S) {
  c.fillStyle = S.jar; rr(c, L - 6, LINE - 30, R - L + 12, FLOOR - LINE + 36, 26); c.fill();
  c.lineWidth = 4; c.strokeStyle = S.rim; c.stroke();
}
function draw(now) {
  const S = LOOKS[look];
  ctx.setTransform(1, 0, 0, 1, 0, 0);
  ctx.clearRect(0, 0, cv.width, cv.height);
  ctx.setTransform(scale, 0, 0, scale, 0, 0);
  backdrop(ctx, S, W, H, now);
  if (shake > 0) ctx.translate((Math.random() - 0.5) * shake, (Math.random() - 0.5) * shake);
  jar(ctx, S);
  if (!game) return;
  // the top line: a calm dashed line that warms up only when the jar gets full
  const d = Math.min(1, game.danger);
  ctx.save(); ctx.setLineDash([8, 8]); ctx.lineWidth = 3;
  ctx.strokeStyle = d > 0.05 ? `rgba(255,70,90,${0.35 + 0.6 * d * (0.6 + 0.4 * Math.sin(now / 90))})` : 'rgba(43,35,64,.14)';
  ctx.beginPath(); ctx.moveTo(L + 4, LINE); ctx.lineTo(R - 4, LINE); ctx.stroke(); ctx.restore();
  // aim: the next gummy follows the finger, with a soft guide down
  const cur = game.cur();
  const ax = clampX(aimX, cur);
  if (game.state === 'run' && (!game.limit || game.drops < game.limit)) {
    const ready = game.cool <= 0;
    ctx.strokeStyle = 'rgba(43,35,64,.12)'; ctx.lineWidth = 2; ctx.setLineDash([4, 8]);
    ctx.beginPath(); ctx.moveTo(ax, DROP_Y + RADII[cur]); ctx.lineTo(ax, FLOOR); ctx.stroke(); ctx.setLineDash([]);
    gummy(ctx, ax, DROP_Y, cur, 1, 1, false, ready ? 1 : 0.35);
  }
  // gummies
  const t = performance.now();
  for (const b of game.balls) {
    let sx = 1, sy = 1;
    const p = pops.get(b.id);
    if (p) { const a = (t - p) / 1000; if (a > 0.6) pops.delete(b.id); else { const k = Math.exp(-a * 7) * Math.cos(a * 26) * 0.22; sx = 1 + k; sy = 1 - k; } }
    const blink = ((t / 1000 + b.id * 1.7) % 4.2) < 0.12;
    gummy(ctx, b.x, b.y, b.tier, sx, sy, blink);
  }
  for (const r of rings) { ctx.globalAlpha = r.life * 0.7; ctx.strokeStyle = r.c; ctx.lineWidth = 5 * r.life; ctx.beginPath(); ctx.arc(r.x, r.y, r.r, 0, TAU); ctx.stroke(); }
  ctx.globalAlpha = 1;
  for (const p of fx) {
    ctx.globalAlpha = Math.max(0, Math.min(1, p.life * 1.3)); ctx.fillStyle = p.c;
    if (p.star) { ctx.save(); ctx.translate(p.x, p.y); ctx.rotate(p.life * 6); ctx.fillRect(-p.r, -p.r * 0.35, p.r * 2, p.r * 0.7); ctx.fillRect(-p.r * 0.35, -p.r, p.r * 0.7, p.r * 2); ctx.restore(); }
    else { ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, TAU); ctx.fill(); }
  }
  ctx.globalAlpha = 1; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
  for (const x of texts) {
    ctx.globalAlpha = Math.min(1, x.life * 1.5);
    ctx.font = '900 20px system-ui, -apple-system, sans-serif'; ctx.lineWidth = 5; ctx.strokeStyle = '#FFFFFF'; ctx.fillStyle = S.ink;
    ctx.strokeText(x.t, x.x, x.y); ctx.fillText(x.t, x.x, x.y);
    if (x.big) { ctx.font = '900 16px system-ui, -apple-system, sans-serif'; ctx.fillStyle = '#E2477A'; ctx.strokeText(x.big, x.x, x.y - 22); ctx.fillText(x.big, x.x, x.y - 22); }
  }
  ctx.globalAlpha = 1;
  // one score chip (top-left) and the next gummy (top-right)
  ctx.fillStyle = 'rgba(255,255,255,.85)'; rr(ctx, 12, 10, 124, 40, 20); ctx.fill();
  ctx.fillStyle = S.ink; ctx.textAlign = 'left'; ctx.font = '900 22px system-ui, -apple-system, sans-serif';
  ctx.fillText(game.score.toLocaleString('en-US'), 26, 31);
  if (game.limit) { ctx.font = '700 12px system-ui, -apple-system, sans-serif'; ctx.globalAlpha = 0.6; ctx.textAlign = 'right'; ctx.fillText(`${game.left()} left`, 128, 31); ctx.globalAlpha = 1; }
  const nx = game.next();
  if (!game.limit || game.left() > 1) {
  ctx.fillStyle = 'rgba(255,255,255,.85)'; rr(ctx, W - 96, 10, 84, 40, 20); ctx.fill();
  ctx.fillStyle = S.ink; ctx.globalAlpha = 0.6; ctx.font = '700 11px system-ui, -apple-system, sans-serif'; ctx.textAlign = 'left'; ctx.fillText('next', W - 86, 31); ctx.globalAlpha = 1;
  ctx.save(); const ns = Math.min(1, 15 / RADII[nx]); gummy(ctx, W - 34, 30, nx, ns, ns); ctx.restore();
  }
  // one short hint line that fades after the first drop
  if (hint > 0 && game.state === 'run') {
    ctx.globalAlpha = Math.min(1, hint * 1.5); ctx.textAlign = 'center'; ctx.fillStyle = S.ink;
    ctx.font = '800 17px system-ui, -apple-system, sans-serif';
    ctx.fillText('Drag, then let go to drop', W / 2, H * 0.52);
    ctx.font = '600 14px system-ui, -apple-system, sans-serif'; ctx.globalAlpha *= 0.7;
    ctx.fillText('Two the same size plop into a bigger one', W / 2, H * 0.52 + 24);
    ctx.globalAlpha = 1;
  }
}

// ------------------------------------------------------------------ results, share, card
function label() { return mode.kind === 'practice' ? 'endless' : `#${dayNumber(mode.date)}`; }
function shareText() { return `${coreShare(game, label())}\nCan you make a Sunny?`; }
function showOver(newBest, done) {
  $('over-title').textContent = newBest ? 'New best!' : done ? 'Jar done!' : 'Jar full!';
  $('over-score').textContent = game.score.toLocaleString('en-US');
  const s = mode.kind === 'daily' ? dayStats(mode) : null;
  $('over-line').textContent = `Biggest: ${NAMES[game.best]}` + (s ? ` · today's best ${s.best.toLocaleString('en-US')}` : '');
  $('over-grid').textContent = coreShare(game, label());
  const it = intents(GAME, shareText(), PATH);
  $('sx').href = it.x; $('sb').href = it.bsky; $('sw').href = it.whatsapp;
  $('over').hidden = false; shopVis(true);
  pass.afterWin($('over'), 'over');
}
function card() {
  const S = LOOKS[look], c = document.createElement('canvas');
  c.width = 1080; c.height = 1350;
  const x = c.getContext('2d');
  backdrop(x, S, 1080, 1350, 0);
  x.save(); x.scale(3, 3);
  for (let k = 0; k <= Math.min(game.best, MAX_TIER); k++) gummy(x, 40 + ((k * 61) % 280), 300 + (k % 3) * 40 + Math.floor(k / 4) * 20, k);
  x.restore();
  x.fillStyle = S.ink; x.textAlign = 'left';
  x.font = '900 72px system-ui, sans-serif'; x.fillText('GUMMY JAR', 80, 140);
  x.globalAlpha = 0.75; x.font = '700 40px system-ui, sans-serif';
  x.fillText(mode.kind === 'daily' ? `Daily Jar #${dayNumber(mode.date)} · ${mode.date}` : 'Endless jar', 80, 200);
  x.globalAlpha = 1; x.font = '900 170px system-ui, sans-serif'; x.fillText(game.score.toLocaleString('en-US'), 80, 400);
  x.font = '700 44px system-ui, sans-serif'; x.fillText(`points · biggest: ${NAMES[game.best]}`, 80, 470);
  x.font = '60px system-ui, sans-serif'; x.fillText(EMOJI.map((e, k) => (k <= game.best ? e : '⚪')).join(''), 80, 1170);
  x.font = '800 44px system-ui, sans-serif'; x.fillText('Can you make a Sunny?', 80, 1250);
  x.globalAlpha = 0.7; x.font = '600 32px system-ui, sans-serif'; x.fillText('Play free: cybermaxtools.com/games/gummy-jar', 80, 1305);
  return c;
}
const fname = () => `plopwick-${mode.date || 'endless'}.png`;
$('share').onclick = async () => share(GAME, shareText(), PATH, await canvasBlob(card()), fname());
['sx', 'sb', 'sw'].forEach((id) => $(id).addEventListener('click', () => count(GAME, 'share', { sx: 'x', sb: 'bsky', sw: 'whatsapp' }[id])));
$('card').onclick = async () => { download(await canvasBlob(card()), fname()); count(GAME, 'share', 'png'); };

// ------------------------------------------------------------------ input: drag to aim, let go to drop (mouse: hover + click)
const toWorld = (e) => { const r = cv.getBoundingClientRect(); return ((e.clientX - r.left) / r.width) * W; };
cv.addEventListener('pointerdown', (e) => {
  e.preventDefault();
  if (!game || game.state === 'over' || game.state === 'done') { if (endShown) start(); return; }
  aiming = true; aimX = toWorld(e);
  try { cv.setPointerCapture(e.pointerId); } catch { /* */ }
});
cv.addEventListener('pointermove', (e) => { if (aiming || e.pointerType === 'mouse') aimX = toWorld(e); });
cv.addEventListener('pointerup', (e) => { if (!aiming) return; aiming = false; aimX = toWorld(e); release(aimX); });
cv.addEventListener('pointercancel', () => { aiming = false; });
document.addEventListener('keydown', (e) => {
  if (/^(INPUT|TEXTAREA|SELECT)$/.test(document.activeElement && document.activeElement.tagName)) return;
  if (e.key === 'ArrowLeft') { aimX = Math.max(L, aimX - 18); e.preventDefault(); }
  else if (e.key === 'ArrowRight') { aimX = Math.min(R, aimX + 18); e.preventDefault(); }
  else if (e.key === ' ' || e.key === 'Enter' || e.key === 'ArrowDown') { if (document.activeElement && document.activeElement.tagName === 'BUTTON') return; e.preventDefault(); if (endShown) start(); else release(aimX); }
  else if (e.key === 'r' || e.key === 'R') { e.preventDefault(); start(); }
  else if (e.key === 'm' || e.key === 'M') $('mute').click();
});
$('pass-btn').onclick = () => pass.open('header');
$('again').onclick = () => { start(); cv.focus(); };
document.addEventListener('visibilitychange', () => { last = performance.now(); acc = 0; });
function renderLooks() {
  const el = $('looks'); el.innerHTML = '';
  for (const [id, s] of Object.entries(LOOKS)) {
    const b = document.createElement('button');
    const lock = !canUse(s);
    b.type = 'button'; b.title = `${s.name}${s.tier === 'pass' ? ' (Fun Pass)' : ''}`; b.setAttribute('aria-label', b.title);
    b.style.background = `linear-gradient(135deg, ${s.top} 50%, ${s.line} 50%)`;
    b.setAttribute('aria-pressed', String(id === look));
    if (lock) b.classList.add('locked');
    b.onclick = () => { if (lock) { pass.open('theme'); return; } look = id; store('plopwick:look', id); renderLooks(); };
    el.appendChild(b);
  }
}
const refresh = () => { if (!canUse(LOOKS[look])) { look = 'sunny'; store('plopwick:look', 'sunny'); } renderLooks(); renderModes(); };
pass.onChange(refresh);

// test + screenshot hooks (no effect on normal play)
window.__plopwick = {
  state: () => ({ state: game.state, score: game.score, drops: game.drops, balls: game.balls.length, best: game.best, left: game.left(), mode: mode.kind, endShown }),
  bot: (on = true) => { autoBot = on; },
  drop: (x) => release(x),
};
window.__cmxShot = async () => {
  start();
  const g = makeGame(`daily:${todayIso}`);
  for (let i = 0; i < 60 * 14 && g.drops < 26; i++) { if (g.canDrop()) g.drop(botX(g)); g.tick(); g.events.length = 0; }
  game = g; hint = 0; aimX = W * 0.62;
};

$('num').textContent = `#${todayNum}`;
renderModes(); renderLooks();
start();
requestAnimationFrame(frame);