// Sobella Landing Page — sections & components
const { useState, useEffect, useRef } = React;

const L = {
  primary: '#154734',
  deepGreen: '#0B2017',
  bg: '#FFF1E4',
  bgWarm: '#E8DFD1',
  cream: '#FAF9F5',
  charcoal: '#1F1E1D',
  nearBlack: '#141413',
  sage: '#D0D5CC',
  orange: '#E66B33',
  white: '#FFFFFF',
  fg2: 'rgba(31,30,29,0.60)',
  fg3: 'rgba(31,30,29,0.40)',
  fg4: 'rgba(31,30,29,0.30)',
  fg5: 'rgba(31,30,29,0.20)',
};

function useWindowWidth() {
  const [w, setW] = useState(window.innerWidth);
  useEffect(() => {
    const h = () => setW(window.innerWidth);
    window.addEventListener('resize', h);
    return () => window.removeEventListener('resize', h);
  }, []);
  return w;
}

// ── Wordmark ─────────────────────────────────────────────────
function Wordmark({ size = 13, color = L.charcoal }) {
  return (
    <span style={{
      fontWeight: 900, fontSize: size, letterSpacing: '0.8em',
      textTransform: 'uppercase', color, marginRight: '-0.8em',
      display: 'inline-block',
    }}>SOBELLA</span>
  );
}

function Eyebrow({ children, color, track = '0.3em', size = 11 }) {
  return (
    <span style={{
      fontWeight: 900, fontSize: size, letterSpacing: track,
      textTransform: 'uppercase', color: color || L.fg3,
    }}>{children}</span>
  );
}

function LeafMark({ size = 28, color = '#fff' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M11 20A7 7 0 0 1 4 13a7 7 0 0 1 7-7 7 7 0 0 1 7 7c0 3-2 5-4 6-1 1-3 1-3 1z"/>
      <path d="M2 22a13 13 0 0 1 10-10"/>
    </svg>
  );
}

// ── NAV ──────────────────────────────────────────────────────
function Nav({ variant }) {
  const w = useWindowWidth();
  const mob = w < 768;
  const dark = variant === 'dark';
  const fg = dark ? '#fff' : L.charcoal;
  const muted = dark ? 'rgba(255,255,255,0.6)' : L.fg2;
  return (
    <nav style={{
      position: 'sticky', top: 0, zIndex: 50,
      background: dark ? 'rgba(11,32,23,0.8)' : 'rgba(255,241,228,0.8)',
      backdropFilter: 'blur(16px) saturate(180%)',
      WebkitBackdropFilter: 'blur(16px) saturate(180%)',
      borderBottom: `1px solid ${dark ? 'rgba(255,255,255,0.08)' : 'rgba(31,30,29,0.06)'}`,
    }}>
      <div style={{
        maxWidth: 1240, margin: '0 auto',
        padding: mob ? '16px 20px' : '20px 40px',
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      }}>
        <Wordmark size={14} color={fg}/>
        {!mob && (
          <div style={{ display: 'flex', gap: 36, alignItems: 'center' }}>
            {['Approach', 'Features', 'Stories', 'Pricing'].map(l => (
              <a key={l} href={`#${l.toLowerCase()}`} style={{
                color: muted, textDecoration: 'none',
                fontSize: 14, fontWeight: 500,
              }}>{l}</a>
            ))}
          </div>
        )}
        <div style={{ display: 'flex', gap: mob ? 8 : 12, alignItems: 'center' }}>
          {!mob && (
            <a href="#" style={{
              color: fg, textDecoration: 'none',
              fontSize: 14, fontWeight: 600,
            }}>Sign in</a>
          )}
          <button style={{
            background: dark ? '#fff' : L.primary,
            color: dark ? L.primary : '#fff',
            border: 'none', borderRadius: 9999,
            padding: mob ? '10px 16px' : '11px 20px',
            fontFamily: 'inherit',
            fontSize: mob ? 13 : 14, fontWeight: 700, cursor: 'pointer',
          }}>Download the app</button>
        </div>
      </div>
    </nav>
  );
}

// ── PROMISE BAND ─────────────────────────────────────────────
function Promise() {
  const w = useWindowWidth();
  const mob = w < 768;
  return (
    <section style={{
      background: L.bg, padding: mob ? '60px 20px 80px' : '60px 40px 120px',
      position: 'relative',
    }}>
      <div style={{ maxWidth: 1040, margin: '0 auto', textAlign: 'center' }}>
        <Eyebrow>Our promise</Eyebrow>
        <p style={{
          marginTop: 24,
          fontSize: mob ? 26 : 44,
          fontWeight: 700,
          letterSpacing: '-0.025em', lineHeight: 1.2,
          color: L.charcoal, textWrap: 'balance',
        }}>
          No streaks to break. No shame spiral.
          <span style={{ color: L.fg3 }}> Just a soft, steady hand on the days that feel hard —
          and the ones that feel ordinary.</span>
        </p>
      </div>
    </section>
  );
}

// ── FEATURE PILLARS ──────────────────────────────────────────
const PILLARS = [
  {
    eyebrow: 'Journal',
    title: 'A short prompt, not a blank page.',
    body: 'Two minutes a day. Your companion asks the right question — you answer in your own words, or in your own voice.',
    tint: 'primary',
    ico: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <path d="M12 20h9M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4z"/>
      </svg>
    ),
  },
  {
    eyebrow: 'Toolkit',
    title: 'Cravings, met with technique.',
    body: 'When the pull hits, tap one button. Breathing, grounding, urge surfing — practical techniques that actually fit in 90 seconds.',
    tint: 'orange',
    ico: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <path d="m12 3-1.9 5.8L4 10l5.1 3.2L8 20l4-3.7L16 20l-1-6.8L20 10l-6-1.2z"/>
      </svg>
    ),
  },
  {
    eyebrow: 'AF drinks',
    title: 'Keep the ritual. Change the fuel.',
    body: 'A curated library of zero-proof cocktails and picks for every bar, store, and mood. The glass still feels good in your hand.',
    tint: 'primary',
    ico: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <path d="M17 8h1a4 4 0 0 1 0 8h-1"/>
        <path d="M3 8h14v9a4 4 0 0 1-4 4H7a4 4 0 0 1-4-4z"/>
        <path d="M6 2v3M10 2v3M14 2v3"/>
      </svg>
    ),
  },
  {
    eyebrow: 'Days',
    title: 'A counter that holds, never breaks.',
    body: 'Slips happen. Your Day count becomes a Chapter — the work doesn\'t reset to zero, because you aren\'t starting from zero.',
    tint: 'primary',
    ico: (
      <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <rect x="3" y="4" width="18" height="18" rx="2"/>
        <path d="M16 2v4M8 2v4M3 10h18"/>
      </svg>
    ),
  },
];

function Pillars() {
  const w = useWindowWidth();
  const mob = w < 768;
  const tab = w < 1024;
  return (
    <section id="features" style={{
      background: L.bgWarm, padding: mob ? '80px 20px' : '120px 40px',
    }}>
      <div style={{ maxWidth: 1240, margin: '0 auto' }}>
        <div style={{
          display: 'flex', alignItems: mob ? 'flex-start' : 'flex-end',
          flexDirection: mob ? 'column' : 'row',
          justifyContent: 'space-between',
          marginBottom: mob ? 40 : 64, gap: 24, flexWrap: 'wrap',
        }}>
          <div>
            <Eyebrow>Inside the app</Eyebrow>
            <h2 style={{
              marginTop: 16, fontSize: mob ? 34 : 56, fontWeight: 700,
              letterSpacing: '-0.03em', lineHeight: 1.05,
              color: L.charcoal, maxWidth: 640,
              textWrap: 'balance',
            }}>
              Four quiet tools. One steady companion.
            </h2>
          </div>
          <p style={{
            fontSize: 17, lineHeight: 1.55, color: L.fg2,
            maxWidth: 360, margin: 0,
          }}>
            Nothing to learn, nothing to game.
            Sobella is built to fit in the margins of your day — not to become one more thing on the list.
          </p>
        </div>

        <div style={{
          display: 'grid',
          gridTemplateColumns: mob ? '1fr' : 'repeat(2, 1fr)',
          gap: 16,
        }}>
          {PILLARS.map((p, i) => (
            <div key={i} style={{
              background: '#fff', borderRadius: 40,
              padding: mob ? '28px 24px 32px' : '40px 40px 44px',
              border: '1px solid #fff',
              boxShadow: '0 8px 30px rgba(0,0,0,0.04)',
              display: 'flex', flexDirection: 'column', gap: 20,
              position: 'relative', overflow: 'hidden',
            }}>
              <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                <div style={{
                  width: 48, height: 48, borderRadius: 16,
                  background: p.tint === 'orange' ? 'rgba(230,107,51,0.10)' : 'rgba(21,71,52,0.08)',
                  color: p.tint === 'orange' ? L.orange : L.primary,
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                }}>
                  {p.ico}
                </div>
                <Eyebrow color={p.tint === 'orange' ? L.orange : L.primary}>{p.eyebrow}</Eyebrow>
              </div>
              <h3 style={{
                fontSize: mob ? 24 : 30, fontWeight: 700, lineHeight: 1.15,
                letterSpacing: '-0.02em', color: L.charcoal,
                margin: 0, maxWidth: 380, textWrap: 'balance',
              }}>{p.title}</h3>
              <p style={{
                fontSize: 16, lineHeight: 1.55, color: L.fg2,
                margin: 0, maxWidth: 420, textWrap: 'pretty',
              }}>{p.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── DARK CEREMONIAL BAND ─────────────────────────────────────
function Ceremony() {
  const w = useWindowWidth();
  const mob = w < 768;
  return (
    <section id="stories" style={{
      background: L.deepGreen, color: '#fff',
      padding: mob ? '80px 24px' : '140px 40px',
      position: 'relative', overflow: 'hidden',
    }}>
      <div style={{
        position: 'absolute', top: '50%', left: '50%',
        width: 900, height: 900, transform: 'translate(-50%,-50%)',
        borderRadius: '50%', border: '1px solid rgba(255,255,255,0.04)',
      }}/>
      <div style={{
        position: 'absolute', top: '50%', left: '50%',
        width: 640, height: 640, transform: 'translate(-50%,-50%)',
        borderRadius: '50%', border: '1px solid rgba(255,255,255,0.06)',
      }}/>

      <div style={{
        maxWidth: 900, margin: '0 auto', position: 'relative',
        textAlign: 'center',
      }}>
        <Eyebrow color="rgba(255,255,255,0.5)">Written by someone on their Day 214</Eyebrow>
        <blockquote style={{
          margin: '32px 0 0',
          fontSize: mob ? 22 : 42,
          fontWeight: 500,
          lineHeight: 1.3, letterSpacing: '-0.015em',
          color: '#fff', textWrap: 'balance',
        }}>
          "I expected a tracker.
          I got something that felt like it was sitting next to me
          at 9pm on a Tuesday,
          <span style={{ color: 'rgba(255,255,255,0.55)' }}> quietly reminding me
          why I started."</span>
        </blockquote>

        <div style={{
          marginTop: 56,
          display: 'flex', alignItems: 'center', gap: 16,
          justifyContent: 'center',
        }}>
          <div style={{
            width: 48, height: 48, borderRadius: '50%',
            background: 'rgba(255,255,255,0.1)',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 17, fontWeight: 700, color: '#fff',
          }}>M</div>
          <div style={{ textAlign: 'left' }}>
            <div style={{ fontSize: 15, fontWeight: 700, color: '#fff' }}>Maren, 34</div>
            <div style={{ fontSize: 13, color: 'rgba(255,255,255,0.5)', marginTop: 2 }}>
              Chapter 2 · 214 quiet days
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── HOW IT WORKS ─────────────────────────────────────────────
function HowItWorks() {
  const w = useWindowWidth();
  const mob = w < 768;
  const steps = [
    {
      n: '01',
      title: 'Meet your companion.',
      body: 'Answer three gentle questions — your name, your starting point, what\'s drawing you toward change. Takes about a minute. No account needed to start.',
    },
    {
      n: '02',
      title: 'Mark Day 1.',
      body: 'A quiet ceremony — not a countdown. Today becomes the day you decided. Sobella remembers the date and shows up tomorrow, and the day after.',
    },
    {
      n: '03',
      title: 'Show up, most days.',
      body: 'A two-minute check-in, a toolkit for the hard moments, an AF drink when you want the ritual. Small things, done steadily, become a chapter.',
    },
  ];

  return (
    <section id="approach" style={{
      background: L.bg, padding: mob ? '80px 20px' : '140px 40px',
    }}>
      <div style={{ maxWidth: 1240, margin: '0 auto' }}>
        <div style={{ maxWidth: 640, marginBottom: mob ? 40 : 72 }}>
          <Eyebrow>How it works</Eyebrow>
          <h2 style={{
            marginTop: 16, fontSize: mob ? 34 : 56, fontWeight: 700,
            letterSpacing: '-0.03em', lineHeight: 1.05,
            color: L.charcoal, textWrap: 'balance',
          }}>
            Three soft steps. No cliffs, no streaks.
          </h2>
        </div>

        <div style={{
          display: 'grid',
          gridTemplateColumns: mob ? '1fr' : 'repeat(3, 1fr)',
          gap: 2,
          background: 'rgba(21,71,52,0.12)',
          borderRadius: 32, overflow: 'hidden',
        }}>
          {steps.map((s, i) => (
            <div key={i} style={{
              background: L.bg, padding: mob ? '36px 28px 40px' : '48px 40px 56px',
              display: 'flex', flexDirection: 'column', gap: 20,
            }}>
              <div style={{
                fontSize: mob ? 48 : 64, fontWeight: 700, color: L.primary,
                letterSpacing: '-0.04em', lineHeight: 1,
                fontFeatureSettings: '"tnum"',
              }}>{s.n}</div>
              <h3 style={{
                fontSize: mob ? 22 : 26, fontWeight: 700, lineHeight: 1.15,
                letterSpacing: '-0.02em', color: L.charcoal,
                margin: 0, maxWidth: 320, textWrap: 'balance',
              }}>{s.title}</h3>
              <p style={{
                fontSize: 16, lineHeight: 1.55, color: L.fg2,
                margin: 0, textWrap: 'pretty',
              }}>{s.body}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── FINAL CTA ────────────────────────────────────────────────
function FinalCTA() {
  const w = useWindowWidth();
  const mob = w < 768;
  return (
    <section id="pricing" style={{
      background: L.bg, padding: mob ? '40px 20px 80px' : '60px 40px 120px',
    }}>
      <div style={{
        maxWidth: 1040, margin: '0 auto',
        background: L.primary, color: '#fff',
        borderRadius: mob ? 32 : 48,
        padding: mob ? '48px 28px' : '80px 60px',
        position: 'relative', overflow: 'hidden',
        boxShadow: '0 30px 60px -20px rgba(21,71,52,0.3)',
      }}>
        <div style={{
          position: 'absolute', top: -120, right: -120,
          width: 420, height: 420, borderRadius: '50%',
          border: '12px solid rgba(255,255,255,0.04)',
        }}/>
        <div style={{
          position: 'absolute', top: -40, right: -40,
          width: 260, height: 260, borderRadius: '50%',
          border: '12px solid rgba(255,255,255,0.04)',
        }}/>

        <div style={{ position: 'relative', maxWidth: 640 }}>
          <Eyebrow color="rgba(255,255,255,0.55)">Tomorrow you</Eyebrow>
          <h2 style={{
            marginTop: 20, fontSize: mob ? 36 : 64, fontWeight: 700,
            letterSpacing: '-0.03em', lineHeight: 1.05,
            color: '#fff', textWrap: 'balance',
          }}>
            The best day to begin was yesterday.
            <br/>
            <span style={{ color: 'rgba(255,255,255,0.55)' }}>The next best day is today.</span>
          </h2>

          <p style={{
            marginTop: 28, fontSize: mob ? 16 : 18, lineHeight: 1.55,
            color: 'rgba(255,255,255,0.7)', maxWidth: 520,
          }}>
            Free forever to start. $6.99/month unlocks the full toolkit, unlimited drinks library, and weekly reflections. Cancel anytime, your data stays yours.
          </p>

          <div style={{
            marginTop: 40, display: 'flex',
            flexDirection: mob ? 'column' : 'row',
            gap: 12, flexWrap: 'wrap',
          }}>
            <button style={{
              background: '#fff', color: L.primary,
              border: 'none', borderRadius: 9999,
              padding: '20px 32px', fontFamily: 'inherit',
              fontSize: 16, fontWeight: 700, cursor: 'pointer',
              boxShadow: '0 10px 24px rgba(0,0,0,0.18)',
              display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10,
            }}>
              Begin — it's free
              <svg width="18" height="18" viewBox="0 0 24 24" fill="none"
                stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
                <path d="M5 12h14M12 5l7 7-7 7"/>
              </svg>
            </button>
            <button style={{
              background: 'transparent', color: '#fff',
              border: '1.5px solid rgba(255,255,255,0.3)', borderRadius: 9999,
              padding: '18.5px 28px', fontFamily: 'inherit',
              fontSize: 16, fontWeight: 600, cursor: 'pointer',
            }}>See what's inside</button>
          </div>
        </div>
      </div>
    </section>
  );
}

// ── FOOTER ───────────────────────────────────────────────────
function Footer() {
  const w = useWindowWidth();
  const mob = w < 768;
  return (
    <footer style={{
      background: L.bg, padding: mob ? '32px 20px 48px' : '40px 40px 60px',
      borderTop: '1px solid rgba(31,30,29,0.08)',
    }}>
      <div style={{
        maxWidth: 1240, margin: '0 auto',
        display: 'grid',
        gridTemplateColumns: mob ? '1fr 1fr' : '1.5fr 1fr 1fr 1fr',
        gap: mob ? 32 : 40, paddingTop: 40,
      }}>
        <div style={{ gridColumn: mob ? '1 / -1' : 'auto' }}>
          <Wordmark size={13}/>
          <p style={{
            marginTop: 20, fontSize: 14, lineHeight: 1.55,
            color: L.fg2, maxWidth: 320,
          }}>
            A quiet companion for changing your relationship with alcohol. Made with care, held with respect.
          </p>
        </div>
        {[
          { title: 'Product', items: ['Download', 'Features', 'Pricing', 'What\'s new'] },
          { title: 'Company', items: ['Approach', 'Stories', 'Press', 'Contact'] },
          { title: 'Care', items: [
              { label: 'Privacy', href: '/privacy.html' },
              { label: 'Terms', href: '/terms.html' },
              { label: 'Crisis resources', href: 'https://www.befrienders.org' },
              { label: 'Contact', href: 'mailto:hello@sobella.app' },
            ]},
        ].map(c => (
          <div key={c.title}>
            <Eyebrow>{c.title}</Eyebrow>
            <div style={{ marginTop: 18, display: 'flex', flexDirection: 'column', gap: 10 }}>
              {c.items.map(i => (
                <a key={typeof i === 'string' ? i : i.label}
                   href={typeof i === 'string' ? '#' : i.href}
                   style={{
                     color: L.charcoal, fontSize: 14, fontWeight: 500,
                     textDecoration: 'none',
                   }}>{typeof i === 'string' ? i : i.label}</a>
              ))}
            </div>
          </div>
        ))}
      </div>
      <div style={{
        maxWidth: 1240, margin: '60px auto 0',
        paddingTop: 24, borderTop: '1px solid rgba(31,30,29,0.08)',
        display: 'flex',
        flexDirection: mob ? 'column' : 'row',
        justifyContent: 'space-between', gap: mob ? 8 : 0,
        fontSize: 12, color: L.fg3, fontWeight: 500,
      }}>
        <div>© 2026 Sobella. Held gently.</div>
        <div>If you are in crisis, please call or text 988.</div>
      </div>
    </footer>
  );
}

Object.assign(window, {
  LandingNav: Nav,
  LandingPromise: Promise,
  LandingPillars: Pillars,
  LandingCeremony: Ceremony,
  LandingHowItWorks: HowItWorks,
  LandingFinalCTA: FinalCTA,
  LandingFooter: Footer,
  LandingTokens: L,
  useWindowWidth,
});
