:root {
  --bg: #f5efe3; --card: #fffdf8; --ink: #1e2a44; --muted: #776f63; --line: #e6dccb;
  --seal: #9e2b25; --seal-ink: #fff; --accent: #1e2a44; --accent-ink: #fff; --soft: #ece3d2; --danger: #9e2b25;
  --paper: #fffdf8; --rule: #eadfcb;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16140f; --card: #211e18; --ink: #efe7d8; --muted: #a79d8c; --line: #3a342a; --accent: #e9dfcc; --accent-ink: #16140f;
    --soft: #2b261e; --paper: #221f19; --rule: #332d24; --seal: #c0443b; }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--sans); font-size: 17px; -webkit-text-size-adjust: 100%; }
a { color: var(--ink); }
h1 { font-family: var(--serif); font-size: 2rem; line-height: 1.15; margin: 8px 0 12px; letter-spacing: -.01em; }
h2 { font-family: var(--serif); font-size: 1.25rem; margin: 28px 0 10px; }
h3 { margin: 0 0 8px; font-size: 1rem; }
p { line-height: 1.55; }
.page { max-width: 600px; margin: 0 auto; padding: calc(var(--sat) + 8px) 16px calc(var(--sab) + 40px); }
.page.wide { max-width: 1000px; }
.center { text-align: center; } .right { text-align: right; }
.muted { color: var(--muted); } .small { font-size: .88rem; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 0 14px; }
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; font-family: var(--serif); font-weight: 700; font-size: 1.25rem; color: var(--ink); }
.brand .seal { width: 28px; height: 28px; color: var(--seal); }
.topbar .small { text-decoration: none; color: var(--muted); font-weight: 500; }
.hero { padding: 18px 0 8px; }
.hero h1 { font-size: 2.5rem; }
.lead { font-size: 1.12rem; color: var(--ink); }
.btn { display: block; width: 100%; margin: 12px 0 0; padding: 15px 18px; border-radius: 14px; border: 0; background: var(--accent); color: var(--accent-ink); font: 600 17px var(--sans); text-align: center; text-decoration: none; cursor: pointer; }
.btn[disabled] { opacity: .4; }
.btn.secondary { background: var(--soft); color: var(--ink); }
.btn.ghost { background: transparent; color: var(--ink); }
.btn.danger { color: var(--danger); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; margin: 14px 0; display: block; text-decoration: none; color: inherit; }
label { display: block; font-weight: 600; margin: 12px 0 6px; }
label .hint { display: block; font-weight: 400; color: var(--muted); font-size: .88rem; margin-top: 2px; }
input, textarea { width: 100%; font: 17px var(--sans); color: var(--ink); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
input:focus, textarea:focus { outline: 2px solid var(--seal); outline-offset: 1px; }
.err { color: var(--danger); font-size: .92rem; min-height: 1.2em; margin-top: 8px; }
.foot { text-align: center; color: var(--muted); font-size: .88rem; margin-top: 30px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.chip { border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 999px; padding: 9px 14px; font: 500 15px var(--sans); cursor: pointer; text-decoration: none; }
.chip.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.pill { display: inline-block; background: var(--seal); color: #fff; border-radius: 999px; padding: 1px 8px; font-size: .75rem; font-weight: 700; vertical-align: 1px; }
.how div { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; line-height: 1.45; }
.how b { flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--seal); color: #fff; display: flex; align-items: center; justify-content: center; font-size: .9rem; }
.spinner { width: 28px; height: 28px; margin: 80px auto; border: 3px solid var(--line); border-top-color: var(--seal); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* writing paper */
.env { font-weight: 500; }
.env span { font-family: var(--serif); font-size: .95rem; color: var(--muted); } .env em { font-style: normal; font-size: .85rem; }
.env input { font-family: var(--serif); font-style: italic; }
.paper { background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 22px 20px 26px; margin: 14px 0 6px; box-shadow: 0 1px 0 var(--line), 0 12px 30px -18px rgba(30,42,68,.35);
  background-image: repeating-linear-gradient(transparent 0 33px, var(--rule) 33px 34px); background-position: 0 58px; }
.paper .date { font-family: var(--serif); color: var(--muted); text-align: right; font-size: .95rem; margin-bottom: 14px; }
.paper textarea { border: 0; background: transparent; padding: 0; min-height: 320px; resize: none; font: 19px/34px var(--serif); outline: none; overflow: hidden; }
.letter-body { font: 19px/34px var(--serif); white-space: pre-wrap; overflow-wrap: anywhere; }
.reading .env-line { font-family: var(--serif); font-style: italic; color: var(--muted); }
input[type="date"] { font-family: var(--sans); max-width: 260px; }

/* envelopes */
.envelope { position: relative; background: #efe4cf; border-radius: 6px; box-shadow: 0 10px 24px -14px rgba(30,42,68,.5); overflow: hidden; }
.envelope .flap { position: absolute; left: 0; right: 0; top: 0; height: 58%; background: #e6d8bd; clip-path: polygon(0 0, 100% 0, 50% 100%); }
.envelope .wax { position: absolute; left: 50%; top: 58%; transform: translate(-50%, -50%); color: var(--seal); filter: drop-shadow(0 2px 2px rgba(0,0,0,.25)); }
.envelope.big { width: 260px; height: 170px; margin: 24px auto 20px; animation: seal-in .7s cubic-bezier(.2,.8,.3,1.2) both; }
.envelope.big .wax .seal { width: 56px; height: 56px; }
.envelope.big .env-note { position: absolute; left: 14px; right: 14px; bottom: 12px; font: italic 14px var(--serif); color: #5c5347; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.envelope.sm { width: 64px; height: 44px; flex: none; }
.envelope.sm .wax .seal { width: 20px; height: 20px; }
.envelope.open .flap { transform: scaleY(-1); transform-origin: top; opacity: .6; }
@keyframes seal-in { from { transform: translateY(24px) rotate(-4deg); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .envelope.big { animation: none; } }
.list { display: flex; flex-direction: column; gap: 10px; }
.envelope-row { display: flex; gap: 14px; align-items: center; text-decoration: none; color: inherit; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; }
.envelope-row .t, .read-row .t { font-family: var(--serif); font-weight: 600; }
.envelope-row .s, .read-row .s, .arrived .s { color: var(--muted); font-size: .86rem; margin-top: 2px; }
.grow { flex: 1; min-width: 0; }
.read-row { display: flex; align-items: center; gap: 10px; padding: 12px 2px; text-decoration: none; color: inherit; border-bottom: 1px solid var(--line); }
.read-row:last-child { border-bottom: 0; }
.arrived { display: flex; gap: 14px; align-items: center; border-color: var(--seal); }

/* sheet + toast */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 560px; background: var(--card); border-radius: 20px 20px 0 0; padding: 14px 16px calc(var(--sab) + 16px); }
.sheet .h { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 12px; }
#toast { position: fixed; left: 50%; bottom: calc(var(--sab) + 24px); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-size: .95rem; z-index: 50; max-width: 90vw; text-align: center; }

/* admin */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.stats div { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; }
.stats b { display: block; font: 700 1.6rem var(--serif); } .stats span { color: var(--muted); font-size: .85rem; }
.chart { margin: 8px 0 16px; } .chart svg { width: 100%; height: 80px; display: block; margin-top: 6px; } .chart rect { fill: var(--seal); }
.tbl { overflow-x: auto; } table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; }
