:root {
  --paper: #fff8f0;
  --paper-shadow: #e8d5c4;
  --rose: #e57373;
  --rose-dark: #c62828;
  --ink: #4a3728;
  --sky: #d4ecfa;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  background: radial-gradient(circle at top, var(--sky), #f5e6d8 55%, #e8d0bf);
  color: var(--ink);
  font-family: "Nunito", system-ui, sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  min-height: 100dvh;
  overflow: hidden;
}

.card-frame {
  position: relative;
  width: min(900px, 100%);
  max-height: calc(100dvh - 48px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  background: var(--paper);
  border-radius: 20px;
  box-shadow:
    0 18px 50px rgba(74, 55, 40, 0.18),
    0 0 0 1px rgba(255, 255, 255, 0.6) inset;
  padding: 12px;
}

#game {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100dvh - 80px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  border-radius: 12px;
  background: #b8e0f5;
  cursor: pointer;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 12px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
}

.cover {
  background:
    radial-gradient(circle at 12% 18%, rgba(198, 40, 40, 0.06) 0 1.5px, transparent 2px),
    radial-gradient(circle at 88% 22%, rgba(74, 55, 40, 0.05) 0 1px, transparent 1.5px),
    radial-gradient(circle at 18% 82%, rgba(74, 55, 40, 0.04) 0 1.2px, transparent 1.8px),
    radial-gradient(circle at 78% 78%, rgba(198, 40, 40, 0.05) 0 1px, transparent 1.5px),
    linear-gradient(160deg, #fff8f0 0%, #fdebd3 45%, #f8d7da 100%);
  cursor: pointer;
  overflow: hidden;
}

/* Quiet corner doodles */
.cover::before,
.cover::after {
  content: "";
  position: absolute;
  pointer-events: none;
  z-index: 0;
}

.cover::before {
  top: 18px;
  left: 18px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(198, 40, 40, 0.28);
  box-shadow:
    14px 4px 0 -1.5px rgba(198, 40, 40, 0.18),
    6px 16px 0 -2px rgba(74, 55, 40, 0.14),
    22px 14px 0 -2px rgba(198, 40, 40, 0.16);
}

.cover::after {
  bottom: 20px;
  right: 20px;
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 7px solid rgba(198, 40, 40, 0.22);
  box-shadow:
    -16px -6px 0 -1px rgba(74, 55, 40, 0.12),
    -8px -18px 0 -2px rgba(198, 40, 40, 0.14);
}

.envelope {
  max-width: 360px;
  padding: 32px 24px 28px;
  border: 2px solid rgba(198, 40, 40, 0.28);
  border-radius: 16px;
  background:
    radial-gradient(circle at 30% 20%, rgba(74, 55, 40, 0.035) 0 0.6px, transparent 0.8px),
    radial-gradient(circle at 70% 55%, rgba(74, 55, 40, 0.03) 0 0.5px, transparent 0.7px),
    radial-gradient(circle at 45% 80%, rgba(74, 55, 40, 0.025) 0 0.55px, transparent 0.75px),
    linear-gradient(175deg, #fffdf9 0%, #fff6ec 55%, #fceee6 100%);
  background-size:
    12px 12px,
    10px 10px,
    14px 14px,
    auto;
  position: relative;
  z-index: 1;
  outline: 1px dotted rgba(198, 40, 40, 0.22);
  outline-offset: -8px;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.85) inset,
    0 14px 32px rgba(74, 55, 40, 0.14),
    0 4px 10px rgba(74, 55, 40, 0.08),
    2px 2px 0 rgba(232, 213, 196, 0.6);
}

/* Washi-tape strip across top of envelope */
.envelope::before {
  content: "";
  position: absolute;
  top: -11px;
  left: 50%;
  width: 96px;
  height: 22px;
  transform: translateX(-50%) rotate(-2.5deg);
  background:
    repeating-linear-gradient(
      90deg,
      rgba(255, 255, 255, 0.35) 0 3px,
      transparent 3px 7px
    ),
    repeating-linear-gradient(
      180deg,
      transparent 0 4px,
      rgba(198, 40, 40, 0.08) 4px 5px
    ),
    linear-gradient(180deg, #f5c6c6 0%, #e8a0a0 100%);
  border-radius: 1px;
  opacity: 0.9;
  box-shadow:
    0 1px 3px rgba(74, 55, 40, 0.18),
    inset 0 1px 0 rgba(255, 255, 255, 0.35);
  pointer-events: none;
}

/* Soft paper corner fold */
.envelope::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 26px;
  height: 26px;
  background: linear-gradient(
    135deg,
    transparent 48%,
    rgba(232, 213, 196, 0.95) 50%,
    #f0ddd0 100%
  );
  border-radius: 0 0 14px 0;
  box-shadow: -1px -1px 3px rgba(74, 55, 40, 0.1);
  pointer-events: none;
}

.heart-seal {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 255, 255, 0.35), transparent 42%),
    radial-gradient(circle at 50% 55%, #ef9a9a 0%, var(--rose) 48%, var(--rose-dark) 100%);
  color: rgba(255, 255, 255, 0.95);
  font-size: 22px;
  line-height: 56px;
  position: relative;
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.28),
    inset 0 -3px 6px rgba(0, 0, 0, 0.12),
    0 0 0 2px rgba(198, 40, 40, 0.55),
    0 0 0 5px rgba(255, 248, 240, 0.95),
    0 0 0 7px rgba(198, 40, 40, 0.4),
    0 8px 18px rgba(198, 40, 40, 0.32);
}

.cover h1 {
  font-family: "Caveat", cursive;
  font-size: clamp(2rem, 6vw, 2.8rem);
  margin: 0 0 8px;
  color: var(--rose-dark);
}

.cover p {
  margin: 0;
  font-size: 1.05rem;
}

.hint {
  margin-top: 18px !important;
  font-size: 0.9rem !important;
  opacity: 0.7;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.55; }
  50% { opacity: 1; }
}

.hud {
  position: absolute;
  top: 22px;
  left: 22px;
  right: 22px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
}

.hud span {
  font-size: 0.85rem;
  font-weight: 700;
  color: white;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

.progress-track {
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.35);
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15) inset;
}

.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #f48fb1, #e57373);
  transition: width 0.2s ease;
}

.toast {
  position: absolute;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: rgba(255, 255, 255, 0.95);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.95rem;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(74, 55, 40, 0.15);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  white-space: nowrap;
  max-width: calc(100% - 40px);
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.ending {
  background: transparent;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.ending.show-message {
  background: rgba(255, 248, 240, 0.72);
  pointer-events: auto;
}

.letter {
  max-width: 420px;
  padding: 32px 28px 24px;
  pointer-events: auto;
  text-align: center;
  position: relative;
  opacity: 0;
  transform: translateY(12px) rotate(0.6deg);
  transition: opacity 1s ease, transform 1s ease;
  background:
    radial-gradient(circle at 25% 30%, rgba(74, 55, 40, 0.03) 0 0.55px, transparent 0.75px),
    radial-gradient(circle at 72% 60%, rgba(74, 55, 40, 0.025) 0 0.5px, transparent 0.7px),
    linear-gradient(175deg, #fffdf8 0%, #fff6eb 60%, #faf0e4 100%);
  background-size: 11px 11px, 13px 13px, auto;
  border: 1px solid rgba(198, 40, 40, 0.18);
  border-radius: 4px;
  outline: 1px dotted rgba(198, 40, 40, 0.16);
  outline-offset: -7px;
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.9) inset,
    0 16px 36px rgba(74, 55, 40, 0.16),
    0 4px 12px rgba(74, 55, 40, 0.1),
    1px 2px 0 rgba(232, 213, 196, 0.7);
}

/* Washi tape pinning the notecard over the photo */
.letter::before {
  content: "";
  position: absolute;
  top: -10px;
  left: 50%;
  width: 88px;
  height: 20px;
  transform: translateX(-50%) rotate(1.5deg);
  background:
    repeating-linear-gradient(
      90deg,
      rgba(255, 255, 255, 0.4) 0 3px,
      transparent 3px 8px
    ),
    linear-gradient(180deg, #d4e8f5 0%, #b8d4e8 100%);
  border-radius: 1px;
  opacity: 0.88;
  box-shadow:
    0 1px 3px rgba(74, 55, 40, 0.16),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
  pointer-events: none;
}

.ending.show-message .letter {
  opacity: 1;
  transform: translateY(0) rotate(0.6deg);
}

.win-message {
  font-family: "Caveat", cursive;
  font-size: clamp(1.7rem, 5.2vw, 2.35rem) !important;
  font-weight: 700;
  line-height: 1.4 !important;
  margin: 0 0 18px !important;
  color: var(--ink);
  letter-spacing: 0.01em;
}

.win-signature {
  font-family: "Caveat", cursive;
  font-size: 1.35rem !important;
  font-weight: 700;
  color: var(--rose-dark);
  margin: 0 !important;
  position: relative;
  display: inline-block;
  padding-bottom: 10px;
}

/* Small sign-off flourish under signature */
.win-signature::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 36px;
  height: 8px;
  transform: translateX(-50%);
  background:
    radial-gradient(circle at 50% 40%, var(--rose) 0 2px, transparent 2.5px),
    linear-gradient(
      90deg,
      transparent 0%,
      rgba(198, 40, 40, 0.45) 18%,
      rgba(198, 40, 40, 0.45) 82%,
      transparent 100%
    );
  background-size: 8px 8px, 100% 1.5px;
  background-position: center, center 55%;
  background-repeat: no-repeat;
  opacity: 0.7;
  pointer-events: none;
}

#replay {
  margin-top: 22px;
  border: none;
  background: transparent;
  color: var(--ink);
  font-family: "Nunito", sans-serif;
  font-size: 0.82rem;
  font-weight: 600;
  opacity: 0.55;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

#replay:hover,
#replay:focus-visible {
  opacity: 0.8;
}

.controls-hint {
  margin: 10px 0 0;
  font-size: 0.8rem;
  opacity: 0.65;
}

.hidden {
  display: none !important;
}

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top, var(--sky), #f5e6d8 55%, #e8d0bf);
  color: var(--ink);
  font-family: "Nunito", system-ui, sans-serif;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.asset-warning {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  background: #fff3cd;
  border: 1px solid #ffc107;
  color: #664d03;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 0.75rem;
  max-width: 90%;
  text-align: center;
}

.asset-warning code {
  font-size: 0.72rem;
}

.rotate-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(74, 55, 40, 0.72);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.rotate-overlay.visible {
  display: flex;
}

.rotate-overlay.dismissed {
  display: none !important;
}

.rotate-card {
  max-width: 320px;
  padding: 28px 24px;
  border-radius: 20px;
  background: var(--paper);
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.rotate-icon {
  font-size: 3rem;
  margin-bottom: 12px;
  animation: rotate-hint 2.4s ease-in-out infinite;
}

@keyframes rotate-hint {
  0%, 100% { transform: rotate(0deg); }
  35% { transform: rotate(-90deg); }
  65% { transform: rotate(-90deg); }
}

.rotate-card p,
.rotate-title {
  margin: 0 0 8px;
  font-family: "Caveat", cursive;
  font-size: 1.6rem;
  color: var(--rose-dark);
  line-height: 1.3;
}

.rotate-sub {
  margin: 0 0 18px !important;
  font-family: "Nunito", system-ui, sans-serif !important;
  font-size: 0.92rem !important;
  color: var(--ink) !important;
  opacity: 0.75;
  line-height: 1.4;
}

#rotate-dismiss {
  border: none;
  background: transparent;
  color: var(--ink);
  font-family: "Nunito", sans-serif;
  font-size: 0.85rem;
  opacity: 0.65;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

@media (max-width: 900px) and (orientation: portrait) {
  .rotate-overlay:not(.dismissed) {
    display: flex;
  }

  body {
    padding: 0;
  }

  .card-frame {
    width: 100%;
    max-height: 100dvh;
    border-radius: 0;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    box-shadow: none;
  }

  #game {
    max-height: calc(100dvh - 36px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    border-radius: 10px;
  }

  .controls-hint {
    position: fixed;
    bottom: max(6px, env(safe-area-inset-bottom));
    left: 0;
    right: 0;
    margin: 0;
    text-align: center;
    font-size: 0.72rem;
    opacity: 0.5;
    pointer-events: none;
  }
}

@media (max-width: 900px) and (orientation: landscape) {
  body {
    padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right))
      max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
  }

  .card-frame {
    width: min(100%, 960px);
    max-height: calc(100dvh - 12px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    padding: 8px;
    border-radius: 14px;
  }

  #game {
    max-height: calc(100dvh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }

  .controls-hint {
    margin: 4px 0 0;
    font-size: 0.72rem;
  }

  .overlay {
    inset: 8px;
    padding: 12px;
  }

  .envelope {
    padding: 16px 14px;
  }

  .cover h1 {
    font-size: clamp(1.6rem, 5vw, 2.2rem);
  }
}

@media (max-width: 600px) {
  .card-frame {
    padding: 8px;
    border-radius: 14px;
  }

  .overlay {
    inset: 8px;
    padding: 16px;
  }

  .toast {
    font-size: 0.85rem;
    white-space: normal;
    text-align: center;
  }
}
