// ============================================================================
// IMS — Auth: Sign-in page + Magic link sent page (full-screen, no modal)
// ============================================================================

function GoogleIcon() {
  return (
    <svg width="17" height="17" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg" style={{ flexShrink: 0 }}>
      <path d="M17.64 9.205c0-.639-.057-1.252-.164-1.841H9v3.481h4.844a4.14 4.14 0 0 1-1.796 2.716v2.259h2.908c1.702-1.567 2.684-3.875 2.684-6.615Z" fill="#4285F4"/>
      <path d="M9 18c2.43 0 4.467-.806 5.956-2.18l-2.908-2.259c-.806.54-1.837.86-3.048.86-2.344 0-4.328-1.584-5.036-3.711H.957v2.332A8.997 8.997 0 0 0 9 18Z" fill="#34A853"/>
      <path d="M3.964 10.71A5.41 5.41 0 0 1 3.682 9c0-.593.102-1.17.282-1.71V4.958H.957A8.996 8.996 0 0 0 0 9c0 1.452.348 2.827.957 4.042l3.007-2.332Z" fill="#FBBC05"/>
      <path d="M9 3.58c1.321 0 2.508.454 3.44 1.345l2.582-2.58C13.463.891 11.426 0 9 0A8.997 8.997 0 0 0 .957 4.958L3.964 7.29C4.672 5.163 6.656 3.58 9 3.58Z" fill="#EA4335"/>
    </svg>
  );
}

function AppleIcon() {
  return (
    <svg width="17" height="17" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" style={{ flexShrink: 0 }}>
      <path d="M18.71 19.5c-.83 1.24-1.71 2.45-3.05 2.47-1.34.03-1.77-.79-3.29-.79-1.53 0-2 .77-3.27.82-1.31.05-2.3-1.32-3.14-2.53C4.25 17 2.94 12.45 4.7 9.39c.87-1.52 2.43-2.48 4.12-2.51 1.28-.02 2.5.87 3.29.87.78 0 2.26-1.07 3.8-.91.65.03 2.47.26 3.64 1.98l-.09.06c-.22.14-2.15 1.26-2.13 3.76.03 2.99 2.63 3.99 2.66 4l.06.02c-.08.22-.42 1.44-1.34 2.84zM13 3.5c.73-.83 1.94-1.46 2.94-1.5.13 1.17-.34 2.35-1.04 3.19-.69.85-1.83 1.51-2.95 1.42-.15-1.15.41-2.35 1.05-3.11z"/>
    </svg>
  );
}

// ── Sign-in page ──────────────────────────────────────────────────────────────

function AuthSignInPage({ onSent, onAuth }) {
  const [email, setEmail] = useState("");
  const [loading, setLoading] = useState(false);
  const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim());

  const handleMagicLink = () => {
    if (!valid || loading) return;
    setLoading(true);
    setTimeout(() => { setLoading(false); onSent(email.trim()); }, 900);
  };

  return (
    <AuthPage>
      <div style={A.brand}>
        <div style={A.brandMark}>i</div>
        <div>
          <div style={A.brandName}>IMS</div>
          <div style={A.brandSub}>Influencer Management System</div>
        </div>
      </div>

      <div style={A.cardHead}>
        <h1 style={A.heading}>Sign in to your account</h1>
        <p style={A.sub}>We'll send you a secure login link to your email address.</p>
      </div>

      <div style={A.field}>
        <label style={A.label}>Email address</label>
        <input
          style={A.input}
          type="email"
          placeholder="you@company.com"
          value={email}
          autoFocus
          onChange={e => setEmail(e.target.value)}
          onKeyDown={e => e.key === "Enter" && handleMagicLink()}
          onFocus={e => e.target.style.borderColor = "var(--accent)"}
          onBlur={e => e.target.style.borderColor = "var(--line-2)"}
        />
      </div>

      <button
        style={{ ...A.btnPrimary, ...(!valid || loading ? A.btnMuted : {}) }}
        disabled={!valid || loading}
        onClick={handleMagicLink}
      >
        {loading ? "Sending…" : "Send magic link"}
      </button>

      <div style={A.divider}>
        <span style={A.dividerLine} />
        <span style={A.dividerText}>or continue with</span>
        <span style={A.dividerLine} />
      </div>

      <div style={A.oauthRow}>
        <button style={A.btnOAuth} onClick={onAuth}>
          <GoogleIcon />
          Google
        </button>
        <button style={A.btnOAuth} onClick={onAuth}>
          <AppleIcon />
          Apple
        </button>
      </div>

      <p style={A.fine}>
        By signing in you agree to our{" "}
        <span style={A.link}>Terms of Service</span> and{" "}
        <span style={A.link}>Privacy Policy</span>.
      </p>
    </AuthPage>
  );
}

// ── Magic link sent page ──────────────────────────────────────────────────────

function AuthMagicSentPage({ email, onBack, onAuth }) {
  const [resent, setResent] = useState(false);

  const handleResend = () => {
    if (resent) return;
    setResent(true);
    setTimeout(() => setResent(false), 3000);
  };

  return (
    <AuthPage>
      <div style={A.brand}>
        <div style={A.brandMark}>i</div>
        <div>
          <div style={A.brandName}>IMS</div>
          <div style={A.brandSub}>Influencer Management System</div>
        </div>
      </div>

      <div style={A.sentIconWrap}>
        <svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" style={{ color: "var(--accent)" }}>
          <rect x="2" y="4" width="20" height="16" rx="2"/>
          <path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/>
        </svg>
      </div>

      <div style={A.cardHead}>
        <h1 style={A.heading}>Check your inbox</h1>
        <p style={A.sub}>
          We have sent a magic link to your email. Please check your inbox and spam folder.
        </p>
      </div>

      <div style={A.emailChip}>{email}</div>

      <button style={{ ...A.btnPrimary, marginTop: 20, marginBottom: 0 }} onClick={onAuth}>
        Enter IMS →
      </button>

      <p style={{ ...A.fine, textAlign: "center", marginTop: 14 }}>
        Didn't receive it?{" "}
        <button style={A.linkBtn} onClick={handleResend}>
          {resent ? "Sent!" : "Resend link"}
        </button>
      </p>

      <button style={A.btnBack} onClick={onBack}>
        ← Back to sign in
      </button>
    </AuthPage>
  );
}

// ── Full-screen page wrapper ──────────────────────────────────────────────────

function AuthPage({ children }) {
  return (
    <div style={A.screen}>
      {/* Left panel — branding / pattern */}
      <div style={A.left}>
        <div style={A.leftInner}>
          <div style={A.leftQuote}>
            "The right influencer, the right message, the right moment."
          </div>
          <div style={A.leftMeta}>IMS · v0.9</div>
        </div>
        <div style={A.leftGrid} />
      </div>

      {/* Right panel — form sits directly on surface, no card box */}
      <div style={A.right}>
        <div style={A.formWrap}>
          {children}
        </div>
        <p style={A.copyright}>© 2026 IMS. All rights reserved.</p>
      </div>
    </div>
  );
}

// ── Orchestrator ──────────────────────────────────────────────────────────────

function AuthFlow({ onAuth }) {
  const [view, setView] = useState("signin");
  const [sentEmail, setSentEmail] = useState("");

  if (view === "sent") {
    return <AuthMagicSentPage email={sentEmail} onBack={() => setView("signin")} onAuth={onAuth} />;
  }

  return <AuthSignInPage onSent={(email) => { setSentEmail(email); setView("sent"); }} onAuth={onAuth} />;
}

// ── Styles ────────────────────────────────────────────────────────────────────

const A = {
  screen: {
    display: "grid",
    gridTemplateColumns: "1fr 1fr",
    minHeight: "100vh",
    background: "var(--bg)",
    fontFamily: "var(--font)",
  },

  // Left decorative panel
  left: {
    position: "relative",
    background: "var(--ink)",
    display: "flex",
    flexDirection: "column",
    justifyContent: "flex-end",
    padding: "48px 52px",
    overflow: "hidden",
  },
  leftInner: {
    position: "relative",
    zIndex: 2,
  },
  leftQuote: {
    fontSize: 22,
    fontWeight: 600,
    color: "var(--accent-ink)",
    lineHeight: 1.45,
    letterSpacing: "-0.02em",
    maxWidth: 340,
    marginBottom: 24,
  },
  leftMeta: {
    fontSize: 11,
    color: "rgba(250,248,244,0.35)",
    fontFamily: "'IBM Plex Mono', monospace",
    letterSpacing: "0.1em",
    textTransform: "uppercase",
  },
  leftGrid: {
    position: "absolute",
    inset: 0,
    backgroundImage: `
      linear-gradient(rgba(192,106,44,0.08) 1px, transparent 1px),
      linear-gradient(90deg, rgba(192,106,44,0.08) 1px, transparent 1px)
    `,
    backgroundSize: "40px 40px",
    zIndex: 1,
  },

  // Right form panel — no card, form on bare surface
  right: {
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "center",
    padding: "48px 40px",
    background: "var(--bg)",
    position: "relative",
  },
  formWrap: {
    width: "100%",
    maxWidth: 420,
    display: "flex",
    flexDirection: "column",
  },
  copyright: {
    position: "absolute",
    bottom: 20,
    fontSize: 11,
    color: "var(--ink-4)",
    fontFamily: "'IBM Plex Mono', monospace",
  },

  // Brand block
  brand: {
    display: "flex",
    alignItems: "center",
    gap: 10,
    marginBottom: 36,
  },
  brandMark: {
    width: 32, height: 32,
    background: "var(--accent)",
    color: "var(--accent-ink)",
    borderRadius: "var(--r)",
    display: "grid", placeItems: "center",
    fontSize: 15, fontWeight: 700,
    letterSpacing: "-0.03em",
    flexShrink: 0,
  },
  brandName: {
    fontSize: 14, fontWeight: 700,
    letterSpacing: "-0.02em",
    color: "var(--ink)",
    lineHeight: 1.2,
  },
  brandSub: {
    fontSize: 10.5,
    color: "var(--ink-4)",
    fontFamily: "'IBM Plex Mono', monospace",
    letterSpacing: "0.02em",
    marginTop: 2,
  },

  // Text
  cardHead: { marginBottom: 20 },
  heading: {
    fontSize: 18, fontWeight: 700,
    color: "var(--ink)",
    letterSpacing: "-0.02em",
    margin: "0 0 6px",
    lineHeight: 1.3,
  },
  sub: {
    fontSize: 13,
    color: "var(--ink-3)",
    margin: 0,
    lineHeight: 1.6,
  },

  // Form
  field: {
    display: "flex", flexDirection: "column", gap: 5,
    marginBottom: 12,
  },
  label: {
    fontSize: 10.5,
    fontWeight: 600,
    letterSpacing: "0.07em",
    textTransform: "uppercase",
    color: "var(--ink-4)",
    fontFamily: "'IBM Plex Mono', monospace",
  },
  input: {
    width: "100%",
    padding: "10px 13px",
    background: "var(--paper)",
    border: "1px solid var(--line-2)",
    borderRadius: "var(--r-md)",
    fontSize: 13.5,
    color: "var(--ink)",
    outline: "none",
    transition: "border-color 0.15s",
    boxShadow: "var(--shadow-sm)",
  },

  // Buttons
  btnPrimary: {
    width: "100%",
    padding: "10px 16px",
    background: "var(--accent)",
    color: "var(--accent-ink)",
    borderRadius: "var(--r)",
    fontSize: 13.5,
    fontWeight: 600,
    letterSpacing: "-0.01em",
    cursor: "pointer",
    border: "none",
    marginTop: 4,
    marginBottom: 20,
  },
  btnMuted: { opacity: 0.45, cursor: "not-allowed" },
  oauthRow: {
    display: "grid",
    gridTemplateColumns: "1fr 1fr",
    gap: 8,
    marginBottom: 0,
  },
  btnOAuth: {
    padding: "10px 12px",
    background: "var(--paper)",
    border: "1px solid var(--line-2)",
    boxShadow: "var(--shadow-sm)",
    borderRadius: "var(--r)",
    fontSize: 13,
    fontWeight: 500,
    color: "var(--ink-2)",
    cursor: "pointer",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    gap: 8,
  },
  divider: {
    display: "flex", alignItems: "center", gap: 10,
    marginBottom: 14,
  },
  dividerLine: {
    flex: 1, height: 1,
    background: "var(--line)",
  },
  dividerText: {
    fontSize: 10.5,
    color: "var(--ink-4)",
    fontFamily: "'IBM Plex Mono', monospace",
    letterSpacing: "0.06em",
    whiteSpace: "nowrap",
  },
  fine: {
    fontSize: 11.5,
    color: "var(--ink-4)",
    margin: "16px 0 0",
    lineHeight: 1.6,
  },
  link: {
    color: "var(--accent)",
    cursor: "pointer",
    textDecoration: "underline",
    textUnderlineOffset: 2,
  },

  // Sent page extras
  sentIconWrap: {
    width: 52, height: 52,
    background: "var(--accent-bg)",
    border: "1px solid var(--accent-dim)",
    borderRadius: "var(--r-lg)",
    display: "flex", alignItems: "center", justifyContent: "center",
    marginBottom: 20,
  },
  emailChip: {
    padding: "8px 14px",
    background: "var(--bg-2)",
    border: "1px solid var(--line)",
    borderRadius: "var(--r)",
    fontFamily: "'IBM Plex Mono', monospace",
    fontSize: 12,
    color: "var(--ink-2)",
    fontWeight: 500,
    textAlign: "center",
  },
  linkBtn: {
    background: "none", border: "none", padding: 0,
    color: "var(--accent)",
    fontSize: "inherit",
    fontFamily: "inherit",
    cursor: "pointer",
    textDecoration: "underline",
    textUnderlineOffset: 2,
  },
  btnBack: {
    width: "100%",
    padding: "9px 16px",
    background: "transparent",
    border: "1px solid var(--line-2)",
    borderRadius: "var(--r)",
    fontSize: 13,
    fontWeight: 500,
    color: "var(--ink-3)",
    cursor: "pointer",
    marginTop: 12,
  },
};
