:root {
  --navy: #2b4c8c;
  --navy-2: #22407a;
  --blue: #2a4c8c;
  --blue-2: #355fa8;
  --sky: #a5c3dc;
  --sky-2: #d9e8f4;
  --white: #ffffff;
  --wine: #7a2a3e;
  --wine-2: #973a50;
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, sans-serif;
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --ease-snap: cubic-bezier(.2, .9, .25, 1.08);
  --drop: 220px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--white);
  background: var(--navy);
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
img { display: block; max-width: 100%; height: auto; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- خلفية ---------- */
.bg { position: fixed; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }
.bg-glow {
  position: absolute; inset: -20%;
  background:
    radial-gradient(60% 45% at 50% 42%, rgba(96, 140, 206, .5), transparent 70%),
    radial-gradient(40% 30% at 15% 90%, rgba(30, 58, 112, .55), transparent 70%),
    radial-gradient(35% 30% at 90% 10%, rgba(30, 58, 112, .45), transparent 70%);
  transition: opacity 1.2s;
}
.bg-arch {
  position: absolute; left: 50%; bottom: -62vmin; width: 150vmin; height: 150vmin;
  transform: translateX(-50%);
  border-radius: 50%;
  border: 14vmin solid rgba(255, 255, 255, .035);
  border-bottom-color: transparent;
  box-shadow: inset 0 0 0 4vmin rgba(165, 195, 220, .04);
  transition: transform 2s var(--ease-out), opacity 2s;
}
.bg-grain {
  position: absolute; inset: 0; opacity: .07; mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}
.app[data-state="done"] ~ .bg .bg-arch { transform: translateX(-50%) scale(1.08); opacity: 1.4; }

/* ---------- الهيكل ---------- */
.app {
  position: relative; z-index: 1;
  width: min(100%, 560px);
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(18px, env(safe-area-inset-top)) 20px max(16px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 8px;
}

.brand { display: flex; justify-content: center; padding-top: 6px; }
.brand img { width: clamp(118px, 32vw, 150px); filter: drop-shadow(0 6px 24px rgba(0, 0, 0, .35)); animation: fade-down .9s var(--ease-out) both; }

/* ---------- المسرح ---------- */
.stage {
  position: relative;
  height: clamp(380px, 52dvh, 470px);
  transition: height .9s var(--ease-out);
}
.app[data-state="done"] .stage { height: clamp(225px, 31dvh, 260px); }

.slot, .piece {
  position: absolute; left: 50%; top: 6%;
  width: clamp(160px, 44vw, 196px); aspect-ratio: 1;
  transform: translateX(-50%);
}

/* الفراغ الناقص */
.slot { animation: fade-up 1s .25s var(--ease-out) both; transition: opacity .5s, transform .5s; }
.slot-svg { width: 100%; height: 100%; overflow: visible; }
.slot-svg use {
  fill: rgba(165, 195, 220, .05);
  stroke: var(--sky); stroke-width: 1.2; stroke-dasharray: 4 5; stroke-linejoin: round;
  opacity: .55;
  animation: dash 6s linear infinite;
}
.slot-label {
  position: absolute; left: 50%; bottom: -8px; transform: translate(-50%, 100%);
  font-size: 12px; font-weight: 300; letter-spacing: .04em; color: var(--sky); opacity: .7; white-space: nowrap;
}
.app[data-state="snap"] .slot, .app[data-state="done"] .slot { opacity: 0; transform: translateX(-50%) scale(1.15); }

/* القطعة */
.piece {
  opacity: 0;
  transform: translate(-50%, var(--drop)) scale(.12) rotate(-40deg);
  filter: drop-shadow(0 0 0 rgba(165, 195, 220, 0));
  will-change: transform, opacity, filter;
  z-index: 3;
}
.piece-svg { width: 100%; height: 100%; overflow: visible; }
.app[data-state="rising"] .piece,
.app[data-state="snap"] .piece,
.app[data-state="done"] .piece {
  opacity: 1;
  transform: translate(-50%, 0) scale(1) rotate(0deg);
  filter: drop-shadow(0 18px 40px rgba(0, 0, 0, .4)) drop-shadow(0 0 24px rgba(255, 255, 255, .5));
  transition: transform 1.25s var(--ease-snap), opacity .35s ease-out, filter 1.25s;
}
.app[data-state="snap"] .piece,
.app[data-state="done"] .piece {
  transform: translate(-50%, 0) scale(1.08) rotate(0deg);
  transition: transform .45s var(--ease-snap), filter .6s;
}
.app[data-state="done"] .piece { animation: float 5s ease-in-out 1s infinite; }

.piece-ring {
  position: absolute; inset: -6%; border-radius: 50%;
  border: 2px solid var(--sky-2);
  opacity: 0; transform: scale(.6); pointer-events: none;
}
.app[data-state="snap"] .piece-ring, .app[data-state="done"] .piece-ring { animation: ring .9s var(--ease-out) both; }

/* ---------- الصندوق ---------- */
.box {
  --s: clamp(100px, 27vw, 124px);
  --h: calc(var(--s) / 2);
  position: absolute; left: 50%; bottom: 25%;
  width: var(--s); height: var(--s);
  transform: translateX(-50%);
  background: none; border: 0; padding: 0;
  perspective: 900px;
  animation: fade-up 1s .4s var(--ease-out) both;
  transition: opacity .7s, transform .9s var(--ease-out);
  z-index: 2;
  touch-action: manipulation;
}
.box:focus-visible { outline: 2px solid var(--sky); outline-offset: 14px; border-radius: 8px; }
.app[data-state="snap"] .box, .app[data-state="done"] .box { opacity: 0; transform: translateX(-50%) translateY(40px) scale(.8); pointer-events: none; }

.cube {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transform: rotateX(-28deg) rotateY(-38deg);
  animation: hover-cube 4s ease-in-out infinite;
}
.app[data-state="opening"] .cube { animation: shake .55s ease-in-out both; }
.app[data-state="rising"] .cube { animation: none; transform: rotateX(-28deg) rotateY(-38deg); }

.face {
  position: absolute; inset: 0;
  background: linear-gradient(160deg, #1b2f5e, #0f1b3d 70%);
  border: 1px solid rgba(165, 195, 220, .55);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .25), inset 0 0 40px rgba(53, 95, 168, .25);
  backface-visibility: hidden;
}
.face::after {
  content: ""; position: absolute; inset: 10%;
  border: 1px solid rgba(165, 195, 220, .18);
}
.front  { transform: translateZ(var(--h)); }
.back   { transform: rotateY(180deg) translateZ(var(--h)); }
.right  { transform: rotateY(90deg) translateZ(var(--h)); background: linear-gradient(160deg, #15264f, #0b1430 70%); }
.left   { transform: rotateY(-90deg) translateZ(var(--h)); background: linear-gradient(160deg, #22407a, #142653 70%); }
.bottom { transform: rotateX(-90deg) translateZ(var(--h)); }

.lid {
  transform: rotateX(90deg) translateZ(var(--h));
  transform-style: preserve-3d;
  background: none; border: 0; box-shadow: none;
}
.lid::after { display: none; }
.lid-inner {
  position: absolute; inset: 0;
  background: linear-gradient(135deg, #3a63ab, #22407a 60%, #1a3263);
  border: 1px solid rgba(217, 232, 244, .7);
  box-shadow: inset 0 0 30px rgba(165, 195, 220, .25);
  transform-origin: 50% 0%;
  transition: transform 1s var(--ease-out);
  backface-visibility: visible;
}
.lid-inner::before {
  content: ""; position: absolute; inset: 12%;
  border: 1px solid rgba(217, 232, 244, .35);
}
.lid-inner::after {
  content: ""; position: absolute; left: 50%; top: 50%; width: 22%; height: 22%;
  transform: translate(-50%, -50%);
  border: 1.5px solid var(--sky-2); border-radius: 50%;
  box-shadow: 0 0 14px rgba(217, 232, 244, .6);
}
.app[data-state="opening"] .lid-inner,
.app[data-state="rising"] .lid-inner,
.app[data-state="snap"] .lid-inner,
.app[data-state="done"] .lid-inner { transform: rotateX(118deg); }

.light {
  position: absolute; left: 50%; top: 50%; width: 180%; height: 180%;
  transform: translate(-50%, -50%) scale(0);
  transform-style: flat;
  background: radial-gradient(closest-side, rgba(255, 255, 255, .95), rgba(217, 232, 244, .55) 30%, rgba(165, 195, 220, 0) 70%);
  border-radius: 50%; opacity: 0; pointer-events: none;
  transition: transform .9s var(--ease-out), opacity .5s;
}
.app[data-state="opening"] .light, .app[data-state="rising"] .light { opacity: 1; transform: translate(-50%, -50%) scale(1); }

.box-shadow {
  position: absolute; left: 50%; bottom: -22%; width: 150%; height: 26%;
  transform: translateX(-50%);
  background: radial-gradient(closest-side, rgba(165, 195, 220, .5), rgba(165, 195, 220, 0));
  filter: blur(6px);
  animation: hover-shadow 4s ease-in-out infinite;
}
.tap {
  position: absolute; right: -12%; top: -14%; width: 34px; height: 34px;
  border-radius: 50%; border: 1.5px solid var(--sky-2);
  animation: tap 1.8s ease-out infinite;
}
.tap::after { content: ""; position: absolute; inset: 10px; border-radius: 50%; background: var(--sky-2); }
.app:not([data-state="idle"]) .tap { display: none; }

.hint {
  position: absolute; left: 0; right: 0; bottom: 0; margin: 0;
  text-align: center; line-height: 1.5;
  animation: fade-up 1s .6s var(--ease-out) both;
  transition: opacity .4s, transform .4s;
}
.hint-title { display: block; font-size: clamp(22px, 6vw, 28px); font-weight: 700; letter-spacing: -.01em; }
.hint-sub { display: block; font-size: 14px; font-weight: 300; color: var(--sky); }
.app:not([data-state="idle"]) .hint { opacity: 0; transform: translateY(10px); }
/* إيقاف أنيميشن الدخول حتى لا تتغلب على حالات ما بعد الفتح */
.app:not([data-state="idle"]) .hint,
.app:not([data-state="idle"]) .box,
.app:not([data-state="idle"]) .slot { animation: none; }

/* ---------- الكشف ---------- */
.reveal { display: none; text-align: center; }
.app[data-state="done"] .reveal { display: block; }
.app[data-state="done"] .reveal > * { animation: fade-up .8s var(--ease-out) both; }
.app[data-state="done"] .reveal > :nth-child(1) { animation-delay: .05s; }
.app[data-state="done"] .reveal > :nth-child(2) { animation-delay: .18s; }
.app[data-state="done"] .reveal > :nth-child(3) { animation-delay: .32s; }
.app[data-state="done"] .reveal > :nth-child(4) { animation-delay: .44s; }
.app[data-state="done"] .reveal > :nth-child(5) { animation-delay: .6s; }

.badge {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 16px; border-radius: 999px;
  background: var(--wine); color: var(--white);
  font-size: 12px; font-weight: 700; letter-spacing: .12em;
  box-shadow: 0 8px 24px rgba(122, 42, 62, .45);
}
.badge svg { background: var(--white); color: var(--wine); border-radius: 50%; padding: 2px; }
.headline {
  margin: 14px 0 4px;
  font-size: clamp(34px, 9.5vw, 48px); font-weight: 700; line-height: 1.25; letter-spacing: -.01em;
  text-shadow: 0 8px 30px rgba(0, 0, 0, .35);
}
.lead { margin: 0; font-size: clamp(17px, 4.6vw, 21px); font-weight: 500; color: var(--sky-2); }
.sub { margin: 10px 0 0; font-size: 15px; font-weight: 300; color: var(--sky); line-height: 1.7; }
.sub strong { font-weight: 500; color: var(--white); }
.sub .en { display: block; font-size: 13px; letter-spacing: .06em; opacity: .8; }

.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 22px; }
.btn {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 50px; padding: 0 22px; border-radius: 999px; border: 0;
  font-size: 16px; font-weight: 700;
  transition: transform .2s, box-shadow .2s, background .2s;
}
.btn:active { transform: scale(.97); }
.btn[disabled] { opacity: .6; pointer-events: none; }
.btn.primary {
  background: linear-gradient(135deg, var(--wine-2), var(--wine));
  box-shadow: 0 12px 30px rgba(122, 42, 62, .5), inset 0 1px 0 rgba(255, 255, 255, .2);
}
.btn.primary:hover { box-shadow: 0 16px 36px rgba(122, 42, 62, .6); transform: translateY(-1px); }
.btn.ghost { background: rgba(165, 195, 220, .1); border: 1px solid rgba(165, 195, 220, .45); font-weight: 500; }
.btn.ghost:hover { background: rgba(165, 195, 220, .18); }
.tag { margin: 14px 0 0; font-size: 12px; font-weight: 300; color: var(--sky); opacity: .7; letter-spacing: .02em; }

/* ---------- الفوتر ---------- */
.foot { display: flex; justify-content: center; padding-top: 18px; border-top: 1px solid rgba(165, 195, 220, .15); margin-top: 10px; }
.foot img { width: min(100%, 340px); opacity: .9; animation: fade-up 1s .9s var(--ease-out) both; }

/* ---------- تأثيرات ---------- */
#fx { position: fixed; inset: 0; z-index: 5; pointer-events: none; width: 100%; height: 100%; }
.toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 10;
  transform: translate(-50%, 20px); opacity: 0;
  padding: 10px 18px; border-radius: 999px;
  background: rgba(255, 255, 255, .95); color: var(--navy);
  font-size: 14px; font-weight: 500; white-space: nowrap;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .35);
  transition: opacity .3s, transform .3s; pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes fade-up { from { opacity: 0; transform: translateY(18px); } to { opacity: 1; transform: translateY(0); } }
@keyframes fade-down { from { opacity: 0; transform: translateY(-14px); } to { opacity: 1; transform: translateY(0); } }
@keyframes dash { to { stroke-dashoffset: -90; } }
@keyframes hover-cube {
  0%, 100% { transform: rotateX(-28deg) rotateY(-38deg) translateY(0); }
  50% { transform: rotateX(-28deg) rotateY(-38deg) translateY(-8px); }
}
@keyframes hover-shadow { 0%, 100% { transform: translateX(-50%) scale(1); opacity: 1; } 50% { transform: translateX(-50%) scale(.85); opacity: .7; } }
@keyframes shake {
  0% { transform: rotateX(-28deg) rotateY(-38deg); }
  20% { transform: rotateX(-26deg) rotateY(-44deg) translateX(-3px); }
  40% { transform: rotateX(-31deg) rotateY(-32deg) translateX(3px); }
  60% { transform: rotateX(-25deg) rotateY(-42deg) translateX(-2px); }
  80% { transform: rotateX(-30deg) rotateY(-35deg) translateX(2px); }
  100% { transform: rotateX(-28deg) rotateY(-38deg); }
}
@keyframes tap { 0% { transform: scale(.6); opacity: 0; } 30% { opacity: 1; } 100% { transform: scale(1.5); opacity: 0; } }
@keyframes ring { 0% { opacity: .9; transform: scale(.6); } 100% { opacity: 0; transform: scale(1.5); } }
@keyframes float { 0%, 100% { transform: translate(-50%, 0) scale(1.08); } 50% { transform: translate(-50%, -8px) scale(1.08); } }

/* الفوتر يلتصق بالأسفل عند الصفحات الطويلة */
@media (min-width: 600px) {
  .app { padding-top: 32px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .2s !important; }
  .slot, .box, .hint, .brand img, .foot img { animation: none; opacity: 1; }
  .cube { animation: none; transform: rotateX(-28deg) rotateY(-38deg); }
}
