const { useState, useEffect } = React;

// ============ Tweak defaults (persisted by host) ============
const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "logoSize": 72
} /*EDITMODE-END*/;

// ============ Language helper ============
function useLang() {
  const [lang, setLangState] = useState(() => {
    try { return localStorage.getItem("snu-lang") || "en"; } catch (e) { return "en"; }
  });
  useEffect(() => {
    document.documentElement.setAttribute("lang", lang);
    try { localStorage.setItem("snu-lang", lang); } catch (e) {}
  }, [lang]);
  return [lang, setLangState];
}

function LangToggle({ lang, setLang }) {
  return (
    <div className="lang-toggle" role="group" aria-label="Language">
      <button className={lang === "en" ? "active" : ""} onClick={() => setLang("en")} aria-pressed={lang === "en"}>EN</button>
      <button className={lang === "th" ? "active" : ""} onClick={() => setLang("th")} aria-pressed={lang === "th"}>TH</button>
    </div>);
}

// ============ Placeholder ============
function PH({ label = "Image" }) {
  const id = "ph-c-" + label.replace(/\W/g, '');
  return (
    <svg viewBox="0 0 400 300" preserveAspectRatio="xMidYMid slice" style={{ width: '100%', height: '100%', display: 'block' }}>
      <defs>
        <pattern id={id} patternUnits="userSpaceOnUse" width="8" height="8" patternTransform="rotate(45)">
          <line x1="0" y1="0" x2="0" y2="8" stroke="#d9d3c0" strokeWidth="1.5" />
        </pattern>
      </defs>
      <rect x="0" y="0" width="400" height="300" fill="#ebe5d2" />
      <rect x="0" y="0" width="400" height="300" fill={`url(#${id})`} opacity="0.6" />
      <text x="20" y="285" fill="#6b6557" fontFamily="JetBrains Mono, monospace" fontSize="10" letterSpacing="1.2">{label.toUpperCase()}</text>
    </svg>);
}

// ============ Brand + Nav ============
function Brand({ onClick }) {
  return (
    <div className="brand" onClick={onClick} data-comment-anchor="brand-logo">
      <img className="logo" src="assets/logo-colour.png" alt="Siam Nex Up" style={{ width: "70px", height: "70px" }} />
    </div>);
}

const PAGE_IDS = ["home", "about", "services", "cases", "insights", "contact"];

function Nav({ page, setPage, t, lang, setLang }) {
  return (
    <div className="nav-wrap">
      <nav className="nav" data-screen-label="Global nav">
        <Brand onClick={() => setPage("home")} />
        <div className="nav-links">
          {PAGE_IDS.slice(0, 5).map((id) =>
            <button key={id} className={page === id ? "active" : ""} onClick={() => setPage(id)}>{t.nav[id]}</button>
          )}
        </div>
        <div className="nav-right">
          <LangToggle lang={lang} setLang={setLang} />
          <button className="nav-cta" onClick={() => setPage("contact")}>{t.cta.getInTouch}</button>
        </div>
      </nav>
    </div>);
}

// ============ Buffer diagram (purple/coral palette) ============
function BufferDiagram({ t }) {
  return (
    <svg viewBox="0 0 400 500" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <linearGradient id="snu-grad" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#3a1466" />
          <stop offset="50%" stopColor="#c4226e" />
          <stop offset="100%" stopColor="#f47a36" />
        </linearGradient>
      </defs>

      {/* Grid */}
      <g>
        <rect x="60" y="20" width="280" height="56" rx="14" fill="#faf7f5" stroke="#ddd4dc" />
        <text x="200" y="44" textAnchor="middle" fill="#6b5e75" fontFamily="JetBrains Mono" fontSize="10" letterSpacing="1.5">{t.diagram.grid}</text>
        <text x="200" y="62" textAnchor="middle" fill="#3a1466" fontFamily="Manrope" fontWeight="700" fontSize="13">{t.diagram.gridSub}</text>
      </g>

      {/* Arrow */}
      <g stroke="#3a1466" fill="none" strokeWidth="1.5">
        <line x1="200" y1="80" x2="200" y2="110" />
        <polygon points="195,108 200,118 205,108" fill="#3a1466" />
      </g>
      <text x="210" y="100" fill="#6b5e75" fontFamily="JetBrains Mono" fontSize="9">{t.diagram.gridArrow}</text>

      {/* VRFB Battery — gradient fill */}
      <g>
        <rect x="60" y="130" width="280" height="160" rx="20" fill="url(#snu-grad)" />
        <text x="80" y="158" fill="rgba(255,255,255,0.8)" fontFamily="JetBrains Mono" fontSize="10" letterSpacing="1.5">{t.diagram.buffer}</text>
        <text x="80" y="200" fill="#fff" fontFamily="Manrope" fontWeight="700" fontSize="40" letterSpacing="-2">500</text>
        <text x="162" y="200" fill="#fff" fontFamily="Manrope" fontWeight="500" fontSize="18" opacity="0.85">kWh</text>
        <text x="80" y="220" fill="#fff" fontFamily="Manrope" fontSize="12" opacity="0.8">{t.diagram.bufferSub}</text>
        <line x1="80" y1="234" x2="320" y2="234" stroke="rgba(255,255,255,0.25)" />
        <g transform="translate(80, 250)">
          {[0, 1, 2, 3, 4, 5, 6, 7].map((i) =>
            <rect key={i} x={i * 31} y="0" width="26" height="22" rx="4" fill="#fff" opacity={i < 6 ? 0.95 : 0.22} />
          )}
        </g>
      </g>

      {/* Arrow */}
      <g stroke="#3a1466" fill="none" strokeWidth="1.5">
        <line x1="200" y1="300" x2="200" y2="330" />
        <polygon points="195,328 200,338 205,328" fill="#3a1466" />
      </g>
      <text x="210" y="320" fill="#6b5e75" fontFamily="JetBrains Mono" fontSize="9">{t.diagram.burst}</text>

      {/* Charger */}
      <g>
        <rect x="60" y="350" width="280" height="90" rx="16" fill="#faf7f5" stroke="#ddd4dc" />
        <text x="80" y="378" fill="#6b5e75" fontFamily="JetBrains Mono" fontSize="10" letterSpacing="1.5">{t.diagram.charger}</text>
        <g transform="translate(165, 388)">
          <rect x="0" y="0" width="70" height="44" rx="8" fill="#3a1466" />
          <text x="35" y="22" textAnchor="middle" fill="#f47a36" fontFamily="Manrope" fontSize="11" fontWeight="700">120</text>
          <text x="35" y="34" textAnchor="middle" fill="rgba(255,255,255,0.7)" fontFamily="JetBrains Mono" fontSize="7">kW DC</text>
        </g>
      </g>

      {/* VPP label */}
      <g transform="translate(60, 460)">
        <rect x="0" y="0" width="280" height="28" rx="14" fill="#f47a36" />
        <text x="14" y="18" fill="#fff" fontFamily="JetBrains Mono" fontSize="10" letterSpacing="1.5">{t.diagram.vpp}</text>
        <text x="180" y="18" fill="#fff" fontFamily="Manrope" fontWeight="600" fontSize="11">{t.diagram.vppSub}</text>
      </g>
    </svg>);
}

// ============ Home ============
function HomePage({ setPage, c, t }) {
  return (
    <div className="page-enter" data-screen-label="Home">
      <section className="hero container">
        <div className="badge-row">
          <span className="badge"><span className="dot"></span>{t.badges.reliability}</span>
          <span className="label">{t.badges.slogan}</span>
        </div>
        <h1>{t.hero.h1Pre}<em>{t.hero.h1Em}</em>{t.hero.h1Post}</h1>
        <div className="hero-bottom">
          <p>{t.hero.lead}</p>
          <div className="hero-cards">
            <div className="hc">
              <div className="lbl">{t.hero.card1Lbl}</div>
              <div className="v">500<sup>kWh</sup></div>
              <div className="k">{t.hero.card1K}</div>
            </div>
            <div className="hc amber">
              <div className="lbl">{t.hero.card2Lbl}</div>
              <div className="v">30–60<sup>%</sup></div>
              <div className="k">{t.hero.card2K}</div>
            </div>
          </div>
        </div>
        <div className="stats-strip">
          {c.hero.stats.map((s, i) =>
            <div className="item" key={i}>
              <div className="v">{s.v}<sup>{s.unit}</sup></div>
              <div className="k">{s.k}</div>
            </div>
          )}
        </div>
      </section>

      <section className="section bg-2">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.home.sec02Label}</div>
              <h2>{t.home.sec02Heading[0]}<em>{t.home.sec02Heading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.home.sec02Body}</p>
            </div>
          </div>
          <div className="pillars">
            {c.pillars.map((p, i) =>
              <div className="pillar" key={i}>
                <div className="ix">{p.ix}</div>
                <h3>{p.title}</h3>
                <p>{p.body}</p>
                <div className="tags">
                  {p.tags.map((tg, j) => <div className="tag" key={j}>{tg}</div>)}
                </div>
              </div>
            )}
          </div>
        </div>
      </section>

      <section className="section container">
        <div className="sec-head">
          <div>
            <div className="label">{t.home.sec03Label}</div>
            <h2>{t.home.sec03Heading[0]}<em>{t.home.sec03Heading[1]}</em></h2>
          </div>
          <div className="right">
            <p>{t.home.sec03Body}</p>
          </div>
        </div>
        <div className="problems-grid">
          {c.problems.map((p, i) =>
            <div className={"prob " + (i === 0 ? "featured" : "")} key={i}>
              <div className="pix">{p.ix}</div>
              <div className="ptag">{p.tag}</div>
              <h3>{p.title}</h3>
              <p>{p.body}</p>
              {p.cases &&
                <div className="incidents">
                  {p.cases.map((cs, j) =>
                    <div className="incident" key={j}>
                      <div className="when">{cs.date}</div>
                      <div className="where">{cs.loc}</div>
                      <div className="what">{cs.what}</div>
                    </div>
                  )}
                </div>
              }
              {p.ref && <div className="ref">{p.ref}</div>}
            </div>
          )}
        </div>
      </section>

      <section className="section bg-2">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.home.sec04Label}</div>
              <h2>{t.home.sec04Heading[0]}<em>{t.home.sec04Heading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.home.sec04Body}</p>
            </div>
          </div>
          <div className="diagram-section">
            <div className="diagram-art"><BufferDiagram t={t} /></div>
            <div className="diagram-text">
              <h3>{t.home.diagramHeading[0]}<em>{t.home.diagramHeading[1]}</em>{t.home.diagramHeading[2]}<em>{t.home.diagramHeading[3]}</em>{t.home.diagramHeading[4]}</h3>
              <p>{c.diagramCopy.body}</p>
              <div className="steps">
                {c.diagramCopy.legend.map((l, i) =>
                  <div className="step" key={i}>
                    <div className="num">{l.step}</div>
                    <div className="desc">{l.desc}</div>
                    <div className="val">{l.val}</div>
                  </div>
                )}
              </div>
            </div>
          </div>
        </div>
      </section>

      <section className="section bg-2">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.home.sec05Label}</div>
              <h2>{t.home.sec05Heading[0]}<em>{t.home.sec05Heading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.home.sec05Body}</p>
            </div>
          </div>
          <div className="cases-grid">
            {c.cases.map((cs, i) =>
              <div className="case-card" key={i} onClick={() => setPage("cases")}>
                <div className="ph">
                  <PH label={cs.title} />
                  <div className="badge">{cs.badge}</div>
                </div>
                <div className="body">
                  <div className="meta">{cs.meta.map((m, j) => <span key={j}>{m}</span>)}</div>
                  <h3>{cs.title}</h3>
                  <p>{cs.summary}</p>
                </div>
              </div>
            )}
          </div>
        </div>
      </section>

      <section className="section container">
        <div className="sec-head">
          <div>
            <div className="label">{t.home.sec06Label}</div>
            <h2>{t.home.sec06Heading[0]}<em>{t.home.sec06Heading[1]}</em></h2>
          </div>
          <div className="right">
            <p>{t.home.sec06Body}</p>
          </div>
        </div>
        <div className="ins-grid">
          {c.insights.map((ins, i) =>
            <div className="ins" key={i} onClick={() => setPage("insights")}>
              <div className="head">
                <span>{ins.ix}</span>
                <span className="tag">{ins.tag}</span>
              </div>
              <h3>{ins.title}</h3>
              <p>{ins.body}</p>
              <div className="by">— {ins.author} · {ins.date}</div>
            </div>
          )}
        </div>
      </section>

      <section className="section container">
        <div className="cta-block">
          <h2>{t.home.ctaHeading[0]}<em>{t.home.ctaHeading[1]}</em></h2>
          <div className="cta-side">
            <p>{t.home.ctaBody}</p>
            <div className="btn-row">
              <button className="btn primary" onClick={() => setPage("contact")}>{t.cta.startConversation}</button>
              <button className="btn" onClick={() => setPage("about")}>{t.cta.aboutShort}</button>
            </div>
          </div>
        </div>
      </section>
    </div>);
}

// ============ About ============
function AboutPage({ setPage, c, t }) {
  return (
    <div className="page-enter" data-screen-label="About">
      <section className="page-hero container">
        <div className="label">{t.about.label}</div>
        <h1>{t.about.h1[0]}<em>{t.about.h1[1]}</em>{t.about.h1[2]}</h1>
      </section>

      <section className="container">
        <div className="quote-block">
          <p className="q">{t.about.quote}</p>
          <div className="body">
            <p>{t.about.body1}</p>
            <p>{t.about.body2}</p>
          </div>
        </div>
      </section>

      <section className="section bg-2">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.about.valuesLabel}</div>
              <h2>{t.about.valuesHeading[0]}<em>{t.about.valuesHeading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.about.valuesBody}</p>
            </div>
          </div>
          <div className="values">
            {c.values.map((v, i) =>
              <div className="value" key={i}>
                <div className="ix">{v.ix}</div>
                <h3>{v.title} <em>{v.em}</em></h3>
                <p>{v.body}</p>
              </div>
            )}
          </div>
        </div>
      </section>

      <section className="section container">
        <div className="sec-head">
          <div>
            <div className="label">{t.about.historyLabel}</div>
            <h2>{t.about.historyHeading[0]}<em>{t.about.historyHeading[1]}</em></h2>
          </div>
          <div className="right">
            <p>{t.about.historyBody}</p>
          </div>
        </div>
        <div className="timeline">
          {c.timeline.map((tl, i) =>
            <div className="tl-item" key={i}>
              <div className="tl-year">{tl.y}</div>
              <div className="tl-title">{tl.t}</div>
              <div className="tl-desc">{tl.d}</div>
            </div>
          )}
        </div>
      </section>

      <section className="section bg-2">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.about.teamLabel}</div>
              <h2>{t.about.teamHeading[0]}<em>{t.about.teamHeading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.about.teamBody}</p>
            </div>
          </div>
          <div className="team-grid">
            {c.team.map((m, i) => {
              const featured = i === 0;
              return (
                <div className={"team-card " + (featured ? "featured" : "")} key={i}>
                  <div className="team-photo">
                    <img src={m.photo} alt={m.name} loading="lazy" style={{ objectFit: "contain" }} />
                    <div className="frame"></div>
                  </div>
                  <div className="info">
                    <h3>{m.name}</h3>
                    <div className="role">{m.role}</div>
                    <p className="bio">{m.bio}</p>
                    <div className="origin" style={{ color: "rgb(255, 255, 255)" }}>— {m.origin}</div>
                  </div>
                </div>);
            })}
          </div>
        </div>
      </section>

      <section className="section">
        <div className="container">
          <div className="sec-head">
            <div>
              <div className="label">{t.about.partnersLabel}</div>
              <h2>{t.about.partnersHeading[0]}<em>{t.about.partnersHeading[1]}</em></h2>
            </div>
            <div className="right">
              <p>{t.about.partnersBody}</p>
            </div>
          </div>
          <div className="partners">
            {c.partners.map((p, i) =>
              <div className="partner" key={i}>
                <div className="nm">{p.n}</div>
                <span className="role">{p.role}</span>
              </div>
            )}
          </div>
        </div>
      </section>

      <section className="container" style={{ padding: "60px 0" }}>
        <div className="invest-block">
          <div className="container" style={{ padding: 0 }}>
            <div className="sec-head">
              <div>
                <div className="label" style={{ color: "rgba(251,249,244,0.7)" }}>{t.about.investorLabel}</div>
                <h2>{t.about.investorHeading[0]}<em>{t.about.investorHeading[1]}</em></h2>
              </div>
              <div className="right">
                <p>{t.about.investorBody}</p>
              </div>
            </div>
            <div className="invest-grid">
              {c.investor.map((it, i) =>
                <div className="inv" key={i}>
                  <div className="v">{it.v}<sup>{it.unit}</sup></div>
                  <div className="k">{it.k}</div>
                  <div className="note">{it.note}</div>
                </div>
              )}
            </div>
          </div>
        </div>
      </section>
    </div>);
}

// ============ Services ============
function ServicesPage({ setPage, c, t }) {
  return (
    <div className="page-enter" data-screen-label="Services">
      <section className="page-hero container">
        <div className="label">{t.services.label}</div>
        <h1>{t.services.h1[0]}<em>{t.services.h1[1]}</em>{t.services.h1[2]}<em>{t.services.h1[3]}</em></h1>
      </section>

      <section className="container">
        <div className="svc-list">
          {c.services.map((s, i) =>
            <div className="svc" key={i}>
              <div className="sid">0{i + 1}</div>
              <div>
                <h3>{s.title[0]} <em>{s.title[1]}</em></h3>
                <p>{s.body}</p>
                <div className="pills">
                  {s.pills.map((p, j) => <span className="pill" key={j}>{p}</span>)}
                </div>
              </div>
              <div className="svc-stats-c">
                {s.stats.map((st, j) =>
                  <div className="svc-stat" key={j}>
                    <span className="k">{st.k}</span>
                    <span className="v">{st.v}</span>
                  </div>
                )}
              </div>
            </div>
          )}
        </div>
      </section>

      <section className="section container">
        <div className="cta-block">
          <h2>{t.services.ctaHeading[0]}<em>{t.services.ctaHeading[1]}</em></h2>
          <div className="cta-side">
            <p>{t.services.ctaBody}</p>
            <div className="btn-row">
              <button className="btn primary" onClick={() => setPage("contact")}>{t.cta.startConversation}</button>
            </div>
          </div>
        </div>
      </section>
    </div>);
}

// ============ Cases ============
function CasesPage({ setPage, c, t }) {
  return (
    <div className="page-enter" data-screen-label="Cases">
      <section className="page-hero container">
        <div className="label">{t.cases.label}</div>
        <h1>{t.cases.h1[0]}<em>{t.cases.h1[1]}</em></h1>
      </section>

      <div className="container">
        {c.cases.map((cs, i) =>
          <div className="case-full" key={i}>
            <div className="case-img"><PH label={cs.title} /></div>
            <div>
              <div className="badge"><span className="dot"></span>{cs.badge} · {cs.meta.join(' · ')}</div>
              <h2>{cs.title}</h2>
              <p className="summary">{cs.summary}</p>
              <div className="case-stats">
                {cs.stats.map((st, j) =>
                  <div className="item" key={j}>
                    <div className="v">{st.v}</div>
                    <div className="k">{st.k}</div>
                  </div>
                )}
              </div>
            </div>
          </div>
        )}
      </div>

      <section className="section container">
        <div className="cta-block">
          <h2>{t.cases.ctaHeading[0]}<em>{t.cases.ctaHeading[1]}</em></h2>
          <div className="cta-side">
            <p>{t.cases.ctaBody}</p>
            <div className="btn-row">
              <button className="btn primary" onClick={() => setPage("contact")}>{t.cta.walkSite}</button>
            </div>
          </div>
        </div>
      </section>
    </div>);
}

// ============ Insights ============
function InsightsPage({ setPage, c, t }) {
  const featured = c.insights[0];
  const rest = c.insights.slice(1);
  return (
    <div className="page-enter" data-screen-label="Insights">
      <section className="page-hero container">
        <div className="label">{t.insights.label}</div>
        <h1>{t.insights.h1[0]}<em>{t.insights.h1[1]}</em></h1>
      </section>

      <div className="container">
        <div className="ins-feat">
          <div className="ph"><PH label={t.insights.featureImg} /></div>
          <div>
            <div className="top"><span className="tag">{featured.tag}</span>{featured.date}</div>
            <h2>{featured.title}</h2>
            <p>{featured.body}</p>
            <div className="label">— {featured.author}</div>
          </div>
        </div>
        <div className="ins-list">
          {rest.map((ins, i) =>
            <div className="ins-row" key={i}>
              <div className="ix">{ins.ix}</div>
              <h4>{ins.title}</h4>
              <div className="auth">{ins.author}</div>
              <div className="date">{ins.date}</div>
            </div>
          )}
        </div>
      </div>
    </div>);
}

// ============ Contact ============
function ContactPage({ setPage, c, t }) {
  const [topic, setTopic] = useState(t.contact.topics[0]);
  const [form, setForm] = useState({ name: "", org: "", email: "", msg: "" });
  const [submitted, setSubmitted] = useState(false);
  const canSubmit = form.name && form.email.includes("@") && form.msg.length > 4;
  const submit = (e) => { e.preventDefault(); if (canSubmit) setSubmitted(true); };

  // If language changes mid-session and the selected topic no longer matches the language's list, reset.
  useEffect(() => {
    if (!t.contact.topics.includes(topic)) setTopic(t.contact.topics[0]);
  }, [t]);

  return (
    <div className="page-enter" data-screen-label="Contact">
      <section className="page-hero container">
        <div className="label">{t.contact.label}</div>
        <h1>{t.contact.h1[0]}<em>{t.contact.h1[1]}</em></h1>
      </section>

      <div className="container">
        <div className="contact-grid">
          <div className="form-card">
            {!submitted ?
              <form onSubmit={submit}>
                <h2>{t.contact.formHeading}</h2>
                <div className="field">
                  <label>{t.contact.topicLabel}</label>
                  <div className="chips">
                    {t.contact.topics.map((topicOpt) =>
                      <button type="button" key={topicOpt} className={topic === topicOpt ? "active" : ""} onClick={() => setTopic(topicOpt)}>{topicOpt}</button>
                    )}
                  </div>
                </div>
                <div className="field"><label>{t.contact.nameLabel}</label>
                  <input value={form.name} onChange={(e) => setForm({ ...form, name: e.target.value })} placeholder={t.contact.namePh} /></div>
                <div className="field"><label>{t.contact.orgLabel}</label>
                  <input value={form.org} onChange={(e) => setForm({ ...form, org: e.target.value })} placeholder={t.contact.orgPh} /></div>
                <div className="field"><label>{t.contact.emailLabel}</label>
                  <input type="email" value={form.email} onChange={(e) => setForm({ ...form, email: e.target.value })} placeholder={t.contact.emailPh} /></div>
                <div className="field"><label>{t.contact.msgLabel}</label>
                  <textarea value={form.msg} onChange={(e) => setForm({ ...form, msg: e.target.value })} placeholder={t.contact.msgPh} rows="4" /></div>
                <div className="submit-row">
                  <span className="label">{t.contact.replyTime}</span>
                  <button className="submit-btn" disabled={!canSubmit}>{t.contact.sendBtn}</button>
                </div>
              </form> :
              <div className="success">
                <div className="tick">✓</div>
                <h3>{t.contact.thanks} {form.name.split(' ')[0]}.</h3>
                <p>{t.contact.thanksBody} <b>{topic.toLowerCase()}</b> {t.contact.thanksBodyEnd}</p>
              </div>
            }
          </div>
          <div className="contact-side">
            {t.contact.cards.map((card, i) =>
              <div className="contact-card" key={i}>
                <h4>{card.h4}</h4>
                <p>
                  {card.body.split("\n").map((line, j, arr) =>
                    <React.Fragment key={j}>{line}{j < arr.length - 1 && <br />}</React.Fragment>
                  )}
                  {card.email && <><br /><a>{card.email}</a></>}
                </p>
              </div>
            )}
          </div>
        </div>
      </div>
    </div>);
}

// ============ Footer ============
function Footer({ setPage, t }) {
  return (
    <footer data-screen-label="Footer">
      <div className="container">
        <div className="brand-block">
          <img className="flogo" src="assets/logo-colour.png" alt="Siam Nex Up" />
          <h4>{t.footer.role}</h4>
          <div className="nm">Siam Nex Up <span style={{ color: "var(--muted)", fontWeight: 400, fontSize: "16px" }}>{t.footer.coLtd}</span></div>
          <div className="tag">{t.footer.tagline}</div>
          <div style={{ marginTop: "14px", fontSize: "14px", fontWeight: 600, color: "var(--purple)" }}>siamnexup.com</div>
          <div style={{ fontFamily: "'JetBrains Mono', monospace", fontSize: "11px", color: "var(--muted)", letterSpacing: "0.08em", marginTop: "8px" }}>{t.footer.niaCode}</div>
        </div>
        <div className="col">
          <h4>{t.footer.site}</h4>
          <ul>{PAGE_IDS.map((id) => <li key={id}><button onClick={() => setPage(id)}>{t.nav[id]}</button></li>)}</ul>
        </div>
        <div className="col">
          <h4>{t.footer.channels}</h4>
          <ul>
            <li><button onClick={() => setPage("contact")}>partnerships@</button></li>
            <li><button onClick={() => setPage("contact")}>ir@</button></li>
            <li><button onClick={() => setPage("contact")}>engineering@</button></li>
          </ul>
        </div>
        <div className="col">
          <h4>{t.footer.office}</h4>
          <ul>
            {t.footer.officeLines.map((line, i) =>
              <li key={i} style={{ color: "var(--muted)", fontSize: "14px" }}>{line}</li>
            )}
          </ul>
        </div>
      </div>
      <div className="colophon">
        <span>{t.footer.copy}</span>
        <span>{t.footer.direction}</span>
      </div>
    </footer>);
}

// ============ App ============
function App() {
  const [page, setPageRaw] = useState("home");
  const setPage = (p) => { setPageRaw(p); window.scrollTo({ top: 0, behavior: "instant" }); };
  const [tw, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [lang, setLang] = useLang();

  useEffect(() => {
    document.documentElement.style.setProperty("--logo-size", `${tw.logoSize}px`);
  }, [tw.logoSize]);

  const c = lang === "th" ? window.SNU_CONTENT_TH : window.SNU_CONTENT;
  const t = window.SNU_UI[lang] || window.SNU_UI.en;

  let el;
  switch (page) {
    case "about": el = <AboutPage setPage={setPage} c={c} t={t} />; break;
    case "services": el = <ServicesPage setPage={setPage} c={c} t={t} />; break;
    case "cases": el = <CasesPage setPage={setPage} c={c} t={t} />; break;
    case "insights": el = <InsightsPage setPage={setPage} c={c} t={t} />; break;
    case "contact": el = <ContactPage setPage={setPage} c={c} t={t} />; break;
    default: el = <HomePage setPage={setPage} c={c} t={t} />;
  }

  return (
    <>
      <Nav page={page} setPage={setPage} t={t} lang={lang} setLang={setLang} />
      <main key={page + "-" + lang}>{el}</main>
      <Footer setPage={setPage} t={t} />
      <TweaksPanel>
        <TweakSection title="Brand">
          <TweakSlider
            label="Logo size"
            value={tw.logoSize}
            onChange={(v) => setTweak("logoSize", v)}
            min={32}
            max={120}
            step={1}
            suffix="px" />
        </TweakSection>
      </TweaksPanel>
    </>);
}

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