/* ===== SELECTED WORK — individual full-height snap sections ===== */

type CaseMedia = "tower" | "jewel" | "hr";
type CaseTone = "dark" | "paper" | "ochre";
type CaseStudy = {
  idx: string;
  year: string;
  title: string;
  kind: string;
  desc: string;
  role: string;
  stack: string;
  metrics: Array<[string, string]>;
  accent: CaseTone;
  media: CaseMedia;
};

const CaseArt = ({ kind, hover }: { kind: CaseMedia; hover: boolean }) => {
  if (kind === "tower") {
    return (
      <svg viewBox="0 0 600 500" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
        <rect width="600" height="500" fill="#0f0d0a" />
        {/* moon */}
        <circle cx="500" cy="90" r="42" fill="#d9a441" opacity="0.8" />
        <circle cx="500" cy="90" r="42" fill="none" stroke="#d9a441" strokeWidth="0.5" strokeDasharray="2 3" />
        {/* towers */}
        <rect x="120" y="140" width="80" height="360" fill="none" stroke="#d9a441" strokeWidth="2" />
        <rect x="215" y="70" width="110" height="430" fill="none" stroke="#f6f0e3" strokeWidth="2" />
        <rect x="345" y="180" width="75" height="320" fill="none" stroke="#d9a441" strokeWidth="2" />
        <rect x="435" y="240" width="60" height="260" fill="none" stroke="#f6f0e3" strokeWidth="2" />
        {/* windows */}
        {Array.from({ length: 14 }).map((_, r) =>
          Array.from({ length: 4 }).map((_, c) => {
            const lit = (r * 7 + c * 3) % 5 > 1;
            return (
              <rect
                key={`m-${r}-${c}`}
                x={225 + c * 24} y={82 + r * 28}
                width="14" height="16"
                fill={lit ? "#d9a441" : "#1a1814"}
                opacity={hover && lit ? 1 : 0.85}
              />
            );
          })
        )}
        {Array.from({ length: 12 }).map((_, r) =>
          Array.from({ length: 3 }).map((_, c) => {
            const lit = (r + c) % 3 !== 0;
            return (
              <rect key={`l-${r}-${c}`} x={128 + c * 22} y={152 + r * 28} width="12" height="14"
                fill={lit ? "#d9a441" : "#1a1814"} opacity="0.7" />
            );
          })
        )}
        {/* chat bubble */}
        <g transform="translate(440 320)" opacity={hover ? 1 : 0.85}>
          <rect x="0" y="0" width="130" height="70" rx="10" fill="#f6f0e3" stroke="#d9a441" strokeWidth="1.5" />
          <circle cx="16" cy="18" r="5" fill="#d9a441" />
          <rect x="28" y="14" width="80" height="4" fill="#1a1814" />
          <rect x="16" y="28" width="100" height="4" fill="#1a1814" opacity="0.6" />
          <rect x="16" y="38" width="70" height="4" fill="#1a1814" opacity="0.6" />
          <rect x="16" y="48" width="90" height="4" fill="#1a1814" opacity="0.6" />
          <path d="M 14 70 L 26 82 L 38 70" fill="#f6f0e3" stroke="#d9a441" strokeWidth="1.5" />
        </g>
        {/* ground */}
        <line x1="0" y1="500" x2="600" y2="500" stroke="#d9a441" strokeWidth="4" />
      </svg>
    );
  }
  if (kind === "jewel") {
    return (
      <svg viewBox="0 0 600 500" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
        <rect width="600" height="500" fill="#f6f0e3" />
        <g transform="translate(300 250)">
          <g style={{ transformOrigin: "center", transform: hover ? "rotate(8deg)" : "rotate(0deg)", transition: "transform 0.6s" }}>
            <path d="M 0 -120 L 80 -60 L 100 40 L 0 140 L -100 40 L -80 -60 Z"
              fill="#1a1814" stroke="#d9a441" strokeWidth="2.5" />
            <path d="M 0 -120 L 0 140" stroke="#d9a441" strokeWidth="1" />
            <path d="M -80 -60 L 80 -60" stroke="#d9a441" strokeWidth="1" />
            <path d="M -100 40 L 100 40" stroke="#d9a441" strokeWidth="1" />
            <path d="M 0 -120 L -100 40" stroke="#d9a441" strokeWidth="0.6" />
            <path d="M 0 -120 L 100 40" stroke="#d9a441" strokeWidth="0.6" />
            <path d="M -80 -60 L 0 140" stroke="#d9a441" strokeWidth="0.6" />
            <path d="M 80 -60 L 0 140" stroke="#d9a441" strokeWidth="0.6" />
            <polygon points="0,-120 80,-60 0,-30 -80,-60" fill="#d9a441" opacity="0.25" />
            <polygon points="80,-60 100,40 0,0" fill="#d9a441" opacity="0.35" />
          </g>
        </g>
        <g transform="translate(40 50)" fontFamily="JetBrains Mono, monospace" fontSize="11" fill="#1a1814">
          <rect x="0" y="0" width="140" height="46" fill="none" stroke="#1a1814" strokeWidth="1" />
          <text x="10" y="18">SKU · AU-2401</text>
          <text x="10" y="34" opacity="0.7">22K · ₹ 84,200</text>
        </g>
        <g transform="translate(410 400)" fontFamily="JetBrains Mono, monospace" fontSize="11" fill="#1a1814">
          <rect x="0" y="0" width="140" height="46" fill="none" stroke="#1a1814" strokeWidth="1" />
          <text x="10" y="18">SKU · DI-0082</text>
          <text x="10" y="34" opacity="0.7">VS1 · ₹ 2,14,900</text>
        </g>
      </svg>
    );
  }
  // hr
  return (
    <svg viewBox="0 0 600 500" width="100%" height="100%" preserveAspectRatio="xMidYMid slice">
      <rect width="600" height="500" fill="#f6f0e3" />
      {/* org chart */}
      <g stroke="#1a1814" strokeWidth="1.6" fill="#f6f0e3">
        <rect x="250" y="40" width="100" height="56" />
        <line x1="300" y1="96" x2="300" y2="140" />
        <line x1="120" y1="140" x2="480" y2="140" />
        <line x1="120" y1="140" x2="120" y2="180" />
        <line x1="300" y1="140" x2="300" y2="180" />
        <line x1="480" y1="140" x2="480" y2="180" />
        <rect x="70" y="180" width="100" height="56" fill="#d9a441" />
        <rect x="250" y="180" width="100" height="56" />
        <rect x="430" y="180" width="100" height="56" />
      </g>
      <g fontFamily="JetBrains Mono, monospace" fontSize="11" fill="#1a1814">
        <text x="300" y="72" textAnchor="middle">CEO</text>
        <text x="120" y="212" textAnchor="middle" fontWeight="600">ENG</text>
        <text x="300" y="212" textAnchor="middle">DESIGN</text>
        <text x="480" y="212" textAnchor="middle">OPS</text>
      </g>
      {/* conversion chart */}
      <g transform="translate(60 280)">
        <rect width="480" height="180" fill="#fff" stroke="#1a1814" strokeWidth="1.5" />
        <text x="16" y="24" fontFamily="JetBrains Mono, monospace" fontSize="10" fill="#1a1814" opacity="0.7">CONVERSIONS · QoQ</text>
        <polyline
          points="30,150 90,140 150,130 210,120 270,100 330,80 390,50 450,30"
          fill="none" stroke="#d9a441" strokeWidth="3"
          strokeDasharray={hover ? "0" : "500"} strokeDashoffset={hover ? "0" : "500"}
          style={{ transition: "stroke-dashoffset 1.2s ease-out" }} />
        <circle cx="450" cy="30" r="6" fill="#d9a441" />
        <text x="395" y="22" fontFamily="JetBrains Mono, monospace" fontSize="13" fill="#c94a3f" fontWeight="700">+320%</text>
        <line x1="30" y1="160" x2="450" y2="160" stroke="#1a1814" strokeWidth="1" opacity="0.4" />
      </g>
    </svg>
  );
};

const caseList: CaseStudy[] = [
  {
    idx: "01", year: "2025", title: "Ramdev Builders",
    kind: "Real estate · Custom site + AI chatbot",
    desc: "A construction firm needed a presence that felt as solid as their buildings — plus a 24/7 site concierge. We shipped a bilingual marketing site with an AI assistant trained on 14 active projects.",
    role: "Design + Build + Chatbot", stack: "Next.js · Sanity · OpenAI",
    metrics: [["14", "active listings"], ["2.4s", "avg response"], ["8mo", "turnaround"]],
    accent: "dark", media: "tower",
  },
  {
    idx: "02", year: "2025", title: "Aureo Jewels",
    kind: "E-commerce · Saleor headless storefront",
    desc: "Luxury jewellery brand with 340+ SKUs and complex variant pricing. Built a headless storefront on Saleor with custom admin workflows for karat, stone, and making-charge calculations.",
    role: "Frontend + Admin Panel", stack: "Saleor · Next.js · Stripe",
    metrics: [["340", "SKUs modelled"], ["2.1", "AOV multiplier"], ["900", "LCP target ms"]],
    accent: "paper", media: "jewel",
  },
  {
    idx: "03", year: "2026", title: "Hirefold HR",
    kind: "SaaS · Interactive HR consultancy site",
    desc: "Next-gen HR consultancy wanted a site that felt like a tool, not a brochure. Built with scroll-driven interactions, live ROI calculator, and embedded booking — conversions jumped 3.2×.",
    role: "Design + Full-stack", stack: "Next.js · Framer Motion · Cal.com",
    metrics: [["320", "conversion lift %"], ["18", "avg session sec"], ["94", "Lighthouse"]],
    accent: "ochre", media: "hr",
  },
];

// Tick-up counter — animates when element is active
const useTickUp = (target: string, active: boolean) => {
  const [val, setVal] = React.useState(0);
  React.useEffect(() => {
    if (!active) return;
    let start: Nullable<number> = null;
    const dur = 1100;
    const from = 0;
    const to = parseFloat(target) || 0;
    let raf = 0;
    const step = (t: number) => {
      if (!start) start = t;
      const p = Math.min(1, (t - start) / dur);
      const eased = 1 - Math.pow(1 - p, 3);
      setVal(from + (to - from) * eased);
      if (p < 1) raf = requestAnimationFrame(step);
    };
    raf = requestAnimationFrame(step);
    return () => cancelAnimationFrame(raf);
  }, [active, target]);
  return val;
};

const formatMetric = (raw: string, val: number) => {
  // Preserve formatting: if raw has '+' prefix, '%', decimals, etc.
  const rawStr = String(raw);
  if (rawStr.includes(".")) return val.toFixed(1);
  return Math.round(val).toString();
};

const CaseMetric = ({ raw, label, active }: { raw: string; label: string; active: boolean }) => {
  const val = useTickUp(raw, active);
  const display = formatMetric(raw, val);
  // Detect label hints for suffix
  let prefix = "", suffix = "";
  const lbl = label.toLowerCase();
  if (lbl.includes("%") || lbl.includes("lift")) { prefix = "+"; suffix = "%"; }
  else if (lbl.includes("multiplier")) { suffix = "×"; }
  else if (lbl.includes("ms")) { suffix = "ms"; }
  else if (lbl.includes("sec")) { suffix = "s"; }
  else if (lbl.includes("lighthouse")) { suffix = "/100"; }
  return (
    <div className="case-metric">
      <div className="k">
        {prefix}{display}{suffix}
      </div>
      <div className="l">{label.replace(/%|ms| sec|multiplier/gi, "").replace(/\s+/g, " ").trim()}</div>
    </div>
  );
};

const SplitCasePanel = ({ c, active, progress }: { c: CaseStudy; i: number; active: boolean; progress: number }) => {
  // Drive visibility from `active` (clean in/out) and use progress only for parallax
  const parallaxY = active ? (progress - 0.5) * -30 : 0;

  return (
    <div
      className={`split-case tone-${c.accent} ${active ? "is-active" : ""}`}
      data-screen-label={`04.${c.idx} ${c.title}`}
      style={{
        opacity: active ? 1 : 0,
        transform: active ? "translateX(0)" : "translateX(60px)",
        pointerEvents: active ? "auto" : "none",
      }}
    >
      <div className="split-case-art-wrap">
      <div className="split-case-art" style={{ transform: `translateY(${parallaxY}px)` }}>
        <CaseArt kind={c.media} hover={active} />
        <div className="split-corner tl">
          <div>PROJECT</div>
          <div className="v">{c.idx} / {String(caseList.length).padStart(2, "0")}</div>
        </div>
        <div className="split-corner tr">
          <div>YEAR</div>
          <div className="v">{c.year}</div>
        </div>
        <div className="split-corner bl">
          <div>STATUS</div>
          <div className="v" style={{ color: "#6b8e4e" }}>● LIVE</div>
        </div>
      </div>
      </div>

      <div className="split-case-body">
        <div className="split-kicker">
          <span className="num">CASE {c.idx}</span>
          <span className="dash">——</span>
          <span>{c.kind}</span>
        </div>
        <h2 className="split-title">{c.title}</h2>
        <p className="split-desc">{c.desc}</p>

        <div className="split-metrics">
          {c.metrics.map(([k, v]) => (
            <CaseMetric key={k + v} raw={k} label={v} active={active} />
          ))}
        </div>

        <div className="split-foot">
          <div className="split-kv"><span>Role</span><b>{c.role}</b></div>
          <div className="split-kv"><span>Stack</span><b>{c.stack}</b></div>
        </div>

        <a href="#contact" className="split-cta">
          Start a project like this <ArrowRight size={14} />
        </a>
      </div>
    </div>
  );
};

const Cases = () => {
  const sectionRef = React.useRef<HTMLDivElement | null>(null);
  const [progress, setProgress] = React.useState([0, 0, 0]); // per-panel progress
  const [activeIdx, setActiveIdx] = React.useState(0);

  React.useEffect(() => {
    const onScroll = () => {
      const el = sectionRef.current;
      if (!el) return;
      const rect = el.getBoundingClientRect();
      const vh = window.innerHeight;
      const total = el.offsetHeight - vh; // scrollable distance while pinned
      const scrolled = Math.max(0, -rect.top);
      const t = total > 0 ? Math.min(1, scrolled / total) : 0;
      // Divide 0..1 into 3 equal segments
      const segs = caseList.map((_, i) => {
        const start = i / caseList.length;
        const end = (i + 1) / caseList.length;
        if (t <= start) return 0;
        if (t >= end) return 1;
        return (t - start) / (end - start);
      });
      setProgress(segs);
      // active = the panel whose segment contains t (or last segment at end)
      const idx = Math.max(0, Math.min(caseList.length - 1, Math.floor(t * caseList.length)));
      setActiveIdx(idx === caseList.length ? caseList.length - 1 : idx);
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const jumpTo = (i: number) => {
    const el = sectionRef.current;
    if (!el) return;
    const total = el.offsetHeight - window.innerHeight;
    const target = el.offsetTop + (i / caseList.length) * total + 4;
    window.scrollTo({ top: target, behavior: "smooth" });
  };

  return (
    <div id="work" ref={sectionRef} className="cases-pin">
      <div className="cases-sticky">
        {/* LEFT SIDEBAR */}
        <aside className="cases-sidebar">
          <div className="cases-sidebar-head">
            <div className="cases-kicker">
              <span>§04</span>
              <span className="dash">——</span>
              <span>SELECTED WORK</span>
            </div>
            <h2 className="cases-sidebar-title">
              Three projects,<br/>
              shipped with <em>care</em>.
            </h2>
            <p className="cases-sidebar-sub">Scroll to advance · each enters from the right.</p>
          </div>

          <ol className="cases-list">
            {caseList.map((c, i) => (
              <li key={c.idx}>
                <button
                  className={`cases-list-item ${activeIdx === i ? "is-active" : ""}`}
                  onClick={() => jumpTo(i)}
                >
                  <span className="cli-num">{c.idx}</span>
                  <span className="cli-body">
                    <span className="cli-title">{c.title}</span>
                    <span className="cli-kind">{c.kind}</span>
                  </span>
                  <span className="cli-rail">
                    <span
                      className="cli-fill"
                      style={{ transform: `scaleY(${activeIdx === i ? progress[i] : activeIdx > i ? 1 : 0})` }}
                    />
                  </span>
                </button>
              </li>
            ))}
          </ol>

          {/* mini-map dots at bottom of sidebar */}
          <div className="cases-dots">
            {caseList.map((c, i) => (
              <button
                key={c.idx}
                className={`cases-dot ${activeIdx === i ? "is-active" : ""}`}
                onClick={() => jumpTo(i)}
                aria-label={`Go to case ${c.idx}`}
              >
                <span />
              </button>
            ))}
          </div>
        </aside>

        {/* RIGHT STAGE */}
        <div className="cases-stage">
          {caseList.map((c, i) => (
            <SplitCasePanel key={c.idx} c={c} i={i} active={activeIdx === i} progress={progress[i]} />
          ))}
        </div>
      </div>

      <style>{`
        .cases-pin {
          height: 400vh; /* 3 panels × ~1.33vh each */
          position: relative;
          background: var(--paper);
          border-top: 1.5px solid var(--ink);
          border-bottom: 1.5px solid var(--ink);
        }
        .cases-sticky {
          position: sticky;
          top: 0;
          height: 100vh;
          display: grid;
          grid-template-columns: 340px 1fr;
          grid-template-rows: 100vh;
          overflow: hidden;
        }
        .cases-sidebar {
          border-right: 1.5px solid var(--ink);
          padding: 48px 32px 32px;
          display: flex;
          flex-direction: column;
          background: var(--paper);
          position: relative;
          z-index: 2;
        }
        .cases-kicker {
          display: inline-flex;
          gap: 10px;
          font-family: var(--font-mono);
          font-size: 11px;
          letter-spacing: 0.15em;
          text-transform: uppercase;
          color: var(--ink-soft);
        }
        .cases-kicker .dash { color: var(--ink-faint); }
        .cases-sidebar-title {
          font-family: var(--font-display);
          font-weight: 500;
          font-size: 42px;
          line-height: 0.98;
          letter-spacing: -0.03em;
          margin-top: 18px;
        }
        .cases-sidebar-title em {
          font-family: var(--font-hand);
          font-weight: 400;
          color: var(--ochre-deep);
        }
        .cases-sidebar-sub {
          font-size: 13px;
          line-height: 1.45;
          color: var(--ink-soft);
          margin-top: 14px;
          font-family: var(--font-mono);
        }

        .cases-list {
          list-style: none;
          padding: 0;
          margin: 40px 0 0;
          border-top: 1px solid var(--ink);
        }
        .cases-list li { border-bottom: 1px solid var(--ink); }
        .cases-list-item {
          display: grid;
          grid-template-columns: 40px 1fr 3px;
          gap: 14px;
          align-items: flex-start;
          width: 100%;
          padding: 20px 0 20px 4px;
          background: transparent;
          border: none;
          cursor: pointer;
          text-align: left;
          font: inherit;
          color: inherit;
          transition: padding 0.3s cubic-bezier(.2,.8,.2,1), opacity 0.3s;
          opacity: 0.55;
        }
        .cases-list-item:hover { opacity: 0.85; padding-left: 8px; }
        .cases-list-item.is-active { opacity: 1; padding-left: 12px; }
        .cli-num {
          font-family: var(--font-mono);
          font-size: 11px;
          letter-spacing: 0.1em;
          color: var(--ochre-deep);
          padding-top: 5px;
          font-weight: 500;
        }
        .cli-body { display: flex; flex-direction: column; gap: 4px; }
        .cli-title {
          font-family: var(--font-display);
          font-weight: 500;
          font-size: 20px;
          letter-spacing: -0.02em;
          line-height: 1.1;
        }
        .cli-kind {
          font-family: var(--font-mono);
          font-size: 10px;
          text-transform: uppercase;
          letter-spacing: 0.08em;
          color: var(--ink-soft);
          line-height: 1.4;
        }
        .cli-rail {
          position: relative;
          width: 2px;
          align-self: stretch;
          background: var(--ink-faint);
          overflow: hidden;
        }
        .cli-fill {
          position: absolute;
          inset: 0;
          background: var(--ochre-deep);
          transform-origin: top;
          transition: transform 0.1s linear;
        }

        .cases-dots {
          margin-top: auto;
          padding-top: 32px;
          display: flex;
          gap: 12px;
          align-items: center;
        }
        .cases-dot {
          width: 10px; height: 10px;
          padding: 4px;
          background: none;
          border: none;
          cursor: pointer;
          position: relative;
        }
        .cases-dot span {
          display: block;
          width: 10px; height: 10px;
          border-radius: 50%;
          background: var(--ink-faint);
          transition: all 0.3s cubic-bezier(.2,.8,.2,1);
        }
        .cases-dot.is-active span {
          background: var(--ochre-deep);
          transform: scale(1.4);
          box-shadow: 0 0 0 4px rgba(217, 164, 65, 0.18);
        }

        .cases-stage {
          position: relative;
          overflow: hidden;
          height: 100%;
        }
        .split-case {
          position: absolute;
          inset: 0;
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 48px;
          padding: 72px 56px;
          align-items: center;
          transition: opacity 0.55s cubic-bezier(.2,.8,.2,1), transform 0.55s cubic-bezier(.2,.8,.2,1);
          will-change: transform, opacity;
        }
        .split-case.tone-dark { background: #0f0d0a; color: var(--paper); }
        .split-case.tone-ochre { background: var(--ochre); color: var(--ink); }
        .split-case.tone-paper { background: var(--paper-deep); color: var(--ink); }

        .split-case-art {
          position: relative;
          aspect-ratio: 6/5;
          border: 1.5px solid currentColor;
          overflow: hidden;
          background: var(--paper);
          will-change: transform;
        }
        .split-case.tone-dark .split-case-art { border-color: var(--paper); }
        .split-corner {
          position: absolute;
          font-family: var(--font-mono);
          font-size: 9px;
          letter-spacing: 0.15em;
          text-transform: uppercase;
          padding: 10px 12px;
          color: #1a1814;
          background: rgba(246, 240, 227, 0.92);
          line-height: 1.3;
        }
        .split-corner .v { font-size: 11px; font-weight: 500; margin-top: 2px; letter-spacing: 0.05em; }
        .split-corner.tl { top: 0; left: 0; border-right: 1px solid #1a1814; border-bottom: 1px solid #1a1814; }
        .split-corner.tr { top: 0; right: 0; border-left: 1px solid #1a1814; border-bottom: 1px solid #1a1814; text-align: right; }
        .split-corner.bl { bottom: 0; left: 0; border-right: 1px solid #1a1814; border-top: 1px solid #1a1814; }

        .split-case-body { display: flex; flex-direction: column; }
        .split-kicker {
          display: flex; gap: 12px;
          font-family: var(--font-mono);
          font-size: 11px;
          letter-spacing: 0.12em;
          text-transform: uppercase;
          opacity: 0.75;
        }
        .split-kicker .dash { opacity: 0.5; }
        .split-title {
          font-family: var(--font-display);
          font-weight: 500;
          font-size: clamp(44px, 5.2vw, 80px);
          letter-spacing: -0.03em;
          line-height: 0.95;
          margin-top: 18px;
        }
        .split-desc {
          font-size: 16px;
          line-height: 1.55;
          max-width: 50ch;
          margin-top: 22px;
          opacity: 0.85;
        }
        .split-metrics {
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          margin-top: 32px;
          border-top: 1px solid currentColor;
          border-bottom: 1px solid currentColor;
        }
        .case-metric {
          padding: 16px 14px 16px 0;
          border-right: 1px dashed currentColor;
        }
        .case-metric:last-child { border-right: none; }
        .case-metric .k {
          font-family: var(--font-display);
          font-size: 32px;
          font-weight: 500;
          letter-spacing: -0.03em;
          line-height: 1;
          font-variant-numeric: tabular-nums;
        }
        .case-metric .l {
          font-family: var(--font-mono);
          font-size: 9px;
          text-transform: uppercase;
          letter-spacing: 0.1em;
          margin-top: 6px;
          opacity: 0.7;
          line-height: 1.3;
        }
        .split-foot {
          display: flex;
          gap: 40px;
          margin-top: 24px;
        }
        .split-kv span {
          display: block;
          font-family: var(--font-mono);
          font-size: 10px;
          text-transform: uppercase;
          letter-spacing: 0.1em;
          opacity: 0.6;
        }
        .split-kv b {
          font-family: var(--font-display);
          font-weight: 500;
          font-size: 15px;
          margin-top: 4px;
          display: block;
        }
        .split-cta {
          margin-top: 32px;
          align-self: flex-start;
          display: inline-flex;
          align-items: center;
          gap: 10px;
          padding: 13px 20px;
          border: 1.5px solid currentColor;
          border-radius: 999px;
          font-family: var(--font-mono);
          font-size: 12px;
          text-transform: uppercase;
          letter-spacing: 0.1em;
          transition: transform 0.2s, background 0.2s, color 0.2s;
        }
        .split-cta:hover {
          transform: translate(-2px, -2px);
          background: currentColor;
          color: var(--paper);
        }
        .split-case.tone-dark .split-cta:hover { color: var(--ink); }

        @media (max-width: 820px) {
          .cases-sticky { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
          .cases-sidebar {
            border-right: none;
            border-bottom: 1.5px solid var(--ink);
            padding: 20px 20px 16px;
          }
          .cases-sidebar-title { font-size: 28px; }
          .cases-list { display: none; }
          .cases-dots { padding-top: 16px; }
          .split-case {
            grid-template-columns: 1fr;
            gap: 24px;
            padding: 32px 20px;
            align-content: start;
            overflow-y: auto;
          }
          .split-case-art { aspect-ratio: 16/10; }
          .split-metrics { grid-template-columns: 1fr; }
          .case-metric { border-right: none; border-bottom: 1px dashed currentColor; }
        }
      `}</style>
    </div>
  );
};


const Stack = () => {
  const notes = [
    { cat: "DESIGN", head: "Figma & Sketch", tools: "Figma · Framer · Mobbin · Dribbble" },
    { cat: "FRONTEND", head: "React in my sleep", tools: "Next.js · React · TypeScript · Tailwind · Framer Motion" },
    { cat: "BACKEND", head: "Typed to the brim", tools: "Node · Supabase · Postgres · Prisma · tRPC" },
    { cat: "CMS / COMMERCE", head: "Headless, mostly", tools: "Sanity · Strapi · Saleor · Shopify · WordPress · Wix Studio" },
    { cat: "AUTOMATION", head: "n8n + AI wiring", tools: "n8n · Zapier · OpenAI · LangChain · Pinecone" },
    { cat: "THE BORING BITS", head: "Ops & delivery", tools: "Linear · Notion · Vercel · Sentry · Plausible" },
  ];
  const noteRefs = React.useRef<Array<HTMLDivElement | null>>([]);
  React.useEffect(() => {
    const handler = () => {
      noteRefs.current.forEach((el, i) => {
        if (!el) return;
        const rect = el.getBoundingClientRect();
        const center = rect.top + rect.height / 2;
        const offset = (center - window.innerHeight / 2) / window.innerHeight;
        const drift = (i % 2 === 0 ? -1 : 1) * offset * 20;
        el.style.setProperty("--drift", `${drift}px`);
      });
    };
    window.addEventListener("scroll", handler, { passive: true });
    handler();
    return () => window.removeEventListener("scroll", handler);
  }, []);
  return (
    <section className="section shell" id="stack" data-screen-label="05 Tools">
      <div className="section-head reveal">
        <span className="num">§05</span>
        <h2 className="title">The <em style={{fontFamily:"var(--font-hand)", color:"var(--ochre-deep)"}}>tools</em> on my desk</h2>
        <span className="meta">Stack · pinned to the wall</span>
      </div>
      <div className="stack-grid">
        {notes.map((n, i) => (
          <div key={i} ref={(el) => (noteRefs.current[i] = el)} className="sticky-note"
            style={{ transform: `translateY(var(--drift, 0)) rotate(${(i % 2 === 0 ? -1 : 1) * (0.6 + i * 0.2)}deg)` }}>
            <div className="pin" />
            <div className="cat">{n.cat}</div>
            <h4>{n.head}</h4>
            <div className="tools">{n.tools}</div>
          </div>
        ))}
      </div>
    </section>
  );
};

/* ===== WALL OF WOW — testimonials ===== */
const WallOfWow = () => {
  const quotes = [
    {
      name: "Priya Nair", role: "Interior Designer, Studio Nair",
      body: "Aditya built us the site I'd been sketching in my head for years. Everyone who visits says 'wow, who did this?' — that's the whole review.",
      accent: "ochre", rot: -2,
    },
    {
      name: "Rohan Mehta", role: "Principal Architect, Form & Function",
      body: "Rare to find someone who sketches AND ships. He sat in on our studio crits like a designer, then had the thing in production by Friday. Absurd.",
      accent: "paper", rot: 1.2,
    },
    {
      name: "Emma Lindqvist", role: "Design Lead (ex-Figma)",
      body: "Pixel-level taste plus the engineering to back it. I'd put his work next to anything coming out of Stockholm or SF right now.",
      accent: "dark", rot: -0.8,
    },
    {
      name: "Kabir Shah", role: "Interior Designer, Atelier Lumen",
      body: "I'm a designer. I'm allergic to bad websites. Aditya made me one I actually brag about. Clients book consultations from it.",
      accent: "ochre", rot: 2.4,
    },
    {
      name: "Mariana Costa", role: "Creative Director, São Paulo",
      body: "Thoughtful like an editor, fast like a startup engineer. The kind of freelance collaborator you hoard and don't share.",
      accent: "paper", rot: -1.5,
    },
    {
      name: "Aarav Kapoor", role: "Founder, Ramdev Builders",
      body: "Delivered what three agencies couldn't. And the AI concierge he built has genuinely taken work off our plate.",
      accent: "paper", rot: 0.9,
    },
  ];

  return (
    <section className="section shell" id="wow" data-screen-label="08 Wall of Wow">
      <div className="section-head reveal">
        <span className="num">§08</span>
        <h2 className="title">Wall of <em style={{ fontFamily: "var(--font-hand)", color: "var(--ochre-deep)" }}>wow</em></h2>
        <span className="meta">Interior designers · architects · studios</span>
      </div>

      <div className="wow-grid">
        {quotes.map((q, i) => (
          <div key={i} className={`wow-card tone-${q.accent}`} style={{ transform: `rotate(${q.rot}deg)` }}>
            <div className="wow-quote-mark">"</div>
            <p className="wow-body">{q.body}</p>
            <div className="wow-rule" />
            <div className="wow-meta">
              <div className="wow-avatar">{q.name.split(" ").map(n => n[0]).join("")}</div>
              <div>
                <div className="wow-name">{q.name}</div>
                <div className="wow-role">{q.role}</div>
              </div>
            </div>
            <div className="wow-stars">
              {[0,1,2,3,4].map(s => (
                <svg key={s} width="14" height="14" viewBox="0 0 24 24">
                  <path d="M12 2 L 14 9 L 21 10 L 15.5 14.5 L 17.5 21.5 L 12 17.5 L 6.5 21.5 L 8.5 14.5 L 3 10 L 10 9 Z"
                    fill="var(--ochre-deep)" />
                </svg>
              ))}
            </div>
          </div>
        ))}
      </div>

      <div className="wow-ticker">
        <div className="wow-ticker-track">
          {Array.from({ length: 3 }).flatMap((_, i) => [
            <span key={`a-${i}`}>WOW</span>,
            <span key={`b-${i}`} className="ast">✶</span>,
            <span key={`c-${i}`}>CRAFTED</span>,
            <span key={`d-${i}`} className="ast">✶</span>,
            <span key={`e-${i}`}>SHIPPED</span>,
            <span key={`f-${i}`} className="ast">✶</span>,
            <span key={`g-${i}`}>WORLD-CLASS</span>,
            <span key={`h-${i}`} className="ast">✶</span>,
            <span key={`i-${i}`}>BESPOKE</span>,
            <span key={`j-${i}`} className="ast">✶</span>,
          ])}
        </div>
      </div>

      <style>{`
        .wow-grid {
          display: grid;
          grid-template-columns: repeat(3, 1fr);
          gap: 28px;
          margin-bottom: 80px;
        }
        .wow-card {
          padding: 32px 28px 28px;
          border: 1.5px solid var(--ink);
          background: var(--paper);
          position: relative;
          transition: transform 0.3s cubic-bezier(.2,.8,.2,1);
          box-shadow: 4px 4px 0 var(--paper-shadow);
          min-height: 280px;
          display: flex;
          flex-direction: column;
        }
        .wow-card:hover {
          transform: rotate(0deg) translate(-3px, -3px) !important;
          box-shadow: 7px 7px 0 var(--ink);
          z-index: 2;
        }
        .wow-card.tone-ochre { background: var(--ochre); }
        .wow-card.tone-dark { background: #0f0d0a; color: var(--paper); }
        .wow-card.tone-dark .wow-rule { background: var(--paper); opacity: 0.3; }
        .wow-card.tone-paper { background: var(--paper-deep); }
        .wow-quote-mark {
          font-family: var(--font-display);
          font-size: 80px;
          line-height: 0.5;
          color: var(--ochre-deep);
          margin-bottom: 14px;
          height: 30px;
        }
        .wow-card.tone-ochre .wow-quote-mark { color: var(--ink); }
        .wow-body {
          font-family: var(--font-display);
          font-size: 18px;
          line-height: 1.45;
          letter-spacing: -0.01em;
          flex: 1;
        }
        .wow-rule {
          height: 1px;
          background: var(--ink);
          opacity: 0.2;
          margin: 20px 0 16px;
        }
        .wow-meta {
          display: flex;
          gap: 12px;
          align-items: center;
        }
        .wow-avatar {
          width: 36px; height: 36px;
          border-radius: 50%;
          background: var(--ink);
          color: var(--paper);
          display: flex;
          align-items: center;
          justify-content: center;
          font-family: var(--font-mono);
          font-size: 11px;
          font-weight: 500;
          letter-spacing: 0.05em;
          flex-shrink: 0;
        }
        .wow-card.tone-dark .wow-avatar { background: var(--ochre); color: var(--ink); }
        .wow-name {
          font-family: var(--font-display);
          font-size: 14px;
          font-weight: 500;
        }
        .wow-role {
          font-family: var(--font-mono);
          font-size: 10px;
          text-transform: uppercase;
          letter-spacing: 0.08em;
          opacity: 0.7;
          margin-top: 2px;
        }
        .wow-stars {
          position: absolute;
          top: 24px;
          right: 24px;
          display: flex;
          gap: 2px;
        }
        .wow-card.tone-dark .wow-stars svg path { fill: var(--ochre); }

        .wow-ticker {
          overflow: hidden;
          border-top: 1.5px solid var(--ink);
          border-bottom: 1.5px solid var(--ink);
          background: var(--ink);
          color: var(--ochre);
          padding: 24px 0;
          margin: 0 calc(-1 * (100vw - 100%) / 2);
        }
        .wow-ticker-track {
          display: flex;
          gap: 40px;
          white-space: nowrap;
          font-family: var(--font-display);
          font-size: 48px;
          font-weight: 500;
          letter-spacing: -0.02em;
          animation: wow-ticker 30s linear infinite;
          will-change: transform;
        }
        .wow-ticker-track .ast {
          color: var(--paper);
          font-size: 40px;
          display: inline-flex;
          align-items: center;
        }
        @keyframes wow-ticker {
          from { transform: translateX(0); }
          to { transform: translateX(-50%); }
        }

        @media (max-width: 900px) {
          .wow-grid { grid-template-columns: 1fr; }
          .wow-ticker-track { font-size: 32px; }
        }
      `}</style>
    </section>
  );
};

Object.assign(window, { Cases, WallOfWow, Stack });
