:root {
  --paper: #ecebe6;
  --card: #f8f6f0;
  --ink: #3b3a37;
  --ink2: #6f6c66;
  --line: rgba(59, 58, 55, .08);
  --red: #d9534a;
  --teal: #a8cbbf;
  --font: 'Caveat', 'LXGW WenKai TC', cursive;
  --hand: 'LXGW WenKai TC', 'Caveat', cursive;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--paper); color: var(--ink); font-family: var(--hand); }
body.en { --hand: 'Caveat', 'LXGW WenKai TC', cursive; }
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }

#paper {
  position: fixed; inset: 0;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255,255,255,.55), transparent 65%),
    repeating-linear-gradient(60deg, transparent 0 46px, var(--line) 46px 47px),
    repeating-linear-gradient(-60deg, transparent 0 46px, var(--line) 46px 47px),
    var(--paper);
  opacity: .9;
}
#paper::after {
  content: ''; position: absolute; inset: 0; opacity: .35; mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5 0 0 0 0 .48 0 0 0 0 .44 0 0 0 .35 0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)'/%3E%3C/svg%3E");
}
#doodles { position: fixed; inset: 0; pointer-events: none; }
#doodles svg { position: absolute; width: 18px; height: 18px; stroke: var(--ink2); stroke-width: 1.4; opacity: .55; animation: bob 6s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) rotate(8deg); } }
#globe { position: fixed; inset: 0; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#globe:active { cursor: grabbing; }

/* top bar */
#top { position: fixed; top: calc(10px + var(--safe-t)); left: 12px; right: 12px; display: flex; gap: 8px; align-items: center; z-index: 5; }
#top .spacer { flex: 1; }
.chip, .round {
  display: inline-flex; align-items: center; gap: 6px; height: 40px; padding: 0 14px; border-radius: 20px;
  background: rgba(248,246,240,.85); border: 1.5px solid var(--ink); font-family: var(--font); font-size: 21px; font-weight: 600;
  box-shadow: 2px 2px 0 rgba(59,58,55,.18); backdrop-filter: blur(4px);
}
.round { width: 40px; padding: 0; justify-content: center; }
#heartChip svg { fill: var(--red); stroke: var(--ink); }
#btnSound.off .wave { display: none; }
#heartChip.bump { animation: bump .5s; }
@keyframes bump { 40% { transform: scale(1.25) rotate(-6deg); } }

#hint {
  position: fixed; top: calc(62px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 4;
  font-family: var(--font); font-size: 22px; color: var(--ink2); text-align: center; pointer-events: none; width: 90%;
  transition: opacity .4s;
}
#pinLabel {
  position: fixed; z-index: 4; transform: translate(-50%, calc(-100% - 34px)); pointer-events: none;
  background: var(--card); border: 1.5px solid var(--ink); border-radius: 12px; padding: 3px 12px 2px;
  font-family: var(--font); font-size: 22px; font-weight: 600; white-space: nowrap; box-shadow: 2px 2px 0 rgba(59,58,55,.2);
}
#pinLabel::after { content: ''; position: absolute; left: 50%; bottom: -7px; width: 10px; height: 10px; background: var(--card); border-right: 1.5px solid var(--ink); border-bottom: 1.5px solid var(--ink); transform: translateX(-50%) rotate(45deg); }
#toast {
  position: fixed; left: 50%; top: 30%; transform: translate(-50%, -50%); z-index: 30; pointer-events: none;
  background: var(--card); border: 1.5px solid var(--ink); border-radius: 14px; padding: 8px 18px; font-family: var(--font); font-size: 24px;
  box-shadow: 3px 3px 0 rgba(59,58,55,.2); animation: toast 2.2s forwards;
}
@keyframes toast { 0% { opacity: 0; transform: translate(-50%, -30%); } 12%, 80% { opacity: 1; transform: translate(-50%, -50%); } 100% { opacity: 0; } }

/* call to action */
.pill, .primary, .ghost {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; border-radius: 999px; border: 1.5px solid var(--ink);
  font-family: var(--font); font-size: 23px; font-weight: 600; padding: 8px 22px; background: var(--card); box-shadow: 2px 3px 0 rgba(59,58,55,.25);
  white-space: nowrap;
  transition: transform .12s, box-shadow .12s;
}
.pill:active, .primary:active, .ghost:active { transform: translate(2px, 2px); box-shadow: 0 0 0 rgba(0,0,0,0); }
.primary { background: var(--ink); color: var(--card); }
.primary svg { stroke: var(--card); }
.primary:disabled { opacity: .35; pointer-events: none; }
.ghost.danger { color: var(--red); border-color: var(--red); }
#cta { position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%); z-index: 5; font-size: 25px; padding: 10px 28px; white-space: nowrap; }
#cta:active { transform: translate(calc(-50% + 2px), 2px); }
#cta.glow { animation: glow 2s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 2px 3px 0 rgba(59,58,55,.25), 0 0 0 8px rgba(217,83,74,.12); } }
.link { background: none; border: none; font-family: var(--font); font-size: 19px; text-decoration: underline; color: var(--ink2); padding: 4px; }

/* bottom nav */
#nav {
  position: fixed; left: 50%; bottom: calc(18px + var(--safe-b)); transform: translateX(-50%); z-index: 5; display: flex; gap: 4px; padding: 5px;
  background: rgba(248,246,240,.9); border: 1.5px solid var(--ink); border-radius: 999px; box-shadow: 2px 3px 0 rgba(59,58,55,.2);
  width: max-content;
}
#nav button { white-space: nowrap; display: flex; flex-direction: column; align-items: center; gap: 0; border: none; background: none; border-radius: 999px; padding: 5px 16px 3px; font-family: var(--font); font-size: 16px; color: var(--ink2); }
#nav button.on { background: #e3e1da; color: var(--ink); }

/* clue sheet */
.sheet {
  position: fixed; left: 50%; bottom: calc(86px + var(--safe-b)); transform: translateX(-50%); width: min(460px, calc(100% - 24px)); z-index: 6;
  background: var(--card); border: 1.5px solid var(--ink); border-radius: 18px; padding: 12px 14px; box-shadow: 3px 4px 0 rgba(59,58,55,.2);
  background-image: repeating-linear-gradient(transparent 0 29px, var(--line) 29px 30px); background-position: 0 8px;
  animation: rise .35s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes rise { from { transform: translate(-50%, 40px); opacity: 0; } }
.cc-head { display: flex; gap: 10px; align-items: center; }
.mini-stamp { width: 40px; height: 47px; flex: none; }
.cc-to { flex: 1; line-height: 1.1; }
.cc-label { font-family: var(--font); font-size: 16px; color: var(--ink2); }
.cc-name { font-family: var(--font); font-size: 21px; font-weight: 700; line-height: 1.15; }
#clueList { margin: 6px 0 8px; padding-left: 22px; font-size: 17px; line-height: 30px; }
#clueList li { animation: ink .6s; }
#clueList li.locked { color: transparent; text-shadow: 0 0 8px rgba(59,58,55,.4); user-select: none; }
@keyframes ink { from { opacity: 0; filter: blur(3px); } }
.cc-actions { display: flex; gap: 8px; justify-content: space-between; }
.cc-actions .ghost { font-size: 17px; padding: 6px 12px; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.cc-actions .primary { flex: none; padding: 7px 20px; }

/* overlays */
.overlay {
  position: fixed; inset: 0; z-index: 20; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  padding: calc(56px + var(--safe-t)) 16px calc(24px + var(--safe-b)); background: rgba(236,235,230,.86); backdrop-filter: blur(3px);
  animation: fade .3s;
}
@keyframes fade { from { opacity: 0; } }
.close { position: absolute; top: calc(12px + var(--safe-t)); left: 14px; width: 40px; height: 40px; border-radius: 50%; border: 1.5px solid var(--ink); background: var(--card); font-size: 18px; }
.lv-meta { font-family: var(--font); font-size: 19px; color: var(--ink2); text-align: center; }
.letter {
  position: relative; width: min(400px, 100%); min-height: 380px; padding: 30px 26px 26px; background: #fbf9f3; border-radius: 4px;
  box-shadow: 0 1px 0 rgba(0,0,0,.05), 6px 10px 26px rgba(59,58,55,.18);
  background-image: linear-gradient(#fbf9f3 0 31px, transparent 31px), repeating-linear-gradient(transparent 0 37px, rgba(59,58,55,.13) 37px 38px);
  background-position: 0 0, 0 30px; background-repeat: no-repeat, repeat;
  animation: unfold .6s cubic-bezier(.2,.9,.3,1.1); transform-origin: 50% 0;
}
@keyframes unfold { from { transform: perspective(800px) rotateX(-70deg) translateY(30px); opacity: 0; } }
.letter .stamp { position: absolute; right: 18px; bottom: 16px; width: 64px; height: 75px; transform: rotate(4deg); }
.postmark { position: absolute; right: 54px; bottom: 44px; width: 74px; height: 74px; pointer-events: none; opacity: .8; }
.salute { font-family: var(--hand); font-weight: 400; font-size: 30px; margin: 0; line-height: 38px; }
.postmark svg, .big-postmark svg { width: 100%; height: 100%; }
.body, .sign { font-family: var(--hand); font-size: 24px; line-height: 38px; margin: 0; white-space: pre-wrap; }
body.en .salute { font-size: 36px; }
body.en .body, body.en .sign { font-size: 28px; }
.sign { margin-top: 0; padding-right: 80px; color: var(--ink2); font-size: 20px; }
.lv-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.lv-actions .ghost { font-size: 19px; padding: 7px 16px; }
.ghost.saved { background: var(--ink); color: var(--card); }

.letter.write textarea {
  width: 100%; border: none; background: transparent; resize: none; outline: none; font-family: var(--hand); font-size: 24px; line-height: 38px; color: var(--ink); padding: 0;
}
body.en .letter.write textarea { font-size: 28px; }
.starters { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; max-width: 420px; }
.starters button { border: 1.2px dashed var(--ink2); background: transparent; border-radius: 999px; padding: 3px 12px; font-family: var(--hand); font-size: 16px; color: var(--ink2); }

/* result & day */
.result-card {
  position: relative; width: min(380px, 100%); background: var(--card); border: 1.5px solid var(--ink); border-radius: 20px; padding: 26px 22px 20px;
  text-align: center; box-shadow: 4px 6px 0 rgba(59,58,55,.2); animation: rise2 .4s cubic-bezier(.2,.9,.3,1.3);
}
@keyframes rise2 { from { transform: scale(.85); opacity: 0; } }
.result-card h2 { font-family: var(--font); font-size: 34px; margin: 6px 0 4px; }
.result-card p { font-size: 18px; line-height: 1.5; margin: 0 0 10px; color: var(--ink2); }
.big-postmark { width: 120px; height: 120px; margin: -70px auto 0; animation: thump .5s .15s both cubic-bezier(.3,1.6,.5,1); }
@keyframes thump { from { transform: scale(2.2) rotate(-25deg); opacity: 0; } }
.rs-hearts { font-family: var(--font); font-size: 30px; color: var(--red); min-height: 36px; }
.rs-stamp { display: flex; flex-direction: column; align-items: center; gap: 2px; margin: 4px 0 12px; font-family: var(--font); font-size: 20px; }
.rs-stamp canvas { width: 86px; height: 101px; animation: pop .6s .5s both cubic-bezier(.3,1.6,.5,1); }
@keyframes pop { from { transform: scale(0) rotate(-30deg); } }
.rank { font-family: var(--font); font-size: 22px; margin: 6px 0 14px; }
.rank .bar { height: 8px; border: 1.2px solid var(--ink); border-radius: 6px; margin: 6px auto 0; width: 80%; overflow: hidden; }
.rank .bar i { display: block; height: 100%; background: var(--ink); }

/* album */
.title { font-family: var(--font); font-size: 34px; margin: 0; }
.tabs { display: flex; background: #e3e1da; border-radius: 999px; padding: 4px; border: 1.2px solid var(--ink); }
.tabs button { border: none; background: none; border-radius: 999px; padding: 5px 22px; font-family: var(--font); font-size: 20px; }
.tabs button.on { background: var(--card); box-shadow: 1px 1px 0 rgba(0,0,0,.15); }
.count { font-family: var(--font); font-size: 18px; color: var(--ink2); }
.stamp-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px 10px; width: min(420px, 100%); overflow-y: auto; flex: 1; padding: 4px 4px 20px; align-content: start; }
.stamp-cell { display: flex; flex-direction: column; align-items: center; font-family: var(--font); font-size: 17px; line-height: 1.1; text-align: center; }
.stamp-cell canvas { width: 100%; max-width: 104px; aspect-ratio: 150/176; }
.stamp-cell small { color: var(--ink2); font-size: 14px; }
.letter-list { width: min(420px, 100%); overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 10px; padding-bottom: 20px; }
.letter-item { text-align: left; background: var(--card); border: 1.2px solid var(--ink); border-radius: 12px; padding: 10px 14px; font-size: 17px; line-height: 1.4; }
.letter-item small { display: block; color: var(--ink2); font-family: var(--font); font-size: 16px; }
.empty { font-family: var(--font); font-size: 20px; color: var(--ink2); text-align: center; margin-top: 30px; }

/* menu */
.menu-card { width: min(360px, 100%); background: var(--card); border: 1.5px solid var(--ink); border-radius: 18px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.menu-card label { display: flex; justify-content: space-between; align-items: center; font-family: var(--font); font-size: 21px; gap: 10px; }
.menu-card select { font-family: var(--hand); font-size: 16px; padding: 4px 8px; border: 1.2px solid var(--ink); border-radius: 8px; background: #fff; color: var(--ink); max-width: 60%; }

/* intro */
#intro { background: rgba(236,235,230,.7); }
.intro-card { width: min(400px, 100%); text-align: center; background: #fbf9f3; padding: 30px 26px 24px; border-radius: 6px; box-shadow: 6px 10px 26px rgba(59,58,55,.18); animation: unfold .7s; }
.intro-card h1 { font-family: var(--hand); font-size: 42px; margin: 0 0 8px; font-weight: 700; }
.intro-card h1 small { display: block; font-family: 'Caveat', cursive; font-size: 26px; font-weight: 400; color: var(--ink2); }
.intro-card p { font-size: 19px; line-height: 1.7; margin: 0 0 20px; white-space: pre-line; }

#loading { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; background: var(--paper); transition: opacity .6s; font-size: 48px; }
#loading span { animation: bob 1.4s ease-in-out infinite; display: inline-block; }
#loading.done { opacity: 0; pointer-events: none; }

@media (min-width: 900px) {
  .sheet { left: auto; right: 28px; transform: none; bottom: 110px; animation: none; }
}
