:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #070708;
  color: #f8f4eb;
  --frame-rotate-x: 0deg;
  --frame-rotate-y: 0deg;
  --light-x: 50%;
  --light-y: 44%;
}

* { box-sizing: border-box; }

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

body {
  min-height: 100svh;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 46%, rgba(110, 52, 153, .2), transparent 36%),
    radial-gradient(circle at 16% 15%, rgba(194, 121, 35, .12), transparent 27%),
    linear-gradient(155deg, #111013 0%, #09090b 46%, #050506 100%);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 4;
  background: radial-gradient(ellipse at center, transparent 44%, rgba(0, 0, 0, .58) 100%);
}

body::after {
  content: "";
  position: fixed;
  inset: -20%;
  z-index: 2;
  pointer-events: none;
  opacity: 0;
  background:
    repeating-conic-gradient(from 10deg at 50% 50%, transparent 0 8deg, rgba(221, 151, 59, .09) 9deg 10deg, transparent 11deg 21deg),
    radial-gradient(circle at 50% 48%, rgba(142, 55, 206, .34), transparent 34%),
    radial-gradient(circle at 50% 48%, transparent 0 27%, rgba(247, 181, 74, .2) 28%, transparent 31%);
  filter: blur(2px) saturate(1.35);
  transform: scale(.82) rotate(-7deg);
  transition: opacity 500ms ease, transform 4s cubic-bezier(.16, 1, .3, 1);
}

body.rift-open::after {
  opacity: 1;
  transform: scale(1.16) rotate(17deg);
  animation: rift-pulse .82s ease-in-out infinite alternate;
}

#atmosphere,
.ambient-light {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.corner-sigil {
  position: fixed;
  z-index: 3;
  width: clamp(42px, 6vw, 78px);
  aspect-ratio: 1;
  pointer-events: none;
  opacity: .3;
  border: 1px solid rgba(241, 190, 105, .52);
  border-radius: 50%;
  background:
    linear-gradient(45deg, transparent 46%, rgba(236, 178, 91, .42) 47% 49%, transparent 50%),
    linear-gradient(-45deg, transparent 46%, rgba(151, 81, 203, .4) 47% 49%, transparent 50%),
    radial-gradient(circle, transparent 0 27%, rgba(239, 187, 102, .25) 28% 30%, transparent 31% 100%);
  box-shadow: inset 0 0 18px rgba(146, 63, 198, .2), 0 0 18px rgba(221, 143, 51, .08);
  animation: sigil-turn 24s linear infinite;
}

.corner-sigil::before,
.corner-sigil::after {
  content: "";
  position: absolute;
  inset: 16%;
  border: 1px solid rgba(168, 101, 214, .5);
  transform: rotate(45deg);
}

.corner-sigil::after {
  inset: 31%;
  border-color: rgba(255, 213, 139, .65);
  border-radius: 50%;
}

.corner-sigil-one { top: max(1rem, env(safe-area-inset-top)); left: 1rem; }
.corner-sigil-two { top: max(1rem, env(safe-area-inset-top)); right: 1rem; animation-direction: reverse; }
.corner-sigil-three { bottom: max(1rem, env(safe-area-inset-bottom)); left: 1rem; animation-direction: reverse; }
.corner-sigil-four { bottom: max(1rem, env(safe-area-inset-bottom)); right: 1rem; }

#atmosphere { z-index: 1; }

.ambient-light {
  z-index: 0;
  opacity: .72;
  background:
    conic-gradient(from 215deg at 50% 50%, transparent, rgba(99, 39, 138, .11), transparent 35%),
    radial-gradient(circle at var(--light-x) var(--light-y), rgba(232, 162, 67, .12), transparent 30%);
  filter: blur(30px);
}

.page-shell {
  position: relative;
  z-index: 5;
  min-height: 100svh;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: clamp(1rem, 2.4vw, 2rem);
  padding: max(clamp(1rem, 2.5vw, 2rem), env(safe-area-inset-top)) clamp(1rem, 3vw, 2.5rem);
  padding-bottom: max(clamp(1.25rem, 3vw, 2.5rem), env(safe-area-inset-bottom));
}

.image-stage {
  min-height: 0;
  display: grid;
  place-items: center;
  perspective: 1300px;
}

.frame-wrap {
  position: relative;
  display: grid;
  place-items: center;
  width: min(100%, 1400px);
  height: min(100%, 1400px);
  transform: perspective(1300px)
    rotateX(var(--frame-rotate-x))
    rotateY(var(--frame-rotate-y));
  transform-style: preserve-3d;
  transition: transform 600ms cubic-bezier(.22, 1, .36, 1);
}

.frame-wrap::before,
.frame-wrap::after {
  content: "";
  position: absolute;
  pointer-events: none;
}

.frame-wrap::before {
  inset: 11% 8%;
  z-index: -1;
  border-radius: 38%;
  background: radial-gradient(circle, rgba(131, 65, 174, .42), rgba(220, 130, 42, .17) 42%, transparent 72%);
  filter: blur(clamp(26px, 5vw, 70px));
  transform: translateZ(-50px) scale(.96);
}

.frame-wrap::after {
  width: 70%;
  height: 8%;
  left: 15%;
  bottom: 2%;
  z-index: -2;
  border-radius: 50%;
  background: rgba(0, 0, 0, .7);
  filter: blur(22px);
  transform: translateY(14px) translateZ(-80px);
}

.frame-wrap img {
  position: relative;
  z-index: 2;
  display: block;
  width: min(100%, 1400px);
  height: min(100%, 1400px);
  object-fit: contain;
  object-position: center;
  filter:
    drop-shadow(0 18px 16px rgba(0, 0, 0, .38))
    drop-shadow(0 0 26px rgba(220, 145, 55, .09));
  animation: frame-breathe 9s ease-in-out infinite;
}

.reality-rings {
  position: absolute;
  z-index: 1;
  inset: -7% -12%;
  pointer-events: none;
  transform: translateZ(-20px);
}

.reality-rings span {
  position: absolute;
  inset: 4%;
  border: 1px solid rgba(232, 175, 90, .32);
  border-radius: 50%;
  box-shadow: 0 0 18px rgba(134, 52, 190, .16), inset 0 0 14px rgba(231, 159, 70, .08);
  animation: orbit-turn 19s linear infinite;
}

.reality-rings span::before,
.reality-rings span::after {
  content: "";
  position: absolute;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #ffe1a0;
  box-shadow: 0 0 12px #f1a542, 0 0 22px rgba(144, 63, 207, .8);
}

.reality-rings span::before { left: 12%; top: 13%; }
.reality-rings span::after { right: 10%; bottom: 18%; }

.reality-rings span:nth-child(2) {
  inset: 10% -1%;
  border-color: rgba(156, 84, 211, .36);
  transform: rotate(62deg);
  animation-duration: 27s;
  animation-direction: reverse;
}

.reality-rings span:nth-child(3) {
  inset: 17% -5%;
  border-style: dashed;
  border-color: rgba(242, 206, 148, .2);
  transform: rotate(-37deg);
  animation-duration: 34s;
}

body.rift-open .reality-rings span {
  border-color: rgba(255, 215, 143, .8);
  box-shadow: 0 0 24px rgba(157, 65, 225, .55), inset 0 0 20px rgba(239, 169, 76, .3);
  animation-duration: 1.15s;
}

body.rift-open .frame-wrap img {
  filter: drop-shadow(0 25px 20px rgba(0, 0, 0, .4)) drop-shadow(0 0 42px rgba(231, 154, 60, .34)) saturate(1.16);
}

footer {
  position: relative;
  z-index: 6;
  min-height: 3.8rem;
  text-align: center;
  font-family: "Iowan Old Style", Baskerville, "Times New Roman", serif;
  font-size: clamp(1.15rem, 2vw, 1.6rem);
  line-height: 1.2;
  letter-spacing: .075em;
  font-weight: 800;
  color: #050506;
  -webkit-text-stroke: 2px #fff;
  paint-order: stroke fill;
  text-shadow: 0 0 18px rgba(244, 184, 92, .28), 0 3px 12px rgba(0, 0, 0, .65);
}

footer::before {
  content: "";
  display: block;
  width: min(13rem, 48vw);
  height: 2px;
  margin: 0 auto clamp(.9rem, 2vw, 1.25rem);
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, .95) 18%, #f5c878 50%, rgba(255, 255, 255, .95) 82%, transparent);
  box-shadow: 0 0 12px rgba(231, 170, 77, .38);
}

.drift-letter {
  display: inline-block;
  transform-origin: 50% 60%;
}

.familiar {
  --pet-x: 24px;
  --pet-y: calc(100svh - 150px);
  --face: 1;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 30;
  width: 76px;
  height: 82px;
  margin: 0;
  padding: 0;
  border: 0;
  color: inherit;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
  transform: translate3d(var(--pet-x), var(--pet-y), 0);
  transition: transform var(--travel-time, 6s) cubic-bezier(.37, .02, .21, 1);
  -webkit-tap-highlight-color: transparent;
}

.familiar:focus-visible {
  outline: 2px solid #f5c878;
  outline-offset: 5px;
  border-radius: 18px;
}

.familiar-shadow {
  position: absolute;
  left: 13px;
  bottom: 4px;
  width: 52px;
  height: 12px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .5);
  filter: blur(4px);
}

.familiar-creature {
  position: absolute;
  inset: 0;
  transform: scaleX(var(--face));
  transform-origin: center bottom;
  filter: drop-shadow(0 0 9px rgba(245, 190, 91, .55));
}

.familiar-body {
  position: absolute;
  left: 18px;
  top: 35px;
  width: 41px;
  height: 27px;
  border: 1px solid #ffdc93;
  border-radius: 52% 58% 45% 48%;
  background:
    radial-gradient(circle at 68% 28%, #fff2be 0 8%, transparent 9%),
    linear-gradient(135deg, #ffdfa0 0%, #c27d24 42%, #683a12 100%);
  box-shadow: inset -6px -5px 8px rgba(75, 36, 7, .42), inset 5px 3px 7px rgba(255, 244, 199, .28);
}

.familiar-head {
  position: absolute;
  left: 44px;
  top: 25px;
  width: 23px;
  height: 25px;
  border: 1px solid #ffdc93;
  border-radius: 55% 58% 48% 50%;
  background: linear-gradient(145deg, #ffe5a7, #b86f1d 72%);
  transform: rotate(8deg);
}

.familiar-head::before,
.familiar-head::after {
  content: "";
  position: absolute;
  top: -5px;
  width: 9px;
  height: 12px;
  background: #c9832c;
  clip-path: polygon(50% 0, 100% 100%, 0 82%);
}

.familiar-head::before { left: 1px; transform: rotate(-22deg); }
.familiar-head::after { right: 1px; transform: rotate(22deg); }

.familiar-eye {
  position: absolute;
  right: 4px;
  top: 9px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #0a0710;
  box-shadow: 0 0 0 1px #fff3b8, 0 0 7px #fff0a0;
}

.familiar-horn {
  position: absolute;
  left: 55px;
  top: 11px;
  width: 7px;
  height: 19px;
  background: linear-gradient(90deg, #8d4e11, #fff0ae 55%, #bd761d);
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
  transform: rotate(20deg);
  transform-origin: bottom center;
}

.familiar-tail {
  position: absolute;
  left: 7px;
  top: 37px;
  width: 22px;
  height: 18px;
  border-left: 5px solid #d9902e;
  border-bottom: 4px solid #f0b851;
  border-radius: 0 0 0 80%;
  transform: rotate(20deg);
  transform-origin: right center;
}

.familiar-leg {
  position: absolute;
  top: 57px;
  width: 5px;
  height: 15px;
  border-radius: 0 0 3px 3px;
  background: linear-gradient(#b76d1e, #f5c675);
  transform-origin: top center;
}

.familiar-leg-front { left: 49px; }
.familiar-leg-back { left: 26px; }

.familiar-bubble {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  min-width: max-content;
  max-width: 170px;
  padding: .5rem .72rem;
  border: 1px solid rgba(245, 200, 120, .68);
  border-radius: 12px 12px 12px 3px;
  color: #f9e9c8;
  background: rgba(15, 11, 17, .92);
  box-shadow: 0 9px 28px rgba(0, 0, 0, .38), 0 0 20px rgba(222, 150, 60, .11);
  font: 600 .72rem/1.25 Inter, ui-sans-serif, system-ui, sans-serif;
  letter-spacing: .02em;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 7px) scale(.96);
  transition: opacity 220ms ease, transform 220ms ease;
}

.familiar.is-speaking .familiar-bubble {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}

.familiar[data-moving="true"] .familiar-creature {
  animation: familiar-step .58s ease-in-out infinite alternate;
}

.familiar[data-moving="true"] .familiar-leg-front { animation: leg-step .58s ease-in-out infinite alternate; }
.familiar[data-moving="true"] .familiar-leg-back { animation: leg-step .58s ease-in-out infinite alternate-reverse; }
.familiar[data-moving="true"] .familiar-tail { animation: tail-wag .72s ease-in-out infinite alternate; }

.spark {
  position: fixed;
  z-index: 24;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  pointer-events: none;
  background: #ffe7a6;
  box-shadow: 0 0 7px #f4a83f, 0 0 14px rgba(180, 80, 255, .45);
  animation: spark-away 1.65s ease-out forwards;
}

.trail-spark {
  position: fixed;
  z-index: 22;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  pointer-events: none;
  background: #f7dca0;
  box-shadow: 0 0 8px #f0a33b, 0 0 18px rgba(151, 67, 220, .7);
  animation: trail-vanish .9s ease-out forwards;
}

.meteor {
  position: fixed;
  z-index: 2;
  width: clamp(80px, 12vw, 170px);
  height: 2px;
  pointer-events: none;
  border-radius: 999px;
  background: linear-gradient(90deg, transparent, rgba(163, 80, 220, .45), #ffe7ad 86%, #fff 100%);
  box-shadow: 0 0 8px rgba(245, 182, 82, .8);
  transform: rotate(-28deg);
  animation: meteor-crossing var(--meteor-time, 2.8s) cubic-bezier(.16, .7, .2, 1) forwards;
}

.shockwave {
  position: fixed;
  z-index: 28;
  width: 24px;
  height: 24px;
  margin: -12px 0 0 -12px;
  border: 2px solid rgba(255, 220, 153, .9);
  border-radius: 50%;
  pointer-events: none;
  box-shadow: 0 0 24px rgba(160, 70, 226, .8), inset 0 0 18px rgba(245, 166, 66, .45);
  animation: shockwave-open 1.25s cubic-bezier(.12, .74, .18, 1) forwards;
}

@keyframes frame-breathe {
  0%, 100% { transform: translateY(0) scale(.998); filter: drop-shadow(0 18px 16px rgba(0, 0, 0, .38)) drop-shadow(0 0 26px rgba(220, 145, 55, .08)); }
  50% { transform: translateY(-5px) scale(1); filter: drop-shadow(0 23px 20px rgba(0, 0, 0, .34)) drop-shadow(0 0 34px rgba(220, 145, 55, .14)); }
}

@keyframes sigil-turn {
  to { transform: rotate(360deg); }
}

@keyframes orbit-turn {
  to { transform: rotate(360deg); }
}

@keyframes rift-pulse {
  from { filter: blur(2px) saturate(1.25) hue-rotate(-4deg); }
  to { filter: blur(4px) saturate(1.8) hue-rotate(9deg); }
}

@keyframes familiar-step {
  from { transform: scaleX(var(--face)) translateY(0) rotate(-1deg); }
  to { transform: scaleX(var(--face)) translateY(-4px) rotate(1deg); }
}

@keyframes leg-step {
  from { transform: rotate(-18deg); }
  to { transform: rotate(20deg); }
}

@keyframes tail-wag {
  from { transform: rotate(8deg); }
  to { transform: rotate(34deg); }
}

@keyframes spark-away {
  0% { opacity: 1; transform: translate3d(0, 0, 0) scale(1); }
  100% { opacity: 0; transform: translate3d(var(--spark-x), 28px, 0) scale(.2); }
}

@keyframes trail-vanish {
  from { opacity: .9; transform: translate3d(0, 0, 0) scale(1); }
  to { opacity: 0; transform: translate3d(var(--trail-x), var(--trail-y), 0) scale(.15); }
}

@keyframes meteor-crossing {
  0% { opacity: 0; transform: translate3d(0, 0, 0) rotate(-28deg) scaleX(.4); }
  12% { opacity: 1; }
  100% { opacity: 0; transform: translate3d(-115vw, 72vh, 0) rotate(-28deg) scaleX(1.2); }
}

@keyframes shockwave-open {
  0% { opacity: 1; transform: scale(.4); }
  100% { opacity: 0; transform: scale(18); }
}

@keyframes settle-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes letter-drift {
  0%, 6.25% { opacity: 1; transform: translate3d(0, 0, 0) rotate(0); }
  38% { opacity: .98; }
  70% { opacity: .75; }
  92% { opacity: 0; transform: translate3d(var(--drift-x), var(--drift-y), 0) rotate(var(--drift-r)); }
  96% { opacity: 0; transform: translate3d(0, 0, 0) rotate(0); }
  100% { opacity: 1; transform: translate3d(0, 0, 0) rotate(0); }
}

@media (prefers-reduced-motion: no-preference) {
  .image-stage img,
  footer { animation: settle-in 500ms cubic-bezier(.22, 1, .36, 1) both; }

  footer { animation-delay: 80ms; }

  .drift-letter {
    will-change: transform, opacity;
    animation: letter-drift 32s cubic-bezier(.34, .02, .22, 1) infinite;
  }
}

@media (max-width: 600px) {
  .page-shell { gap: .85rem; }
  footer {
    min-height: 3.4rem;
    letter-spacing: .085em;
    -webkit-text-stroke-width: 1.5px;
  }
  .frame-wrap::before { inset: 14% 2%; }
  .familiar { transform: translate3d(var(--pet-x), var(--pet-y), 0) scale(.88); }
  .corner-sigil { opacity: .18; }
  .corner-sigil-three,
  .corner-sigil-four { display: none; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .page-shell { gap: .5rem; padding-block: .75rem; }
  footer { min-height: 2.5rem; font-size: 1rem; }
  footer::before { margin-bottom: .55rem; }
}

@media (prefers-reduced-motion: reduce) {
  .frame-wrap,
  .frame-wrap img,
  .drift-letter,
  .familiar,
  .familiar-creature,
  .familiar-leg,
  .familiar-tail,
  .spark {
    animation: none !important;
    transition: none !important;
    transform: none;
  }

  #atmosphere { display: none; }
  .meteor,
  .trail-spark,
  .shockwave { display: none; }

  .corner-sigil,
  .reality-rings span { animation: none !important; }

  .familiar {
    top: auto;
    left: auto;
    right: max(1rem, env(safe-area-inset-right));
    bottom: max(4.5rem, env(safe-area-inset-bottom));
  }
}
