/* ---- SVG ICONS ---- */
const Icon = ({ n, s = 20 }) => {
  const d = {
    doc: <><rect x="4" y="2" width="16" height="20" rx="2" fill="none" stroke="currentColor" strokeWidth="1.5" /><line x1="8" y1="8" x2="16" y2="8" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="8" y1="12" x2="16" y2="12" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="8" y1="16" x2="12" y2="16" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /></>,
    pos: <><rect x="2" y="5" width="20" height="14" rx="2" fill="none" stroke="currentColor" strokeWidth="1.5" /><line x1="2" y1="10" x2="22" y2="10" stroke="currentColor" strokeWidth="1.5" /><line x1="6" y1="15" x2="9" y2="15" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /></>,
    chart: <><rect x="2" y="12" width="4" height="9" rx="1" fill="currentColor" opacity=".6" /><rect x="9" y="7" width="4" height="14" rx="1" fill="currentColor" opacity=".8" /><rect x="16" y="3" width="4" height="18" rx="1" fill="currentColor" /></>,
    ai: <><rect x="7" y="7" width="10" height="10" rx="1" fill="none" stroke="currentColor" strokeWidth="1.5" /><line x1="4" y1="9.5" x2="7" y2="9.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="4" y1="12" x2="7" y2="12" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="4" y1="14.5" x2="7" y2="14.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="17" y1="9.5" x2="20" y2="9.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="17" y1="12" x2="20" y2="12" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="17" y1="14.5" x2="20" y2="14.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="10" y1="4" x2="10" y2="7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /><line x1="14" y1="4" x2="14" y2="7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /></>,
    warehouse: <><path d="M3 9.5L12 3l9 6.5V20a1 1 0 01-1 1H4a1 1 0 01-1-1V9.5z" fill="none" stroke="currentColor" strokeWidth="1.5" /><rect x="9" y="12" width="6" height="9" fill="none" stroke="currentColor" strokeWidth="1.5" /></>,
    calc: <><rect x="4" y="2" width="16" height="20" rx="2" fill="none" stroke="currentColor" strokeWidth="1.5" /><rect x="7" y="6" width="10" height="4" rx="1" fill="none" stroke="currentColor" strokeWidth="1.5" /><circle cx="8.5" cy="15" r="1" fill="currentColor" /><circle cx="12" cy="15" r="1" fill="currentColor" /><circle cx="15.5" cy="15" r="1" fill="currentColor" /><circle cx="8.5" cy="19" r="1" fill="currentColor" /><circle cx="12" cy="19" r="1" fill="currentColor" /></>,
    shield: <><path d="M12 2l8 3v7c0 5-3.5 9.7-8 11-4.5-1.3-8-6-8-11V5l8-3z" fill="none" stroke="currentColor" strokeWidth="1.5" /><polyline points="9,12 11,14 15,10" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" /></>,
    truck: <><rect x="1" y="8" width="14" height="10" rx="1" fill="none" stroke="currentColor" strokeWidth="1.5" /><polyline points="15,11 15,4 21,4 23,8 23,18 15,18" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" /><circle cx="5.5" cy="18.5" r="1.5" fill="none" stroke="currentColor" strokeWidth="1.5" /><circle cx="18.5" cy="18.5" r="1.5" fill="none" stroke="currentColor" strokeWidth="1.5" /></>,
    check: <polyline points="20,6 9,17 4,12" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" />,
    mail: <><rect x="2" y="4" width="20" height="16" rx="2" fill="none" stroke="currentColor" strokeWidth="1.5" /><polyline points="2,6 12,13 22,6" fill="none" stroke="currentColor" strokeWidth="1.5" /></>,
    pin: <><path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0118 0z" fill="none" stroke="currentColor" strokeWidth="1.5" /><circle cx="12" cy="10" r="3" fill="none" stroke="currentColor" strokeWidth="1.5" /></>,
    clock: <><circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" strokeWidth="1.5" /><polyline points="12,6 12,12 16,14" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" /></>,
    globe: <><circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" strokeWidth="1.5" /><path d="M2 12h20M12 2a15.3 15.3 0 010 20M12 2a15.3 15.3 0 000 20" fill="none" stroke="currentColor" strokeWidth="1.5" /></>,
    close: <><line x1="18" y1="6" x2="6" y2="18" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /><line x1="6" y1="6" x2="18" y2="18" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /></>,
    arrow: <><line x1="5" y1="12" x2="19" y2="12" stroke="currentColor" strokeWidth="2" strokeLinecap="round" /><polyline points="12,5 19,12 12,19" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
    </>
  };
  return (
    <svg width={s} height={s} viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
      {d[n]}
    </svg>);

};

/* ---- NAV ---- */
const Nav = ({ lang, setLang, t, scrolled }) => {
  const links = [
  { label: t.nav.home, href: '#inicio' },
  { label: t.nav.about, href: '#nosotros' },
  { label: t.nav.services, href: '#servicios' },
  { label: t.nav.plans, href: '#planes' },
  { label: t.nav.contact, href: '#contacto' }];

  return (
    <nav className={`nav ${scrolled ? 'solid' : ''}`}>
      <div className="nav-inner">
        <a href="#inicio" className="nav-logo">
          <img src="uploads/txs-logo-cropped.png" alt="TileXpertSoftware" style={{ height: "95px", objectFit: "contain", width: "130px" }} />
        </a>
        <div className="nav-center">
          {links.map((l) =>
          <a key={l.href} href={l.href} className="nav-link">{l.label}</a>
          )}
        </div>
        <div className="nav-right">
          <div className="lang-sw">
            {['es', 'en', 'pt'].map((code) =>
            <button key={code} className={`lang-btn ${lang === code ? 'active' : ''}`} onClick={() => setLang(code)}>
                {code.toUpperCase()}
              </button>
            )}
          </div>
          <a href="#contacto" className="btn btn-out-nav btn-sm">{t.nav.cta}</a>
        </div>
      </div>
    </nav>);

};

/* ---- HERO ---- */
const Hero = ({ t }) =>
<section className="hero" id="inicio">
    <div className="hero-content fade-up">
      <img src="uploads/txs-logo-cropped.png" alt="TXS" className="hero-logo" />
      <div className="hero-badge">{t.hero.badge}</div>
      <h1 className="hero-title">
        {t.hero.t1}<br /><em>{t.hero.t2}</em>
      </h1>
      <p className="hero-sub">{t.hero.sub}</p>
      <div className="hero-ctas">
        <a href="#contacto" className="btn btn-orange btn-lg">{t.hero.cta1} <Icon n="arrow" s={18} /></a>
        <a href="#servicios" className="btn btn-out-white btn-lg">{t.hero.cta2}</a>
      </div>
      <div className="hero-stats">
        {[
      { n: t.hero.s1n, l: t.hero.s1l },
      { n: t.hero.s2n, l: t.hero.s2l },
      { n: t.hero.s3n, l: t.hero.s3l },
      { n: t.hero.s4n, l: t.hero.s4l }].
      map((s, i) =>
      <div key={i} className="hero-stat">
            <div className="hero-stat-num"><b>{s.n}</b></div>
            <div className="hero-stat-label">{s.l}</div>
          </div>
      )}
      </div>
    </div>
  </section>;


/* ---- ABOUT ---- */
const About = ({ t }) =>
<section className="about section" id="nosotros">
    <div className="container">
      <div className="about-grid">
        <div className="about-text">
          <div className="tag">{t.about.tag}</div>
          <h2 className="sec-title">{t.about.title}</h2>
          <p>{t.about.p1}</p>
          <p style={{ marginTop: '12px' }}>{t.about.p2}</p>
          <p style={{ marginTop: '12px' }}>{t.about.p3}</p>
        </div>
        <div className="about-feats">
          {[
        { icon: 'chart', title: t.about.f1t, desc: t.about.f1d },
        { icon: 'ai', title: t.about.f2t, desc: t.about.f2d },
        { icon: 'clock', title: t.about.f3t, desc: t.about.f3d }].
        map((f, i) =>
        <div key={i} className="about-feat">
              <div className="af-icon"><Icon n={f.icon} s={22} /></div>
              <div className="af-text">
                <h4>{f.title}</h4>
                <p>{f.desc}</p>
              </div>
            </div>
        )}
        </div>
      </div>
    </div>
  </section>;


/* ---- SERVICES ---- */
const Services = ({ t }) =>
<section className="services section" id="servicios">
    <div className="container">
      <div className="sec-head center">
        <div className="tag">{t.services.tag}</div>
        <h2 className="sec-title">{t.services.title}</h2>
        <p className="sec-desc">{t.services.sub}</p>
      </div>
      <div className="srv-grid">
        {t.services.items.map((item, i) =>
      <div key={i} className="srv-card">
            <div className="srv-icon"><Icon n={item.icon} s={24} /></div>
            <h3>{item.t}</h3>
            <p>{item.d}</p>
          </div>
      )}
      </div>
    </div>
  </section>;


/* ---- SCREENSHOTS ---- */
const Screenshots = ({ t }) => {
  const [active, setActive] = React.useState(0);
  const imgs = [
  'uploads/Captura Dashboard TXS.jpg',
  'uploads/Captura Pedido TXS.jpg',
  'uploads/Captura Calculadora TXS.jpg',
  'uploads/Captura Gest usuarios TXS.jpg'];

  return (
    <section className="screenshots section" id="capturas">
      <div className="container">
        <div className="sec-head center">
          <div className="tag">{t.screenshots.tag}</div>
          <h2 className="sec-title">{t.screenshots.title}</h2>
          <p className="sec-desc">{t.screenshots.sub}</p>
        </div>
        <div className="sc-tabs">
          {t.screenshots.tabs.map((tab, i) =>
          <button key={i} className={`sc-tab ${active === i ? 'active' : ''}`} onClick={() => setActive(i)}>
              {tab}
            </button>
          )}
        </div>
        <div className="browser">
          <div className="browser-bar">
            <div className="browser-dots">
              <div className="browser-dot dot-r"></div>
              <div className="browser-dot dot-y"></div>
              <div className="browser-dot dot-g"></div>
            </div>
            <div className="browser-url">
              <Icon n="globe" s={12} />
              app.tilexpertsoftware.com
            </div>
          </div>
          <div style={{ overflow: 'hidden', background: '#f3f4f6' }}>
            <img
              key={active}
              src={imgs[active]}
              alt={t.screenshots.tabs[active]}
              className="browser-img"
              style={{ animation: 'fadeUp 0.35s ease both' }} />
            
          </div>
        </div>
      </div>
    </section>);

};

/* ---- TESTIMONIALS ---- */
const Testimonials = ({ t }) =>
<section className="testimonials section" id="testimonios">
    <div className="container">
      <div className="sec-head center">
        <div className="tag">{t.testimonials.tag}</div>
        <h2 className="sec-title">{t.testimonials.title}</h2>
      </div>
      <div className="test-grid">
        {t.testimonials.items.map((item, i) =>
      <div key={i} className="test-card">
            <div className="test-stars">{'★★★★★'.split('').map((s, j) => <span key={j}>{s}</span>)}</div>
            <p className="test-quote">{item.text}</p>
            <div className="test-author">
              <div className="test-avatar">{item.avatar}</div>
              <div>
                <div className="test-name">{item.name}</div>
                <div className="test-role">{item.role} · {item.company}</div>
              </div>
            </div>
          </div>
      )}
      </div>
    </div>
  </section>;


/* ---- PLANS ---- */
const Plans = ({ t }) => {
  const badgeClass = { rec: 'badge-rec', lim: 'badge-lim', cus: 'badge-cus' };
  return (
    <section className="plans section" id="planes">
      <div className="container">
        <div className="sec-head center">
          <div className="tag">{t.plans.tag}</div>
          <h2 className="sec-title">{t.plans.title}</h2>
          <p className="sec-desc">{t.plans.sub}</p>
        </div>
        <div className="plans-grid">
          {t.plans.items.map((plan, i) =>
          <div key={i} className={`plan-card ${plan.featured ? 'featured' : ''}`}>
              {plan.badge ?
            <div className={`plan-badge ${badgeClass[plan.btype] || ''}`}>{plan.badge}</div> :
            <span className="plan-badge-ph"></span>
            }
              <div className="plan-name">{plan.name}</div>
              <div className="plan-users"><Icon n="check" s={12} />{plan.users}</div>
              <p className="plan-desc">{plan.desc}</p>
              <div className="plan-high">
                {plan.high.map((h, j) =>
              <div key={j} className="plan-high-item">
                    <div className="ph-check"><Icon n="check" s={10} /></div>
                    {h}
                  </div>
              )}
              </div>
              <div className="plan-feats">
                {plan.feats.map((f, j) =>
              <div key={j} className="plan-feat">
                    <div className="pf-dot"></div>{f}
                  </div>
              )}
              </div>
              <a href="#contacto"
            className={`btn plan-btn ${plan.featured ? 'btn-orange' : plan.btype === 'rec' ? 'btn-blue' : 'btn-out-blue'}`}>
                {t.plans.cta}
              </a>
            </div>
          )}
        </div>
      </div>
    </section>);

};

/* ---- CONTACT ---- */
const Contact = ({ t }) => {
  const [form, setForm] = React.useState({ name: '', company: '', email: '', phone: '', plan: '', message: '' });
  const [sent, setSent] = React.useState(false);
  const set = (k, v) => setForm((p) => ({ ...p, [k]: v }));
  const submit = (e) => {
    e.preventDefault();
    setSent(true);
  };
  return (
    <section className="contact section" id="contacto">
      <div className="container">
        <div className="sec-head">
          <div className="tag">{t.contact.tag}</div>
          <h2 className="sec-title">{t.contact.title}</h2>
          <p className="sec-desc">{t.contact.sub}</p>
        </div>
        <div className="contact-grid">
          <div className="contact-form-wrap">
            {sent ?
            <div className="form-ok">
                <div className="form-ok-icon"><Icon n="check" s={28} /></div>
                <h3>{t.contact.ok_title}</h3>
                <p>{t.contact.ok_text}</p>
              </div> :

            <form onSubmit={submit}>
                <div className="form-row">
                  <div className="form-grp">
                    <label className="form-lbl">{t.contact.f_name} *</label>
                    <input className="form-inp" required value={form.name} onChange={(e) => set('name', e.target.value)} placeholder={t.contact.f_name} />
                  </div>
                  <div className="form-grp">
                    <label className="form-lbl">{t.contact.f_company} *</label>
                    <input className="form-inp" required value={form.company} onChange={(e) => set('company', e.target.value)} placeholder={t.contact.f_company} />
                  </div>
                </div>
                <div className="form-row">
                  <div className="form-grp">
                    <label className="form-lbl">{t.contact.f_email} *</label>
                    <input className="form-inp" type="email" required value={form.email} onChange={(e) => set('email', e.target.value)} placeholder="hola@empresa.com" />
                  </div>
                  <div className="form-grp">
                    <label className="form-lbl">{t.contact.f_phone}</label>
                    <input className="form-inp" value={form.phone} onChange={(e) => set('phone', e.target.value)} placeholder="+34 600 000 000" />
                  </div>
                </div>
                <div className="form-grp">
                  <label className="form-lbl">{t.contact.f_plan}</label>
                  <select className="form-inp" value={form.plan} onChange={(e) => set('plan', e.target.value)}>
                    {t.contact.f_planOpts.map((opt, i) => <option key={i} value={opt}>{opt}</option>)}
                  </select>
                </div>
                <div className="form-grp">
                  <label className="form-lbl">{t.contact.f_msg}</label>
                  <textarea className="form-inp" rows="4" value={form.message} onChange={(e) => set('message', e.target.value)} placeholder="Cuéntanos tu caso..."></textarea>
                </div>
                <button type="submit" className="btn btn-orange btn-lg form-submit">
                  {t.contact.f_send} <Icon n="arrow" s={18} />
                </button>
              </form>
            }
          </div>
          <div className="contact-info">
            <div className="ci-title">{t.contact.ci_title}</div>
            {[
            { icon: 'mail', label: t.contact.ci_emailL, val: t.contact.ci_email },
            { icon: 'pin', label: t.contact.ci_locL, val: t.contact.ci_loc },
            { icon: 'clock', label: t.contact.ci_schL, val: t.contact.ci_sch }].
            map((item, i) =>
            <div key={i} className="ci-item">
                <div className="ci-icon"><Icon n={item.icon} s={20} /></div>
                <div className="ci-text">
                  <strong>{item.label}</strong>
                  <span>{item.val}</span>
                </div>
              </div>
            )}
          </div>
        </div>
      </div>
    </section>);

};

/* ---- FOOTER ---- */
const Footer = ({ t, onLegal }) =>
<footer className="footer">
    <div className="container">
      <div className="footer-grid">
        <div className="footer-brand">
          <img src="uploads/txs-logo-cropped.png" alt="TXS" className="footer-logo" />
          <p>{t.footer.tagline}</p>
        </div>
        <div>
          <div className="footer-col-ttl">{t.footer.navTitle}</div>
          <div className="footer-links">
            {t.footer.navLinks.map((lnk, i) =>
          <a key={i} href={t.footer.navHrefs[i]} className="footer-lnk">{lnk}</a>
          )}
          </div>
        </div>
        <div>
          <div className="footer-col-ttl">{t.footer.legalTitle}</div>
          <div className="footer-links">
            {['aviso', 'privacidad', 'cookies', 'terminos'].map((key, i) =>
          <span key={i} className="footer-lnk" onClick={() => onLegal(key)}>{t.footer.legalLinks[i]}</span>
          )}
          </div>
        </div>
      </div>
      <div className="footer-bottom">
        <span className="footer-copy">{t.footer.copy}</span>
        <div className="footer-leg">
          {['aviso', 'privacidad', 'cookies', 'terminos'].map((key, i) =>
        <span key={i} className="footer-leg-lnk" onClick={() => onLegal(key)}>{t.footer.legalLinks[i]}</span>
        )}
        </div>
      </div>
    </div>
  </footer>;


/* ---- LEGAL MODAL ---- */
const LegalModal = ({ type, t, onClose }) => {
  const content = t.legal[type];
  if (!content) return null;
  return (
    <div className="modal-ov" onClick={(e) => {if (e.target === e.currentTarget) onClose();}}>
      <div className="modal-box">
        <div className="modal-hd">
          <h2>{content.title}</h2>
          <button className="modal-x" onClick={onClose}><Icon n="close" s={16} /></button>
        </div>
        <div className="modal-bd">
          {content.body.map((sec, i) =>
          <div key={i}>
              <h3>{sec.h}</h3>
              <p>{sec.p}</p>
            </div>
          )}
        </div>
      </div>
    </div>);

};

/* ---- APP ---- */
const App = () => {
  const [lang, setLang] = React.useState('es');
  const [scrolled, setScrolled] = React.useState(false);
  const [modal, setModal] = React.useState(null);

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 50);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const t = window.T[lang];

  return (
    <div>
      <Nav lang={lang} setLang={setLang} t={t} scrolled={scrolled} />
      <Hero t={t} />
      <About t={t} />
      <Services t={t} />
      <Screenshots t={t} />
      <Testimonials t={t} />
      <Plans t={t} />
      <Contact t={t} />
      <Footer t={t} onLegal={setModal} />
      {modal && <LegalModal type={modal} t={t} onClose={() => setModal(null)} />}
    </div>);

};

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