:root {
  --night: #0D1830;
  --night-2: #15254A;
  --paper: #15254A;
  --paper-deep: #1E3363;
  --ink: #F6EEDC;
  --ink-soft: #B7C2DC;
  --gold: #EBC66E;
  --royal: #3A63D8;
  --sky: #8FC6F2;
  --peri: #AEB8F7;
  --deep: #22408A;
  --ice: #DCEBFA;
  --locked-bg: #12213F;
  --locked-ink: #93A1C4;
  --gap: 10px;
  --pad: 18px;
  --radius: 20px;
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --cell: calc((min(100vw, 520px) - 2 * var(--pad) - 2 * var(--gap)) / 3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; background: var(--night); }
body {
  margin: 0;
  min-height: 100dvh;
  background:
    linear-gradient(180deg, #172B58 0%, var(--night) 55%) no-repeat,
    var(--night);
  color: var(--ink);
  font: 500 17px/1.55 "Nunito", ui-rounded, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
::selection { background: var(--gold); color: var(--night); }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; border-radius: 12px; }
html.locked { overflow: hidden; }

.page {
  max-width: 520px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 40px) var(--pad) calc(env(safe-area-inset-bottom) + 96px);
}

/* Header */
.hero { margin: 0 4px 28px; }
.hero h1 {
  margin: 0;
  font-family: "Gluten", "Nunito", system-ui, sans-serif;
  font-weight: 750;
  font-size: clamp(2.6rem, 12vw, 3.6rem);
  line-height: .98;
  letter-spacing: -0.025em;
}
.hero em { font-style: normal; color: var(--gold); }
.sub { margin: 14px 0 0; color: var(--ink-soft); font-size: 1.02rem; }

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: var(--cell);
  grid-auto-flow: dense;
  gap: var(--gap);
}
.tile {
  --bg: var(--sky);
  --fg: var(--night);
  position: relative;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--fg);
  background: color-mix(in srgb, var(--bg) 70%, #000 30%);
  border-radius: var(--radius);
  perspective: 700px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
.tile.big { grid-column: span 2; grid-row: span 2; }
.tile.wide { grid-column: span 2; }
.tile.tall { grid-row: span 2; }
.tone-0 { --bg: var(--royal); --fg: var(--ink); }
.tone-1 { --bg: var(--sky); }
.tone-2 { --bg: var(--peri); }
.tone-3 { --bg: var(--deep); --fg: var(--ink); }
.tone-4 { --bg: var(--ice); }

.door {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: inherit;
  background: var(--bg);
  box-shadow: 0 6px 14px -8px rgba(0, 0, 0, .55), inset 0 -3px 0 rgba(0, 0, 0, .08);
  transform-origin: 0 50%;
  transition: transform .7s var(--ease-out), box-shadow .2s;
  backface-visibility: hidden;
}
.tile:active .door { transform: scale(.97); }
.num {
  font-family: "Gluten", "Nunito", system-ui, sans-serif;
  font-weight: 700;
  font-size: calc(var(--cell) * .42);
  line-height: 1;
  font-variant-numeric: lining-nums;
}
.big .num { font-size: calc(var(--cell) * .9); }
.tile svg { width: 18px; height: 18px; }
.door .mark { position: absolute; bottom: 10px; right: 11px; opacity: .8; }

/* Locked */
.tile.locked { cursor: default; background: transparent; }
.tile.locked .door {
  background: var(--locked-bg);
  color: var(--locked-ink);
  box-shadow: none;
  outline: 1.5px dashed rgba(235, 198, 110, .38);
  outline-offset: -1.5px;
}
.tile.locked:active .door { transform: none; }

/* Today: a small heartbeat to say "this one" */
.tile.today .door { animation: nudge 3.2s var(--ease-out) infinite; }
.tile.today::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: calc(var(--radius) + 4px);
  border: 2px solid var(--gold);
  pointer-events: none;
}
@keyframes nudge {
  0%, 70%, 100% { transform: rotate(0); }
  76% { transform: rotate(-2.5deg) scale(1.02); }
  84% { transform: rotate(2deg) scale(1.02); }
  90% { transform: rotate(0) scale(1); }
}

/* Opening */
.tile.opening .door { transform: rotateY(-105deg); box-shadow: none; animation: none; }

/* Opened: the photo peeks through */
.tile.opened { background: var(--bg); overflow: hidden; }
.tile.opened .door {
  background: var(--thumb, none) center / cover, var(--bg);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, .55);
}
.tile.opened.has-thumb .num {
  color: #FFF8EE;
  text-shadow: 0 2px 10px rgba(5, 10, 30, .7);
}

/* Sheet */
.sheet {
  width: 100%;
  max-width: 520px;
  max-height: calc(100dvh - env(safe-area-inset-top) - 24px);
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 28px 28px 0 0;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 -12px 40px -12px rgba(0, 0, 0, .6);
  overscroll-behavior: contain;
}
.sheet[open] { animation: rise .55s var(--ease-out); }
.sheet::backdrop { background: rgba(5, 10, 25, .6); animation: fade .4s ease-out; }
@keyframes rise { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
.sheet-inner { padding: 20px 20px calc(env(safe-area-inset-bottom) + 28px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.sheet-date {
  margin: 0;
  font-family: "Gluten", "Nunito", system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.6rem;
  letter-spacing: -0.015em;
}
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--paper-deep);
  color: var(--ink);
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }

.photo {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 18px;
  background: var(--paper-deep);
  box-shadow: 0 10px 24px -14px rgba(0, 0, 0, .7);
}
.photo.fallback { aspect-ratio: 4 / 3; object-fit: contain; padding: 18%; }
.msg {
  margin: 18px 2px 0;
  font-size: 1.15rem;
  line-height: 1.6;
  white-space: pre-line;
  max-width: 34ch;
  text-wrap: pretty;
}
.actions:empty { display: none; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 22px; }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  font: 800 1rem/1 "Nunito", system-ui, sans-serif;
  text-decoration: none;
  cursor: pointer;
  background: var(--paper-deep);
  color: var(--ink);
}
.btn.primary { background: var(--gold); color: var(--night); box-shadow: 0 6px 16px -8px rgba(0, 0, 0, .7); }
.btn svg { width: 20px; height: 20px; }
.btn:disabled { opacity: .6; cursor: progress; }

.foot { display: grid; place-items: center; margin-top: 36px; color: var(--gold); }
.foot-heart { width: 26px; height: 26px; }

/* Preview bar (creator only, ?date=...) */
.preview {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: calc(100vw - 24px);
  padding: 6px 6px 6px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--night);
  font-size: .8rem;
  font-weight: 700;
  box-shadow: 0 10px 24px -10px rgba(0, 0, 0, .7);
  z-index: 10;
}
.preview[hidden] { display: none; }
.preview-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview input {
  font: 700 16px/1 "Nunito", system-ui, sans-serif; /* 16px stops iOS zooming */
  border: 0;
  border-radius: 999px;
  padding: 6px 8px;
  background: var(--night);
  color: var(--ink);
  color-scheme: dark;
}
.preview button {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: rgba(13, 24, 48, .1);
  color: inherit;
  font: 800 1.2rem/1 "Nunito", system-ui, sans-serif;
  cursor: pointer;
}
.preview svg { width: 16px; height: 16px; }
@media (max-width: 380px) { .preview-label { display: none; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
