Back to the app
The code
Love Letter by Claude Sonnet 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.)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<meta name="theme-color" content="#7c1624">
<title>Love Letter Postman</title>
<style>
:root{--rd:#9e1b2c;--rd2:#7c1624;--cr:#fff3dc;--g1:#e0a93b;--g2:#f6d77e;--hand:"Segoe Script","Bradley Hand","Lucida Handwriting","Snell Roundhand","Brush Script MT","Comic Sans MS","Chalkboard SE",cursive}
*{box-sizing:border-box;-webkit-tap-highlight-color:transparent}
html,body{margin:0;height:100%;overflow:hidden;background:#3b0a14;font-family:Georgia,"Times New Roman",serif;color:var(--cr);touch-action:manipulation;-webkit-user-select:none;user-select:none}
input,textarea{-webkit-user-select:text;user-select:text}
button{font:inherit;color:inherit;border:0;cursor:pointer;touch-action:manipulation}
.hide{display:none!important}
/* ---------- write screen ---------- */
#write{position:fixed;left:0;top:0;right:0;bottom:0;overflow-y:auto;display:flex;padding:14px;background:radial-gradient(ellipse at 50% 0,#b3303f 0,#7c1624 55%,#4a0b16 100%)}
.form{position:relative;margin:auto;width:min(100%,460px);background:var(--cr);color:var(--rd2);border-radius:22px;padding:20px 18px 18px;box-shadow:0 0 0 3px var(--g1),0 0 0 7px #7c1624,0 0 0 9px var(--g2),0 18px 40px rgba(0,0,0,.45)}
h1{margin:0 44px 2px;text-align:center;font:700 31px/1.1 var(--hand);color:var(--rd)}
.sub{margin:0 0 4px;text-align:center;font-size:15px;color:#8a4a44}
label{display:block;margin:12px 0 4px;font-size:13px;letter-spacing:.08em;text-transform:uppercase;color:#8a2a36;font-weight:bold}
input,textarea{display:block;width:100%;font:20px var(--hand);color:#5a1420;background:#fffaf0;border:2px solid #e6c987;border-radius:14px;padding:12px 14px;outline:0}
input:focus,textarea:focus{border-color:var(--rd);box-shadow:0 0 0 3px rgba(224,169,59,.4)}
textarea{height:112px;resize:none}
.chips{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-top:8px;font-size:14px}
.chip{background:#f7e3b6;color:var(--rd2);border-radius:999px;padding:0 16px;min-height:44px;font-size:15px;font-weight:bold}
.styles{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}
.sty{border-radius:14px;padding:8px 4px 6px;background:#fffaf0;border:3px solid #ecd8a8;color:var(--rd2);font-size:13px;min-height:76px}
.sty i{display:block;height:34px;border-radius:8px;margin-bottom:5px;box-shadow:inset 0 0 0 2px rgba(0,0,0,.14)}
.sty.on{border-color:var(--rd);box-shadow:0 0 0 3px rgba(224,169,59,.55)}
.send{display:block;width:100%;margin-top:18px;min-height:62px;border-radius:18px;font-size:22px;font-weight:bold;letter-spacing:.05em;color:#5a1420;background:linear-gradient(#f9df8b,#e0a93b);box-shadow:0 4px 0 #a97618,0 8px 16px rgba(0,0,0,.3)}
.send:active{transform:translateY(3px);box-shadow:0 1px 0 #a97618}
.mu{width:44px;height:44px;border-radius:50%;background:rgba(60,8,20,.78);color:var(--g2);font-size:22px;line-height:1;position:relative;flex:none}
.mu.off{color:#b9908f}
.mu.off:after{content:"";position:absolute;left:9px;right:9px;top:50%;height:3px;margin-top:-1px;background:var(--cr);transform:rotate(-45deg);border-radius:2px}
.form .mu{position:absolute;right:12px;top:12px}
/* ---------- stage ---------- */
#stage{position:fixed;left:0;top:0;right:0;bottom:0;overflow:hidden;background:linear-gradient(#26060f 0%,#651325 45%,#b0404b 70%,#e8a26a 100%)}
#stage>*{position:absolute}
#sky{left:0;top:0;right:0;bottom:0}
.st{position:absolute;width:7px;height:7px;background:#fff0b8;-webkit-clip-path:polygon(50% 0,62% 38%,100% 50%,62% 62%,50% 100%,38% 62%,0 50%,38% 38%);clip-path:polygon(50% 0,62% 38%,100% 50%,62% 62%,50% 100%,38% 62%,0 50%,38% 38%);animation:tw 2.6s ease-in-out infinite alternate}
@keyframes tw{from{opacity:.25}to{opacity:1}}
#moonbg{right:9%;top:6%;width:min(24vw,104px);aspect-ratio:1;border-radius:50%;background:radial-gradient(circle at 36% 34%,#fffbe6,#f6d77e 62%,#dfa53a);box-shadow:0 0 36px 12px rgba(246,215,126,.35)}
#skyline{left:0;right:0;bottom:34%;height:22%;background-repeat:repeat-x;background-position:left bottom;background-size:auto 100%}
#ground{left:0;right:0;bottom:0;height:34%;background:linear-gradient(#f2d08a,#e3ae58 45%,#c4843a);border-top:4px solid #fff0c2;box-shadow:0 -10px 26px rgba(0,0,0,.25)}
#ground:before{content:"";position:absolute;left:0;top:0;right:0;bottom:0;background:repeating-linear-gradient(90deg,transparent 0 46px,rgba(110,50,20,.14) 46px 48px),repeating-linear-gradient(0deg,transparent 0 30px,rgba(110,50,20,.1) 30px 32px)}
#walker{left:0;bottom:calc(84px + 10%);width:min(34vw,150px);z-index:3;will-change:transform}
#walker svg{display:block;width:100%;height:auto;overflow:visible}
.wb{animation:wb .26s ease-in-out infinite alternate}
@keyframes wb{to{transform:translateY(-4px)}}
.lg{animation:step .52s ease-in-out infinite alternate}
.lg.a{transform-origin:52px 114px}
.lg.b{transform-origin:74px 114px;animation-direction:alternate-reverse}
@keyframes step{from{transform:rotate(-26deg)}to{transform:rotate(26deg)}}
.tail{transform-origin:34px 108px;animation:sway .7s ease-in-out infinite alternate}
@keyframes sway{from{transform:rotate(-10deg)}to{transform:rotate(10deg)}}
.idle .wb,.idle .lg{animation:none}
#cap{transform-origin:62px 40px}
#say{position:absolute;left:48%;bottom:100%;margin-bottom:6px;white-space:nowrap;background:var(--cr);color:var(--rd);font:bold 15px var(--hand);padding:8px 12px;border-radius:14px;box-shadow:0 3px 10px rgba(0,0,0,.35);opacity:0;transform:scale(.6);transform-origin:20% 100%;transition:opacity .25s,transform .25s}
#say.on{opacity:1;transform:none}
#say:after{content:"";position:absolute;left:14px;bottom:-8px;border:8px solid transparent;border-top-color:var(--cr);border-bottom:0}
#envw{left:50%;bottom:calc(84px + 4%);width:min(76vw,350px);margin-left:calc(min(76vw,350px) / -2);z-index:5;visibility:hidden}
#env{display:block;width:100%;height:auto;overflow:visible;cursor:pointer;filter:drop-shadow(0 12px 12px rgba(0,0,0,.4))}
.eb{animation:ebob 1.3s ease-in-out infinite alternate}
@keyframes ebob{from{transform:translateY(0) rotate(-1.5deg)}to{transform:translateY(-9px) rotate(1.5deg)}}
#hint{position:absolute;left:0;right:0;top:-46px;text-align:center;font:bold 21px var(--hand);color:var(--g2);text-shadow:0 2px 6px #000;opacity:0;pointer-events:none}
.ready #hint{opacity:1;animation:hint .9s ease-in-out infinite alternate}
@keyframes hint{to{transform:translateY(-8px)}}
.rg{transform-origin:120px 104px}
.ready .rg{animation:rg 1.2s ease-out infinite}
@keyframes rg{from{transform:scale(.8);opacity:.95}to{transform:scale(2.3);opacity:0}}
.fp{fill:var(--env3)}
#flap{transform-origin:120px 24px}
#env.open #flap{animation:flip .8s ease-in-out forwards}
#env.open .fp{animation:fpc .8s steps(1,end) forwards}
@keyframes flip{0%{transform:scaleY(1)}50%{transform:scaleY(0)}100%{transform:scaleY(-1)}}
@keyframes fpc{0%{fill:var(--env3)}50%{fill:var(--lin)}100%{fill:var(--lin)}}
#env.open #seal{opacity:0;transition:opacity .15s}
/* ---------- pop-up card ---------- */
#cardwrap{left:0;right:0;top:0;bottom:84px;display:flex;align-items:center;justify-content:center;padding:10px 8px 0;z-index:8;pointer-events:none}
#card{position:relative;pointer-events:auto;width:min(100%,520px);height:min(100%,800px);display:flex;flex-direction:column;border-radius:18px;box-shadow:0 18px 40px rgba(0,0,0,.5);transform-origin:50% 100%;opacity:0}
.show #card{animation:rise 1s cubic-bezier(.2,.9,.25,1.05) both}
@keyframes rise{0%{transform:translateY(40%) scale(.3) rotate(-5deg);opacity:0}25%{opacity:1}100%{transform:none;opacity:1}}
.dio{flex:0 0 auto;width:100%;aspect-ratio:400/270;max-height:56%;background:#000;border-radius:18px 18px 0 0;overflow:hidden;border-bottom:3px solid var(--gd);transform-origin:50% 100%}
.show .dio{animation:unf 1.1s cubic-bezier(.25,1.15,.4,1) .55s both}
@keyframes unf{from{transform:perspective(900px) rotateX(86deg)}to{transform:perspective(900px) rotateX(0)}}
.dio svg{display:block;width:100%;height:100%}
.paper{flex:1;min-height:0;position:relative;display:flex;flex-direction:column;padding:14px 20px 12px;overflow:hidden;border-radius:0 0 18px 18px;background-color:var(--paper);background-image:var(--pbg);color:var(--ink);font-family:var(--hand);font-size:24px;box-shadow:inset 0 0 0 5px var(--paper),inset 0 0 0 7px var(--gd)}
.to{flex:none;font-size:1.1em;font-weight:bold;margin-bottom:.2em}
.msg{flex:1;min-height:0;overflow:hidden;line-height:1.55em;background:linear-gradient(transparent calc(1.55em - 1px),var(--rule) 0) 0 0/100% 1.55em}
.from{flex:none;text-align:right;line-height:1.3em;padding-top:.2em}
.w{display:inline-block;white-space:nowrap}
.c{display:inline-block;opacity:0;white-space:pre}
.c.on{opacity:1;animation:ink .3s ease-out}
@keyframes ink{from{clip-path:inset(-10% 100% -10% 0);transform:translateY(2px) rotate(-4deg)}to{clip-path:inset(-10% -10% -10% -10%);transform:none}}
#pen{position:absolute;left:20px;top:30px;width:0;height:0;pointer-events:none;opacity:0;z-index:3;transition:left .09s linear,top .09s linear,opacity .3s}
#pen:after{content:"";position:absolute;left:-1px;top:-46px;width:8px;height:46px;background:linear-gradient(90deg,#a9741a,#f6d77e,#a9741a);border-radius:4px 4px 1px 1px;transform-origin:50% 100%;transform:rotate(36deg)}
#pen:before{content:"";position:absolute;left:-1px;top:-4px;width:8px;height:7px;background:var(--ink);-webkit-clip-path:polygon(0 0,100% 0,50% 100%);clip-path:polygon(0 0,100% 0,50% 100%);z-index:1}
.bh{position:absolute;bottom:18%;pointer-events:none;z-index:30;opacity:0;animation:bh 2.2s ease-out forwards}
@keyframes bh{0%{transform:translateY(0) scale(.5);opacity:1}100%{transform:translateY(-190px) scale(1.2) rotate(var(--r,0deg));opacity:0}}
/* svg paper layers */
.pl,.pz,.pt{transform-box:fill-box}
.pl{transform-origin:50% 100%;transform:scaleY(0)}
.pt{transform-origin:50% 0;transform:scaleY(0)}
.pz{transform-origin:50% 50%;transform:scale(0)}
.open .pl,.open .pt{animation:pop .8s cubic-bezier(.3,1.5,.5,1) var(--d,0s) both}
.open .pz{animation:zoom .9s cubic-bezier(.3,1.4,.5,1) var(--d,0s) both}
@keyframes pop{from{transform:scaleY(0)}to{transform:scaleY(1)}}
@keyframes zoom{from{transform:scale(0)}to{transform:scale(1)}}
.open .sw{animation:swing 3s ease-in-out var(--d,0s) infinite alternate}
@keyframes swing{from{transform:rotate(-6deg)}to{transform:rotate(6deg)}}
.fl{opacity:0}
.open .fl{animation:flo 3.6s ease-in-out var(--d,0s) infinite}
@keyframes flo{0%{transform:translateY(10px);opacity:0}25%{opacity:1}100%{transform:translateY(-38px);opacity:0}}
.gl{transform-box:fill-box;transform-origin:50% 50%}
.open .gl{animation:gl 3s ease-in-out infinite alternate}
@keyframes gl{from{transform:scale(.9)}to{transform:scale(1.08)}}
.open .tl{animation:tl 2.2s ease-in-out infinite alternate}
@keyframes tl{from{transform:rotate(-6deg)}to{transform:rotate(8deg)}}
.tw{animation:tw 2.4s ease-in-out infinite alternate}
/* bar */
#bar{left:0;right:0;bottom:0;z-index:20;display:flex;flex-wrap:wrap;gap:8px;justify-content:center;align-items:center;padding:14px 8px calc(10px + env(safe-area-inset-bottom));background:linear-gradient(rgba(40,5,14,0),rgba(40,5,14,.88) 45%)}
#sbs{display:flex;gap:6px}
.sb{width:44px;height:44px;border-radius:50%;border:3px solid var(--cr);box-shadow:0 2px 6px rgba(0,0,0,.4)}
.sb.on{border-color:var(--g2);box-shadow:0 0 0 3px var(--g1),0 2px 8px rgba(0,0,0,.5)}
.act{min-height:48px;padding:0 13px;border-radius:24px;font-size:16px;font-weight:bold;background:linear-gradient(#f9df8b,#e0a93b);color:#5a1420;box-shadow:0 3px 0 #a97618}
.act:active{transform:translateY(2px)}
#stage .mu{right:8px;top:8px;z-index:30}
#fing{position:fixed;left:50%;top:60%;width:46px;height:46px;margin:-23px 0 0 -23px;border-radius:50%;background:radial-gradient(circle,rgba(255,236,170,.95),rgba(224,169,59,.55) 70%);box-shadow:0 0 0 3px rgba(255,255,255,.5),0 4px 14px rgba(0,0,0,.4);z-index:99;pointer-events:none;opacity:0;transition:left .45s ease,top .45s ease,opacity .25s,transform .12s}
#fing.on{opacity:1}
#fing.pr{transform:scale(.68)}
#dtag{position:fixed;top:8px;left:50%;transform:translateX(-50%);z-index:98;background:rgba(40,5,14,.82);color:var(--g2);font-size:14px;padding:6px 14px;border-radius:16px;pointer-events:none;white-space:nowrap}
@media (orientation:landscape) and (max-height:600px){
#card{flex-direction:row;width:min(100%,900px)}
.dio{width:52%;max-height:none;height:100%;aspect-ratio:auto;border-radius:18px 0 0 18px;border-bottom:0;border-right:3px solid var(--gd)}
.paper{border-radius:0 18px 18px 0}
#walker{width:min(20vw,110px)}
#envw{width:min(40vw,260px);margin-left:calc(min(40vw,260px) / -2)}
}
</style>
</head>
<body>
<svg width="0" height="0" style="position:absolute" aria-hidden="true"><defs>
<linearGradient id="gw" x1="0" y1="0" x2="0" y2="1"><stop offset="0" style="stop-color:var(--w1)"/><stop offset="1" style="stop-color:var(--w2)"/></linearGradient>
<radialGradient id="gm"><stop offset=".35" style="stop-color:var(--glow);stop-opacity:.6"/><stop offset="1" style="stop-color:var(--glow);stop-opacity:0"/></radialGradient>
<filter id="sh" x="-10%" y="-10%" width="120%" height="130%"><feGaussianBlur in="SourceAlpha" stdDeviation="2.2"/><feOffset dy="3"/><feComponentTransfer><feFuncA type="linear" slope=".5"/></feComponentTransfer><feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge></filter>
<path id="hp" d="M0 8C-14 -4 -12 -16 -5 -16C-2 -16 0 -13 0 -11C0 -13 2 -16 5 -16C12 -16 14 -4 0 8Z"/>
<g id="fd"><g style="fill:var(--pet)"><ellipse cy="-10" rx="5.5" ry="10"/><ellipse cy="-10" rx="5.5" ry="10" transform="rotate(72)"/><ellipse cy="-10" rx="5.5" ry="10" transform="rotate(144)"/><ellipse cy="-10" rx="5.5" ry="10" transform="rotate(216)"/><ellipse cy="-10" rx="5.5" ry="10" transform="rotate(288)"/></g><circle r="5.5" style="fill:var(--gd)"/></g>
<g id="tu"><path d="M-10 0C-12 -15 -9 -27 -5 -30L0 -19L5 -30C9 -27 12 -15 10 0C5 6 -5 6 -10 0Z" style="fill:var(--pet2)"/><path d="M0 -19C-3 -10 -3 0 0 4C3 0 3 -10 0 -19Z" style="fill:var(--pet);opacity:.55"/></g>
</defs></svg>
<section id="write">
<div class="form">
<button class="mu" aria-label="Sound on or off">♪</button>
<h1>♥ Love Letter ♥</h1>
<p class="sub">Write it, press Send, and a little cat postman will deliver it.</p>
<label for="from">From</label>
<input id="from" maxlength="26" placeholder="Who is it from?" autocomplete="off">
<label for="to">For</label>
<input id="to" maxlength="26" placeholder="Who is it for?" autocomplete="off">
<label for="msg">Short message</label>
<textarea id="msg" maxlength="240" placeholder="A few sweet words..."></textarea>
<div class="chips"><span>Or tap one:</span><button class="chip">Sweet</button><button class="chip">Funny</button><button class="chip">Dreamy</button></div>
<label>Paper style</label>
<div class="styles" id="stys"></div>
<button class="send" id="send">Send ♥</button>
</div>
</section>
<section id="stage" class="hide">
<div id="sky"></div>
<div id="moonbg"></div>
<div id="skyline"></div>
<div id="ground"></div>
<div id="walker"><div id="say"></div>
<svg viewBox="0 0 130 160" aria-hidden="true"><g class="wb">
<path class="tail" d="M34 110C8 114 4 82 20 72" style="stroke:#e8893a" fill="none" stroke-width="10" stroke-linecap="round"/>
<g class="lg a"><rect x="44" y="112" width="15" height="36" rx="7" fill="#b45f1b"/><ellipse cx="54" cy="150" rx="13" ry="6.5" fill="#3b1e14"/></g>
<g class="lg b"><rect x="66" y="112" width="15" height="36" rx="7" fill="#d27a2b"/><ellipse cx="76" cy="150" rx="13" ry="6.5" fill="#3b1e14"/></g>
<path d="M36 76C34 112 40 126 64 126C88 126 94 112 90 76C88 62 38 62 36 76Z" fill="#c8323f"/>
<path d="M47 66L86 122" stroke="#e0a93b" stroke-width="5" fill="none"/>
<rect x="28" y="94" width="26" height="24" rx="5" fill="#f4dfae" stroke="#c9972f" stroke-width="2.5"/>
<circle cx="70" cy="90" r="2.6" fill="#f6d77e"/><circle cx="70" cy="104" r="2.6" fill="#f6d77e"/>
<path d="M38 36L36 12L58 28ZM88 36L92 12L70 28Z" fill="#e8893a"/>
<ellipse cx="63" cy="50" rx="28" ry="24" fill="#e8893a"/>
<ellipse cx="78" cy="58" rx="15" ry="11" fill="#fff1d6"/>
<circle cx="76" cy="49" r="3.7" fill="#2a1410"/><circle cx="77.2" cy="47.7" r="1.3" fill="#fff"/>
<circle cx="56" cy="49" r="3.7" fill="#2a1410"/><circle cx="57.2" cy="47.7" r="1.3" fill="#fff"/>
<path d="M85 54h6l-3 4z" fill="#d86a78"/>
<path d="M81 62q4 4 8 0" stroke="#4a2a1c" stroke-width="1.6" fill="none" stroke-linecap="round"/>
<path d="M90 58L110 53M90 61L110 63" stroke="#fff8ea" stroke-width="1.4" stroke-linecap="round"/>
<ellipse cx="66" cy="58" rx="4" ry="2.6" fill="#f08a8a" opacity=".5"/>
<g id="cap"><path d="M35 40C33 6 93 4 92 40Z" fill="#2f6fd0"/><rect x="35" y="33" width="57" height="8" rx="3" fill="#1f4fa8"/><path d="M80 38C94 33 110 37 114 43C102 46 88 45 80 42Z" fill="#1f4fa8"/><circle cx="63" cy="21" r="6.5" fill="#f2c24a"/><path d="M59.5 19h7v5h-7zM59.5 19l3.5 3 3.5-3" fill="#c8323f" stroke="#fff3dc" stroke-width=".6"/></g>
<path d="M80 80C94 84 98 96 92 103" stroke="#c8323f" stroke-width="11" fill="none" stroke-linecap="round"/>
<g class="menv"><rect x="88" y="93" width="30" height="21" rx="2.5" fill="#fff3dc" stroke="#d9a441" stroke-width="2"/><path d="M88.5 94L103 105L117.5 94" fill="none" stroke="#d9a441" stroke-width="2"/><circle cx="103" cy="105" r="3.2" fill="#c8323f"/></g>
<circle cx="91" cy="104" r="6" fill="#e8893a"/>
</g></svg></div>
<div id="envw"><div id="hint">Tap the envelope!</div>
<svg id="env" viewBox="0 0 240 170" role="button" aria-label="Open the letter">
<rect x="4" y="24" width="232" height="142" rx="9" style="fill:var(--env);stroke:var(--gd);stroke-width:2"/>
<path d="M4 24L236 24L120 112Z" style="fill:var(--lin)"/>
<path d="M4 30L124 102L4 162Z" style="fill:var(--env2);opacity:.55"/>
<path d="M236 30L116 102L236 162Z" style="fill:var(--env2);opacity:.55"/>
<path d="M4 166L120 84L236 166Z" style="fill:var(--env2);opacity:.9"/>
<text id="envto" x="120" y="152" text-anchor="middle" style="fill:var(--lin);font:bold 17px var(--hand)">For you</text>
<g id="flap"><path class="fp" d="M4 24L236 24L120 112Z" style="stroke:var(--env2);stroke-width:1.5"/></g>
<g id="seal"><circle cx="120" cy="104" r="14" style="fill:var(--gd)"/><use href="#hp" transform="translate(120 106) scale(.7)" style="fill:#a8192c"/></g>
<circle class="rg" cx="120" cy="104" r="15" fill="none" style="stroke:var(--g2);stroke-width:3;opacity:0"/>
</svg></div>
<div id="cardwrap" class="hide"><div id="card">
<div class="dio"><svg id="dsvg" viewBox="0 0 400 270" preserveAspectRatio="xMidYMid slice"></svg></div>
<div class="paper" id="paper"><div class="to" id="lto"></div><div class="msg" id="lmsg"></div><div class="from" id="lfrom"></div><i id="pen"></i></div>
</div></div>
<button class="mu" aria-label="Sound on or off">♪</button>
<div id="bar"><div id="sbs"></div><button class="act" id="rep">↻ Replay</button><button class="act" id="edit">✎ Edit</button></div>
</section>
<div id="fing"></div>
<div id="dtag" class="hide">Demo · touch anywhere to try it</div>
<script>
(()=>{
'use strict';
const $=s=>document.querySelector(s),$$=s=>[...document.querySelectorAll(s)];
const root=document.documentElement;
const wr=$('#write'),sg=$('#stage'),iF=$('#from'),iT=$('#to'),iM=$('#msg'),wk=$('#walker'),say=$('#say'),envw=$('#envw'),env=$('#env'),cw=$('#cardwrap'),card=$('#card'),dsvg=$('#dsvg'),pen=$('#pen'),fing=$('#fing'),lto=$('#lto'),lmsg=$('#lmsg'),lfrom=$('#lfrom'),paper=$('#paper');
let run=0,cur=0,mute=false,ug=false,ac=null,mg=null,nb=null,stepT=0,mt=0,mi=0,ready=false,opened=false,skip=false,demo=false,onReady=null,swallow=0,parts=[];
const data={from:'',to:'',msg:''};
const SM=['You make every ordinary day feel like a little bit of magic. I love you!','You are my favourite person to share the last cookie with. Please never change!','Under the same moon, my heart always finds its way back to you.'];
const sl=(ms,t)=>new Promise((ok,no)=>setTimeout(()=>t===run?ok():no(0),ms));
const safe=p=>p.catch(e=>{if(e!==0)console.error(e)});
/* ---------- paper styles ---------- */
const K='w1 w2 glow moon moon2 h1 h2 pet pet2 leaf ca ca2 cb cb2 paper ink rule env env2 env3 lin gd hrt pbg'.split(' ');
const STY=[
{n:'Rose Red',sw:'linear-gradient(135deg,#ee5a68,#8e1a2e)',v:['#4d0a1a','#a52a3d','#ffd98a','#fff0c2','#f2d58f','#8c1830','#6a0f24','#ffe3cf','#e5475b','#7f9444','#e8893a','#b85c1c','#fff1d6','#e4c795','#fff1e3','#7b1020','transparent','#c8323f','#a52230','#dd4a55','#f7dba5','#eab04a','#ff6b7a','radial-gradient(circle at 10% 8%,#ffd9cd,transparent 42%),radial-gradient(circle at 92% 96%,#ffd0c2,transparent 46%)']},
{n:'Cream Lace',sw:'linear-gradient(135deg,#fffaf0,#e9c98d)',v:['#e7b86e','#fbe9c8','#fff6dc','#fffdf4','#f1dfb8','#d98a5c','#b8423e','#c83a45','#ffffff','#6f8c45','#dc7a2b','#a85a1b','#6a3d34','#4a2a26','#fff8e8','#6f1a26','rgba(190,130,70,.3)','#f6e3b9','#e9cd97','#f1d8a6','#c8323f','#cf9326','#c8323f','none']},
{n:'Golden Foil',sw:'linear-gradient(135deg,#fbe08a,#c98f23)',v:['#33081a','#7b1d2e','#ffe28a','#ffe9a6','#f0c75f','#d19b35','#a8741d','#ffefb8','#f4be4a','#8a9b4a','#fff0d2','#e1c28a','#a3283a','#6d1322','#fbe9b9','#5c1521','transparent','#e2ae44','#c68f26','#f0c45c','#7b1d2e','#f3c75a','#ffd46b','linear-gradient(115deg,rgba(255,255,255,.55) 0,rgba(255,255,255,0) 30%,rgba(255,255,255,.4) 48%,rgba(255,255,255,0) 62%)']}
];
function applyStyle(){STY[cur].v.forEach((x,i)=>root.style.setProperty('--'+K[i],x));$$('.sty,.sb').forEach(b=>b.classList.toggle('on',+b.dataset.i===cur))}
function setStyle(i){cur=i;applyStyle();tone(500+i*130,0,.3,{type:'triangle',g:.05});if(!demo)sv()}
/* ---------- storage (always guarded) ---------- */
function ld(){try{return JSON.parse(localStorage.getItem('llp1')||'{}')||{}}catch(e){return {}}}
function sv(){try{localStorage.setItem('llp1',JSON.stringify({f:iF.value,t:iT.value,m:iM.value,s:cur,mu:mute?1:0}))}catch(e){}}
/* ---------- sounds made with code ---------- */
function rs(){try{const p=ac.resume();p&&p.catch&&p.catch(()=>{})}catch(e){}}
function AC(){
if(mute)return null;
if(!ac){
if(!ug)return null;
try{ac=new(window.AudioContext||window.webkitAudioContext)()}catch(e){return null}
mg=ac.createGain();mg.gain.value=.85;mg.connect(ac.destination);
const d=ac.createDelay(1),fb=ac.createGain(),wt=ac.createGain();
d.delayTime.value=.27;fb.gain.value=.3;wt.gain.value=.3;
mg.connect(d);d.connect(fb);fb.connect(d);d.connect(wt);wt.connect(ac.destination);
}
if(ac.state!=='running'){if(!ug)return null;rs()}
return ac;
}
function tone(f,t0,d,o){
const a=AC();if(!a)return;o=o||{};
const t=a.currentTime+t0,os=a.createOscillator(),g=a.createGain();
os.type=o.type||'sine';os.frequency.setValueAtTime(f,t);
if(o.to)os.frequency.exponentialRampToValueAtTime(o.to,t+d);
g.gain.setValueAtTime(.0001,t);g.gain.linearRampToValueAtTime(o.g||.07,t+.012);g.gain.exponentialRampToValueAtTime(.0001,t+d);
os.connect(g);g.connect(mg);os.start(t);os.stop(t+d+.05);
}
function noise(t0,d,f,f2,g,type){
const a=AC();if(!a)return;
if(!nb){nb=a.createBuffer(1,a.sampleRate,a.sampleRate);const c=nb.getChannelData(0);for(let i=0;i<c.length;i++)c[i]=Math.random()*2-1}
const t=a.currentTime+t0,s=a.createBufferSource(),fl=a.createBiquadFilter(),gn=a.createGain();
s.buffer=nb;s.loop=true;fl.type=type||'bandpass';fl.Q.value=.9;
fl.frequency.setValueAtTime(f,t);fl.frequency.exponentialRampToValueAtTime(f2||f,t+d);
gn.gain.setValueAtTime(.0001,t);gn.gain.linearRampToValueAtTime(g,t+d*.3);gn.gain.exponentialRampToValueAtTime(.0001,t+d);
s.connect(fl);fl.connect(gn);gn.connect(mg);s.start(t);s.stop(t+d+.05);
}
function meow(){
const a=AC();if(!a)return;
const t=a.currentTime,o=a.createOscillator(),g=a.createGain(),f=a.createBiquadFilter();
o.type='sawtooth';f.type='bandpass';f.Q.value=2.5;
o.frequency.setValueAtTime(520,t);o.frequency.linearRampToValueAtTime(900,t+.12);o.frequency.linearRampToValueAtTime(600,t+.4);
f.frequency.setValueAtTime(900,t);f.frequency.linearRampToValueAtTime(1800,t+.15);f.frequency.linearRampToValueAtTime(1000,t+.4);
g.gain.setValueAtTime(.0001,t);g.gain.linearRampToValueAtTime(.085,t+.06);g.gain.exponentialRampToValueAtTime(.0001,t+.45);
o.connect(f);f.connect(g);g.connect(mg);o.start(t);o.stop(t+.5);
}
const bell=(f,t,g)=>{g=g||.06;tone(f,t,1.2,{g});tone(f*2,t,.55,{g:g*.3})};
const sSend=()=>{noise(0,.5,500,2600,.05);[523,659,784,1047].forEach((f,i)=>bell(f,.05+i*.1,.045))};
const sStep=()=>tone(150+Math.random()*40,0,.1,{type:'triangle',g:.05,to:80});
const sDing=()=>{bell(784,0,.07);bell(659,.28,.07)};
const sOpen=()=>{noise(0,.4,3500,7000,.035,'highpass');[784,988,1175,1568].forEach((f,i)=>bell(f,.12+i*.09,.04))};
const pops=()=>{for(let i=0;i<7;i++)tone(320+i*55,.15+i*.17,.14,{g:.06,to:620+i*80});bell(1319,1.3,.04)};
const sTick=()=>noise(0,.05,2200+Math.random()*1800,3200,.012);
const sChime=()=>[523,659,784,1047,1319].forEach((f,i)=>bell(f,i*.12,.05));
const MEL=[523.25,659.25,783.99,880,783.99,659.25,587.33,659.25,783.99,1046.5,880,783.99,659.25,587.33,523.25,587.33];
const mbox=()=>{const f=MEL[mi++%MEL.length];tone(f,0,1.1,{g:.04});tone(f*2,0,.45,{g:.012})};
function updMute(){$$('.mu').forEach(b=>b.classList.toggle('off',mute))}
/* ---------- build the paper pop-up scene, styles, sky ---------- */
function cat(x,fl,r,a,b){
return `<g transform="translate(${x} 200) scale(${.95*fl} .95) rotate(${r})">`+
`<path class="tl" style="transform-origin:26px -4px;stroke:var(--${a});fill:none;stroke-width:11;stroke-linecap:round" d="M26 -4C62 -2 66 -44 46 -58"/>`+
`<path style="fill:var(--${a})" d="M-31 0C-35 -34 -22 -66 0 -66C22 -66 35 -34 31 0Z"/>`+
`<path style="stroke:var(--${b});stroke-width:3.5;fill:none;stroke-linecap:round" d="M-15 -22Q0 -16 15 -22M-12 -36Q0 -31 12 -36M-9 -49Q0 -45 9 -49"/>`+
`<g style="fill:var(--${a})"><circle cy="-78" r="21"/><path d="M-19 -86L-17 -108L-4 -93ZM19 -86L17 -108L4 -93Z"/></g>`+
`<path style="fill:var(--${b})" d="M-15 -90L-15 -101L-8 -93ZM15 -90L15 -101L8 -93Z"/></g>`;
}
function buildScene(){
let s='<rect width="400" height="270" fill="url(#gw)"/>';
for(let i=0;i<16;i++){const x=(i*73+20)%380+10,y=(i*41+14)%150+8;
s+=`<path class="tw" d="M0-5L1.4-1.4L5 0L1.4 1.4L0 5L-1.4 1.4L-5 0L-1.4-1.4Z" transform="translate(${x} ${y}) scale(${.5+(i%3)*.3})" style="fill:var(--glow);animation-delay:${(i%5)*.4}s"/>`}
s+=`<g class="pz" style="--d:.3s"><circle class="gl" cx="285" cy="80" r="72" fill="url(#gm)"/><circle cx="285" cy="80" r="40" style="fill:var(--moon);stroke:var(--gd);stroke-width:2"/><circle cx="271" cy="68" r="8" style="fill:var(--moon2)"/><circle cx="299" cy="93" r="11" style="fill:var(--moon2)"/><circle cx="298" cy="62" r="5" style="fill:var(--moon2)"/></g>`;
s+=`<g class="pl" style="--d:.15s" filter="url(#sh)"><path d="M-10 280V196C50 176 110 190 180 196C250 202 320 170 410 186V280Z" style="fill:var(--h1)"/></g>`;
s+=`<g class="pl" style="--d:.45s" filter="url(#sh)">${cat(168,-1,-4,'ca','ca2')}${cat(212,1,-4,'cb','cb2')}</g>`;
s+=`<g class="pl" style="--d:.6s" filter="url(#sh)"><path d="M-10 280V232C60 212 130 236 210 230C280 224 340 208 410 226V280Z" style="fill:var(--h2)"/></g>`;
[[22,34,1],[58,46,0],[100,36,1],[146,50,0],[256,48,1],[300,36,0],[342,50,1],[382,38,0]].forEach(([x,h,t],i)=>{
const y=272-h;
s+=`<g class="pl" style="--d:${.85+i*.09}s"><path d="M${x} 276C${x-5} ${276-h/3} ${x+5} ${276-h*2/3} ${x} ${y}" style="stroke:var(--leaf);stroke-width:3;fill:none"/><path d="M${x} ${y+h*.5}c-14 -2 -18 -12 -17 -17c10 0 17 7 17 17z" style="fill:var(--leaf)"/><use href="#${t?'tu':'fd'}" transform="translate(${x} ${y}) scale(${t?1.05:.95})"/></g>`});
[[70,34,1.2,'hrt',0],[118,58,.8,'gd',.3],[330,40,1.1,'gd',.15],[372,64,.8,'hrt',.45]].forEach(([x,l,sc,c,d])=>{
s+=`<g class="pt" style="--d:${.6+d}s"><g class="sw" style="transform-origin:${x}px 0px;--d:${d}s"><path d="M${x} 0V${l}" style="stroke:var(--gd);stroke-width:1.5;fill:none"/><use href="#hp" transform="translate(${x} ${l+10}) scale(${sc})" style="fill:var(--${c})"/></g></g>`});
[[190,96,.55,'hrt',0],[150,120,.4,'gd',1.2],[232,118,.45,'hrt',2.2],[120,90,.35,'gd',2.8],[250,150,.4,'hrt',.6]].forEach(([x,y,sc,c,d])=>{
s+=`<g class="fl" style="--d:${d}s"><use href="#hp" transform="translate(${x} ${y}) scale(${sc*1.4})" style="fill:var(--${c})"/></g>`});
s+=`<rect x="2" y="2" width="396" height="266" rx="12" fill="none" style="stroke:var(--gd)" stroke-width="4"/>`;
dsvg.innerHTML=s;
}
function buildUI(){
STY.forEach((s,i)=>{
const b=document.createElement('button');b.className='sty';b.dataset.i=i;b.innerHTML='<i></i>'+s.n;b.firstChild.style.background=s.sw;b.onclick=()=>setStyle(i);$('#stys').appendChild(b);
const c=document.createElement('button');c.className='sb';c.dataset.i=i;c.style.background=s.sw;c.setAttribute('aria-label',s.n);c.onclick=()=>setStyle(i);$('#sbs').appendChild(c);
});
const sk=$('#sky');
for(let i=0;i<26;i++){const e=document.createElement('i');e.className='st';e.style.cssText=`left:${Math.random()*100}%;top:${Math.random()*50}%;animation-delay:${Math.random()*3}s;transform:scale(${.5+Math.random()})`;sk.appendChild(e)}
const svg="<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 240 100'><g fill='#34091a'><rect x='4' y='40' width='44' height='60'/><path d='M0 42L26 18L52 42Z'/><rect x='60' y='56' width='36' height='44'/><rect x='104' y='30' width='50' height='70'/><path d='M100 32L129 8L158 32Z'/><rect x='166' y='50' width='34' height='50'/><rect x='206' y='36' width='30' height='64'/><path d='M202 38L221 14L240 38Z'/></g><g fill='#f6c453'><rect x='14' y='56' width='8' height='10'/><rect x='30' y='56' width='8' height='10'/><rect x='70' y='70' width='8' height='10'/><rect x='116' y='46' width='8' height='10'/><rect x='134' y='62' width='8' height='10'/><rect x='214' y='56' width='8' height='10'/></g></svg>";
$('#skyline').style.backgroundImage='url("data:image/svg+xml,'+encodeURIComponent(svg)+'")';
$$('.chip').forEach((b,i)=>b.onclick=()=>{iM.value=SM[i];sTick()});
}
/* ---------- the handwritten letter ---------- */
function fill(el,txt){
el.textContent='';const cs=[];
txt.split('\n').forEach((ln,li)=>{
if(li)el.appendChild(document.createElement('br'));
ln.split(' ').forEach((w,wi)=>{
if(wi)el.appendChild(document.createTextNode(' '));
if(!w)return;
const s=document.createElement('span');s.className='w';
for(const ch of w){const c=document.createElement('span');c.className='c';c.textContent=ch;s.appendChild(c);cs.push(c)}
el.appendChild(s);
});
});
return cs;
}
function fit(){
let s=Math.min(30,paper.clientWidth/11);
for(;s>10;s--){paper.style.fontSize=s+'px';if([lto,lmsg,lfrom].every(e=>e.scrollHeight<=e.clientHeight+1&&e.scrollWidth<=e.clientWidth+1))break}
paper.style.fontSize=s+'px';
}
function buildLetter(){
parts=[[lto,'Dear '+data.to+','],[lmsg,data.msg],[lfrom,'With love,\n'+data.from]].map(([el,tx])=>[el,fill(el,tx)]);
fit();
}
async function writeAll(t){
skip=false;pen.style.opacity=1;
const n=data.msg.length,k=demo?.55:1,base=Math.max(24,Math.min(72,6200/Math.max(n,20)))*k;
let i=0;
for(const [el,cs] of parts){
for(const c of cs){
const ch=c.textContent;
if(!skip)await sl(base*(.7+Math.random()*.6)*(/[.!?]/.test(ch)?5:/[,;:]/.test(ch)?3:1),t);
c.classList.add('on');
if(!skip){pen.style.left=(c.offsetLeft+c.offsetWidth)+'px';pen.style.top=(c.offsetTop+c.offsetHeight*.8)+'px';if(++i%2)sTick()}
}
if(!skip)await sl(350*k,t);
}
pen.style.opacity=0;
burst();
}
function burst(){
sChime();
for(let i=0;i<14;i++){
const h=document.createElement('i');h.className='bh';h.textContent='♥';
h.style.cssText=`left:${8+Math.random()*84}%;font-size:${14+Math.random()*20}px;animation-delay:${Math.random()*.7}s;--r:${Math.random()*50-25}deg;color:var(--${i%3?'hrt':'gd'})`;
card.appendChild(h);setTimeout(()=>h.remove(),3200);
}
}
/* ---------- the delivery ---------- */
function stopAll(){
clearInterval(stepT);clearInterval(mt);
[wk,env,card,$('.dio'),$('#flap'),$('#cap')].forEach(e=>{if(e&&e.getAnimations)e.getAnimations().forEach(a=>a.cancel())});
env.classList.remove('open','eb');envw.classList.remove('ready');envw.style.visibility='hidden';
cw.classList.remove('show');cw.classList.add('hide');dsvg.classList.remove('open');
wk.classList.remove('idle');say.classList.remove('on');$('.menv').style.visibility='visible';
pen.style.opacity=0;ready=false;opened=false;skip=false;
$$('.bh').forEach(e=>e.remove());
}
async function deliver(t){
stopAll();
$('#envto').textContent='For '+data.to.slice(0,16);
const W=innerWidth,k=demo?.8:1,cwid=wk.offsetWidth,xs=Math.max(6,W*.3-cwid/2);
wk.animate([{transform:`translateX(${-cwid-24}px)`},{transform:`translateX(${xs}px)`}],{duration:3400*k,easing:'linear',fill:'forwards'});
stepT=setInterval(sStep,270);
await sl(3400*k,t);
clearInterval(stepT);wk.classList.add('idle');
say.textContent='Special delivery!';say.classList.add('on');meow();
const hand=$('.menv'),a=hand.getBoundingClientRect();
envw.style.visibility='visible';
const b=env.getBoundingClientRect(),s=a.width/b.width;
const dx=a.left+a.width/2-b.left-b.width/2,dy=a.top+a.height/2-b.top-b.height/2;
hand.style.visibility='hidden';
env.animate([{transform:`translate(${dx}px,${dy}px) scale(${s})`,offset:0},{transform:`translate(${dx*.45}px,${dy*.3-70}px) scale(${(1+s)/2})`,offset:.5},{transform:'translate(0px,0px) scale(1)',offset:1}],{duration:900,easing:'ease-in-out'});
await sl(900,t);sDing();
env.classList.add('eb');envw.classList.add('ready');ready=true;
if(onReady){const f=onReady;onReady=null;f()}
await sl(500*k,t);
$('#cap').animate([{transform:'none'},{transform:'rotate(-16deg) translateY(-5px)'},{transform:'none'}],{duration:800});
await sl(1100*k,t);
say.classList.remove('on');wk.classList.remove('idle');
stepT=setInterval(sStep,270);
wk.animate([{transform:`translateX(${xs}px)`},{transform:`translateX(${W+40}px)`}],{duration:2800*k,easing:'ease-in',fill:'forwards'});
await sl(2800*k,t);clearInterval(stepT);
}
async function openEnv(t){
if(!ready||opened)return;
opened=true;ready=false;env.classList.remove('eb');envw.classList.remove('ready');
sOpen();env.classList.add('open');
await sl(850,t);
cw.classList.remove('hide');buildLetter();void cw.offsetWidth;
cw.classList.add('show');
dsvg.classList.remove('open');void dsvg.getBoundingClientRect();dsvg.classList.add('open');
env.animate([{transform:'none',opacity:1},{transform:'translateY(70px) scale(.85)',opacity:0}],{duration:700,easing:'ease-in',fill:'forwards'});
pops();
await sl(1800*(demo?.85:1),t);
clearInterval(mt);mt=setInterval(mbox,560);
await writeAll(t);
}
function readForm(){
data.from=(iF.value.trim()||'Your secret admirer').slice(0,26);
data.to=(iT.value.trim()||'My love').slice(0,26);
data.msg=(iM.value.trim()||SM[0]).slice(0,240);
}
function start(t){
readForm();if(!demo)sv();sSend();
wr.classList.add('hide');sg.classList.remove('hide');
safe(deliver(t));
}
/* ---------- hands-free demo (#demo) ---------- */
async function typeIn(el,txt,t,ms){el.value='';for(const ch of txt){el.value+=ch;sTick();await sl(ms,t)}}
async function tapEl(el,t){
if(el.scrollIntoView)el.scrollIntoView({block:'nearest'});
const r=el.getBoundingClientRect();
fing.style.left=(r.left+Math.min(r.width*.6,r.width-14))+'px';fing.style.top=(r.top+r.height*.55)+'px';
fing.classList.add('on');await sl(450,t);
fing.classList.add('pr');await sl(140,t);fing.classList.remove('pr');
}
async function demoOnce(t){
stopAll();sg.classList.add('hide');wr.classList.remove('hide');
iF.value=iT.value=iM.value='';cur=0;applyStyle();
await sl(400,t);
await tapEl(iF,t);await typeIn(iF,'Mimi',t,90);
await tapEl(iT,t);await typeIn(iT,'Leo',t,90);
await tapEl(iM,t);await typeIn(iM,'You make every day feel like a warm hug.',t,26);
await tapEl($('.sty[data-i="1"]'),t);setStyle(1);
await tapEl($('#send'),t);
await new Promise(r=>{onReady=r;start(t)});
await sl(450,t);
await tapEl(env,t);
await openEnv(t);
await sl(700,t);
await tapEl($('.sb[data-i="0"]'),t);setStyle(0);
await sl(500,t);
await tapEl($('#rep'),t);
safe(deliver(t));
await sl(1900,t);
fing.classList.remove('on');
}
function startDemo(){
if(demo)return;
demo=true;$('#dtag').classList.remove('hide');
(async()=>{while(demo){const t=++run;try{await demoOnce(t)}catch(e){if(e!==0)console.error(e)}}})();
}
function endDemo(){
if(!demo)return;
demo=false;run++;swallow=Date.now()+600;onReady=null;stopAll();
fing.classList.remove('on');$('#dtag').classList.add('hide');
sg.classList.add('hide');wr.classList.remove('hide');
iF.value=iT.value=iM.value='';cur=0;applyStyle();
}
/* ---------- wiring ---------- */
['pointerdown','touchstart','mousedown','keydown'].forEach(ev=>addEventListener(ev,e=>{if(e.isTrusted){ug=true;endDemo()}},true));
addEventListener('click',e=>{if(e.isTrusted){ug=true;if(Date.now()<swallow){e.stopPropagation();e.preventDefault()}}},true);
addEventListener('touchend',e=>{if(e.isTrusted)ug=true},true);
$('#send').onclick=()=>start(++run);
$('#rep').onclick=()=>{safe(deliver(++run))};
$('#edit').onclick=()=>{run++;stopAll();sg.classList.add('hide');wr.classList.remove('hide')};
env.onclick=()=>{if(ready&&!opened)safe(openEnv(run))};
paper.onclick=()=>{skip=true};
$$('.mu').forEach(b=>b.onclick=()=>{mute=!mute;updMute();if(!demo)sv();if(!mute)tone(880,0,.3,{g:.06})});
addEventListener('resize',()=>{if(!cw.classList.contains('hide')&&parts.length)fit()});
const isDemo=()=>/#demo$/i.test(location.hash);
addEventListener('hashchange',()=>{if(isDemo())startDemo();else endDemo()});
buildUI();buildScene();
const sd=ld();mute=!!sd.mu;updMute();
if(!isDemo()){iF.value=sd.f||'';iT.value=sd.t||'';iM.value=sd.m||'';cur=(sd.s|0)%3}
applyStyle();
if(isDemo())startDemo();
})();
</script>
</body>
</html>
