/* =========================================================
   «На всех языках» — письмо, галактика, портрет, сердце.
   ========================================================= */
:root {
  --night: #0c0e1f;
  --night-2: #151937;
  --night-3: #202452;
  --star: #eeeaff;
  --lav: #b8a6ff;
  --lav-soft: rgba(184, 166, 255, 0.55);
  --lav-faint: rgba(184, 166, 255, 0.22);
  --seal: #6a4bb0;
  --seal-deep: #45307a;
  --seal-hi: #9c83e6;
  --paper: #f8f5fc;
  --paper-2: #efe9f7;
  --ink: #2b2638;
  --ink-soft: rgba(43, 38, 56, 0.74);
  --cream: #f4e8d0;

  --hand: 'Marck Script', 'Segoe Script', cursive;
  --serif: 'Cormorant Garamond', Georgia, 'Times New Roman', serif;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

  --ease: cubic-bezier(.22, .61, .36, 1);
  --ease-out: cubic-bezier(.16, 1, .3, 1);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 90% at 50% 38%, var(--night-3) 0%, var(--night-2) 38%, var(--night) 78%);
  color: var(--star);
  font-family: var(--serif);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

#sky {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#app { position: fixed; inset: 0; }
/* Жесты по небу — наши: браузеру тут нечего прокручивать и масштабировать.
   Письмо — исключение: если текст не влез, его листают. */
#sky, .scene { touch-action: none; }
/* Письмо — прокручиваемый блок: листать можно, масштабировать страницу — нет
   (иначе, увеличив письмо, дальше уже не уменьшить: у сцен после него жесты наши). */
.letter { touch-action: pan-y; overscroll-behavior: contain; }

.scene {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(28px, env(safe-area-inset-bottom));
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 1s var(--ease), visibility 0s linear 1s;
}
.scene.is-active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transition: opacity 1s var(--ease), visibility 0s;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
button:focus-visible { outline: 1.5px solid var(--lav); outline-offset: 4px; border-radius: 999px; }

/* ---------- кнопки и подсказки ---------- */
.pill {
  margin-top: 26px;
  padding: 13px 26px;
  border-radius: 999px;
  background: linear-gradient(180deg, #fbf3e2, #efdcbb);
  color: #3a2c1c;
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 500;
  letter-spacing: 0.01em;
  box-shadow: 0 10px 30px -10px rgba(244, 232, 208, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.7);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity .8s var(--ease), transform .8s var(--ease), box-shadow .3s;
}
.pill.is-in { opacity: 1; transform: none; }
/* Пока кнопка не показана, её нет: ни пальцем, ни с клавиатуры. Иначе тап
   под письмом пропускал письмо, а тап внизу рождающейся галактики —
   саму галактику. `visibility` не ставим на .is-in: тогда кнопка
   просвечивала бы сквозь неактивную сцену. */
.pill:not(.is-in) {
  visibility: hidden;
  pointer-events: none;
  transition: opacity .8s var(--ease), transform .8s var(--ease), visibility 0s linear .8s;
}
.pill:active { transform: scale(0.97); }
.pill--glow { animation: glow 2.6s ease-in-out infinite; }
.pill--quiet {
  background: rgba(238, 234, 255, 0.08);
  color: var(--star);
  border: 1px solid rgba(238, 234, 255, 0.28);
  box-shadow: none;
  font-size: 16px;
  padding: 10px 22px;
  background: rgba(12, 14, 31, 0.45);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
@keyframes glow {
  0%, 100% { box-shadow: 0 10px 30px -10px rgba(244, 232, 208, 0.5), inset 0 1px 0 rgba(255,255,255,.7); }
  50% { box-shadow: 0 10px 44px -6px rgba(244, 232, 208, 0.85), inset 0 1px 0 rgba(255,255,255,.7); }
}

.hint {
  margin: 22px 0 0;
  font-family: var(--hand);
  font-size: 22px;
  color: var(--lav);
  opacity: 0;
  transition: opacity 1s var(--ease);
}
.hint.is-in { opacity: 1; animation: breathe 3s ease-in-out infinite; }
.hint--small {
  font-family: var(--sans);
  font-size: 11.5px;
  letter-spacing: 0.06em;
  color: rgba(238, 234, 255, 0.55);
}
@keyframes breathe { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }

/* ---------- 1. заставка ---------- */
.intro__name, .intro__line {
  margin: 0;
  font-family: var(--hand);
  color: var(--star);
  text-shadow: 0 0 24px rgba(184, 166, 255, 0.35);
  opacity: 0;
  filter: blur(8px);
  transform: translateY(6px);
  transition: opacity 1.6s var(--ease), filter 1.6s var(--ease), transform 1.6s var(--ease);
}
.intro__name { font-size: clamp(34px, 10vw, 52px); }
.intro__line { font-size: clamp(40px, 12vw, 62px); margin-top: 6px; }
.intro__name.is-in, .intro__line.is-in { opacity: 1; filter: none; transform: none; }

/* ---------- 2. конверт ---------- */
.envelope {
  --w: min(78vw, 340px);
  position: relative;
  width: var(--w);
  aspect-ratio: 1.55;
  border-radius: 10px;
  perspective: 900px;
  animation: float 5s ease-in-out infinite;
  transition: transform 1s var(--ease-out), opacity 1s var(--ease);
}
@keyframes float { 0%, 100% { translate: 0 -4px; } 50% { translate: 0 6px; } }
.envelope > span { position: absolute; display: block; }

.envelope__back {
  inset: 0;
  border-radius: 10px;
  background: linear-gradient(160deg, #e8e3f2, #d6cfe6);
  box-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(255,255,255,.4) inset;
}
.envelope__paper {
  left: 7%; right: 7%; top: 8%; height: 84%;
  border-radius: 6px;
  background: var(--paper);
  box-shadow: 0 2px 6px rgba(0,0,0,.08);
  padding: 16% 10% 0;
  display: flex !important;
  flex-direction: column;
  gap: 9px;
  transition: transform 1.1s var(--ease-out);
  z-index: 1;
}
.envelope__paper-line { height: 2px; border-radius: 2px; background: rgba(106, 75, 176, 0.18); }
.envelope__paper-line--short { width: 55%; }
.envelope__front {
  inset: 0;
  border-radius: 10px;
  background:
    linear-gradient(to top right, #efeaf7 49.6%, transparent 50%) left bottom / 50.4% 100% no-repeat,
    linear-gradient(to top left, #ebe5f5 49.6%, transparent 50%) right bottom / 50.4% 100% no-repeat,
    linear-gradient(to top, #f3eff9 0%, #eae4f4 100%) bottom / 100% 45% no-repeat;
  z-index: 2;
}
.envelope__flap {
  left: 0; right: 0; top: 0; height: 58%;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  background: linear-gradient(180deg, #ddd6ec, #e9e4f3);
  transform-origin: top center;
  transform-style: preserve-3d;
  transition: transform .9s var(--ease), z-index 0s linear .45s;
  z-index: 3;
  filter: drop-shadow(0 4px 4px rgba(0,0,0,.08));
}
.envelope__to {
  left: 0; right: 0; bottom: 5%;
  text-align: center;
  font-family: var(--hand);
  font-size: calc(var(--w) * 0.085);
  color: var(--seal-deep);
  z-index: 4;
}
.envelope__stamp {
  right: 5%; bottom: 8%;
  width: calc(var(--w) * 0.1);
  color: rgba(106, 75, 176, 0.45);
  z-index: 4;
  transform: rotate(8deg);
}
.envelope__stamp svg { display: block; width: 100%; }

/* Сургуч: две половины одной печати. Раскалывается по вертикали. */
.seal {
  left: 50%; top: 58%;
  width: calc(var(--w) * 0.18);
  aspect-ratio: 1;
  translate: -50% -50%;
  z-index: 5;
}
.seal__half {
  position: absolute;
  inset: 0;
  transition: transform 1.1s cubic-bezier(.5, 0, .75, .3), opacity 1.1s ease-in;
}
.seal__half--l { clip-path: polygon(0 0, 52% 0, 46% 30%, 54% 55%, 47% 100%, 0 100%); }
.seal__half--r { clip-path: polygon(52% 0, 100% 0, 100% 100%, 47% 100%, 54% 55%, 46% 30%); }
.seal__face {
  position: absolute;
  /* Отступ — под кольцо сургуча: половинки режутся по своему квадрату, и
     без отступа кольцо срезалось его сторонами, печать выходила восьмиугольной. */
  inset: 8%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--hand);
  font-size: calc(var(--w) * 0.09);
  color: rgba(255, 255, 255, 0.55);
  text-shadow: -1px -1px 0 rgba(0, 0, 0, 0.25), 1px 1px 0 rgba(255, 255, 255, 0.18);
  background:
    radial-gradient(circle at 35% 30%, var(--seal-hi) 0%, var(--seal) 38%, var(--seal-deep) 100%);
  box-shadow:
    0 0 0 calc(var(--w) * 0.012) rgba(106, 75, 176, 0.55),
    0 6px 14px -4px rgba(40, 20, 80, 0.6),
    inset 0 0 0 calc(var(--w) * 0.018) rgba(255, 255, 255, 0.08);
}
.seal__face::after {
  content: '';
  position: absolute;
  inset: 16%;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.22);
}

.envelope.is-cracked .seal__half--l { transform: translate(-40%, 120%) rotate(-38deg); opacity: 0; }
.envelope.is-cracked .seal__half--r { transform: translate(40%, 130%) rotate(34deg); opacity: 0; }
.envelope.is-open .envelope__flap { transform: rotateX(180deg); z-index: 0; }
.envelope.is-out .envelope__paper { transform: translateY(-58%); }
.envelope.is-gone { transform: scale(1.12) translateY(10px); opacity: 0; }

/* ---------- 3. письмо ---------- */
.letter {
  position: relative;
  width: min(90vw, 420px);
  max-height: calc(100vh - 150px);
  max-height: calc(100dvh - 150px);
  overflow: hidden auto;
  padding: 28px 26px 22px;
  border-radius: 14px;
  background:
    radial-gradient(120% 60% at 0% 0%, rgba(184,166,255,0.14), transparent 60%),
    linear-gradient(180deg, var(--paper), var(--paper-2));
  color: var(--ink);
  box-shadow: 0 40px 80px -30px rgba(0, 0, 0, 0.8);
  transform: translateY(30px) scale(0.94);
  opacity: 0;
  transition: transform 1.1s var(--ease-out), opacity .9s var(--ease), filter 1.2s var(--ease);
  scrollbar-width: none;
}
.letter::-webkit-scrollbar { display: none; }
.scene--letter.is-active .letter { transform: none; opacity: 1; }
.letter.is-leaving { transform: scale(0.02); opacity: 0; filter: blur(12px); transition-duration: 1.4s; }
.letter__head {
  margin: 0 0 12px;
  font-family: var(--hand);
  font-size: 22px;
  color: var(--seal);
}
.letter__body p {
  margin: 0 0 12px;
  font-family: var(--hand);
  font-size: clamp(18px, 4.9vw, 21px);
  line-height: 1.42;
  color: var(--ink);
  opacity: 0;
  filter: blur(4px);
  transform: translateY(4px);
  transition: opacity 1.1s var(--ease), filter 1.1s var(--ease), transform 1.1s var(--ease);
}
.letter__body p.is-in { opacity: 1; filter: none; transform: none; }
.letter__sign {
  margin: 4px 0 0;
  text-align: right;
  font-family: var(--hand);
  font-size: 24px;
  color: var(--seal-deep);
  opacity: 0;
  transition: opacity 1.2s var(--ease);
}
.letter__sign.is-in { opacity: 1; }
.scene--letter .pill { margin-top: 18px; }

/* ---------- 4–6. поверх холста ---------- */
.galaxy-ui, .portrait-ui, .heart-ui {
  position: absolute;
  left: 0; right: 0;
  bottom: max(34px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 0 20px;
}
.galaxy-ui .hint { margin: 0; }
.galaxy-ui .pill { margin-top: 14px; }

.portrait__name, .portrait__line, .heart__line, .heart__sign {
  margin: 0;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 1.4s var(--ease), transform 1.4s var(--ease);
}
.portrait__name {
  font-family: var(--hand);
  font-size: clamp(44px, 13vw, 64px);
  line-height: 1;
  color: var(--star);
  text-shadow: 0 0 30px rgba(184, 166, 255, 0.55);
}
.portrait__line {
  margin-top: 6px;
  font-family: var(--hand);
  font-size: clamp(19px, 5.4vw, 24px);
  color: var(--lav);
}
.portrait-ui .pill { margin-top: 16px; }
.portrait-ui .hint, .heart-ui .hint { margin-top: 12px; }
.heart-ui .pill { margin-top: 14px; }
.heart__line {
  max-width: 30ch;
  font-family: var(--serif);
  font-style: italic;
  font-size: clamp(18px, 5vw, 22px);
  line-height: 1.35;
  color: var(--star);
}
.heart__sign {
  margin-top: 8px;
  font-family: var(--hand);
  font-size: clamp(24px, 7vw, 30px);
  color: var(--lav);
}
.is-shown .portrait__name, .is-shown .portrait__line,
.is-shown .heart__line, .is-shown .heart__sign { opacity: 1; transform: none; }
.portrait__line, .heart__sign { transition-delay: .5s; }

/* ---------- подсказка над словом ---------- */
.word-tip {
  position: fixed;
  left: 0; top: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 14px 9px;
  border-radius: 12px;
  background: rgba(12, 14, 31, 0.78);
  border: 1px solid rgba(184, 166, 255, 0.35);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  box-shadow: 0 12px 30px -12px rgba(0, 0, 0, 0.8);
  pointer-events: none;
  translate: -50% calc(-100% - 14px);
  animation: tip-in .35s var(--ease-out);
}
.word-tip__word { font-family: var(--sans); font-size: 16px; color: #fff; white-space: nowrap; }
.word-tip__lang { font-family: var(--serif); font-style: italic; font-size: 14px; color: var(--lav); }
@keyframes tip-in { from { opacity: 0; transform: translateY(6px) scale(.96); } }

/* ---------- звук ---------- */
.sound {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  z-index: 11;
  width: 38px; height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: rgba(238, 234, 255, 0.7);
  background: rgba(238, 234, 255, 0.06);
  border: 1px solid rgba(238, 234, 255, 0.16);
}
.sound svg { width: 18px; height: 18px; }
/* Атрибут `hidden` проигрывает любому `display` в классе — возвращаем ему силу. */
.sound[hidden], .word-tip[hidden] { display: none; }
.sound[aria-pressed="false"] .sound__wave { opacity: 0; }

@media (prefers-reduced-motion: reduce) {
  .envelope, .pill--glow, .hint.is-in { animation: none; }
  *, *::before, *::after { transition-duration: .01ms !important; }
}
