// Domains (bento) + Exploring + Contact + Footer - English only
function SiteDomains() {
  const T = window.TOKENS;
  const C = window.SITE_CONTENT;
  const d = C.domains;
  return (
    <section id="domains" style={{
      padding: "140px 32px",
      borderTop: `1px solid ${T.rule}`,
      background: `linear-gradient(180deg, ${T.bg}, ${T.bgDeep})`,
      position: "relative",
    }}>
      <div style={{
        fontFamily: "'JetBrains Mono', monospace", fontSize: "12px",
        letterSpacing: "0.15em", textTransform: "uppercase",
        color: T.green, marginBottom: "24px",
        display: "flex", alignItems: "center", gap: "10px",
      }}><span>◆</span><span>{d.label}</span></div>
      <h2 style={{
        fontSize: "clamp(48px, 8vw, 110px)",
        fontWeight: 400, letterSpacing: "-0.04em",
        lineHeight: 0.92, margin: "0 0 60px 0",
        maxWidth: "1100px", color: T.ink,
      }}>
        <span>{d.title[0]}</span><br/>
        <span style={{ color: T.green, fontStyle: "italic", fontFamily: "'Fraunces', serif", fontWeight: 300 }}>
          {d.title[1]}
        </span>
      </h2>

      <div style={{
        display: "grid",
        gridTemplateColumns: "repeat(12, 1fr)",
        gap: "16px",
      }}>
        {d.items.map((item, i) => {
          const tone = T.toneFor(item.tone);
          const span = i === 0 ? 7 : i === 1 ? 5 : i === 2 ? 5 : 7;
          return (
            <div key={item.name} className="domain-card" style={{
              gridColumn: `span ${span}`,
              border: `1px solid ${T.ruleHi}`,
              borderRadius: "20px",
              padding: "32px",
              background: T.card,
              transition: "all .3s",
              position: "relative",
              overflow: "hidden",
              minHeight: "240px",
              display: "flex",
              flexDirection: "column",
              justifyContent: "space-between",
            }}>
              <div style={{
                position: "absolute", top: "-50px", right: "-50px",
                width: "200px", height: "200px",
                background: `radial-gradient(circle, ${tone.hi}25, transparent 70%)`,
                pointerEvents: "none",
              }}></div>
              <div style={{ position: "relative", zIndex: 1 }}>
                <div style={{
                  fontFamily: "'Fraunces', serif",
                  fontStyle: "italic",
                  fontSize: "60px",
                  lineHeight: 1, fontWeight: 300,
                  color: tone.hi, marginBottom: "16px",
                }}>0{i+1}.</div>
                <div style={{
                  fontSize: "28px", fontWeight: 500,
                  letterSpacing: "-0.01em",
                  color: T.ink, marginBottom: "20px",
                }}>{item.name}</div>
              </div>
              <div style={{ display: "flex", flexWrap: "wrap", gap: "8px", position: "relative", zIndex: 1 }}>
                {item.items.map(it => (
                  <span key={it} style={{
                    fontFamily: "'JetBrains Mono', monospace", fontSize: "12px",
                    padding: "5px 12px",
                    background: `${T.ink}08`,
                    border: `1px solid ${T.rule}`,
                    borderRadius: "100px",
                    color: T.ink,
                  }}>{it}</span>
                ))}
              </div>
            </div>
          );
        })}
      </div>

      {/* Exploring strip */}
      <div style={{
        marginTop: "60px",
        padding: "28px 32px",
        border: `1px dashed ${T.ruleHi}`,
        borderRadius: "20px",
        display: "flex",
        alignItems: "center",
        gap: "32px",
        flexWrap: "wrap",
      }}>
        <div style={{
          fontFamily: "'JetBrains Mono', monospace", fontSize: "11px",
          color: T.violet, letterSpacing: "0.1em",
          textTransform: "uppercase", whiteSpace: "nowrap",
        }}>// {C.exploring.label}</div>
        <div style={{ display: "flex", flexWrap: "wrap", gap: "10px" }}>
          {C.exploring.items.map(it => (
            <span key={it} style={{
              padding: "6px 14px",
              fontSize: "13px",
              border: `1px solid ${T.violet}40`,
              borderRadius: "100px",
              color: T.ink,
              background: `${T.violet}10`,
            }}>{it}</span>
          ))}
        </div>
      </div>
    </section>
  );
}

function SiteContact() {
  const T = window.TOKENS;
  const C = window.SITE_CONTENT;
  const c = C.contact;
  const f = c.form;
  const [sent, setSent] = React.useState(false);
  const [loading, setLoading] = React.useState(false);
  const [error, setError] = React.useState(null);
  const [name, setName] = React.useState('');
  const [email, setEmail] = React.useState('');
  const [subject, setSubject] = React.useState('');
  const [message, setMessage] = React.useState('');
  const inputStyle = {
    width: "100%",
    padding: "14px 18px",
    background: T.card,
    border: `1px solid ${T.ruleHi}`,
    borderRadius: "12px",
    color: T.ink,
    fontFamily: "inherit",
    fontSize: "15px",
    outline: "none",
    transition: "border-color .2s, background .2s",
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    setLoading(true);
    setError(null);
    const token = document.querySelector('[name="cf-turnstile-response"]')?.value;
    if (!token) {
      setError('Completa la verifica captcha.');
      setLoading(false);
      return;
    }
    try {
      const response = await fetch('https://contact.filibertocanino.it/send-email', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ name, email, subject, message, captchaToken: token }),
      });
      if (response.ok) {
        setSent(true);
        setTimeout(() => setSent(false), 4000);
        // Reset form
        setName('');
        setEmail('');
        setSubject('');
        setMessage('');
        // Reset captcha
        if (window.turnstile) window.turnstile.reset();
      } else {
        const data = await response.json();
        setError(data.error || 'Errore nell\'invio.');
      }
    } catch (err) {
      setError('Errore di rete.');
    } finally {
      setLoading(false);
    }
  };

  return (
    <section id="contact" style={{
      padding: "140px 32px 80px",
      borderTop: `1px solid ${T.rule}`,
      position: "relative",
      overflow: "hidden",
    }}>
      <div style={{
        position: "absolute", top: "20%", right: "-10%",
        width: "50vw", height: "50vw",
        background: `radial-gradient(circle, ${T.orange}20, transparent 60%)`,
        filter: "blur(40px)",
        pointerEvents: "none",
      }}></div>
      <div style={{
        fontFamily: "'JetBrains Mono', monospace", fontSize: "12px",
        letterSpacing: "0.15em", textTransform: "uppercase",
        color: T.orange, marginBottom: "24px",
        display: "flex", alignItems: "center", gap: "10px",
        position: "relative", zIndex: 1,
      }}><span>◆</span><span>{c.label}</span></div>
      <h2 style={{
        fontSize: "clamp(64px, 12vw, 200px)",
        fontWeight: 400, letterSpacing: "-0.05em",
        lineHeight: 0.85, margin: "0 0 24px 0",
        color: T.ink, position: "relative", zIndex: 1,
      }}>
        <span>{c.title[0]} </span>
        <span style={{ color: T.orange, fontStyle: "italic", fontFamily: "'Fraunces', serif", fontWeight: 300 }}>
          {c.title[1]}
        </span><br/>
        <span>{c.title[2]}</span>
      </h2>
      <p style={{
        fontSize: "20px", color: T.inkDim,
        maxWidth: "640px", marginBottom: "60px",
        position: "relative", zIndex: 1, lineHeight: 1.5,
      }}>{c.sub}</p>

      <div style={{
        display: "grid",
        gridTemplateColumns: "1.4fr 1fr",
        gap: "60px",
        maxWidth: "1200px",
        position: "relative", zIndex: 1,
      }}>
        <form onSubmit={handleSubmit}
          style={{ display: "flex", flexDirection: "column", gap: "16px" }}>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "16px" }}>
            <input type="text" required placeholder={f.name} value={name} onChange={e => setName(e.target.value)} style={inputStyle}
              onFocus={e => { e.target.style.borderColor = T.orange; e.target.style.background = T.cardHi; }}
              onBlur={e => { e.target.style.borderColor = T.ruleHi; e.target.style.background = T.card; }}/>
            <input type="email" required placeholder={f.email} value={email} onChange={e => setEmail(e.target.value)} style={inputStyle}
              onFocus={e => { e.target.style.borderColor = T.orange; e.target.style.background = T.cardHi; }}
              onBlur={e => { e.target.style.borderColor = T.ruleHi; e.target.style.background = T.card; }}/>
          </div>
          <input type="text" required placeholder={f.subject} value={subject} onChange={e => setSubject(e.target.value)} style={inputStyle}
            onFocus={e => { e.target.style.borderColor = T.orange; e.target.style.background = T.cardHi; }}
            onBlur={e => { e.target.style.borderColor = T.ruleHi; e.target.style.background = T.card; }}/>
          <textarea required placeholder={f.message} rows={5} value={message} onChange={e => setMessage(e.target.value)}
            style={{ ...inputStyle, resize: "vertical", fontFamily: "inherit" }}
            onFocus={e => { e.target.style.borderColor = T.orange; e.target.style.background = T.cardHi; }}
            onBlur={e => { e.target.style.borderColor = T.ruleHi; e.target.style.background = T.card; }}/>
          <div style={{ display: "flex", justifyContent: "center", margin: "20px 0" }}>
            <div className="cf-turnstile" data-sitekey="0x4AAAAAAA1e7IGsmo79mgWJ"></div>
          </div>
          <button type="submit" disabled={loading} style={{
            padding: "16px 24px",
            background: sent ? T.green : loading ? T.violet : T.orange,
            color: T.bg,
            border: "none",
            borderRadius: "100px",
            fontFamily: "inherit",
            fontSize: "16px",
            fontWeight: 600,
            cursor: loading ? "not-allowed" : "pointer",
            transition: "all .25s",
            alignSelf: "flex-start",
            letterSpacing: "0.02em",
          }}
            onMouseEnter={e => { if (!sent && !loading) e.target.style.transform = "translateY(-2px)"; }}
            onMouseLeave={e => { if (!sent && !loading) e.target.style.transform = "translateY(0)"; }}
          >
            {loading ? "Invio..." : sent ? "✓ Inviato!" : f.submit}
          </button>
          {error && <div style={{ color: T.red, fontSize: "14px", marginTop: "8px" }}>{error}</div>}
          <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: "10px", color: T.inkSoft, marginTop: "4px" }}>
            // {f.placeholder}
          </div>
        </form>

        <aside>
          <div style={{
            fontFamily: "'JetBrains Mono', monospace", fontSize: "11px",
            color: T.inkDim, letterSpacing: "0.1em",
            marginBottom: "16px", textTransform: "uppercase",
          }}>{c.altLabel}</div>
          <div style={{ display: "flex", flexDirection: "column", gap: "10px" }}>
            <a href={c.linkedin} target="_blank" rel="noopener" className="contact-alt" style={{
              padding: "16px 20px",
              background: T.card,
              border: `1px solid ${T.ruleHi}`,
              borderRadius: "14px",
              color: T.ink,
              textDecoration: "none",
              display: "flex", justifyContent: "space-between", alignItems: "center",
              fontSize: "15px", fontWeight: 500,
              transition: "all .2s",
            }}>
              <span>{c.linkedinLabel}</span>
              <span style={{ color: T.violet }}>↗</span>
            </a>
            <a href={c.cv} target="_blank" rel="noopener" className="contact-alt" style={{
              padding: "16px 20px",
              background: T.card,
              border: `1px solid ${T.ruleHi}`,
              borderRadius: "14px",
              color: T.ink,
              textDecoration: "none",
              display: "flex", justifyContent: "space-between", alignItems: "center",
              fontSize: "15px", fontWeight: 500,
              transition: "all .2s",
            }}>
              <span>{c.cvLabel}</span>
              <span style={{ color: T.green }}>↓</span>
            </a>
          </div>
        </aside>
      </div>
    </section>
  );
}

function SiteFooter() {
  const T = window.TOKENS;
  const C = window.SITE_CONTENT;
  return (
    <footer style={{
      padding: "32px",
      borderTop: `1px solid ${T.rule}`,
      display: "flex",
      justifyContent: "space-between",
      alignItems: "center",
      fontFamily: "'JetBrains Mono', monospace",
      fontSize: "11px",
      color: T.inkSoft,
      flexWrap: "wrap",
      gap: "12px",
    }}>
      <div>© Filiberto Canino · 2026</div>
      <div>{C.footer.line1} {C.footer.line2}</div>
    </footer>
  );
}

window.SiteDomains = SiteDomains;
window.SiteContact = SiteContact;
window.SiteFooter = SiteFooter;
