/* Shared UI components, v1 visual language. */
const { useState, useEffect, useRef } = React;

/* Hover preview for a YouTube reel embed.
   Toggling autoplay in the src reliably starts a muted preview on hover and
   resets to YouTube's own thumbnail on mouse-out. While the clip is actually
   playing, YouTube hides its title/channel overlay, so the video stays clear. */
function reelPreview(card, on) {
  const iframe = card && card.querySelector("iframe");
  if (!iframe) return;
  const base = iframe.dataset.base;
  if (!base) return;
  const next = on ? base + "&autoplay=1" : base;
  if (iframe.getAttribute("src") !== next) iframe.setAttribute("src", next);
}

/* ---------- Icons ---------- */
function Icon({ name, size = 22, stroke = 1.7, style, fill = "none" }) {
  const p = { width: size, height: size, viewBox: "0 0 24 24", fill, stroke: "currentColor",
    strokeWidth: stroke, strokeLinecap: "round", strokeLinejoin: "round", style };
  const paths = {
    arrow: <><line x1="5" y1="12" x2="19" y2="12" /><polyline points="13 6 19 12 13 18" /></>,
    chevronRight: <polyline points="9 6 15 12 9 18" />,
    phone: <path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3-8.6A2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1.9.3 1.8.6 2.6a2 2 0 0 1-.5 2.1L8.1 9.5a16 16 0 0 0 6 6l1.1-1.1a2 2 0 0 1 2.1-.5c.8.3 1.7.5 2.6.6a2 2 0 0 1 1.7 2z" />,
    pin: <><path d="M12 21s-7-5.6-7-11a7 7 0 1 1 14 0c0 5.4-7 11-7 11z" /><circle cx="12" cy="10" r="2.5" /></>,
    building: <path d="M3 21h18M5 21V7l8-4 8 4v14M9 21v-6h6v6" />,
    calendar: <><rect x="3" y="5" width="18" height="16" rx="2" /><line x1="3" y1="9" x2="21" y2="9" /><line x1="8" y1="3" x2="8" y2="6" /><line x1="16" y1="3" x2="16" y2="6" /></>,
    clock: <><circle cx="12" cy="12" r="9" /><polyline points="12 7 12 12 15 14" /></>,
    check: <polyline points="5 13 10 18 19 7" />,
    plus: <><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></>,
    close: <><line x1="6" y1="6" x2="18" y2="18" /><line x1="18" y1="6" x2="6" y2="18" /></>,
    menu: <path d="M4 7h16M4 12h16M4 17h16" />,
    mail: <><path d="M3 5h18v14H3z" /><path d="M3 7l9 6 9-6" /></>,
    shield: <><path d="M12 3l8 4v6c0 5-3.5 7.5-8 8-4.5-.5-8-3-8-8V7z" /><path d="M9 12l2 2 4-4" /></>,
    heart: <path d="M12 20s-7-4.5-7-9.5A3.8 3.8 0 0 1 12 7a3.8 3.8 0 0 1 7 3.5C19 15.5 12 20 12 20z" />,
    brain: <path d="M9 3a4 4 0 0 0-4 4c-1.5.5-2 2-1 3.5-1 1.5 0 3 1.5 3.5 0 2 2 3 4 3M15 3a4 4 0 0 1 4 4c1.5.5 2 2 1 3.5 1 1.5 0 3-1.5 3.5 0 2-2 3-4 3M9 3a3 3 0 0 1 6 0M12 3v14" />,
    spine: <path d="M12 2v20M9 4h6M8 8h8M9 12h6M8 16h8M9 20h6" />,
    bolt: <path d="M4 12h4l2-7 4 14 2-7h4" />,
    play: <path d="M8 5v14l11-7z" />,
    user: <><circle cx="12" cy="8" r="4" /><path d="M4 20c0-4 4-6 8-6s8 2 8 6" /></>,
    home: <><path d="M3.8 11.8c-.35-.32-.36-.86-.02-1.18L10.9 4.2a1.6 1.6 0 0 1 2.2 0l7.12 6.42c.34.32.33.86-.02 1.18" /><path d="M6.2 10.4v7.6c0 1.33 1.07 2.4 2.4 2.4h6.8c1.33 0 2.4-1.07 2.4-2.4v-7.6" /><path d="M9.6 20.4v-3.7c0-.99.81-1.8 1.8-1.8h1.2c.99 0 1.8.81 1.8 1.8v3.7" /></>,
    star: <path d="M12 3.2l2.6 5.3 5.8.8-4.2 4.1 1 5.8L12 16.5l-5.2 2.7 1-5.8-4.2-4.1 5.8-.8z" />,
    award: <><circle cx="12" cy="9" r="5.5" /><path d="M9 13.5 7.5 21 12 18.5 16.5 21 15 13.5" /></>,
    doc: <><path d="M6 2h8l4 4v16H6z" /><path d="M14 2v4h4M9 12h6M9 16h6" /></>,
    cap: <path d="M12 4 2 9l10 5 10-5-10-5zM6 11v5c0 1.5 2.7 3 6 3s6-1.5 6-3v-5" />,
    whatsapp: <path fill="currentColor" stroke="none" d="M12.04 2c-5.46 0-9.91 4.45-9.91 9.91 0 1.75.46 3.45 1.32 4.95L2 22l5.25-1.38c1.45.79 3.08 1.21 4.79 1.21 5.46 0 9.91-4.45 9.91-9.91S17.5 2 12.04 2zm0 18.15c-1.53 0-3.03-.41-4.34-1.19l-.31-.18-3.12.82.83-3.04-.2-.31a8.2 8.2 0 0 1-1.26-4.36c0-4.54 3.7-8.23 8.24-8.23 2.2 0 4.27.86 5.82 2.42a8.18 8.18 0 0 1 2.41 5.82c0 4.54-3.7 8.23-8.24 8.23zm4.52-6.16c-.25-.12-1.47-.72-1.69-.81-.23-.08-.39-.12-.56.12-.16.25-.64.81-.79.97-.14.17-.29.19-.54.06-.25-.12-1.05-.39-1.99-1.23-.74-.66-1.23-1.47-1.38-1.72-.14-.25-.02-.38.11-.51.11-.11.25-.29.37-.43.12-.14.16-.25.25-.41.08-.17.04-.31-.02-.43-.06-.12-.56-1.34-.76-1.84-.2-.48-.41-.42-.56-.43h-.48c-.17 0-.43.06-.66.31-.23.25-.86.85-.86 2.07 0 1.22.89 2.4 1.01 2.56.12.17 1.75 2.67 4.23 3.74.59.26 1.05.41 1.41.52.59.19 1.13.16 1.56.1.48-.07 1.47-.6 1.68-1.18.21-.58.21-1.07.14-1.18-.06-.11-.22-.17-.47-.29z" />,
    youtube: <><path fill="#FF0000" stroke="none" d="M23.5 7.2a2.9 2.9 0 0 0-2.05-2.05C19.5 4.7 12 4.7 12 4.7s-7.5 0-9.45.45A2.9 2.9 0 0 0 .5 7.2 30.6 30.6 0 0 0 0 12a30.6 30.6 0 0 0 .5 4.8 2.9 2.9 0 0 0 2.05 2.05C4.5 19.3 12 19.3 12 19.3s7.5 0 9.45-.45a2.9 2.9 0 0 0 2.05-2.05A30.6 30.6 0 0 0 24 12a30.6 30.6 0 0 0-.5-4.8z" /><path fill="#fff" stroke="none" d="M9.75 15.02V8.98L15.5 12z" /></>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
}

/* Latest platform logos — hard-coded brand fills (no opacity washout) */
function SocialBrandIcon({ brand, size = 18 }) {
  const s = { width: size, height: size, viewBox: "0 0 24 24", "aria-hidden": true, focusable: "false" };
  if (brand === "instagram") {
    return (
      <svg {...s}>
        <defs>
          <linearGradient id="hdr-ig" x1="0%" y1="100%" x2="100%" y2="0%">
            <stop offset="0%" stopColor="#FFD600" />
            <stop offset="30%" stopColor="#FF0069" />
            <stop offset="60%" stopColor="#D300C5" />
            <stop offset="100%" stopColor="#7638FA" />
          </linearGradient>
        </defs>
        <path fill="url(#hdr-ig)" d="M12 0C8.74 0 8.333.015 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.74 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0 3.675a6.162 6.162 0 1 0 0 12.324 6.162 6.162 0 0 0 0-12.324zM12 16a4 4 0 1 1 0-8 4 4 0 0 1 0 8zm6.406-11.845a1.44 1.44 0 1 0 0 2.881 1.44 1.44 0 0 0 0-2.881z" />
      </svg>
    );
  }
  if (brand === "linkedin") {
    return (
      <svg {...s}>
        <path fill="#0A66C2" d="M6.94 5a2 2 0 1 1-4-.002 2 2 0 0 1 4 .002zM3.5 8.75h3v12h-3v-12zM9.75 8.75h2.86v1.64h.04c.4-.75 1.37-1.54 2.82-1.54 3.02 0 3.58 1.99 3.58 4.57v7.33h-3v-6.5c0-1.55-.03-3.54-2.16-3.54-2.16 0-2.49 1.69-2.49 3.43v6.61h-3v-12z" />
      </svg>
    );
  }
  if (brand === "youtube") {
    return (
      <svg {...s}>
        <path fill="#FF0000" d="M23.5 6.5a3.05 3.05 0 0 0-2.15-2.16C19.4 3.9 12 3.9 12 3.9s-7.4 0-9.35.44A3.05 3.05 0 0 0 .5 6.5 31 31 0 0 0 0 12a31 31 0 0 0 .5 5.5 3.05 3.05 0 0 0 2.15 2.16C4.6 20.1 12 20.1 12 20.1s7.4 0 9.35-.44a3.05 3.05 0 0 0 2.15-2.16A31 31 0 0 0 24 12a31 31 0 0 0-.5-5.5z" />
        <path fill="#FFFFFF" d="M9.75 15.4V8.6L16.4 12z" />
      </svg>
    );
  }
  if (brand === "x") {
    return (
      <svg {...s}>
        <path fill="#000000" d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z" />
      </svg>
    );
  }
  return null;
}

function Stars({ n = 5 }) {return <span className="stars">{"★".repeat(n)}</span>;}

function StoryCard({ story, expanded, onToggle, readMoreLabel, onReadMore }) {
  const expandable = typeof onToggle === "function";
  const clamped = expandable ? !expanded : true;
  const longQuote = story.quote.length > 280;
  return (
    <article className="home-story-card reveal">
      <Stars />
      <span className="home-story-tag">{story.treatment}</span>
      <p className={"home-story-text" + (clamped ? "" : " is-expanded")}>&ldquo;{story.quote}&rdquo;</p>
      <div className="who">
        <div className="av">{story.initial}</div>
        <div><b>{story.name}</b><small>{story.detail}</small></div>
      </div>
      {expandable && longQuote &&
        <button type="button" className="home-story-link" onClick={onToggle}>
          {expanded ? "Read less" : "Read more"} <Icon name="arrow" size={14} />
        </button>
      }
      {readMoreLabel && onReadMore &&
        <button type="button" className="home-story-link" onClick={onReadMore}>
          {readMoreLabel} <Icon name="arrow" size={14} />
        </button>
      }
    </article>
  );
}

/* ---------- Placeholder image ---------- */
function Ph({ label, ratio = "4 / 3", round = 16, style, className = "" }) {
  return (
    <div className={"ph " + className} style={{ aspectRatio: ratio, borderRadius: round, ...style }}>
      <span className="ph-tag">{label}</span>
    </div>);

}

/* ---------- Button ---------- */
function Btn({ children, variant = "primary", onClick, icon, type = "button", block, href, newTab, disabled, contact }) {
  const cls = `btn${variant !== "primary" ? " btn-" + variant : ""}${block ? " btn-block" : ""}`;
  const ref = useRef(null);
  useMagnetic(ref);
  const inner = <>{children}{icon && <Icon name={icon} size={16} />}</>;
  // Text color follows the variant so light/ghost buttons aren't forced to white.
  // Primary sits on a dark fill -> white; light/ghost sit on pale fills -> deep teal.
  const txt = (variant === "light" || variant === "ghost") ? "var(--deep)" : "#fff";
  if (contact) {
    return (
      <ContactLink
        kind={contact}
        className={cls}
        style={{ color: txt }}
        newTab={newTab || contact === "whatsapp"}
        onClick={onClick}
        magRef={ref}
      >
        {inner}
      </ContactLink>
    );
  }
  if (href) return <a ref={ref} className={cls} href={href} style={{ color: txt }} target={newTab ? "_blank" : undefined} rel={newTab ? "noopener noreferrer" : undefined}>{inner}</a>;
  return <button ref={ref} type={type} className={cls} onClick={onClick} style={{ color: txt }} disabled={disabled}>{inner}</button>;
}

/* ---------- Protected contact links (anti-scrape) ---------- */
function ContactText({ kind }) {
  return ContactProtect.label(kind === "email" ? "email" : "phone");
}

function ContactLink({
  kind,
  className,
  children,
  icon,
  iconSize = 16,
  newTab,
  onClick,
  style,
  role,
  "aria-label": ariaLabel,
  magRef,
}) {
  const localRef = useRef(null);
  const ref = magRef || localRef;
  const [href, setHref] = useState("");

  const arm = () => {
    const next = ContactProtect.href(kind);
    setHref(next);
    return next;
  };

  const handleClick = (e) => {
    const next = href || arm();
    if (onClick) onClick(e);
    if (!href) {
      e.preventDefault();
      if (newTab) window.open(next, "_blank", "noopener,noreferrer");
      else window.location.href = next;
    }
  };

  return (
    <a
      ref={ref}
      className={className}
      href={href || "#"}
      style={style}
      role={role}
      aria-label={ariaLabel}
      target={newTab && href ? "_blank" : undefined}
      rel={newTab ? "noopener noreferrer nofollow" : "nofollow"}
      onClick={handleClick}
      onMouseEnter={arm}
      onFocus={arm}
      onTouchStart={arm}
      data-contact={kind}
    >
      {icon ? <Icon name={icon} size={iconSize} /> : null}
      {children}
    </a>
  );
}

function Eyebrow({ children, light }) {
  return <span className={"eyebrow" + (light ? " eyebrow-light" : "")}>{children}</span>;
}

/* ---------- Header ---------- */
function Header({ view, go }) {
  const [open, setOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);
  const [contactOpen, setContactOpen] = useState(false);
  const contactRef = useRef(null);
  const brandAlignRef = useRef(null);
  const brandTagRef = useRef(null);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener("scroll", onScroll);onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  useEffect(() => {
    const fit = () => {
      const align = brandAlignRef.current;
      const tag = brandTagRef.current;
      if (!align || !tag) return;
      tag.style.letterSpacing = "0px";
      tag.style.maxWidth = "none";
      const alignW = align.getBoundingClientRect().width;
      const tagW = tag.getBoundingClientRect().width;
      const chars = Math.max(tag.textContent.length - 1, 1);
      if (tagW <= alignW) {
        const next = Math.max(0, (alignW - tagW) / chars);
        tag.style.letterSpacing = `${next.toFixed(3)}px`;
        tag.style.maxWidth = "none";
      } else {
        tag.style.letterSpacing = "0px";
        tag.style.maxWidth = `${alignW}px`;
      }
    };
    fit();
    window.addEventListener("resize", fit, { passive: true });
    if (document.fonts) document.fonts.ready.then(fit);
    return () => window.removeEventListener("resize", fit);
  }, [scrolled, open]);

  useEffect(() => {
    if (!contactOpen) return;
    const onDoc = (e) => {
      if (contactRef.current && !contactRef.current.contains(e.target)) setContactOpen(false);
    };
    const onKey = (e) => {
      if (e.key === "Escape") setContactOpen(false);
    };
    document.addEventListener("mousedown", onDoc);
    document.addEventListener("keydown", onKey);
    return () => {
      document.removeEventListener("mousedown", onDoc);
      document.removeEventListener("keydown", onKey);
    };
  }, [contactOpen]);

  useEffect(() => {
    setContactOpen(false);
  }, [view, open]);

  const links = [
  ["home", "Home"], ["about", "About"], ["speciality", "Speciality"], ["why-us", "Why Us?"],
  ["media", "Social"], ["stories", "Patient Stories"]];

  const nav = (id) => {go(id);setOpen(false);};
  const mapsUrl = (query) => `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(query)}`;
  const mapKphb = mapsUrl(LOCATIONS[0].mapQuery);
  const mapHitech = mapsUrl(LOCATIONS[1].mapQuery);
  return (
    <header className={"hdr" + (scrolled ? " scrolled" : "") + (open ? " menu-open" : "")}>
      <div className="hdr-inner">
        <button className="brand" onClick={() => nav("home")} aria-label="Home">
          <img className="brand-mark" src="logo-embed-nav.png" alt="" />
          <span className="brand-words">
            <span className="brand-name">
              <span className="brand-name-main" ref={brandAlignRef}>DR.RAJASEKHAR</span>
              <span className="brand-name-rest"> REDDY&apos;s</span>
            </span>
            <small className="brand-tag" ref={brandTagRef}>NEUROSPINE CARE</small>
          </span>
        </button>
        <nav className="nav-links" aria-label="Main navigation">
          {links.map(([id, label]) =>
            <button
              key={id}
              type="button"
              className={"navlink" + (view === id ? " active" : "") + (id === "home" ? " navlink--icon" : "")}
              onClick={() => nav(id)}
              aria-label={id === "home" ? "Home" : undefined}
            >
              {id === "home" ? <Icon name="home" size={21} stroke={view === id ? 1.9 : 1.65} /> : label}
            </button>
          )}
          <Btn onClick={() => nav("booking")}>Appointment</Btn>
        </nav>
        <button className="menu-toggle" onClick={() => setOpen(!open)} aria-label={open ? "Close menu" : "Open menu"} aria-expanded={open}>
          <Icon name={open ? "close" : "menu"} size={26} stroke={2} />
        </button>
      </div>
      <div className="hdr-sub">
        <div className="hdr-sub-inner">
          <span className="hdr-sub-call-ic" aria-hidden="true"><Icon name="phone" size={14} /></span>
          <span className="hdr-sub-contact" ref={contactRef}>
            <button
              type="button"
              className="hdr-sub-phone"
              aria-haspopup="menu"
              aria-expanded={contactOpen}
              aria-label="Appointment contact options"
              onClick={() => setContactOpen((v) => !v)}
            >
              <ContactText kind="phone" />
            </button>
            {contactOpen &&
              <div className="hdr-sub-menu" role="menu" aria-label="Contact options">
                <ContactLink
                  kind="whatsapp"
                  role="menuitem"
                  className="hdr-sub-menu-item hdr-sub-menu-item--wa"
                  newTab
                  onClick={() => setContactOpen(false)}
                >
                  <Icon name="whatsapp" size={16} /> WhatsApp
                </ContactLink>
                <ContactLink
                  kind="phone"
                  role="menuitem"
                  className="hdr-sub-menu-item"
                  onClick={() => setContactOpen(false)}
                >
                  <Icon name="phone" size={16} /> Call
                </ContactLink>
              </div>
            }
          </span>
          <span className="hdr-sub-sep" aria-hidden="true" />
          <a className="hdr-sub-map-ic" href={mapKphb} target="_blank" rel="noopener noreferrer" aria-label="Open clinic on Google Maps">
            <Icon name="pin" size={14} />
          </a>
          <a className="hdr-sub-map-link" href={mapKphb} target="_blank" rel="noopener noreferrer">KPHB</a>
          <span className="hdr-sub-map-dot" aria-hidden="true">·</span>
          <a className="hdr-sub-map-link" href={mapHitech} target="_blank" rel="noopener noreferrer">Hi-tech city</a>
          <nav className="hdr-sub-social" aria-label="Social media">
            {[
              ["instagram", "Instagram", CLINIC.social && CLINIC.social.instagram],
              ["linkedin", "LinkedIn", CLINIC.social && CLINIC.social.linkedin],
              ["youtube", "YouTube", CLINIC.social && CLINIC.social.youtube],
              ["x", "X", CLINIC.social && CLINIC.social.x],
            ].map(([brand, label, href]) =>
              <a
                key={label}
                className={"hdr-sub-social-link hdr-sub-social-link--" + brand + (href ? "" : " is-pending")}
                href={href || "#"}
                target={href ? "_blank" : undefined}
                rel={href ? "noopener noreferrer" : undefined}
                aria-label={href ? label : label + " (link coming soon)"}
                aria-disabled={href ? undefined : "true"}
                onClick={href ? undefined : (e) => e.preventDefault()}
              >
                <SocialBrandIcon brand={brand} size={18} />
              </a>
            )}
          </nav>
        </div>
      </div>
      {open &&
      <div className="hdr-mobile">
          {links.map(([id, label]) =>
        <button key={id} className={"navlink-m" + (view === id ? " active" : "")} onClick={() => nav(id)}>{label}</button>
        )}
          <div style={{ marginTop: 16 }}><Btn block onClick={() => nav("booking")} icon="calendar">Appointment</Btn></div>
        </div>
      }
    </header>);

}

/* ---------- Reel carousel (shared by Home + Media) ---------- */
function ReelCarousel() {
  const trackRef = useRef(null);
  const base = (id) =>
    `https://www.youtube.com/embed/${id}?playsinline=1&mute=1&rel=0&controls=0&modestbranding=1&iv_load_policy=3`;

  const scroll = (dir) => {
    const el = trackRef.current;
    if (!el) return;
    const card = el.querySelector(".reel-card");
    const step = card ? (card.offsetWidth + 16) * 2 : 320;
    const max = el.scrollWidth - el.clientWidth;
    const target = Math.max(0, Math.min(max, el.scrollLeft + dir * step));
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
      el.scrollLeft = target;
      return;
    }
    const start = el.scrollLeft;
    const dist = target - start;
    const dur = 380;
    let t0 = null;
    const ease = (p) => 1 - Math.pow(1 - p, 3);
    const tick = (ts) => {
      if (t0 === null) t0 = ts;
      const p = Math.min(1, (ts - t0) / dur);
      el.scrollLeft = start + dist * ease(p);
      if (p < 1) requestAnimationFrame(tick);
    };
    requestAnimationFrame(tick);
  };

  return (
    <div className="reel-carousel-wrap reveal">
      <button type="button" className="reel-arrow reel-arrow--prev" aria-label="Previous reels" onClick={() => scroll(-1)}>
        <Icon name="chevronRight" size={22} style={{ transform: "rotate(180deg)" }} />
      </button>
      <div className="reel-carousel" role="list" aria-label="Short-form video reels" ref={trackRef}>
        {MEDIA_REELS.map(r => (
          <div
            key={r.id}
            className="reel-card"
            role="listitem"
            onMouseEnter={(e) => reelPreview(e.currentTarget, true)}
            onMouseLeave={(e) => reelPreview(e.currentTarget, false)}
          >
            <div className="reel-thumb">
              <iframe
                className="reel-iframe"
                src={base(r.videoId)}
                data-base={base(r.videoId)}
                title={r.title}
                loading="lazy"
                allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
                allowFullScreen
              ></iframe>
            </div>
          </div>
        ))}
      </div>
      <button type="button" className="reel-arrow reel-arrow--next" aria-label="Next reels" onClick={() => scroll(1)}>
        <Icon name="chevronRight" size={22} />
      </button>
    </div>
  );
}

/* ---------- Footer ---------- */
function Footer({ go }) {
  const year = new Date().getFullYear();
  const brandAlignRef = useRef(null);
  const brandTagRef = useRef(null);

  useEffect(() => {
    const fit = () => {
      const align = brandAlignRef.current;
      const tag = brandTagRef.current;
      if (!align || !tag) return;
      tag.style.letterSpacing = "0px";
      tag.style.maxWidth = "none";
      const alignW = align.getBoundingClientRect().width;
      const tagW = tag.getBoundingClientRect().width;
      const chars = Math.max(tag.textContent.length - 1, 1);
      if (tagW <= alignW) {
        const next = Math.max(0, (alignW - tagW) / chars);
        tag.style.letterSpacing = `${next.toFixed(3)}px`;
        tag.style.maxWidth = "none";
      } else {
        tag.style.letterSpacing = "0px";
        tag.style.maxWidth = `${alignW}px`;
      }
    };
    fit();
    window.addEventListener("resize", fit, { passive: true });
    if (document.fonts) document.fonts.ready.then(fit);
    return () => window.removeEventListener("resize", fit);
  }, []);

  return (
    <footer className="footer">
      <div className="wrap">
        <div className="foot-grid">
          <div className="foot-brand">
            <button className="brand" onClick={() => go("home")} aria-label="Home">
              <img className="brand-mark" src="logo-embed-foot.png" alt="" />
              <span className="brand-words">
                <span className="brand-name">
                  <span className="brand-name-main" ref={brandAlignRef}>DR.RAJASEKHAR</span>
                  <span className="brand-name-rest"> REDDY&apos;s</span>
                </span>
                <small className="brand-tag" ref={brandTagRef}>NEUROSPINE CARE</small>
              </span>
            </button>
            <div className="foot-brand-contact">
              <ContactLink kind="email" className="foot-link" icon="mail" iconSize={16}>
                <ContactText kind="email" />
              </ContactLink>
              <ContactLink kind="phone" className="foot-link" icon="phone" iconSize={16}>
                <ContactText kind="phone" />
              </ContactLink>
            </div>

            {/* ---- Social media placeholder: add profile URLs here later ---- */}
            <div className="foot-social" aria-label="Social media">
              {/*
              <a className="foot-social-link" href="INSTAGRAM_URL" aria-label="Instagram" target="_blank" rel="noopener">Instagram</a>
              <a className="foot-social-link" href="FACEBOOK_URL" aria-label="Facebook" target="_blank" rel="noopener">Facebook</a>
              <a className="foot-social-link" href="YOUTUBE_URL" aria-label="YouTube" target="_blank" rel="noopener">YouTube</a>
              */}
            </div>
          </div>

          <div className="foot-loc">
            <h4>Neuro &amp; Spine Clinic, KPHB</h4>
            <p className="foot-addr">
              1st Floor, Road No. 4, Beside Vijaya Diagnostics,<br />
              KPHB Colony, Hyderabad, Telangana 500072
            </p>
            <p className="foot-contact">
              <ContactLink kind="phone" className="foot-link" icon="phone" iconSize={15}>
                <ContactText kind="phone" />
              </ContactLink><br />
              Mon–Fri: 5:30pm – 8:30pm<br />
              Sat: 5:30pm – 9:00pm<br />
              Sunday closed
            </p>
            <a className="foot-directions" href="https://maps.app.goo.gl/RYNaKggBk4DuXq838" target="_blank" rel="noopener">
              <Icon name="pin" size={15} /> Get directions
            </a>
          </div>

          <div className="foot-loc">
            <h4>Yashoda Hospitals, Hitech City</h4>
            <p className="foot-addr">
              Hi-Tech City, Hyderabad, Telangana 500084
            </p>
            <p className="foot-contact">
              <ContactLink kind="phone" className="foot-link" icon="phone" iconSize={15}>
                <ContactText kind="phone" />
              </ContactLink><br />
              Hours: Open 24 hours
            </p>
            <a className="foot-directions" href="https://maps.app.goo.gl/Nbsqo89E35CnyP4H7" target="_blank" rel="noopener">
              <Icon name="pin" size={15} /> Get directions
            </a>
          </div>
        </div>

        <div className="foot-bottom">
          <p className="foot-copy">&copy; {year} Dr. Rajasekhar Reddy Neuro Spine Care.</p>
          <nav className="foot-legal" aria-label="Legal">
            <button type="button" className="foot-legal-link" onClick={() => go("privacy")}>Privacy Policy</button>
          </nav>
        </div>
      </div>
    </footer>);
}

/* ---------- Page hero ---------- */
function PageHero({ eyebrow, title, sub }) {
  return (
    <section className="pagehero" style={{ padding: undefined }}>
      <div className="wrap pagehero-inner">
        {eyebrow && <Eyebrow light>{eyebrow}</Eyebrow>}
        <h1>{title}</h1>
        {sub && <p>{sub}</p>}
      </div>
    </section>);

}

/* ---------- Locations ---------- */
function LocationsSection({ go }) {
  return (
    <section className="soft-bg locations-section">
      <div className="wrap">
        <div className="sec-head reveal">
          <Eyebrow>Where to find Dr. Reddy</Eyebrow>
          <h2>Two trusted locations across the city.</h2>
        </div>
        <div className="card-grid card-grid--2">
          {LOCATIONS.map(l => (
            <div className="card location-card reveal" key={l.name} style={{ cursor: "default" }}>
              <div className="location-card-main">
                <div className="ic"><Icon name={l.icon} size={26} /></div>
                <h3>{l.name}</h3>
                <p>{l.line1}, {l.note.toLowerCase()}.</p>
                <div className="locations-cta">
                  <Btn variant="ghost" icon="arrow" onClick={() => go("booking")}>Directions &amp; hours</Btn>
                </div>
              </div>
              <div className="location-card-map">
                <iframe
                  title={`Map: ${l.name}`}
                  src={`https://www.google.com/maps?q=${encodeURIComponent(l.mapQuery)}&output=embed`}
                  loading="lazy"
                  referrerPolicy="no-referrer-when-downgrade"
                  allowFullScreen
                />
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------- CTA band ---------- */
function CTABand({ go }) {
  return (
    <section className="ctaband">
      <SpineGraphic />
      <div className="wrap ctaband-inner">
        <div>
          <Eyebrow light>Take the next step</Eyebrow>
          <h2>You don't have to live with the pain.<br />Get a diagnosis that actually explains things.</h2>
          <p>Appointment with {CLINIC.doctorShort}, clear answers, honest options, and surgery only when it's truly the right choice.</p>
        </div>
        <div className="ctaband-actions">
          <Btn variant="light" icon="calendar" onClick={() => go("booking")}>Appointment</Btn>
          <ContactLink kind="phone" className="ctaband-call" icon="phone" iconSize={18}>
            Call <ContactText kind="phone" />
          </ContactLink>
        </div>
      </div>
    </section>);

}

/* ---------- Scroll assist (back to top + WhatsApp) ---------- */
function ScrollAssist({ view }) {
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 320);
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  useEffect(() => {
    setScrolled(false);
  }, [view]);

  const toTop = () => {
    window.scrollTo({ top: 0, behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "auto" : "smooth" });
  };

  return (
    <div className="scroll-assist in">
      <button
        type="button"
        className={"scroll-to-top" + (scrolled ? "" : " is-pending")}
        aria-label="Back to top"
        aria-hidden={!scrolled}
        tabIndex={scrolled ? 0 : -1}
        onClick={toTop}
      >
        <Icon name="chevronRight" size={20} style={{ transform: "rotate(-90deg)" }} />
      </button>
      <ContactLink
        kind="whatsapp"
        className="scroll-wa"
        newTab
        aria-label="Message us on WhatsApp"
        icon="whatsapp"
        iconSize={24}
      />
    </div>
  );
}

/* ---------- Mobile bottom navigation ---------- */
function MobileBottomNav({ view, go }) {
  const items = [
    { id: "home", label: "Home", icon: "home" },
    { id: "why-us", label: "Why Us", icon: "shield" },
    { id: "stories", label: "Reviews", icon: "star" },
    { id: "media", label: "Shorts", icon: "youtube", brand: true },
  ];
  return (
    <nav className="mobile-bottom-nav" aria-label="Primary">
      {items.map((item) => {
        const isActive = view === item.id;
        return (
          <button
            key={item.id}
            type="button"
            className={"mobile-bottom-nav-item" + (isActive ? " is-active" : "") + (item.brand ? " mobile-bottom-nav-item--brand" : "")}
            aria-current={isActive ? "page" : undefined}
            onClick={() => go(item.id)}
          >
            <Icon name={item.icon} size={22} stroke={item.brand ? 0 : (isActive ? 2 : 1.7)} />
            <span>{item.label}</span>
          </button>
        );
      })}
    </nav>
  );
}

/* ---------- Reveal-on-scroll hook ---------- */
function useReveal() {
  useEffect(() => {
    const io = new IntersectionObserver((es) => es.forEach((e) => {
      if (e.isIntersecting) {e.target.classList.add("in");io.unobserve(e.target);}
    }), { threshold: 0.12 });
    document.querySelectorAll(".reveal:not(.in)").forEach((el, i) => {
      el.style.transitionDelay = i % 4 * 60 + "ms";
      io.observe(el);
    });
    return () => io.disconnect();
  });
}

Object.assign(window, { Icon, Stars, Ph, Btn, Eyebrow, Header, Footer, PageHero, LocationsSection, CTABand, ScrollAssist, MobileBottomNav, ReelCarousel, StoryCard, useReveal });