/* ============================================================
   Two palettes. Pick one with `theme` in content.js
   ("morning" or "frost"), or try one with ?theme=frost
   ============================================================ */

/* Christmas Morning: warm greys, brass and oak on a Baltic teal tree */
:root,
[data-theme="morning"] {
  --bg: #ebe6dc;
  --bg-glow: #f8f4ec;
  --bg-low: #d9d2c4;
  --text: #303226;
  --muted: rgba(48, 50, 38, .62);
  --accent: #7a6951;

  --tree-a: #1c6a7f;
  --tree-b: #145467;
  --trunk: #5f4e35;
  --light: #f6dca2;
  --snow: rgba(149, 137, 120, .45);

  --d0-bg: #aba69b; --d0-fg: #303226;
  --d1-bg: #958978; --d1-fg: #f6f1e8;
  --d2-bg: #7a6951; --d2-fg: #f6f1e8;
  --d3-bg: #f3eee4; --d3-fg: #7a6951;
  --star-a: #f1dfb2; --star-b: #a88c58; --star-fg: #303226;
  --star-glow: rgba(214, 170, 80, .75);

  --hole: #0d3341;
  --paper: #fbf8f2;
  --ink: #303226;
  --ink-accent: #145467;
  --rule: rgba(20, 84, 103, .16);
  --backdrop: rgba(48, 50, 38, .72);
  --toast-bg: #303226; --toast-fg: #f3eee4;
  --ph-a: #145467; --ph-b: #7a6951; --ph-fg: #f3eee4;
}

/* Festive White: frosty silver greys on a dark winter night */
[data-theme="frost"] {
  --bg: #272a28;
  --bg-glow: #3b3f3d;
  --bg-low: #1d1f1e;
  --text: #ced4db;
  --muted: rgba(206, 212, 219, .66);
  --accent: #adb6be;

  --tree-a: #6b787c;
  --tree-b: #444643;
  --trunk: #1b1d1c;
  --light: #ffffff;
  --snow: rgba(255, 255, 255, .8);

  --d0-bg: #ced4db; --d0-fg: #272a28;
  --d1-bg: #adb6be; --d1-fg: #272a28;
  --d2-bg: #7b8a92; --d2-fg: #f2f4f6;
  --d3-bg: #f2f4f6; --d3-fg: #5b6567;
  --star-a: #ffffff; --star-b: #adb6be; --star-fg: #272a28;
  --star-glow: rgba(255, 255, 255, .85);

  --hole: #1a1c1b;
  --paper: #f4f6f8;
  --ink: #272a28;
  --ink-accent: #5b6567;
  --rule: rgba(91, 101, 103, .2);
  --backdrop: rgba(18, 20, 19, .8);
  --toast-bg: #f2f4f6; --toast-fg: #272a28;
  --ph-a: #5b6567; --ph-b: #272a28; --ph-fg: #ced4db;
}

:root {
  --serif: "Fraunces", Georgia, "Times New Roman", serif;
  --hand: "Caveat", "Segoe Print", "Bradley Hand", cursive;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;

  /* window size: fits 6 windows + branches on a 375px phone */
  --g: 5px;
  --s: clamp(40px, calc((100vw - 32px - 25px) / 6.9), 60px);
  --pad: calc(var(--s) * .45);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(90% 55% at 50% 38%, var(--bg-glow) 0%, transparent 70%),
    linear-gradient(to bottom, var(--bg) 60%, var(--bg-low));
  background-color: var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--sans);
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

#snow {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.page {
  position: relative;
  z-index: 1;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(26px + env(safe-area-inset-top, 0px)) 16px calc(28px + env(safe-area-inset-bottom, 0px));
}

/* ---------- header ---------- */
.head { text-align: center; margin-bottom: 14px; }
.eyebrow {
  margin: 0 0 6px;
  font-size: 12px;
  letter-spacing: .22em;
  text-transform: uppercase;
  color: var(--accent);
}
h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(28px, 8vw, 40px);
  line-height: 1.1;
}
.status {
  margin: 8px 0 0;
  font-size: 15px;
  color: var(--muted);
  min-height: 1.4em;
}

/* ---------- the tree ---------- */
.tree {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.tier {
  --tw: calc(((var(--n) - 1) * var(--s) + (var(--n) - 2) * var(--g)) / 2 + var(--s) * .18);
  position: relative;
  display: flex;
  justify-content: center;
  gap: var(--g);
  padding: calc(var(--s) * .4) var(--pad) calc(var(--s) * .32);
  margin-top: calc(var(--s) * -.2);
}
.tier::before {               /* the branches */
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(to bottom, var(--tree-a), var(--tree-b));
  clip-path: polygon(calc(50% - var(--tw)) 0, calc(50% + var(--tw)) 0, 100% 100%, 0 100%);
}
.tier::after {                /* string of lights along each tier */
  content: "";
  position: absolute;
  left: calc(var(--pad) * .5);
  right: calc(var(--pad) * .5);
  bottom: 3px;
  height: 7px;
  background: radial-gradient(circle, var(--light) 0 2px, transparent 2.6px) center / 15px 7px repeat-x;
  filter: drop-shadow(0 0 3px var(--light));
  animation: twinkle 2.6s ease-in-out infinite alternate;
  pointer-events: none;
}
.tier:nth-child(odd)::after { animation-delay: -1.3s; }
@keyframes twinkle { from { opacity: .45; } to { opacity: 1; } }

.trunk {
  position: relative;
  display: flex;
  gap: var(--g);
  padding: calc(var(--s) * .22) calc(var(--s) * .22) calc(var(--s) * .26);
  background: var(--trunk);
  border-radius: 0 0 8px 8px;
}

.star-wrap {
  position: relative;
  z-index: 20;
  margin-bottom: calc(var(--s) * -.42);
  filter: drop-shadow(0 0 10px var(--star-glow));
}

/* ---------- a window ---------- */
.cell {
  position: relative;
  flex: none;
  width: var(--s);
  height: var(--s);
  perspective: 600px;
  border-radius: 8px;
}

.inside {
  position: absolute;
  inset: 0;
  padding: 0;
  border: 0;
  border-radius: inherit;
  overflow: hidden;
  background: var(--hole);
  box-shadow: inset 0 2px 8px rgba(0,0,0,.6);
  cursor: pointer;
}
.inside img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0;
  transform: scale(1.08);
  transition: opacity .6s ease .25s, transform 1.2s ease .25s;
}
.cell.open .inside img { opacity: 1; transform: scale(1); }
.cell:not(.open) .inside { pointer-events: none; }

.door {
  position: absolute;
  inset: 0;
  padding: 0;
  border: 0;
  border-radius: inherit;
  cursor: pointer;
  transform-origin: left center;
  transition: transform .9s cubic-bezier(.3, .7, .2, 1), opacity .5s ease;
  display: grid;
  place-items: center;
  font-family: var(--serif);
  box-shadow: 0 2px 0 rgba(0,0,0,.18), 0 4px 10px rgba(0,0,0,.22);
  z-index: 2;
}
.door::before {               /* stitched inner frame */
  content: "";
  position: absolute;
  inset: 4px;
  border-radius: 5px;
  border: 1px dashed currentColor;
  opacity: .35;
}
.door .num {
  font-size: calc(var(--s) * .44);
  font-weight: 600;
  line-height: 1;
  transition: opacity .3s ease;
}
.door .knob {
  position: absolute;
  right: 7px;
  top: 50%;
  width: 4px;
  height: 4px;
  margin-top: -2px;
  border-radius: 50%;
  background: currentColor;
  opacity: .55;
}

.tone-0 .door { background: var(--d0-bg); color: var(--d0-fg); }
.tone-1 .door { background: var(--d1-bg); color: var(--d1-fg); }
.tone-2 .door { background: var(--d2-bg); color: var(--d2-fg); }
.tone-3 .door { background: var(--d3-bg); color: var(--d3-fg); }

.cell.open .door {
  transform: rotateY(-102deg);
  box-shadow: none;
  pointer-events: none;
  z-index: 3;
}
.cell.open .door .num,
.cell.open .door .knob { opacity: 0; }

/* the star: day 24 */
.cell.star {
  width: calc(var(--s) * 1.75);
  height: calc(var(--s) * 1.7);
  border-radius: 0;
  clip-path: polygon(50% 0%, 61.8% 35%, 98% 35%, 68.5% 57%, 79.5% 92%, 50% 71%, 20.5% 92%, 31.5% 57%, 2% 35%, 38.2% 35%);
}
.cell.star .door {
  background: radial-gradient(circle at 50% 40%, var(--star-a), var(--star-b));
  color: var(--star-fg);
  box-shadow: none;
  transform-origin: center;
}
.cell.star .door::before,
.cell.star .door .knob { display: none; }
.cell.star .door .num { font-size: calc(var(--s) * .52); margin-top: calc(var(--s) * .12); }
.cell.star.open .door { transform: scale(.3) rotate(144deg); opacity: 0; }
.cell.star .inside { box-shadow: none; }

.cell.instant .door,
.cell.instant .inside img { transition: none; }

/* a window ready to open */
.cell.ready .door { animation: glow 2.4s ease-in-out infinite; }
@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 0 transparent, 0 4px 10px rgba(0,0,0,.22); }
  50%      { box-shadow: 0 0 0 3px var(--light), 0 0 18px var(--light); }
}
.cell.star.ready .door { animation: none; }
.star-wrap:has(.ready) { animation: starglow 2.4s ease-in-out infinite; }
@keyframes starglow {
  50% { filter: drop-shadow(0 0 22px var(--star-glow)) drop-shadow(0 0 6px var(--star-glow)); }
}

.cell.locked .door { filter: saturate(.8) brightness(.94); }
.cell.shake .door { animation: shake .45s ease; }
.cell.star.shake .door { animation: wobble .45s ease; }
@keyframes shake {
  0%, 100% { transform: rotateY(0); }
  25% { transform: rotateY(-16deg); }
  50% { transform: rotateY(-4deg); }
  75% { transform: rotateY(-10deg); }
}
@keyframes wobble {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-8deg); }
  75% { transform: rotate(8deg); }
}

.foot {
  margin: 16px 0 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

/* ---------- viewer ---------- */
.viewer {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
}
.viewer[hidden] { display: none; }
.backdrop {
  position: absolute;
  inset: 0;
  background: var(--backdrop);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade .35s ease;
}
.stage {
  position: relative;
  perspective: 1400px;
  animation: rise .5s cubic-bezier(.2, .8, .2, 1);
}
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(24px) scale(.96); } }

/* fixed letter shape (about 3:4.3) so a handwritten page always fits */
.card {
  --cw: min(86vw, 420px, calc((100vh - 120px) * .7));
  --cw: min(86vw, 420px, calc((100dvh - 120px) * .7));
  position: relative;
  display: block;
  width: var(--cw);
  height: calc(var(--cw) / .7);
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform .8s cubic-bezier(.4, .2, .2, 1);
  font: inherit;
  color: inherit;
  text-align: left;
}
.card.flipped { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  display: block;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  border-radius: 6px;
  box-shadow: 0 24px 60px rgba(0,0,0,.45);
}
.back { transform: rotateY(180deg); }

.polaroid {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--paper);
  padding: 12px 12px 0;
  border-radius: 6px;
}
.polaroid img {
  display: block;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  object-fit: contain;
  background: var(--bg-low);
  border-radius: 2px;
}
.caption {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 12px 4px 14px;
  color: var(--ink);
}
.cap-date { font-family: var(--hand); font-size: 26px; font-weight: 600; }
.cap-hint {
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  opacity: .5;
}

.letter {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 26px 26px 22px;
  border-radius: 6px;
  color: var(--ink);
  background: var(--paper);
}
.letter-date {
  font-size: 12px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-accent);
  margin-bottom: 10px;
}
.letter-msg {
  font-family: var(--hand);
  font-size: 25px;
  line-height: 34px;
  white-space: pre-wrap;
  flex: 1 0 auto;
  background: repeating-linear-gradient(to bottom, transparent 0 29px, var(--rule) 29px 30px, transparent 30px 34px);
}
.letter-msg.empty { opacity: .45; }
.letter-msg[hidden] { display: none; }

/* handwritten letter: transparent ink on the same 16-line paper as the pad */
.letter.hand { padding: 16px 14px 14px; overflow: hidden; }
.letter.hand .letter-date { margin: 0 4px 8px; }
.letter-hand {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 4;
  background: linear-gradient(to bottom, transparent calc(100% - 1px), var(--rule) calc(100% - 1px)) 0 0 / 100% 6.25%;
}
.letter-hand[hidden] { display: none; }
.letter-hand img { display: block; width: 100%; height: 100%; }
.letter-sig {
  font-family: var(--hand);
  font-size: 28px;
  font-weight: 600;
  text-align: right;
  color: var(--ink-accent);
  margin-top: 12px;
}

.close {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top, 0px));
  right: 12px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: rgba(255,255,255,.16);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  z-index: 40;
  width: max-content;
  max-width: calc(100vw - 32px);
  text-align: center;
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--toast-bg);
  color: var(--toast-fg);
  font-size: 15px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3);
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease, transform .3s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

@media (prefers-reduced-motion: reduce) {
  .door, .card, .inside img { transition-duration: .01s !important; }
  .cell.ready .door, .cell.shake .door, .stage, .backdrop,
  .tier::after, .star-wrap { animation: none !important; }
}
