* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #f7f4f2;
  font-family: Arial, sans-serif;
  overflow: hidden;
}

.scene {
  min-height: 100vh;
  display: grid;
  place-items: center;
  position: relative;
}

.hint {
  position: absolute;
  top: calc(50% + min(82vw, 340px) * 0.40);
  width: 100%;
  text-align: center;
  font-size: 17px;
  font-weight: 700;
  color: #333;
  letter-spacing: .2px;
  animation: pulseHint 1.8s ease-in-out infinite;
  pointer-events: none;
}

@keyframes pulseHint {
  0%, 100% { opacity: .65; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(4px); }
}

.envelope-button {
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
}

.envelope {
  position: relative;
  width: min(82vw, 340px);
  height: calc(min(82vw, 340px) * 0.66);
  perspective: 1000px;
}

.back {
  position: absolute;
  inset: 0;
  background: #ffffff;
  border: 2px solid #d8d8d8;
  border-radius: 8px;
  box-shadow: 0 14px 35px rgba(0,0,0,.14);
}

.front {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 72%;
  background: #fafafa;
  border: 2px solid #d8d8d8;
  clip-path: polygon(0 0, 50% 58%, 100% 0, 100% 100%, 0 100%);
  border-radius: 0 0 8px 8px;
  z-index: 4;
}

.flap {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 58%;
  background: #fdfdfd;
  border: 2px solid #d8d8d8;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  transform-origin: top;
  transition: transform .8s ease;
  z-index: 5;
}

.letter {
  position: absolute;
  left: 7%;
  width: 86%;
  height: 86%;
  bottom: 7%;
  background: white;
  border-radius: 8px;
  box-shadow: 0 6px 18px rgba(0,0,0,.12);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 18px;
  text-align: center;
  opacity: 0;
  transform: translateY(10px);
  transition: transform .9s ease .25s, opacity .5s ease .25s;
  z-index: 3;
}

.letter p {
  margin: 8px 0 0;
  font-size: clamp(16px, 4.5vw, 21px);
  line-height: 1.35;
  color: #222;
  font-weight: 600;
}

.heart {
  font-size: 42px;
  display: inline-block;
  transform-origin: center;
}

.envelope.open .flap {
  transform: rotateX(180deg);
  z-index: 1;
}

.envelope.open .letter {
  opacity: 1;
  transform: translateY(-115px);
  z-index: 6;
}

.envelope.open .heart {
  animation: beat 1s infinite;
}

@keyframes beat {
  0%, 100% { transform: scale(1); }
  25% { transform: scale(1.18); }
  45% { transform: scale(.98); }
  65% { transform: scale(1.14); }
}

@media (max-width: 420px) {
  .envelope.open .letter {
    transform: translateY(-95px);
  }
}

.edit-toggle {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 20;
  border: 0;
  border-radius: 999px;
  padding: 12px 16px;
  background: #222;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(0,0,0,.18);
}

.editor {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(4px);
}

.editor.show { display: flex; }

.editor-card {
  position: relative;
  width: min(92vw, 440px);
  padding: 25px;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0,0,0,.25);
}

.editor-card h2 { margin: 0 0 8px; font-size: 23px; }
.editor-card p { margin: 0 0 16px; color: #666; font-size: 14px; line-height: 1.4; }

.close-editor {
  position: absolute;
  top: 10px;
  right: 14px;
  border: 0;
  background: transparent;
  font-size: 30px;
  color: #777;
  cursor: pointer;
}

#messageInput {
  width: 100%;
  min-height: 130px;
  resize: vertical;
  border: 1px solid #ddd;
  border-radius: 12px;
  padding: 13px;
  font: inherit;
  outline: none;
}

#messageInput:focus { border-color: #999; }

.editor-actions { display: flex; gap: 10px; margin-top: 12px; }
.editor-actions button {
  flex: 1;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  font-weight: 700;
  cursor: pointer;
}
.save-btn { background: #222; color: #fff; }
.reset-btn { background: #eee; color: #222; }
.editor-card small { display: block; margin-top: 12px; color: #888; font-size: 12px; }

@media (max-width: 420px) {
  .edit-toggle { top: 12px; right: 12px; padding: 10px 13px; font-size: 13px; }
  .editor-card { padding: 22px 18px; }
}
