// WakeMeBot landing — atomic visual bits.
// All exports attach to window at the bottom.

const { useState, useEffect, useRef } = React;

// ─── SunDisc ──────────────────────────────────────────────────────────────
// Radial gradient sun with subtle specular highlight + soft halo + pulse.
const SunDisc = ({ size = 360, halo = true, hover = true }) => {
  const VB = 200;
  return (
    <div style={{ position: 'relative', width: size, height: size, display: 'inline-block' }}>
      {halo && (
        <div style={{
          position: 'absolute', inset: '-22%',
          borderRadius: '50%',
          background: 'radial-gradient(closest-side, rgba(255,178,108,0.55), rgba(255,178,108,0.18) 55%, transparent 75%)',
          filter: 'blur(8px)',
          animation: hover ? 'wmbHalo 5.5s ease-in-out infinite' : 'none',
        }} />
      )}
      <svg viewBox={`0 0 ${VB} ${VB}`} width={size} height={size}
           style={{ position: 'relative', display: 'block',
                    animation: hover ? 'wmbPulse 5.5s ease-in-out infinite' : 'none',
                    filter: 'drop-shadow(0 30px 50px rgba(184, 63, 24, 0.35))' }}>
        <defs>
          <radialGradient id="sunBody" cx="0.5" cy="0.25" r="0.85">
            <stop offset="0%"   stopColor="#FFE9C2" />
            <stop offset="35%"  stopColor="#FFC79E" />
            <stop offset="72%"  stopColor="#F0A055" />
            <stop offset="100%" stopColor="#C95018" />
          </radialGradient>
          <radialGradient id="sunSpec" cx="0.42" cy="0.22" r="0.45">
            <stop offset="0%"   stopColor="#FFFFFF" stopOpacity="0.85" />
            <stop offset="60%"  stopColor="#FFFFFF" stopOpacity="0.10" />
            <stop offset="100%" stopColor="#FFFFFF" stopOpacity="0" />
          </radialGradient>
          <radialGradient id="sunRim" cx="0.5" cy="0.5" r="0.5">
            <stop offset="92%" stopColor="rgba(122,28,8,0)" />
            <stop offset="100%" stopColor="rgba(122,28,8,0.30)" />
          </radialGradient>
        </defs>
        <circle cx={VB/2} cy={VB/2} r={VB/2 - 2} fill="url(#sunBody)" />
        <ellipse cx={VB*0.42} cy={VB*0.30} rx={VB*0.34} ry={VB*0.22} fill="url(#sunSpec)" />
        <circle cx={VB/2} cy={VB/2} r={VB/2 - 2} fill="url(#sunRim)" />
        {/* faint highlight crescent at the very top */}
        <path d={`M ${VB*0.18} ${VB*0.28} Q ${VB/2} ${VB*0.06} ${VB*0.82} ${VB*0.28}`}
              stroke="rgba(255,255,255,0.55)" strokeWidth="1.6" fill="none" strokeLinecap="round" />
      </svg>
    </div>
  );
};

// ─── Wordmark ─────────────────────────────────────────────────────────────
const Wordmark = ({ size = 28, weight = 600 }) => (
  <span style={{
    fontFamily: 'var(--font-display)', fontSize: size, fontWeight: weight,
    letterSpacing: '-0.035em', color: 'var(--ink)', lineHeight: 1,
    display: 'inline-flex', alignItems: 'baseline',
  }}>
    WakeMe<span style={{ color: 'var(--coral)' }}>Bot</span>
  </span>
);

// ─── GlassCard ────────────────────────────────────────────────────────────
const GlassCard = ({ children, style, pad = 36, radius = 28, tint = 0.55 }) => (
  <div style={{
    background: `rgba(255, 251, 241, ${tint})`,
    backdropFilter: 'blur(28px) saturate(1.2)',
    WebkitBackdropFilter: 'blur(28px) saturate(1.2)',
    borderRadius: radius,
    boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.7), 0 24px 60px -20px rgba(122, 53, 14, 0.30), 0 2px 6px rgba(122, 53, 14, 0.06)',
    padding: pad,
    position: 'relative',
    ...style,
  }}>
    {children}
  </div>
);

// ─── iOS App Store badge — original CTA pill (not Apple's official badge) ─
const AppStorePill = ({ primary = true }) => (
  <a href="#download" style={{
    display: 'inline-flex', alignItems: 'center', gap: 12,
    padding: '14px 22px 14px 18px',
    borderRadius: 999,
    background: primary ? 'var(--ink)' : 'rgba(255,255,255,0.55)',
    color: primary ? '#FFF6E4' : 'var(--ink)',
    boxShadow: primary
      ? '0 12px 30px -10px rgba(42, 26, 18, 0.55), inset 0 1px 0 rgba(255,255,255,0.12)'
      : 'inset 0 0 0 1px rgba(255,255,255,0.85), 0 8px 24px -10px rgba(122, 53, 14, 0.25)',
    backdropFilter: primary ? 'none' : 'blur(20px)',
    WebkitBackdropFilter: primary ? 'none' : 'blur(20px)',
    fontWeight: 500, letterSpacing: '-0.01em',
    transition: 'transform 180ms ease',
  }}
  onMouseEnter={e => e.currentTarget.style.transform = 'translateY(-1px)'}
  onMouseLeave={e => e.currentTarget.style.transform = 'translateY(0)'}>
    <svg width="22" height="22" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
      <path d="M16.365 1.43c0 1.14-.434 2.23-1.21 3.06-.78.83-2.05 1.5-3.23 1.41-.13-1.12.42-2.27 1.16-3.06.84-.9 2.27-1.55 3.28-1.55v.14zM20.5 17.36c-.5 1.18-.74 1.7-1.4 2.74-.92 1.43-2.22 3.22-3.84 3.23-1.43.02-1.8-.93-3.74-.92-1.94.01-2.35.94-3.78.92-1.62-.02-2.85-1.62-3.77-3.06C2.43 15.83 2.16 9.7 4.6 6.6c1.07-1.35 2.77-2.2 4.36-2.2 1.62 0 2.65.9 4 .9 1.3 0 2.1-.9 3.97-.9 1.42 0 2.92.77 4 2.1-3.51 1.92-2.94 6.94.57 8.86z"/>
    </svg>
    <span style={{ display: 'inline-flex', flexDirection: 'column', lineHeight: 1.1, alignItems: 'flex-start' }}>
      <span style={{ fontSize: 10, opacity: 0.7, letterSpacing: '0.04em', textTransform: 'uppercase' }}>Get it on</span>
      <span style={{ fontSize: 16, fontWeight: 600, letterSpacing: '-0.01em' }}>iOS — App Store</span>
    </span>
  </a>
);

// ─── Secondary text CTA ───────────────────────────────────────────────────
const TextCTA = ({ href = '#how', children, arrow = true }) => (
  <a href={href} style={{
    display: 'inline-flex', alignItems: 'center', gap: 8,
    padding: '12px 18px', borderRadius: 999,
    color: 'var(--ink)', fontWeight: 500, letterSpacing: '-0.01em',
    boxShadow: 'inset 0 0 0 1px rgba(42, 26, 18, 0.18)',
    transition: 'background 180ms ease, box-shadow 180ms ease',
  }}
  onMouseEnter={e => { e.currentTarget.style.background = 'rgba(255,255,255,0.4)'; }}
  onMouseLeave={e => { e.currentTarget.style.background = 'transparent'; }}>
    {children}
    {arrow && <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 6l6 6-6 6"/></svg>}
  </a>
);

// ─── Animated voice waveform ──────────────────────────────────────────────
const Waveform = ({ bars = 28, height = 56, color = 'var(--coral)', running = true }) => {
  // Deterministic heights so SSR/RSC-style hydration mismatches don't happen
  const heights = React.useMemo(() => Array.from({ length: bars }, (_, i) => {
    const t = i / bars;
    const w = 0.45 + 0.55 * Math.abs(Math.sin(t * Math.PI * 2.4) * Math.cos(t * Math.PI * 1.6));
    return Math.max(0.18, w);
  }), [bars]);
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 4, height }}>
      {heights.map((h, i) => (
        <span key={i} style={{
          display: 'inline-block', width: 4, borderRadius: 2,
          height: `${h * 100}%`, background: color,
          transformOrigin: 'center',
          animation: running ? `wmbWave ${0.9 + (i % 5) * 0.13}s ease-in-out ${i * 0.04}s infinite` : 'none',
        }} />
      ))}
    </div>
  );
};

// ─── Section heading ──────────────────────────────────────────────────────
const Eyebrow = ({ children, color = 'var(--coral)' }) => (
  <div style={{
    fontFamily: 'var(--font-mono)', fontSize: 12, fontWeight: 500,
    letterSpacing: '0.14em', textTransform: 'uppercase', color,
  }}>{children}</div>
);

// ─── iPhone bezel mock — for screenshots in calendar section ──────────────
const PhoneFrame = ({ children, width = 320, height = 656 }) => (
  <div style={{
    width, height, borderRadius: 52, padding: 10,
    background: 'linear-gradient(180deg, #2a1a12, #0e0805)',
    boxShadow: '0 40px 90px -30px rgba(122, 53, 14, 0.55), inset 0 0 0 1.5px rgba(255,255,255,0.08), inset 0 0 0 6px #0a0604',
    position: 'relative',
  }}>
    <div style={{
      width: '100%', height: '100%', borderRadius: 42, overflow: 'hidden', position: 'relative',
      background: 'linear-gradient(180deg, #FFD7A8 0%, #F0A055 35%, #E8732C 70%, #B83F18 100%)',
    }}>
      {/* Dynamic island */}
      <div style={{
        position: 'absolute', top: 10, left: '50%', transform: 'translateX(-50%)',
        width: 96, height: 28, borderRadius: 16, background: '#0a0604', zIndex: 5,
      }} />
      {/* Status bar */}
      <div style={{
        position: 'absolute', top: 14, left: 22, right: 22, display: 'flex',
        justifyContent: 'space-between', alignItems: 'center',
        fontSize: 13, fontWeight: 600, color: '#FFF6E4', zIndex: 4,
      }}>
        <span>9:41</span>
        <span style={{ display: 'inline-flex', gap: 5, alignItems: 'center' }}>
          <svg width="16" height="10" viewBox="0 0 16 10" fill="currentColor"><rect x="0" y="6" width="3" height="4" rx="0.5"/><rect x="4.5" y="4" width="3" height="6" rx="0.5"/><rect x="9" y="2" width="3" height="8" rx="0.5"/><rect x="13.5" y="0" width="3" height="10" rx="0.5"/></svg>
          <svg width="22" height="10" viewBox="0 0 22 10" fill="none"><rect x="0.5" y="0.5" width="18" height="9" rx="2" stroke="currentColor"/><rect x="2" y="2" width="14" height="6" rx="1" fill="currentColor"/><rect x="19.5" y="3.5" width="1.5" height="3" rx="0.5" fill="currentColor"/></svg>
        </span>
      </div>
      {children}
    </div>
  </div>
);

// ─── Calendar-suggestion phone screen ─────────────────────────────────────
const PhoneCalendarScreen = () => (
  <div style={{ position: 'absolute', inset: 0, paddingTop: 56, paddingLeft: 18, paddingRight: 18 }}>
    <div style={{ color: '#FFF6E4', fontSize: 11, fontWeight: 600, letterSpacing: '0.1em', textTransform: 'uppercase', opacity: 0.85 }}>AI Suggestions</div>
    <div style={{ color: '#FFF6E4', fontSize: 30, fontWeight: 600, letterSpacing: '-0.03em', marginTop: 4, lineHeight: 1.05 }}>
      Three smart alarms<br/>for tomorrow
    </div>
    <div style={{ marginTop: 18, display: 'flex', flexDirection: 'column', gap: 10 }}>
      {[
        { time: '5:45', label: 'Flight · LHR → JFK', why: 'Leave by 6:30 to make 8:55 boarding', icon: '✈' },
        { time: '7:00', label: 'Investor pitch · Sequoia', why: 'You said you wanted 90 min of prep', icon: '◐' },
        { time: '8:15', label: 'Standup · Eng team',     why: 'Recurring · 30 min before',          icon: '◍' },
      ].map((s, i) => (
        <div key={i} style={{
          padding: '13px 14px', borderRadius: 18,
          background: 'rgba(255, 251, 241, 0.18)',
          backdropFilter: 'blur(22px)', WebkitBackdropFilter: 'blur(22px)',
          boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.35)',
          color: '#FFF6E4',
        }}>
          <div style={{ display: 'flex', alignItems: 'baseline', gap: 10 }}>
            <div style={{ fontSize: 26, fontWeight: 600, letterSpacing: '-0.04em' }}>{s.time}</div>
            <div style={{ fontSize: 10, opacity: 0.7 }}>AM</div>
            <div style={{ marginLeft: 'auto', fontSize: 16, opacity: 0.8 }}>{s.icon}</div>
          </div>
          <div style={{ fontSize: 13, fontWeight: 600, marginTop: 2, letterSpacing: '-0.01em' }}>{s.label}</div>
          <div style={{ fontSize: 11, opacity: 0.78, marginTop: 2, lineHeight: 1.35 }}>{s.why}</div>
        </div>
      ))}
    </div>
    {/* Bottom CTA */}
    <div style={{
      position: 'absolute', left: 18, right: 18, bottom: 26,
      borderRadius: 16, padding: '14px 16px', textAlign: 'center',
      background: '#FFF6E4', color: 'var(--ink)',
      fontSize: 14, fontWeight: 600, letterSpacing: '-0.01em',
      boxShadow: '0 10px 24px -10px rgba(0,0,0,0.35)',
    }}>
      Add all three
    </div>
  </div>
);

// ─── Voice-prompt phone screen ────────────────────────────────────────────
const PhoneVoiceScreen = () => (
  <div style={{ position: 'absolute', inset: 0, paddingTop: 56, paddingLeft: 18, paddingRight: 18,
                display: 'flex', flexDirection: 'column' }}>
    <div style={{ color: '#FFF6E4', fontSize: 11, fontWeight: 600, letterSpacing: '0.1em', textTransform: 'uppercase', opacity: 0.85 }}>New alarm</div>
    <div style={{ color: '#FFF6E4', fontSize: 28, fontWeight: 600, letterSpacing: '-0.03em', marginTop: 4 }}>Tell me why</div>
    <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center', gap: 22 }}>
      {/* Pulse ring */}
      <div style={{ position: 'relative', width: 168, height: 168 }}>
        <div style={{ position: 'absolute', inset: 0, borderRadius: '50%',
                      background: 'radial-gradient(closest-side, rgba(255,246,228,0.55), transparent 70%)',
                      animation: 'wmbHalo 3.8s ease-in-out infinite' }} />
        <div style={{ position: 'absolute', inset: 14, borderRadius: '50%',
                      background: 'rgba(255, 251, 241, 0.2)',
                      boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.5)',
                      display: 'flex', alignItems: 'center', justifyContent: 'center',
                      animation: 'wmbPulse 3.8s ease-in-out infinite' }}>
          <svg width="40" height="56" viewBox="0 0 24 32" fill="none" stroke="#FFF6E4" strokeWidth="2.4" strokeLinecap="round">
            <rect x="8" y="2" width="8" height="16" rx="4"/>
            <path d="M4 14a8 8 0 0 0 16 0M12 22v6M8 28h8"/>
          </svg>
        </div>
      </div>
      <div style={{ color: '#FFF6E4', fontSize: 13, opacity: 0.85, textAlign: 'center', maxWidth: 240, lineHeight: 1.45 }}>
        "Wake me at 7am tomorrow for my investor pitch with Sequoia."
      </div>
    </div>
    <div style={{ paddingBottom: 28, display: 'flex', justifyContent: 'center', gap: 16 }}>
      {[0,1,2,3,4,5,6,7,8,9,10,11].map(i => (
        <span key={i} style={{
          width: 3, borderRadius: 2, background: '#FFF6E4',
          height: `${20 + 20 * Math.abs(Math.sin(i * 0.9))}px`,
          opacity: 0.9,
          animation: `wmbWave ${0.7 + (i%4)*0.12}s ease-in-out ${i*0.05}s infinite`,
        }} />
      ))}
    </div>
  </div>
);

Object.assign(window, {
  SunDisc, Wordmark, GlassCard, AppStorePill, TextCTA, Waveform, Eyebrow,
  PhoneFrame, PhoneCalendarScreen, PhoneVoiceScreen,
});
