/* =====================================================================
   SCROLL-DRIVEN 3D SCENE
   Inserts between Stats Bar (#stats) and Tech Stack (#tech-stack).
   The wrapper creates 600vh of scroll distance. The stage is sticky
   so the canvas + chapter overlays stay in view while scrolling.

   Chapters:
     1. intro  (0% - 15%)   - sphere fades in, no text
     2. about  (15% - 45%)  - existing Context section ("من أنا")
     3. services (45% - 85%) - existing Services section ("ماذا سأبني لك")
     4. exit   (85% - 100%) - sphere fades out, no text
   ===================================================================== */

.scroll-scene-wrapper {
    position: relative;
    height: 600vh;
    background: #04060c;
    /* matches sphere backdrop so seams disappear at chapter 1 / 4 */
    isolation: isolate;
}

.scroll-scene-stage {
    position: sticky;
    top: 0;
    width: 100%;
    height: 100dvh;
    overflow: hidden;
}

#ai-sphere-canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    background: #04060c;
}

/* Faint starfield overlay - decorative only, sits above canvas */
.scroll-scene-stage::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 2;
    background-image:
        radial-gradient(1px 1px at 20% 30%, rgba(255, 255, 255, 0.4) 50%, transparent 100%),
        radial-gradient(1px 1px at 70% 60%, rgba(180, 200, 255, 0.3) 50%, transparent 100%),
        radial-gradient(1px 1px at 40% 80%, rgba(255, 255, 255, 0.25) 50%, transparent 100%),
        radial-gradient(1px 1px at 85% 20%, rgba(150, 180, 255, 0.3) 50%, transparent 100%),
        radial-gradient(1px 1px at 10% 65%, rgba(255, 255, 255, 0.35) 50%, transparent 100%);
    pointer-events: none;
    opacity: 0.6;
}

/* Soft scrim improves text legibility without darkening the sphere */
.scroll-scene-stage::after {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 3;
    background:
        linear-gradient(90deg, rgba(4, 6, 12, 0.55) 0%, rgba(4, 6, 12, 0) 45%, rgba(4, 6, 12, 0) 65%, rgba(4, 6, 12, 0.35) 100%),
        linear-gradient(0deg, rgba(4, 6, 12, 0.45) 0%, rgba(4, 6, 12, 0) 30%, rgba(4, 6, 12, 0) 70%, rgba(4, 6, 12, 0.45) 100%);
    pointer-events: none;
}

/* ===== Chapter container ===== */
.scroll-chapter {
    position: absolute;
    inset: 0;
    z-index: 4;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: clamp(24px, 4vh, 48px) clamp(16px, 3vw, 48px);
    opacity: 0;
    transform: translateY(24px);
    transition: opacity 600ms cubic-bezier(0.22, 1, 0.36, 1),
                transform 700ms cubic-bezier(0.22, 1, 0.36, 1);
    pointer-events: none;
}

.scroll-chapter.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

/* ===== Chapter 1 (intro) - no text, just the sphere ===== */
.scroll-chapter[data-chapter="intro"] {
    background: transparent;
}

.scroll-chapter[data-chapter="intro"] .scroll-chapter-text {
    display: none;
}

/* ===== Chapter 2 (about / من أنا) ===== */
.scroll-chapter[data-chapter="about"] {
    justify-content: center;
    align-items: center;
    text-align: center;
}

.scroll-chapter[data-chapter="about"][dir="ltr"],
html[dir="ltr"] .scroll-chapter[data-chapter="about"] {
    justify-content: center;
    align-items: center;
    text-align: center;
}

.scroll-chapter[data-chapter="about"] .scroll-chapter-text {
    max-width: 620px;
    width: 100%;
    text-align: center;
    margin: 0 auto;
    padding: clamp(16px, 2vw, 28px);
}

/* Override Tailwind text-3xl / text-4xl for the heading to prevent overlap */
.scroll-chapter[data-chapter="about"] .scroll-chapter-text h2 {
    font-size: clamp(1.35rem, 2.4vw, 1.85rem) !important;
    line-height: 1.45 !important;
    margin-bottom: 0.75rem !important;
}

.scroll-chapter[data-chapter="about"] .scroll-chapter-text p {
    font-size: clamp(0.85rem, 1.1vw, 1rem) !important;
    line-height: 1.7 !important;
}

/* ===== Chapter 3 (services / ماذا سأبني لك) ===== */
.scroll-chapter[data-chapter="services"] {
    align-items: center;
    justify-content: center;
    padding: clamp(16px, 2.5vh, 32px) clamp(16px, 2.5vw, 40px);
}

.scroll-chapter[data-chapter="services"] .scroll-chapter-text {
    max-width: 920px;
    width: 100%;
    padding: clamp(14px, 1.8vh, 22px) clamp(16px, 2vw, 24px);
    border-radius: 1.25rem;
}

/* ===== Chapter 4 (exit) - no text ===== */
.scroll-chapter[data-chapter="exit"] {
    background: linear-gradient(180deg, rgba(4, 6, 12, 0) 0%, rgba(4, 6, 12, 0.7) 100%);
}

.scroll-chapter[data-chapter="exit"] .scroll-chapter-text {
    display: none;
}

/* ===== Glass text panel ===== */
.scroll-chapter-text {
    background: rgba(8, 12, 24, 0.65);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 1.5rem;
    padding: clamp(20px, 2.5vw, 36px);
    box-shadow: 0 25px 60px -20px rgba(0, 0, 0, 0.6),
                inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

/* Make existing Tailwind headings inside the panel pop on dark */
.scroll-chapter-text h2,
.scroll-chapter-text h3 {
    color: #F8FAFC;
    text-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
}

.scroll-chapter-text p {
    color: rgba(232, 237, 245, 0.92);
    text-shadow: 0 1px 6px rgba(0, 0, 0, 0.5);
}

.scroll-chapter-text .text-brand-orange,
.scroll-chapter-text .accent,
.scroll-chapter-text .text-cta {
    color: #FF8A3D;
    /* slightly brighter orange on dark */
}

/* Force the Context section's white background to disappear
   so the glass panel shows through. The wrapper handles the
   outer surface; the inner section was authored for white. */
.scroll-chapter[data-chapter="about"] > section,
.scroll-chapter[data-chapter="services"] > section {
    background: transparent !important;
    padding: 0;
    box-shadow: none;
    border: none;
}

/* Hide original section padding since the chapter provides it */
.scroll-chapter[data-chapter="about"] > section > div,
.scroll-chapter[data-chapter="services"] > section > div {
    padding: 0;
}

/* Re-style service cards for dark glass aesthetic in chapter 3 */
.scroll-chapter[data-chapter="services"] .service-card-rebuilt,
.scroll-chapter[data-chapter="services"] .scroll-service-card {
    background: rgba(20, 55, 120, 0.35);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 0.875rem;
    padding: clamp(10px, 1.3vh, 14px) clamp(12px, 1.4vw, 18px);
    box-shadow: 0 8px 24px -8px rgba(0, 0, 0, 0.4),
                inset 0 1px 0 rgba(255, 255, 255, 0.08);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.scroll-chapter[data-chapter="services"] .scroll-service-card:hover {
    background: rgba(40, 83, 184, 0.35);
    transform: translateY(-3px);
    border-color: rgba(255, 255, 255, 0.25);
}

.scroll-chapter[data-chapter="services"] .scroll-service-card .icon-box {
    width: 2.25rem;
    height: 2.25rem;
    background: rgba(59, 130, 246, 0.18);
    border: 1px solid rgba(59, 130, 246, 0.35);
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.15rem;
    margin-bottom: 0.35rem;
    color: #93C5FD;
    transition: transform 0.3s ease;
}

.scroll-chapter[data-chapter="services"] .scroll-service-card:hover .icon-box {
    transform: scale(1.08);
    background: rgba(59, 130, 246, 0.28);
}

.scroll-chapter[data-chapter="services"] .scroll-service-card h3 {
    font-size: clamp(0.95rem, 1.1vw, 1.12rem) !important;
    font-weight: 700 !important;
    margin-bottom: 0.25rem !important;
    line-height: 1.35 !important;
}

.scroll-chapter[data-chapter="services"] .scroll-service-card p {
    font-size: clamp(0.8rem, 0.88vw, 0.88rem) !important;
    line-height: 1.5 !important;
}

/* Stagger card reveals inside chapter 3 */
.scroll-chapter[data-chapter="services"] .scroll-service-card {
    opacity: 0;
    transform: translateY(16px);
    transition: opacity 500ms ease, transform 600ms ease;
}

.scroll-chapter.is-visible[data-chapter="services"] .scroll-service-card {
    opacity: 1;
    transform: translateY(0);
}

.scroll-chapter.is-visible[data-chapter="services"] .scroll-service-card:nth-child(1) { transition-delay: 150ms; }
.scroll-chapter.is-visible[data-chapter="services"] .scroll-service-card:nth-child(2) { transition-delay: 260ms; }
.scroll-chapter.is-visible[data-chapter="services"] .scroll-service-card:nth-child(3) { transition-delay: 370ms; }
.scroll-chapter.is-visible[data-chapter="services"] .scroll-service-card:nth-child(4) { transition-delay: 480ms; }

/* Services grid gap control */
.scroll-chapter[data-chapter="services"] .scroll-services-grid {
    gap: clamp(8px, 1.2vh, 14px) !important;
}

/* Services title reveal */
.scroll-chapter[data-chapter="services"] .scroll-services-title {
    opacity: 0;
    transform: translateY(16px);
    transition: opacity 500ms ease, transform 600ms ease;
    font-size: clamp(1.3rem, 1.8vw, 1.65rem) !important;
    margin-bottom: clamp(10px, 1.4vh, 16px) !important;
    text-align: center;
}

.scroll-chapter.is-visible[data-chapter="services"] .scroll-services-title {
    opacity: 1;
    transform: translateY(0);
}

/* ===== Reveal animations for chapter 2 text ===== */
.scroll-chapter[data-chapter="about"] .scroll-reveal {
    overflow: hidden;
    display: block;
}

.scroll-chapter[data-chapter="about"] .scroll-reveal > * {
    display: block;
    transform: translateY(110%);
    transition: transform 800ms cubic-bezier(0.22, 1, 0.36, 1);
}

.scroll-chapter.is-visible[data-chapter="about"] .scroll-reveal > * {
    transform: translateY(0);
}

.scroll-chapter.is-visible[data-chapter="about"] .scroll-reveal:nth-child(2) > * {
    transition-delay: 200ms;
}

/* Canvas size / focal point adjustments for mobile */
@media (max-width: 780px) {
    .scroll-scene-stage::after {
        background:
            linear-gradient(180deg, rgba(4, 6, 12, 0.3) 0%, rgba(4, 6, 12, 0) 25%, rgba(4, 6, 12, 0) 65%, rgba(4, 6, 12, 0.85) 100%);
    }

    .scroll-chapter {
        align-items: flex-end;
        justify-content: center;
        padding: 10px 10px 20px;
    }

    .scroll-chapter[data-chapter="about"],
    .scroll-chapter[data-chapter="services"] {
        align-items: center;
        justify-content: center;
        padding: 10px 10px 20px;
    }

    .scroll-chapter[data-chapter="about"] .scroll-chapter-text,
    .scroll-chapter[data-chapter="services"] .scroll-chapter-text {
        max-width: 100%;
        padding: clamp(10px, 2.5vw, 14px);
        border-radius: 1rem;
    }

    .scroll-chapter[data-chapter="about"] .scroll-chapter-text h2 {
        font-size: 1.35rem !important;
        line-height: 1.35 !important;
        margin-bottom: 0.4rem !important;
    }

    .scroll-chapter[data-chapter="about"] .scroll-chapter-text p {
        font-size: 0.88rem !important;
        line-height: 1.6 !important;
    }

    .scroll-chapter[data-chapter="services"] {
        overflow-y: auto;
    }

    .scroll-chapter[data-chapter="services"] .scroll-services-title {
        font-size: 1.4rem !important;
        margin-bottom: clamp(6px, 1.5vh, 10px) !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-services-grid {
        grid-template-columns: 1fr !important;
        gap: 6px !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-service-card {
        padding: clamp(6px, 1.8vw, 10px) clamp(8px, 2.5vw, 12px) !important;
        border-radius: 0.75rem !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-service-card .icon-box {
        width: 1.6rem !important;
        height: 1.6rem !important;
        font-size: 0.85rem !important;
        margin-bottom: 0.2rem !important;
        border-radius: 0.4rem !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-service-card h3 {
        font-size: 0.92rem !important;
        margin-bottom: 0.15rem !important;
        line-height: 1.3 !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-service-card p {
        font-size: 0.78rem !important;
        line-height: 1.45 !important;
    }
}

/* ===== Reduced motion: collapse the scene to one static frame ===== */
@media (prefers-reduced-motion: reduce) {
    .scroll-scene-wrapper {
        height: auto;
    }

    .scroll-scene-stage {
        position: relative;
        height: auto;
    }

    .scroll-chapter {
        position: relative;
        inset: auto;
        height: auto;
        min-height: 60vh;
        opacity: 1 !important;
        transform: none !important;
        padding: 80px 20px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    }

    .scroll-chapter .scroll-reveal > * {
        transform: translateY(0) !important;
    }

    .scroll-chapter[data-chapter="services"] .scroll-service-card {
        opacity: 1 !important;
        transform: none !important;
    }
}
