Skip to content
VibeCodeBench
Back to the app

The code

Love Letter by Claude Haiku 5.5

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.)22.9 KB · 426 lines · no libraries
Download
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title>Love Letter</title>
<style>
:root{--cream:#fff4df;--gold:#e6b85c;--paper-a:#b3262e;--paper-b:#86161d;--paper-ink:#fff4df;--paper-edge:#e6b85c}
body[data-paper="cream"]{--paper-a:#fff7e6;--paper-b:#efdcb0;--paper-ink:#5a1b1f;--paper-edge:#c9962e}
body[data-paper="gold"]{--paper-a:#f5d982;--paper-b:#c99a3b;--paper-ink:#4a1014;--paper-edge:#8e1a20}
*{box-sizing:border-box;touch-action:manipulation}
html{background:#1d0507}
body{margin:0;min-height:100vh;color:var(--cream);font-family:system-ui,"Segoe UI",Roboto,sans-serif;background:radial-gradient(120% 80% at 50% 0%,#7b1a22,#3a0a10 55%,#1d0507) no-repeat;-webkit-tap-highlight-color:transparent}
button,input,textarea{font:inherit}
#app{max-width:560px;margin:0 auto;padding:18px 16px 28px}
.screen{display:none;flex-direction:column}
.screen.on{display:flex;animation:fade .45s ease both}
@keyframes fade{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
.title{margin:4px 0 0;text-align:center;font:400 40px/1.1 "Segoe Script","Brush Script MT",cursive;color:#f6d27a}
.sub{margin:6px 0 10px;text-align:center;opacity:.85}
.field{display:block;margin-top:12px;font-size:16px}
input,textarea{display:block;width:100%;margin-top:6px;padding:14px;border:2px solid var(--gold);border-radius:14px;background:rgba(255,244,223,.08);color:var(--cream);font-size:18px;resize:none}
input{height:54px}
textarea{height:130px}
::placeholder{color:rgba(255,244,223,.55)}
.label{margin:18px 0 6px;font-size:15px;opacity:.9}
.papers{display:flex;gap:10px}
.chip{flex:1;min-height:56px;border:3px solid transparent;border-radius:16px;background:linear-gradient(135deg,var(--a),var(--b));color:var(--c);font-size:17px;font-weight:600;box-shadow:0 4px 0 rgba(0,0,0,.35);cursor:pointer}
.chip[aria-pressed="true"]{border-color:var(--cream);transform:translateY(-2px)}
.chip.red{--a:#d13b34;--b:#7e141a;--c:#fff4df}
.chip.cream{--a:#fffaf0;--b:#efdcb0;--c:#5a1b1f}
.chip.gold{--a:#f7dc8c;--b:#c58f2e;--c:#4a1014}
.big,.ghost{display:block;width:100%;margin-top:14px;border-radius:18px;font-size:20px;font-weight:700;cursor:pointer}
.big{min-height:62px;border:0;background:linear-gradient(#ffe08a,#e0a544);color:#4a0b10;box-shadow:0 6px 0 #7a4a12}
.big:active,.big.press{transform:translateY(4px);box-shadow:0 2px 0 #7a4a12}
.ghost{min-height:56px;margin-top:10px;font-size:17px;font-weight:500;background:transparent;color:var(--cream);border:2px solid var(--gold)}
.hint{min-height:22px;margin:10px 0 0;color:#f6d27a;font-size:16px}
:focus-visible{outline:3px solid #f6d27a;outline-offset:2px}
.paper{padding:12px 14px 18px;border:3px solid var(--paper-edge);border-radius:22px;background-color:var(--paper-a);background-image:radial-gradient(circle at 18% 8%,rgba(255,255,255,.28),transparent 42%),radial-gradient(circle at 85% 95%,rgba(0,0,0,.1),transparent 45%);color:var(--paper-ink);box-shadow:0 18px 40px rgba(0,0,0,.45);transform-origin:50% 100%;transition:background-color .5s,color .5s,border-color .5s}
.popup.unfold{animation:unfold 1s cubic-bezier(.2,.8,.2,1) both}
@keyframes unfold{from{opacity:0;transform:perspective(900px) rotateX(-75deg)}to{opacity:1;transform:perspective(900px) rotateX(0)}}
.scene{display:block;width:100%;height:auto;border-radius:14px}
.to,.sign{font-family:"Segoe Script","Brush Script MT",cursive;margin:8px 4px 0}
.to{font-size:21px}
.writing{margin:4px 4px 0;min-height:6em;font:400 22px/1.55 "Segoe Script","Bradley Hand","Brush Script MT","Comic Sans MS",cursive;white-space:pre-wrap;overflow-wrap:anywhere}
.ch{animation:ink .25s ease-out both}
@keyframes ink{from{opacity:0}to{opacity:1}}
.caret{display:inline-block;width:2px;height:1.1em;margin-left:2px;vertical-align:-.2em;background:currentColor;opacity:0}
.caret.on{opacity:1;animation:blink .8s steps(2) infinite}
@keyframes blink{to{visibility:hidden}}
.sign{text-align:right;font-size:20px;opacity:0;transition:opacity .8s}
.sign.on{opacity:1}
.pop{transform-box:fill-box;transform-origin:50% 100%;animation:pop .8s var(--d,0s) both cubic-bezier(.2,1.3,.4,1)}
@keyframes pop{from{opacity:0;transform:translateY(16px) scale(.4)}to{opacity:1;transform:none}}
.grow{transform-box:fill-box;transform-origin:50% 100%;animation:grow .9s var(--d,0s) both}
@keyframes grow{from{transform:scale(0)}to{transform:scale(1)}}
.float{animation:float 3.2s ease-in-out var(--d,0s) infinite}
@keyframes float{50%{transform:translateY(-6px)}}
.moon{transform-box:fill-box;transform-origin:50% 50%;animation:moon 1.1s .2s both}
@keyframes moon{from{opacity:0;transform:scale(.5)}to{opacity:1;transform:none}}
.star{animation:twinkle 2.4s ease-in-out var(--d,0s) infinite}
@keyframes twinkle{50%{opacity:.2}}
.cat{fill:#fff4df}
.dk{fill:#3a0a10}
.tail{fill:none;stroke:#fff4df;stroke-width:7;stroke-linecap:round}
.ptail{fill:none;stroke:#e58a34;stroke-width:9;stroke-linecap:round}
.pm{position:absolute;bottom:60px;left:-200px;width:170px;height:130px;transition:left 2.6s linear}
.pm svg{display:block;width:100%;height:100%;overflow:visible}
.leg{transform-box:fill-box;transform-origin:50% 0}
.walking .leg.a{animation:sa .45s ease-in-out infinite alternate}
.walking .leg.b{animation:sb .45s ease-in-out infinite alternate}
.walking .bob{animation:bob .45s ease-in-out infinite alternate}
@keyframes sa{from{transform:rotate(-22deg)}to{transform:rotate(22deg)}}
@keyframes sb{from{transform:rotate(22deg)}to{transform:rotate(-22deg)}}
@keyframes bob{to{transform:translateY(-3px)}}
.carry{transition:opacity .25s}
.carry.off{opacity:0}
.street{position:relative;flex:1;min-height:380px;overflow:hidden;border-radius:24px;background:linear-gradient(#2a070b,#5e1218 62%,#7a2a22);box-shadow:inset 0 0 0 2px rgba(230,184,92,.35);user-select:none}
.street-moon{position:absolute;top:18px;right:24px;width:46px;height:46px;border-radius:50%;background:radial-gradient(circle at 35% 35%,#fff3c4,#f2c75c 60%,#d89b2b);box-shadow:0 0 30px rgba(246,210,122,.6)}
.cap{position:absolute;top:18px;left:18px;right:90px;margin:0;font-size:17px;opacity:.9}
.ground{position:absolute;left:0;right:0;bottom:0;height:60px;background:linear-gradient(#4a0e16,#2a070b);border-top:3px solid var(--gold)}
.env{position:absolute;left:50%;bottom:60px;width:150px;height:100px;margin-left:-75px;perspective:520px;opacity:0;pointer-events:none;cursor:pointer}
.env.show{opacity:1;pointer-events:auto;animation:drop .55s cubic-bezier(.3,1.3,.5,1) both}
.env.open{animation:out .7s ease-in both}
@keyframes drop{from{opacity:0;transform:translateY(-70px) rotate(-6deg)}to{opacity:1;transform:none}}
@keyframes out{from{opacity:1;transform:none}to{opacity:0;transform:scale(1.2) translateY(-12px)}}
.env-body{position:absolute;inset:0;border:2px solid var(--paper-edge);border-radius:10px;background:var(--paper-a);box-shadow:0 10px 18px rgba(0,0,0,.45)}
.env-flap{position:absolute;left:0;right:0;top:0;height:58%;background:var(--paper-b);clip-path:polygon(0 0,100% 0,50% 100%);transform-origin:50% 0;transition:transform .5s ease}
.env.open .env-flap{transform:rotateX(180deg)}
.env-seal{position:absolute;left:50%;top:50%;width:30px;height:30px;margin:-4px 0 0 -15px;border-radius:50%;background:#b3262e;color:var(--cream);display:grid;place-items:center;font-size:15px}
.tap{position:absolute;left:0;right:0;bottom:186px;margin:0;text-align:center;font:400 24px "Segoe Script","Brush Script MT",cursive;opacity:0;transition:opacity .6s}
.tap.on{opacity:1;animation:pulse 1.6s ease-in-out infinite}
@keyframes pulse{50%{opacity:.5}}
</style>
</head>
<body>
<main id="app">

  <section id="compose" class="screen on">
    <h1 class="title">Love Letter</h1>
    <p class="sub">Write it, send it, watch it arrive.</p>
    <form id="form">
      <label class="field">From<input id="from" maxlength="30" placeholder="Your name" autocomplete="off"></label>
      <label class="field">To<input id="to" maxlength="30" placeholder="Who is it for?" autocomplete="off"></label>
      <label class="field">Message<textarea id="msg" maxlength="280" placeholder="Write a few words of love..."></textarea></label>
      <p class="label">Paper</p>
      <div class="papers">
        <button type="button" class="chip red" data-paper="red" aria-pressed="true">Warm red</button>
        <button type="button" class="chip cream" data-paper="cream" aria-pressed="false">Cream</button>
        <button type="button" class="chip gold" data-paper="gold" aria-pressed="false">Gold</button>
      </div>
      <p id="hint" class="hint" aria-live="polite"></p>
      <button id="send" type="button" class="big">Send</button>
    </form>
  </section>

  <section id="delivery" class="screen">
    <div class="street">
      <div class="ground"></div>
      <div class="street-moon"></div>
      <p class="cap">A letter just arrived for you</p>
      <div id="pm" class="pm">
        <svg viewBox="0 0 170 130" aria-hidden="true">
          <path class="ptail" d="M24 96Q4 76 16 54"/>
          <g class="bob">
            <rect class="leg a" x="50" y="100" width="11" height="24" rx="5.5" fill="#e58a34"/>
            <rect class="leg b" x="66" y="100" width="11" height="24" rx="5.5" fill="#e58a34"/>
            <rect class="leg b" x="92" y="100" width="11" height="24" rx="5.5" fill="#e58a34"/>
            <rect class="leg a" x="108" y="100" width="11" height="24" rx="5.5" fill="#e58a34"/>
            <ellipse cx="76" cy="88" rx="44" ry="25" fill="#f0a04b"/>
            <polygon points="96,50 98,28 114,42" fill="#f0a04b"/>
            <polygon points="122,42 138,28 140,50" fill="#f0a04b"/>
            <circle cx="118" cy="62" r="26" fill="#f0a04b"/>
            <path d="M92 50Q94 24 118 24Q142 24 144 50Z" fill="#2f6fb5"/>
            <path d="M104 46H152Q156 49 151 53H104Z" fill="#1f4f8f"/>
            <circle cx="118" cy="34" r="3" fill="#e6b85c"/>
            <circle cx="110" cy="64" r="3" fill="#2a0f0a"/>
            <circle cx="128" cy="64" r="3" fill="#2a0f0a"/>
            <circle cx="111" cy="63" r="1" fill="#fff"/>
            <circle cx="129" cy="63" r="1" fill="#fff"/>
            <ellipse cx="119" cy="72" rx="3" ry="2.2" fill="#e06a6a"/>
            <path d="M115 77Q119 80 123 77" fill="none" stroke="#2a0f0a" stroke-width="1.5" stroke-linecap="round"/>
            <path d="M100 72L86 69M100 77L86 79" stroke="#2a0f0a" stroke-width="1" stroke-linecap="round"/>
            <g class="carry">
              <g transform="translate(120 80) rotate(-6)">
                <rect width="38" height="26" rx="3" fill="#fff4df" stroke="#e6b85c" stroke-width="1.5"/>
                <path d="M0 0L19 14L38 0" fill="none" stroke="#b3262e" stroke-width="1.5"/>
                <circle cx="19" cy="16" r="3.2" fill="#b3262e"/>
              </g>
            </g>
            <path d="M96 90Q112 102 138 94" fill="none" stroke="#f0a04b" stroke-width="8" stroke-linecap="round"/>
            <circle cx="138" cy="94" r="5" fill="#f0a04b"/>
          </g>
        </svg>
      </div>
      <div id="env" class="env" role="button" tabindex="0" aria-label="Open the letter">
        <div class="env-body"></div>
        <div class="env-flap"></div>
        <div class="env-seal">&#9829;</div>
      </div>
      <p id="tap" class="tap">Tap the envelope</p>
    </div>
    <button id="back" type="button" class="ghost">Back to writing</button>
  </section>

  <section id="letter" class="screen">
    <article id="popup" class="paper popup">
      <svg class="scene" viewBox="0 0 320 210" role="img" aria-label="Two cats sitting under the moon with hearts and flowers">
        <defs>
          <linearGradient id="sky" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#7b1a22"/><stop offset="1" stop-color="#2a070b"/></linearGradient>
          <radialGradient id="moonG"><stop offset="0" stop-color="#fff3c4"/><stop offset=".6" stop-color="#f2c75c"/><stop offset="1" stop-color="#d89b2b"/></radialGradient>
          <radialGradient id="halo"><stop offset="0" stop-color="#f6d27a" stop-opacity=".6"/><stop offset="1" stop-color="#f6d27a" stop-opacity="0"/></radialGradient>
          <path id="heart" d="M0 4C-6-1-10-5-5-8-2-10 0-7 0-5 0-7 2-10 5-8 10-5 6-1 0 4Z"/>
          <symbol id="flower" viewBox="-12 -12 24 24"><g fill="#f6c46b"><circle cy="-6.5" r="4.5"/><circle cx="6.2" cy="-2" r="4.5"/><circle cx="3.8" cy="5.3" r="4.5"/><circle cx="-3.8" cy="5.3" r="4.5"/><circle cx="-6.2" cy="-2" r="4.5"/></g><circle r="3.2" fill="#e0474c"/></symbol>
        </defs>
        <rect width="320" height="210" fill="url(#sky)"/>
        <circle class="star" cx="40" cy="36" r="2" fill="#fff4df"/>
        <circle class="star" style="--d:.8s" cx="92" cy="18" r="1.6" fill="#fff4df"/>
        <circle class="star" style="--d:1.5s" cx="196" cy="22" r="2" fill="#fff4df"/>
        <circle class="star" style="--d:.4s" cx="22" cy="92" r="1.6" fill="#fff4df"/>
        <circle class="star" style="--d:1.1s" cx="150" cy="60" r="1.4" fill="#fff4df"/>
        <circle cx="256" cy="54" r="58" fill="url(#halo)"/>
        <circle class="moon" cx="256" cy="54" r="27" fill="url(#moonG)"/>
        <path d="M0 168Q80 140 160 162T320 158V210H0Z" fill="#3d0c12"/>
        <path d="M46 170V206M276 166V206M236 180V206" stroke="#7fb36a" stroke-width="3"/>
        <g transform="translate(46 166)"><g class="grow" style="--d:1.3s"><use href="#flower" x="-14" y="-14" width="28" height="28"/></g></g>
        <g transform="translate(276 162)"><g class="grow" style="--d:1.5s"><use href="#flower" x="-14" y="-14" width="28" height="28"/></g></g>
        <g transform="translate(236 180)"><g class="grow" style="--d:1.7s"><use href="#flower" x="-9" y="-9" width="18" height="18"/></g></g>
        <g class="pop" style="--d:.9s">
          <path class="tail" d="M102 186Q76 184 80 150"/>
          <ellipse class="cat" cx="124" cy="166" rx="22" ry="26"/>
          <circle class="cat" cx="124" cy="128" r="18"/>
          <polygon class="cat" points="107,122 108,102 120,114"/>
          <polygon class="cat" points="141,122 140,102 128,114"/>
          <circle class="dk" cx="117" cy="128" r="2.3"/>
          <circle class="dk" cx="131" cy="128" r="2.3"/>
          <path d="M121 135Q124 138 127 135" fill="none" stroke="#3a0a10" stroke-width="1.5" stroke-linecap="round"/>
        </g>
        <g class="pop" style="--d:1.05s">
          <path class="tail" d="M194 186Q218 182 212 148"/>
          <ellipse class="cat" cx="170" cy="166" rx="22" ry="26"/>
          <circle class="cat" cx="168" cy="128" r="18"/>
          <polygon class="cat" points="151,122 150,102 162,114"/>
          <polygon class="cat" points="185,122 186,102 174,114"/>
          <path d="M158 128Q161 124 164 128M172 128Q175 124 178 128" fill="none" stroke="#3a0a10" stroke-width="1.6" stroke-linecap="round"/>
          <path d="M164 136Q168 139 172 136" fill="none" stroke="#3a0a10" stroke-width="1.5" stroke-linecap="round"/>
          <use href="#heart" transform="translate(170 150) scale(1.1)" fill="#ff6b6b"/>
        </g>
        <g transform="translate(64 96) scale(1.1)"><g class="pop" style="--d:.5s"><g class="float"><use href="#heart" fill="#ff6b6b"/></g></g></g>
        <g transform="translate(100 62) scale(.8)"><g class="pop" style="--d:.8s"><g class="float"><use href="#heart" fill="#f6c46b"/></g></g></g>
        <g transform="translate(208 70) scale(1.2)"><g class="pop" style="--d:1s"><g class="float"><use href="#heart" fill="#ff6b6b"/></g></g></g>
        <g transform="translate(274 114) scale(.8)"><g class="pop" style="--d:1.2s"><g class="float"><use href="#heart" fill="#f6c46b"/></g></g></g>
      </svg>
      <p id="toLine" class="to"></p>
      <p class="writing"><span id="typed"></span><span id="caret" class="caret"></span></p>
      <p id="sign" class="sign"></p>
    </article>
    <p class="label">Paper style</p>
    <div class="papers">
      <button type="button" class="chip red" data-paper="red" aria-pressed="true">Warm red</button>
      <button type="button" class="chip cream" data-paper="cream" aria-pressed="false">Cream</button>
      <button type="button" class="chip gold" data-paper="gold" aria-pressed="false">Gold</button>
    </div>
    <button id="replay" type="button" class="big">Replay delivery</button>
    <button id="again" type="button" class="ghost">Write another letter</button>
  </section>

</main>
<script>
(function(){
"use strict";
const $ = s => document.querySelector(s);
const $$ = s => Array.from(document.querySelectorAll(s));
let demoOn = location.hash === "#demo";
let session = 0, busy = false;
let letter = { from: "", to: "", msg: "" };

function later(fn, ms){ const s = session; setTimeout(() => { if (s === session) fn(); }, ms); }
function stopAll(){ session++; busy = false; }
function show(id){ $$(".screen").forEach(s => s.classList.toggle("on", s.id === id)); window.scrollTo(0, 0); }
function press(sel){ const b = $(sel); b.classList.add("press"); later(() => b.classList.remove("press"), 220); }
function setPaper(name){
  document.body.dataset.paper = name;
  $$("[data-paper]").forEach(b => b.setAttribute("aria-pressed", String(b.dataset.paper === name)));
}

/* Sounds made with Web Audio (no files) */
let ac = null, out = null;
function audio(){
  try{
    if (!ac){
      const C = window.AudioContext || window.webkitAudioContext;
      if (!C) return null;
      ac = new C();
      out = ac.createGain(); out.gain.value = .6; out.connect(ac.destination);
    }
    if (ac.state === "suspended" && ac.resume) ac.resume().catch(() => {});
    return ac;
  }catch(e){ return null; }
}
function tone(f, dur, vol, type, when, toF){
  const a = audio(); if (!a) return;
  try{
    const t = a.currentTime + (when || 0), o = a.createOscillator(), g = a.createGain();
    o.type = type || "sine";
    o.frequency.setValueAtTime(f, t);
    if (toF) o.frequency.exponentialRampToValueAtTime(toF, t + dur);
    g.gain.setValueAtTime(.0001, t);
    g.gain.exponentialRampToValueAtTime(vol, t + .02);
    g.gain.exponentialRampToValueAtTime(.0001, t + dur);
    o.connect(g); g.connect(out); o.start(t); o.stop(t + dur + .05);
  }catch(e){}
}
function sfxChime(){ [659.25, 783.99, 987.77, 1318.5].forEach((f, i) => tone(f, 1.6, .07, "sine", i * .12)); }
function sfxPlop(){ tone(300, .28, .25, "sine", 0, 110); }
function sfxTick(){ tone(1700, .03, .015, "triangle"); }
function sfxPop(){ tone(620, .14, .06, "sine", 0, 980); }
function sfxWhoosh(){
  const a = audio(); if (!a) return;
  try{
    const n = Math.floor(a.sampleRate * .9), buf = a.createBuffer(1, n, a.sampleRate), d = buf.getChannelData(0);
    for (let i = 0; i < n; i++) d[i] = (Math.random() * 2 - 1) * Math.sin(Math.PI * i / n) * .6;
    const src = a.createBufferSource(), bp = a.createBiquadFilter(), g = a.createGain();
    src.buffer = buf; bp.type = "bandpass"; bp.Q.value = .9;
    bp.frequency.setValueAtTime(260, a.currentTime);
    bp.frequency.exponentialRampToValueAtTime(1100, a.currentTime + .9);
    g.gain.value = .25;
    src.connect(bp); bp.connect(g); g.connect(out); src.start();
  }catch(e){}
}

/* The letter is written by hand, one character at a time */
function typeLetter(text, done){
  const box = $("#typed"), caret = $("#caret"), chars = Array.from(text);
  let i = 0;
  box.textContent = "";
  caret.classList.add("on");
  const step = () => {
    if (i >= chars.length){ caret.classList.remove("on"); done(); return; }
    const ch = chars[i++];
    const sp = document.createElement("span");
    sp.className = "ch"; sp.textContent = ch;
    box.appendChild(sp);
    if (i % 3 === 0 && ch.trim()) sfxTick();
    let d = 48 + Math.random() * 40;
    if (/[.,;:!?]/.test(ch)) d += 260;
    if (ch === "\n") d += 200;
    later(step, d);
  };
  later(step, 120);
}

function toCompose(fill){
  if (fill){ $("#from").value = "Carlos"; $("#to").value = "Ana"; $("#msg").value = "I love you. Thank you for being you."; }
  $("#hint").textContent = "";
  show("compose");
}

function sendLetter(){
  const msg = $("#msg").value.trim();
  if (!msg){ $("#hint").textContent = "Write a message first."; $("#msg").focus(); return; }
  letter = { from: $("#from").value.trim(), to: $("#to").value.trim(), msg: msg };
  $("#hint").textContent = "";
  $("#toLine").textContent = letter.to ? "For " + letter.to : "";
  runDelivery();
}

function runDelivery(){
  show("delivery");
  const pm = $("#pm"), env = $("#env"), tap = $("#tap");
  env.classList.remove("show", "open");
  tap.classList.remove("on");
  $$(".carry").forEach(c => c.classList.remove("off"));
  pm.classList.remove("walking");
  pm.style.transition = "none";
  pm.style.left = "-200px";
  void pm.offsetWidth;
  pm.style.transition = "";
  pm.classList.add("walking");
  void pm.offsetWidth;
  pm.style.left = "calc(50% - 150px)";
  sfxWhoosh();
  later(() => {
    pm.classList.remove("walking");
    $$(".carry").forEach(c => c.classList.add("off"));
    sfxPlop();
    env.classList.add("show");
    later(() => tap.classList.add("on"), 1000);
    later(() => {
      pm.classList.add("walking");
      pm.style.transition = "left 2.4s linear";
      pm.style.left = "120%";
    }, 500);
  }, 2800);
}

function tapEnvelope(){
  const env = $("#env");
  if (busy || !env.classList.contains("show")) return;
  busy = true;
  env.classList.add("open");
  $("#tap").classList.remove("on");
  sfxChime();
  later(() => { openLetter(); busy = false; }, 850);
}

function openLetter(){
  show("letter");
  const pop = $("#popup");
  pop.classList.remove("unfold"); void pop.offsetWidth; pop.classList.add("unfold");
  $("#typed").textContent = "";
  $("#sign").classList.remove("on");
  later(() => typeLetter(letter.msg, () => {
    $("#sign").textContent = "With love" + (letter.from ? ", " + letter.from : "");
    $("#sign").classList.add("on");
  }), 700);
}

/* #demo: hands-free loop of about 20 seconds */
function demoLoop(){
  if (!demoOn) return;
  stopAll();
  toCompose(true);
  setPaper("red");
  later(() => press("#send"), 1000);
  later(sendLetter, 1260);
  later(tapEnvelope, 4900);
  later(() => setPaper("cream"), 9800);
  later(() => setPaper("gold"), 10800);
  later(() => setPaper("red"), 11800);
  later(() => press("#replay"), 12900);
  later(runDelivery, 13160);
  later(demoLoop, 20000);
}

/* Any real touch ends the demo */
function onTouch(){
  if (!demoOn) return;
  demoOn = false;
  stopAll();
  $("#from").value = ""; $("#to").value = ""; $("#msg").value = "";
  toCompose(false);
}

$("#form").addEventListener("submit", e => e.preventDefault());
$("#send").addEventListener("click", () => { stopAll(); sendLetter(); });
$("#back").addEventListener("click", () => { stopAll(); toCompose(false); });
$("#again").addEventListener("click", () => { stopAll(); toCompose(false); });
$("#replay").addEventListener("click", () => { stopAll(); runDelivery(); });
$("#env").addEventListener("click", () => { stopAll(); tapEnvelope(); });
$("#env").addEventListener("keydown", e => {
  if (e.key === "Enter" || e.key === " "){ e.preventDefault(); stopAll(); tapEnvelope(); }
});
document.addEventListener("click", e => {
  const b = e.target.closest ? e.target.closest("[data-paper]") : null;
  if (b){ setPaper(b.dataset.paper); sfxPop(); }
});
window.addEventListener("pointerdown", onTouch, true);
window.addEventListener("touchstart", onTouch, { capture: true, passive: true });

setPaper("red");
if (demoOn) demoLoop();
})();
</script>
</body>
</html>