/* global React */
const { useState, useEffect, useRef } = React;

/* ----------------------------- tiny icons (simple strokes) ----------------------------- */
const Ico = {
  arrow: (p) => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...p}><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg>,
  phone: (p) => <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.36 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.9.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92z"/></svg>,
  mail: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><rect x="2" y="4" width="20" height="16" rx="2"/><path d="m22 7-10 6L2 7"/></svg>,
  pin: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/><circle cx="12" cy="10" r="3"/></svg>,
  clock: (p) => <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" {...p}><circle cx="12" cy="12" r="9"/><polyline points="12 7 12 12 15 14"/></svg>,
  check: (p) => <svg width="30" height="30" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" {...p}><polyline points="20 6 9 17 4 12"/></svg>,
  star: (p) => <svg width="17" height="17" viewBox="0 0 24 24" fill="currentColor" {...p}><path d="M12 2l2.9 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l7.1-1.01z"/></svg>,
};

/* ----------------------------- Placeholder ----------------------------- */
function Ph({ label, dark, className = "", style, src }) {
  return (
    <div className={`ph ${dark ? "dark" : ""} ${src ? "has-img" : ""} ${className}`} data-label={label} style={style}>
      {src ? <img src={src} alt={label} loading="lazy" decoding="async" /> : null}
    </div>
  );
}

/* ----------------------------- scroll reveal hook ----------------------------- */
/* Robust against throttled/frozen render clocks:
   - Above-the-fold elements are NEVER hidden -> always visible on first paint.
   - Below-the-fold elements get .pre (hidden) then .in on scroll.
   - If the animation clock never ticks (frozen iframe / headless), .pre is stripped
     so content can't get stuck invisible. */
function useReveal() {
  useEffect(() => {
    const els = Array.from(document.querySelectorAll(".reveal"));
    const vh0 = window.innerHeight || document.documentElement.clientHeight;
    const line = vh0 * 0.86;
    // hide only what's below the fold right now
    els.forEach((el) => {
      const r = el.getBoundingClientRect();
      if (r.top > line) el.classList.add("pre");
    });
    let raf = 0;
    const check = () => {
      const vh = window.innerHeight || document.documentElement.clientHeight;
      for (const el of els) {
        if (!el.classList.contains("pre") || el.classList.contains("in")) continue;
        if (el.getBoundingClientRect().top < vh * 0.86) el.classList.add("in");
      }
    };
    const onScroll = () => { cancelAnimationFrame(raf); raf = requestAnimationFrame(check); };
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    // frozen-clock guard: if rAF never advances, strip hidden state entirely
    let ticked = false;
    requestAnimationFrame(() => requestAnimationFrame(() => { ticked = true; check(); }));
    const guard = setTimeout(() => {
      if (!ticked) els.forEach((el) => el.classList.remove("pre"));
    }, 1500);
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      cancelAnimationFrame(raf);
      clearTimeout(guard);
    };
  }, []);
}

/* ----------------------------- DATA ----------------------------- */
const SERVICES = [
  {
    n: "01", t: "Custom Cabinetry", img: "Custom kitchen cabinets", src: "images/projects/builtin-media-wall.jpg",
    d: "Built-to-order cabinets designed around your space, your storage, and the way you actually cook and live.",
    list: ["Full kitchen & pantry systems", "Bath vanities & built-ins", "Solid-wood doors & soft-close hardware"],
  },
  {
    n: "02", t: "Countertop Fabrication", img: "Quartz countertop slab", src: "images/projects/counter-brittanicca.jpg",
    d: "Precision templating, cutting, and polishing of premium stone — fabricated in-house and installed to a hairline seam.",
    list: ["Quartz, granite, marble & quartzite", "Laser templating for exact fit", "Undermount sink & edge profiling"],
  },
  {
    n: "03", t: "Full Kitchen Remodels", img: "Finished remodeled kitchen", src: "images/projects/kitchen-marble-waterfall.jpg",
    d: "One crew, start to finish — design, demo, cabinets, counters, and the trim details that pull a room together.",
    list: ["Design & 3D layout planning", "Coordinated install timeline", "Backsplash, lighting & finishing"],
  },
];

const MATERIALS = [
  { name: "Quartz", swatch: "oklch(0.92 0.01 90)", img: "Quartz slab — veined white", src: "images/projects/counter-calacatta-navy.jpg",
    d: "Engineered for durability and consistency. Non-porous, scratch-resistant, and virtually maintenance-free — our most-requested surface.",
    meta: [["Durability", "Excellent"], ["Upkeep", "No sealing"], ["Best for", "Busy family kitchens"]] },
  { name: "Granite", swatch: "oklch(0.45 0.02 60)", img: "Granite slab — speckled", src: "images/projects/counter-fantasy-brown.jpg",
    d: "Natural stone, no two slabs alike. Heat-resistant and dramatic, with deep movement and character that anchors a room.",
    meta: [["Durability", "Excellent"], ["Upkeep", "Seal yearly"], ["Best for", "Statement islands"]] },
  { name: "Marble", swatch: "oklch(0.95 0.008 100)", img: "Marble slab — soft veining", src: "images/projects/bath-marble-vanity.jpg",
    d: "Timeless and luminous. A softer stone that develops a lived-in patina — for those who love classic, old-world elegance.",
    meta: [["Durability", "Moderate"], ["Upkeep", "Seal often"], ["Best for", "Baking & bath"]] },
  { name: "Butcher Block", swatch: "oklch(0.62 0.06 60)", img: "Walnut butcher block", src: "images/projects/material-butcher-block.jpg",
    d: "Warm solid hardwood that softens a modern kitchen. Sandable, renewable, and beautiful as a contrast island top.",
    meta: [["Durability", "Good"], ["Upkeep", "Oil monthly"], ["Best for", "Warm accent tops"]] },
];

const PROJECTS = [
  { t: "Coastal Grey Showpiece Kitchen", cat: "Kitchens", tag: "Full Kitchen", size: "wide", src: "images/projects/kitchen-coastal-grey.jpg" },
  { t: "Brittanicca Waterfall Island", cat: "Countertops", tag: "Quartz", size: "tall", src: "images/projects/counter-brittanicca.jpg" },
  { t: "Marble Waterfall Island", cat: "Kitchens", tag: "Quartz", size: "", src: "images/projects/kitchen-marble-waterfall.jpg" },
  { t: "Black & White Marble Vanity", cat: "Baths", tag: "Marble", size: "tall", src: "images/projects/bath-marble-vanity.jpg" },
  { t: "Calacatta & Navy Island", cat: "Countertops", tag: "Quartz", size: "", src: "images/projects/counter-calacatta-navy.jpg" },
  { t: "Reclaimed-Wood Media Wall", cat: "Built-Ins", tag: "Millwork", size: "wide", src: "images/projects/builtin-media-wall.jpg" },
  { t: "Mood-Lit Grey Kitchen", cat: "Kitchens", tag: "Full Kitchen", size: "tall", src: "images/projects/kitchen-moodlit-grey.jpg" },
  { t: "Fantasy Brown Peninsula", cat: "Countertops", tag: "Granite", size: "", src: "images/projects/counter-fantasy-brown.jpg" },
  { t: "Coastal Powder Bath", cat: "Baths", tag: "Quartz", size: "tall", src: "images/projects/bath-coastal-powder.jpg" },
  { t: "Stone & Shelf Coffee Niche", cat: "Built-Ins", tag: "Millwork", size: "tall", src: "images/projects/builtin-coffee-niche.jpg" },
  { t: "Taupe Quartzite Waterfall", cat: "Countertops", tag: "Quartzite", size: "", src: "images/projects/counter-taupe-quartzite.jpg" },
  { t: "Nautical Drop Zone", cat: "Built-Ins", tag: "Millwork", size: "tall", src: "images/projects/builtin-nautical.jpg" },
  { t: "Spa Tub & Rain Glass", cat: "Baths", tag: "Tile & Stone", size: "tall", src: "images/projects/bath-spa-tub.jpg" },
];
const CATS = ["All Work", "Kitchens", "Countertops", "Built-Ins", "Baths"];

const PROCESS = [
  { n: "01", t: "Consult & Measure", d: "We visit your home, talk through how you live, and laser-template every surface for an exact fit." },
  { n: "02", t: "Design & Quote", d: "You get a 3D layout, material samples in hand, and a clear, itemized estimate — no surprises later." },
  { n: "03", t: "Build & Fabricate", d: "Cabinets and stone are crafted in our shop, where we control every cut, seam, and finish." },
  { n: "04", t: "Install & Walk-Through", d: "One tidy crew installs it all, then we walk the finished space together until it's right." },
];

const TESTIMONIALS = [
  { q: "Kane's rebuilt our entire kitchen in three weeks and the seam on the quartz is genuinely invisible. Craftsmanship you don't see anymore.", n: "Marcy & Dev Patel", l: "Kitchen Remodel", i: "M" },
  { q: "They templated, fabricated, and installed our island in-house — no middlemen, no finger-pointing. Just one team that owns the result.", n: "Tom Reyes", l: "Countertop Install", i: "T" },
  { q: "The cabinets fit our weird old farmhouse walls perfectly. Every drawer glides, every door lines up. Worth every penny.", n: "Jenna Whitfield", l: "Custom Cabinetry", i: "J" },
];

Object.assign(window, { React, useState, useEffect, useRef, Ico, Ph, useReveal, SERVICES, MATERIALS, PROJECTS, CATS, PROCESS, TESTIMONIALS });
