/* Marquee + Results (Before-After + Shade strip) + How it Works
   + Why 360 + UGC reels + Footer */
const { useState: u3S, useEffect: u3E, useRef: u3R, useCallback: u3CB } = React;

/* ---------- Marquee — between hero and results ---------- */
function Marquee() {
  const items = [
    "Loved by dentists",
    "★",
    "2.4M+ TikTok views",
    "★",
    "Sold out twice",
    "★",
    "Made for sensitive teeth",
    "★",
    "Featured by @[Stassie]",
    "★",
    "Whisper-network favourite",
    "★",
  ];
  const track = (
    <div className="marquee__track" aria-hidden="true">
      <span className="marquee__item">
        {items.map((it, i) => (
          <span key={i} className={it === "★" ? "marquee__dot" : ""}>{it}</span>
        ))}
      </span>
    </div>
  );
  return (
    <div className="marquee" style={{ background: "var(--ink-2)" }}>
      {track}
      {track}
    </div>
  );
}

/* ---------- Results — B/A slider + Shade strip ---------- */
function Results() {
  return (
    <section id="results" className="section section--light on-light">
      <div className="wrap">
        <div style={{
          display: "flex", justifyContent: "space-between",
          alignItems: "baseline", marginBottom: "clamp(40px, 5vw, 64px)",
          flexWrap: "wrap", gap: 16,
        }}>
          <Reveal>
            <span className="eyebrow">— The verdict</span>
          </Reveal>
          <Reveal delay={120}>
            <span className="eyebrow" style={{ color: "var(--accent-deep)" }}>Real mouths · No filter · No retouch</span>
          </Reveal>
        </div>

        <Reveal delay={80}>
          <h2 className="h-section" style={{
            fontSize: "clamp(48px, 7vw, 116px)",
            margin: "0 0 clamp(48px, 6vw, 80px)",
            color: "var(--ink)",
            maxWidth: 1100,
          }}>
            From <em style={{ color: "var(--accent-deep)" }}>"is that veneers?"</em> in fourteen days.
          </h2>
        </Reveal>

        <div style={{
          display: "grid",
          gridTemplateColumns: "minmax(0, 1.1fr) minmax(0, 0.9fr)",
          gap: "clamp(24px, 4vw, 64px)",
          alignItems: "stretch",
        }}>
          <Reveal delay={120}>
            <BeforeAfter />
          </Reveal>
          <Reveal delay={220}>
            <ShadeStrip />
          </Reveal>
        </div>

        {/* Stat band */}
        <Reveal delay={120}>
          <div style={{
            display: "grid", gridTemplateColumns: "repeat(3, 1fr)",
            gap: "clamp(12px, 2vw, 24px)", marginTop: "clamp(32px, 4vw, 56px)",
          }}>
            {[
              ["14", "days", "standard course"],
              ["2", "ways", "one hour or overnight"],
              ["0", "drama", "near-zero sensitivity"],
            ].map(([n, u, l]) => (
              <div key={l} style={{
                background: "var(--pearl-2)", borderRadius: 6,
                padding: "clamp(20px, 2.4vw, 32px)", textAlign: "center",
              }}>
                <div style={{ display: "flex", alignItems: "baseline", justifyContent: "center", gap: 8 }}>
                  <span style={{ fontFamily: "var(--font-display)", fontSize: "clamp(40px, 5vw, 64px)", color: "var(--ink)", lineHeight: 1, letterSpacing: "-0.03em" }}>{n}</span>
                  <span style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontSize: "clamp(18px, 2vw, 24px)", color: "var(--accent-deep)" }}>{u}</span>
                </div>
                <div className="eyebrow" style={{ color: "var(--on-light-60)", marginTop: 10 }}>{l}</div>
              </div>
            ))}
          </div>
        </Reveal>
        <Reveal delay={160}>
          <p style={{
            fontFamily: "var(--font-mono)", fontSize: 11, lineHeight: 1.6,
            letterSpacing: "0.04em", color: "var(--on-light-40)",
            margin: "20px 0 0", maxWidth: 720,
          }}>
            Results vary by starting shade, formulation and adherence. Shade estimates are
            AI-assisted from progress photos and are indicative, not clinical measurements.
          </p>
        </Reveal>
      </div>
    </section>
  );
}

/* Draggable B/A slider — captions placeholder, real photos drop in later */
function BeforeAfter() {
  const wrapRef = u3R(null);
  const [pos, setPos] = u3S(52);
  const dragging = u3R(false);

  const update = (clientX) => {
    const r = wrapRef.current.getBoundingClientRect();
    const p = Math.max(0, Math.min(100, ((clientX - r.left) / r.width) * 100));
    setPos(p);
  };
  u3E(() => {
    const onMove = (e) => { if (dragging.current) update(e.clientX); };
    const onTouchMove = (e) => { if (dragging.current && e.touches[0]) update(e.touches[0].clientX); };
    const onUp = () => { dragging.current = false; };
    window.addEventListener("mousemove", onMove);
    window.addEventListener("touchmove", onTouchMove, { passive: true });
    window.addEventListener("mouseup", onUp);
    window.addEventListener("touchend", onUp);
    return () => {
      window.removeEventListener("mousemove", onMove);
      window.removeEventListener("touchmove", onTouchMove);
      window.removeEventListener("mouseup", onUp);
      window.removeEventListener("touchend", onUp);
    };
  }, []);

  return (
    <div ref={wrapRef} style={{
      position: "relative",
      aspectRatio: "4 / 5",
      borderRadius: 6,
      overflow: "hidden",
      cursor: "ew-resize",
      userSelect: "none",
      background: "#F5B225",
    }}
      onMouseDown={(e) => { dragging.current = true; update(e.clientX); }}
      onTouchStart={(e) => { dragging.current = true; if (e.touches[0]) update(e.touches[0].clientX); }}
    >
      {/* AFTER (full) — base layer, yellow sweater, brighter teeth */}
      <img src="uploads/after-web.png" alt="After 14 days of 360 Whitening"
        draggable="false"
        style={{
          position: "absolute", inset: 0,
          width: "100%", height: "100%",
          objectFit: "cover", objectPosition: "center 28%",
          pointerEvents: "none",
        }} />

      {/* BEFORE — clipped left side, black sweater */}
      <div style={{
        position: "absolute", inset: 0,
        clipPath: `inset(0 ${100 - pos}% 0 0)`,
        WebkitClipPath: `inset(0 ${100 - pos}% 0 0)`,
      }}>
        <img src="uploads/before-web.png" alt="Before 360 Whitening"
          draggable="false"
          style={{
            position: "absolute", inset: 0,
            width: "100%", height: "100%",
            objectFit: "cover", objectPosition: "center 28%",
            pointerEvents: "none",
          }} />
      </div>

      {/* Drag handle */}
      <div style={{
        position: "absolute", top: 0, bottom: 0,
        left: `${pos}%`, transform: "translateX(-50%)",
        width: 2, background: "var(--pearl)",
        boxShadow: "0 0 0 1px rgba(14,11,7,0.15), 0 6px 16px rgba(0,0,0,0.18)",
      }}>
        <div style={{
          position: "absolute", left: "50%", top: "50%",
          transform: "translate(-50%, -50%)",
          width: 48, height: 48, borderRadius: 999,
          background: "var(--pearl)", color: "var(--ink)",
          display: "grid", placeItems: "center",
          boxShadow: "0 8px 24px rgba(0,0,0,0.25)",
          fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.18em",
        }}>‹ ›</div>
      </div>

      {/* Labels */}
      <span style={{
        position: "absolute", left: 16, bottom: 16,
        background: "rgba(14,11,7,0.7)", color: "var(--pearl)",
        padding: "8px 12px", borderRadius: 999,
        fontFamily: "var(--font-mono)", fontSize: 10,
        letterSpacing: "0.2em", textTransform: "uppercase",
        backdropFilter: "blur(4px)",
      }}>Before · Day 0</span>
      <span style={{
        position: "absolute", right: 16, bottom: 16,
        background: "var(--accent)", color: "var(--ink)",
        padding: "8px 12px", borderRadius: 999,
        fontFamily: "var(--font-mono)", fontSize: 10,
        letterSpacing: "0.2em", textTransform: "uppercase",
      }}>After · Day 14</span>
    </div>
  );
}

/* Shade strip — interactive thumb across A4 → B1 */
function ShadeStrip() {
  const shades = [
    { code: "A4", color: "#9F7B4A" },
    { code: "A3.5", color: "#B49266" },
    { code: "A3", color: "#C6A983" },
    { code: "A2", color: "#D7BD9C" },
    { code: "A1", color: "#E6D3B6" },
    { code: "B1", color: "#F4E6C9" },
    { code: "BL3", color: "#F8EDD3" },
    { code: "BL2", color: "#FBF3DC" },
    { code: "BL1", color: "#FDF8E8" },
  ];
  const [pos, setPos] = u3S(7); // index of "starts here"
  const wrapRef = u3R(null);
  const dragging = u3R(false);
  const update = (clientX) => {
    const r = wrapRef.current.getBoundingClientRect();
    const p = (clientX - r.left) / r.width;
    const idx = Math.round(p * (shades.length - 1));
    setPos(Math.max(0, Math.min(shades.length - 1, idx)));
  };
  u3E(() => {
    const onMove = (e) => { if (dragging.current) update(e.clientX); };
    const onTouchMove = (e) => { if (dragging.current && e.touches[0]) update(e.touches[0].clientX); };
    const onUp = () => { dragging.current = false; };
    window.addEventListener("mousemove", onMove);
    window.addEventListener("touchmove", onTouchMove, { passive: true });
    window.addEventListener("mouseup", onUp);
    window.addEventListener("touchend", onUp);
    return () => {
      window.removeEventListener("mousemove", onMove);
      window.removeEventListener("touchmove", onTouchMove);
      window.removeEventListener("mouseup", onUp);
      window.removeEventListener("touchend", onUp);
    };
  }, []);

  const start = 1; // where most users start
  const startShade = shades[start];
  const targetShade = shades[pos];

  return (
    <div style={{
      background: "var(--pearl-2)",
      borderRadius: 6,
      padding: "clamp(24px, 3vw, 36px)",
      display: "flex", flexDirection: "column", justifyContent: "space-between",
      gap: 24,
      minHeight: "100%",
    }}>
      <div>
        <span className="eyebrow" style={{ color: "var(--on-light-60)" }}>— Pick your goal</span>
        <h3 style={{
          fontFamily: "var(--font-display)", fontWeight: 360,
          fontSize: "clamp(28px, 3vw, 44px)", letterSpacing: "-0.015em",
          margin: "12px 0 0", color: "var(--ink)", lineHeight: 1.05,
        }}>
          The <em style={{ color: "var(--accent-deep)" }}>shade ladder.</em><br/>
          You start here. We get you there.
        </h3>
      </div>

      {/* Strip */}
      <div ref={wrapRef}
        style={{ position: "relative", padding: "32px 0 0", cursor: "ew-resize", userSelect: "none" }}
        onMouseDown={(e) => { dragging.current = true; update(e.clientX); }}
        onTouchStart={(e) => { dragging.current = true; if (e.touches[0]) update(e.touches[0].clientX); }}
      >
        <div style={{
          display: "grid", gridTemplateColumns: `repeat(${shades.length}, 1fr)`,
          gap: 4, height: 64, borderRadius: 4, overflow: "hidden",
        }}>
          {shades.map((s, i) => (
            <div key={s.code} style={{ background: s.color, height: "100%", opacity: i >= start && i <= pos ? 1 : 0.55, transition: "opacity .3s ease" }} />
          ))}
        </div>
        {/* Start marker */}
        <div style={{
          position: "absolute", top: 0,
          left: `${((start + 0.5) / shades.length) * 100}%`,
          transform: "translateX(-50%)",
          fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.18em",
          color: "var(--on-light-60)", textTransform: "uppercase",
        }}>↓ you</div>
        {/* Goal thumb */}
        <div style={{
          position: "absolute", top: 22,
          left: `${((pos + 0.5) / shades.length) * 100}%`,
          transform: "translateX(-50%)",
          display: "flex", flexDirection: "column", alignItems: "center", gap: 4,
        }}>
          <div style={{
            fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.2em",
            color: "var(--ink)", textTransform: "uppercase",
          }}>Goal</div>
          <div style={{
            width: 36, height: 88, borderRadius: 4,
            background: targetShade.color,
            border: "2px solid var(--ink)",
            boxShadow: "0 6px 20px rgba(0,0,0,0.18)",
            marginTop: 4,
          }} />
        </div>
      </div>

      {/* Readout */}
      <div style={{
        display: "grid", gridTemplateColumns: "1fr auto 1fr",
        alignItems: "center", gap: 18,
        paddingTop: 24, borderTop: "1px solid var(--on-light-15)",
      }}>
        <div>
          <div className="eyebrow" style={{ color: "var(--on-light-60)" }}>Day 0</div>
          <div style={{ fontFamily: "var(--font-display)", fontSize: 23, marginTop: 4, color: "var(--ink)", fontStyle: "italic" }}>Where you are</div>
        </div>
        <I.Arrow style={{ color: "var(--accent-deep)" }} />
        <div style={{ textAlign: "right" }}>
          <div className="eyebrow" style={{ color: "var(--on-light-60)" }}>Day 14</div>
          <div style={{
            fontFamily: "var(--font-display)", fontSize: 23, marginTop: 4,
            color: "var(--accent-deep)", fontStyle: "italic",
          }}>Noticeably brighter</div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { Marquee, Results });
