// Shared components: iPhone screenshot frame, animated gradient bg, particles, section header

const APP_STORE_URL = "https://apps.apple.com/app/digme/id6759730270";

function DigmeMark({ size = 32 }) {
  return (
    <img src="assets/digme-logo.png" alt="DIGME"
      width={size} height={size}
      style={{
        display: 'block', flexShrink: 0,
        width: size, height: size,
        borderRadius: size * 0.22,
        objectFit: 'cover',
      }} />
  );
}

function AppStoreBadge({ accent = '#FF6B1A', compact = false }) {
  return (
    <a href={APP_STORE_URL} target="_blank" rel="noopener" className="app-store-badge"
       style={compact ? { padding: '10px 18px' } : {}}>
      <svg width={compact ? 22 : 28} height={compact ? 22 : 28} viewBox="0 0 24 24" fill="#fff">
        <path d="M17.05 12.04c-.03-2.34 1.91-3.46 2-3.52-1.09-1.59-2.78-1.81-3.39-1.83-1.43-.15-2.81.85-3.54.85-.74 0-1.86-.83-3.07-.81-1.57.02-3.04.92-3.85 2.34-1.66 2.87-.42 7.11 1.18 9.44.78 1.14 1.71 2.42 2.92 2.37 1.18-.05 1.62-.76 3.05-.76 1.42 0 1.83.76 3.07.74 1.27-.02 2.07-1.16 2.85-2.31.91-1.32 1.27-2.6 1.29-2.67-.03-.01-2.46-.94-2.49-3.74zM14.74 4.59c.65-.79 1.09-1.89.97-2.99-.94.04-2.07.63-2.74 1.41-.6.69-1.13 1.81-.99 2.88 1.05.08 2.12-.53 2.76-1.3z"/>
      </svg>
      <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
        <span className="label">App Storeで</span>
        <span className="name">ダウンロード</span>
      </div>
    </a>
  );
}

// iPhone screenshot frame — minimal, just a rounded device with a screen-fitted image
function PhoneFrame({ src, width = 280, tilt = 0, glow = null, style = {} }) {
  const aspect = 19.5 / 9; // tall iPhone
  const height = width * aspect;
  return (
    <div style={{
      width, height, borderRadius: width * 0.13,
      background: '#000',
      padding: width * 0.025,
      position: 'relative',
      transform: tilt ? `rotate(${tilt}deg)` : undefined,
      boxShadow: glow
        ? `0 60px 120px ${glow}, 0 30px 60px rgba(0,0,0,0.55), inset 0 0 0 1.2px rgba(255,255,255,0.12)`
        : '0 50px 100px rgba(0,0,0,0.55), 0 20px 40px rgba(0,0,0,0.4), inset 0 0 0 1.2px rgba(255,255,255,0.12)',
      ...style,
    }}>
      <div style={{
        width: '100%', height: '100%',
        borderRadius: width * 0.105,
        overflow: 'hidden',
        background: '#000',
        position: 'relative',
      }}>
        <img src={src} alt="" style={{
          width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'top center',
          display: 'block',
        }} />
        {/* dynamic island */}
        <div style={{
          position: 'absolute', top: width * 0.038, left: '50%', transform: 'translateX(-50%)',
          width: width * 0.31, height: width * 0.085, borderRadius: width * 0.05,
          background: '#000',
        }} />
      </div>
    </div>
  );
}

// Animated gradient hero background — multiple soft blobs
function HeroBackdrop({ accent = '#FF6B1A', accent2 = '#E5223A' }) {
  return (
    <div className="hero-bg" aria-hidden="true">
      <div className="blob blob-a" style={{ background: `radial-gradient(circle, ${accent2} 0%, transparent 60%)` }}></div>
      <div className="blob blob-b" style={{ background: `radial-gradient(circle, ${accent} 0%, transparent 65%)` }}></div>
      <div className="blob blob-c" style={{ background: `radial-gradient(circle, #7A2DD4 0%, transparent 60%)` }}></div>
      <div className="blob blob-d" style={{ background: `radial-gradient(circle, #FF1F8F 0%, transparent 60%)` }}></div>
      <div className="grain"></div>
      <style>{`
        .hero-bg {
          position: absolute; inset: 0; overflow: hidden; z-index: 0;
          pointer-events: none;
        }
        .blob {
          position: absolute; width: 80vw; height: 80vw;
          max-width: 1100px; max-height: 1100px;
          filter: blur(80px);
          opacity: 0.55;
          mix-blend-mode: screen;
          will-change: transform;
        }
        .blob-a { top: -25%; left: -15%; animation: float-a 18s ease-in-out infinite; }
        .blob-b { top: 10%; right: -25%; animation: float-b 22s ease-in-out infinite; opacity: 0.5; }
        .blob-c { bottom: -35%; left: 15%; animation: float-c 26s ease-in-out infinite; opacity: 0.4; }
        .blob-d { top: 35%; left: 30%; width: 60vw; height: 60vw; animation: float-d 20s ease-in-out infinite; opacity: 0.35; }
        @keyframes float-a {
          0%,100% { transform: translate(0,0) scale(1); }
          50% { transform: translate(8vw, 6vw) scale(1.1); }
        }
        @keyframes float-b {
          0%,100% { transform: translate(0,0) scale(1); }
          50% { transform: translate(-10vw, 4vw) scale(1.15); }
        }
        @keyframes float-c {
          0%,100% { transform: translate(0,0) scale(1); }
          50% { transform: translate(6vw, -8vw) scale(1.05); }
        }
        @keyframes float-d {
          0%,100% { transform: translate(0,0) scale(1) rotate(0deg); }
          50% { transform: translate(-4vw, -6vw) scale(1.2) rotate(20deg); }
        }
        .grain {
          position: absolute; inset: 0;
          background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence baseFrequency='0.85' numOctaves='2'/></filter><rect width='100%25' height='100%25' filter='url(%23n)' opacity='0.55'/></svg>");
          opacity: 0.12; mix-blend-mode: overlay;
        }
      `}</style>
    </div>
  );
}

// Section heading
function SectionHead({ kicker, title, sub, align = 'left' }) {
  return (
    <div style={{
      display: 'flex', flexDirection: 'column', gap: 14,
      alignItems: align === 'center' ? 'center' : 'flex-start',
      textAlign: align,
      marginBottom: 56,
    }}>
      <span style={{
        fontFamily: 'JetBrains Mono, monospace',
        fontSize: 11, letterSpacing: '0.22em', textTransform: 'uppercase',
        color: 'var(--accent)',
        display: 'inline-flex', alignItems: 'center', gap: 8,
      }}>
        <span style={{ width: 18, height: 1, background: 'var(--accent)' }}></span>
        {kicker}
      </span>
      <h2 className="display" style={{
        fontSize: 'clamp(36px, 5vw, 64px)',
        fontWeight: 700, lineHeight: 1.05,
        maxWidth: 880,
        textWrap: 'balance',
      }}>{title}</h2>
      {sub && <p style={{
        color: 'var(--muted)', maxWidth: 620, fontSize: 16, lineHeight: 1.7,
      }}>{sub}</p>}
    </div>
  );
}

// Reveal-on-scroll wrapper using IntersectionObserver
function Reveal({ children, delay = 0, as: Tag = 'div', style = {}, className = '' }) {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current) return;
    const obs = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          setTimeout(() => setShown(true), delay);
          obs.disconnect();
        }
      });
    }, { threshold: 0.1, rootMargin: '0px 0px -60px 0px' });
    obs.observe(ref.current);
    return () => obs.disconnect();
  }, [delay]);
  return <Tag ref={ref} className={`reveal ${shown ? 'in' : ''} ${className}`} style={style}>{children}</Tag>;
}

Object.assign(window, {
  APP_STORE_URL, DigmeMark, AppStoreBadge, PhoneFrame, HeroBackdrop, SectionHead, Reveal,
});
