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

// ============================================================
// Shared primitives: Wordmark, SectionTitle, Placeholder
// ============================================================

function Wordmark({ size = "md" }) {
  return (
    <a href="#top" className="hx-wordmark" style={{ alignItems: "flex-start" }}>
      <span className="hx-wordmark__mark" aria-hidden="true">葉</span>
      <span style={{ display: "flex", flexDirection: "column", lineHeight: 1 }}>
        <span className="hx-wordmark__jp">葉須木クリニック</span>
        <span className="hx-wordmark__en">Hasuki Dental Clinic</span>
      </span>
    </a>
  );
}

function SectionTitle({ jp, en, align = "center", icon = "fa-tooth" }) {
  const cls = "hx-title" + (align === "left" ? " hx-title--left" : "");
  return (
    <div className={cls}>
      <i className={`fa-solid ${icon} sparkle`} aria-hidden="true"></i>
      <h2>{jp}</h2>
      <span className="eyebrow">{en}</span>
    </div>
  );
}

function Placeholder({ label, ratio = "4 / 3", radius = 16, dark = false, style = {} }) {
  return (
    <div
      className={"ph" + (dark ? " ph--dark" : "")}
      style={{
        aspectRatio: ratio,
        borderRadius: radius,
        width: "100%",
        ...style,
      }}
    >
      <span className="ph__label">{label}</span>
    </div>
  );
}

// ============================================================
// Watercolor branch-and-leaves SVG (inline, painterly filter)
// ============================================================
function WatercolorBranch({ seed = 7, style = {} }) {
  const uid = `wc-${seed}`;
  return (
    <svg viewBox="0 0 220 260" xmlns="http://www.w3.org/2000/svg" style={style} aria-hidden="true">
      <defs>
        <filter id={`${uid}-paint`} x="-15%" y="-15%" width="130%" height="130%">
          <feTurbulence type="fractalNoise" baseFrequency="0.014" numOctaves="2" seed={seed} result="noise" />
          <feDisplacementMap in="SourceGraphic" in2="noise" scale="9" result="disp" />
          <feGaussianBlur in="disp" stdDeviation="1.2" />
        </filter>
        <filter id={`${uid}-bloom`} x="-20%" y="-20%" width="140%" height="140%">
          <feGaussianBlur stdDeviation="6" />
        </filter>
        <radialGradient id={`${uid}-leaf`} cx="38%" cy="28%" r="78%">
          <stop offset="0%" stopColor="#9fd3c3" stopOpacity="0.95" />
          <stop offset="55%" stopColor="#3a9689" stopOpacity="0.9" />
          <stop offset="100%" stopColor="#1a5d55" stopOpacity="0.85" />
        </radialGradient>
        <radialGradient id={`${uid}-leaf-light`} cx="45%" cy="35%" r="80%">
          <stop offset="0%" stopColor="#cfe8d9" stopOpacity="0.9" />
          <stop offset="100%" stopColor="#55ad9f" stopOpacity="0.8" />
        </radialGradient>
      </defs>

      {/* soft bleed wash — pigment spreading into paper */}
      <g opacity="0.3" filter={`url(#${uid}-bloom)`}>
        <ellipse cx="115" cy="140" rx="95" ry="115" fill="#a8d4cb" />
        <ellipse cx="150" cy="80" rx="55" ry="45" fill="#cfe8d9" />
      </g>

      {/* branch stem */}
      <g filter={`url(#${uid}-paint)`}>
        <path d="M205 25 Q170 70 140 105 Q108 150 78 195 Q50 228 20 248"
              fill="none" stroke="#2b7a6e" strokeWidth="2.6" strokeLinecap="round" opacity="0.65" />
        <path d="M150 100 Q160 85 175 72" fill="none" stroke="#2b7a6e" strokeWidth="1.6" strokeLinecap="round" opacity="0.55" />
        <path d="M105 160 Q120 145 135 140" fill="none" stroke="#2b7a6e" strokeWidth="1.6" strokeLinecap="round" opacity="0.55" />
        <path d="M68 210 Q55 205 42 212" fill="none" stroke="#2b7a6e" strokeWidth="1.4" strokeLinecap="round" opacity="0.5" />
      </g>

      {/* main leaves — painterly, layered */}
      <g filter={`url(#${uid}-paint)`}>
        <path d="M188 42 C168 30 146 38 138 60 C133 80 156 92 183 80 C200 72 202 54 188 42 Z"
              fill={`url(#${uid}-leaf)`} opacity="0.85" />
        <path d="M155 72 C170 60 188 62 193 78 C193 94 175 100 160 92 C148 86 147 78 155 72 Z"
              fill={`url(#${uid}-leaf-light)`} opacity="0.75" />
        <path d="M138 118 C118 110 94 122 92 146 C94 166 118 172 140 158 C154 146 156 130 138 118 Z"
              fill={`url(#${uid}-leaf)`} opacity="0.88" />
        <path d="M140 140 C152 136 165 146 160 160 C152 170 138 166 134 154 C132 146 134 142 140 140 Z"
              fill={`url(#${uid}-leaf-light)`} opacity="0.7" />
        <path d="M92 198 C70 192 52 208 58 228 C68 242 90 240 104 224 C114 212 110 202 92 198 Z"
              fill={`url(#${uid}-leaf)`} opacity="0.85" />
        <path d="M38 218 C28 216 20 226 26 238 C36 244 48 236 48 228 C46 220 42 218 38 218 Z"
              fill={`url(#${uid}-leaf-light)`} opacity="0.72" />
      </g>

      {/* darker vein accents */}
      <g filter={`url(#${uid}-paint)`} opacity="0.45">
        <path d="M168 52 Q174 62 178 70" fill="none" stroke="#1a5d55" strokeWidth="1" strokeLinecap="round" />
        <path d="M115 135 Q128 145 140 148" fill="none" stroke="#1a5d55" strokeWidth="1" strokeLinecap="round" />
        <path d="M72 212 Q85 220 96 222" fill="none" stroke="#1a5d55" strokeWidth="1" strokeLinecap="round" />
      </g>

      {/* tiny pigment specks */}
      <g opacity="0.4">
        <circle cx="165" cy="60" r="1.8" fill="#1a5d55" />
        <circle cx="120" cy="145" r="2" fill="#1a5d55" />
        <circle cx="80" cy="215" r="1.6" fill="#1a5d55" />
      </g>
    </svg>
  );
}

// ============================================================
// Topbar — horizontal nav (replaces Morinosato's left sidebar)
// Dental sites traditionally use a horizontal header; we keep
// the eyebrow EN+JP stack on each nav item.
// ============================================================
function Topbar() {
  const links = [
    ["当院について", "ABOUT"],
    ["診療案内", "MEDICAL"],
    ["院内紹介", "CLINIC"],
    ["スタッフ", "STAFF"],
    ["アクセス", "ACCESS"],
    ["FAQ", "FAQ"],
  ];
  return (
    <div className="hx-topbar">
      <Wordmark />
      <nav>
        {links.map(([jp, en]) => (
          <a key={jp} href={`#${en.toLowerCase()}`}>
            {jp}
            <span className="eny">{en}</span>
          </a>
        ))}
      </nav>
      <div className="hx-topcta">
        <a className="hx-btn hx-btn--small hx-btn--ghost">TEL 0000-00-0000</a>
        <a className="hx-btn hx-btn--small">WEB予約</a>
      </div>
    </div>
  );
}

// ============================================================
// Hero — JP display headline, Latin tagline, photo placeholder
// A small card hovers over the photo's bottom-left, echoing the
// Morinosato "English OK" callout but for first-visit reassurance.
// ============================================================
function HeroSlideshow() {
  const slides = [
    "assets/img_clinic_fv01.webp",
    "assets/img_clinic_fv02.webp",
    "assets/img_clinic_fv03.webp",
  ];
  const [idx, setIdx] = useState(0);
  useEffect(() => {
    const t = setInterval(() => setIdx(i => (i + 1) % slides.length), 5000);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{ position: "relative", width: "100%", aspectRatio: "13 / 9", borderRadius: 32, overflow: "hidden", boxShadow: "var(--shadow-md)" }}>
      {slides.map((src, i) => (
        <img key={i} src={src} alt="" style={{
          position: "absolute",
          inset: 0,
          width: "100%",
          height: "100%",
          objectFit: "cover",
          opacity: i === idx ? 1 : 0,
          transition: "opacity 1.2s ease-in-out",
          pointerEvents: i === idx ? "auto" : "none",
        }} />
      ))}
      <div style={{
        position: "absolute",
        left: 20,
        bottom: 20,
        display: "flex",
        gap: 6,
      }}>
        {slides.map((_, i) => (
          <span key={i} style={{
            width: i === idx ? 22 : 8,
            height: 4,
            borderRadius: 4,
            background: "rgba(255,255,255,0.9)",
            opacity: i === idx ? 1 : 0.5,
            transition: "all var(--dur-normal) var(--ease-out-soft)",
            boxShadow: "0 1px 2px rgba(0,0,0,0.2)",
          }} />
        ))}
      </div>
    </div>
  );
}

function Hero() {
  return (
    <section className="hx-sec hx-hero" style={{ paddingTop: 40, paddingBottom: 64 }}>
      <div className="hx-hero-grid" style={{ display: "grid", gridTemplateColumns: "460px 1fr", gap: 128, alignItems: "center" }}>
        <div className="hx-hero-copy" style={{ position: "relative", zIndex: 2 }}>
          <div className="hx-eyebrow-latin" style={{ marginBottom: 18 }}>| DENTAL CLINIC |</div>
          <h1 style={{
            fontFamily: "var(--font-jp-sans)",
            fontSize: 46,
            fontWeight: 500,
            color: "var(--fg-brand)",
            letterSpacing: "0.08em",
            lineHeight: 1.55,
            margin: 0,
          }}>
            歯のことで、<br/>
            まよったら。
          </h1>
          <div style={{
            fontFamily: "var(--font-latin)",
            color: "var(--hasu-taupe-500)",
            letterSpacing: "0.14em",
            fontSize: 14,
            marginTop: 20,
          }}>
            Take care of your smile, together.
          </div>
          <p style={{
            fontFamily: "var(--font-jp-sans)",
            color: "var(--fg-2)",
            fontSize: 14,
            lineHeight: 2,
            marginTop: 28,
            maxWidth: 380,
          }}>
            お子さまからご年配の方まで、<br/>
            ご家族みなさまで通える歯科医院です。<br/>
            一人ひとりの歯に寄り添う診療を心がけています。
          </p>
          <div style={{ display: "flex", gap: 12, marginTop: 36 }}>
            <a className="hx-btn">WEB予約 <span className="arr">→</span></a>
            <a className="hx-btn hx-btn--ghost">はじめての方へ</a>
          </div>
        </div>
        <div className="hx-hero-media" style={{ position: "relative", zIndex: 1, minWidth: 0 }}>
          <HeroSlideshow />
          <div className="hx-hero-badge" style={{
            position: "absolute",
            right: 24,
            bottom: 24,
            background: "rgba(255,255,255,0.96)",
            padding: "14px 18px",
            borderRadius: 14,
            fontSize: 12,
            fontFamily: "var(--font-jp-sans)",
            color: "var(--fg-2)",
            lineHeight: 1.65,
            maxWidth: 320,
            boxShadow: "var(--shadow-md)",
          }}>
            <span style={{ color: "var(--hasu-teal-700)", marginRight: 6, fontWeight: 700 }}>✓</span>
            はじめての方も安心してご来院ください。<br/>
            <span style={{ color: "var(--fg-3)", fontSize: 11 }}>First-time patients are always welcome.</span>
          </div>
        </div>
      </div>
    </section>
  );
}

// ============================================================
// Specialty chips strip — a clean dental touch, reminding the
// visitor what this clinic handles. Sits between hero and news.
// ============================================================
function SpecialtyStrip() {
  const items = [
    "一般歯科", "小児歯科", "予防歯科", "矯正歯科", "インプラント",
    "ホワイトニング", "審美歯科", "口腔外科", "訪問歯科", "マウスピース",
  ];
  return (
    <section className="hx-sec--tight" style={{ padding: "28px 0 40px" }}>
      <div style={{
        display: "flex",
        flexWrap: "wrap",
        gap: 8,
        justifyContent: "center",
        borderTop: "1px dashed var(--border-hairline)",
        borderBottom: "1px dashed var(--border-hairline)",
        padding: "22px 0",
      }}>
        {items.map(i => <span key={i} className="hx-tag">{i}</span>)}
      </div>
    </section>
  );
}

window.Wordmark = Wordmark;
window.SectionTitle = SectionTitle;
window.Placeholder = Placeholder;
window.WatercolorBranch = WatercolorBranch;
window.Topbar = Topbar;
window.Hero = Hero;
window.SpecialtyStrip = SpecialtyStrip;
