/* =========================================================================
   konvolutt — styles
   aesthetic: vintage correspondence — kraft envelope, deep red wax,
              cream paper with faint ruling, refined serif typography.
   ========================================================================= */

:root {
  /* paper */
  --paper-light:  #fbf6e8;
  --paper-mid:    #f3ecd5;
  --paper-shade:  #e7decb;
  --paper-edge:   #d8cdb0;
  --rule:         rgba(120, 92, 40, 0.07);

  /* envelope (kraft) */
  --env-light:    #d9b283;
  --env-mid:      #c69664;
  --env-dark:     #a87844;
  --env-deep:     #80572b;
  --env-seam:     rgba(50, 30, 8, 0.18);

  /* wax */
  --wax-hi:       #d35353;
  --wax-mid:      #982e2e;
  --wax-deep:     #3a0808;
  --wax-text:     #f5d8a4;

  /* ink + accents */
  --ink:          #2b2520;
  --ink-soft:     #5d544a;
  --ink-faint:    #8a7e6d;
  --gold:         #b8954a;
  --burnt:        #5a3a18;

  /* zoom */
  --backdrop:     rgba(28, 18, 6, 0.86);
}

/* -------------------------- base ---------------------------------------- */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Cormorant Garamond", Georgia, serif;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  /* warm vintage backdrop */
  background:
    radial-gradient(ellipse at 50% 25%, #f4e6c5 0%, #e6d2a8 45%, #c8a974 100%);
  position: relative;
}

/* paper grain on body for richness */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(circle at 20% 30%, rgba(120,80,30,0.04) 0, transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(80,50,20,0.05) 0, transparent 40%);
  pointer-events: none;
  z-index: 0;
}

/* dark backdrop overlay used during zoom */
body::after {
  content: "";
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.6s ease;
  z-index: 50;
}
body.zoomed::after {
  opacity: 1;
  pointer-events: auto;
  cursor: zoom-out;
}

/* -------------------------- instruction --------------------------------- */

.instruction {
  position: fixed;
  top: 24px;
  left: 50%;
  transform: translateX(-50%);
  font-family: "Caveat", cursive;
  font-size: clamp(1.05rem, 1rem + 0.4vw, 1.4rem);
  color: var(--burnt);
  letter-spacing: 0.02em;
  text-align: center;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.45s ease, transform 0.45s ease;
  z-index: 60;
  margin: 0;
  text-shadow: 0 1px 0 rgba(255,250,235,0.4);
}
.instruction.hide {
  opacity: 0;
  transform: translate(-50%, -8px);
}
body.zoomed .instruction { opacity: 0; }

/* -------------------------- scene (auto-fit) ---------------------------- */

.scene {
  /* auto-fit to viewport: scale so the 460x820 stage fits with margin,
     up to 1.1x. min() gives the smallest of: width-fit, height-fit, cap. */
  --fit: min(
    calc((100vw - 24px) / 460),
    calc((100vh - 60px) / 820),
    1.1
  );
  position: relative;
  width: 460px;
  height: 820px;
  perspective: 1700px;
  perspective-origin: 50% 35%;
  transform: scale(var(--fit));
  z-index: 100;       /* sits on top of the body backdrop layer */
  overflow: visible;
}

/* ============================ ENVELOPE ================================== */

.envelope {
  position: absolute;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%);
  width: 400px;
  height: 260px;
  transform-style: preserve-3d;
}

/* back wall */
.env-back {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, var(--env-mid) 0%, var(--env-deep) 100%);
  border-radius: 5px;
  box-shadow:
    inset 0 0 36px rgba(0,0,0,0.22),
    inset 0 2px 0 rgba(255,235,200,0.12);
  z-index: 1;
  pointer-events: none;
  overflow: hidden;
}

/* faint diagonal grain inside the back wall (visible when flap is open) */
.env-back-pattern {
  position: absolute;
  inset: 0;
  background-image:
    repeating-linear-gradient(
      45deg,
      transparent 0,
      transparent 6px,
      rgba(255,235,200,0.025) 6px,
      rgba(255,235,200,0.025) 7px
    );
  opacity: 0.7;
}

/* front pocket with V notch */
.env-front {
  position: absolute;
  inset: 0;
  background:
    /* diagonal seams from corners to V tip — the real envelope folds */
    linear-gradient(135deg,
      transparent 49.5%, var(--env-seam) 49.7%, var(--env-seam) 50.1%, transparent 50.3%),
    linear-gradient(225deg,
      transparent 49.5%, var(--env-seam) 49.7%, var(--env-seam) 50.1%, transparent 50.3%),
    /* main paper colour */
    linear-gradient(180deg, var(--env-mid) 0%, var(--env-dark) 100%);
  clip-path: polygon(0 0, 50% 50%, 100% 0, 100% 100%, 0 100%);
  border-radius: 0 0 5px 5px;
  box-shadow:
    inset 0 -3px 10px rgba(0,0,0,0.18),
    inset 0 1px 0 rgba(255,235,200,0.10),
    0 8px 18px rgba(60, 38, 12, 0.28);
  z-index: 4;
  pointer-events: none;
}

/* the flap */
.env-flap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background:
    linear-gradient(180deg, var(--env-light) 0%, var(--env-mid) 78%, var(--env-dark) 100%);
  clip-path: polygon(0 0, 100% 0, 50% 50%);
  transform-origin: top center;
  transform: rotateX(0deg);
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(.4,0,.2,1);
  z-index: 5;
  cursor: grab;
  border-top: 1px solid rgba(255,240,210,0.30);
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
  touch-action: none;
}
.env-flap.grabbing { cursor: grabbing; }
.env-flap.open { z-index: 0; }

/* faint diagonal seams on the flap to match the body */
.env-flap::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(225deg,
      transparent 49.5%, var(--env-seam) 49.7%, var(--env-seam) 50.1%, transparent 50.3%),
    linear-gradient(135deg,
      transparent 49.5%, var(--env-seam) 49.7%, var(--env-seam) 50.1%, transparent 50.3%);
  pointer-events: none;
}

/* wax seal (SVG) */
.wax-seal {
  position: absolute;
  left: 50%;
  top: 38%;
  width: 58px;
  height: 58px;
  transform: translate(-50%, -50%);
  pointer-events: none;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.35));
}

.env-shadow {
  position: absolute;
  bottom: 38px;
  left: 50%;
  transform: translateX(-50%);
  width: 360px;
  height: 22px;
  background: radial-gradient(ellipse, rgba(0,0,0,0.32) 0%, transparent 70%);
  filter: blur(2.5px);
  z-index: 0;
  pointer-events: none;
}

/* ============================ LETTER ===================================== */
/* IMPORTANT (preserve-3d note):
   .letter-stage MUST NOT have transform-style: preserve-3d, or the
   panels' translateZ values would propagate up into the envelope's 3D
   space and render in front of env-front. By keeping the stage as a
   flat 2D layer, the panels still fold in 3D inside the inner .letter
   element, but the result composites as a flat layer that respects
   z-index against env-front. */

.letter-stage {
  position: absolute;
  top: -170px;            /* puts middle panel near envelope's vertical centre */
  left: 50%;
  transform: translate(-50%, 0);
  width: 320px;
  height: 600px;          /* 3 panels × 200 */
  z-index: 3;
  pointer-events: none;
  transition: transform 0.7s cubic-bezier(.22,.9,.3,1.05);
  touch-action: none;
}
.letter-stage.available { pointer-events: auto; }
.letter-stage.out {
  transform: translate(-50%, -340px);
  z-index: 6;
}

.letter {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  cursor: grab;
  /* zoom transition lives here — set by JS inline transform when zoomed */
  transition: transform 0.65s cubic-bezier(.22,.9,.3,1.12);
}
.letter.grabbing { cursor: grabbing; }
.letter.unfolded { cursor: zoom-in; }    /* hint that it can be enlarged */

/* panel (1/3 of unfolded letter) */
.panel {
  position: absolute;
  left: 0;
  width: 100%;
  height: 200px;
  transform-style: preserve-3d;
  transition: transform 0.7s cubic-bezier(.55,.05,.25,1);
}

/* face shared styles */
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background:
    /* faint horizontal ruling for that "real letter paper" look */
    repeating-linear-gradient(
      0deg,
      transparent 0,
      transparent 14px,
      var(--rule) 14px,
      var(--rule) 15px
    ),
    /* corner warm-tone */
    radial-gradient(ellipse at 0% 0%, #f7eed1 0%, transparent 60%),
    radial-gradient(ellipse at 100% 100%, #f0e6c8 0%, transparent 60%),
    linear-gradient(180deg, var(--paper-light) 0%, var(--paper-mid) 100%);
  border: 1px solid var(--paper-edge);
  box-shadow:
    inset 0 0 26px rgba(120,90,40,0.06),
    0 1px 2px rgba(0,0,0,0.07);
  padding: 14px 18px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.face-back {
  transform: rotateX(180deg);
  background:
    linear-gradient(180deg, var(--paper-mid) 0%, var(--paper-shade) 100%);
}
.crease-mark {
  position: absolute;
  left: 8%;
  right: 8%;
  top: 50%;
  height: 1px;
  background:
    linear-gradient(90deg, transparent, rgba(120,90,40,0.20), transparent);
}

/* ----- panel positions (unfolded layout) ----- */
.panel-top    { top:   0px; transform-origin: bottom center; }
.panel-middle { top: 200px; }
.panel-bottom { top: 400px; transform-origin: top    center; }

/* ----- fold states ----- */
.letter[data-step="0"] .panel-top    { transform: translateZ(2px) rotateX(-180deg); }
.letter[data-step="0"] .panel-bottom { transform: translateZ(1px) rotateX( 180deg); }
.letter[data-step="1"] .panel-top    { transform: translateZ(2px) rotateX(-180deg); }
.letter[data-step="1"] .panel-bottom { transform: translateZ(0)   rotateX(   0deg); }
.letter[data-step="2"] .panel-top    { transform: translateZ(0)   rotateX(   0deg); }
.letter[data-step="2"] .panel-bottom { transform: translateZ(0)   rotateX(   0deg); }

/* ============================ LETTER TYPOGRAPHY ========================== */

.letter-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 9px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin-bottom: 4px;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(120,90,40,0.10);
}
.letter-monogram {
  color: var(--gold);
  font-weight: 600;
  font-style: italic;
  letter-spacing: 0.25em;
  text-transform: none;
  font-size: 11px;
}
.letter-date {
  font-family: "Caveat", cursive;
  font-size: 13px;
  font-style: normal;
  letter-spacing: 0;
  text-transform: none;
  color: var(--burnt);
}

.letter-greeting {
  font-family: "Cormorant Garamond", serif;
  font-size: 22px;
  font-weight: 500;
  font-style: italic;
  color: var(--ink);
  margin: 4px 0 6px;
  line-height: 1;
  letter-spacing: 0.005em;
}

.letter-text {
  font-family: "Cormorant Garamond", serif;
  font-size: 11px;
  line-height: 1.42;
  color: var(--ink);
  margin: 0 0 6px;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
}
.letter-text:last-of-type { margin-bottom: 0; }

.letter-ornament {
  text-align: center;
  font-size: 14px;
  color: var(--gold);
  margin: 4px 0;
  line-height: 1;
  opacity: 0.85;
}

.letter-signoff {
  font-family: "Caveat", cursive;
  font-size: 16px;
  color: var(--burnt);
  margin: 8px 0 6px;
  line-height: 1.15;
}
.letter-sig {
  display: inline-block;
  font-size: 22px;
  font-weight: 600;
  margin-left: 8px;
  color: var(--ink);
}

.letter-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  margin-top: auto;
  padding-top: 4px;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  font-family: "Cormorant Garamond", serif;
  font-size: 10px;
  font-style: italic;
  letter-spacing: 0.05em;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--paper-edge);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s, color 0.2s;
  pointer-events: none;          /* turned on once fully unfolded */
  white-space: nowrap;
}
.action-btn:hover {
  background: rgba(184, 149, 74, 0.10);
  border-color: var(--gold);
}
.action-btn-primary {
  background: var(--burnt);
  border-color: var(--burnt);
  color: var(--paper-light);
}
.action-btn-primary:hover {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper-light);
}
.action-btn:disabled {
  opacity: 0.55;
  cursor: default;
}
.letter[data-step="2"] .action-btn { pointer-events: auto; }

/* ============================ ZOOM ======================================= */

.zoom-close {
  position: fixed;
  top: 22px;
  right: 22px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.20);
  color: rgba(255,255,255,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  z-index: 200;
  transition: opacity 0.4s ease, background 0.2s, transform 0.2s;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.zoom-close:hover {
  background: rgba(255,255,255,0.16);
  transform: scale(1.05);
}
body.zoomed .zoom-close {
  opacity: 1;
  pointer-events: auto;
}

/* While zoomed, fade the envelope chrome away so the letter can stand
   alone against the dark backdrop. The letter itself is left visible. */
body.zoomed .env-back,
body.zoomed .env-front,
body.zoomed .env-flap,
body.zoomed .env-shadow {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
}

/* When zoomed, give the letter a pronounced shadow + raise it visually */
body.zoomed .letter {
  box-shadow: 0 24px 60px rgba(0,0,0,0.55);
}

/* hide any visible hints while zoomed */
body.zoomed .hint { opacity: 0; pointer-events: none; }

/* ============================ HINTS ====================================== */
/* Hints are positioned fixed (set by JS to track elements). They live above
   the scene chrome (z-index 70) but below the zoom backdrop (50/200). The
   visual language: sepia ink, dashed lines, soft caveat label — matching
   the vintage aesthetic instead of tech-ui blue. */

.hint {
  position: fixed;
  display: none;             /* shown via .visible */
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  color: var(--burnt);
  z-index: 70;
  filter: drop-shadow(0 1px 2px rgba(255, 248, 230, 0.5));
  /* gentle entrance */
  opacity: 0;
  transition: opacity 0.4s ease;
}
.hint.visible {
  display: flex;
  opacity: 0.92;
}
.hint.fading { opacity: 0; }

.hint-label {
  font-family: "Caveat", cursive;
  font-size: 18px;
  letter-spacing: 0.02em;
  margin-top: -4px;
  white-space: nowrap;
  color: var(--burnt);
  text-shadow: 0 1px 0 rgba(255, 250, 235, 0.4);
}

/* ----- arrow-up hints (flap, letter) ----- */
.hint-arrow-up svg {
  /* the whole arrow drifts up & down to suggest the gesture */
  animation: hint-bob 1.6s ease-in-out infinite;
}
.hint-arrow-up .hint-dot {
  /* pulsing origin dot */
  animation: hint-pulse 1.6s ease-in-out infinite;
  transform-origin: 30px 90px;
}

@keyframes hint-bob {
  0%, 100% { transform: translateY(6px); }
  50%      { transform: translateY(-6px); }
}
@keyframes hint-pulse {
  0%, 100% { opacity: 1;   r: 4; }
  50%      { opacity: 0.4; r: 6; }
}

/* ----- tap hint (unfolding) ----- */
.hint-tap {
  /* tap hint is centred on its target, so we offset upward for the label */
}
.hint-tap-rings {
  position: relative;
  width: 60px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hint-tap-rings::before {
  /* central solid dot */
  content: "";
  position: absolute;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--burnt);
  box-shadow: 0 0 0 3px rgba(255, 250, 235, 0.4);
  z-index: 2;
}
.hint-tap-rings span {
  position: absolute;
  width: 60px; height: 60px;
  border-radius: 50%;
  border: 2px solid var(--burnt);
  opacity: 0;
  animation: hint-ripple 1.8s ease-out infinite;
}
.hint-tap-rings span:nth-child(2) { animation-delay: 0.6s; }
.hint-tap-rings span:nth-child(3) { animation-delay: 1.2s; }

@keyframes hint-ripple {
  0%   { transform: scale(0.3); opacity: 0.7; }
  100% { transform: scale(1.4); opacity: 0;   }
}

/* ============================ MOBILE TWEAKS ============================== */

@media (max-width: 480px) {
  .instruction { top: 14px; font-size: 1.1rem; }
  .zoom-close { top: 14px; right: 14px; }
  /* slightly bigger buttons on touch screens for comfortable tap targets */
  .action-btn { padding: 6px 12px; font-size: 11px; }
}

/* very small heights — push the scene up a bit so the instruction has room */
@media (max-height: 600px) {
  .scene { perspective-origin: 50% 25%; }
}