// Shared utilities, icons, placeholder visuals for both variations
const PHONE_DISPLAY = "(205) 452-9543";
const PHONE_HREF = "tel:+12054529543";
const SERVICE_AREAS = [
  "Birmingham", "Hoover", "Vestavia", "Mountain Brook", "Homewood",
  "Trussville", "Pinson", "Gardendale", "Fultondale", "Irondale",
  "Pelham", "Alabaster", "Helena", "Bessemer", "McCalla",
  "Center Point", "Chelsea", "Leeds", "Moody"
];

// ---------- Countdown hook ----------
function useCountdown() {
  const [t, setT] = React.useState(() => {
    const stored = parseInt(localStorage.getItem("xs-cd-end") || "0", 10);
    if (stored && stored > Date.now()) return stored;
    const end = Date.now() + 24 * 3600 * 1000;
    localStorage.setItem("xs-cd-end", String(end));
    return end;
  });
  const [now, setNow] = React.useState(Date.now());
  React.useEffect(() => {
    const id = setInterval(() => setNow(Date.now()), 1000);
    return () => clearInterval(id);
  }, []);
  let diff = Math.max(0, t - now);
  const h = Math.floor(diff / 3600000); diff -= h * 3600000;
  const m = Math.floor(diff / 60000); diff -= m * 60000;
  const s = Math.floor(diff / 1000);
  return {
    h: String(h).padStart(2, "0"),
    m: String(m).padStart(2, "0"),
    s: String(s).padStart(2, "0"),
  };
}

// ---------- Before/After slider ----------
function BeforeAfter({ height = 480, primary = "#00C6F3", round = 24 }) {
  const ref = React.useRef(null);
  const [pos, setPos] = React.useState(54);
  const [drag, setDrag] = React.useState(false);

  const handleMove = (clientX) => {
    if (!ref.current) return;
    const r = ref.current.getBoundingClientRect();
    const p = Math.max(0, Math.min(100, ((clientX - r.left) / r.width) * 100));
    setPos(p);
  };

  React.useEffect(() => {
    const up = () => setDrag(false);
    const move = (e) => {
      if (!drag) return;
      const x = e.touches ? e.touches[0].clientX : e.clientX;
      handleMove(x);
    };
    window.addEventListener("mouseup", up);
    window.addEventListener("touchend", up);
    window.addEventListener("mousemove", move);
    window.addEventListener("touchmove", move);
    return () => {
      window.removeEventListener("mouseup", up);
      window.removeEventListener("touchend", up);
      window.removeEventListener("mousemove", move);
      window.removeEventListener("touchmove", move);
    };
  }, [drag]);

  return (
    <div
      ref={ref}
      style={{
        position: "relative",
        width: "100%",
        height,
        borderRadius: round,
        overflow: "hidden",
        cursor: "ew-resize",
        userSelect: "none",
        background: "#0A1628",
      }}
      onMouseDown={(e) => { setDrag(true); handleMove(e.clientX); }}
      onTouchStart={(e) => { setDrag(true); handleMove(e.touches[0].clientX); }}
    >
      {/* BEFORE - dingy carpet */}
      <DirtyCarpet />
      {/* AFTER - clean carpet, clipped */}
      <div style={{ position: "absolute", inset: 0, clipPath: `inset(0 0 0 ${pos}%)` }}>
        <CleanCarpet primary={primary} />
      </div>
      {/* Labels */}
      <div style={{
        position: "absolute", top: 16, left: 16, padding: "6px 12px",
        background: "rgba(10,22,40,0.85)", color: "#fff",
        fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: 1.5,
        borderRadius: 999, textTransform: "uppercase",
      }}>BEFORE</div>
      <div style={{
        position: "absolute", top: 16, right: 16, padding: "6px 12px",
        background: primary, color: "#0A1628",
        fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: 1.5,
        borderRadius: 999, textTransform: "uppercase", fontWeight: 700,
      }}>AFTER</div>
      {/* Divider line + handle */}
      <div style={{
        position: "absolute", top: 0, bottom: 0, left: `${pos}%`,
        width: 2, background: "#fff", boxShadow: "0 0 24px rgba(0,0,0,0.4)",
      }} />
      <div style={{
        position: "absolute", top: "50%", left: `${pos}%`,
        transform: "translate(-50%, -50%)",
        width: 56, height: 56, borderRadius: "50%",
        background: "#fff", display: "grid", placeItems: "center",
        boxShadow: "0 8px 32px rgba(0,0,0,0.3)",
      }}>
        <svg width="22" height="22" viewBox="0 0 24 24" fill="none">
          <path d="M9 6L3 12L9 18M15 6L21 12L15 18" stroke="#0A1628" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" />
        </svg>
      </div>
    </div>
  );
}

function DirtyCarpet() {
  return (
    <svg width="100%" height="100%" viewBox="0 0 800 600" preserveAspectRatio="xMidYMid slice"
      style={{ position: "absolute", inset: 0, display: "block" }}>
      <defs>
        <pattern id="dirtTex" width="6" height="6" patternUnits="userSpaceOnUse">
          <rect width="6" height="6" fill="#5a4a3a" />
          <circle cx="2" cy="3" r="0.6" fill="#3d2f22" />
          <circle cx="5" cy="1" r="0.4" fill="#7a6a5a" />
        </pattern>
        <radialGradient id="dirtVignette" cx="40%" cy="60%" r="80%">
          <stop offset="0%" stopColor="#6b5a48" />
          <stop offset="100%" stopColor="#2e2418" />
        </radialGradient>
      </defs>
      <rect width="800" height="600" fill="url(#dirtVignette)" />
      <rect width="800" height="600" fill="url(#dirtTex)" opacity="0.55" />
      {/* Stains */}
      <ellipse cx="280" cy="340" rx="90" ry="65" fill="#1a0f06" opacity="0.55" />
      <ellipse cx="310" cy="320" rx="60" ry="40" fill="#3a200d" opacity="0.7" />
      <ellipse cx="540" cy="220" rx="70" ry="45" fill="#2a1808" opacity="0.5" />
      <ellipse cx="180" cy="480" rx="120" ry="55" fill="#1d1208" opacity="0.45" />
      <ellipse cx="640" cy="450" rx="55" ry="35" fill="#2a1c0c" opacity="0.6" />
      {/* footprint hints */}
      <ellipse cx="400" cy="180" rx="22" ry="40" fill="#1a1208" opacity="0.4" transform="rotate(-15 400 180)" />
      <ellipse cx="450" cy="200" rx="22" ry="40" fill="#1a1208" opacity="0.4" transform="rotate(-15 450 200)" />
    </svg>
  );
}

function CleanCarpet({ primary = "#00C6F3" }) {
  return (
    <svg width="100%" height="100%" viewBox="0 0 800 600" preserveAspectRatio="xMidYMid slice"
      style={{ position: "absolute", inset: 0, display: "block" }}>
      <defs>
        <pattern id="cleanTex" width="4" height="4" patternUnits="userSpaceOnUse">
          <rect width="4" height="4" fill="#f4f6f5" />
          <circle cx="1" cy="2" r="0.3" fill="#dde2e0" />
          <circle cx="3" cy="1" r="0.3" fill="#e8edeb" />
        </pattern>
        <linearGradient id="cleanLight" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#ffffff" />
          <stop offset="100%" stopColor="#dde6e8" />
        </linearGradient>
        <radialGradient id="sparkle" cx="50%" cy="50%">
          <stop offset="0%" stopColor={primary} stopOpacity="0.4" />
          <stop offset="60%" stopColor={primary} stopOpacity="0" />
        </radialGradient>
      </defs>
      <rect width="800" height="600" fill="url(#cleanLight)" />
      <rect width="800" height="600" fill="url(#cleanTex)" opacity="0.7" />
      {/* Vacuum tracks */}
      {[0,1,2,3,4,5,6,7].map(i => (
        <rect key={i} x={i * 100} y="0" width="50" height="600" fill="#e8eef0" opacity="0.5" />
      ))}
      {/* Sparkles */}
      <circle cx="200" cy="180" r="60" fill="url(#sparkle)" />
      <circle cx="560" cy="350" r="80" fill="url(#sparkle)" />
      <g fill={primary}>
        <path d="M150 150 l4 12 l12 4 l-12 4 l-4 12 l-4 -12 l-12 -4 l12 -4 z" opacity="0.7" />
        <path d="M620 280 l3 9 l9 3 l-9 3 l-3 9 l-3 -9 l-9 -3 l9 -3 z" opacity="0.6" />
        <path d="M380 420 l3 9 l9 3 l-9 3 l-3 9 l-3 -9 l-9 -3 l9 -3 z" opacity="0.5" />
      </g>
    </svg>
  );
}

// ---------- Stars ----------
function Stars({ n = 5, size = 16, color = "#FFB800" }) {
  return (
    <span style={{ display: "inline-flex", gap: 2 }}>
      {Array.from({ length: n }).map((_, i) => (
        <svg key={i} width={size} height={size} viewBox="0 0 24 24" fill={color}>
          <path d="M12 2l2.9 6.9L22 10l-5.5 4.7L18 22l-6-3.7L6 22l1.5-7.3L2 10l7.1-1.1z" />
        </svg>
      ))}
    </span>
  );
}

// ---------- Icons (line, geometric) ----------
const Icon = ({ name, size = 28, color = "currentColor", strokeWidth = 1.8 }) => {
  const props = { width: size, height: size, viewBox: "0 0 24 24", fill: "none", stroke: color, strokeWidth, strokeLinecap: "round", strokeLinejoin: "round" };
  switch (name) {
    case "shield": return <svg {...props}><path d="M12 2l8 4v6c0 5-3.5 9-8 10-4.5-1-8-5-8-10V6l8-4z"/><path d="M9 12l2 2 4-4"/></svg>;
    case "leaf": return <svg {...props}><path d="M5 19c0-9 6-14 16-14 0 10-5 16-14 16-1 0-2-.3-2-2z"/><path d="M5 19l8-8"/></svg>;
    case "clock": return <svg {...props}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>;
    case "badge": return <svg {...props}><path d="M12 2l2.4 2.5 3.4-.5.5 3.4L20.8 10 19 12l1.8 2-2.5 2.4.5 3.4-3.4.5L12 22l-2.4-2.4-3.4.5-.5-3.4L3.2 14 5 12 3.2 10l2.5-2.4-.5-3.4 3.4.5z"/><path d="M9 12l2 2 4-4"/></svg>;
    case "phone": return <svg {...props}><path d="M22 16.9v3a2 2 0 01-2.2 2 19.8 19.8 0 01-8.6-3.1 19.5 19.5 0 01-6-6 19.8 19.8 0 01-3.1-8.7A2 2 0 014 2h3a2 2 0 012 1.7c.1.9.3 1.8.6 2.6a2 2 0 01-.5 2.1L7.9 9.7a16 16 0 006 6l1.3-1.3a2 2 0 012.1-.5c.8.3 1.7.5 2.6.6a2 2 0 011.7 2z"/></svg>;
    case "calendar": return <svg {...props}><rect x="3" y="5" width="18" height="16" rx="2"/><path d="M8 3v4M16 3v4M3 11h18"/></svg>;
    case "truck": return <svg {...props}><path d="M3 7h11v9H3z"/><path d="M14 10h4l3 3v3h-7"/><circle cx="7" cy="18" r="2"/><circle cx="17" cy="18" r="2"/></svg>;
    case "spray": return <svg {...props}><rect x="9" y="9" width="6" height="12" rx="1"/><path d="M11 9V5h2v4M9 4h6M5 6h1M5 10h1M3 8h1M19 6h-1M19 10h-1M21 8h-1"/></svg>;
    case "check": return <svg {...props}><path d="M4 12l5 5L20 6"/></svg>;
    case "arrow": return <svg {...props}><path d="M5 12h14M13 5l7 7-7 7"/></svg>;
    case "pin": return <svg {...props}><path d="M12 22s7-7 7-12a7 7 0 10-14 0c0 5 7 12 7 12z"/><circle cx="12" cy="10" r="2.5"/></svg>;
    case "plus": return <svg {...props}><path d="M12 5v14M5 12h14"/></svg>;
    case "minus": return <svg {...props}><path d="M5 12h14"/></svg>;
    case "google": return <svg width={size} height={size} viewBox="0 0 24 24"><path fill="#4285F4" d="M21.6 12.2c0-.7-.1-1.3-.2-2H12v3.8h5.4a4.6 4.6 0 01-2 3v2.5h3.2c1.9-1.7 3-4.3 3-7.3z"/><path fill="#34A853" d="M12 22c2.7 0 5-1 6.6-2.5l-3.2-2.5c-.9.6-2 1-3.4 1-2.6 0-4.8-1.7-5.6-4.1H3v2.5A10 10 0 0012 22z"/><path fill="#FBBC04" d="M6.4 13.9a6 6 0 010-3.8V7.6H3a10 10 0 000 8.8l3.4-2.5z"/><path fill="#EA4335" d="M12 5.9c1.5 0 2.8.5 3.8 1.5l2.8-2.8A10 10 0 003 7.6l3.4 2.5C7.2 7.6 9.4 5.9 12 5.9z"/></svg>;
    default: return null;
  }
};

// ---------- Service area map (stylized SVG, not real geography) ----------
function ServiceMap({ primary = "#00C6F3" }) {
  // arranged loosely around a Birmingham core
  const dots = [
    { x: 50, y: 50, label: "Birmingham", primary: true, r: 14 },
    { x: 38, y: 38, label: "Fultondale", r: 7 },
    { x: 30, y: 30, label: "Gardendale", r: 7 },
    { x: 25, y: 22, label: "Pinson", r: 6 },
    { x: 65, y: 28, label: "Trussville", r: 8 },
    { x: 78, y: 38, label: "Leeds", r: 7 },
    { x: 82, y: 48, label: "Moody", r: 6 },
    { x: 70, y: 50, label: "Irondale", r: 7 },
    { x: 62, y: 60, label: "Mountain Brook", r: 8 },
    { x: 55, y: 70, label: "Homewood", r: 8 },
    { x: 48, y: 78, label: "Vestavia", r: 8 },
    { x: 55, y: 86, label: "Hoover", r: 9 },
    { x: 70, y: 80, label: "Chelsea", r: 7 },
    { x: 68, y: 90, label: "Pelham", r: 7 },
    { x: 60, y: 95, label: "Helena", r: 7 },
    { x: 35, y: 88, label: "Bessemer", r: 8 },
    { x: 22, y: 80, label: "McCalla", r: 7 },
    { x: 40, y: 60, label: "Center Point", r: 7 },
    { x: 48, y: 92, label: "Alabaster", r: 7 },
  ];
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: "1.2/1", background: "#F2F6F8", borderRadius: 24, overflow: "hidden" }}>
      <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none" style={{ position: "absolute", inset: 0 }}>
        <defs>
          <pattern id="grid" width="5" height="5" patternUnits="userSpaceOnUse">
            <path d="M5 0H0v5" stroke="#dfe7eb" strokeWidth="0.15" fill="none" />
          </pattern>
        </defs>
        <rect width="100" height="100" fill="url(#grid)" />
        {/* abstract roads */}
        <path d="M0 40 Q 35 50 50 50 T 100 60" stroke="#cfd9de" strokeWidth="0.6" fill="none" />
        <path d="M50 0 Q 45 40 50 50 T 55 100" stroke="#cfd9de" strokeWidth="0.6" fill="none" />
        <path d="M10 90 L 90 10" stroke="#cfd9de" strokeWidth="0.4" fill="none" />
        {/* coverage halo */}
        <circle cx="50" cy="55" r="42" fill={primary} fillOpacity="0.07" />
        <circle cx="50" cy="55" r="42" fill="none" stroke={primary} strokeOpacity="0.35" strokeWidth="0.3" strokeDasharray="1 1" />
      </svg>
      {dots.map((d, i) => (
        <div key={i} style={{
          position: "absolute", left: `${d.x}%`, top: `${d.y}%`, transform: "translate(-50%, -50%)",
          display: "flex", alignItems: "center", gap: 6,
        }}>
          <div style={{
            width: d.r, height: d.r, borderRadius: "50%",
            background: d.primary ? primary : "#0A1628",
            boxShadow: d.primary ? `0 0 0 6px ${primary}33, 0 0 0 14px ${primary}1a` : "none",
            border: d.primary ? "2px solid #fff" : "1.5px solid #fff",
          }} />
          <span style={{
            fontFamily: "Inter, sans-serif",
            fontSize: d.primary ? 13 : 10,
            fontWeight: d.primary ? 700 : 500,
            color: "#0A1628",
            whiteSpace: "nowrap",
            background: "rgba(255,255,255,0.85)",
            padding: "1px 5px",
            borderRadius: 4,
          }}>{d.label}</span>
        </div>
      ))}
    </div>
  );
}

// ---------- Exit-intent ribbon ----------
function ExitRibbon({ primary, accent = "#FF5B1F" }) {
  const [shown, setShown] = React.useState(false);
  const [closed, setClosed] = React.useState(false);
  React.useEffect(() => {
    const onMove = (e) => {
      if (e.clientY < 8 && !closed) setShown(true);
    };
    document.addEventListener("mouseleave", onMove);
    document.addEventListener("mousemove", onMove);
    return () => {
      document.removeEventListener("mouseleave", onMove);
      document.removeEventListener("mousemove", onMove);
    };
  }, [closed]);
  if (!shown || closed) return null;
  return (
    <div style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 9999,
      background: "#0A1628", color: "#fff",
      padding: "14px 20px",
      display: "flex", alignItems: "center", justifyContent: "center", gap: 18,
      fontFamily: "Inter, sans-serif", fontSize: 15,
      boxShadow: "0 8px 24px rgba(0,0,0,0.2)",
      animation: "slideDown 0.4s ease-out",
    }}>
      <span style={{
        background: primary, color: "#0A1628", padding: "4px 10px", borderRadius: 4,
        fontFamily: "JetBrains Mono, monospace", fontSize: 11, fontWeight: 700, letterSpacing: 1.2,
      }}>WAIT!</span>
      <span>Don't leave without claiming your <b>2 rooms for $120</b> deal.</span>
      <a href="#book" onClick={() => setShown(false)} style={{
        background: accent, color: "#fff", padding: "8px 18px", borderRadius: 6,
        textDecoration: "none", fontWeight: 600,
      }}>Claim it now →</a>
      <button onClick={() => setClosed(true)} style={{
        background: "transparent", border: "none", color: "#9aa6b2", cursor: "pointer",
        fontSize: 22, lineHeight: 1, padding: 0,
      }}>×</button>
    </div>
  );
}

Object.assign(window, {
  PHONE_DISPLAY, PHONE_HREF, SERVICE_AREAS,
  useCountdown, BeforeAfter, Stars, Icon, ServiceMap, ExitRibbon,
});
