// phone-screens.jsx — Hero iPhone component for Keisa landing
// Renders real app screens (Home · Timeline · Chat) inside an iPhone shell

const GRAD = 'linear-gradient(135deg, #6C63FF 0%, #5B8EFF 55%, #3FA9F5 100%)';
const INK  = '#111827';
const GRY  = '#6B7280';
const LGY  = '#9CA3AF';
const PUR  = '#6C63FF';

// ─── K Symbol ──────────────────────────────────────────────────────────────
const PhKSymbol = ({ size = 22, white = false }) => {
  const fill = white ? '#FFFFFF' : 'url(#ph_ksym_g)';
  return (
    <svg width={size} height={Math.round(size * 1.1)} viewBox="0 0 70 88" fill="none">
      {!white && (
        <defs>
          <linearGradient id="ph_ksym_g" x1="5" y1="5" x2="65" y2="80" gradientUnits="userSpaceOnUse">
            <stop offset="0%" stopColor="#6C63FF" />
            <stop offset="100%" stopColor="#3FA9F5" />
          </linearGradient>
        </defs>
      )}
      <path d="M12 4C13 4 14.5 4.5 16 5C17 6.5 17 8.5 17 8.5L17 79.5C17 81.5 16 83 14.5 83.5C13 84 12 84 12 84C10 84 9 82.5 9 80.5L9 7.5C9 5.5 10 4 12 4Z" fill={fill} />
      <path d="M17 44C21 41.5 29 35 41 26C47 21.5 53 18.5 56 19C60 19.8 61 23 59.5 26C58.5 28.5 55 30.5 51 32.5L28 44Z" fill={fill} />
      <path d="M17 46L51 58.5C55 60.5 58.5 62.5 59.5 65.5C61 68.5 60 71.8 56 72.5C53 73 47 70 41 65.5C29 56.5 21 49 17 46Z" fill={fill} opacity="0.9" />
      <circle cx="58" cy="45" r="5" fill={fill} opacity="0.5" />
    </svg>
  );
};

// ─── Shared sub-components ─────────────────────────────────────────────────
const NavBottom = ({ active }) => {
  const items = [
    { id: 'home', label: 'Inicio',
      icon: (c) => <svg width="20" height="20" fill="none" viewBox="0 0 24 24"><path d="M3 9.5L12 3l9 6.5V20a1 1 0 01-1 1H4a1 1 0 01-1-1V9.5z" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/><path d="M9 21V12h6v9" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/></svg> },
    { id: 'timeline', label: 'Causa',
      icon: (c) => <svg width="20" height="20" fill="none" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" stroke={c} strokeWidth="1.8"/><path d="M12 7v5l3 3" stroke={c} strokeWidth="1.8" strokeLinecap="round"/></svg> },
    { id: 'chat', label: 'Asistente',
      icon: (c) => <svg width="20" height="20" fill="none" viewBox="0 0 24 24"><path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z" stroke={c} strokeWidth="1.8" strokeLinejoin="round"/></svg> },
  ];
  return (
    <div style={{ display: 'flex', borderTop: '1px solid #F3F4F6', background: '#fff', paddingBottom: 6, flexShrink: 0 }}>
      {items.map(it => {
        const isAct = it.id === active;
        const c = isAct ? PUR : LGY;
        return (
          <div key={it.id} style={{ flex: 1, display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3, padding: '9px 0 4px' }}>
            {it.icon(c)}
            <span style={{ fontSize: 9.5, fontWeight: isAct ? 600 : 400, color: c, fontFamily: "'DM Sans',sans-serif" }}>{it.label}</span>
          </div>
        );
      })}
    </div>
  );
};

// ─── Screen 1: Home ────────────────────────────────────────────────────────
const PhScreenHome = () => (
  <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: '#F8F9FA' }}>
    {/* Top bar */}
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '10px 16px', background: '#fff', borderBottom: '1px solid #F3F4F6', flexShrink: 0 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
        <PhKSymbol size={22} />
        <span style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 700, fontSize: 15, color: '#0F172A', letterSpacing: '-0.3px' }}>Keisa</span>
      </div>
      <div style={{ display: 'flex', gap: 14 }}>
        <svg width="18" height="18" fill="none" viewBox="0 0 24 24"><path d="M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9M13.73 21a2 2 0 01-3.46 0" stroke={GRY} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
        <svg width="18" height="18" fill="none" viewBox="0 0 24 24"><path d="M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4M16 17l5-5-5-5M21 12H9" stroke={GRY} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
      </div>
    </div>

    {/* Content */}
    <div style={{ flex: 1, padding: '18px 16px 12px', display: 'flex', flexDirection: 'column', gap: 12, overflowY: 'hidden' }}>
      {/* Greeting */}
      <div>
        <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 700, fontSize: 21, color: INK, lineHeight: '27px' }}>Hola, Valentina</div>
        <div style={{ fontSize: 11, color: LGY, marginTop: 2 }}>Pensión alimenticia · Rol C-1234-2023</div>
      </div>

      {/* Notification card (gradient) */}
      <div style={{ background: 'linear-gradient(135deg, rgba(108,99,255,0.14) 0%, rgba(63,169,245,0.2) 100%)', borderRadius: 14, padding: '14px 16px', border: '1px solid rgba(108,99,255,0.24)' }}>
        <div style={{ fontSize: 9.5, fontWeight: 700, color: '#3FA9F5', textTransform: 'uppercase', letterSpacing: '0.08em', marginBottom: 5 }}>Nueva resolución</div>
        <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 700, fontSize: 14, color: INK, marginBottom: 4 }}>El tribunal agendó una audiencia</div>
        <div style={{ fontSize: 12, color: '#4B5563', lineHeight: '16px', marginBottom: 11 }}>15 de junio — confirma con tu abogado antes del 10/06.</div>
        <div style={{ display: 'inline-flex', alignItems: 'center', height: 30, padding: '0 14px', background: GRAD, borderRadius: 15, fontSize: 11, color: '#fff', fontWeight: 500 }}>¿Qué debo hacer?</div>
      </div>

      {/* Status card */}
      <div style={{ background: '#fff', borderRadius: 14, padding: '14px 16px', border: '1px solid #F3F4F6', boxShadow: '0 1px 4px rgba(0,0,0,0.04)' }}>
        <div style={{ fontSize: 10, fontWeight: 600, color: LGY, textTransform: 'uppercase', letterSpacing: '0.06em', marginBottom: 6 }}>Estado actual</div>
        <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 700, fontSize: 16, color: INK, marginBottom: 8 }}>En espera de audiencia</div>
        <span style={{ display: 'inline-flex', alignItems: 'center', gap: 4, padding: '4px 12px', borderRadius: 9999, background: 'rgba(245,158,11,0.12)', color: '#F59E0B', fontSize: 12, fontWeight: 500, fontFamily: "'DM Sans',sans-serif" }}>● En espera</span>
      </div>

      {/* Next deadline */}
      <div style={{ background: '#fff', borderRadius: 14, padding: '12px 14px', border: '1px solid #F3F4F6', display: 'flex', gap: 10, alignItems: 'flex-start' }}>
        <svg width="16" height="16" fill="none" viewBox="0 0 24 24" style={{ flexShrink: 0, marginTop: 1 }}><rect x="3" y="4" width="18" height="18" rx="2" stroke={PUR} strokeWidth="1.8"/><path d="M16 2v4M8 2v4M3 10h18" stroke={PUR} strokeWidth="1.8" strokeLinecap="round"/></svg>
        <div>
          <div style={{ fontSize: 10, fontWeight: 600, color: LGY, textTransform: 'uppercase', letterSpacing: '0.06em', marginBottom: 2 }}>Próximo plazo</div>
          <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 600, fontSize: 12, color: INK }}>Presentar documentos de ingresos antes del 15 de octubre.</div>
        </div>
      </div>
    </div>

    <NavBottom active="home" />
  </div>
);

// ─── Screen 2: Timeline ────────────────────────────────────────────────────
const PhScreenTimeline = () => {
  const events = [
    { date: '12 OCT', title: 'Tu audiencia ya tiene fecha', body: 'El tribunal agendó una reunión oficial. Debes confirmar asistencia con tu abogado.', action: 'Confirma con tu abogado antes del 10 de junio.', active: true },
    { date: '11 OCT', title: 'Tu demanda fue entregada', body: 'Tu abogado presentó formalmente tu petición al tribunal.', action: null, active: false },
    { date: '5 OCT', title: 'Causa conectada a Keisa', body: 'Monitoreamos cada movimiento desde esta fecha.', action: null, active: false },
  ];
  return (
    <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: '#F8F9FA' }}>
      {/* Top bar */}
      <div style={{ display: 'flex', alignItems: 'center', padding: '10px 16px', background: '#fff', borderBottom: '1px solid #F3F4F6', gap: 8, flexShrink: 0 }}>
        <svg width="18" height="18" fill="none" viewBox="0 0 24 24"><path d="M19 12H5M12 5l-7 7 7 7" stroke={INK} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
        <span style={{ fontFamily: "'DM Sans',sans-serif", fontSize: 14, fontWeight: 600, color: INK }}>Movimientos de la causa</span>
      </div>

      {/* Content */}
      <div style={{ flex: 1, padding: '16px 16px 8px', overflowY: 'hidden', display: 'flex', flexDirection: 'column' }}>
        <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontWeight: 700, fontSize: 17, color: INK, marginBottom: 2 }}>En simple, esto ha pasado</div>
        <div style={{ fontSize: 11, color: LGY, marginBottom: 14 }}>Pensión · Rol C-1234-2023</div>

        {events.map((ev, i) => (
          <div key={i} style={{ display: 'flex', gap: 10 }}>
            {/* Timeline spine */}
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', paddingTop: 5, flexShrink: 0 }}>
              <div style={{ width: 10, height: 10, borderRadius: 9999, background: ev.active ? GRAD : '#D1D5DB' }} />
              {i < events.length - 1 && <div style={{ width: 2, flex: 1, minHeight: 28, background: '#E5E7EB', margin: '3px 0' }} />}
            </div>
            {/* Card */}
            <div style={{ flex: 1, background: '#fff', borderRadius: 12, padding: '10px 12px', border: '1px solid #F3F4F6', boxShadow: '0 1px 3px rgba(0,0,0,0.04)', marginBottom: 8 }}>
              <div style={{ fontSize: 9, fontWeight: 700, color: LGY, textTransform: 'uppercase', letterSpacing: '0.06em', marginBottom: 3 }}>{ev.date}</div>
              <div style={{ fontFamily: "'Plus Jakarta Sans',sans-serif", fontSize: 12, fontWeight: 600, color: INK, marginBottom: 3 }}>{ev.title}</div>
              <div style={{ fontSize: 11, color: GRY, lineHeight: '15px' }}>{ev.body}</div>
              {ev.action && (
                <div style={{ marginTop: 7, padding: '5px 8px', background: 'rgba(63,169,245,0.07)', borderRadius: 6 }}>
                  <div style={{ fontSize: 9, fontWeight: 700, color: '#3FA9F5', textTransform: 'uppercase', letterSpacing: '0.06em', marginBottom: 2 }}>¿Qué hacer?</div>
                  <div style={{ fontSize: 10.5, color: '#4B5563', fontStyle: 'italic', lineHeight: '14px' }}>{ev.action}</div>
                </div>
              )}
            </div>
          </div>
        ))}
      </div>

      <NavBottom active="timeline" />
    </div>
  );
};

// ─── Screen 3: Chat ─────────────────────────────────────────────────────────
const PhScreenChat = () => {
  const msgs = [
    { text: 'Hola Valentina. El tribunal fijó una audiencia para el 15 de junio. ¿En qué te puedo ayudar hoy?', isUser: false },
    { text: '¿Tengo que llevar algo el día de la audiencia?', isUser: true },
    { text: 'Sí. Lleva tu carnet de identidad, el comprobante del RIT y, si los tienes, ingresos del mes pasado.', isUser: false },
  ];
  return (
    <div style={{ display: 'flex', flexDirection: 'column', height: '100%', background: '#F8F9FA' }}>
      {/* Top bar */}
      <div style={{ display: 'flex', alignItems: 'center', padding: '10px 16px', background: '#fff', borderBottom: '1px solid #F3F4F6', gap: 8, flexShrink: 0 }}>
        <svg width="18" height="18" fill="none" viewBox="0 0 24 24"><path d="M19 12H5M12 5l-7 7 7 7" stroke={INK} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
        <div style={{ display: 'flex', alignItems: 'center', gap: 7 }}>
          <div style={{ width: 28, height: 28, borderRadius: 9999, background: GRAD, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
            <PhKSymbol size={13} white />
          </div>
          <span style={{ fontFamily: "'DM Sans',sans-serif", fontSize: 14, fontWeight: 600, color: INK }}>Asistente Keisa</span>
        </div>
      </div>

      {/* Privacy banner */}
      <div style={{ padding: '6px 14px', background: 'rgba(63,169,245,0.08)', borderBottom: '1px solid rgba(63,169,245,0.1)', display: 'flex', gap: 6, alignItems: 'center', flexShrink: 0 }}>
        <svg width="12" height="12" fill="none" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" stroke="#3FA9F5" strokeWidth="2"/><path d="M12 8v4M12 16h.01" stroke="#3FA9F5" strokeWidth="2" strokeLinecap="round"/></svg>
        <span style={{ fontSize: 10.5, color: '#3FA9F5', fontFamily: "'DM Sans',sans-serif" }}>No estás sola. Todo lo que hablemos es 100% privado.</span>
      </div>

      {/* Messages */}
      <div style={{ flex: 1, padding: '14px 14px 8px', display: 'flex', flexDirection: 'column', gap: 10 }}>
        {msgs.map((m, i) => (
          <div key={i} style={{ display: 'flex', justifyContent: m.isUser ? 'flex-end' : 'flex-start', gap: 6, alignItems: 'flex-end' }}>
            {!m.isUser && (
              <div style={{ width: 26, height: 26, borderRadius: 9999, background: GRAD, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                <PhKSymbol size={12} white />
              </div>
            )}
            <div style={{
              maxWidth: '76%', padding: '8px 12px',
              fontSize: 12, lineHeight: '17px',
              borderRadius: m.isUser ? '14px 14px 4px 14px' : '14px 14px 14px 4px',
              background: m.isUser ? GRAD : '#fff',
              color: m.isUser ? '#fff' : INK,
              boxShadow: '0 1px 3px rgba(0,0,0,0.07)',
              fontFamily: "'DM Sans',sans-serif",
            }}>{m.text}</div>
          </div>
        ))}
      </div>

      {/* Input bar */}
      <div style={{ padding: '8px 12px', borderTop: '1px solid #F3F4F6', background: '#fff', display: 'flex', gap: 8, alignItems: 'center', flexShrink: 0 }}>
        <div style={{ flex: 1, height: 38, background: '#F3F4F6', borderRadius: 19, display: 'flex', alignItems: 'center', padding: '0 14px' }}>
          <span style={{ fontSize: 12, color: LGY, fontFamily: "'DM Sans',sans-serif" }}>Escribe tu pregunta...</span>
        </div>
        <div style={{ width: 38, height: 38, borderRadius: 9999, background: GRAD, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
          <svg width="14" height="14" fill="none" viewBox="0 0 24 24"><path d="M22 2L11 13M22 2L15 22l-4-9-9-4 20-7z" stroke="#fff" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
        </div>
      </div>

      <NavBottom active="chat" />
    </div>
  );
};

// ─── iPhone Shell ───────────────────────────────────────────────────────────
const HeroPhone = () => {
  const [screen, setScreen] = React.useState(0);
  const screens = [<PhScreenHome />, <PhScreenTimeline />, <PhScreenChat />];

  React.useEffect(() => {
    const t = setInterval(() => {
      setScreen(prev => {
        const next = (prev + 1) % 3;
        document.dispatchEvent(new CustomEvent('ph:change', { detail: next }));
        return next;
      });
    }, 3800);
    return () => clearInterval(t);
  }, []);

  React.useEffect(() => {
    window.goToPhoneScreen = n => {
      setScreen(n);
      document.dispatchEvent(new CustomEvent('ph:change', { detail: n }));
    };
  }, []);

  return (
    <div style={{
      width: 308,
      borderRadius: 54,
      background: 'linear-gradient(160deg, #2C2C2E 0%, #1C1C1E 55%, #0F0F10 100%)',
      border: '2px solid rgba(255,255,255,0.22)',
      padding: '16px 12px 26px',
      boxShadow: '0 0 0 1px rgba(0,0,0,0.8), 0 36px 88px rgba(0,0,0,0.62), 0 0 64px rgba(108,99,255,0.18), inset 0 1px 2px rgba(255,255,255,0.14)',
      position: 'relative',
    }}>
      {/* Physical buttons */}
      <div style={{ position: 'absolute', right: -4, top: 104, width: 4, height: 62, background: 'rgba(255,255,255,0.18)', borderRadius: '0 3px 3px 0' }} />
      <div style={{ position: 'absolute', left: -4, top: 80,  width: 4, height: 30, background: 'rgba(255,255,255,0.18)', borderRadius: '3px 0 0 3px' }} />
      <div style={{ position: 'absolute', left: -4, top: 120, width: 4, height: 30, background: 'rgba(255,255,255,0.18)', borderRadius: '3px 0 0 3px' }} />

      {/* Screen glass */}
      <div style={{ background: '#F8F9FA', borderRadius: 42, overflow: 'hidden', height: 580, position: 'relative' }}>
        {/* Dynamic Island */}
        <div style={{
          position: 'absolute', top: 12, left: '50%', transform: 'translateX(-50%)',
          width: 118, height: 34, background: '#000', borderRadius: 20, zIndex: 20,
          boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.04)',
        }} />

        {/* Status bar */}
        <div style={{ height: 52, display: 'flex', justifyContent: 'space-between', alignItems: 'flex-end', padding: '0 22px 9px', position: 'relative', zIndex: 10 }}>
          <span style={{ fontFamily: "'DM Sans',sans-serif", fontSize: 13, fontWeight: 700, color: INK }}>9:41</span>
          <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
            {/* Signal */}
            <svg width="15" height="11" fill="none" viewBox="0 0 15 11">
              <rect x="0"   y="7" width="2.5" height="4"  rx="0.8" fill={INK} opacity="0.35"/>
              <rect x="3.5" y="5" width="2.5" height="6"  rx="0.8" fill={INK} opacity="0.6"/>
              <rect x="7"   y="3" width="2.5" height="8"  rx="0.8" fill={INK} opacity="0.8"/>
              <rect x="10.5" y="0" width="2.5" height="11" rx="0.8" fill={INK}/>
            </svg>
            {/* Battery */}
            <svg width="24" height="12" fill="none" viewBox="0 0 24 12">
              <rect x="0.5" y="0.5" width="20" height="11" rx="3.5" stroke={INK} strokeOpacity="0.35"/>
              <rect x="2"   y="2"   width="17" height="8"  rx="2.5" fill={INK}/>
              <path d="M22 4V8C22.8 7.5 23.5 6.8 23.5 6S22.8 4.5 22 4Z" fill={INK} opacity="0.4"/>
            </svg>
          </div>
        </div>

        {/* Screen frames */}
        <div style={{ height: 'calc(100% - 52px)', position: 'relative' }}>
          {screens.map((sc, i) => (
            <div key={i} style={{
              position: 'absolute', inset: 0,
              opacity: i === screen ? 1 : 0,
              transition: 'opacity 0.7s ease',
              pointerEvents: i === screen ? 'auto' : 'none',
            }}>{sc}</div>
          ))}
        </div>
      </div>

      {/* Floating notification badge */}
      <div style={{
        position: 'absolute', bottom: 96, right: -58,
        background: '#1C2433', border: '1px solid rgba(108,99,255,0.35)',
        borderRadius: 10, padding: '9px 13px',
        boxShadow: '0 4px 16px rgba(0,0,0,0.4)',
        fontSize: 12, fontFamily: "'DM Sans',sans-serif", fontWeight: 500,
        color: '#fff', whiteSpace: 'nowrap',
        display: 'flex', alignItems: 'center', gap: 8,
        animation: 'badgePop 0.4s ease 0.8s both',
      }}>
        <span style={{ width: 7, height: 7, borderRadius: '50%', background: '#22C55E', flexShrink: 0, display: 'block' }} />
        Nueva resolución detectada
      </div>
    </div>
  );
};

// ─── Mount ──────────────────────────────────────────────────────────────────
(function mount() {
  const el = document.getElementById('hero-phone-react');
  if (!el) return;
  ReactDOM.createRoot(el).render(<HeroPhone />);

  // Sync HTML dots
  document.addEventListener('ph:change', e => {
    const n = e.detail;
    document.querySelectorAll('.screen-dot').forEach((d, i) => d.classList.toggle('active', i === n));
  });
  document.querySelectorAll('.screen-dot').forEach((d, i) => {
    d.onclick = () => { if (window.goToPhoneScreen) window.goToPhoneScreen(i); };
  });
})();
