:root {
  --paper: #f6efe4;
  --card: #fffaf2;
  --ink: #2b2220;
  --muted: #7a6a62;
  --line: #e6d9c6;
  --accent: #9b2c3a;
  --accent-ink: #fff7f0;
  --envelope: #ead8bd;
  --envelope-dark: #d9c19d;
  --envelope-inside: #c9ad84;
  --shadow: 0 1px 2px rgba(60, 35, 20, .06), 0 8px 28px rgba(60, 35, 20, .08);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #1d1817; --card: #272120; --ink: #f1e7dc; --muted: #b3a196; --line: #3b3230;
    --accent: #e0788a; --accent-ink: #1d1817; --envelope: #5a4a3c; --envelope-dark: #4a3c30; --envelope-inside: #382d24;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 28px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--paper); color: var(--ink);
  font: 17px/1.55 var(--sans);
  padding: env(safe-area-inset-top) 16px calc(32px + env(safe-area-inset-bottom));
}
main { max-width: 640px; margin: 0 auto; }
h1, h2, .brand, .letter, .stats strong { font-family: var(--serif); }
h1 { font-size: 1.7rem; line-height: 1.2; margin: .4em 0 .3em; font-weight: 600; text-wrap: balance; }
h2 { font-size: 1.15rem; margin: 1.6em 0 .6em; }
a { color: var(--accent); }

.top { text-align: center; padding: 20px 0 6px; }
.brand { color: var(--ink); text-decoration: none; font-size: 1.35rem; letter-spacing: .02em; font-style: italic; }

.nav { display: flex; justify-content: center; gap: 4px; margin: 6px auto 20px; max-width: 640px; flex-wrap: wrap; }
.nav a, .tabs a {
  color: var(--muted); text-decoration: none; padding: 8px 14px; border-radius: 999px; font-size: .95rem;
  min-height: 44px; display: inline-flex; align-items: center;
}
.nav a.on, .tabs a.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 24px 20px; box-shadow: var(--shadow); }
.card.narrow { max-width: 420px; margin: 0 auto; }
.center { text-align: center; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: .9rem; }
.error { color: var(--accent); font-weight: 600; }
.notice { background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 10px; padding: 10px 14px; }
.eyebrow { color: var(--muted); font-size: .85rem; letter-spacing: .06em; text-transform: uppercase; margin: 0 0 8px; }

.stack { display: grid; gap: 14px; margin-top: 16px; }
label { display: grid; gap: 6px; font-weight: 600; font-size: .95rem; }
label .muted { font-weight: 400; }
input, textarea {
  font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--line);
  border-radius: 10px; padding: 12px; width: 100%;
}
textarea { font-family: var(--serif); font-size: 1.08rem; line-height: 1.7; resize: vertical; min-height: 280px; }
input:focus-visible, textarea:focus-visible, .btn:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  font: 600 1rem var(--sans); background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 999px;
  padding: 12px 22px; min-height: 48px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn.big { font-size: 1.1rem; padding: 14px 28px; }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.btn:disabled { opacity: .6; }
.link { text-decoration: none; font-weight: 600; display: inline-block; padding: 12px 0; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 16px 0 4px; text-align: center; }
.stats div { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 6px; }
.stats strong { display: block; font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.stats small { font-size: .9rem; color: var(--muted); }
.stats span { color: var(--muted); font-size: .8rem; }

/* Envelope */
.envelope { position: relative; width: 200px; height: 130px; margin: 8px auto 12px; background: var(--envelope); border-radius: 8px; }
.envelope .flap {
  position: absolute; inset: 0 0 auto 0; height: 78px; background: var(--envelope-dark);
  clip-path: polygon(0 0, 100% 0, 50% 100%); border-radius: 8px 8px 0 0; transform-origin: top; z-index: 2;
}
.envelope .seal {
  position: absolute; left: 50%; top: 58px; width: 42px; height: 42px; margin-left: -21px; z-index: 3;
  border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center;
  font-size: 18px; box-shadow: 0 2px 6px rgba(0,0,0,.25);
  animation: pulse 2.4s ease-in-out infinite;
}
/* Open: flap folded up behind, paper sticking out, front pocket on top. */
.envelope.open { margin-top: 78px; background: var(--envelope-dark); }
.envelope.open .flap { top: -66px; height: 66px; clip-path: polygon(0 100%, 100% 100%, 50% 0); border-radius: 0; z-index: 0; }
.envelope.open .paper {
  position: absolute; left: 18px; right: 18px; top: -46px; height: 120px; z-index: 1;
  background: var(--card); border: 1px solid var(--line); border-radius: 4px;
  background-image: repeating-linear-gradient(transparent 0 13px, var(--line) 13px 14px); background-clip: content-box; padding: 16px 14px;
}
.envelope.open::after {
  content: ""; position: absolute; inset: 0; z-index: 2; background: var(--envelope); border-radius: 8px;
  clip-path: polygon(0 0, 50% 48%, 100% 0, 100% 100%, 0 100%);
}
.envelope.empty { opacity: .5; }
@keyframes pulse { 50% { transform: scale(1.07); } }

/* Letter */
.letter {
  background: var(--card); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow);
  padding: 32px 24px 24px; font-size: 1.15rem; line-height: 1.75;
  border-top: 4px solid var(--accent);
}
.letter .eyebrow { font-family: var(--sans); }
.letter .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.letter .meta { color: var(--muted); font-size: .85rem; font-family: var(--sans); margin-top: 28px; text-align: right; }
.letter.sealed, [data-after].sealed { display: none; }
.letter.unfold { transform-origin: top center; animation: unfold .9s cubic-bezier(.2, .8, .2, 1) both; }
@keyframes unfold {
  0%   { opacity: 0; transform: perspective(1000px) rotateX(-75deg) scaleY(.6) translateY(-30px); }
  60%  { opacity: 1; }
  100% { opacity: 1; transform: none; }
}

/* ---------- Envelope opening scene ---------- */
.scene { min-height: 62vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding-top: 110px; transition: opacity .45s ease, transform .45s ease; }
.scene.done { opacity: 0; transform: translateY(30px) scale(.96); }
.env {
  position: relative; display: block; width: min(330px, 86vw); aspect-ratio: 10 / 7;
  border: 0; padding: 0; background: none; cursor: pointer; perspective: 900px;
  -webkit-tap-highlight-color: transparent; font: inherit; color: inherit;
}
.env:focus-visible { outline: 2px solid var(--accent); outline-offset: 8px; border-radius: 10px; }
.env > span { position: absolute; display: block; }
.env-back  { inset: 0; background: var(--envelope-inside); border-radius: 10px; z-index: 0; box-shadow: var(--shadow); }
.env-front {
  inset: 0; z-index: 3; border-radius: 10px;
  background: linear-gradient(160deg, var(--envelope) 0%, var(--envelope) 60%, var(--envelope-dark) 140%);
  clip-path: polygon(0 0, 50% 54%, 100% 0, 100% 100%, 0 100%);
}
.env-flap {
  left: 0; right: 0; top: 0; height: 56%; z-index: 4; transform-origin: top center;
  background: linear-gradient(180deg, var(--envelope-dark), var(--envelope-dark) 70%, color-mix(in srgb, var(--envelope-dark) 80%, #000));
  clip-path: polygon(0 0, 100% 0, 50% 100%); border-radius: 10px 10px 0 0;
  backface-visibility: visible;
}
.env-seal {
  left: 50%; top: calc(56% - 26px); width: 52px; height: 52px; margin-left: -26px; z-index: 5;
  border-radius: 50%; display: grid !important; place-items: center; font-size: 22px;
  background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--accent) 70%, #fff), var(--accent) 55%, color-mix(in srgb, var(--accent) 70%, #000));
  color: var(--accent-ink); box-shadow: 0 3px 8px rgba(0,0,0,.3), inset 0 0 0 3px color-mix(in srgb, var(--accent) 80%, #000);
  animation: pulse 2.4s ease-in-out infinite;
}
.env-paper {
  left: 7%; right: 7%; top: 6%; height: 88%; z-index: 2; padding: 9% 9%;
  background: var(--card); border: 1px solid var(--line); border-radius: 4px; box-shadow: 0 2px 10px rgba(60,35,20,.12);
  text-align: left;
}
.env-paper-title { display: block; font-family: var(--serif); font-size: 1rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.env-paper-lines { display: block; height: 60%; margin-top: 10px; background: repeating-linear-gradient(transparent 0 11px, var(--line) 11px 12px); }
.env-hearts { inset: 0; z-index: 7; pointer-events: none; }
.env-hearts i { position: absolute; bottom: 55%; left: 50%; font-style: normal; color: var(--accent); opacity: 0; font-size: 18px; }
.tap-hint { color: var(--muted); margin-top: 26px; transition: opacity .3s; }
.env:hover .env-flap { transform: rotateX(14deg); transition: transform .3s ease; }

/* The opening sequence: seal pops, flap swings up and back, letter slides out and comes forward. */
.scene.opening .tap-hint { opacity: 0; }
.scene.opening .env { cursor: default; }
.scene.opening .env-seal  { animation: seal-pop .45s ease-in forwards; }
.scene.opening .env-flap  { animation: flap-open .75s cubic-bezier(.4, 0, .2, 1) .35s forwards; }
.scene.opening .env-paper { animation: paper-out 1.5s cubic-bezier(.45, 0, .2, 1) 1.05s forwards; }
.scene.opening .env-hearts i { animation: heart-float 1.8s ease-out forwards; }
.env-hearts i:nth-child(1) { --x: -120px; animation-delay: 1.35s !important; }
.env-hearts i:nth-child(2) { --x: -70px;  animation-delay: 1.5s !important; font-size: 13px; }
.env-hearts i:nth-child(3) { --x: -25px;  animation-delay: 1.4s !important; font-size: 22px; }
.env-hearts i:nth-child(4) { --x: 20px;   animation-delay: 1.6s !important; }
.env-hearts i:nth-child(5) { --x: 65px;   animation-delay: 1.45s !important; font-size: 14px; }
.env-hearts i:nth-child(6) { --x: 110px;  animation-delay: 1.55s !important; font-size: 20px; }
.env-hearts i:nth-child(7) { --x: 0px;    animation-delay: 1.7s !important; font-size: 12px; }

@keyframes seal-pop {
  0%   { transform: scale(1); opacity: 1; }
  40%  { transform: scale(1.25) rotate(-8deg); opacity: 1; }
  100% { transform: scale(0) rotate(25deg); opacity: 0; }
}
@keyframes flap-open {
  0%   { transform: rotateX(0); z-index: 4; }
  50%  { z-index: 4; }
  51%  { z-index: 1; }
  100% { transform: rotateX(180deg); z-index: 1; }
}
@keyframes paper-out {
  0%   { transform: translateY(0); z-index: 2; }
  55%  { transform: translateY(-82%); z-index: 2; }
  56%  { transform: translateY(-82%); z-index: 6; }
  100% { transform: translateY(-18%) scale(1.14) rotate(-1.5deg); z-index: 6; box-shadow: 0 18px 40px rgba(60,35,20,.25); }
}
@keyframes heart-float {
  0%   { opacity: 0; transform: translate(-50%, 0) scale(.4); }
  25%  { opacity: 1; }
  100% { opacity: 0; transform: translate(calc(-50% + var(--x)), -170px) scale(1.1) rotate(15deg); }
}

/* Lists */
.tabs { display: flex; gap: 4px; justify-content: center; margin-bottom: 12px; flex-wrap: wrap; }
.list { list-style: none; padding: 0; margin: 0; background: var(--card); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.list li + li { border-top: 1px solid var(--line); }
.list a { display: flex; justify-content: space-between; gap: 12px; padding: 14px 16px; color: var(--ink); text-decoration: none; min-height: 48px; align-items: center; }
.list .t { font-family: var(--serif); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.list .t b { color: var(--muted); font-weight: 400; }
.list .s { color: var(--muted); font-size: .85rem; white-space: nowrap; }
.list .s.opened { color: var(--accent); }
.list .empty { padding: 20px 16px; color: var(--muted); text-align: center; }

/* Seal confirmation dialog */
.modal {
  border: 1px solid var(--line); border-radius: 22px; padding: 30px 22px 20px; margin: auto;
  width: min(380px, calc(100vw - 32px)); background: var(--card); color: var(--ink); text-align: center;
  box-shadow: 0 24px 60px rgba(40, 20, 10, .35);
}
.modal[open] { animation: modal-in .32s cubic-bezier(.2, .9, .3, 1.2); }
.modal::backdrop { background: rgba(35, 18, 12, .45); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.modal[open]::backdrop { animation: fade-in .25s ease; }
.modal h2 { font-family: var(--serif); font-size: 1.45rem; margin: 6px 0 8px; }
.modal p { color: var(--muted); margin: 0 0 22px; text-wrap: pretty; }
.modal-seal {
  position: relative; width: 64px; height: 64px; margin: 0 auto 8px; border-radius: 50%;
  display: grid; place-items: center; font-size: 26px; color: var(--accent-ink);
  background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--accent) 70%, #fff), var(--accent) 55%, color-mix(in srgb, var(--accent) 70%, #000));
  box-shadow: 0 4px 12px rgba(0,0,0,.25), inset 0 0 0 4px color-mix(in srgb, var(--accent) 80%, #000);
}
.modal-seal::after {
  content: ""; position: absolute; inset: -6px; border-radius: 50%; border: 2px solid var(--accent); opacity: 0;
}
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.modal-actions .btn { width: 100%; }
.modal.sealing .modal-seal { animation: stamp .7s cubic-bezier(.3, 0, .2, 1) forwards; }
.modal.sealing .modal-seal::after { animation: ring .7s ease-out .3s forwards; }
.modal.sealing .modal-actions { opacity: .5; transition: opacity .2s; }
@keyframes modal-in { from { opacity: 0; transform: translateY(16px) scale(.94); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes stamp {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.45) translateY(-8px) rotate(-10deg); }
  60%  { transform: scale(.88) rotate(4deg); }
  100% { transform: scale(1) rotate(0); }
}
@keyframes ring { 0% { opacity: .8; transform: scale(.9); } 100% { opacity: 0; transform: scale(1.8); } }

/* Account sections, reminders */
.section + .section { margin-top: 16px; }
.section h2 { margin-top: 0; }
.push { margin: 14px 0 6px; }
.push-status { margin: 0 0 12px; font-weight: 600; }
.push-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.reminder-time { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.reminder-time label { display: inline; }
select {
  font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px 12px; min-height: 44px;
}
.nudge { text-decoration: none; color: inherit; }
.nudge-icon { font-size: 1.5rem; width: 44px; text-align: center; flex: none; }
.nudge-go { color: var(--accent); font-size: 1.2rem; padding-right: 6px; }
.install span { display: block; }

/* Install banner */
.install {
  display: flex; align-items: center; gap: 12px; margin-top: 20px; padding: 12px 12px 12px 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow);
}
.install img { border-radius: 10px; flex: none; }
.install div { flex: 1; min-width: 0; font-size: .9rem; color: var(--muted); }
.install strong { display: block; color: var(--ink); font-size: .95rem; }
.btn.small { min-height: 40px; padding: 8px 16px; font-size: .9rem; }
.install .close { flex: none; width: 40px; height: 40px; border: 0; background: none; color: var(--muted); font-size: 1.5rem; cursor: pointer; border-radius: 50%; }

/* Installed app: no browser chrome, so keep content clear of the status bar and home indicator. */
@media (display-mode: standalone) {
  body { padding-top: max(12px, env(safe-area-inset-top)); user-select: none; -webkit-user-select: none; }
  .letter, textarea, input { user-select: text; -webkit-user-select: text; }
}

@media (prefers-reduced-motion: reduce) {
  .envelope .seal, .env-seal, .letter.unfold, .modal[open], .modal.sealing .modal-seal { animation: none; }
}
