const { useState, useEffect, useRef } = React;

const SUPABASE_URL = "https://pwiycqakanacbqfhepid.supabase.co";
const SUPABASE_ANON_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InB3aXljcWFrYW5hY2JxZmhlcGlkIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzQ5MTgzNzUsImV4cCI6MjA5MDQ5NDM3NX0.OO8-Q3ZAAbUN91L092ZjIDo8Gvzr71uZBsuU0cya81Y";
const db = supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);

function SwyMark({ size = 70 }) {
  return (
    <img
      src="assets/Rotated_trans_cream.png"
      alt="swy"
      width={size}
      height={size}
      style={{ mixBlendMode: "multiply", display: "block", objectFit: "contain" }}
    />
  );
}

function Scanline({ children, className = "", style = {} }) {
  return (
    <span className={"scanline " + className} style={style}>
      <span className="scanline-base" aria-hidden="true">{children}</span>
      <span className="scanline-stripes" aria-hidden="true">{children}</span>
      <span className="sr">{children}</span>
    </span>
  );
}

function TopBar() {
  const [time, setTime] = useState(() => formatTime(new Date()));
  useEffect(() => {
    const id = setInterval(() => setTime(formatTime(new Date())), 1000);
    return () => clearInterval(id);
  }, []);

  return (
    <header className="topbar">
      <div className="brand">
        <a href="/" aria-label="swy home" style={{ display: "flex" }}>  <SwyMark />  </a>
        {/* <span className="wordmark">SWY</span> */}
      </div>
      <div className="topbar-meta">
        <span className="dot" />
        <span>In development</span>
        <span className="sep">·</span>
        {/* <span className="mono">{time} PT</span> */}
        <span className="mono">SWY AI</span>
      </div>
    </header>
  );
}

function formatTime(d) {
  return new Intl.DateTimeFormat("en-US", {
    hour: "2-digit", minute: "2-digit", second: "2-digit",
    hour12: false, timeZone: "America/Los_Angeles",
  }).format(d);
}

function Waitlist() {
  const [email, setEmail] = useState("");
  const [state, setState] = useState("idle");
  const [position, setPosition] = useState(null);
  const inputRef = useRef(null);

  function valid(e) {
    return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e.trim());
  }

  function onSubmit(e) {
    e.preventDefault();
    if (state === "submitting" || state === "done") return;
    if (!valid(email)) {
      setState("error");
      inputRef.current?.focus();
      return;
    }
    setState("submitting");
    db.from("early_access").insert({ email: email.trim() }).then(({ error }) => {
      if (error) {
        setState("error");
      } else {
        const seed = [...email].reduce((a, c) => a + c.charCodeAt(0), 0);
        setPosition(412 + (seed % 287));
        setState("done");
      }
    });
  }

  if (state === "done") {
    return (
      <div className="waitlist done">
        <div className="done-row">
          <span className="check" aria-hidden="true">
            <svg viewBox="0 0 20 20" width="18" height="18">
              <path d="M4 10.5l4 4 8-9" stroke="currentColor" strokeWidth="2.4" fill="none" strokeLinecap="round" strokeLinejoin="round" />
            </svg>
          </span>
          <span>You're on the list.</span>
        </div>
        <div className="done-meta mono">
          {/* <span>POSITION&nbsp;#{position}</span> */}
          {/* <span className="sep">·</span> */}
          {/* <span>{email.replace(/(^.).*(@.*$)/, "$1•••$2")}</span> */}
        </div>
      </div>
    );
  }

  return (
    <form className={"waitlist " + state} onSubmit={onSubmit}>
      <label className="mono label" htmlFor="email">EARLY ACCESS</label>
      <div className="field">
        <input
          ref={inputRef}
          id="email"
          type="email"
          inputMode="email"
          autoComplete="email"
          placeholder="you@yourstore.com"
          value={email}
          onChange={(e) => { setEmail(e.target.value); if (state === "error") setState("idle"); }}
          disabled={state === "submitting"}
        />
        <button type="submit" disabled={state === "submitting"}>
          {state === "submitting" ? (
            <span className="spinner" aria-hidden="true" />
          ) : (
            <>
              <span>Request invite</span>
              <svg viewBox="0 0 20 20" width="16" height="16" aria-hidden="true">
                <path d="M3 10h13M11 5l5 5-5 5" stroke="currentColor" strokeWidth="2" fill="none" strokeLinecap="round" strokeLinejoin="round" />
              </svg>
            </>
          )}
        </button>
      </div>
      <div className="hint mono">
        {state === "error"
          ? <span className="err">↑ that doesn't look like an email.</span>
          : <>No spam. One note when we open the door.</>}
      </div>
    </form>
  );
}

function Ticker() {
  const items = [
    "AGENTIC COMMERCE", "★",
    "BUILT FOR STORES", "★",
    "DYNAMIC PRICING", "★",
    "THE BAZAAR LAYER", "★",
    "BETA · Q3", "★",
    "THE DEAL LAYER", "★",
  ];
  const row = [...items, ...items, ...items, ...items];
  return (
    <div className="ticker" aria-hidden="true">
      <div className="ticker-track">
        {row.map((t, i) => (
          <span key={i} className="ticker-item">{t}</span>
        ))}
      </div>
    </div>
  );
}

function App() {
  return (
    <main className="page">
      <TopBar />

      <section className="hero">
        <div className="eyebrow mono">
          <span className="eyebrow-dash" />
          <span>A NEW LAYER FOR AGENTIC COMMERCE</span>
        </div>

        <h1 className="headline">
          <span className="line">Every great shopkeeper</span>
          <span className="line">
            knows the deal&nbsp;<Scanline>before</Scanline>&nbsp;you ask.
          </span>
        </h1>

        <p className="subhead">Soon, so will your store.</p>

        <div className="cta">
          <Waitlist />
        </div>
      </section>

      <section className="lower">
        <div className="lower-grid">
          <div className="card">
            <div className="card-label mono">01 / KNOWS</div>
            <div className="card-body">
              Learns your products, your margins, and what your customers actually want.
            </div>
          </div>
          <div className="card">
            <div className="card-label mono">02 / OFFERS</div>
            <div className="card-body">
              Quotes the right price at the right moment — like a clerk who's been there for years.
            </div>
          </div>
          <div className="card">
            <div className="card-label mono">03 / CLOSES</div>
            <div className="card-body">
              Carts, checkouts, rewards and follow-ups, handled. You keep the margins.
            </div>
          </div>
        </div>
      </section>

      <Ticker />

      <footer className="foot">
        <div className="foot-left mono">
          <span className="dot small" />
          GETSWY.AI
        </div>
        <div className="foot-mid mono">
          <div>© 2026 SWY — ALL RIGHTS NOT YET RESERVED</div>
          <div className="foot-socials">
            <a href="https://www.linkedin.com/company/swy-ai" target="_blank" rel="noopener noreferrer">LINKEDIN</a>
            <a href="https://x.com/swy_ai" target="_blank" rel="noopener noreferrer">X</a>
            <a href="https://www.instagram.com/swy_ai/" target="_blank" rel="noopener noreferrer">INSTAGRAM</a>
          </div>
        </div>
        <div className="foot-right mono">
          <a href="mailto:hello@getswy.ai">HELLO@GETSWY.AI</a>
        </div>
      </footer>

      <PageStyles />
    </main>
  );
}

function PageStyles() {
  return (
    <style>{`
      .page {
        min-height: 100vh;
        display: flex; flex-direction: column;
        padding: 28px 36px 0;
        max-width: 1480px;
        margin: 0 auto;
      }

      .mono { font-family: 'JetBrains Mono', ui-monospace, monospace; letter-spacing: 0.04em; }
      .sr { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

      /* Top bar */
      .topbar {
        display: flex; justify-content: space-between; align-items: center;
        padding-bottom: 10px;
        border-bottom: 1px solid rgba(245, 230, 203, 0.22);
      }
      .brand { display: flex; align-items: center; gap: 10px; color: var(--cream); }
      .wordmark {
        font-family: 'Archivo Black', sans-serif;
        font-size: 22px;
        letter-spacing: -0.02em;
        line-height: 1;
        margin-top: 2px;
      }
      .topbar-meta {
        display: flex; align-items: center; gap: 10px;
        font-family: 'JetBrains Mono', monospace;
        font-size: 11.5px;
        color: var(--cream-dim);
        text-transform: uppercase;
        letter-spacing: 0.08em;
      }
      .topbar-meta .sep { opacity: 0.5; }
      .dot {
        width: 8px; height: 8px; border-radius: 999px;
        background: var(--cream);
        animation: pulse 1.8s ease-out infinite;
      }
      .dot.small { width: 6px; height: 6px; animation: none; }
      @keyframes pulse {
        0%   { box-shadow: 0 0 0 0 rgba(245,230,203,0.55); }
        70%  { box-shadow: 0 0 0 9px rgba(245,230,203,0); }
        100% { box-shadow: 0 0 0 0 rgba(245,230,203,0); }
      }

      /* Hero */
      .hero {
        padding: 7vh 0 6vh;
        flex: 1;
        display: flex; flex-direction: column;
        justify-content: center;
        gap: 28px;
      }
      .eyebrow {
        display: flex; align-items: center; gap: 12px;
        color: var(--cream);
        font-size: 11.5px;
        letter-spacing: 0.18em;
        text-transform: uppercase;
        opacity: 0.85;
      }
      .eyebrow-dash { width: 28px; height: 2px; background: var(--cream); display: inline-block; }

      .headline {
        font-family: 'Archivo Black', 'Archivo', sans-serif;
        font-weight: 900;
        font-size: clamp(44px, 8.4vw, 132px);
        line-height: 0.95;
        letter-spacing: -0.035em;
        color: var(--cream);
        text-wrap: balance;
        text-shadow: 0 2px 0 rgba(60, 20, 0, 0.06);
      }
      .headline .line { display: block; }

      .subhead {
        font-family: 'Archivo', sans-serif;
        font-weight: 500;
        font-size: clamp(20px, 2.2vw, 30px);
        color: var(--cream);
        opacity: 0.88;
        max-width: 60ch;
        margin-top: -8px;
      }

      /* Scanline text effect */
      .scanline { position: relative; display: inline-block; }
      .scanline-base { color: rgba(245, 230, 203, 0.28); }
      .scanline-stripes {
        position: absolute; inset: 0;
        color: var(--cream);
        -webkit-mask-image: repeating-linear-gradient(to bottom, black 0, black 4px, transparent 4px, transparent 8px);
        mask-image: repeating-linear-gradient(to bottom, black 0, black 4px, transparent 4px, transparent 8px);
        animation: scanmove 6s linear infinite;
      }
      @keyframes scanmove {
        0%   { -webkit-mask-position: 0 0;    mask-position: 0 0; }
        100% { -webkit-mask-position: 0 16px; mask-position: 0 16px; }
      }

      /* CTA / waitlist */
      .cta { margin-top: 12px; }
      .waitlist { display: flex; flex-direction: column; gap: 10px; max-width: 520px; }
      .label { font-size: 11px; color: var(--cream-dim); letter-spacing: 0.18em; }
      .field {
        display: flex; align-items: stretch;
        background: rgba(42, 22, 6, 0.18);
        border: 1.5px solid var(--cream);
        border-radius: 999px;
        padding: 6px 6px 6px 22px;
        gap: 8px;
        transition: background 160ms ease, transform 160ms ease;
      }
      .waitlist.error .field {
        border-color: #FFD7B0;
        background: rgba(200, 72, 8, 0.28);
        animation: shake 360ms ease;
      }
      @keyframes shake {
        0%, 100% { transform: translateX(0); }
        20%  { transform: translateX(-6px); }
        40%  { transform: translateX(5px); }
        60%  { transform: translateX(-3px); }
        80%  { transform: translateX(2px); }
      }
      .field input {
        flex: 1;
        background: transparent; border: none; outline: none;
        color: var(--cream);
        font-family: 'Archivo', sans-serif;
        font-size: 18px; font-weight: 500;
        padding: 12px 0;
        letter-spacing: -0.01em;
      }
      .field input::placeholder { color: rgba(245,230,203,0.5); }
      .field button {
        display: inline-flex; align-items: center; gap: 8px;
        background: var(--cream);
        color: var(--orange-deep);
        border: none; border-radius: 999px;
        padding: 0 22px; height: 46px;
        font-family: 'Archivo', sans-serif;
        font-weight: 700; font-size: 15px;
        letter-spacing: -0.005em;
        cursor: pointer;
        transition: transform 140ms ease, background 140ms ease, box-shadow 140ms ease;
        box-shadow: 0 2px 0 rgba(60,20,0,0.18);
      }
      .field button:hover:not(:disabled) {
        transform: translateY(-1px);
        background: #fff3df;
        box-shadow: 0 4px 0 rgba(60,20,0,0.18);
      }
      .field button:active:not(:disabled) { transform: translateY(1px); box-shadow: 0 0 0 rgba(60,20,0,0.18); }
      .field button:disabled { opacity: 0.7; cursor: default; }
      .spinner {
        width: 16px; height: 16px;
        border: 2px solid rgba(200,72,8,0.25);
        border-top-color: var(--orange-deep);
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
      }
      @keyframes spin { to { transform: rotate(360deg); } }

      .hint { font-size: 11.5px; color: var(--cream-dim); text-transform: uppercase; letter-spacing: 0.1em; padding-left: 22px; }
      .hint .err { color: #FFEAD0; }

      .waitlist.done { display: flex; flex-direction: column; gap: 8px; max-width: 520px; }
      .done-row {
        display: inline-flex; align-items: center; gap: 12px;
        background: var(--cream); color: var(--orange-deep);
        padding: 14px 22px; border-radius: 999px;
        font-family: 'Archivo', sans-serif; font-weight: 700; font-size: 17px;
        align-self: flex-start;
        animation: pop 320ms cubic-bezier(.2,.9,.3,1.3);
      }
      @keyframes pop { 0% { transform: scale(0.92); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
      .check {
        display: inline-flex; align-items: center; justify-content: center;
        width: 22px; height: 22px; border-radius: 999px;
        background: var(--orange-deep); color: var(--cream);
      }
      .done-meta {
        display: flex; gap: 10px; align-items: center;
        font-size: 11.5px; color: var(--cream-dim);
        text-transform: uppercase; padding-left: 22px;
      }
      .done-meta .sep { opacity: 0.5; }

      /* Lower cards */
      .lower { padding: 12vh 0 80px; }
      .lower-grid {
        display: grid; grid-template-columns: repeat(3, 1fr);
        gap: 1px;
        background: rgba(245,230,203,0.22);
        border-top: 1px solid rgba(245,230,203,0.22);
        border-bottom: 1px solid rgba(245,230,203,0.22);
      }
      .card {
        background: var(--orange);
        padding: 32px 28px 38px;
        display: flex; flex-direction: column; gap: 20px;
        min-height: 200px;
        transition: background 200ms ease;
      }
      .card:hover { background: #F1581A; }
      .card-label { font-size: 11.5px; color: var(--cream-dim); letter-spacing: 0.16em; }
      .card-body {
        font-family: 'Archivo', sans-serif; font-weight: 500;
        font-size: 19px; line-height: 1.35;
        color: var(--cream); letter-spacing: -0.005em;
        text-wrap: pretty; max-width: 32ch;
      }

      /* Ticker */
      .ticker {
        overflow: hidden;
        border-top: 1px solid rgba(245,230,203,0.22);
        border-bottom: 1px solid rgba(245,230,203,0.22);
        margin: 0 -36px; padding: 18px 0;
      }
      .ticker-track {
        display: inline-flex; gap: 36px;
        white-space: nowrap;
        animation: marquee 38s linear infinite;
      }
      .ticker-item { font-family: 'Archivo Black', sans-serif; font-size: 22px; letter-spacing: 0.02em; color: var(--cream); }
      .ticker-item:nth-child(even) { font-family: 'JetBrains Mono', monospace; font-size: 14px; opacity: 0.55; align-self: center; }
      @keyframes marquee {
        from { transform: translateX(0); }
        to   { transform: translateX(-25%); }
      }

      /* Footer */
      .foot {
        display: grid; grid-template-columns: 1fr 1fr 1fr;
        align-items: center;
        padding: 22px 0 28px;
        font-size: 11.5px; color: var(--cream-dim);
        text-transform: uppercase; letter-spacing: 0.14em;
      }
      .foot-left { display: inline-flex; align-items: center; gap: 10px; }
      .foot-mid { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; }
      .foot-socials { display: flex; gap: 14px; }
      .foot-socials a { color: var(--cream-dimmer); text-decoration: none; transition: color 150ms ease; }
      .foot-socials a:hover { color: var(--cream); }
      .foot-right { text-align: right; }
      .foot a { color: inherit; text-decoration: none; border-bottom: 1px solid rgba(245,230,203,0.35); padding-bottom: 1px; }
      .foot a:hover { color: var(--cream); border-bottom-color: var(--cream); }

      /* Responsive */
      @media (max-width: 820px) {
        .page { padding: 16px 20px 0; }
        .ticker { margin: 0 -20px; }

        /* Topbar */
        .topbar-meta { font-size: 10px; gap: 7px; }

        /* Hero */
        .hero { padding: 5vh 0 4vh; gap: 20px; }
        .eyebrow { font-size: 10px; letter-spacing: 0.12em; }
        .subhead { font-size: clamp(18px, 4.8vw, 24px); margin-top: -4px; }

        /* Form — 16px input prevents iOS Safari auto-zoom */
        .waitlist { max-width: 100%; }
        .label { padding-left: 14px; }
        .field { padding: 6px; gap: 6px; }
        .field input { padding: 10px 14px; min-width: 0; flex: 1; font-size: 16px; }
        .field button { width: auto; flex-shrink: 0; padding: 0; width: 46px; justify-content: center; }
        .field button span:not(.spinner) { display: none; }
        .hint { padding-left: 14px; }

        /* Cards */
        .lower { padding: 6vh 0 48px; }
        .lower-grid { grid-template-columns: 1fr; }
        .card { padding: 24px 20px 28px; min-height: unset; }
        .card-body { font-size: 17px; }

        /* Footer */
        .foot { grid-template-columns: 1fr; gap: 20px; padding: 24px 0 36px; }
        .foot-mid { align-items: flex-start; text-align: left; }
        .foot-right { text-align: left; }
      }

      /* Finer scanlines on mobile so more stripes fit the smaller text */
      .scanline-stripes {
        -webkit-mask-image: repeating-linear-gradient(to bottom, black 0, black 2px, transparent 2px, transparent 4px);
        mask-image: repeating-linear-gradient(to bottom, black 0, black 2px, transparent 2px, transparent 4px);
        animation: scanmove-mobile 6s linear infinite;
      }
      @keyframes scanmove-mobile {
        0%   { -webkit-mask-position: 0 0;   mask-position: 0 0; }
        100% { -webkit-mask-position: 0 8px; mask-position: 0 8px; }
      }

      /* Small phones — hide verbose topbar text, keep dot + label */
      @media (max-width: 390px) {
        .topbar-meta span:not(.dot):not(.mono) { display: none; }
        .topbar-meta .sep { display: none; }
        .eyebrow-dash { display: none; }
      }
    `}</style>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
