// ui_kits/mobile/InsightsTab.jsx
// Insights AI hub with Money/Learn-style top nav.
// Quick chat lives above the bottom nav as a persistent compose bar.

const { useState: useStateIT, useRef: useRefIT, useEffect: useEffectIT } = React;

const INSIGHTS_PAGES = [
  { key: 'overview', label: 'Overview' },
  { key: 'spending', label: 'Spending' },
  { key: 'coach',    label: 'AI Coach' },
];

function InsightsAITabContainer({ hidden, onToggleHidden, onOpenZen }) {
  const [page, setPage] = useStateIT(0);
  const isLanding = page === 0;
  const currentTitle = INSIGHTS_PAGES[page].label;

  // Compose state — collapsed shows the input only; expanded reveals a chat panel.
  const [composeOpen, setComposeOpen] = useStateIT(false);
  const [messages, setMessages] = useStateIT([
    { role: 'goji', text: "Salary lands Friday — you're $214 ahead of last week." },
  ]);
  const [draft, setDraft] = useStateIT('');
  const [processing, setProcessing] = useStateIT(false);
  // Broadcast chat open state so the bottom nav can shrink in response.
  useEffectIT(() => {
    window.dispatchEvent(new CustomEvent('goji-chat-open', { detail: composeOpen }));
    return () => {
      window.dispatchEvent(new CustomEvent('goji-chat-open', { detail: false }));
    };
  }, [composeOpen]);
  const send = () => {
    if (!draft.trim()) return;
    const userMsg = { role: 'user', text: draft.trim() };
    setMessages(m => [...m, userMsg]);
    setDraft('');
    setProcessing(true);
    setTimeout(() => {
      setMessages(m => [...m, { role: 'goji', text: 'After Rent and Comcast, you have $1,128 left for the month — $400 home keeps $728 for spending.' }]);
      setProcessing(false);
    }, 1600);
  };

  return (
    <div style={{ flex: 1, minHeight: 0, display: 'flex', flexDirection: 'column', position: 'relative' }}>
      {/* Crossfading header */}
      <div style={{ position: 'relative', height: 48, flexShrink: 0 }}>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isLanding ? 1 : 0,
          transform: isLanding ? 'translateY(0)' : 'translateY(-4px)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isLanding ? 'auto' : 'none',
        }}>
          <div style={{
            display: 'flex', alignItems: 'center',
            padding: '10px 12px 10px 20px', height: 48,
          }}>
            <span style={{
              fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 14,
              letterSpacing: '-0.01em', color: 'var(--color-text-primary)',
            }}>Insights AI</span>
            <button
              onClick={() => setPage(1)}
              aria-label={`More — ${INSIGHTS_PAGES[1].label}`}
              style={{
                marginLeft: 10,
                display: 'inline-flex', alignItems: 'center', gap: 5,
                height: 22, padding: '0 8px',
                borderRadius: 9999,
                background: 'transparent',
                border: '1px solid var(--color-border)',
                cursor: 'pointer',
                fontFamily: 'var(--font-sans)', fontSize: 10, fontWeight: 500,
                letterSpacing: '0.06em',
                color: 'var(--color-text-muted)',
                fontVariantNumeric: 'tabular-nums',
                fontFeatureSettings: '"tnum" 1',
              }}>
              1 / {INSIGHTS_PAGES.length}
              <span style={{
                display: 'inline-grid', placeItems: 'center',
                animation: 'goji-peek-right 2.2s var(--ease-smooth) infinite',
              }}>
                <Icon name="chevronRight" size={12} />
              </span>
            </button>
            <div style={{ marginLeft: 'auto' }}>
              <HeaderActions hidden={hidden} onToggleHidden={onToggleHidden} />
            </div>
          </div>
        </div>
        <div style={{
          position: 'absolute', inset: 0,
          opacity: isLanding ? 0 : 1,
          transform: isLanding ? 'translateY(4px)' : 'translateY(0)',
          transition: 'opacity 280ms var(--ease-smooth), transform 320ms var(--ease-smooth)',
          pointerEvents: isLanding ? 'none' : 'auto',
        }}>
          <TopNavHeader
            title={currentTitle}
            onBack={() => setPage(0)}
            hidden={hidden}
            onToggleHidden={onToggleHidden}
          />
        </div>
      </div>

      {/* Page dots */}
      <div style={{
        height: 21,
        opacity: isLanding ? 0 : 1,
        transition: 'opacity 220ms var(--ease-smooth)',
        flexShrink: 0,
      }}>
        <PageDots total={INSIGHTS_PAGES.length} active={page} onJump={setPage} />
      </div>

      <SwipePager page={page} onPageChange={setPage}>
        <AIOverview />
        <AISpending />
        <AICoachLanding onOpenZen={onOpenZen} />
      </SwipePager>

      {/* Persistent compose bar — floats above the bottom nav.
          On the AI Coach sub-page it gives way: the FAB mic + the
          page itself are the coach entrypoints. */}
      {page !== 2 && (
        <ChatDock
          open={composeOpen}
          onOpen={() => setComposeOpen(true)}
          onClose={() => setComposeOpen(false)}
          messages={messages}
          draft={draft}
          setDraft={setDraft}
          onSend={send}
          processing={processing}
        />
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// ChatDock — collapsed pill OR expanded chat sheet.
// Sits above the bottom nav (which is ~92 px tall). Pointer-events
// confined to itself so the FAB and nav still work normally.
// ─────────────────────────────────────────────────────────────
function ChatComposeIcon({ size = 18, processing = false }) {
  return (
    <svg viewBox="0 0 56 20" width={size * 1.6} height={size * 0.6}
      style={{ flexShrink: 0, overflow: 'visible' }}>
      <circle cx="17" cy="10" r="3.5" fill="#E3B788"
        style={{ animation: processing ? 'goji-wave-dot 1100ms ease-in-out infinite' : 'none' }} />
      <circle cx="28" cy="10" r="3.5" fill="#E0876A"
        style={{ animation: processing ? 'goji-wave-dot 1100ms ease-in-out 180ms infinite' : 'none' }} />
      <circle cx="39" cy="10" r="3.5" fill="#9B7BD6"
        style={{ animation: processing ? 'goji-wave-dot 1100ms ease-in-out 360ms infinite' : 'none' }} />
    </svg>
  );
}

function ChatDock({ open, onOpen, onClose, messages, draft, setDraft, onSend, processing }) {
  const inputRef = useRefIT(null);
  const scrollerRef = useRefIT(null);
  useEffectIT(() => {
    if (open && inputRef.current) inputRef.current.focus();
  }, [open]);
  useEffectIT(() => {
    if (open && scrollerRef.current) {
      scrollerRef.current.scrollTop = scrollerRef.current.scrollHeight;
    }
  }, [messages, open]);

  return (
    <>
      {/* Backdrop when expanded — taps outside to close */}
      <div
        onClick={onClose}
        style={{
          position: 'absolute', inset: 0, zIndex: 40,
          background: open ? 'rgba(0,0,0,0.35)' : 'transparent',
          opacity: open ? 1 : 0,
          pointerEvents: open ? 'auto' : 'none',
          transition: 'opacity var(--dur-normal) var(--ease-smooth)',
        }}
      />

      {/* Gradient fade above the compose — sits in the zone between the
          last scrollable card and the compose pill. Content scrolling up
          fades into bg BEFORE it can reach the compose, even mid-scroll. */}
      <div style={{
        position: 'absolute', left: 0, right: 0,
        bottom: 148,   // top edge of the compose pill
        height: 64,
        background: 'linear-gradient(180deg, transparent 0%, var(--color-bg) 100%)',
        pointerEvents: 'none',
        zIndex: 44,
        opacity: open ? 0 : 1,
        transition: 'opacity 280ms var(--ease-smooth)',
      }} />

      {/* Solid bg backdrop directly behind the compose so anything visible
          through transparent regions of the pill resolves to bg, not card. */}
      <div style={{
        position: 'absolute', left: 0, right: 0,
        bottom: 92,
        height: 60,
        background: 'var(--color-bg)',
        pointerEvents: 'none',
        zIndex: 44,
        opacity: open ? 0 : 1,
        transition: 'opacity 280ms var(--ease-smooth)',
      }} />

      <div
        style={{
          position: 'absolute',
          left: 16,
          right: open ? 80 : 16,  // when open, clear the FAB corner
          bottom: open ? 18 : 96,
          zIndex: 45,
          display: 'flex', flexDirection: 'column',
          pointerEvents: 'none',
          transition: 'bottom 360ms var(--ease-smooth), right 360ms var(--ease-smooth)',
        }}
      >
        {/* Expanded chat panel — grows above the input */}
        <div
          ref={scrollerRef}
          style={{
            maxHeight: open ? 800 : 0,
            opacity: open ? 1 : 0,
            overflow: 'hidden auto',
            transition: 'max-height 380ms var(--ease-smooth), opacity 280ms var(--ease-smooth), margin 280ms var(--ease-smooth)',
            marginBottom: open ? 10 : 0,
            marginRight: open ? -64 : 0,
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border-accent)',
            borderRadius: 'var(--radius-card)',
            boxShadow: 'var(--shadow-glass)',
            padding: open ? '16px 18px' : '0 16px',
            pointerEvents: open ? 'auto' : 'none',
            display: 'flex', flexDirection: 'column', gap: 12,
          }}
        >
          {/* Header inside the panel */}
          <div style={{
            display: 'flex', alignItems: 'center', justifyContent: 'space-between',
            paddingBottom: 8, borderBottom: '1px solid var(--color-border-subtle)',
          }}>
            <div style={{
              display: 'flex', alignItems: 'center', gap: 8,
              fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
              letterSpacing: '0.12em', textTransform: 'uppercase',
              color: 'var(--goji-accent)',
            }}>
              <span style={{ width: 6, height: 6, borderRadius: 999, background: 'var(--goji-accent)' }} />
              Ask Goji
            </div>
            <button onClick={onClose} aria-label="Close"
              style={{
                width: 26, height: 26, borderRadius: 999,
                background: 'transparent', border: 'none', cursor: 'pointer',
                color: 'var(--color-text-muted)',
                display: 'grid', placeItems: 'center',
              }}>
              <Icon name="close" size={12} />
            </button>
          </div>

          {/* Messages */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
            {messages.map((m, i) => (
              <div key={i} style={{
                alignSelf: m.role === 'user' ? 'flex-end' : 'flex-start',
                maxWidth: '85%',
                padding: '10px 14px',
                borderRadius: 16,
                fontFamily: 'var(--font-sans)', fontSize: 14, lineHeight: 1.5,
                background: m.role === 'user'
                  ? 'var(--goji-gradient)'
                  : 'var(--color-surface-2)',
                color: m.role === 'user' ? 'var(--goji-btn-text)' : 'var(--color-text-primary)',
                border: m.role === 'user' ? 'none' : '1px solid var(--color-border)',
                boxShadow: m.role === 'user' ? '0 2px 10px rgba(193,160,129,0.30)' : 'none',
              }}>
                {m.text}
              </div>
            ))}
          </div>

          {/* Suggested prompts — show only when chat is short */}
          {messages.length < 3 && (
            <div style={{
              display: 'flex', flexWrap: 'wrap', gap: 6, paddingTop: 4,
            }}>
              {['Can I afford $400 home?', 'Where did money go?', 'How am I doing this month?'].map(q => (
                <button key={q}
                  onClick={() => { setDraft(q); }}
                  style={{
                    height: 28, padding: '0 10px', borderRadius: 9999,
                    background: 'var(--color-input-bg)', border: '1px solid var(--color-border)',
                    color: 'var(--color-text-body)', cursor: 'pointer',
                    fontFamily: 'var(--font-sans)', fontSize: 11,
                  }}>{q}</button>
              ))}
            </div>
          )}
        </div>

        {/* Compose pill — always visible */}
        <div
          onClick={() => !open && onOpen()}
          style={{
            display: 'flex', alignItems: 'center', gap: 8,
            padding: '6px 6px 6px 16px',
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border-accent)',
            borderRadius: 9999,
            boxShadow: 'var(--shadow-premium)',
            cursor: 'text',
            pointerEvents: 'auto',
          }}
        >
          <ChatComposeIcon size={20} processing={processing} />
          <input
            ref={inputRef}
            value={draft}
            onChange={(e) => setDraft(e.target.value)}
            onKeyDown={(e) => { if (e.key === 'Enter') onSend(); }}
            placeholder={open ? 'Type a question…' : 'Ask Goji about your money…'}
            style={{
              flex: 1, minWidth: 0,
              background: 'transparent', border: 'none', outline: 'none',
              fontFamily: 'var(--font-sans)', fontSize: 13,
              color: 'var(--color-text-primary)',
              padding: '8px 0',
            }}
          />
          <button
            aria-label="Send"
            onClick={(e) => { e.stopPropagation(); onSend(); }}
            disabled={!draft.trim()}
            style={{
              width: 32, height: 32, borderRadius: 999,
              background: draft.trim() ? 'var(--goji-gradient)' : 'var(--color-input-bg)',
              border: 'none',
              cursor: draft.trim() ? 'pointer' : 'default',
              color: draft.trim() ? 'var(--goji-btn-text)' : 'var(--color-text-muted)',
              display: 'grid', placeItems: 'center', flexShrink: 0,
              transition: 'background var(--dur-fast) var(--ease-smooth)',
            }}>
            <Icon name="arrowRight" size={14} stroke={2.25} />
          </button>
        </div>
      </div>
    </>
  );
}

// AICoachLanding — sub-page is the coach launcher. No inline compose;
// the FAB mic + the suggestion/thread rows here are the entry points.
function AICoachLanding({ onOpenZen }) {
  const open = onOpenZen || (() => {});
  const suggestions = [
    'Can I afford to send $400 home this month?',
    "What's a safe amount to save toward my trip?",
    'Show me my biggest expenses this year.',
    'How can I reduce my subscription spend?',
  ];
  const threads = [
    { q: 'Can I afford to send $400 home this month?', when: '2d ago' },
    { q: 'Why did I spend more last Friday?',          when: '4d ago' },
    { q: 'Is Spotify Family worth it?',                when: '1w ago' },
  ];
  return (
    <Page extraBottom={20}>
      {/* Hero — calm entry to the coach space */}
      <div style={{ padding: '4px 20px 0' }}>
        <div style={{
          background: 'var(--color-surface-2)',
          border: '1px solid var(--color-border-accent)',
          borderRadius: 'var(--radius-card)',
          padding: '22px',
          boxShadow: 'var(--shadow-premium)',
          position: 'relative', overflow: 'hidden',
        }}>
          <div style={{
            position: 'absolute', top: -50, right: -50, width: 180, height: 180,
            background: 'radial-gradient(circle, var(--color-glow-subtle) 0%, transparent 70%)',
            pointerEvents: 'none',
          }} />
          <div style={{
            position: 'relative',
            display: 'flex', alignItems: 'center', gap: 8,
            fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
            letterSpacing: '0.16em', textTransform: 'uppercase',
            color: 'var(--goji-accent)',
          }}>
            <Icon name="sparkle" size={14} />
            AI Coach
          </div>
          <h2 style={{
            position: 'relative',
            margin: '10px 0 8px', fontFamily: 'var(--font-serif)', fontWeight: 600,
            fontSize: 22, letterSpacing: '-0.02em', color: 'var(--color-text-primary)',
            lineHeight: 1.2,
          }}>Your coach space</h2>
          <p style={{
            position: 'relative',
            margin: 0, fontFamily: 'var(--font-sans)', fontSize: 13,
            color: 'var(--color-text-body)', lineHeight: 1.5,
          }}>A slower conversation for the harder money questions. Tap the mic, pick a thread to resume, or start from one of the prompts below.</p>
          <button
            onClick={() => open()}
            style={{
              marginTop: 14, height: 44, padding: '0 20px', borderRadius: 9999,
              background: 'var(--goji-gradient)', border: 'none', cursor: 'pointer',
              color: 'var(--goji-btn-text)',
              fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 600,
              display: 'inline-flex', alignItems: 'center', gap: 8,
              boxShadow: 'var(--shadow-fab)',
            }}>
            Open coach space <Icon name="arrowRight" size={15} stroke={2.25} />
          </button>
        </div>
      </div>

      <SectionTitle>Start with a question</SectionTitle>
      <div style={{ padding: '0 20px', display: 'flex', flexDirection: 'column', gap: 8 }}>
        {suggestions.map(q => (
          <button key={q}
            onClick={() => open()}
            style={{
              textAlign: 'left',
              background: 'var(--color-input-bg)', border: '1px solid var(--color-border)',
              borderRadius: 'var(--radius-input)',
              padding: '14px 16px',
              fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--color-text-body)',
              cursor: 'pointer', display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12,
            }}>
            <span>{q}</span>
            <Icon name="arrowRight" size={14} style={{ color: 'var(--goji-accent)', flexShrink: 0 }} />
          </button>
        ))}
      </div>

      <SectionTitle>Recent threads</SectionTitle>
      <div style={{
        margin: '0 20px',
        background: 'var(--color-surface)',
        border: '1px solid var(--color-border)',
        borderRadius: 'var(--radius-card)',
        boxShadow: 'var(--shadow-premium)',
        overflow: 'hidden',
      }}>
        {threads.map((t, i, arr) => (
          <div key={t.q}
            onClick={() => open()}
            style={{
              padding: '14px 18px',
              borderBottom: i < arr.length - 1 ? '1px solid var(--color-border-subtle)' : 'none',
              display: 'grid', gridTemplateColumns: '1fr auto', gap: 12, alignItems: 'center',
              cursor: 'pointer',
            }}>
            <div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 14, fontWeight: 500,
                color: 'var(--color-text-primary)', lineHeight: 1.35,
              }}>{t.q}</div>
              <div style={{
                fontFamily: 'var(--font-sans)', fontSize: 11, color: 'var(--color-text-muted)',
                marginTop: 4,
              }}>{t.when}</div>
            </div>
            <Icon name="chevronRight" size={14} style={{ color: 'var(--color-text-muted)' }} />
          </div>
        ))}
      </div>
    </Page>
  );
}

// ─────────────────────────────────────────────────────────────
// ZenChat — full-screen AI coach. Launched from the FAB on Insights AI.
// No bottom nav, no chrome — calm immersive conversation.
// ─────────────────────────────────────────────────────────────
function ZenChat({ open, onClose }) {
  const [messages, setMessages] = useStateIT([
    { role: 'goji', text: "Welcome to your coach space. What's on your mind about money?" },
  ]);
  const [draft, setDraft] = useStateIT('');
  const [processing, setProcessing] = useStateIT(false);
  const scrollRef = useRefIT(null);
  useEffectIT(() => {
    if (open && scrollRef.current) {
      scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
    }
  }, [messages, processing, open]);

  const send = () => {
    if (!draft.trim()) return;
    setMessages(m => [...m, { role: 'user', text: draft.trim() }]);
    setDraft('');
    setProcessing(true);
    setTimeout(() => {
      setMessages(m => [...m, {
        role: 'goji',
        text: "Let's think about it together. What feels most important to you about this — clarity, freedom, or staying ahead?"
      }]);
      setProcessing(false);
    }, 1600);
  };

  const SUGGESTIONS = [
    "I'm worried about my savings rate.",
    "How do I balance sending home and saving?",
    'Plan a 3-month spending experiment.',
  ];

  return (
    <div style={{
      position: 'absolute', inset: 0, zIndex: 90,
      background: 'var(--color-bg)',
      opacity: open ? 1 : 0,
      transform: open ? 'scale(1)' : 'scale(0.97)',
      pointerEvents: open ? 'auto' : 'none',
      transition: 'opacity 320ms var(--ease-smooth), transform 360ms var(--ease-smooth)',
      display: 'flex', flexDirection: 'column',
      paddingTop: 56,
    }}>
      {/* Minimal header — just close + title */}
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '10px 16px', height: 48,
      }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 8,
          fontFamily: 'var(--font-sans)', fontSize: 11, fontWeight: 500,
          letterSpacing: '0.16em', textTransform: 'uppercase',
          color: 'var(--goji-accent)',
        }}>
          <Icon name="sparkle" size={14} />
          AI Coach
        </div>
        <button onClick={onClose} aria-label="Close"
          style={{
            width: 32, height: 32, borderRadius: 999,
            background: 'var(--color-input-bg)',
            border: '1px solid var(--color-border)',
            cursor: 'pointer', color: 'var(--color-text-body)',
            display: 'grid', placeItems: 'center',
          }}>
          <Icon name="close" size={14} />
        </button>
      </div>

      {/* Hero — calming prompt */}
      <div style={{ padding: '8px 24px 0' }}>
        <h1 style={{
          margin: 0,
          fontFamily: 'var(--font-serif)', fontWeight: 600,
          fontSize: 26, lineHeight: 1.2, letterSpacing: '-0.02em',
          color: 'var(--color-text-primary)',
          textWrap: 'pretty',
        }}>Let's talk it through.</h1>
        <p style={{
          margin: '6px 0 0', fontFamily: 'var(--font-sans)', fontSize: 13,
          color: 'var(--color-text-muted)', lineHeight: 1.5,
        }}>Slow conversation. No pressure. I'll keep notes on what we figure out together.</p>
      </div>

      {/* Conversation */}
      <div ref={scrollRef} style={{
        flex: 1, minHeight: 0, overflowY: 'auto',
        padding: '24px 20px 24px',
        display: 'flex', flexDirection: 'column', gap: 10,
      }}>
        {messages.map((m, i) => (
          <div key={i} style={{
            alignSelf: m.role === 'user' ? 'flex-end' : 'flex-start',
            maxWidth: '88%',
            padding: '12px 16px',
            borderRadius: 18,
            fontFamily: 'var(--font-sans)',
            fontWeight: 400,
            fontSize: 14,
            lineHeight: 1.5,
            letterSpacing: 0,
            background: m.role === 'user'
              ? 'var(--goji-gradient)'
              : 'var(--color-surface)',
            color: m.role === 'user' ? 'var(--goji-btn-text)' : 'var(--color-text-primary)',
            border: m.role === 'user' ? 'none' : '1px solid var(--color-border)',
            boxShadow: m.role === 'user'
              ? '0 2px 14px rgba(193,160,129,0.30)'
              : 'var(--shadow-premium)',
          }}>
            {m.text}
          </div>
        ))}

        {processing && (
          <div style={{
            alignSelf: 'flex-start',
            padding: '14px 18px',
            borderRadius: 18,
            background: 'var(--color-surface)',
            border: '1px solid var(--color-border)',
            boxShadow: 'var(--shadow-premium)',
            display: 'inline-flex', alignItems: 'center',
          }}>
            <ChatComposeIcon size={22} processing />
          </div>
        )}

        {messages.length < 3 && !processing && (
          <div style={{ display: 'flex', flexDirection: 'column', gap: 8, marginTop: 6 }}>
            {SUGGESTIONS.map(s => (
              <button key={s}
                onClick={() => setDraft(s)}
                style={{
                  alignSelf: 'flex-start',
                  background: 'var(--color-input-bg)', border: '1px solid var(--color-border)',
                  borderRadius: 9999, padding: '10px 16px',
                  fontFamily: 'var(--font-sans)', fontSize: 13,
                  color: 'var(--color-text-body)', cursor: 'pointer',
                }}>{s}</button>
            ))}
          </div>
        )}
      </div>

      {/* Compose */}
      <div style={{
        padding: '0 16px 24px',
        display: 'flex', gap: 8, alignItems: 'center',
      }}>
        <div style={{
          flex: 1,
          display: 'flex', alignItems: 'center', gap: 8,
          padding: '6px 6px 6px 18px',
          background: 'var(--color-surface)',
          border: '1px solid var(--color-border-accent)',
          borderRadius: 9999,
          boxShadow: 'var(--shadow-premium)',
        }}>
          <input
            value={draft}
            onChange={(e) => setDraft(e.target.value)}
            onKeyDown={(e) => { if (e.key === 'Enter') send(); }}
            placeholder="Take your time…"
            style={{
              flex: 1, minWidth: 0,
              background: 'transparent', border: 'none', outline: 'none',
              fontFamily: 'var(--font-sans)', fontSize: 14,
              color: 'var(--color-text-primary)',
              padding: '8px 0',
            }}
          />
          <button
            aria-label="Send"
            onClick={send}
            disabled={!draft.trim()}
            style={{
              width: 36, height: 36, borderRadius: 999,
              background: draft.trim() ? 'var(--goji-gradient)' : 'var(--color-input-bg)',
              border: 'none',
              cursor: draft.trim() ? 'pointer' : 'default',
              color: draft.trim() ? 'var(--goji-btn-text)' : 'var(--color-text-muted)',
              display: 'grid', placeItems: 'center', flexShrink: 0,
              transition: 'background var(--dur-fast) var(--ease-smooth)',
            }}>
            <Icon name="arrowRight" size={15} stroke={2.25} />
          </button>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, {
  InsightsAITabContainer, ChatDock, AICoachLanding, ZenChat,
});
