/**
 * SERENITY BLOCKS - Cinematic Cosmic Intro
 * Premium deep-space reveal animation
 */

/* ===== CHROMADELIC COLOR PALETTE ===== */
:root {
    --intro-pink: #FF3366;
    --intro-cyan: #00FFFF;
    --intro-yellow: #FFFF00;
    --intro-purple: #9933FF;
    --intro-blue: #3399FF;
    --intro-magenta: #FF0099;
    --intro-orange: #FF6600;
    --intro-mint: #00FF66;
}

/* Intro Container - full screen. The world (First Light, src/ui/intro-world/) draws the whole
   picture and owns its grade; this background is what shows before its first frame, and the
   whole picture if no renderer could start (the same sky the loading screen paints). */
#intro-animation {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 10000;
    background:
        radial-gradient(130% 62% at 50% 118%, #093542 0%, #051f29 34%, transparent 72%),
        linear-gradient(180deg, #03040a 0%, #04060f 38%, #070a17 66%, #0a0f22 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: pointer;
    opacity: 1;
    transition: opacity 1s ease-out;
}

#intro-animation.fade-out {
    opacity: 0;
    pointer-events: none;
}

#intro-animation.hidden {
    display: none;
}

/* Background-only mode: lower z-index so start modal can appear on top */
#intro-animation.background-only {
    z-index: 1;
    pointer-events: none;
    /* Allow clicks to pass through to modal */
}

/* The world has its own stars; the CSS twinkle is only the picture when no renderer started. */
.intro-starfield {
    z-index: 2;
    opacity: 0.7;
    display: none;
}
#intro-animation.no-world .intro-starfield { display: block; }

.intro-warp-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
    z-index: 20;
    background:
        radial-gradient(circle at 50% 50%, rgba(140, 210, 255, 0.25), transparent 40%),
        repeating-linear-gradient(90deg,
            rgba(170, 225, 255, 0.0) 0px,
            rgba(170, 225, 255, 0.22) 2px,
            rgba(170, 225, 255, 0.0) 4px,
            rgba(170, 225, 255, 0.0) 10px);
    transform: scale(0.92);
    filter: blur(0.5px);
}

#intro-animation.warp-dismiss .intro-warp-overlay {
    animation: warpOverlayBurst 1.1s cubic-bezier(0.2, 0.7, 0.1, 1) forwards;
}

@keyframes warpOverlayBurst {
    0% {
        opacity: 0;
        transform: scale(0.92);
    }

    30% {
        opacity: 0.78;
        transform: scale(1.08);
    }

    100% {
        opacity: 0;
        transform: scale(1.25);
    }
}


/* Title Container */
.intro-title-container {
    position: relative;
    width: max-content;
    max-width: 92vw;
    z-index: 10;
}

/* Glitch animation keyframes (combined with titleReveal below) */

@keyframes glitchShift {

    0%,
    95%,
    100% {
        transform: scale(1) rotateX(0deg) translateZ(0);
        filter: blur(0px);
    }

    96% {
        transform: scale(1) rotateX(0deg) translateZ(0) translateX(-2px);
        filter: none;
    }

    97% {
        transform: scale(1) rotateX(0deg) translateZ(0) translateX(2px) skewX(1deg);
        filter: none;
    }

    98% {
        transform: scale(1) rotateX(0deg) translateZ(0) translateX(-1px);
        filter: none;
    }
}

/* The same vector artwork moves from the cover to this resting position. */
.intro-title {
    --intro-logo-width: var(--sb-logo-title-width);
    width: var(--intro-logo-width);
    position: relative;
    margin: 0;
    padding: 0;
    line-height: 0;
    animation: titleReveal 1.65s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    transform-origin: center;
}

.intro-wordmark {
    display: block;
    width: 100%;
    height: auto;
    transform-origin: center;
    /* The artwork pulses independently of the title's reveal and menu positioning. */
    animation: sb-wordmark-pulse var(--sb-logo-pulse-duration) ease-in-out infinite;
}
.intro-title-hold .intro-wordmark { animation-play-state: paused; }
.intro-title-connected .intro-wordmark {
    /* Match the arriving cover until it clears, then breathe from that exact pose. */
    animation-delay: var(--sb-opening-duration, 3200ms);
}

/* Pulsing glow behind title */
.intro-title-container::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 98%;
    height: 115%;
    transform: translate(-50%, -50%);
    background: radial-gradient(ellipse at center,
            rgba(140, 215, 255, 0.1) 0%,
            rgba(199, 125, 255, 0.055) 40%,
            transparent 72%);
    filter: blur(20px);
    animation: titleGlowPulse 6.5s ease-in-out infinite;
    pointer-events: none;
    z-index: -1;
}

@keyframes titleGlowPulse {

    0%,
    100% {
        opacity: 0.08;
        transform: translate(-50%, -50%) scale(1);
    }

    50% {
        opacity: 0.15;
        transform: translate(-50%, -50%) scale(1.025);
    }
}

/* Resolve the title behind the aurora before the sweeping curtain exposes it. */
.intro-title-container.intro-title-hold .intro-title,
.intro-title-container.intro-title-hold .intro-shimmer,
.intro-title-container.intro-title-hold .intro-shimmer::before,
.intro-title-container.intro-title-hold .intro-eyebrow,
.intro-title-container.intro-title-hold .intro-subtitle {
    animation: none !important;
    opacity: 0 !important;
}

.intro-title-container.intro-title-hold::before {
    animation: none !important;
    opacity: 0 !important;
}

/* Hold the "PRESS ANY KEY TO BEGIN" prompt hidden until the deferred title has appeared
   and begin-interaction unlocks — so we never invite a press that does nothing. */
.intro-prompt.intro-prompt-hold {
    animation: none !important;
    opacity: 0 !important;
}

/* The prompt follows the wordmark rather than competing with its first readable frame, then
   breathes with the world (in for 4 s, out for 6 s). */
.intro-prompt.intro-prompt-shown {
    animation:
        promptFadeIn 0.9s ease-out 0.6s both,
        promptBreath 10s ease-in-out 1.5s infinite;
}

@keyframes promptFadeIn {
    from { opacity: 0; transform: translateX(-50%) translateY(6px); }
    to { opacity: 0.6; transform: translateX(-50%) translateY(0); }
}

@keyframes promptBreath {
    0%, 100% { opacity: 0.6; }
    40% { opacity: 0.94; }
}

@keyframes promptBreathLine {
    0%, 100% { transform: scaleX(0.3); opacity: 0.35; }
    40% { transform: scaleX(1); opacity: 0.9; }
}

@keyframes titleReveal {
    0% {
        opacity: 0;
        transform: translateY(16px) scale(0.965);

    }

    45% {
        opacity: 0.52;
        transform: translateY(5px) scale(0.992);

    }

    100% {
        opacity: 1;
        transform: translateY(0) scale(1);

    }
}

/* One wordmark travels from the loading cover into this exact resting position.
   The GPU background is already live beneath it; there is no second title entrance. */
.intro-title-connected .intro-title {
    animation: connectedTitleArrive var(--sb-opening-duration, 3200ms) linear both;
}
.intro-title-connected .intro-eyebrow,
.intro-title-connected .intro-subtitle {
    animation-delay: calc(var(--sb-opening-duration, 3200ms) * 0.65);
}
.intro-title-connected .intro-shimmer::before {
    animation: shimmerReveal 1.2s ease-in-out calc(var(--sb-opening-duration, 3200ms) * 0.8) both;
}
@keyframes connectedTitleArrive {
    /* Resolve underneath the still-opaque cover wordmark before it fades.
       Fading both copies together dims identical translucent layers mid-handoff. */
    0%, 82% { opacity: 0; }
    84%, 100% { opacity: 1; }
}

/* Chromatic aberration effect */
.intro-chromatic {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0;
    animation: chromaticPulse 6s ease-in-out 1.5s infinite;
    mix-blend-mode: screen;
}

@keyframes chromaticPulse {

    0%,
    100% {
        opacity: 0;
        transform: scale(1);
    }

    50% {
        opacity: 0.16;
        transform: scale(1.012);
    }
}

/* Prompt text */
.intro-prompt {
    position: absolute;
    bottom: 11vh;
    left: 50%;
    transform: translateX(-50%);
    font-family: 'Space Mono', monospace;
    width: min(90vw, 680px);
    font-size: 0.74rem;
    color: rgba(236, 242, 250, 0.9);
    text-align: center;
    opacity: 0;
    animation: promptBreath 10s ease-in-out 3s infinite;
    text-shadow: 0 0 22px rgba(184, 164, 255, 0.38);
    background: none;
    border: 0;
    padding: 16px;
    cursor: pointer;
    letter-spacing: 0.34em;
    text-transform: uppercase;
    line-height: 1.45;
    z-index: 15;
}
.intro-prompt span {
    position: relative;
    display: inline-block;
    padding-left: 0.34em;
}
/* A hairline that swells on the in-breath, in the logo's spectrum. */
.intro-prompt span::after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: -12px;
    width: 72px;
    height: 1px;
    margin-left: -36px;
    background: linear-gradient(90deg, transparent, #b8a4ff, #9ee8ed 60%, transparent);
    transform: scaleX(0.3);
    opacity: 0.35;
    animation: promptBreathLine 10s ease-in-out 1.5s infinite;
}

.intro-loading-indicator {
    position: absolute;
    bottom: 5vh;
    left: 50%;
    transform: translateX(-50%);
    z-index: 24;
    font-family: 'Space Mono', monospace;
    letter-spacing: 0.2em;
    font-size: clamp(0.6rem, 1.5vw, 0.9rem);
    color: rgba(180, 230, 255, 0.85);
    text-shadow: 0 0 16px rgba(120, 200, 255, 0.6);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease-out;
}

.intro-loading-indicator.visible {
    opacity: 1;
    animation: loadingPulse 1s ease-in-out infinite;
}

@keyframes loadingPulse {
    0%,
    100% {
        transform: translateX(-50%) scale(1);
        opacity: 0.72;
    }

    50% {
        transform: translateX(-50%) scale(1.03);
        opacity: 1;
    }
}

@keyframes promptPulse {

    0%,
    100% {
        opacity: 0.62;
    }

    50% {
        opacity: 0.88;
    }
}

/* Ripple effect on click */
.intro-ripple {
    position: absolute;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(100, 200, 255, 0.4) 0%, transparent 70%);
    pointer-events: none;
    animation: rippleExpand 1.5s ease-out forwards;
}

@keyframes rippleExpand {
    0% {
        width: 0;
        height: 0;
        opacity: 1;
    }

    100% {
        width: 800px;
        height: 800px;
        opacity: 0;
    }
}

/* Particle burst on click */
.intro-burst-particle {
    position: absolute;
    width: 5px;
    height: 5px;
    border-radius: 50%;
    pointer-events: none;
    animation: burstParticle 1s ease-out forwards;
    /* Background and box-shadow are set dynamically via JavaScript */
}

@keyframes burstParticle {
    0% {
        transform: translate(0, 0) scale(1);
        opacity: 1;
    }

    100% {
        transform: translate(var(--burst-x), var(--burst-y)) scale(0);
        opacity: 0;
    }
}

/* Holographic shimmer effect: the logo's own shape masks it, so the streak only ever touches
   the letters (unmasked it was a bare bar crossing the sky on either side of the wordmark). */
.intro-shimmer {
    position: absolute;
    inset: 0;
    overflow: hidden;
    -webkit-mask: url('../assets/branding/serenity-blocks-logo.svg') center / contain no-repeat;
    mask: url('../assets/branding/serenity-blocks-logo.svg') center / contain no-repeat;
    /* Screen-blend so the streak ADDS light to the letters as it passes. */
    mix-blend-mode: screen;
    pointer-events: none;
}

.intro-shimmer::before {
    content: '';
    position: absolute;
    top: 0;
    left: -60%;
    /* Narrow, tilted bright bar → reads as a specular light streak, not a wash. */
    width: 60%;
    height: 100%;
    background: linear-gradient(105deg,
            transparent 0%,
            rgba(180, 230, 255, 0) 38%,
            rgba(236, 248, 255, 0.55) 50%,
            rgba(180, 230, 255, 0) 62%,
            transparent 100%);
    opacity: 0.9;
    /* C2 — one bright specular sweep as the letters finish landing (the title
       feels "forged"), then a subtle recurring glint. */
    animation:
        shimmerReveal 1.25s cubic-bezier(0.4, 0, 0.2, 1) 0.95s 1 both,
        shimmerSweep 7.5s ease-in-out 4s infinite;
}

/* One-time bright reveal sweep */
@keyframes shimmerReveal {
    0% {
        transform: translateX(-100%);
        opacity: 0;
    }

    18% {
        opacity: 0.95;
    }

    100% {
        transform: translateX(550%);
        opacity: 0;
    }
}

/* Subtle recurring glint (a quick pass, then idle for the rest of the cycle) */
@keyframes shimmerSweep {
    0% {
        transform: translateX(-100%);
        opacity: 0;
    }

    9% {
        opacity: 0.32;
    }

    26% {
        opacity: 0;
    }

    100% {
        transform: translateX(550%);
        opacity: 0;
    }
}

/* The supporting copy sits outside the wordmark bounds used by menu-logo tracking. */
.intro-eyebrow, .intro-subtitle {
    position: absolute;
    width: 100%;
    left: 0;
    text-align: center;
    pointer-events: none;
    opacity: 0;
    animation: introCopyReveal 1.4s ease-out 0.6s forwards;
}
.intro-eyebrow {
    bottom: calc(100% + 34px);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.45em;
    color: #a1c8c0;
}
.intro-subtitle {
    top: calc(100% + 28px);
    font-family: 'Space Mono', monospace;
    font-size: clamp(8px, 1vw, 10px);
    font-weight: 400;
    line-height: 1.5;
    letter-spacing: 0.24em;
    word-spacing: 0.12em;
    text-indent: 0.24em;
    text-transform: uppercase;
    white-space: nowrap;
    color: #c0d7e5;
}
.intro-prompt small {
    display: block;
    margin-top: 22px;
    font-size: 9px;
    letter-spacing: 0.16em;
    text-transform: none;
    opacity: 0.55;
}
.intro-prompt:focus-visible { outline: 1px solid #9ccbc5; outline-offset: 4px; border-radius: 4px; }
.shrink-to-logo .intro-subtitle {
    top: calc(100% + 8px);
    font-size: 8px;
    font-weight: 700;
    opacity: 0.8;
    animation: none;
    transform: none;
}
@keyframes introCopyReveal {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 0.8; transform: translateY(0); }
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .intro-prompt {
        font-size: 0.78rem;
        bottom: 5vh;
    }

    .intro-loading-indicator {
        bottom: 2.5vh;
    }
}

/* Keep copy and the continue button outside the wordmark on short viewports. */
@media (max-height: 540px) {
    .intro-eyebrow { bottom: calc(100% + 18px); font-size: 8px; }
    .intro-subtitle { top: calc(100% + 16px); font-size: 9px; }
    .intro-prompt { bottom: max(10px, env(safe-area-inset-bottom)); padding: 10px; }
    .intro-prompt small { display: none; }
}

/* Reduce motion for accessibility */
@media (prefers-reduced-motion: reduce) {

    .intro-title,
    .intro-wordmark,
    .intro-shimmer::before,
    .intro-chromatic,
    .intro-warp-overlay,
    .intro-loading-indicator,
    .intro-eyebrow,
    .intro-subtitle {
        animation: none !important;
        opacity: 1 !important;
        transform: none !important;
    }

    .intro-prompt {
        opacity: 0.8 !important;
        animation: none !important;
    }

    .intro-prompt span::after {
        animation: none !important;
        transform: scaleX(0.6);
        opacity: 0.6;
    }

    .intro-title-container::before { animation: none !important; }
    .intro-shimmer, .intro-chromatic, .intro-warp-overlay { opacity: 0 !important; }
}

/* Resting menu geometry. The handoff layer owns the move into this slot. */
.shrink-to-logo {
    --menu-logo-top: 24px;
    --menu-logo-scale: 0.2;
    animation: none !important;
    position: fixed !important;
    left: 50% !important;
    top: var(--menu-logo-top);
    transform: translateX(-50%) !important;
    transform-origin: top center !important;
    z-index: 200 !important;
    transition: opacity 0.3s ease-out;
}

.shrink-to-logo .intro-shimmer { opacity: 0; }
.shrink-to-logo .intro-shimmer::before { animation: none !important; }

/* Main menu: keep the intro title as the logo — hide the static menu logo instead. */
body.start-modal-open .intro-title-container.shrink-to-logo {
    opacity: 1;
    top: var(--menu-logo-top) !important;
    /* Size the SVG at its actual menu dimensions, without a rasterized scale. */
    transform: translate(-50%, 0) !important;
    perspective: none !important;
    transform-style: flat !important;
    will-change: auto !important;
    transition: opacity 0.3s ease-out !important;
    animation: none !important;
    pointer-events: none !important;
}

/* One transform-only flight stays above both screens while their layout changes. */
.intro-menu-wordmark-flight {
    position: fixed;
    z-index: 11000;
    pointer-events: none;
    transform-origin: top left;
    will-change: transform;
}
.intro-menu-wordmark-flight__image {
    display: block;
    width: 100%;
    height: 100%;
    transform-origin: center;
    animation: sb-wordmark-pulse var(--sb-logo-pulse-duration) ease-in-out infinite;
    will-change: transform, opacity;
}
.intro-menu-wordmark-flight__caption {
    position: absolute;
    top: calc(100% + 28px);
    width: 100%;
    text-align: center;
    white-space: nowrap;
    text-transform: uppercase;
    animation: intro-menu-caption-out 200ms ease-out forwards;
}
@keyframes intro-menu-caption-out { to { opacity: 0; } }
body.intro-menu-handoff .intro-title-container,
body.intro-menu-handoff .main-menu-logo { visibility: hidden !important; }
/* Let the wordmark leave the center before the cards become prominent. */
body.intro-menu-handoff.start-modal-open .game-mode-cards-container {
    animation-delay: 340ms;
}
/* From the title, the menu arrives a beat behind the begin wave and the eye's glide into the
   menu's view, so the world and the interface move as one. Opacity only: the wordmark's flight
   measures its destination inside this layout and it must not move. */
body.intro-menu-revealed.start-modal-open #start-modal .sb-menu {
    animation: sb-menu-arrive 0.95s cubic-bezier(0.22, 0.8, 0.24, 1) 0.16s both;
}
@keyframes sb-menu-arrive {
    from { opacity: 0; }
    to { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
    body.intro-menu-revealed.start-modal-open #start-modal .sb-menu { animation: none; }
}
/* The destination must remain still while the incoming artwork reaches it. */
body.intro-menu-revealed.start-modal-open .main-menu-logo {
    animation: none !important;
    translate: none;
    opacity: 1;
}

body.start-modal-open .intro-title-container.shrink-to-logo::before {
    width: 104%;
    height: 136%;
    background: radial-gradient(ellipse at center,
            rgba(128, 90, 255, 0.08) 0%,
            rgba(74, 196, 255, 0.12) 34%,
            transparent 74%);
    filter: blur(16px);
    opacity: 0.42;
    animation: menuLogoHalo 6s ease-in-out infinite;
}

body.start-modal-open .intro-title-container.shrink-to-logo .intro-title {
    width: calc(var(--intro-logo-width) * var(--menu-logo-scale, 0.2));
    animation: none !important;
    transform: none;
    transform-style: flat !important;
    will-change: auto;
}

body.start-modal-open .intro-title-container.shrink-to-logo .intro-shimmer {
    opacity: 0 !important;
}

body.start-modal-open .intro-title-container.shrink-to-logo .intro-shimmer::before {
    animation: none !important;
}

/* Reduced motion: the boot gate already skips the whole cinematic, but if the
   menu logo is ever shown with motion (reduced-motion toggled mid-session, or a
   menu re-entry), hold it perfectly still — no shrink-to-logo flight, no halo
   pulse, no idle glow. Placed AFTER the resting-logo rules above so it wins on
   source order at equal specificity (the earlier @media block can't override the
   more specific body.start-modal-open halo rule). */
@media (prefers-reduced-motion: reduce) {
    .intro-title-container.shrink-to-logo {
        animation: none !important;
        transition: none !important;
    }

    body.start-modal-open .intro-title-container.shrink-to-logo::before,
    body.start-modal-open .intro-title-container.shrink-to-logo::after {
        animation: none !important;
    }
}

@keyframes menuLogoFloat {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(3px);
    }
}

@keyframes menuLogoHalo {

    0%,
    100% {
        opacity: 0.34;
        transform: translate(-50%, -50%) scale(0.98);
    }

    50% {
        opacity: 0.48;
        transform: translate(-50%, -50%) scale(1.03);
    }
}

@keyframes menuLetterGlow {

    0%,
    100% {
        filter: drop-shadow(0 0 7px rgba(110, 210, 255, 0.28));
    }

    50% {
        filter: drop-shadow(0 0 9px rgba(128, 90, 255, 0.22));
    }
}

/* Hide the static menu logo — the intro title is the logo */
body.start-modal-open .main-menu-logo {
    display: none !important;
}

/* The watchdog and explicit skip paths dispose the cinematic renderer. In that
   terminal state the normal DOM logo becomes the stable menu identity. */
body.startup-intro-skipped.start-modal-open .main-menu-logo {
    display: block !important;
}

/* Electron: prefer a normal DOM logo in the menu. Keeping the shrunken intro
   title alive behind the cards creates a composited/filtered layer that can go
   soft in packaged Windows builds. */
body.electron-app.start-modal-open .intro-title-container.shrink-to-logo {
    display: none !important;
}

body.electron-app.start-modal-open .intro-title-container.shrink-to-logo::before,
body.electron-app.start-modal-open .intro-title-container.shrink-to-logo::after {
    display: none !important;
    content: none !important;
}

body.electron-app.start-modal-open .main-menu-logo {
    display: block !important;
}

/* A compact menu scrolls as one document, including its logo. A fixed title
   would overlap the first card when there is no free space above the list. */
@media (max-width: 768px), (max-height: 700px) {
    body.start-modal-open .intro-title-container.shrink-to-logo { display: none !important; }
    body.start-modal-open .main-menu-logo { display: block !important; }
}
