:root {
  --bg: #2b2228;
  --ink: #342b31;
  --paper: #fffaf3;
  --accent: #84534e;
  --pad: 52px;
  --edge: 20px; /* 사파리 가장자리 뒤로 가기 제스처와 겹치지 않게 */
  font-family: -apple-system, "Apple SD Gothic Neo", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  position: fixed;
  inset: 0;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--paper);
}
body {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; cursor: pointer; }
img { -webkit-user-drag: none; }

/* ---------- 레이아웃: 위 게임 영역(3:4), 아래 조작 영역 ---------- */

#app {
  height: 100dvh;
  max-width: 430px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: env(safe-area-inset-top);
}

#stage {
  position: relative;
  flex: none;
  width: min(100vw, 430px, calc((100dvh - 150px - env(safe-area-inset-top)) * 0.75));
  aspect-ratio: 3 / 4;
  overflow: hidden;
  background: #f1e3ce;
}
#game { display: block; width: 100%; height: 100%; }

/* 결혼식장 우상단 '청혼 다시 보기' */
#replay-proposal {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  min-height: 44px;
  padding: 0 16px;
  border: 1.5px solid rgba(132, 83, 78, 0.35);
  border-radius: 22px;
  background: rgba(255, 250, 243, 0.92);
  color: #84534e;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 2px 10px rgba(70, 40, 40, 0.18);
  cursor: pointer;
}
#replay-proposal:active { transform: scale(0.97); }

#fade {
  position: absolute;
  inset: 0;
  background: #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}
#fade.on { opacity: 1; }

#controls {
  position: relative;
  flex: 1;
  min-height: 150px;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
}

/* 조작 영역이 낮으면 버튼을 44pt 로 줄인다 */
@media (max-height: 720px) { :root { --pad: 44px; } }

#dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--pad));
  grid-template-rows: repeat(3, var(--pad));
  touch-action: none;
}
.pad {
  border: 0;
  border-radius: 12px;
  background: rgba(255, 250, 243, 0.14);
  color: rgba(255, 250, 243, 0.8);
  font-size: 18px;
  padding: 0;
  touch-action: none;
}
.pad.on { background: rgba(255, 250, 243, 0.34); }
.pad--up { grid-area: 1 / 2; }
.pad--left { grid-area: 2 / 1; }
.pad--right { grid-area: 2 / 3; }
.pad--down { grid-area: 3 / 2; }

/* ---------- 스피커 ---------- */

#speaker {
  position: fixed;
  right: max(var(--edge), calc((100vw - 430px) / 2 + var(--edge)));
  bottom: calc(env(safe-area-inset-bottom) + 16px);
  z-index: 900;
  width: 44px;
  height: 44px;
  padding: 9px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 250, 243, 0.14);
  color: var(--paper);
}
#speaker svg { display: block; width: 100%; height: 100%; }
#speaker .slash { display: none; }
#speaker.muted .slash { display: inline; }
#speaker.muted .waves { display: none; }

/* 꽃가루: 팝업(800)보다 위, 스피커(900)보다 아래. 터치는 통과 */
#confetti { position: fixed; inset: 0; width: 100vw; height: 100dvh; z-index: 850; pointer-events: none; }

/* ---------- 팝업 ---------- */

.popup {
  position: fixed;
  inset: 0;
  z-index: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(24, 16, 22, 0.62);
  animation: pop-in 0.18s ease-out;
}
@keyframes pop-in { from { opacity: 0; } }

.popup__card {
  position: relative;
  width: min(90vw, 400px);
  max-height: 80dvh;
  display: flex;
  flex-direction: column;
  border-radius: 16px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}
.popup__x {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 44px;
  height: 44px;
  border: 0;
  background: none;
  color: #6b5a63;
  font-size: 18px;
}
.popup__body {
  overflow-y: auto;
  padding: 22px 22px 18px;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}
.popup__title { margin: 0 36px 10px; text-align: center; font-size: 19px; }
.popup__art { display: block; width: min(70%, 220px); margin: 8px auto 12px; }
.popup__text { margin: 0; line-height: 1.6; white-space: pre-wrap; word-break: keep-all; overflow-wrap: anywhere; text-align: center; color: #5b4c54; }
.popup__buttons { display: flex; gap: 10px; padding: 0 22px 22px; }

/* 편지지: 노란 편지(연노랑 바탕, 주황 밑줄)와 흰 편지(흰 바탕, 검은 밑줄).
   밑줄은 글줄 높이에 맞춰 긋고, 스크롤하면 줄도 같이 움직인다 */
.popup__card--note { --note-line: 38px; --note-bg: #fff5c4; --note-rule: #f2a14a; --note-rule-w: 2px; --note-ink: #4a3628;
  width: min(92vw, 440px); max-height: 88dvh; background: var(--note-bg); overflow: hidden; }
.popup__card--note.note--white { --note-bg: #fff; --note-rule: #2b2b2b; --note-rule-w: 1px; --note-ink: #222; }
.popup__card--note .popup__body { display: flex; padding: 0; overflow: hidden; }
.note__paper {
  flex: 1;
  min-height: min(56dvh, 480px);
  overflow-y: auto;
  padding: 52px 26px 30px;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  background-color: var(--note-bg);
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--note-line) - var(--note-rule-w)), var(--note-rule) calc(var(--note-line) - var(--note-rule-w)), var(--note-rule) var(--note-line));
  background-position: 0 52px; /* 첫 줄 글자 아래부터 */
  background-attachment: local;
  background-clip: content-box;
}
.note__text { margin: 0; font-size: 18px; line-height: var(--note-line); color: var(--note-ink); white-space: pre-wrap; word-break: keep-all; overflow-wrap: anywhere; }

/* 사진 넘겨 보기 */
.popup__card--gallery { width: min(96vw, 500px); max-height: 96dvh; }
.popup__card--gallery .popup__body { padding: 14px 10px 12px; }
/* 제목 없이 사진부터: 닫기 버튼은 사진 오른쪽 위에 띄운다 */
.popup__card--gallery .popup__x { top: 18px; right: 14px; z-index: 1; width: 40px; height: 40px; border-radius: 50%; background: rgba(255, 255, 255, 0.82); color: #4a3a42; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18); }
.gallery__frame {
  position: relative;
  height: min(760px, calc(96dvh - 130px)); /* 글·번호 자리를 빼고 화면을 거의 채운다 */
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: #efe6de;
  overflow: hidden;
  touch-action: pan-y; /* 가로로 미는 것은 넘기기로 쓴다 */
  user-select: none;
}
.gallery__scroll {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.gallery__img { max-width: 100%; max-height: 100%; object-fit: contain; -webkit-user-drag: none; }
.gallery__missing { margin: 0; color: #8a7a82; font-size: 14px; }
.gallery__nav {
  position: absolute;
  top: 50%;
  width: 44px;
  height: 44px;
  margin-top: -22px;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.82);
  color: #4a3a42;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
}
.gallery__nav svg { width: 22px; height: 22px; display: block; }
.gallery__nav--prev { left: 6px; }
.gallery__nav--next { right: 6px; }
.gallery__count { margin-top: 6px; text-align: center; font-size: 13px; color: #8a7a82; font-variant-numeric: tabular-nums; }
.gallery__caption { margin: 10px 4px 0; line-height: 1.5; white-space: pre-wrap; text-align: center; color: #4f4048; font-size: 18px; font-weight: 600; }
.popup__action {
  flex: 1;
  min-height: 48px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
}

/* ---------- 시작 화면 ---------- */

#intro, #loading {
  position: fixed;
  inset: 0;
  z-index: 750; /* 스피커(900)보다 아래 */
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(env(safe-area-inset-top) + 12vh) 24px calc(env(safe-area-inset-bottom) + 84px);
  background: radial-gradient(ellipse at 50% 20%, #4a3445 0%, var(--bg) 70%);
  text-align: center;
}
.intro__top { width: min(100%, 320px); }
.intro__sub { margin: 0 0 6px; color: #f6b6c8; letter-spacing: 0.12em; font-size: 14px; }
.intro__question { margin: 0 0 28px; font-size: 30px; color: #ffe2a0; }
.intro__form { display: flex; gap: 8px; }
.intro__form input {
  flex: 1;
  min-width: 0;
  height: 52px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: #fffaf3;
  color: var(--ink);
  font-size: 22px; /* 16px 이상이어야 누를 때 화면이 확대되지 않는다 */
  text-align: center;
  letter-spacing: 0.2em;
  user-select: text;
  -webkit-user-select: text;
}
.intro__form button {
  height: 52px;
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 16px;
}
.intro__message { min-height: 1.4em; margin: 12px 0 0; color: #ffb3b3; font-size: 15px; }
.intro__bottom { margin-top: auto; }
.intro__sound { margin: 0; color: rgba(255, 250, 243, 0.45); font-size: 13px; }
.shake { animation: shake 0.4s; }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

/* ---------- 로딩 화면 ---------- */

#loading { justify-content: center; padding-top: 0; }
.loading__text { margin: 0 0 28px; color: #fffaf3; font-size: 17px; line-height: 1.6; }
.loading__track { position: relative; width: min(100%, 360px); height: 96px; overflow: hidden; }
.loading__walker { position: absolute; bottom: 0; width: 72px; height: 96px; transform: translateX(-50%); object-fit: contain; }
.loading__bar { width: min(100%, 360px); height: 6px; margin-top: 6px; border-radius: 999px; background: rgba(255, 250, 243, 0.14); overflow: hidden; }
.loading__bar span { display: block; height: 100%; width: 0; background: #f6b6c8; transition: width 0.3s; }
.loading__percent { margin: 10px 0 0; color: rgba(255, 250, 243, 0.8); font-size: 15px; font-variant-numeric: tabular-nums; }
.loading__retry {
  margin-top: 18px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid rgba(255, 250, 243, 0.3);
  border-radius: 999px;
  background: rgba(255, 250, 243, 0.08);
  color: #fffaf3;
}

/* ---------- 엔딩 ---------- */

#ending {
  position: fixed;
  inset: 0;
  z-index: 700; /* 스피커(900)보다 아래 */
  height: 100dvh;
  background: #05060c;
}
#tree { display: block; width: 100%; height: 100%; }

.ending__bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(env(safe-area-inset-bottom) + 72px); /* 스피커 버튼 위로 */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 1.6s ease, transform 1.6s ease;
  pointer-events: none;
}
.ending__bottom.show { opacity: 1; transform: none; pointer-events: auto; }
.ending__message {
  margin: 0;
  font-size: 26px;
  font-weight: 800;
  color: #ffe9a8;
  text-shadow: 0 0 18px rgba(255, 210, 74, 0.45);
}
.ending__buttons { display: flex; gap: 10px; }
.ending__buttons button {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid rgba(255, 250, 243, 0.28);
  border-radius: 999px;
  background: rgba(255, 250, 243, 0.08);
  color: rgba(255, 250, 243, 0.85);
  font-size: 14px;
}

/* ---------- 가로 화면 안내 ---------- */

#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  font-size: 18px;
}
@media (orientation: landscape) and (max-height: 500px) {
  #rotate { display: flex; }
}
