/* ─────────────────────────────────────────────────────────────────
   CorpSol — home page sections (part 3)
   Industries · Security · FAQ · FinalCTA
   ───────────────────────────────────────────────────────────────── */

// ── INDUSTRIES ─────────────────────────────────────────────────────────────
const INDUSTRY_ICONS = {
  1: <g><rect x="6" y="14" width="20" height="14" rx="1.5"/><path d="M6 14L16 6L26 14"/><path d="M10 22h12M10 26h12"/></g>,
  2: <g><path d="M5 8h22l-2 14H7L5 8z"/><path d="M5 8L4 4H1"/><circle cx="11" cy="27" r="2"/><circle cx="21" cy="27" r="2"/></g>,
  3: <g><rect x="3" y="10" width="18" height="12" rx="1.5"/><path d="M21 14h5l3 4v4h-8"/><circle cx="9" cy="24" r="2"/><circle cx="23" cy="24" r="2"/></g>,
  4: <g><path d="M16 5v22M5 16h22"/><rect x="10" y="10" width="12" height="12" rx="1.5"/></g>,
  5: <g><path d="M16 6L4 12l12 6 12-6-12-6z"/><path d="M8 14v6c0 2 4 4 8 4s8-2 8-4v-6"/></g>,
  6: <g><circle cx="10" cy="11" r="3"/><circle cx="22" cy="11" r="3"/><path d="M4 25c0-3 3-6 6-6s6 3 6 6M16 25c0-3 3-6 6-6s6 3 6 6"/></g>,
  7: <g><path d="M6 12h20v14H6z"/><path d="M10 12V8h12v4M12 18h8M12 22h8"/></g>,
  8: <g><path d="M6 28V14L16 6l10 8v14"/><path d="M12 28v-8h8v8"/></g>,
  9: <g><path d="M4 20l3-8h18l3 8M4 20v6h4M28 20v6h-4"/><circle cx="9" cy="22" r="2"/><circle cx="23" cy="22" r="2"/></g>,
  10: <g><path d="M16 4L4 10v8c0 7 6 12 12 14 6-2 12-7 12-14v-8L16 4z"/><path d="M11 16l4 4 7-8"/></g>,
};

function IndustriesSection() {
  const t = useT();
  return (
    <section className="cs-section">
      <div className="cs-container cs-container--wide">
        <div className="cs-sec-head cs-reveal">
          <span className="cs-eyebrow">{t('ind.eyebrow')}</span>
          <h2 className="cs-h2">{t('ind.h2')}</h2>
        </div>
        <div className="cs-ind-grid">
          {[1,2,3,4,5,6,7,8,9,10].map((i) => (
            <div key={i} className="cs-ind-tile cs-reveal">
              <svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
                {INDUSTRY_ICONS[i]}
              </svg>
              <span>{t(`ind.${i}`)}</span>
            </div>
          ))}
        </div>
      </div>
      <style>{`
        .cs-ind-grid {
          display: grid; gap: 12px;
          grid-template-columns: repeat(5, 1fr);
        }
        @media (max-width: 1000px) { .cs-ind-grid { grid-template-columns: repeat(3, 1fr); } }
        @media (max-width: 600px)  { .cs-ind-grid { grid-template-columns: repeat(2, 1fr); } }
        .cs-ind-tile {
          padding: 24px 22px;
          background: var(--c-bg-soft);
          border-radius: var(--r-md);
          display: flex; flex-direction: column; gap: 12px;
          color: var(--c-ink);
          font-family: var(--font-display);
          font-weight: 600; font-size: 15px;
          letter-spacing: -0.015em;
          transition: transform 0.2s var(--ease), background 0.2s var(--ease), color 0.2s var(--ease);
          cursor: default;
          min-height: 120px;
          justify-content: space-between;
        }
        [data-radius="sharp"] .cs-ind-tile { border-radius: 4px; }
        [data-radius="pill"] .cs-ind-tile { border-radius: 20px; }
        .cs-ind-tile:hover { transform: translateY(-3px); background: var(--c-ink); color: var(--c-bg); }
        .cs-ind-tile svg { color: var(--c-accent); }
        .cs-ind-tile:hover svg { color: var(--c-bg); }
      `}</style>
    </section>
  );
}

// ── SECURITY ───────────────────────────────────────────────────────────────
function SecuritySection() {
  const t = useT();
  const items = [
    { n: '01', t: t('sec.1.t'), d: t('sec.1.d'), icon: (
      <g><rect x="6" y="14" width="24" height="18" rx="3"/><path d="M11 14V9a7 7 0 0114 0v5"/><circle cx="18" cy="22" r="2.5" fill="currentColor"/></g>
    ) },
    { n: '02', t: t('sec.2.t'), d: t('sec.2.d'), icon: (
      <g><rect x="5" y="8" width="26" height="20" rx="2"/><path d="M11 28v4M25 28v4M9 32h18M14 14h8M14 18h6M14 22h10"/></g>
    ) },
    { n: '03', t: t('sec.3.t'), d: t('sec.3.d'), icon: (
      <g><path d="M6 6h16l8 8v14a2 2 0 01-2 2H6V6z"/><path d="M22 6v8h8M11 22h14M11 26h10"/></g>
    ) },
  ];
  return (
    <section className="cs-section" style={{ background: 'var(--c-bg-soft)' }}>
      <div className="cs-container cs-container--wide">
        <div className="cs-sec-head cs-reveal">
          <span className="cs-eyebrow">{t('sec.eyebrow')}</span>
          <h2 className="cs-h2">{t('sec.h2')}</h2>
        </div>
        <div className="cs-grid cs-grid--3">
          {items.map((it, i) => (
            <div key={i} className="cs-card cs-sec-card cs-reveal">
              <div className="cs-sec-head-row">
                <span className="cs-sec-num">{it.n}</span>
                <svg width="36" height="36" viewBox="0 0 36 36" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
                  {it.icon}
                </svg>
              </div>
              <h3 className="cs-sec-t">{it.t}</h3>
              <p className="cs-mute" style={{ fontSize: 14.5 }}>{it.d}</p>
            </div>
          ))}
        </div>
      </div>
      <style>{`
        .cs-sec-card { padding: 36px 32px; display: flex; flex-direction: column; gap: 18px; min-height: 280px; background: var(--c-bg); }
        .cs-sec-head-row { display: flex; align-items: center; justify-content: space-between; }
        .cs-sec-num { font-family: var(--font-display); font-size: 13px; font-weight: 600; color: var(--c-faint); letter-spacing: 0.06em; }
        .cs-sec-head-row svg { color: var(--c-accent); }
        .cs-sec-t { font-family: var(--font-display); font-size: 22px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.15; }
      `}</style>
    </section>
  );
}

// ── FAQ — accordion ────────────────────────────────────────────────────────
function FaqSection() {
  const t = useT();
  const [open, setOpen] = React.useState(-1);
  const items = [1,2,3,4,5,6,7,8,9,10].map((i) => ({
    q: t(`faq.${i}.q`), a: t(`faq.${i}.a`),
  }));
  return (
    <section className="cs-section" id="faq">
      <div className="cs-container">
        <div className="cs-faq-grid">
          <div className="cs-faq-side cs-reveal">
            <span className="cs-eyebrow">{t('faq.eyebrow')}</span>
            <h2 className="cs-h2" style={{ marginTop: 16 }}>{t('faq.h2')}</h2>
          </div>
          <div className="cs-faq-list">
            {items.map((it, i) => {
              const isOpen = open === i;
              return (
                <div key={i} className={'cs-faq-item' + (isOpen ? ' is-open' : '')} itemScope itemType="https://schema.org/Question">
                  <button
                    className="cs-faq-q"
                    aria-expanded={isOpen}
                    onClick={() => setOpen(isOpen ? -1 : i)}
                    itemProp="name"
                  >
                    <span>{it.q}</span>
                    <span className="cs-faq-icon" aria-hidden="true">
                      <svg width="16" height="16" viewBox="0 0 16 16"><path d="M3 5l5 5 5-5" stroke="currentColor" strokeWidth="1.8" fill="none" strokeLinecap="round" strokeLinejoin="round" /></svg>
                    </span>
                  </button>
                  <div className="cs-faq-a" itemScope itemType="https://schema.org/Answer">
                    <div className="cs-faq-a-inner" itemProp="text">{it.a}</div>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </div>
      <style>{`
        .cs-faq-grid { display: grid; grid-template-columns: 1fr 1.6fr; gap: 80px; align-items: flex-start; }
        @media (max-width: 900px) { .cs-faq-grid { grid-template-columns: 1fr; gap: 24px; } }
        .cs-faq-side { position: sticky; top: 100px; }
        @media (max-width: 900px) { .cs-faq-side { position: static; } }
        .cs-faq-list { display: flex; flex-direction: column; }
        .cs-faq-item { border-bottom: 1px solid var(--c-line); }
        .cs-faq-item:first-child { border-top: 1px solid var(--c-line); }
        .cs-faq-q {
          width: 100%; text-align: left;
          padding: 24px 0;
          display: flex; align-items: center; justify-content: space-between; gap: 24px;
          background: transparent; border: 0; cursor: pointer;
          font-family: var(--font-display);
          font-size: clamp(17px, 1.5vw, 20px);
          font-weight: 600; letter-spacing: -0.015em;
          color: var(--c-ink);
        }
        .cs-faq-q:hover { color: var(--c-accent); }
        .cs-faq-icon {
          flex-shrink: 0;
          width: 38px; height: 38px; border-radius: 999px;
          background: var(--c-bg-soft); color: var(--c-mute-2);
          display: inline-flex; align-items: center; justify-content: center;
          transition: transform 0.25s var(--ease), background 0.2s var(--ease), color 0.2s var(--ease);
        }
        .cs-faq-item.is-open .cs-faq-icon { transform: rotate(180deg); background: var(--c-accent); color: #fff; }
        .cs-faq-a {
          max-height: 0;
          overflow: hidden;
          transition: max-height 0.35s var(--ease);
        }
        .cs-faq-item.is-open .cs-faq-a { max-height: 600px; }
        .cs-faq-a-inner { color: var(--c-mute-2); font-size: 15.5px; line-height: 1.55; padding-bottom: 24px; max-width: 70ch; }
      `}</style>
    </section>
  );
}

// ── FINAL CTA — inline 2-step form ─────────────────────────────────────────
function FinalCTA() {
  const t = useT();
  const [step, setStep] = React.useState(1);
  const [form, setForm] = React.useState({ name: '', phone: '', company: '', sphere: '', services: [] });
  const [errors, setErrors] = React.useState({});
  const [done, setDone] = React.useState(false);

  const setField = (k, v) => setForm((f) => ({ ...f, [k]: v }));
  const toggleSvc = (s) => setForm((f) => ({
    ...f, services: f.services.includes(s) ? f.services.filter((x) => x !== s) : [...f.services, s],
  }));

  const validate1 = () => {
    const e = {};
    if (!form.name.trim()) e.name = t('err.required');
    const phoneClean = form.phone.replace(/[\s()\-+]/g, '');
    if (!form.phone.trim()) e.phone = t('err.required');
    else if (phoneClean.length < 10 || !/^\d+$/.test(phoneClean)) e.phone = t('err.phone');
    if (!form.company.trim()) e.company = t('err.required');
    setErrors(e);
    return Object.keys(e).length === 0;
  };
  const submit = (e) => {
    e.preventDefault();
    if (step === 1) { if (validate1()) setStep(2); return; }
    setDone(true);
  };

  const svcOptions = [
    { k: 'lead', label: t('cta.svc.lead') },
    { k: 'call', label: t('cta.svc.call') },
    { k: 'custdev', label: t('cta.svc.custdev') },
    { k: 'ai', label: t('cta.svc.ai') },
    { k: 'other', label: t('cta.svc.other') },
  ];

  return (
    <section className="cs-section cs-final" id="cta">
      <div className="cs-container cs-container--wide">
        <div className="cs-final-grid">
          <div className="cs-reveal">
            <span className="cs-eyebrow" style={{ color: 'rgba(255,255,255,0.65)' }}>{t('cta.eyebrow')}</span>
            <h2 className="cs-h2 cs-final-h">{t('cta.h2')}</h2>
            <p className="cs-final-sub">{t('cta.sub')}</p>

            <div className="cs-final-alts">
              <p className="cs-final-alt-h">{t('cta.alt')}</p>
              <div className="cs-final-alt-row">
                <a className="cs-final-alt" href="https://t.me/iliyaszhakipbekov" target="_blank" rel="noopener">
                  <span className="cs-final-alt-i">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M21.5 4.5L2.5 12.3c-.7.3-.7.7-.1.9l4.9 1.5 1.9 5.9c.2.6.6.6.9.3l2.7-2.5 5.4 4c1 .6 1.7.3 1.9-.9l3.4-15.9c.3-1.4-.5-2-1.4-1.6z"/></svg>
                  </span>
                  Telegram
                </a>
                <a className="cs-final-alt" href="https://wa.me/77085100833" target="_blank" rel="noopener">
                  <span className="cs-final-alt-i">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C6.5 2 2 6.5 2 12c0 1.8.5 3.5 1.3 5L2 22l5.2-1.3c1.4.8 3.1 1.3 4.8 1.3 5.5 0 10-4.5 10-10S17.5 2 12 2zm5.7 14.2c-.2.6-1.4 1.2-1.9 1.3-.5.1-1.1.1-1.8-.1-.4-.1-1-.3-1.7-.6-3-1.3-5-4.4-5.1-4.6-.2-.2-1.2-1.6-1.2-3 0-1.5.7-2.2 1-2.5.2-.3.5-.4.7-.4h.5c.2 0 .4 0 .6.5l.8 2.1c.1.2.1.4-.1.6-.1.2-.2.3-.4.5l-.3.3c-.1.1-.2.3-.1.5.1.2.6 1 1.3 1.7.9.8 1.7 1.1 1.9 1.2.2.1.4.1.5-.1.1-.2.6-.7.7-.9.1-.2.3-.2.5-.1.2.1 1.3.6 1.6.7.3.1.4.2.5.3.1.2.1.6-.1 1.2z"/></svg>
                  </span>
                  WhatsApp
                </a>
                <a className="cs-final-alt" href="tel:+77085100833">
                  <span className="cs-final-alt-i">
                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.13.96.36 1.9.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.91.34 1.85.57 2.81.7A2 2 0 0122 16.92z"/></svg>
                  </span>
                  +7 (708) 510-08-33
                </a>
              </div>
            </div>
          </div>

          <div className="cs-final-form-wrap cs-reveal">
            {done ? (
              <div className="cs-final-thanks">
                <div className="cs-cp-check">
                  <svg width="56" height="56" viewBox="0 0 48 48"><circle cx="24" cy="24" r="22" fill="var(--c-accent)" /><path d="M14 24l7 7 13-14" stroke="#fff" strokeWidth="3" fill="none" strokeLinecap="round" strokeLinejoin="round"/></svg>
                </div>
                <h3 className="cs-h3" style={{ marginTop: 20 }}>{t('cta.thanks.t')}</h3>
                <p className="cs-mute" style={{ marginTop: 12 }}>{t('cta.thanks.d')}</p>
              </div>
            ) : (
              <form className="cs-final-form" onSubmit={submit}>
                <div className="cs-cp-progress">
                  <div className={'cs-cp-step ' + (step >= 1 ? 'is-on' : '')}>1</div>
                  <div className="cs-cp-bar"><div style={{ width: step === 2 ? '100%' : '0%' }} /></div>
                  <div className={'cs-cp-step ' + (step >= 2 ? 'is-on' : '')}>2</div>
                </div>
                {step === 1 && (
                  <>
                    <div>
                      <label className="cs-label">{t('cta.field.name')} *</label>
                      <input className={'cs-input' + (errors.name ? ' cs-input--error' : '')}
                             value={form.name} onChange={(e) => setField('name', e.target.value)} placeholder="Айгерим" />
                      {errors.name && <div className="cs-help-error">{errors.name}</div>}
                    </div>
                    <div className="cs-final-row2">
                      <div>
                        <label className="cs-label">{t('cta.field.phone')} *</label>
                        <input className={'cs-input' + (errors.phone ? ' cs-input--error' : '')}
                               value={form.phone} onChange={(e) => setField('phone', e.target.value)} placeholder="+7 (___) ___-__-__" />
                        {errors.phone && <div className="cs-help-error">{errors.phone}</div>}
                      </div>
                      <div>
                        <label className="cs-label">{t('cta.field.company')} *</label>
                        <input className={'cs-input' + (errors.company ? ' cs-input--error' : '')}
                               value={form.company} onChange={(e) => setField('company', e.target.value)} placeholder="Astana Tech LLP" />
                        {errors.company && <div className="cs-help-error">{errors.company}</div>}
                      </div>
                    </div>
                    <div>
                      <label className="cs-label">{t('cta.field.sphere')}</label>
                      <input className="cs-input"
                             value={form.sphere} onChange={(e) => setField('sphere', e.target.value)} placeholder="SaaS / Fintech / Retail…" />
                    </div>
                  </>
                )}
                {step === 2 && (
                  <div>
                    <label className="cs-label">{t('cta.field.services')}</label>
                    <div className="cs-cp-svcs">
                      {svcOptions.map((s) => (
                        <button type="button" key={s.k}
                                className={'cs-cp-svc' + (form.services.includes(s.k) ? ' is-on' : '')}
                                onClick={() => toggleSvc(s.k)}>
                          {form.services.includes(s.k) ? '✓' : '+'} {s.label}
                        </button>
                      ))}
                    </div>
                  </div>
                )}
                <div className="cs-cp-actions">
                  {step === 2 && (
                    <button type="button" className="cs-btn cs-btn--ghost" onClick={() => setStep(1)}>{t('cta.back')}</button>
                  )}
                  <button type="submit" className="cs-btn cs-btn--primary" style={{ marginLeft: 'auto' }}>
                    {step === 1 ? t('cta.next') : t('cta.submit')}
                  </button>
                </div>
                <p className="cs-cp-policy" style={{ color: 'var(--c-faint)' }}><a href="/privacy-policy" style={{ color: 'inherit', textDecoration: 'underline' }}>{t('cta.policy')}</a></p>
              </form>
            )}
          </div>
        </div>
      </div>

      <style>{`
        .cs-final {
          background: var(--c-ink);
          color: var(--c-bg);
          position: relative; overflow: hidden;
        }
        .cs-final::before {
          content: ""; position: absolute; inset: 0;
          background:
            radial-gradient(ellipse at 0% 0%, rgba(37, 99, 235, 0.35) 0%, transparent 50%),
            radial-gradient(ellipse at 100% 100%, rgba(6, 182, 212, 0.2) 0%, transparent 50%);
          pointer-events: none;
        }
        .cs-final-grid { position: relative; display: grid; grid-template-columns: 1fr 1fr; gap: 80px; align-items: center; }
        @media (max-width: 1000px) { .cs-final-grid { grid-template-columns: 1fr; gap: 40px; } }
        @media (max-width: 600px)  { .cs-final-grid { gap: 28px; } }
        .cs-final-h { color: var(--c-bg); max-width: 14ch; margin-top: 16px; }
        .cs-final-sub { margin-top: 20px; font-size: 17px; line-height: 1.55; color: rgba(255,255,255,0.78); max-width: 48ch; }
        .cs-final-alts { margin-top: 40px; padding-top: 32px; border-top: 1px solid rgba(255,255,255,0.12); }
        .cs-final-alt-h { font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: rgba(255,255,255,0.55); margin-bottom: 16px; }
        .cs-final-alt-row { display: flex; flex-wrap: wrap; gap: 10px; }
        .cs-final-alt {
          display: inline-flex; align-items: center; gap: 10px;
          padding: 10px 16px 10px 10px; border-radius: 999px;
          background: rgba(255,255,255,0.08); color: #fff;
          font-size: 14.5px; font-weight: 500;
          transition: background 0.15s var(--ease);
        }
        .cs-final-alt:hover { background: rgba(255,255,255,0.16); }
        .cs-final-alt-i {
          width: 32px; height: 32px; border-radius: 999px;
          background: rgba(255,255,255,0.12);
          display: inline-flex; align-items: center; justify-content: center;
        }

        .cs-final-form-wrap {
          background: var(--c-bg);
          color: var(--c-ink);
          border-radius: 24px;
          padding: 36px;
          box-shadow: 0 40px 80px -20px rgba(0,0,0,0.4);
        }
        @media (max-width: 600px) { .cs-final-form-wrap { padding: 24px 20px; border-radius: 18px; } }
        [data-radius="sharp"] .cs-final-form-wrap { border-radius: 8px; }
        [data-radius="pill"] .cs-final-form-wrap { border-radius: 32px; }
        .cs-final-form { display: flex; flex-direction: column; gap: 16px; }
        .cs-final-row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
        @media (max-width: 540px) { .cs-final-row2 { grid-template-columns: 1fr; } }
        .cs-final-thanks { padding: 24px 0; text-align: center; display: flex; flex-direction: column; align-items: center; }
        .cs-cp-progress { display: flex; align-items: center; gap: 12px; }
        .cs-cp-step {
          width: 28px; height: 28px; border-radius: 999px;
          background: var(--c-bg-soft); color: var(--c-faint);
          font-size: 13px; font-weight: 600;
          display: inline-flex; align-items: center; justify-content: center;
          flex-shrink: 0;
          transition: background 0.2s var(--ease), color 0.2s var(--ease);
        }
        .cs-cp-step.is-on { background: var(--c-accent); color: #fff; }
        .cs-cp-bar { flex: 1; height: 2px; background: var(--c-bg-muted); border-radius: 999px; overflow: hidden; }
        .cs-cp-bar > div { height: 100%; background: var(--c-accent); transition: width 0.3s var(--ease); }
      `}</style>
    </section>
  );
}

Object.assign(window, { IndustriesSection, SecuritySection, FaqSection, FinalCTA });
