/* global React, useState, useEffect, useRef, useReveal, fireConfetti */

function Hero() {
  const ref = useRef(null);
  const [time, setTime] = useState(new Date());

  useEffect(() => {
    const t = setInterval(() => setTime(new Date()), 60_000);
    return () => clearInterval(t);
  }, []);

  useEffect(() => {
    let raf;
    const onScroll = () => {
      cancelAnimationFrame(raf);
      raf = requestAnimationFrame(() => {
        const y = window.scrollY;
        const blobs = document.querySelectorAll(".hero-blob");
        blobs.forEach((b, i) => {
          const factor = (i + 1) * 0.15;
          b.style.transform = `translate3d(0, ${y * factor}px, 0)`;
        });
      });
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const hours = time.getHours();
  const isWeekend = [0, 6].includes(time.getDay());
  const onlineStart = isWeekend ? 10 : 14;
  const isOnline = hours >= onlineStart && hours < 21;

  return (
    <section id="top" ref={ref} style={{ position: "relative", paddingTop: 140, paddingBottom: 60, overflow: "hidden" }}>
      <div className="blob hero-blob" style={{ width: 520, height: 520, background: "#a78bfa", top: -120, left: -120, opacity: 0.45 }} />
      <div className="blob hero-blob" style={{ width: 600, height: 600, background: "#f9a8d4", top: 80, right: -200, opacity: 0.4 }} />
      <div className="blob hero-blob" style={{ width: 380, height: 380, background: "#fde68a", bottom: -100, left: "30%", opacity: 0.25 }} />

      <div className="wrap" style={{ position: "relative", zIndex: 1 }}>
        <div className="hero-fade" style={{ animationDelay: "0ms" }}>
          <span className={isOnline ? "pill pill-live" : "pill"}>
            {isOnline ? <><span className="dot" />Heute per Video erreichbar</> : <>Heute Abend per Video erreichbar</>}
          </span>
        </div>

        <h1 className="hero-title hero-fade" style={{ animationDelay: "100ms" }}>
          Technik. <span className="ital grad-text">einfach</span><br />
          gelöst.
        </h1>

        <p className="hero-sub hero-fade" style={{ animationDelay: "250ms" }}>
          Ihr iPhone spinnt, eine App macht Probleme, ein Konto ist gesperrt?<br />
          Hilfe per <strong style={{ color: "var(--ink)", fontWeight: 600 }}>Video-Chat</strong> — schnell, geduldig, von überall.
        </p>

        <div className="hero-ctas hero-fade" style={{ animationDelay: "380ms" }}>
          <a href="kontakt.html" className="btn btn-grad btn-lg">
            Video-Termin anfragen
            <ArrowIcon />
          </a>
          <a href="#problem" className="btn btn-ghost btn-lg">
            Was ist Ihr Problem?
          </a>
        </div>

        <div className="hero-meta hero-fade" style={{ animationDelay: "500ms" }}>
          <MetaItem k="ab 7,90 €" v="Pauschal pro Problem" />
          <MetaItem k="0 €" v="Wenn nicht gelöst" />
          <MetaItem k="per Video" v="Bildschirm teilen" />
          <MetaItem k="< 24h" v="Antwortzeit" />
        </div>
      </div>

      <div className="wrap" style={{ position: "relative", zIndex: 1, marginTop: 80 }}>
        <BentoGrid />
      </div>

      <style>{`
        .hero-title {
          font-size: clamp(56px, 10vw, 144px);
          font-weight: 600;
          line-height: 0.92;
          letter-spacing: -0.05em;
          margin: 24px 0 28px;
          text-wrap: balance;
        }
        .hero-sub {
          font-size: clamp(18px, 1.9vw, 24px);
          color: var(--muted);
          line-height: 1.4;
          max-width: 620px;
          letter-spacing: -0.005em;
          text-wrap: pretty;
        }
        .hero-ctas {
          display: flex;
          gap: 12px;
          margin-top: 40px;
          flex-wrap: wrap;
        }
        .hero-meta {
          display: grid;
          grid-template-columns: repeat(4, 1fr);
          gap: 32px;
          margin-top: 64px;
          max-width: 720px;
        }
        @media (max-width: 720px) {
          .hero-meta { grid-template-columns: repeat(2, 1fr); gap: 24px; }
        }
        .hero-fade {
          opacity: 0;
          transform: translateY(20px);
          animation: hero-in 1s cubic-bezier(.22,.61,.36,1) forwards;
        }
        @keyframes hero-in {
          to { opacity: 1; transform: translateY(0); }
        }
      `}</style>
    </section>
  );
}

function MetaItem({ k, v }) {
  return (
    <div>
      <div style={{ fontSize: "clamp(22px, 2.4vw, 32px)", fontWeight: 600, letterSpacing: "-0.03em", lineHeight: 1 }}>{k}</div>
      <div style={{ fontSize: 13, color: "var(--muted)", marginTop: 8 }}>{v}</div>
    </div>
  );
}

function ArrowIcon() {
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M3 8h10m0 0L8.5 3.5M13 8l-4.5 4.5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

function BentoGrid() {
  const ref = useReveal();
  return (
    <div ref={ref} className="reveal bento">
      <BentoBig />
      <BentoCard className="bento-wlan"><WlanCard /></BentoCard>
      <BentoCard className="bento-android"><AndroidCard /></BentoCard>
      <BentoCard className="bento-pwd"><PasswordCard /></BentoCard>
      <BentoCard className="bento-fotos"><FotosCard /></BentoCard>
      <BentoCard className="bento-vorort"><VorOrtCard /></BentoCard>

      <style>{`
        .bento {
          display: grid;
          grid-template-columns: repeat(6, 1fr);
          grid-auto-rows: 220px;
          gap: 16px;
        }
        .bento > * { border-radius: 24px; overflow: hidden; position: relative; }
        .bento-big { grid-column: span 3; grid-row: span 2; }
        .bento-wlan { grid-column: span 3; }
        .bento-android { grid-column: span 2; }
        .bento-pwd { grid-column: span 2; }
        .bento-fotos { grid-column: span 2; }
        .bento-vorort { grid-column: span 3; }
        @media (max-width: 900px) {
          .bento { grid-template-columns: repeat(2, 1fr); grid-auto-rows: 200px; }
          .bento-big { grid-column: span 2; grid-row: span 2; }
          .bento-wlan, .bento-android, .bento-pwd, .bento-fotos, .bento-vorort { grid-column: span 1; }
          .bento-vorort { grid-column: span 2; }
        }
      `}</style>
    </div>
  );
}

function BentoCard({ children, className = "" }) {
  return (
    <div
      className={"bento-card " + className}
      style={{
        background: "var(--bg-soft)",
        border: "1px solid var(--line)",
        padding: 24,
        transition: "transform .35s cubic-bezier(.22,.61,.36,1), border-color .25s ease, box-shadow .35s ease",
        position: "relative",
        overflow: "hidden",
      }}
      onMouseEnter={(e) => { e.currentTarget.style.transform = "translateY(-3px)"; e.currentTarget.style.borderColor = "var(--line-strong)"; }}
      onMouseLeave={(e) => { e.currentTarget.style.transform = "translateY(0)"; e.currentTarget.style.borderColor = "var(--line)"; }}
    >
      {children}
    </div>
  );
}

function BentoBig() {
  return (
    <div
      className="bento-card bento-big"
      style={{
        background: "linear-gradient(135deg, #1e1b4b 0%, #4c1d95 40%, #831843 100%)",
        color: "#fff",
        padding: 32,
        position: "relative",
        overflow: "hidden",
        cursor: "default",
        transition: "transform .35s cubic-bezier(.22,.61,.36,1)",
      }}
      onMouseEnter={(e) => { e.currentTarget.style.transform = "translateY(-3px)"; }}
      onMouseLeave={(e) => { e.currentTarget.style.transform = "translateY(0)"; }}
    >
      <div className="sparkle" style={{ top: 30, right: 60 }} />
      <div className="sparkle" style={{ top: 80, right: 130, animationDelay: "0.8s" }} />
      <div className="sparkle" style={{ bottom: 60, right: 40, animationDelay: "1.5s" }} />

      <div style={{ position: "relative", zIndex: 2 }}>
        <span style={{ fontSize: 12, fontWeight: 500, letterSpacing: "0.06em", textTransform: "uppercase", color: "rgba(255,255,255,0.7)" }}>
          So funktioniert's
        </span>
        <h3 style={{ fontSize: "clamp(28px, 3.4vw, 44px)", fontWeight: 600, lineHeight: 1.05, letterSpacing: "-0.03em", marginTop: 14, maxWidth: 380, textWrap: "balance" }}>
          Bildschirm teilen.<br />
          Problem <span className="ital" style={{ color: "#fbcfe8" }}>weg.</span>
        </h3>
        <p style={{ marginTop: 16, color: "rgba(255,255,255,0.78)", maxWidth: 340, lineHeight: 1.4, fontSize: 15 }}>
          Keine Hotline, keine Wartezeit, kein Fachchinesisch. Sie teilen Ihren Bildschirm — ich führe Sie Schritt für Schritt zur Lösung.
        </p>
      </div>

      <MiniPhone />

      <style>{`
        .sparkle {
          position: absolute;
          width: 8px; height: 8px;
          background: #fff;
          border-radius: 50%;
          box-shadow: 0 0 12px 2px rgba(255,255,255,0.6);
          animation: sparkle 2.4s ease-in-out infinite;
        }
        @keyframes sparkle {
          0%, 100% { opacity: 0; transform: scale(0); }
          50% { opacity: 1; transform: scale(1); }
        }
      `}</style>
    </div>
  );
}

function MiniPhone() {
  return (
    <div style={{
      position: "absolute",
      right: 28, bottom: -40,
      width: 220, height: 360,
      borderRadius: 36,
      background: "linear-gradient(180deg, #1a1a1a 0%, #2a2a2a 100%)",
      border: "3px solid #444",
      boxShadow: "0 30px 60px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.1)",
      padding: 8,
      transform: "rotate(8deg)",
      animation: "phone-float 5s ease-in-out infinite",
    }}>
      <div style={{ width: "100%", height: "100%", background: "#fff", borderRadius: 28, padding: 16, position: "relative", overflow: "hidden" }}>
        <div style={{ position: "absolute", top: 0, left: "50%", transform: "translateX(-50%)", width: 80, height: 18, background: "#1a1a1a", borderRadius: "0 0 12px 12px" }} />
        <div style={{ display: "flex", justifyContent: "space-between", fontSize: 11, fontWeight: 600, color: "#000", marginTop: 4 }}>
          <span>14:32</span>
          <span>●●● 📶</span>
        </div>
        <div style={{ marginTop: 28, display: "flex", flexDirection: "column", gap: 8 }}>
          <Bubble side="left" delay="0s" color="#f0f0f0">Hallo Paul, mein iPhone lädt nicht mehr 😩</Bubble>
          <Bubble side="right" delay="1.2s" color="linear-gradient(135deg, #7c3aed, #ec4899)" white>Kein Stress — in wenigen Minuten schauen wir das an.</Bubble>
          <Bubble side="left" delay="2.4s" color="#f0f0f0">Vielen Dank!! 🙏</Bubble>
        </div>
      </div>
      <style>{`
        @keyframes phone-float {
          0%, 100% { transform: rotate(8deg) translateY(0); }
          50% { transform: rotate(6deg) translateY(-10px); }
        }
      `}</style>
    </div>
  );
}

function Bubble({ side, color, white, delay, children }) {
  return (
    <div style={{
      alignSelf: side === "right" ? "flex-end" : "flex-start",
      maxWidth: "78%",
      padding: "7px 12px",
      background: color,
      color: white ? "#fff" : "#000",
      fontSize: 11,
      borderRadius: 14,
      borderBottomRightRadius: side === "right" ? 4 : 14,
      borderBottomLeftRadius: side === "left" ? 4 : 14,
      lineHeight: 1.3,
      opacity: 0,
      animation: `bubble-in 0.5s cubic-bezier(.22,.61,.36,1) ${delay} forwards`,
    }}>
      {children}
      <style>{`
        @keyframes bubble-in {
          from { opacity: 0; transform: translateY(8px) scale(0.9); }
          to { opacity: 1; transform: translateY(0) scale(1); }
        }
      `}</style>
    </div>
  );
}

function WlanCard() {
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "space-between" }}>
      <div>
        <Eyebrow>WLAN & Internet</Eyebrow>
        <Title>Endlich Empfang im ganzen Haus.</Title>
      </div>
      <div style={{ position: "absolute", right: 24, bottom: 24, width: 110, height: 110 }}>
        <WlanAnim />
      </div>
    </div>
  );
}

function WlanAnim() {
  return (
    <svg viewBox="0 0 100 100" width="100%" height="100%">
      <g transform="translate(50 70)">
        {[0, 1, 2].map((i) => (
          <path
            key={i}
            d={`M ${-(i + 1) * 18} 0 A ${(i + 1) * 18} ${(i + 1) * 18} 0 0 1 ${(i + 1) * 18} 0`}
            stroke="url(#wlanGrad)"
            strokeWidth="4"
            fill="none"
            strokeLinecap="round"
            style={{ animation: `wlan-pulse 2.4s cubic-bezier(.4,0,.2,1) ${i * 0.3}s infinite`, transformOrigin: "0 0" }}
          />
        ))}
        <circle cx="0" cy="0" r="4" fill="#ec4899" />
      </g>
      <defs>
        <linearGradient id="wlanGrad" x1="0" y1="0" x2="100" y2="0">
          <stop stopColor="#7c3aed" />
          <stop offset="1" stopColor="#ec4899" />
        </linearGradient>
      </defs>
      <style>{`
        @keyframes wlan-pulse {
          0% { opacity: 0; transform: scale(0.6); }
          40% { opacity: 1; }
          100% { opacity: 0; transform: scale(1.15); }
        }
      `}</style>
    </svg>
  );
}

function AndroidCard() {
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "space-between" }}>
      <div>
        <Eyebrow>iPhone & Android</Eyebrow>
        <Title>Egal welches Gerät.</Title>
      </div>
      <div style={{ display: "flex", gap: 8, marginTop: "auto" }}>
        {["iOS", "Android", "Win", "Mac"].map((p, i) => (
          <span key={p} style={{
            padding: "5px 10px", fontSize: 11, borderRadius: 999,
            background: "rgba(124,58,237,0.08)", color: "var(--violet-deep)", fontWeight: 500,
            animation: `chip-in .5s cubic-bezier(.22,.61,.36,1) ${0.2 + i * 0.1}s both`,
          }}>{p}</span>
        ))}
      </div>
      <style>{`
        @keyframes chip-in {
          from { opacity: 0; transform: translateY(8px); }
          to { opacity: 1; transform: translateY(0); }
        }
      `}</style>
    </div>
  );
}

function PasswordCard() {
  const [unlocked, setUnlocked] = useState(false);
  useEffect(() => {
    const t = setInterval(() => setUnlocked((u) => !u), 2800);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "space-between" }}>
      <div>
        <Eyebrow>Passwörter</Eyebrow>
        <Title>Wieder reinkommen.</Title>
      </div>
      <div style={{ alignSelf: "flex-end", fontSize: 56, transition: "transform .4s cubic-bezier(.22,.61,.36,1)", transform: unlocked ? "rotate(-15deg) scale(1.1)" : "rotate(0) scale(1)" }}>
        {unlocked ? "🔓" : "🔒"}
      </div>
    </div>
  );
}

function FotosCard() {
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "space-between" }}>
      <div>
        <Eyebrow>Fotos & Speicher</Eyebrow>
        <Title>Wieder Platz auf dem Handy.</Title>
      </div>
      <div style={{ marginTop: 16, position: "relative", height: 50 }}>
        <div style={{ height: 8, background: "rgba(10,10,10,0.08)", borderRadius: 999 }}>
          <div style={{ height: "100%", width: "20%", background: "var(--grad)", borderRadius: 999, animation: "shrink 2.6s ease-in-out infinite" }} />
        </div>
        <div style={{ display: "flex", justifyContent: "space-between", fontSize: 11, marginTop: 8, color: "var(--muted)" }}>
          <span>2 GB frei</span>
          <span style={{ color: "var(--violet-deep)", fontWeight: 500 }}>→ 48 GB frei</span>
        </div>
      </div>
      <style>{`
        @keyframes shrink {
          0% { width: 95%; }
          50% { width: 20%; }
          100% { width: 95%; }
        }
      `}</style>
    </div>
  );
}

function VorOrtCard() {
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "space-between", position: "relative" }}>
      <div style={{ zIndex: 1, position: "relative" }}>
        <Eyebrow>Per Video</Eyebrow>
        <Title>Von überall. Live. Mit Bildschirm teilen.</Title>
      </div>
      <div style={{ position: "absolute", right: -10, top: 50, width: 180, height: 130, opacity: 0.95 }}>
        <VideoCallVisual />
      </div>
      <div style={{ display: "flex", gap: 6, marginTop: "auto", zIndex: 1, flexWrap: "wrap" }}>
        <span style={{ padding: "5px 10px", fontSize: 11, borderRadius: 999, background: "rgba(124,58,237,0.1)", color: "var(--violet-deep)", fontWeight: 500 }}>FaceTime · Jitsi</span>
        <span style={{ padding: "5px 10px", fontSize: 11, borderRadius: 999, background: "rgba(236,72,153,0.1)", color: "var(--pink-deep)", fontWeight: 500 }}>Vor Ort am Bodensee</span>
      </div>
    </div>
  );
}

function VideoCallVisual() {
  return (
    <svg viewBox="0 0 180 130" width="100%" height="100%">
      <defs>
        <linearGradient id="vc1" x1="0" y1="0" x2="100" y2="80" gradientUnits="userSpaceOnUse">
          <stop stopColor="#7c3aed" /><stop offset="1" stopColor="#a855f7" />
        </linearGradient>
        <linearGradient id="vc2" x1="0" y1="0" x2="80" y2="60" gradientUnits="userSpaceOnUse">
          <stop stopColor="#ec4899" /><stop offset="1" stopColor="#f9a8d4" />
        </linearGradient>
      </defs>
      <g style={{ animation: "vc-bob1 4s ease-in-out infinite" }}>
        <rect x="10" y="10" width="110" height="80" rx="10" fill="url(#vc1)" />
        <circle cx="65" cy="40" r="11" fill="#fff" opacity="0.95" />
        <path d="M48 70 Q65 56 82 70 L82 90 L48 90 Z" fill="#fff" opacity="0.95" />
      </g>
      <g style={{ animation: "vc-bob2 4s ease-in-out infinite" }}>
        <rect x="80" y="55" width="80" height="60" rx="8" fill="url(#vc2)" stroke="#fff" strokeWidth="2" />
        <circle cx="120" cy="80" r="8" fill="#fff" opacity="0.95" />
        <circle cx="155" cy="62" r="3" fill="#22c55e" />
      </g>
      <style>{`
        @keyframes vc-bob1 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }
        @keyframes vc-bob2 { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(2px); } }
      `}</style>
    </svg>
  );
}

function Eyebrow({ children }) {
  return <div style={{ fontSize: 11, fontWeight: 500, letterSpacing: "0.06em", textTransform: "uppercase", color: "var(--muted)" }}>{children}</div>;
}
function Title({ children }) {
  return <h3 style={{ fontSize: 20, fontWeight: 600, letterSpacing: "-0.02em", marginTop: 10, lineHeight: 1.15, textWrap: "balance" }}>{children}</h3>;
}

Object.assign(window, { Hero });
