// AprendeNegocioDemo — animated mini-tour of the Wapido onboarding flow.
// Auto-cycles 3 screens (Horarios → Categorías → Tono) inside a fake
// product chrome. Designed to live inside the Capacidades / "Aprende tu
// operación" tab on the landing.

const { useState, useEffect, useRef: anUseRef } = React;

// ── timings ────────────────────────────────────────────
const SCREENS = [
{ id: 'horarios', sidebar: 'base', caption: 'Aprendiendo tus horarios…', dur: 6200 },
{ id: 'menu', sidebar: 'menu', caption: 'Cargando tu menú…', dur: 5400 },
{ id: 'tono', sidebar: 'tono', caption: 'Definiendo tu tono de voz…', dur: 6400 }];


// little hook: progress 0→1 over `dur` ms, then advances index
function useScreenCycle() {
  const [idx, setIdx] = useState(0);
  const [p, setP] = useState(0);
  useEffect(() => {
    let raf, start;
    const dur = SCREENS[idx].dur;
    const frame = (now) => {
      if (!start) start = now;
      const pp = Math.min(1, (now - start) / dur);
      setP(pp);
      if (pp < 1) raf = requestAnimationFrame(frame);else
      setIdx((i) => (i + 1) % SCREENS.length);
    };
    raf = requestAnimationFrame(frame);
    return () => cancelAnimationFrame(raf);
  }, [idx]);
  return [idx, p, setIdx];
}

// ── easing + helpers ───────────────────────────────────
const easeOut = (t) => 1 - Math.pow(1 - t, 3);
const clamp = (t, a = 0, b = 1) => Math.max(a, Math.min(b, t));
// remap p from [a,b] to [0,1]
const phase = (p, a, b) => clamp((p - a) / (b - a));

// type a string proportionally to progress (0..1)
function typed(str, p) {
  const n = Math.floor(easeOut(p) * str.length);
  return str.slice(0, n);
}

// ── tiny icons ─────────────────────────────────────────
const Ico = {
  store: (c = 'currentColor') =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 9l1-5h16l1 5" /><path d="M4 9v11h16V9" /><path d="M9 20v-6h6v6" />
    </svg>,

  clip: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <rect x="5" y="4" width="14" height="17" rx="2" /><path d="M9 4h6v3H9z" fill={c} stroke="none" />
    </svg>,

  bowl: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 11h18a8 8 0 0 1-9 8 8 8 0 0 1-9-8z" /><path d="M7 7c0-1 1-2 2-2M12 6c0-1 1-2 2-2M17 7c0-1 1-2 2-2" />
    </svg>,

  chat: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 11.5a8.38 8.38 0 0 1-9 8.5 8.5 8.5 0 0 1-4-1L3 21l1.5-4.5A8.38 8.38 0 0 1 12 3a8.38 8.38 0 0 1 9 8.5z" />
    </svg>,

  brain: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M9 4a3 3 0 0 0-3 3 2.5 2.5 0 0 0-2 4 3 3 0 0 0 2 5 3 3 0 0 0 6 0V4a3 3 0 0 0-3 0z" />
      <path d="M15 4a3 3 0 0 1 3 3 2.5 2.5 0 0 1 2 4 3 3 0 0 1-2 5 3 3 0 0 1-6 0" />
    </svg>,

  fork: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M14 4l6 6-10 10-6-0 0-6z" /><path d="M3 21l3-3" />
    </svg>,

  face: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9" /><path d="M8 14s1.5 2 4 2 4-2 4-2" /><circle cx="9" cy="10" r=".7" fill={c} /><circle cx="15" cy="10" r=".7" fill={c} />
    </svg>,

  bolt: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z" />
    </svg>,

  clock: (c) =>
  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9" /><path d="M12 7v5l3 2" />
    </svg>,

  search: (c) =>
  <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="11" r="7" /><path d="M21 21l-4-4" />
    </svg>,

  chev: (c) =>
  <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 9l6 6 6-6" />
    </svg>,

  plus: (c) =>
  <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 5v14M5 12h14" />
    </svg>

};

const MINT = '#43C98A';
const MINT_DEEP = '#2EA36D';
const MINT_SOFT = '#E8F6EE';

// ── sidebar ────────────────────────────────────────────
function Sidebar({ active }) {
  const items = [
  { id: 'inicio', label: 'Inicio', icon: Ico.store, group: null },
  { id: 'config', label: 'Configuración', icon: Ico.clip, group: null, badge: '3/4' },
  { id: '_AGENT_', label: 'AGENTE', group: 'h' },
  { id: 'pedidos', label: 'Pedidos', icon: Ico.bowl, group: 'agent' },
  { id: 'chat', label: 'Chat', icon: Ico.chat, group: 'agent' },
  { id: '_PERS_', label: 'PERSONALIZA TU AGENTE', group: 'h' },
  { id: 'base', label: 'Base de conocimiento', icon: Ico.brain, group: 'pers' },
  { id: 'menu', label: 'Menú', icon: Ico.fork, group: 'pers' },
  { id: 'tono', label: 'Tono', icon: Ico.face, group: 'pers' },
  { id: 'hab', label: 'Habilidades', icon: Ico.bolt, group: 'pers' }];


  return (
    <aside style={{
      width: 168,
      flexShrink: 0,
      background: '#FFFFFF',
      borderRight: '1px solid #ECEAE5',
      padding: '14px 10px',
      display: 'flex', flexDirection: 'column', gap: 2,
      fontFamily: 'Inter, system-ui, sans-serif',
      fontSize: 11.5
    }}>
      {/* Wapido logo */}
      <div style={{ padding: '4px 6px 14px' }}>
        <span style={{
          fontFamily: 'var(--garamond)',
          fontSize: 19, fontWeight: 500,
          color: MINT, letterSpacing: '-0.01em'
        }}>wapido</span>
      </div>

      {items.map((it) => {
        if (it.group === 'h') {
          return (
            <div key={it.id} style={{
              padding: '12px 8px 4px',
              fontSize: 8.5, letterSpacing: '0.12em',
              color: '#9A9388', fontWeight: 600
            }}>{it.label}</div>);

        }
        const isActive = active === it.id;
        const Icon = it.icon;
        return (
          <div key={it.id} style={{
            display: 'flex', alignItems: 'center', gap: 8,
            padding: '6px 8px',
            borderRadius: 6,
            background: isActive ? MINT_SOFT : 'transparent',
            color: isActive ? MINT_DEEP : '#3A352E',
            fontWeight: isActive ? 600 : 500,
            transition: 'all 0.4s ease',
            position: 'relative'
          }}>
            {Icon(isActive ? MINT_DEEP : '#3A352E')}
            <span style={{ flex: 1, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{it.label}</span>
            {it.badge &&
            <span style={{
              background: '#F5B544', color: '#FFF',
              fontSize: 8.5, fontWeight: 600,
              padding: '1px 5px', borderRadius: 6,
              letterSpacing: '0.02em'
            }}>{it.badge}</span>
            }
          </div>);

      })}

      <div style={{ flex: 1 }} />

      <div style={{
        marginTop: 10,
        padding: '8px 8px', borderTop: '1px solid #ECEAE5',
        display: 'flex', alignItems: 'center', gap: 8,
        color: '#5C5247', fontSize: 11
      }}>
        <span style={{
          width: 14, height: 14, borderRadius: '50%',
          border: '1px solid #C9BEA8', display: 'inline-block'
        }} />
        <span>68 créditos</span>
      </div>
      <div style={{
        padding: '6px 8px', display: 'flex', alignItems: 'center', gap: 8
      }}>
        <span style={{
          width: 22, height: 22, borderRadius: '50%',
          background: MINT_SOFT, color: MINT_DEEP,
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          fontSize: 9, fontWeight: 700
        }}>DE</span>
        <div style={{ lineHeight: 1.15 }}>
          <div style={{ fontSize: 11, fontWeight: 600, color: '#1F1B16' }}>Usuario</div>
          <div style={{ fontSize: 9, color: '#8A7E6E' }}>dev@wapido.com</div>
        </div>
      </div>
    </aside>);

}

// ── icon used by Ico.<x>() above (some are { Ico.brain } as a fn) ──
// Re-export to avoid JSX call form confusion above:
function _IconCall({ render, color }) {return render(color);}

// ── shared bits ────────────────────────────────────────
function Toggle({ on, scale = 1 }) {
  return (
    <span style={{
      display: 'inline-block',
      width: 26 * scale, height: 14 * scale,
      background: on ? MINT : '#D5D0C5',
      borderRadius: 14 * scale,
      position: 'relative',
      transition: 'background 0.32s ease'
    }}>
      <span style={{
        position: 'absolute', top: 2 * scale, left: on ? (26 - 12) * scale - 2 * scale : 2 * scale,
        width: 10 * scale, height: 10 * scale, borderRadius: '50%',
        background: '#FFF',
        transition: 'left 0.32s cubic-bezier(.4,0,.2,1)',
        boxShadow: '0 1px 2px rgba(0,0,0,0.18)'
      }} />
    </span>);

}

function Field({ value, ghost, mono, width = 86, icon, opacity = 1 }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 5,
      width, height: 22, padding: '0 8px',
      border: '1px solid #E5E1D8', borderRadius: 6,
      background: '#FFF',
      fontFamily: mono ? 'var(--mono)' : 'var(--sans)',
      fontSize: 11, color: value ? '#1F1B16' : '#C2BAA8',
      opacity,
      transition: 'opacity 0.3s ease'
    }}>
      <span style={{ flex: 1, whiteSpace: 'nowrap', overflow: 'hidden' }}>{value || ghost}</span>
      {icon}
    </span>);

}

// ── SCREEN A — Horarios ────────────────────────────────
function ScreenHorarios({ p }) {
  const days = ['Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
  // Each day has its own staggered timeline.
  const stagger = 0.085; // delay between rows
  const rowDur = 0.45; // how long each row takes to "fill"

  return (
    <div style={{ padding: '24px 28px', fontFamily: 'Inter, sans-serif' }}>
      <div style={{
        fontFamily: 'Inter, sans-serif',
        fontSize: 20, fontWeight: 700, color: '#1F1B16',
        letterSpacing: '-0.01em', marginBottom: 4
      }}>
        Base de Conocimiento del Agente
      </div>
      <div style={{ fontSize: 11.5, color: '#8A7E6E', marginBottom: 18 }}>
        Administra la información que tu agente utilizará para responder
      </div>

      {/* Collapsed: Datos Generales */}
      <div style={{
        border: '1px solid #E9E5DC',
        borderRadius: 8, padding: '10px 14px',
        background: '#FFF',
        marginBottom: 10,
        display: 'flex', alignItems: 'center', gap: 10
      }}>
        <span style={{
          width: 22, height: 22, borderRadius: 6,
          background: MINT_SOFT, color: MINT_DEEP,
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          flexShrink: 0
        }}>{Ico.store(MINT_DEEP)}</span>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 12, fontWeight: 700, color: '#1F1B16' }}>Datos Generales</div>
          <div style={{ fontSize: 10.5, color: '#8A7E6E' }}>Información básica de tu negocio</div>
        </div>
        {Ico.chev('#8A7E6E')}
      </div>

      {/* Expanded: Horarios */}
      <div style={{
        border: '1.5px solid ' + (p > 0.05 ? MINT : '#E9E5DC'),
        boxShadow: p > 0.05 ? '0 0 0 4px ' + MINT_SOFT : 'none',
        borderRadius: 8, padding: '12px 14px',
        background: '#FFF',
        transition: 'all 0.4s ease'
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 4 }}>
          <span style={{
            width: 22, height: 22, borderRadius: 6,
            background: MINT_SOFT, color: MINT_DEEP,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            flexShrink: 0
          }}>{Ico.clock(MINT_DEEP)}</span>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 12, fontWeight: 700, color: '#1F1B16' }}>Horarios de Operación</div>
            <div style={{ fontSize: 10.5, color: '#8A7E6E' }}>Configura los horarios de apertura y cierre</div>
          </div>
          <span style={{ transform: 'rotate(180deg)', display: 'inline-flex' }}>
            {Ico.chev('#8A7E6E')}
          </span>
        </div>

        <div style={{ marginTop: 10 }}>
          {days.map((d, i) => {
            const t0 = 0.04 + i * stagger;
            const local = clamp((p - t0) / rowDur);
            const togglePhase = clamp(local / 0.35);
            const timesPhase = clamp((local - 0.35) / 0.65);
            const isOn = local > 0.12;
            return (
              <div key={d} style={{
                display: 'grid',
                gridTemplateColumns: '74px 36px 1fr 8px 1fr',
                alignItems: 'center', gap: 8,
                padding: '7px 4px',
                borderBottom: i < days.length - 1 ? '1px solid #F1EEE8' : 'none',
                opacity: 0.35 + 0.65 * local,
                transform: `translateY(${(1 - local) * 4}px)`,
                transition: 'none'
              }}>
                <span style={{ fontSize: 11.5, color: '#1F1B16', fontWeight: 500 }}>{d}</span>
                <Toggle on={isOn} />
                <Field
                  width="100%"
                  value={timesPhase > 0.02 ? typed('08:00 a. m.', timesPhase) : ''}
                  ghost=""
                  icon={Ico.clock('#C2BAA8')}
                  opacity={togglePhase} />
                
                <span style={{ fontSize: 10, color: '#8A7E6E', textAlign: 'center' }}>a</span>
                <Field
                  width="100%"
                  value={timesPhase > 0.5 ? typed('11:59 p. m.', clamp((timesPhase - 0.5) / 0.5)) : ''}
                  ghost=""
                  icon={Ico.clock('#C2BAA8')}
                  opacity={togglePhase} />
                
              </div>);

          })}
        </div>
      </div>
    </div>);

}

// ── SCREEN B — Categorías ──────────────────────────────
function ScreenCategorias({ p }) {
  const rows = [
  { name: 'Papas a la francesa', count: '1 artículo' },
  { name: 'Hamburguesas', count: '4 artículos' },
  { name: 'Bebidas', count: '4 artículos' },
  { name: 'Combos', count: '3 artículos' },
  { name: 'Aderezos disponibles', count: '20 artículos' },
  { name: 'Especialidades', count: '4 artículos' },
  { name: 'Opciones de modif.', count: '0 artículos' }];

  const stagger = 0.085;
  const rowDur = 0.32;

  // Tabs bar at top
  const tabs = ['Disponibilidad', 'Menús', 'Categorías', 'Artículos', 'Modificadores'];
  const activeTab = 2;

  return (
    <div style={{ padding: '18px 28px 24px', fontFamily: 'Inter, sans-serif' }}>
      {/* tabs row */}
      <div style={{
        display: 'flex', gap: 22,
        borderBottom: '1px solid #ECEAE5',
        marginBottom: 18
      }}>
        {tabs.map((t, i) =>
        <div key={t} style={{
          padding: '8px 0',
          borderBottom: i === activeTab ? '2px solid #1F1B16' : '2px solid transparent',
          fontSize: 11.5,
          color: i === activeTab ? '#1F1B16' : '#8A7E6E',
          fontWeight: i === activeTab ? 600 : 500
        }}>{t}</div>
        )}
      </div>

      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        marginBottom: 12
      }}>
        <div style={{
          fontSize: 18, fontWeight: 700, color: '#1F1B16',
          letterSpacing: '-0.01em'
        }}>Categorías</div>
        <button style={{
          background: MINT, color: '#FFF',
          border: 'none', borderRadius: 6,
          padding: '6px 10px', fontSize: 10.5, fontWeight: 600,
          display: 'inline-flex', alignItems: 'center', gap: 4,
          cursor: 'default'
        }}>{Ico.plus('#FFF')} Nueva categoría</button>
      </div>

      {/* search */}
      <div style={{
        border: '1px solid #E5E1D8',
        borderRadius: 6,
        padding: '6px 10px',
        display: 'inline-flex', alignItems: 'center', gap: 6,
        background: '#FFF', marginBottom: 8,
        width: 180
      }}>
        {Ico.search('#9A8E7C')}
        <span style={{ fontSize: 11, color: '#C2BAA8' }}>Buscar…</span>
      </div>

      {/* table head */}
      <div style={{
        display: 'grid',
        gridTemplateColumns: '1.4fr 1.3fr 0.7fr',
        padding: '8px 10px',
        background: '#F6F4EF',
        border: '1px solid #ECEAE5',
        borderBottom: 'none',
        borderRadius: '6px 6px 0 0',
        fontSize: 10, color: '#5C5247', fontWeight: 600
      }}>
        <span>↑ Nombre</span>
        <span>Menús</span>
        <span style={{ textAlign: 'right' }}>Artículos</span>
      </div>

      <div style={{
        border: '1px solid #ECEAE5',
        borderTop: 'none',
        borderRadius: '0 0 6px 6px',
        background: '#FFF',
        overflow: 'hidden'
      }}>
        {rows.map((r, i) => {
          const t0 = 0.08 + i * stagger;
          const local = clamp((p - t0) / rowDur);
          if (local <= 0) {
            return (
              <div key={r.name} style={{
                display: 'grid',
                gridTemplateColumns: '1.4fr 1.3fr 0.7fr',
                padding: '8px 10px',
                borderBottom: i < rows.length - 1 ? '1px solid #F1EEE8' : 'none',
                minHeight: 28
              }}>
                <span style={{ height: 8, background: '#F4F1EB', borderRadius: 4, maxWidth: '60%' }} />
                <span />
                <span />
              </div>);

          }
          return (
            <div key={r.name} style={{
              display: 'grid',
              gridTemplateColumns: '1.4fr 1.3fr 0.7fr',
              padding: '8px 10px',
              borderBottom: i < rows.length - 1 ? '1px solid #F1EEE8' : 'none',
              opacity: local,
              transform: `translateY(${(1 - local) * 4}px)`,
              transition: 'none'
            }}>
              <span style={{ fontSize: 11.5, color: MINT_DEEP, fontWeight: 500 }}>{r.name}</span>
              <span style={{ fontSize: 11, color: '#5C5247' }}>Menu Principal</span>
              <span style={{ fontSize: 11, color: '#5C5247', textAlign: 'right' }}>{r.count}</span>
            </div>);

        })}
      </div>
    </div>);

}

// ── SCREEN C — Tono ────────────────────────────────────
function ScreenTono({ p }) {
  const greetingText = '¡Hola! 👋 Soy Wapi, el asistente de Sabor Casero. ¿Te paso el menú o ya sabes qué se te antoja hoy?';
  const tonos = ['Formal', 'Casual', 'Amigable', 'Divertido'];
  const selectedTono = 2;

  // Phases:
  // 0.00 – 0.20: card "Tono de Voz" expands
  // 0.20 – 0.45: chips appear, "Amigable" highlights at 0.40
  // 0.45 – 0.95: "Al Saludar" message types in

  const expand = phase(p, 0.05, 0.25);
  const chipsIn = phase(p, 0.20, 0.45);
  const greetExpand = phase(p, 0.42, 0.55);
  const typeP = phase(p, 0.50, 0.96);

  return (
    <div style={{ padding: '24px 28px', fontFamily: 'Inter, sans-serif' }}>
      <div style={{
        fontSize: 20, fontWeight: 700, color: '#1F1B16',
        letterSpacing: '-0.01em', marginBottom: 4
      }}>
        Personaliza tu agente
      </div>
      <div style={{ fontSize: 11.5, color: '#8A7E6E', marginBottom: 16 }}>
        Configura el tono de voz y los mensajes de tu agente
      </div>

      {/* Card: Tono de Voz */}
      <div style={{
        border: '1.5px solid ' + (p > 0.02 ? MINT : '#E9E5DC'),
        boxShadow: p > 0.02 ? '0 0 0 4px ' + MINT_SOFT : 'none',
        borderRadius: 8, padding: '12px 14px',
        background: '#FFF',
        marginBottom: 10,
        transition: 'all 0.4s ease'
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <span style={{
            width: 22, height: 22, borderRadius: 6,
            background: MINT_SOFT, color: MINT_DEEP,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center'
          }}>{Ico.chat(MINT_DEEP)}</span>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 12, fontWeight: 700, color: '#1F1B16' }}>Tono de Voz</div>
            <div style={{ fontSize: 10.5, color: '#8A7E6E' }}>Define cómo se comunica tu agente con los clientes</div>
          </div>
          <span style={{
            display: 'inline-flex',
            transform: `rotate(${expand * 180}deg)`,
            transition: 'transform 0.3s ease'
          }}>{Ico.chev('#8A7E6E')}</span>
        </div>

        <div style={{
          maxHeight: expand * 60 + 'px',
          opacity: expand,
          overflow: 'hidden',
          transition: 'none',
          display: 'flex', gap: 6, marginTop: expand > 0.1 ? 12 : 0, flexWrap: 'wrap'
        }}>
          {tonos.map((t, i) => {
            const isOn = i === selectedTono && chipsIn > 0.5;
            const cIn = clamp(chipsIn * 1.3 - i * 0.06);
            return (
              <span key={t} style={{
                padding: '4px 10px',
                fontSize: 10.5, fontWeight: 500,
                border: '1px solid ' + (isOn ? MINT : '#E5E1D8'),
                background: isOn ? MINT_SOFT : '#FFF',
                color: isOn ? MINT_DEEP : '#5C5247',
                borderRadius: 999,
                opacity: cIn,
                transform: `translateY(${(1 - cIn) * 4}px)`,
                display: 'inline-flex', alignItems: 'center', gap: 5
              }}>
                {isOn &&
                <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke={MINT_DEEP} strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
                    <path d="M5 12l5 5L20 7" />
                  </svg>
                }
                {t}
              </span>);

          })}
        </div>
      </div>

      <div style={{
        fontSize: 11.5, color: '#1F1B16', fontWeight: 600,
        marginTop: 14, marginBottom: 4
      }}>Mensajes predeterminados</div>
      <div style={{ fontSize: 10.5, color: '#8A7E6E', marginBottom: 10 }}>
        Textos que envía tu agente en situaciones específicas
      </div>

      {/* Al Saludar — expands and shows typing */}
      <div style={{
        border: '1.5px solid ' + (greetExpand > 0.2 ? MINT : '#E9E5DC'),
        boxShadow: greetExpand > 0.2 ? '0 0 0 4px ' + MINT_SOFT : 'none',
        borderRadius: 8, padding: '12px 14px',
        background: '#FFF',
        marginBottom: 8,
        transition: 'all 0.4s ease'
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <span style={{
            width: 22, height: 22, borderRadius: 6,
            background: MINT_SOFT, color: MINT_DEEP,
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            fontSize: 12
          }}>👋</span>
          <div style={{ flex: 1 }}>
            <div style={{ fontSize: 12, fontWeight: 700, color: '#1F1B16' }}>Al Saludar</div>
            <div style={{ fontSize: 10.5, color: '#8A7E6E' }}>Mensajes de bienvenida para clientes nuevos y recurrentes</div>
          </div>
          <span style={{
            display: 'inline-flex',
            transform: `rotate(${greetExpand * 180}deg)`,
            transition: 'transform 0.3s ease'
          }}>{Ico.chev('#8A7E6E')}</span>
        </div>

        <div style={{
          maxHeight: greetExpand * 70 + 'px',
          opacity: greetExpand,
          overflow: 'hidden',
          marginTop: greetExpand > 0.1 ? 10 : 0
        }}>
          <div style={{
            border: '1px solid #E5E1D8', borderRadius: 6,
            padding: '8px 10px',
            background: '#FCFBF7',
            fontSize: 11.5, lineHeight: 1.45,
            color: '#1F1B16',
            minHeight: 42,
            position: 'relative'
          }}>
            {typed(greetingText, typeP)}
            {typeP < 1 && typeP > 0 &&
            <span style={{
              display: 'inline-block', width: 1, height: 12,
              background: '#1F1B16', marginLeft: 1, marginBottom: -2,
              animation: 'wapidoCaret 0.7s steps(2) infinite'
            }} />
            }
          </div>
        </div>
      </div>

      {/* Collapsed bonus row */}
      <div style={{
        border: '1px solid #E9E5DC',
        borderRadius: 8, padding: '10px 14px',
        background: '#FFF',
        opacity: 0.85,
        display: 'flex', alignItems: 'center', gap: 10
      }}>
        <span style={{
          width: 22, height: 22, borderRadius: 6,
          background: MINT_SOFT, color: MINT_DEEP,
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center', fontSize: 12
        }}>🚫</span>
        <div style={{ flex: 1 }}>
          <div style={{ fontSize: 11.5, fontWeight: 700, color: '#1F1B16' }}>Cuando no estés disponible</div>
          <div style={{ fontSize: 10, color: '#8A7E6E' }}>Mensajes para cuando la tienda está cerrada o pausada</div>
        </div>
        {Ico.chev('#8A7E6E')}
      </div>
    </div>);

}

// ── main demo ──────────────────────────────────────────
function AprendeNegocioDemo() {
  const [idx, p, setIdx] = useScreenCycle();
  const screen = SCREENS[idx];

  // Render the proper inner screen with cross-fade between
  const innerByIdx = (i, pp) => {
    if (i === 0) return <ScreenHorarios p={pp} />;
    if (i === 1) return <ScreenCategorias p={pp} />;
    return <ScreenTono p={pp} />;
  };

  // small enter/exit transition
  const enterP = clamp(p / 0.10);
  const exitP = 1 - clamp((p - 0.94) / 0.06);
  const fade = Math.min(enterP, exitP);

  return (
    <div style={{
      flex: 1,
      width: '100%',
      minHeight: 540,
      margin: '0 auto',
      borderRadius: 14,
      overflow: 'hidden',
      background: '#F7F6F2',
      border: '1px solid #E5E1D8',
      boxShadow: '0 24px 60px -28px rgba(31,27,22,0.32), 0 4px 12px -6px rgba(31,27,22,0.10)',
      fontFamily: 'Inter, system-ui, sans-serif',
      position: 'relative',
      display: 'flex', flexDirection: 'column'
    }}>
      {/* browser-y top bar */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 8,
        padding: '8px 12px',
        background: '#FFFFFF',
        borderBottom: '1px solid #ECEAE5'
      }}>
        <span style={{ width: 9, height: 9, borderRadius: '50%', background: '#FF6058' }} />
        <span style={{ width: 9, height: 9, borderRadius: '50%', background: '#FFBD2E' }} />
        <span style={{ width: 9, height: 9, borderRadius: '50%', background: '#27C940' }} />
        <div style={{
          marginLeft: 14, flex: 1,
          background: '#F4F1EB', border: '1px solid #ECEAE5',
          borderRadius: 6,
          padding: '3px 10px',
          fontSize: 10.5, color: '#8A7E6E',
          fontFamily: 'var(--mono)',
          letterSpacing: '0.02em',
          display: 'flex', alignItems: 'center', gap: 6,
          maxWidth: 320
        }}>
          <span style={{ color: MINT_DEEP, fontWeight: 600 }}>app.wapido.ai</span>
          <span style={{ color: '#C2BAA8' }}>/{screen.id}</span>
        </div>
        {/* caption pill */}
        <div style={{
          background: MINT_SOFT, color: MINT_DEEP,
          borderRadius: 999,
          padding: '4px 10px',
          fontSize: 10, fontWeight: 600,
          display: 'inline-flex', alignItems: 'center', gap: 6
        }}>
          <span style={{
            width: 6, height: 6, borderRadius: '50%', background: MINT_DEEP,
            display: 'inline-block',
            animation: 'wapidoCapPulse 1.2s ease-out infinite'
          }} />
          {screen.caption}
        </div>
      </div>

      <div style={{
        flex: 1,
        display: 'flex',
        background: '#F7F6F2',
        minHeight: 0,
        overflow: 'hidden'
      }}>
        <Sidebar active={screen.sidebar} />
        <div style={{
          flex: 1,
          opacity: fade,
          transform: `translateY(${(1 - fade) * 6}px)`,
          transition: 'none',
          position: 'relative',
          minWidth: 0,
          overflow: 'hidden'
        }}>
          {innerByIdx(idx, p)}
        </div>
      </div>

      {/* progress dots */}
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
        padding: '10px 12px',
        background: '#FFFFFF',
        borderTop: '1px solid #ECEAE5'
      }}>
        {SCREENS.map((s, i) => {
          const isActive = i === idx;
          return (
            <button
              key={s.id}
              onClick={() => setIdx(i)}
              style={{
                position: 'relative',
                width: isActive ? 46 : 8,
                height: 6,
                borderRadius: 999,
                background: isActive ? '#ECEAE5' : '#ECEAE5',
                border: 'none',
                overflow: 'hidden',
                transition: 'width 0.4s ease',
                cursor: 'pointer',
                padding: 0
              }}
              aria-label={s.caption}>
              
              {isActive &&
              <span style={{
                position: 'absolute', inset: 0,
                width: p * 100 + '%',
                background: MINT,
                borderRadius: 999,
                transition: 'none'
              }} />
              }
            </button>);

        })}
        <span style={{
          marginLeft: 8, fontSize: 10, color: '#8A7E6E',
          fontFamily: 'var(--mono)',
          letterSpacing: '0.06em', textTransform: 'uppercase'
        }}>
          {idx + 1}/{SCREENS.length}
        </span>
      </div>

      <style>{`
        @keyframes wapidoCaret {
          0%, 100% { opacity: 1; }
          50% { opacity: 0; }
        }
        @keyframes wapidoCapPulse {
          0% { box-shadow: 0 0 0 0 rgba(46,163,109,0.5); }
          70% { box-shadow: 0 0 0 6px rgba(46,163,109,0); }
          100% { box-shadow: 0 0 0 0 rgba(46,163,109,0); }
        }
      `}</style>
    </div>);

}

// Expose globally so V4App.jsx can use it
window.AprendeNegocioDemo = AprendeNegocioDemo;