// Shared atoms: Logo, Nav, Footer, decorative background
const { useState, useEffect, useRef, useMemo } = React;

// Logo mark (official — from rrlogo.svg, tinted with currentColor)
function LogoMark({ size = 28, color = 'var(--brand)' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 1200 1200" fill={color} aria-hidden="true">
      <path d="M412.5,1100c-117,0-212.5-101-212.5-225v-375h100v375c0,69,50.5,125,112.5,125s112.5-56,112.5-125V325c0-124,95.5-225,212.5-225s212.5,101,212.5,225v225h-100v-225c0-69-50.5-125-112.5-125s-112.5,56-112.5,125v550c0,124-95.5,225-212.5,225Z"/>
      <path d="M250,400c-82.5,0-150-67.5-150-150S167.5,100,250,100s150,67.5,150,150-67.5,150-150,150ZM250,200c-27.5,0-50,22.5-50,50s22.5,50,50,50,50-22.5,50-50-22.5-50-50-50Z"/>
      <path d="M900,1080.5c-31,0-59.5-14-78.5-38.5-121.5-155.5-121.5-218-121.5-238.5,0-112,89.5-203.5,200-203.5s200,91.5,200,203.5c0,20.5,0,83-121.5,238.5-19,24.5-47.5,38.5-78.5,38.5ZM900,700c-55,0-100,46.5-100,103.5,0,22.5,26,82.5,100,177,73.5-95,100-154.5,100-177,0-57-45-103.5-100-103.5Z"/>
      <path d="M950,800c0,27.61-22.39,50-50,50s-50-22.39-50-50,22.39-50,50-50,50,22.39,50,50Z"/>
    </svg>
  );
}

function Wordmark({ size = 22 }) {
  return (
    <div style={{display:'flex', alignItems:'center', gap: 10}}>
      <LogoMark size={size * 1.3}/>
      <span style={{
        fontFamily: 'var(--font-display)',
        fontWeight: 600, fontSize: size, letterSpacing: '-0.02em',
        color: 'var(--ink)'
      }}>Route Radar</span>
    </div>
  );
}

function Nav() {
  return (
    <nav style={{
      position: 'sticky', top: 0, zIndex: 50,
      backdropFilter: 'saturate(140%) blur(14px)',
      WebkitBackdropFilter: 'saturate(140%) blur(14px)',
      background: 'rgba(244,239,228,0.75)',
      borderBottom: '1px solid rgba(22,19,15,0.08)'
    }}>
      <div className="wrap flex between center" style={{height: 68}}>
        <Wordmark size={18}/>
        <div className="nav-links">
          <a href="#features" style={{fontSize: 14, color: 'var(--ink-2)'}}>Features</a>
          <a href="#how" style={{fontSize: 14, color: 'var(--ink-2)'}}>How it works</a>
          <a href="#screens" style={{fontSize: 14, color: 'var(--ink-2)'}}>Screens</a>
          <a href="#faq" style={{fontSize: 14, color: 'var(--ink-2)'}}>Questions</a>
        </div>
        <a className="btn primary" href="#download" style={{padding: '10px 18px', fontSize: 14}}>
          <AppleGlyph size={14}/> Get the app
        </a>
      </div>
    </nav>
  );
}

function AppleGlyph({ size = 16, color = 'currentColor' }) {
  return (
    <svg width={size} height={size * 1.2} viewBox="0 0 384 480" aria-hidden="true">
      <path fill={color} d="M318.7 268.7c-.3-36.6 16.4-64.3 50.1-84.8-18.9-27-47.4-41.9-85-44.8-35.7-2.8-74.5 20.8-88.7 20.8-15 0-49.4-19.8-76.4-19.8-56.2 1-115.5 43.5-115.5 128.5 0 25.1 4.6 51 13.8 77.6 12.3 35.1 56.7 121.2 103 120 23.9-.6 41-17.2 72.1-17.2 30.2 0 46 17.2 72.7 17.2 46.7-.7 87-79 98.9-114.2-63.4-29.8-62-87.4-62-87.4zM257.9 95.3C285.5 62.7 283 33 282.2 22.4c-24.1 1.4-52 16.5-67.9 35.1-17.5 20-27.9 44.8-25.6 72 26.1 2 50-11.4 69.2-34.2z"/>
    </svg>
  );
}

// Decorative topographic background — SVG of concentric contour lines with a route
function TopoBackground({ opacity = 0.6 }) {
  return (
    <svg
      aria-hidden="true"
      style={{
        position: 'absolute', inset: 0, width: '100%', height: '100%',
        opacity, pointerEvents: 'none'
      }}
      viewBox="0 0 1440 900" preserveAspectRatio="xMidYMid slice"
    >
      <defs>
        <pattern id="topo" x="0" y="0" width="1440" height="900" patternUnits="userSpaceOnUse">
          <g stroke="rgba(22,19,15,0.09)" strokeWidth="1" fill="none">
            {/* Contour-ish curves */}
            <path d="M-50,180 C 200,120 420,240 640,180 S 1080,100 1500,200"/>
            <path d="M-50,240 C 220,170 440,300 660,240 S 1100,150 1500,260"/>
            <path d="M-50,310 C 240,230 460,370 680,310 S 1120,210 1500,330"/>
            <path d="M-50,390 C 260,300 480,450 700,390 S 1140,280 1500,410"/>
            <path d="M-50,480 C 280,380 500,540 720,480 S 1160,360 1500,500"/>
            <path d="M-50,580 C 300,470 520,640 740,580 S 1180,450 1500,600"/>
            <path d="M-50,690 C 320,570 540,750 760,690 S 1200,550 1500,710"/>
            <path d="M-50,810 C 340,680 560,870 780,810 S 1220,660 1500,830"/>
          </g>
        </pattern>
      </defs>
      <rect width="1440" height="900" fill="url(#topo)"/>
    </svg>
  );
}

// Stylized route line SVG — used as a flourish
function RouteFlourish({ w = 400, h = 80, stroke = 'var(--brand)' }) {
  return (
    <svg width={w} height={h} viewBox="0 0 400 80" fill="none" aria-hidden="true">
      <circle cx="10" cy="40" r="5" fill={stroke}/>
      <path d="M 20 40 C 60 10, 100 70, 150 40 S 240 10, 290 40 S 370 60, 390 40"
        stroke={stroke} strokeWidth="3" strokeLinecap="round" strokeDasharray="0 0" fill="none"/>
      <path d="M 385 32 L 395 40 L 385 48" stroke={stroke} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
    </svg>
  );
}

// Icon helpers
function Icon({ name, size = 22, color = 'currentColor', strokeWidth = 1.6 }) {
  const common = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
    stroke: color, strokeWidth, strokeLinecap: 'round', strokeLinejoin: 'round' };
  switch (name) {
    case 'coffee': return <svg {...common}><path d="M4 10h12v6a4 4 0 0 1-4 4H8a4 4 0 0 1-4-4v-6Z"/><path d="M16 12h2a3 3 0 0 1 0 6h-2"/><path d="M7 5c.5-1 .5-2 0-3"/><path d="M11 5c.5-1 .5-2 0-3"/></svg>;
    case 'wrench': return <svg {...common}><path d="M14.7 6.3a4 4 0 0 0-5.6 5.6l-7 7 2.8 2.8 7-7a4 4 0 0 0 5.6-5.6l-2.5 2.5-2.1-2.1 2.5-2.5-.7-.7Z"/></svg>;
    case 'gpx': return <svg {...common}><path d="M4 20l4-10 4 6 4-12 4 16"/><circle cx="8" cy="10" r="1.6" fill={color}/><circle cx="16" cy="4" r="1.6" fill={color}/></svg>;
    case 'map': return <svg {...common}><path d="M9 4 3 6v14l6-2 6 2 6-2V4l-6 2-6-2Z"/><path d="M9 4v14"/><path d="M15 6v14"/></svg>;
    case 'pin': return <svg {...common}><path d="M12 22s7-7.5 7-13a7 7 0 1 0-14 0c0 5.5 7 13 7 13Z"/><circle cx="12" cy="9" r="2.5"/></svg>;
    case 'route': return <svg {...common}><circle cx="6" cy="6" r="2.5"/><circle cx="18" cy="18" r="2.5"/><path d="M6 8v6a4 4 0 0 0 4 4h5"/></svg>;
    case 'download': return <svg {...common}><path d="M12 4v12"/><path d="m7 11 5 5 5-5"/><path d="M4 20h16"/></svg>;
    case 'arrow': return <svg {...common}><path d="M5 12h14"/><path d="m13 6 6 6-6 6"/></svg>;
    case 'strava': return <svg viewBox="0 0 24 24" width={size} height={size} fill={color}><path d="M15.4 8.3 13.6 4.5 7 18h4.1l2.5-5.1 2.5 5.1H20L15.4 8.3Z"/></svg>;
    case 'check': return <svg {...common}><path d="m5 12 5 5 9-11"/></svg>;
    default: return null;
  }
}

Object.assign(window, { LogoMark, Wordmark, Nav, AppleGlyph, TopoBackground, RouteFlourish, Icon });
