// Shared UI primitives for the Aewita demo video.
// Exposes AewitaShell (the app chrome), Cursor, and scene helpers.

const COLORS = {
  bgBase: '#0E0F10',          // deepest background
  bgRaised: '#16181A',        // panels/cards
  bgCard: '#1C1F22',          // raised cards
  bgSunken: '#0A0B0C',        // input fields
  border: 'rgba(232,224,216,0.08)',
  borderStrong: 'rgba(232,224,216,0.14)',
  textPrimary: '#E8E0D8',     // warm cream
  textSecondary: '#9C968F',   // warm gray
  textMuted: '#64605B',
  accent: '#D9484E',          // Aewita red
  copper: '#C8856B',          // salmon/copper
  copperDim: '#7A4130',
  sage: '#78B37C',
};

const FONTS = {
  sans: '"Inter", "DM Sans", ui-sans-serif, -apple-system, BlinkMacSystemFont, sans-serif',
  serif: '"Cormorant Garamond", "DM Serif Display", Georgia, serif',
  mono: '"JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace',
};

// ─────────────────────────────────────────────────────────────────────────────
// Aewita quill logomark — uses the real asset
function AewitaQuill({ size = 24, pulse = false, time = 0, rounded = true }) {
  const pulseOpacity = pulse ? 0.82 + 0.18 * Math.sin(time * 3) : 1;
  return (
    <img
      src="assets/aewita-quill.png"
      alt=""
      width={size} height={size}
      style={{
        opacity: pulseOpacity,
        borderRadius: rounded ? size * 0.22 : 0,
        display: 'block',
        filter: pulse ? `drop-shadow(0 0 ${size * 0.2}px rgba(217,72,78,${0.15 + 0.15 * Math.sin(time * 3)}))` : 'none',
      }}
    />
  );
}

// ÆWITA wordmark — lapidary serif, small caps style. Uses the Æ ligature.
function AewitaWordmark({ size = 20, color, letterSpacing = '0.18em', weight = 500 }) {
  return (
    <div style={{
      fontFamily: '"Cormorant Garamond", "Trajan Pro", "DM Serif Display", Georgia, serif',
      fontSize: size,
      fontWeight: weight,
      color: color || COLORS.textPrimary,
      letterSpacing,
      lineHeight: 1,
      textTransform: 'uppercase',
      fontFeatureSettings: '"liga" 1, "dlig" 1',
      whiteSpace: 'nowrap',
    }}>
      ÆWITA
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Sidebar nav item
function NavItem({ icon, label, tagline, active, expanded = true }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 12,
      padding: '10px 12px',
      borderRadius: 8,
      background: active ? 'rgba(217,72,78,0.10)' : 'transparent',
      color: active ? COLORS.accent : COLORS.textSecondary,
      position: 'relative',
      transition: 'background 200ms, color 200ms',
    }}>
      <div style={{ width: 18, height: 18, flexShrink: 0, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
        {icon}
      </div>
      {expanded && (
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13, fontWeight: 500, lineHeight: 1.2 }}>{label}</div>
          {tagline && <div style={{ fontSize: 11, color: active ? 'rgba(217,72,78,0.7)' : COLORS.textMuted, marginTop: 2, lineHeight: 1.2 }}>{tagline}</div>}
        </div>
      )}
      {active && expanded && (
        <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
          <path d="M4 2l4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      )}
    </div>
  );
}

// Icons (custom-drawn, simple line icons — not from lucide)
const Icons = {
  library: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M3 3v12M6 3v12M9 3v12M13 4l3 11M14.5 4.5L16 4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  chat: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M3 5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H8l-3 3v-3H5a2 2 0 0 1-2-2V5z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  pen: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M12 3l3 3-8 8H4v-3l8-8z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  shield: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M9 2l6 2v5c0 4-3 6-6 7-3-1-6-3-6-7V4l6-2z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  folder: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M2 5a1 1 0 0 1 1-1h4l2 2h6a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V5z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/></svg>,
  search: <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><circle cx="6" cy="6" r="4" stroke="currentColor" strokeWidth="1.3"/><path d="M9 9l3 3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  upload: <svg width="20" height="20" viewBox="0 0 20 20" fill="none"><path d="M10 14V4M6 8l4-4 4 4M3 16h14" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  doc: <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M4 2h6l3 3v9a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z M10 2v3h3" stroke="currentColor" strokeWidth="1.2" strokeLinejoin="round"/></svg>,
  settings: <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="2" stroke="currentColor" strokeWidth="1.3"/><path d="M8 1v2M8 13v2M1 8h2M13 8h2M3 3l1.5 1.5M11.5 11.5L13 13M3 13l1.5-1.5M11.5 4.5L13 3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  sun: <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><circle cx="8" cy="8" r="3" stroke="currentColor" strokeWidth="1.3"/><path d="M8 1v1M8 14v1M1 8h1M14 8h1M3 3l.7.7M12.3 12.3l.7.7M3 13l.7-.7M12.3 3.7l.7-.7" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
  chevron: <svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M3 4.5l3 3 3-3" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  check: <svg width="12" height="12" viewBox="0 0 12 12" fill="none"><path d="M2.5 6l2.5 2.5L10 3" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>,
  grid: <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><rect x="2" y="2" width="4" height="4" stroke="currentColor" strokeWidth="1.2"/><rect x="8" y="2" width="4" height="4" stroke="currentColor" strokeWidth="1.2"/><rect x="2" y="8" width="4" height="4" stroke="currentColor" strokeWidth="1.2"/><rect x="8" y="8" width="4" height="4" stroke="currentColor" strokeWidth="1.2"/></svg>,
  brief: <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><rect x="2" y="4" width="10" height="8" rx="1" stroke="currentColor" strokeWidth="1.2"/><path d="M5 4V2.5A.5.5 0 0 1 5.5 2h3a.5.5 0 0 1 .5.5V4" stroke="currentColor" strokeWidth="1.2"/></svg>,
  log: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><rect x="3" y="3" width="12" height="12" rx="1" stroke="currentColor" strokeWidth="1.3"/><path d="M6 7h6M6 9h6M6 11h4" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/></svg>,
  chart: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><path d="M3 15V7M8 15V3M13 15v-5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  users: <svg width="18" height="18" viewBox="0 0 18 18" fill="none"><circle cx="6" cy="6" r="2.5" stroke="currentColor" strokeWidth="1.3"/><circle cx="13" cy="7" r="2" stroke="currentColor" strokeWidth="1.3"/><path d="M2 15c0-2.2 1.8-4 4-4s4 1.8 4 4M11 15c0-1.8 1.3-3 3-3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/></svg>,
};

// ─────────────────────────────────────────────────────────────────────────────
// The Aewita app shell — sidebar + main area. The main area is a slot.
function AewitaShell({ activeMode = 'library', children, pulseLogo = false, time = 0, sidebarVariant = 'main' }) {
  // sidebarVariant: 'main' (top-level modes) or 'admin' (admin sub-sidebar shown INSIDE content)
  const navItems = [
    { id: 'library', label: 'Library', tagline: 'Ask your files a question.', icon: Icons.library },
    { id: 'assistant', label: 'Assistant', tagline: 'Talk through your case.', icon: Icons.chat },
    { id: 'scrivener', label: 'Scrivener', tagline: 'Describe the document you need.', icon: Icons.pen },
    { id: 'admin', label: 'Admin', tagline: 'Manage your team.', icon: Icons.shield },
  ];

  return (
    <div style={{
      position: 'absolute', inset: 0,
      display: 'flex',
      background: COLORS.bgBase,
      color: COLORS.textPrimary,
      fontFamily: FONTS.sans,
    }}>
      {/* Sidebar */}
      <aside style={{
        width: 240,
        flexShrink: 0,
        background: COLORS.bgRaised,
        borderRight: `1px solid ${COLORS.border}`,
        display: 'flex', flexDirection: 'column',
      }}>
        {/* Logo block */}
        <div style={{
          height: 72,
          display: 'flex', alignItems: 'center', gap: 14,
          padding: '0 20px',
          borderBottom: `1px solid ${COLORS.border}`,
        }}>
          <AewitaQuill size={34} pulse={pulseLogo} time={time} />
          <AewitaWordmark size={17} letterSpacing="0.24em" weight={600} />
        </div>

        {/* Nav */}
        <div style={{ padding: '14px 10px', display: 'flex', flexDirection: 'column', gap: 4, flex: 1 }}>
          {navItems.map(n => (
            <NavItem key={n.id} {...n} active={n.id === activeMode} />
          ))}
        </div>

        {/* Bottom block */}
        <div style={{
          padding: '12px 10px',
          borderTop: `1px solid ${COLORS.border}`,
          display: 'flex', flexDirection: 'column', gap: 4,
        }}>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 10,
            padding: '8px 12px', color: COLORS.textSecondary, fontSize: 13,
          }}>
            {Icons.settings}
            <span>Settings</span>
          </div>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 10,
            padding: '8px 12px', color: COLORS.textSecondary, fontSize: 13,
          }}>
            {Icons.sun}
            <span>Light mode</span>
          </div>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 10,
            padding: '8px 8px', marginTop: 6,
          }}>
            <div style={{
              width: 28, height: 28, borderRadius: 14,
              background: '#2A2D31',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              color: COLORS.textSecondary, fontSize: 11, fontWeight: 600,
            }}>SM</div>
            <div style={{ fontSize: 11, color: COLORS.textSecondary, lineHeight: 1.2 }}>
              <div style={{ color: COLORS.textPrimary, fontWeight: 500 }}>sam@firm.com</div>
              <div style={{ color: COLORS.textMuted }}>Partner</div>
            </div>
          </div>
        </div>
      </aside>

      {/* Main */}
      <main style={{ flex: 1, minWidth: 0, position: 'relative', overflow: 'hidden' }}>
        {children}
      </main>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Cursor — animated arrow pointer with physics-based scale and blur.
function Cursor({ x, y, clicking = false, opacity = 1, scale = 1, blur = 'none' }) {
  return (
    <div style={{
      position: 'absolute',
      left: x, top: y,
      pointerEvents: 'none',
      zIndex: 999,
      opacity,
      transform: `scale(${scale})`,
      filter: blur,
      willChange: 'transform, filter',
    }}>
      {clicking && (
        <div style={{
          position: 'absolute', left: -10, top: -10,
          width: 40, height: 40,
          borderRadius: 20,
          background: 'rgba(217,72,78,0.22)',
          transform: `scale(${1 + (1 - scale) * 3})`,
          opacity: scale < 1 ? 0.9 : 0.4,
        }} />
      )}
      <svg width="22" height="26" viewBox="0 0 22 26" fill="none"
           style={{ filter: 'drop-shadow(0 2px 4px rgba(0,0,0,0.5))' }}>
        <path d="M2 2 L2 20 L7 16 L10 23 L13 22 L10 15 L17 15 Z"
              fill="#F5F0EA" stroke="#1A1A1A" strokeWidth="1.2" strokeLinejoin="round"/>
      </svg>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// Placeholder / striped div
function Striped({ width, height, label, radius = 8 }) {
  return (
    <div style={{
      width, height, borderRadius: radius,
      background: 'repeating-linear-gradient(135deg, rgba(232,224,216,0.04) 0 8px, rgba(232,224,216,0.02) 8px 16px)',
      border: `1px solid ${COLORS.border}`,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: FONTS.mono, fontSize: 10,
      color: COLORS.textMuted,
      letterSpacing: '0.08em', textTransform: 'uppercase',
    }}>{label}</div>
  );
}

Object.assign(window, { COLORS, FONTS, AewitaShell, AewitaQuill, AewitaWordmark, Cursor, Icons, Striped, NavItem });
