// scenes-services.jsx — Facilities/Services grid + Experience scene

/* ============================================================
   6. SERVICES — sticky two-column scroller
   Left: pinned heading + active service description
   Right: scrolling list of services that update as you scroll
   ============================================================ */
const SERVICES = [
  { n: '01', name: 'Root Canal Treatment',  desc: 'Painless, single-sitting RCTs with rotary endodontics and apex-locator precision — saving the tooth you would otherwise lose.', tag: 'Endodontics' },
  { n: '02', name: 'Dental Implants',       desc: 'Titanium implants planned with digital imaging — a permanent, natural-feeling replacement for missing teeth.', tag: 'Restorative' },
  { n: '03', name: 'Crowns & Bridges',      desc: 'Ceramic and zirconia crowns crafted to match — durable, lifelike, and finished to a quiet polish.', tag: 'Prosthodontics' },
  { n: '04', name: 'Teeth Whitening',       desc: 'Professional bleaching that lifts years of staining in a single appointment, with no enamel compromise.', tag: 'Cosmetic' },
  { n: '05', name: 'Veneers & Smile Design',desc: 'Bespoke porcelain veneers — designed around your face, your line of smile, and your daily life.', tag: 'Cosmetic' },
  { n: '06', name: 'Braces & Aligners',     desc: 'Conventional braces and clear aligner therapy for adults and teens — quiet correction, lasting result.', tag: 'Orthodontics' },
  { n: '07', name: 'Filling & Restoration', desc: 'Tooth-coloured composite restorations — strong, sealed, and invisible against your natural enamel.', tag: 'General' },
  { n: '08', name: 'Scaling & Cleaning',    desc: 'Ultrasonic scaling and polish to remove tartar and surface staining — the foundation of every healthy mouth.', tag: 'Preventive' },
  { n: '09', name: 'Extractions & Minor Surgery', desc: 'Atraumatic extractions and minor oral surgery, performed with conservative technique and gentle aftercare.', tag: 'Surgery' },
  { n: '10', name: 'Facial Fracture Care',  desc: 'Maxillofacial trauma assessment and management — for fractures of the jaw and face requiring surgical attention.', tag: 'Surgery' },
  { n: '11', name: 'Child Dentistry',       desc: 'Calm, unhurried care for our youngest patients — building a lifetime of comfortable visits, one tooth at a time.', tag: 'Pedodontics' },
  { n: '12', name: 'Full Mouth Rehabilitation', desc: 'Coordinated restoration of function and aesthetics — for patients ready to start over with their smile.', tag: 'Comprehensive' },
  { n: '13', name: 'Digital Dental X-rays', desc: 'Low-radiation digital imaging on-site — sharper diagnosis, faster treatment, less guesswork.', tag: 'Diagnostics' },
];

function ServicesScene() {
  const wrapRef = useRef(null);
  const p = useElementProgress(wrapRef);

  // Detect mobile to switch from pinned scroll-driven scene → stacked list
  const [isMobile, setIsMobile] = useState(false);
  useEffect(() => {
    const mq = window.matchMedia('(max-width: 880px)');
    const update = () => setIsMobile(mq.matches);
    update();
    mq.addEventListener('change', update);
    return () => mq.removeEventListener('change', update);
  }, []);

  // Active index based on scroll progress through the section (desktop only)
  const idx = Math.min(
    SERVICES.length - 1,
    Math.max(0, Math.floor(p * SERVICES.length * 1.05))
  );

  // ────────── MOBILE: interactive horizontal swipe deck ──────────
  if (isMobile) {
    return <ServicesSceneMobile />;
  }

  // ────────── DESKTOP: pinned, scroll-driven ──────────
  return (
    <section id="services" ref={wrapRef} className="scene" style={{
      background: 'var(--cream)',
      height: `${100 + SERVICES.length * 50}vh`,
    }}>
      <div className="scene-pin paper" style={{ background: 'var(--cream)' }}>
        <div className="services-grid">
          {/* Left — sticky header + active card */}
          <div className="services-left" style={{ display: 'flex', flexDirection: 'column', justifyContent: 'space-between', minHeight: 0 }}>
            <div>
              <div className="eyebrow" style={{ marginBottom: 18 }}>● What we do</div>
              <h2 className="h-serif" style={{
                fontSize: 'clamp(38px, 4.6vw, 70px)',
                margin: '0 0 22px',
                color: 'var(--navy)',
                fontWeight: 400,
                lineHeight: 1.08,
              }}>
                Different ways<br />
                <span className="accent">to look after a smile.</span>
              </h2>
              <p style={{
                fontFamily: 'var(--f-serif)',
                fontSize: 'clamp(17px, 1.4vw, 21px)',
                lineHeight: 1.7,
                color: 'var(--ink)',
                opacity: 0.78,
                maxWidth: 460,
                margin: '0 0 36px',
              }}>
                From a six-monthly cleaning to full-mouth rehabilitation —
                everything you need under one roof, performed by the same
                hands that have done it 100,000 times before.
              </p>
            </div>

            {/* Active service card */}
            <div key={idx} className="services-active-card" style={{
              padding: '36px 32px',
              background: 'var(--navy)',
              color: 'var(--cream)',
              borderLeft: '2px solid var(--gold)',
              animation: 'fadeIn .6s cubic-bezier(.16,1,.3,1)',
              position: 'relative',
              overflow: 'hidden',
            }}>
              <div className="eyebrow" style={{ color: 'var(--gold)', marginBottom: 14 }}>
                {SERVICES[idx].tag}
              </div>
              <div className="h-display" style={{
                fontSize: 28, marginBottom: 14, letterSpacing: '0.04em',
                color: 'var(--cream)',
              }}>
                {SERVICES[idx].name}
              </div>
              <p style={{
                fontFamily: 'var(--f-serif)',
                fontSize: 17.5,
                lineHeight: 1.65,
                margin: 0,
                color: 'rgba(245,239,228,.86)',
              }}>{SERVICES[idx].desc}</p>

              <div style={{
                position: 'absolute', right: 20, top: 18,
                fontFamily: 'var(--f-display)',
                fontSize: 11, letterSpacing: '0.28em',
                color: 'var(--gold)',
              }}>
                {String(idx + 1).padStart(2, '0')} / {SERVICES.length}
              </div>
              <style>{`@keyframes fadeIn { from {opacity:0; transform: translateY(12px);} to {opacity:1; transform:none;} }`}</style>
            </div>
          </div>

          {/* Right — scrolling list */}
          <div className="services-list-wrap" style={{ position: 'relative', overflow: 'hidden' }}>
            {/* gradient mask */}
            <div style={{
              position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 2,
              background: 'linear-gradient(180deg, var(--cream), transparent 18%, transparent 82%, var(--cream))',
            }} />
            <div style={{
              transform: `translateY(${ -idx * 72 + 80 }px)`,
              transition: 'transform .9s cubic-bezier(.16,1,.3,1)',
            }}>
              {SERVICES.map((s, i) => {
                const distance = Math.abs(i - idx);
                const active = i === idx;
                return (
                  <div key={s.n} style={{
                    display: 'flex', alignItems: 'baseline', gap: 22,
                    padding: '14px 0 18px',
                    borderBottom: '1px solid rgba(11,30,63,.10)',
                    opacity: 1 - distance * 0.18,
                    transition: 'opacity .6s, color .6s',
                  }}>
                    <span style={{
                      fontFamily: 'var(--f-display)',
                      fontSize: 13,
                      letterSpacing: '0.18em',
                      color: active ? 'var(--gold)' : 'var(--gold-deep)',
                      width: 36,
                      flexShrink: 0,
                    }}>{s.n}</span>
                    <span className="h-serif" style={{
                      fontSize: active ? 'clamp(28px, 3.2vw, 44px)' : 'clamp(22px, 2.4vw, 32px)',
                      color: active ? 'var(--navy)' : 'rgba(11,30,63,.5)',
                      fontWeight: 400,
                      transition: 'font-size .6s cubic-bezier(.16,1,.3,1), color .6s',
                      lineHeight: 1.1,
                    }}>
                      {s.name}
                    </span>
                    {active && (
                      <span style={{
                        flex: 1,
                        height: 1,
                        background: 'linear-gradient(90deg, var(--gold), transparent)',
                        marginLeft: 14,
                        marginBottom: 8,
                      }} />
                    )}
                  </div>
                );
              })}
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

/* ============================================================
   7. EXPERIENCE — full-bleed image with parallax + overlay
   ============================================================ */
function ExperienceScene() {
  const wrapRef = useRef(null);
  const p = useElementProgress(wrapRef);
  const scale = 1 + p * 0.18;

  return (
    <section ref={wrapRef} className="scene" style={{ height: '160vh', background: 'var(--navy-deep)' }}>
      <div className="scene-pin">
        {/* Background video full-bleed with subtle zoom */}
        <div style={{
          position: 'absolute', inset: 0,
          transform: `scale(${scale})`,
          willChange: 'transform',
          transition: 'none',
        }}>
          <video
            src="https://videos.pexels.com/video-files/6763247/6763247-hd_1920_1080_25fps.mp4"
            autoPlay muted loop playsInline preload="auto"
            style={{ width: '100%', height: '100%', objectFit: 'cover' }}
          />
        </div>

        {/* Dark overlay gradient */}
        <div style={{
          position: 'absolute', inset: 0,
          background: 'linear-gradient(180deg, rgba(6,18,41,.4) 0%, rgba(6,18,41,.7) 60%, rgba(6,18,41,.85) 100%)',
        }} />

        {/* Centered text */}
        <div style={{
          position: 'absolute', inset: 0,
          display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
          padding: '0 32px', textAlign: 'center', color: 'var(--cream)',
        }}>
          <Reveal>
            <div className="eyebrow" style={{ color: 'var(--gold)', marginBottom: 24 }}>● The visit</div>
          </Reveal>
          <Reveal delay={0.1}>
            <h2 className="h-serif" style={{
              fontSize: 'clamp(40px, 6.4vw, 100px)',
              maxWidth: 1100,
              margin: 0,
              lineHeight: 1.05,
              fontWeight: 400,
              color: 'var(--cream)',
            }}>
              Every appointment, <br />
              <span className="accent">an unhurried experience.</span>
            </h2>
          </Reveal>
          <Reveal delay={0.22}>
            <p style={{
              fontFamily: 'var(--f-serif)',
              fontSize: 'clamp(17px, 1.5vw, 22px)',
              maxWidth: 660,
              margin: '32px auto 0',
              color: 'rgba(245,239,228,.82)',
              lineHeight: 1.65,
            }}>
              A calm operatory. Digital diagnostics. Sterilised instrumentation
              you can see being unwrapped in front of you. We won't rush you —
              and we won't sell you what you don't need.
            </p>
          </Reveal>

          {/* Three feature ticks */}
          <Reveal delay={0.34}>
            <div style={{
              display: 'flex', gap: 'clamp(20px, 4vw, 60px)', flexWrap: 'wrap',
              justifyContent: 'center', marginTop: 56,
              fontFamily: 'var(--f-sans)',
              fontSize: 13, letterSpacing: '0.22em', textTransform: 'uppercase',
              color: 'rgba(245,239,228,.7)',
            }}>
              {['Digital X-Ray On-Site', 'Sterilised, Single-Use', 'Transparent Pricing'].map((t) => (
                <div key={t} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                  <span style={{ width: 6, height: 6, background: 'var(--gold)', borderRadius: '50%' }} />
                  <span>{t}</span>
                </div>
              ))}
            </div>
          </Reveal>
        </div>
      </div>
    </section>
  );
}

/* ============================================================
   SERVICES — MOBILE: swipeable horizontal card deck
   ============================================================ */
function ServicesSceneMobile() {
  const scrollerRef = useRef(null);
  const [active, setActive] = useState(0);

  useEffect(() => {
    const el = scrollerRef.current;
    if (!el) return;
    let raf = null;
    const onScroll = () => {
      if (raf) return;
      raf = requestAnimationFrame(() => {
        raf = null;
        const cardW = el.clientWidth;
        const idx = Math.round(el.scrollLeft / cardW);
        setActive(Math.max(0, Math.min(SERVICES.length - 1, idx)));
      });
    };
    el.addEventListener('scroll', onScroll, { passive: true });
    return () => { el.removeEventListener('scroll', onScroll); if (raf) cancelAnimationFrame(raf); };
  }, []);

  const goTo = (i) => {
    const el = scrollerRef.current;
    if (!el) return;
    el.scrollTo({ left: i * el.clientWidth, behavior: 'smooth' });
  };

  return (
    <section id="services" className="full paper" style={{
      background: 'var(--cream)',
      padding: 'clamp(70px, 12vw, 100px) 0 clamp(60px, 10vw, 80px)',
      minHeight: 'auto',
      position: 'relative',
      overflow: 'hidden',
    }}>
      {/* Header */}
      <div style={{ padding: '0 22px', marginBottom: 32 }}>
        <Reveal>
          <div className="eyebrow" style={{ marginBottom: 14 }}>● What we do</div>
        </Reveal>
        <Reveal delay={0.05}>
          <h2 className="h-serif" style={{
            fontSize: 'clamp(32px, 8.5vw, 50px)',
            margin: '0 0 16px',
            color: 'var(--navy)',
            fontWeight: 400,
            lineHeight: 1.08,
          }}>
            Different ways<br />
            <span className="accent">to look after a smile.</span>
          </h2>
        </Reveal>
        <Reveal delay={0.12}>
          <p style={{
            fontFamily: 'var(--f-serif)',
            fontSize: 16.5,
            lineHeight: 1.65,
            color: 'var(--ink)',
            opacity: 0.74,
            margin: '0 0 8px',
          }}>
            Swipe to browse our care — from a six-monthly cleaning
            to full-mouth rehabilitation.
          </p>
        </Reveal>
      </div>

      {/* Counter + swipe hint */}
      <div style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        padding: '0 22px', marginBottom: 18,
      }}>
        <div style={{
          fontFamily: 'var(--f-display)',
          fontSize: 12, letterSpacing: '0.28em', color: 'var(--gold-deep)',
        }}>
          {String(active + 1).padStart(2, '0')} <span style={{ opacity: .4 }}>/ {String(SERVICES.length).padStart(2, '0')}</span>
        </div>
        <div className="eyebrow" style={{ fontSize: 9.5, color: 'var(--gold-deep)', display: 'flex', alignItems: 'center', gap: 8 }}>
          Swipe
          <svg width="22" height="10" viewBox="0 0 22 10" fill="none">
            <path d="M1 5 H 20 M 16 1 L 20 5 L 16 9" stroke="currentColor" strokeWidth="1" fill="none" />
          </svg>
        </div>
      </div>

      {/* Scroller */}
      <div ref={scrollerRef} className="svc-scroller">
        {SERVICES.map((s, i) => (
          <article key={s.n} className="svc-card">
            <div className="svc-card-inner">
              <div className="svc-card-top">
                <span className="svc-num">{s.n}</span>
                <span className="svc-divider" />
                <span className="svc-tag">{s.tag}</span>
              </div>
              <div className="svc-big-num">{s.n}</div>
              <div className="svc-card-content">
                <h3 className="h-serif svc-name">{s.name}</h3>
                <div className="svc-rule" />
                <p className="svc-desc">{s.desc}</p>
              </div>
              <div className="svc-card-bottom">
                <LeafOrnament size={20} color="var(--gold)" />
                <span>{String(i + 1).padStart(2, '0')} / {String(SERVICES.length).padStart(2, '0')}</span>
              </div>
            </div>
          </article>
        ))}
      </div>

      {/* Dot indicator */}
      <div className="svc-dots">
        {SERVICES.map((_, i) => (
          <button
            key={i}
            onClick={() => goTo(i)}
            aria-label={`Go to service ${i + 1}`}
            className={'svc-dot ' + (i === active ? 'on' : '')}
          />
        ))}
      </div>

      <div style={{ padding: '0 22px', textAlign: 'center', marginTop: 30 }}>
        <a href="#visit" className="btn btn-gold" style={{ padding: '12px 22px', fontSize: 11 }}>
          Ask about your concern
        </a>
      </div>

      <style>{`
        .svc-scroller {
          display: flex;
          gap: 16px;
          padding: 0 22px;
          overflow-x: auto;
          overflow-y: hidden;
          scroll-snap-type: x mandatory;
          scrollbar-width: none;
          -webkit-overflow-scrolling: touch;
        }
        .svc-scroller::-webkit-scrollbar { display: none; }
        .svc-card {
          flex: 0 0 calc(100vw - 64px);
          max-width: 380px;
          scroll-snap-align: center;
          aspect-ratio: 3 / 4.2;
          position: relative;
        }
        .svc-card-inner {
          position: relative;
          width: 100%; height: 100%;
          background: linear-gradient(165deg, var(--navy) 0%, var(--navy-deep) 100%);
          color: var(--cream);
          border: 1px solid var(--gold);
          padding: 24px 22px 22px;
          display: flex; flex-direction: column;
          overflow: hidden;
          box-shadow: 0 20px 50px rgba(11,30,63,.22), inset 0 0 0 1px rgba(245,239,228,.04);
        }
        .svc-card-inner::before {
          content: "";
          position: absolute; right: -40px; top: -40px;
          width: 180px; height: 180px;
          border-radius: 50%;
          background: radial-gradient(circle, rgba(201,165,91,.18), transparent 65%);
          pointer-events: none;
        }
        .svc-card-top {
          display: flex; align-items: center; gap: 10px;
          font-family: var(--f-sans);
          font-size: 10px;
          letter-spacing: 0.28em;
          color: var(--gold);
          text-transform: uppercase;
          z-index: 2;
        }
        .svc-num { color: var(--gold-light); }
        .svc-divider { width: 18px; height: 1px; background: var(--gold); opacity: .6; }
        .svc-tag { color: rgba(245,239,228,.6); }
        .svc-big-num {
          position: absolute; right: -10px; top: 14px;
          font-family: var(--f-display);
          font-size: 180px;
          line-height: 1;
          color: rgba(201,165,91,.08);
          letter-spacing: -0.04em;
          pointer-events: none;
          font-weight: 500;
        }
        .svc-card-content { margin-top: auto; padding-top: 24px; z-index: 2; }
        .svc-name {
          font-size: clamp(26px, 7vw, 34px);
          margin: 0 0 16px;
          color: var(--cream);
          font-weight: 400;
          line-height: 1.1;
          letter-spacing: -0.01em;
        }
        .svc-rule { width: 38px; height: 1px; background: var(--gold); margin-bottom: 16px; }
        .svc-desc {
          font-family: var(--f-serif);
          font-size: 16px;
          line-height: 1.6;
          color: rgba(245,239,228,.82);
          margin: 0;
        }
        .svc-card-bottom {
          margin-top: 18px;
          padding-top: 16px;
          border-top: 1px solid rgba(201,165,91,.18);
          display: flex; align-items: center; justify-content: space-between;
          font-family: var(--f-display);
          font-size: 10px;
          letter-spacing: 0.22em;
          color: var(--gold);
        }
        .svc-dots {
          display: flex; gap: 6px;
          justify-content: center;
          margin-top: 22px;
          flex-wrap: wrap;
          padding: 0 22px;
        }
        .svc-dot {
          width: 7px; height: 7px;
          border-radius: 50%;
          background: rgba(11,30,63,.18);
          border: none;
          padding: 0;
          cursor: pointer;
          transition: background .3s, transform .3s, width .3s;
        }
        .svc-dot.on {
          background: var(--gold);
          width: 22px;
          border-radius: 4px;
        }
      `}</style>
    </section>
  );
}

Object.assign(window, { ServicesScene, ExperienceScene, ServicesSceneMobile });
