:root {
  --night: #1d0f24;
  --night-2: #3a1636;
  --rose: #f4a6b7;
  --rose-deep: #d9798f;
  --wine: #8b2a4a;
  --paper: #fff4ec;
  --paper-line: rgba(139, 42, 74, .09);
  --gold: #f1c27d;
  --ink: #4a1a33;
  --ok: #ff7aa2;
  --font-ui: "Cairo", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
  --font-hand: "Cairo", system-ui, -apple-system, "Segoe UI", Tahoma, sans-serif;
}

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

/* ضروري: display:flex كان بيلغي خاصية hidden */
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(120% 70% at 50% 0%, #5a1f4a 0%, transparent 60%),
    radial-gradient(90% 60% at 50% 110%, #7a2a52 0%, transparent 65%),
    linear-gradient(180deg, var(--night) 0%, var(--night-2) 100%);
  background-attachment: fixed;
  color: var(--paper);
  font-family: var(--font-ui);
  overflow-x: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- الخلفية ---------- */
.bg {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.star {
  position: absolute;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #ffe9f0;
  opacity: 0;
  animation: twinkle var(--d, 4s) ease-in-out var(--delay, 0s) infinite;
}

@keyframes twinkle {
  0%, 100% { opacity: 0; transform: scale(.6); }
  50% { opacity: .9; transform: scale(1.2); }
}

.float-heart {
  position: absolute;
  bottom: -40px;
  left: var(--x, 50%);
  font-size: var(--s, 18px);
  color: var(--c, var(--rose));
  opacity: 0;
  will-change: transform, opacity;
  animation: rise var(--dur, 14s) linear var(--delay, 0s) infinite;
}

@keyframes rise {
  0%   { transform: translate3d(0, 0, 0) rotate(-8deg); opacity: 0; }
  10%  { opacity: var(--o, .5); }
  50%  { transform: translate3d(var(--sway, 20px), -55vh, 0) rotate(8deg); }
  90%  { opacity: var(--o, .5); }
  100% { transform: translate3d(calc(var(--sway, 20px) * -1), -112vh, 0) rotate(-8deg); opacity: 0; }
}

/* ---------- الهيكل ---------- */
.app {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.gate {
  width: 100%;
  max-width: 440px;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 36px 22px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 34px;
  transition: opacity .8s ease, transform .8s ease;
}

.gate.leaving { opacity: 0; transform: translateY(-18px) scale(.98); pointer-events: none; }

/* ---------- اللغز ---------- */
.riddle {
  margin: 0;
  text-align: center;
  font-family: var(--font-hand);
  font-size: clamp(1.2rem, 5.2vw, 1.55rem);
  font-weight: 600;
  line-height: 2;
  color: #ffe3ec;
  text-shadow: 0 0 22px rgba(244, 166, 183, .35);
  animation: riddleIn 1.6s cubic-bezier(.2, .8, .2, 1) both;
}

@keyframes riddleIn {
  from { opacity: 0; transform: translateY(16px); filter: blur(6px); }
  to   { opacity: 1; transform: none; filter: blur(0); }
}

/* ---------- خانات كلمة السر ---------- */
.inputs {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  transition: opacity .6s ease, max-height .8s ease, margin .8s ease;
  max-height: 200px;
  animation: riddleIn 1.6s cubic-bezier(.2, .8, .2, 1) .5s both;
}

.inputs.done { opacity: 0; max-height: 0; margin: -17px 0; overflow: hidden; pointer-events: none; }

.field {
  position: relative;
  display: block;
}

.field input {
  width: 100%;
  height: 56px;
  padding: 0 52px 0 20px;
  font-family: var(--font-ui);
  font-size: 18px; /* 16px+ يمنع تكبير آيفون */
  letter-spacing: .06em;
  color: #fff;
  text-align: center;
  background: rgba(255, 255, 255, .07);
  border: 1.5px solid rgba(244, 166, 183, .35);
  border-radius: 18px;
  outline: none;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: border-color .3s, box-shadow .3s, background .3s;
  direction: ltr;
}

.field input:focus {
  border-color: var(--rose);
  box-shadow: 0 0 0 4px rgba(244, 166, 183, .15), 0 0 24px rgba(244, 166, 183, .25);
  background: rgba(255, 255, 255, .1);
}

.field.ok input {
  border-color: var(--ok);
  background: rgba(255, 122, 162, .14);
  box-shadow: 0 0 26px rgba(255, 122, 162, .45);
}

.field.wrong input { animation: shake .45s ease; border-color: rgba(255, 170, 170, .6); }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

.tick {
  position: absolute;
  top: 50%;
  right: 18px;
  transform: translateY(-50%) scale(0);
  color: var(--ok);
  font-size: 22px;
  line-height: 1;
  transition: transform .5s cubic-bezier(.3, 1.8, .5, 1);
}

.field.ok .tick { transform: translateY(-50%) scale(1); animation: beat 1.3s ease-in-out .5s infinite; }

@keyframes beat {
  0%, 100% { transform: translateY(-50%) scale(1); }
  50% { transform: translateY(-50%) scale(1.25); }
}

/* ---------- الظرف ---------- */
.envelope-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding-top: 40px;
}

.envelope-wrap:not([hidden]) { animation: envPop 1.2s cubic-bezier(.2, 1.2, .3, 1) both; }

@keyframes envPop {
  from { opacity: 0; transform: translateY(40px) scale(.7) rotate(-4deg); }
  to   { opacity: 1; transform: none; }
}

.envelope {
  position: relative;
  display: block;
  width: min(84vw, 340px);
  aspect-ratio: 4 / 3;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  perspective: 900px;
  filter: drop-shadow(0 18px 30px rgba(0, 0, 0, .45));
  animation: bob 3.6s ease-in-out infinite;
}

.envelope.open { animation: none; cursor: default; }

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-.6deg); }
  50% { transform: translateY(-8px) rotate(.6deg); }
}

.envelope > span { position: absolute; display: block; }

.env-back {
  inset: 0;
  background: #e9a0b0;
  border-radius: 10px;
}

.env-paper {
  left: 7%; right: 7%; top: 8%; bottom: 8%;
  background: var(--paper);
  border-radius: 6px;
  z-index: 2;
  transition: transform 1.1s cubic-bezier(.3, .9, .3, 1) .55s;
}

.envelope.open .env-paper { transform: translateY(-62%); }

.env-front {
  inset: 0;
  z-index: 3;
  border-radius: 10px;
  background: linear-gradient(160deg, #f3b6c4 0%, #e08ea2 100%);
  clip-path: polygon(0 100%, 0 0, 50% 56%, 100% 0, 100% 100%);
}

.env-flap {
  left: 0; right: 0; top: 0;
  height: 56%;
  z-index: 4;
  background: linear-gradient(180deg, #cf7a90 0%, #e49aac 100%);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  transform-origin: top center;
  transition: transform .8s cubic-bezier(.5, 0, .3, 1), z-index 0s .4s;
}

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

.env-seal {
  left: 50%; top: 56%;
  z-index: 5;
  width: 46px; height: 46px;
  margin: -23px 0 0 -23px;
  display: grid !important;
  place-items: center;
  font-size: 24px;
  color: #ffe8ee;
  background: radial-gradient(circle at 35% 30%, #c43a63, var(--wine));
  border-radius: 50%;
  box-shadow: 0 3px 8px rgba(0, 0, 0, .35), inset 0 -3px 6px rgba(0, 0, 0, .25);
  transition: opacity .3s, transform .4s;
  animation: beat2 1.6s ease-in-out infinite;
}

@keyframes beat2 {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

.envelope.open .env-seal { opacity: 0; transform: scale(.4); animation: none; }

.tap-hint {
  font-size: 30px;
  animation: tap 1.4s ease-in-out infinite;
  transition: opacity .3s;
}

.tap-hint.gone { opacity: 0; }

@keyframes tap {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-9px) scale(1.1); }
}

/* ---------- الرسالة ---------- */
.letter-stage {
  width: 100%;
  max-width: 480px;
  padding: 34px 16px 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 44px;
}

.letter-stage:not([hidden]) { animation: sheetIn 1.4s cubic-bezier(.2, .9, .3, 1) both; }

@keyframes sheetIn {
  from { opacity: 0; transform: translateY(60px) scale(.94); }
  to   { opacity: 1; transform: none; }
}

.sheet {
  position: relative;
  width: 100%;
  padding: 36px 24px 40px;
  color: var(--ink);
  background:
    repeating-linear-gradient(180deg, transparent 0, transparent 47px, var(--paper-line) 47px, var(--paper-line) 48px),
    radial-gradient(120% 80% at 50% 0%, #fffaf5 0%, var(--paper) 70%);
  border-radius: 12px;
  box-shadow: 0 22px 50px rgba(0, 0, 0, .45), 0 0 60px rgba(244, 166, 183, .18);
  font-family: var(--font-hand);
  font-size: clamp(1.05rem, 4.5vw, 1.25rem);
  font-weight: 500;
  line-height: 2.1;
  text-align: right;
}

.sheet::before {
  content: "";
  position: absolute;
  top: 0; bottom: 0; right: 14px;
  width: 1.5px;
  background: rgba(217, 121, 143, .35);
  border-radius: 2px;
}

.line {
  display: block;
  margin: 0 0 .55em;
  opacity: 0;
  transform: translateY(14px);
  filter: blur(4px);
  transition: opacity 1.1s ease, transform 1.1s cubic-bezier(.2, .8, .2, 1), filter 1.1s ease;
}

.line.show { opacity: 1; transform: none; filter: blur(0); }
.line:last-child { margin-bottom: 0; }

/* ---------- الجملة الأخيرة + الساعة ---------- */
.ending {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 10px 14px 30px;
  text-align: center;
  opacity: 0;
  transform: translateY(18px);
  transition: opacity 2s ease, transform 2s ease;
}

.ending.show { opacity: 1; transform: none; }

.ending-text {
  margin: 0;
  font-family: var(--font-hand);
  font-size: clamp(1.1rem, 4.8vw, 1.35rem);
  font-weight: 600;
  line-height: 2;
  color: #ffd5e0;
  text-shadow: 0 0 24px rgba(244, 166, 183, .55);
}

.clock { width: 58px; height: 58px; overflow: visible; filter: drop-shadow(0 0 10px rgba(241, 194, 125, .5)); }
.clock-face { fill: none; stroke: var(--gold); stroke-width: 2.4; }
.clock-dot { fill: var(--gold); }
.hand { stroke: var(--gold); stroke-linecap: round; transform-origin: 32px 32px; }
.hand-min { stroke-width: 2.6; animation: rewind 6s linear infinite; }
.hand-hour { stroke-width: 3.4; animation: rewind 72s linear infinite; }

@keyframes rewind {
  from { transform: rotate(0deg); }
  to   { transform: rotate(-360deg); }
}

/* ---------- قلوب الانفجار ---------- */
.burst {
  position: fixed;
  left: 50%; top: 50%;
  z-index: 10;
  pointer-events: none;
  font-size: var(--s, 20px);
  color: var(--c, var(--rose));
  opacity: 0;
  animation: burst 1.6s cubic-bezier(.1, .8, .3, 1) forwards;
}

@keyframes burst {
  0%   { opacity: 1; transform: translate(-50%, -50%) scale(.3); }
  100% { opacity: 0; transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(1.2) rotate(var(--r, 0deg)); }
}

/* شاشات أكبر (الأساس للموبايل) */
@media (min-width: 700px) {
  .gate { max-width: 480px; }
  .letter-stage { padding-top: 60px; }
}

/* تقليل الحركة */
@media (prefers-reduced-motion: reduce) {
  .float-heart, .star, .envelope, .tap-hint, .hand, .env-seal, .tick { animation: none !important; }
  .line, .ending { transition-duration: .01s; }
}
