/* Message Section - Letter Style */
.message-section {
  max-width: max(55vw, 650px);
  width: 100%;
  aspect-ratio: 210 / 297; /* DIN A4 aspect ratio */
  min-height: 0; /* Ensure aspect-ratio works correctly */
  margin: 0 auto;
  padding: 3rem 2.5rem;
  /* Container maintains aspect-ratio to prevent layout shift when font-size changes */
  background: linear-gradient(135deg, #faf8f3 0%, #f5f2e8 50%, #f0ede3 100%);
  border: 1px solid rgba(139, 120, 93, 0.3);
  border-radius: 2px;
  box-shadow: 0 0 0 1px rgba(139, 120, 93, 0.1), 0 2px 4px rgba(0, 0, 0, 0.1),
    0 8px 16px rgba(0, 0, 0, 0.15), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  position: relative;
  text-align: left;
  color: #2c2416;
  font-family: "Caveat", cursive;
  font-size: clamp(1rem, 1.2vw + 0.5rem, 1.5rem); /* Dynamic base font size - responsive to viewport, all child elements will scale relative to this */
  line-height: 1.8;
  opacity: 0;
  transform: translateY(50px) scale(0.9) rotate(-2deg);
  transition: opacity 0.6s ease, transform 0.6s ease;
  overflow-y: auto;
}

.message-section.revealed {
  position: relative;
  margin: calc(50vh - 500px) auto 3rem; /* Adjusted for larger letter size */
  transform: translateY(0) scale(1) rotate(-0.5deg);
  opacity: 1;
  animation: letterReveal 0.8s ease-out forwards;
}

/* Hide overflow when letter is fully unfolded (not trifolded) - but allow overflow when gift card is revealed */
.message-section.revealed:not(.trifolded):not(.unfolding):not(:has(.card.card-revealed)) {
  overflow: hidden;
}

/* Allow overflow when gift card is revealed so it can overflow the letter container */
.message-section.revealed:not(.trifolded):not(.unfolding):has(.card.card-revealed) {
  overflow: visible;
  overflow-y: visible;
  overflow-x: visible;
}

@media (max-height: 800px) {
  .message-section.revealed {
    margin-top: 2rem;
  }
}

.message-section.revealed.shaking {
  animation: letterShake 5s linear infinite;
}

.message-section.revealed.shaking:hover {
  animation: letterShake 5s linear infinite paused;
}

.message-section.revealed:hover {
  transform: translateY(0) scale(1.01) rotate(0deg);
  box-shadow: 0 0 0 1px rgba(139, 120, 93, 0.2), 0 4px 8px rgba(0, 0, 0, 0.15),
    0 12px 24px rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.6);
}

/* Trifolded Letter State */
.message-section.trifolded {
  overflow: hidden;
  max-height: 250px;
  padding: 1.5rem 2.5rem;
  position: relative;
  transform: translateY(0) scale(1.2) rotate(-2deg);
  background: linear-gradient(to bottom, #faf8f3 0%, #f0ede3 100%);
  animation: unfoldHint 2s ease-in-out infinite;
  mask-image: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 60%,
    rgba(0, 0, 0, 0.5) 80%,
    rgba(0, 0, 0, 0) 100%
  );
  -webkit-mask-image: linear-gradient(
    to bottom,
    rgba(0, 0, 0, 1) 0%,
    rgba(0, 0, 0, 1) 60%,
    rgba(0, 0, 0, 0.5) 80%,
    rgba(0, 0, 0, 0) 100%
  );
}

/* Show all content when trifolded - but only top third of height is visible */
.message-section.trifolded .main-title,
.message-section.trifolded .personal-message,
.message-section.trifolded .love-message,
.message-section.trifolded .appendix {
  opacity: 1;
  pointer-events: auto;
  height: auto;
  overflow: visible;
  display: block;
  margin: 0;
}

.message-section.trifolded .main-title {
  margin-bottom: 1rem;
  margin-top: 0;
}

.message-section.trifolded .personal-message {
  margin-bottom: 1rem;
}

.message-section.trifolded .love-message {
  margin-bottom: 1rem;
}

/* Show paper texture when trifolded but make it subtle */
.message-section.trifolded::before {
  opacity: 0.3;
}

/* Override aged paper spots when trifolded - use for fold lines instead */
.message-section.trifolded::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    /* First fold line */
    /* linear-gradient(
      to bottom,
      transparent calc(33% - 1px),
      rgba(139, 120, 93, 0.4) 33%,
      rgba(139, 120, 93, 0.4) calc(33% + 1px),
      transparent calc(33% + 2px)
    ), */
    /* Second fold line */
      /* linear-gradient(
        to bottom,
        transparent calc(66% - 1px),
        rgba(139, 120, 93, 0.4) 66%,
        rgba(139, 120, 93, 0.4) calc(66% + 1px),
        transparent calc(66% + 2px)
      ); */
  pointer-events: none;
  z-index: 10;
  border-radius: 2px;
  box-shadow: inset 0 33% 0 -1px rgba(139, 120, 93, 0.2),
    inset 0 66% 0 -1px rgba(139, 120, 93, 0.2);
}

/* Hover effect for trifolded letter */
.message-section.trifolded:hover {
  transform: translateY(0) scale(1.22) rotate(-1deg);
  box-shadow: 0 0 0 1px rgba(139, 120, 93, 0.2), 0 4px 8px rgba(0, 0, 0, 0.15),
    0 12px 24px rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.6);
  animation: unfoldHint 1.5s ease-in-out infinite;
}

/* Unfolding animation - Accordion style with flip */
.message-section.unfolding {
  overflow: hidden;
  overflow-x: hidden;
  overflow-y: hidden;
  position: relative;
  transform: scale(1.2) rotate(-2deg);
  animation: letterUnfoldAccordionFlip 2.5s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  transform-style: preserve-3d;
  perspective: 1000px;
}

/* First third (personal-message) - stays in place, fully visible */
.message-section.unfolding .personal-message {
  position: relative;
  z-index: 3;
  transform: translateZ(0);
}

/* Second third (love-message) - starts behind first, flips 180° around bottom of first */
.message-section.unfolding .love-message {
  animation: secondThirdFlip 2.5s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  background: linear-gradient(135deg, #faf8f3 0%, #f0ede3 100%);
  padding: 1.5rem 2.5rem;
  margin: 0;
  backface-visibility: hidden;
  transform-style: preserve-3d;
  /* Initial state: flipped 180° */
  transform: rotateX(180deg);
}

/* Third third (appendix) - starts behind second, moves to position (NO FLIP) */
.message-section.unfolding .appendix {
  animation: thirdThirdMove 2.5s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  background: linear-gradient(135deg, #faf8f3 0%, #f0ede3 100%);
  padding: 1.5rem 2.5rem;
  margin: 0;
  transform: translateY(0) !important; /* Ensure no rotation */
}

@keyframes letterUnfoldAccordionFlip {
  0% {
    max-height: 250px;
    padding: 1.5rem 2.5rem;
    transform: scale(1.2) rotate(-2deg);
    mask-image: linear-gradient(
      to bottom,
      rgba(0, 0, 0, 1) 0%,
      rgba(0, 0, 0, 1) 60%,
      rgba(0, 0, 0, 0.5) 80%,
      rgba(0, 0, 0, 0) 100%
    );
    -webkit-mask-image: linear-gradient(
      to bottom,
      rgba(0, 0, 0, 1) 0%,
      rgba(0, 0, 0, 1) 60%,
      rgba(0, 0, 0, 0.5) 80%,
      rgba(0, 0, 0, 0) 100%
    );
  }
  100% {
    max-height: none;
    padding: 3rem 2.5rem;
    transform: scale(1) rotate(-0.5deg);
    mask-image: none;
    -webkit-mask-image: none;
  }
}

@keyframes secondThirdFlip {
  0% {
    transform: rotateX(180deg);
  }
  100% {
    transform: rotateX(0deg);
  }
}

@keyframes thirdThirdMove {
  0% {
    transform: translateY(0) rotateX(0deg);
  }
  100% {
    transform: translateY(var(--third-third-move, 0)) rotateX(0deg);
  }
}

.message-section.unfolding .main-title,
.message-section.unfolding .personal-message,
.message-section.unfolding .love-message,
.message-section.unfolding .appendix {
  /* Content is already visible, just needs to be revealed as letter unfolds */
}

@keyframes expandContent {
  0% {
    max-height: 150px;
  }
  100% {
    max-height: none;
  }
}

@keyframes fadeOutGradient {
  0% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@keyframes fadeInContent {
  0% {
    opacity: 0;
    transform: translateY(10px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Paper texture effect */
.message-section::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(139, 120, 93, 0.03) 2px,
      rgba(139, 120, 93, 0.03) 4px
    ),
    repeating-linear-gradient(
      90deg,
      transparent,
      transparent 2px,
      rgba(139, 120, 93, 0.03) 2px,
      rgba(139, 120, 93, 0.03) 4px
    );
  pointer-events: none;
  border-radius: 2px;
  opacity: 0.6;
}

/* Subtle aged paper spots */
.message-section::after {
  content: "";
  position: absolute;
  top: 10%;
  right: 15%;
  width: 60px;
  height: 60px;
  background: radial-gradient(
    circle,
    rgba(139, 120, 93, 0.08) 0%,
    transparent 70%
  );
  border-radius: 50%;
  pointer-events: none;
}

.main-title {
  font-family: "Caveat", cursive;
  font-size: 2.69em; /* Relative to letter base font-size */
  color: #2c2416;
  margin-bottom: 2rem;
  margin-top: 0;
  text-shadow: none;
  font-weight: 700;
  letter-spacing: 0.5px;
  position: relative;
  z-index: 1;
}

.personal-message {
  font-size: clamp(1.0rem, 1.0vw + 0.8rem, 2.2rem); /* Dynamic scaling with viewport and container */
  line-height: 2;
  margin: 0 0 1.5rem 0;
  color: #2c2416;
  text-shadow: none;
  position: relative;
  z-index: 1;
  font-weight: 400;
}

.love-message {
  font-size: clamp(1.0rem, 1.0vw + 0.8rem, 2.2rem);
  line-height: 2;
  margin: 2rem 0 0 0;
  color: #2c2416;
  font-weight: 400;
  text-align: left;
  text-shadow: none;
  position: relative;
  z-index: 1;
  font-style: italic;
  padding-top: 1.5rem;
  border-top: 1px solid rgba(139, 120, 93, 0.2);
}

#love-message {
  margin-top: 0px;
  border-top-width: 0px;
  border-top-color: rgba(0, 0, 0, 0);
  border-top-style: none;
  border-image: none;
}

.appendix {
  font-size: 0.77em; /* Relative to letter base font-size */
  line-height: 1.4;
  margin: 1.5rem 0 0 0;
  color: #2c2416;
  font-weight: 400;
  text-align: left;
  text-shadow: none;
  position: relative;
  z-index: 1;
  opacity: 0.7;
}

/* Gift card inside message section */
.message-section .card {
  margin: 2rem auto 0;
  max-width: 400px;
  width: 90%;
  opacity: 0;
  transform: translateY(20px) scale(0.95);
  transition: opacity 0.6s ease, transform 0.6s ease;
  /* Reset inherited styles from message-section */
  font-family: "Playfair Display", serif;
  color: var(--text-color);
  text-align: center;
  font-size: 1rem;
  line-height: normal;
  position: relative;
  z-index: 10;
}

.message-section .card.card-revealed {
  opacity: 1;
  transform: translateY(0) scale(1);
  display: flex !important;
}

/* Reset card content styles */
.message-section .card .card-content {
  font-family: "Playfair Display", serif;
  color: var(--text-color);
  text-align: center;
}

.message-section .card .card-number {
  font-family: "Playfair Display", serif;
  color: var(--text-color);
  text-align: center;
  font-size: 1.6rem !important;
  letter-spacing: 0.18rem !important;
}

.message-section .card .card-details {
  font-family: "Playfair Display", serif;
  color: var(--text-color);
}

.message-section .card .label {
  font-family: "Comic Neue", sans-serif;
  color: var(--text-color);
}

.message-section .card .value {
  font-family: "Comic Neue", sans-serif;
  color: var(--text-color);
}

.message-section .card .card-logo {
  font-size: 2.5rem !important;
  font-family: "Comic Neue", sans-serif !important;
}

/* Hide card when letter is trifolded */
.message-section.trifolded .card {
  display: none !important;
}

.easter-egg-word {
  cursor: pointer;
  position: relative;
  text-decoration: underline;
  text-decoration-style: dotted;
  transition: color 0.2s ease;
}

.easter-egg-word:hover {
  color: var(--accent-color);
}

.easter-egg-word::after {
  content: attr(data-message);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(135deg, var(--primary-color) 0%, #ffed4e 100%);
  color: var(--background-color);
  padding: 0.5rem 1rem;
  border-radius: 10px;
  font-size: 0.9rem;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  margin-bottom: 0.5rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4), 0 0 15px rgba(255, 215, 0, 0.3);
  border: 1px solid rgba(255, 215, 0, 0.5);
}

.easter-egg-word:hover::after {
  opacity: 1;
}

.easter-egg-center {
  display: block;
  text-align: center;
  margin: 0;
  line-height: 1.2;
}

