// Telegram Remote — animated iPhone 15 chat + printer mockup on the right.
// Flow: tap Onayla → bot replies with "E-Fatura taslağı hazırlandı" + İmzala button
//       tap İmzala → bot shows e-fatura preview with Paylaş + Yazdır buttons
//       tap Yazdır → printer (right of phone) slowly extrudes a Turkish e-Arşiv fatura

const SectionWrap = window.SectionWrap;
const SectionHead = window.SectionHead;

// ── Touch cursor: pulsing ring + white dot ──
const TouchCursor = ({ show, x, y, tap }) => (
  <div aria-hidden style={{
    position:'absolute', left:x, top:y,
    width:52, height:52, marginLeft:-26, marginTop:-26,
    pointerEvents:'none', zIndex:60,
    opacity: show ? 1 : 0,
    transform: `scale(${tap ? 0.8 : 1})`,
    transition:'opacity .22s ease, transform .18s ease, left .55s cubic-bezier(.4,0,.2,1), top .55s cubic-bezier(.4,0,.2,1)'
  }}>
    <div style={{
      position:'absolute', inset:0, borderRadius:9999,
      background:'rgba(93,230,255,0.18)',
      border:'2.5px solid rgba(93,230,255,0.75)',
      boxShadow:'0 0 26px rgba(93,230,255,0.55)',
      animation: show ? 'sp-ring 1.5s ease-out infinite' : 'none'
    }} />
    <div style={{
      position:'absolute', top:'50%', left:'50%', width:14, height:14,
      marginTop:-7, marginLeft:-7, borderRadius:9999,
      background:'#fff', boxShadow:'0 0 14px rgba(255,255,255,0.85)'
    }} />
  </div>
);

const BotBubble = ({ children, visible, borderLeft }) => (
  <div style={{
    alignSelf:'flex-start', maxWidth:'90%',
    padding:'10px 12px',
    background:'#182533', borderRadius:12, borderTopLeftRadius:4,
    borderLeft: borderLeft || undefined,
    opacity: visible ? 1 : 0,
    transform: visible ? 'translateY(0)' : 'translateY(8px)',
    transition:'opacity .4s ease, transform .4s ease'
  }}>{children}</div>
);

const UserBubble = ({ children, visible }) => (
  <div style={{
    alignSelf:'flex-end', maxWidth:'65%',
    padding:'8px 12px',
    background:'#2B5278', borderRadius:12, borderTopRightRadius:4,
    opacity: visible ? 1 : 0,
    transform: visible ? 'translateY(0) scale(1)' : 'translateY(8px) scale(0.96)',
    transition:'opacity .35s ease, transform .35s ease'
  }}>{children}</div>
);

// ── A4 e-Arşiv fatura, styled after the Bifamo reference ──
// Designed at paper width 210 with aspectRatio 210:297 (A4).
const FakeFatura = () => (
  <div style={{
    width: 210,
    aspectRatio: '210 / 297',
    background: '#fff',
    color: '#111',
    padding: '9px 10px',
    boxSizing: 'border-box',
    fontFamily: 'Helvetica, Arial, sans-serif',
    fontSize: 4.2,
    lineHeight: 1.32,
    boxShadow: '0 0 0 0.5px #ddd, 0 6px 18px rgba(0,0,0,0.25)',
    position: 'relative'
  }}>
    {/* TOP ROW: company / TC stamp / brand + QR */}
    <div style={{ display:'grid', gridTemplateColumns:'1fr 34px 1fr', gap:6, alignItems:'flex-start', marginBottom:6 }}>
      <div>
        <div style={{ fontWeight:800, fontSize:5.5, lineHeight:1.25, marginBottom:1.5 }}>
          MODA TİCARET LİMİTED ŞİRKETİ
        </div>
        <div style={{ color:'#222', fontSize:4, lineHeight:1.4 }}>
          BAĞCILAR MAH. 120 SK. NO: 14<br/>
          BAĞCILAR / İSTANBUL<br/>
          Tel: 0212 555 00 00<br/>
          Web: www.modaticaret.com<br/>
          e-Posta: info@modaticaret.com<br/>
          Vergi Dairesi: BAĞCILAR KURUMLAR<br/>
          VKN: 1234567890<br/>
          Ticaret Sicil No: 208761<br/>
          MERSİS No: 1234567890004821
        </div>
      </div>
      <div style={{ textAlign:'center' }}>
        <div style={{
          width:28, height:28, borderRadius:14,
          border:'1px solid #c0272d',
          display:'flex', alignItems:'center', justifyContent:'center',
          flexDirection:'column',
          margin:'0 auto 2px'
        }}>
          <div style={{ width:18, height:18, borderRadius:9, background:'#c0272d', display:'flex', alignItems:'center', justifyContent:'center', color:'#fff', fontWeight:900, fontSize:6 }}>TC</div>
        </div>
        <div style={{ fontWeight:700, fontSize:4.5, color:'#c0272d' }}>e-Arşiv Fatura</div>
      </div>
      <div style={{ display:'flex', flexDirection:'column', alignItems:'flex-end', gap:3 }}>
        {/* fake QR */}
        <div style={{
          width:30, height:30, background:'#0e0e10',
          backgroundImage:'repeating-linear-gradient(0deg,#fff 0 0.8px,transparent 0.8px 1.8px), repeating-linear-gradient(90deg,#fff 0 0.8px,transparent 0.8px 1.8px)',
          border:'1px solid #0e0e10'
        }} />
      </div>
    </div>

    {/* Signature + brand row */}
    <div style={{ display:'flex', alignItems:'center', justifyContent:'space-between', gap:6, marginBottom:6, paddingBottom:4, borderBottom:'0.5px solid #e5e5e5' }}>
      <div style={{ fontFamily:'Georgia, serif', fontStyle:'italic', fontSize:10, color:'#222', transform:'rotate(-5deg)', marginLeft:4 }}>M. Demir</div>
      <div style={{
        fontWeight:900, fontSize:12, letterSpacing:'-0.05em',
        background:'linear-gradient(90deg,#FF8C33,#FF6B00)',
        WebkitBackgroundClip:'text', backgroundClip:'text', WebkitTextFillColor:'transparent'
      }}>moda<span style={{fontStyle:'italic'}}>ticaret</span></div>
    </div>

    {/* SAYIN + data grid */}
    <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr', gap:6, marginBottom:6 }}>
      <div>
        <div style={{ fontWeight:800, fontSize:5, marginBottom:2 }}>SAYIN</div>
        <div style={{ fontSize:4.2, lineHeight:1.4 }}>
          Mehmet Yılmaz<br/>
          Kadıköy Mah. Bahçe Cd. No:14<br/>
          Kadıköy / İstanbul<br/>
          e-Posta: mehmet.yilmaz@mail.com<br/>
          TCKN: 12345678901
        </div>
        <div style={{ marginTop:3, fontSize:4, color:'#555' }}>
          ETTN: 48291dbbb-ccf7-46b6-bba9-495a9b18e10f
        </div>
      </div>
      <div style={{ border:'0.5px solid #ccc' }}>
        {[
          ['Tarih','17-04-2026'],
          ['Fatura No','EAA2026000048291'],
          ['Özelleştirme No','TR1.2'],
          ['Senaryo','EARSIVFATURA'],
          ['Fatura Tipi','SATIS'],
          ['Sipariş No','TY-48291'],
          ['Sipariş Tarihi','17-04-2026'],
          ['Son Ödeme Tarihi','17-04-2026'],
          ['Oluşma Zamanı','14:32:00'],
        ].map(([k,v],i,a) => (
          <div key={i} style={{ display:'flex', borderBottom: i===a.length-1?'none':'0.5px solid #e5e5e5', fontSize:4.2 }}>
            <div style={{ flex:1, padding:'1.5px 3px', fontWeight:700, background:'#f3f3f3' }}>{k}</div>
            <div style={{ flex:1, padding:'1.5px 3px' }}>{v}</div>
          </div>
        ))}
      </div>
    </div>

    {/* Items table */}
    <table style={{ width:'100%', borderCollapse:'collapse', fontSize:4, marginBottom:4 }}>
      <thead>
        <tr style={{ background:'#f3f3f3' }}>
          {['Sıra No','Mal/Hizmet','Açıklama','Miktar','Birim Fiyat','İskonto','Mal Tutar','KDV %','KDV Tutar'].map(h => (
            <th key={h} style={{ border:'0.5px solid #ccc', padding:'1.5px 2px', textAlign:'left', fontWeight:700, fontSize:3.8 }}>{h}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        <tr>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>1</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>T-Shirt Slim Fit M Siyah</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}></td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>1 Adet</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>290,83 TL</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>0,00 TL</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>290,83 TL</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>%20,00</td>
          <td style={{ border:'0.5px solid #ccc', padding:'1.5px 2px' }}>58,17 TL</td>
        </tr>
      </tbody>
    </table>

    {/* Totals block */}
    <div style={{ display:'flex', justifyContent:'flex-end', marginBottom:6 }}>
      <div style={{ width:'55%', fontSize:4.2 }}>
        {[
          ['Mal Hizmet Toplam Tutarı','290,83 TL'],
          ['KDV Matrahı (%20,00)','290,83 TL'],
          ['Vergi Hariç Tutar','290,83 TL'],
          ['Hesaplanan KDV (%20,00)','58,17 TL'],
          ['Vergiler Dahil Toplam Tutar','349,00 TL'],
          ['Ödenecek Tutar','349,00 TL'],
        ].map(([k,v],i,a) => (
          <div key={i} style={{
            display:'flex', justifyContent:'space-between',
            padding:'1.5px 3px',
            borderBottom: i===a.length-1 ? 'none' : '0.5px solid #e5e5e5',
            fontWeight: i===a.length-1 ? 800 : 500,
            background: i===a.length-1 ? '#fff5ec' : 'transparent',
            color: i===a.length-1 ? '#c53b00' : '#222'
          }}>
            <span>{k}:</span><span>{v}</span>
          </div>
        ))}
      </div>
    </div>

    {/* Gider pusulası + sebep kodu tabloları (simplified) */}
    <div style={{ display:'grid', gridTemplateColumns:'1.1fr 1fr', gap:4, marginBottom:4 }}>
      <div style={{ border:'0.5px solid #ccc' }}>
        <div style={{ padding:'2px 3px', fontWeight:800, fontSize:4.2, background:'#f3f3f3', borderBottom:'0.5px solid #ccc' }}>GİDER PUSULASI</div>
        <table style={{ width:'100%', borderCollapse:'collapse', fontSize:3.6 }}>
          <thead>
            <tr style={{ background:'#fafafa' }}>
              {['Ürün Adı','Adet','Birim','KDV','Toplam','Sebep'].map(h => (
                <th key={h} style={{ border:'0.5px solid #e5e5e5', padding:'1px 2px', textAlign:'left', fontWeight:700 }}>{h}</th>
              ))}
            </tr>
          </thead>
          <tbody>
            {[0,1,2,3,4].map(i => (
              <tr key={i}>
                {[0,1,2,3,4,5].map(j => (
                  <td key={j} style={{ border:'0.5px solid #e5e5e5', padding:'3px 2px' }}></td>
                ))}
              </tr>
            ))}
          </tbody>
        </table>
      </div>
      <div style={{ border:'0.5px solid #ccc' }}>
        <div style={{ padding:'2px 3px', fontWeight:800, fontSize:4.2, background:'#f3f3f3', borderBottom:'0.5px solid #ccc', textAlign:'center' }}>Sebep Kod Tablosu</div>
        {[
          ['A','Beden olarak uygun değil'],
          ['B','Hatalı ürün gönderimi'],
          ['C','Ürün kalitesini beğenmedim'],
          ['D','Ürün deforme oldu'],
          ['E','Ürün sitede göründüğü gibi değil'],
          ['F','Defolu ürün gönderimi'],
          ['G','Siparişim geç gönderildi'],
        ].map(([k,v],i) => (
          <div key={i} style={{ display:'flex', borderBottom:'0.5px solid #e5e5e5', fontSize:3.8 }}>
            <div style={{ width:9, padding:'1px 2px', borderRight:'0.5px solid #e5e5e5', textAlign:'center', fontWeight:700 }}>{k}</div>
            <div style={{ flex:1, padding:'1px 2px' }}>{v}</div>
          </div>
        ))}
      </div>
    </div>

    {/* Footer Ad Soyad / Telefon / Adres / İade Nedeni */}
    <div style={{ display:'grid', gridTemplateColumns:'1fr 1fr 1fr', gap:4 }}>
      <div style={{ border:'0.5px solid #ccc', padding:'2px 3px', fontSize:4, minHeight:16 }}>
        Ad Soyad :<br/>Adres :
      </div>
      <div style={{ border:'0.5px solid #ccc', padding:'2px 3px', fontSize:4, minHeight:16 }}>
        Telefon :<br/>İmza :
      </div>
      <div style={{ border:'0.5px solid #ccc', padding:'2px 3px', fontSize:4, minHeight:16 }}>
        İade Nedeni :
      </div>
    </div>
  </div>
);

// ── Printer mockup ──
// printing prop has three states: false (idle), 'extruding' (paper emerges & stays), 'fading' (paper fades out)
const PrinterMockup = ({ phase }) => {
  const extruded = phase === 'extruding' || phase === 'fading';
  const fading   = phase === 'fading';
  // Paper dimensions: 210 wide (A4 ratio). We let it emerge ABOVE the printer freely.
  return (
    <div style={{
      position:'relative',
      width:260, height:460,
      display:'flex', flexDirection:'column', alignItems:'center', justifyContent:'flex-end'
    }}>
      {/* Mask: clips paper to the area ABOVE the printer slot. The printer body
          (slot lip + body + feet + floor shadow) is ~195px tall measured from the
          bottom of the container, so the slot line is at bottom ≈ 195 (slot lip top).
          Mask bottom sits at that slot line; anything that would render below is clipped. */}
      <div style={{
        position:'absolute',
        left:'50%',
        bottom:180,     // printer body is ~195px; this puts mask bottom right at slot line
        transform:'translateX(-50%)',
        width:240,
        height:320,
        overflow:'hidden',
        pointerEvents:'none',
        display:'flex',
        justifyContent:'center',
        alignItems:'flex-end',
        zIndex:1,
      }}>
        <div style={{
          width:210,
          transform: `translateY(${extruded ? 0 : 100}%) scaleY(0.7)`,
          transformOrigin:'bottom center',
          opacity: fading ? 0 : (extruded ? 1 : 0),
          transition: fading
            ? 'opacity 0.8s ease-in'
            : 'transform 3.2s cubic-bezier(.4,0,.25,1), opacity 0.4s ease-out',
          filter:'drop-shadow(0 6px 10px rgba(0,0,0,0.25))'
        }}>
          <FakeFatura />
        </div>
      </div>

      {/* Printer body */}
      <div style={{ position:'relative', width:240, zIndex:2 }}>
        {/* top slot lip */}
        <div style={{
          height:14, background:'linear-gradient(180deg,#2a2a30,#1a1a1e)',
          borderRadius:'12px 12px 4px 4px',
          borderBottom:'1px solid #000',
          position:'relative'
        }}>
          <div style={{
            position:'absolute', top:4, left:'50%', transform:'translateX(-50%)',
            width:180, height:4, borderRadius:3,
            background:'#050506',
            boxShadow:'inset 0 1px 2px rgba(0,0,0,0.85)'
          }} />
        </div>
        {/* main body */}
        <div style={{
          background:'linear-gradient(180deg,#232328,#141418)',
          borderRadius:'0 0 14px 14px',
          padding:'18px 18px 22px',
          border:'1px solid #0a0a0c',
          borderTop:'none',
          boxShadow:'0 30px 50px rgba(0,0,0,0.5)'
        }}>
          {/* LCD */}
          <div style={{
            background:'#0a0a0c', borderRadius:6,
            padding:'8px 12px',
            border:'1px solid rgba(255,255,255,0.04)',
            display:'flex', alignItems:'center', justifyContent:'space-between',
            marginBottom:14
          }}>
            <div>
              <div style={{ fontSize:8, color:'#6b655f', fontFamily:'JetBrains Mono', letterSpacing:'0.15em', textTransform:'uppercase' }}>ShopPanel Printer</div>
              <div style={{
                fontSize:10, fontFamily:'JetBrains Mono', fontWeight:700, marginTop:2,
                color: phase === 'extruding' ? '#FF8C33' : phase === 'fading' ? '#5de6ff' : '#34d399'
              }}>
                {phase === 'extruding' ? '● yazdırıyor...' : phase === 'fading' ? '✓ tamamlandı' : '✓ hazır'}
              </div>
            </div>
            <div style={{ display:'flex', gap:4 }}>
              <span style={{
                width:8, height:8, borderRadius:9999,
                background: phase === 'extruding' ? '#FF6B00' : '#34d399',
                boxShadow: phase === 'extruding' ? '0 0 10px #FF6B00' : '0 0 8px #34d399',
                animation: phase === 'extruding' ? 'sp-pulse 0.9s ease-in-out infinite' : 'none'
              }} />
              <span style={{ width:8, height:8, borderRadius:9999, background:'#3a3a40' }} />
            </div>
          </div>

          {/* vents */}
          <div style={{ display:'flex', flexDirection:'column', gap:3, marginBottom:14 }}>
            {[0,1,2,3].map(i => (
              <div key={i} style={{ height:2, background:'rgba(0,0,0,0.55)', borderRadius:2 }} />
            ))}
          </div>

          {/* button row */}
          <div style={{ display:'flex', justifyContent:'space-between', alignItems:'center' }}>
            <div style={{ fontSize:8, fontFamily:'JetBrains Mono', color:'#6b655f', letterSpacing:'0.1em' }}>EPSON · LQ-2090</div>
            <div style={{ display:'flex', gap:5 }}>
              {['#FF6B00','#5de6ff','#a78bfa'].map((c,i) => (
                <div key={i} style={{
                  width:10, height:10, borderRadius:9999,
                  background:`linear-gradient(135deg,${c},${c}88)`,
                  boxShadow:'inset 0 1px 2px rgba(255,255,255,0.25)'
                }} />
              ))}
            </div>
          </div>

          {/* feet */}
          <div style={{ display:'flex', justifyContent:'space-between', marginTop:10 }}>
            <div style={{ width:20, height:4, background:'#0a0a0c', borderRadius:'0 0 3px 3px' }} />
            <div style={{ width:20, height:4, background:'#0a0a0c', borderRadius:'0 0 3px 3px' }} />
          </div>
        </div>
      </div>

      {/* floor shadow */}
      <div style={{
        width:240, height:16, marginTop:8,
        background:'radial-gradient(ellipse at center, rgba(0,0,0,0.5), transparent 70%)'
      }} />
    </div>
  );
};

// ── The animated iPhone chat ──
const AnimatedTelegramChat = () => {
  // Steps:
  // 0 = initial order bubble only
  // 2 = user "Onayla" bubble appears
  // 3 = bot "E-Fatura taslağı hazırlandı" + İmzala button
  // 5 = user "İmzala" bubble
  // 6 = bot e-fatura preview with Paylaş + Yazdır
  // 8 = user "Yazdır" bubble + printer starts printing
  const [step, setStep] = React.useState(0);
  const [cursor, setCursor] = React.useState({ show:false, x:180, y:360, tap:false });
  const [tappedOnayla, setTappedOnayla] = React.useState(false);
  const [tappedImzala, setTappedImzala] = React.useState(false);
  const [tappedYazdir, setTappedYazdir] = React.useState(false);
  // printer phase: 'idle' | 'extruding' | 'fading'
  const [printerPhase, setPrinterPhase] = React.useState('idle');
  const scrollRef = React.useRef(null);

  const onaylaRef = React.useRef(null);
  const imzalaRef = React.useRef(null);
  const yazdirRef = React.useRef(null);
  const phoneRef = React.useRef(null);

  const [isMobile, setIsMobile] = React.useState(
    typeof window !== 'undefined' && window.innerWidth < 768
  );
  React.useEffect(() => {
    const onResize = () => setIsMobile(window.innerWidth < 768);
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, []);

  // On Mobile: scale phone + printer down so both fit side-by-side
  const scale = isMobile ? 0.52 : 1;
  const wrapperHeight = isMobile ? 700 * scale + 30 : 'auto';

  // Compute cursor position inside the phone frame.
  // CRITICAL: getBoundingClientRect() returns TRANSFORMED dimensions. When the
  // parent is scaled, we must divide by scale to get back to the unscaled CSS
  // coordinate space used by the TouchCursor's position:absolute (left/top).
  const posOf = (ref) => {
    if (!ref.current || !phoneRef.current) return null;
    const b = ref.current.getBoundingClientRect();
    const p = phoneRef.current.getBoundingClientRect();
    const dx = (b.left - p.left) + b.width / 2;
    const dy = (b.top  - p.top)  + b.height / 2;
    return { x: dx / scale, y: dy / scale };
  };

  React.useEffect(() => {
    let timeouts = [];
    const push = (fn, t) => timeouts.push(setTimeout(fn, t));

    const run = () => {
      // reset
      setStep(0); setTappedOnayla(false); setTappedImzala(false); setTappedYazdir(false);
      setPrinterPhase('idle');
      setCursor({ show:false, x:180, y:360, tap:false });

      // 0.9s: cursor appears near center-bottom
      push(() => setCursor({ show:true, x:100, y:380, tap:false }), 900);

      // 1.5s: glide to Onayla
      push(() => {
        const p = posOf(onaylaRef);
        if (p) setCursor({ show:true, x:p.x, y:p.y, tap:false });
      }, 1500);

      // 2.4s: tap!
      push(() => {
        setCursor(c => ({ ...c, tap:true }));
        setTappedOnayla(true);
      }, 2400);

      // 2.7s: show user bubble, hide cursor
      push(() => {
        setStep(2);
        setCursor(c => ({ ...c, show:false, tap:false }));
      }, 2700);

      // 3.4s: bot reply (step 3)
      push(() => setStep(3), 3400);

      // 4.4s: cursor moves to İmzala
      push(() => {
        const p = posOf(imzalaRef);
        if (p) setCursor({ show:true, x:p.x, y:p.y, tap:false });
      }, 4400);

      // 5.3s: tap İmzala
      push(() => {
        setCursor(c => ({ ...c, tap:true }));
        setTappedImzala(true);
      }, 5300);

      // 5.6s: user bubble
      push(() => {
        setStep(5);
        setCursor(c => ({ ...c, show:false, tap:false }));
      }, 5600);

      // 6.3s: bot e-fatura preview
      push(() => setStep(6), 6300);

      // 7.3s: cursor moves to Yazdır
      push(() => {
        const p = posOf(yazdirRef);
        if (p) setCursor({ show:true, x:p.x, y:p.y, tap:false });
      }, 7300);

      // 8.2s: tap Yazdır
      push(() => {
        setCursor(c => ({ ...c, tap:true }));
        setTappedYazdir(true);
      }, 8200);

      // 8.5s: user bubble + printer begins extruding paper
      push(() => {
        setStep(8);
        setCursor(c => ({ ...c, show:false, tap:false }));
        setPrinterPhase('extruding');
      }, 8500);

      // 13s: paper is fully out, hold briefly, then fade
      push(() => {
        setPrinterPhase('fading');
      }, 13500);

      // 15s: reset paper (back to idle/hidden) so the next loop can extrude again
      push(() => {
        setPrinterPhase('idle');
      }, 15000);

      // loop at 17s
      push(run, 17000);
    };

    run();
    return () => timeouts.forEach(clearTimeout);
  }, []);

  React.useEffect(() => {
    if (scrollRef.current) {
      scrollRef.current.scrollTo({ top: scrollRef.current.scrollHeight, behavior:'smooth' });
    }
  }, [step]);

  return (
    <div style={{
      display:'flex',
      justifyContent:'center',
      // Fixed height (not minHeight) caps the layout box — transform:scale doesn't
      // reduce layout size on its own, so we need a hard cap to kill the empty gap.
      height: isMobile ? wrapperHeight : 'auto',
      overflow: isMobile ? 'hidden' : 'visible',
    }}>
      <div style={{
        display:'flex',
        alignItems: isMobile ? 'flex-start' : 'center',
        justifyContent:'center',
        gap: isMobile ? 6 : 32,
        flexWrap: isMobile ? 'nowrap' : 'wrap',
        transform: isMobile ? `scale(${scale})` : 'none',
        transformOrigin: 'top center',
      }}>
      {/* iPhone */}
      <div ref={phoneRef} style={{
        position:'relative',
        width:340, height:700, borderRadius:50,
        background:'#0a0a0c',
        boxShadow:'0 50px 100px rgba(0,0,0,0.55), 0 0 0 1.5px #1a1a1c, 0 0 0 9px #0a0a0c, 0 0 0 10px #2b2b30, 0 0 60px rgba(93,230,255,0.08)',
        padding:9, boxSizing:'border-box',
        flexShrink:0
      }}>
        {/* Screen */}
        <div style={{
          position:'relative',
          width:'100%', height:'100%',
          borderRadius:42, overflow:'hidden',
          background:'linear-gradient(180deg,#0E1621,#131F2B)',
          display:'flex', flexDirection:'column'
        }}>
          {/* Dynamic island */}
          <div style={{
            position:'absolute', top:10, left:'50%', transform:'translateX(-50%)',
            width:100, height:30, borderRadius:20, background:'#000', zIndex:30
          }} />
          {/* Status bar */}
          <div style={{
            display:'flex', justifyContent:'space-between', alignItems:'center',
            padding:'13px 26px 6px', fontFamily:'-apple-system,system-ui',
            fontSize:12, fontWeight:700, color:'#fff', position:'relative', zIndex:5
          }}>
            <span>14:32</span>
            <span style={{ width:70 }} />
            <span style={{ display:'flex', gap:4, alignItems:'center' }}>
              <svg width="14" height="9" viewBox="0 0 15 10"><rect x="0" y="6" width="2.5" height="4" rx="0.5" fill="#fff"/><rect x="4" y="4" width="2.5" height="6" rx="0.5" fill="#fff"/><rect x="8" y="2" width="2.5" height="8" rx="0.5" fill="#fff"/><rect x="12" y="0" width="2.5" height="10" rx="0.5" fill="#fff"/></svg>
              <svg width="20" height="9" viewBox="0 0 22 10"><rect x="0.5" y="0.5" width="18" height="9" rx="2.5" stroke="#fff" strokeOpacity="0.5" fill="none"/><rect x="2" y="2" width="15" height="6" rx="1" fill="#fff"/></svg>
            </span>
          </div>

          {/* Chat header */}
          <div style={{
            padding:'4px 16px 12px', background:'#17212B',
            borderBottom:'1px solid rgba(255,255,255,0.05)',
            display:'flex', alignItems:'center', gap:10
          }}>
            <div style={{
              width:32, height:32, borderRadius:9999,
              background:'linear-gradient(135deg,#FF6B00,#FF8C33)',
              display:'flex', alignItems:'center', justifyContent:'center',
              fontSize:13, fontWeight:900, color:'#0e0e10'
            }}>◆</div>
            <div style={{ flex:1 }}>
              <div style={{ fontSize:13, fontWeight:700, color:'#fff' }}>ShopPanel Bot</div>
              <div style={{ fontSize:10, color:'#6b9ac9' }}>bot · çevrimiçi</div>
            </div>
          </div>

          {/* Chat scroll body */}
          <div ref={scrollRef} style={{
            flex:1, overflowY:'auto', padding:'14px 12px',
            display:'flex', flexDirection:'column', gap:8,
            scrollbarWidth:'none'
          }}>
            {/* Initial order — NO İmzala, only Onayla + Etiket + E-Fatura */}
            <BotBubble visible={true}>
              <div style={{ fontSize:9, color:'#5de6ff', fontFamily:'JetBrains Mono', letterSpacing:'0.1em', marginBottom:4 }}>
                YENİ SİPARİŞ · TRENDYOL
              </div>
              <div style={{ fontSize:12, color:'#e5e1e4', lineHeight:1.5 }}>
                <b>#TY-48291</b> · 349₺<br/>
                T-Shirt Slim · M · Siyah<br/>
                <span style={{ color:'#9ccaff' }}>M. Yılmaz · İstanbul</span>
              </div>
              <div style={{ display:'flex', gap:5, marginTop:8, flexWrap:'wrap' }}>
                <button
                  ref={onaylaRef}
                  style={{
                    padding:'6px 10px', borderRadius:7,
                    background:'#34d399', color:'#0e0e10', border:'none',
                    fontSize:11, fontWeight:800, cursor:'pointer',
                    transform: tappedOnayla ? 'scale(0.9)' : 'scale(1)',
                    boxShadow: tappedOnayla ? '0 0 22px rgba(52,211,153,0.8)' : 'none',
                    transition:'transform .18s ease, box-shadow .18s ease'
                  }}>✓ Onayla</button>
                <button style={{ padding:'6px 10px', borderRadius:7, background:'rgba(93,230,255,0.9)', color:'#0e0e10', border:'none', fontSize:11, fontWeight:800 }}>🏷 Etiket</button>
                <button style={{ padding:'6px 10px', borderRadius:7, background:'rgba(255,140,51,0.9)', color:'#0e0e10', border:'none', fontSize:11, fontWeight:800 }}>🧾 E-Fatura</button>
              </div>
              <div style={{ fontSize:9, color:'#6b9ac9', marginTop:6, fontFamily:'JetBrains Mono' }}>14:32</div>
            </BotBubble>

            {step >= 2 && (
              <UserBubble visible>
                <div style={{ fontSize:12, color:'#fff' }}>✓ Onayla</div>
                <div style={{ fontSize:9, color:'#a4cdec', marginTop:3, fontFamily:'JetBrains Mono', textAlign:'right' }}>14:32 ✓✓</div>
              </UserBubble>
            )}

            {step >= 3 && (
              <BotBubble visible>
                <div style={{ fontSize:12, color:'#cfc4bf', lineHeight:1.55 }}>
                  ✓ <b style={{ color:'#34d399' }}>Sipariş onaylandı</b><br/>
                  ✓ Etiket yazıcıya gönderildi<br/>
                  ✓ <b style={{ color:'#FF8C33' }}>E-Fatura taslağı hazırlandı</b>
                </div>
                <div style={{ marginTop:8, padding:'8px 10px', borderRadius:8, background:'rgba(255,140,51,0.08)', border:'1px solid rgba(255,140,51,0.3)' }}>
                  <div style={{ fontSize:9, color:'#FF8C33', fontFamily:'JetBrains Mono', letterSpacing:'0.1em', marginBottom:4 }}>E-FATURA TASLAĞI</div>
                  <div style={{ fontSize:11, color:'#e5e1e4' }}>
                    M. Yılmaz · <b>349,00₺</b><br/>
                    <span style={{ color:'#a98a7d' }}>Alıcı ve tutar doğrulandı.</span>
                  </div>
                </div>
                <div style={{ display:'flex', gap:5, marginTop:8 }}>
                  <button
                    ref={imzalaRef}
                    style={{
                      padding:'6px 12px', borderRadius:7,
                      background:'#FF6B00', color:'#572000', border:'none',
                      fontSize:11, fontWeight:800, cursor:'pointer',
                      transform: tappedImzala ? 'scale(0.9)' : 'scale(1)',
                      boxShadow: tappedImzala ? '0 0 22px rgba(255,107,0,0.8)' : 'none',
                      transition:'transform .18s ease, box-shadow .18s ease'
                    }}>✍ İmzala</button>
                  <button style={{ padding:'6px 10px', borderRadius:7, background:'rgba(255,255,255,0.06)', color:'#e5e1e4', border:'1px solid rgba(255,255,255,0.1)', fontSize:11, fontWeight:600 }}>Düzenle</button>
                </div>
                <div style={{ fontSize:9, color:'#6b9ac9', marginTop:6, fontFamily:'JetBrains Mono' }}>14:32</div>
              </BotBubble>
            )}

            {step >= 5 && (
              <UserBubble visible>
                <div style={{ fontSize:12, color:'#fff' }}>✍ İmzala</div>
                <div style={{ fontSize:9, color:'#a4cdec', marginTop:3, fontFamily:'JetBrains Mono', textAlign:'right' }}>14:32 ✓✓</div>
              </UserBubble>
            )}

            {step >= 6 && (
              <BotBubble visible borderLeft="3px solid #34d399">
                <div style={{ fontSize:9, color:'#34d399', fontFamily:'JetBrains Mono', letterSpacing:'0.1em', marginBottom:6 }}>
                  ✓ E-FATURA İMZALANDI · GİB ONAYLI
                </div>
                <div style={{
                  background:'#fff', color:'#0e0e10', borderRadius:6,
                  padding:'8px 10px', fontFamily:'ui-monospace, monospace', fontSize:10, lineHeight:1.5
                }}>
                  <div style={{ display:'flex', justifyContent:'space-between', borderBottom:'1px dashed #ccc', paddingBottom:3, marginBottom:3 }}>
                    <b>E-ARŞİV FATURA</b>
                    <span>#EAA…48291</span>
                  </div>
                  <div>M. Yılmaz · T-Shirt Slim · M</div>
                  <div style={{ display:'flex', justifyContent:'space-between', fontWeight:700 }}>
                    <span>Ödenecek</span><span>349,00 ₺</span>
                  </div>
                </div>
                <div style={{ display:'flex', gap:5, marginTop:8 }}>
                  <button style={{ padding:'6px 12px', borderRadius:7, background:'#5de6ff', color:'#0e0e10', border:'none', fontSize:11, fontWeight:800 }}>⇪ Paylaş</button>
                  <button
                    ref={yazdirRef}
                    style={{
                      padding:'6px 12px', borderRadius:7,
                      background:'#a78bfa', color:'#0e0e10', border:'none',
                      fontSize:11, fontWeight:800, cursor:'pointer',
                      transform: tappedYazdir ? 'scale(0.9)' : 'scale(1)',
                      boxShadow: tappedYazdir ? '0 0 22px rgba(167,139,250,0.85)' : 'none',
                      transition:'transform .18s ease, box-shadow .18s ease'
                    }}>🖨 Yazdır</button>
                </div>
                <div style={{ fontSize:9, color:'#6b9ac9', marginTop:6, fontFamily:'JetBrains Mono' }}>14:32</div>
              </BotBubble>
            )}

            {step >= 8 && (
              <UserBubble visible>
                <div style={{ fontSize:12, color:'#fff' }}>🖨 Yazdır</div>
                <div style={{ fontSize:9, color:'#a4cdec', marginTop:3, fontFamily:'JetBrains Mono', textAlign:'right' }}>14:32 ✓✓</div>
              </UserBubble>
            )}
          </div>

          {/* Home indicator */}
          <div style={{ height:20, display:'flex', justifyContent:'center', alignItems:'flex-end', paddingBottom:6 }}>
            <div style={{ width:110, height:4, borderRadius:4, background:'rgba(255,255,255,0.35)' }} />
          </div>
        </div>

        {/* Cursor */}
        <TouchCursor show={cursor.show} x={cursor.x} y={cursor.y} tap={cursor.tap} />
      </div>

      {/* Printer */}
      <PrinterMockup phase={printerPhase} />
      </div>
    </div>
  );
};

const TelegramRemoteSection = () => {
  const [isMobile, setIsMobile] = React.useState(
    typeof window !== 'undefined' && window.innerWidth < 768
  );
  React.useEffect(() => {
    const onResize = () => setIsMobile(window.innerWidth < 768);
    window.addEventListener('resize', onResize);
    return () => window.removeEventListener('resize', onResize);
  }, []);

  return (
  <SectionWrap id="telegram" label="06 Telegram Remote" padY={isMobile ? 80 : 160}>
    <div style={{ maxWidth:1280, margin:'0 auto', padding: isMobile ? '0 16px' : '0 32px' }}>
      <SectionHead
        eyebrow="telegram · uzaktan kontrol"
        eyebrowColor="#5de6ff"
        title="Onayla, imzala,"
        titleAccent="yazdır. Telefondan."
        lede="Sipariş geldiğinde Telegram'a düşer. Onaylarsınız; bot e-fatura taslağını hazırlar. İmzalarsınız; GİB onayı gelir. Yazdır'a dokunursunuz; mağazadaki yazıcı saniyeler içinde çıktıyı verir."
      />

      <div style={{ marginTop: isMobile ? 32 : 64 }}>
        <AnimatedTelegramChat />
      </div>

      <div style={{
        marginTop: isMobile ? 32 : 64,
        display:'grid',
        gridTemplateColumns: isMobile ? 'repeat(2, minmax(0, 1fr))' : 'repeat(4, 1fr)',
        gap: isMobile ? 10 : 14,
        maxWidth:1080, marginLeft:'auto', marginRight:'auto'
      }}>
        {[
          { t:'Tek sohbette: onay → imza → yazdırma zinciri.', i:'🔗' },
          { t:'GİB e-Arşiv uyumu — taslak, imza, iletim otomatik.', i:'✍' },
          { t:'Uzaktan yazdırma: mağaza içi yazıcılara anında kuyrukla.', i:'🖨' },
          { t:'Mobil uygulama (iOS & Android · yakında) — aynı akış.', i:'📱' },
        ].map((x,i) => (
          <div key={i} style={{ padding:'16px 18px', borderRadius:12, background:'rgba(19,19,21,0.5)', border:'1px solid rgba(255,255,255,0.05)' }}>
            <div style={{ fontSize:22, lineHeight:1, marginBottom:8 }}>{x.i}</div>
            <div style={{ fontSize:13, lineHeight:1.55, color:'#cfc4bf' }}>{x.t}</div>
          </div>
        ))}
      </div>
    </div>
  </SectionWrap>
  );
};

window.TelegramRemoteSection = TelegramRemoteSection;
