// surfaces.jsx
// Brand chrome (monochrome + blue) and the two product surfaces the captures
// flow into: the ScreenSync Library and the Figma canvas.

/* ───────────────────────── marks ───────────────────────── */
function ScreenSyncMark({ size = 22 }) {
  return <img src="assets/logo.svg" width={size} height={size} alt="" style={{ display: 'block' }} />;
}

function AndroidMark({ size = 20 }) {
  const g = '#3DDC84';
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M5 11.5h14V18a1.5 1.5 0 0 1-1.5 1.5h-11A1.5 1.5 0 0 1 5 18v-6.5Z" fill={g}/>
      <path d="M6.5 7.5C7.8 5.9 9.8 5 12 5s4.2.9 5.5 2.5l1.2-1.8M6.5 7.5 5.3 5.7M6.5 7.5C5.6 8.6 5 10 5 11.5h14C19 10 18.4 8.6 17.5 7.5" stroke={g} strokeWidth="1.6" strokeLinecap="round" fill="none"/>
      <circle cx="9.3" cy="9.2" r="0.9" fill="#fff"/>
      <circle cx="14.7" cy="9.2" r="0.9" fill="#fff"/>
      <rect x="3" y="12" width="1.7" height="6" rx="0.85" fill={g}/>
      <rect x="19.3" y="12" width="1.7" height="6" rx="0.85" fill={g}/>
      <rect x="8.5" y="19" width="1.7" height="3.4" rx="0.85" fill={g}/>
      <rect x="13.8" y="19" width="1.7" height="3.4" rx="0.85" fill={g}/>
    </svg>
  );
}

function FigmaMark({ size = 20 }) {
  const u = size / 24;
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M8.5 24a3.75 3.75 0 0 0 3.75-3.75V16.5H8.5a3.75 3.75 0 1 0 0 7.5Z" fill="#0ACF83"/>
      <path d="M4.75 12A3.75 3.75 0 0 1 8.5 8.25h3.75v7.5H8.5A3.75 3.75 0 0 1 4.75 12Z" fill="#A259FF"/>
      <path d="M4.75 3.75A3.75 3.75 0 0 1 8.5 0h3.75v7.5H8.5A3.75 3.75 0 0 1 4.75 3.75Z" fill="#F24E1E"/>
      <path d="M12.25 0H16a3.75 3.75 0 1 1 0 7.5h-3.75V0Z" fill="#FF7262"/>
      <path d="M19.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" fill="#1ABCFE"/>
    </svg>
  );
}

/* ───────────────────────── pill ───────────────────────── */
// The from→to motif: a floating white pill labelling the current surface.
function Pill({ icon, label, sub }) {
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 9,
      padding: '11px 18px',
      background: '#ffffff', borderRadius: 999,
      boxShadow: '0 10px 30px rgba(0,0,0,0.35), 0 0 0 1px rgba(0,0,0,0.04)',
      fontFamily: 'Geist, sans-serif',
    }}>
      <span style={{ display: 'flex' }}>{icon}</span>
      <span style={{ fontWeight: 600, fontSize: 17, color: '#171717', letterSpacing: '-0.02em' }}>{label}</span>
      {sub && <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 11, color: '#8f8f8f', letterSpacing: '0.02em', marginLeft: 2 }}>{sub}</span>}
    </div>
  );
}

/* ───────────────────── frame thumbnail ───────────────────── */
// A captured screen, cropped to a card. Used in Library + Figma.
function FrameThumb({ variant, w = 200, cropH, radius = 12, ring = 'none' }) {
  const scale = w / SCREEN_W;
  const fullH = SCREEN_H * scale;
  const h = cropH != null ? cropH : fullH;
  const ringStyle = ring === 'blue'
    ? '0 0 0 2.5px #007aff, 0 8px 22px rgba(0,122,255,0.22)'
    : ring === 'soft' ? '0 0 0 1px rgba(0,0,0,0.08), 0 4px 14px rgba(0,0,0,0.10)' : 'none';
  return (
    <div style={{ width: w, height: h, borderRadius: radius, overflow: 'hidden', flexShrink: 0, boxShadow: ringStyle, background: '#fff' }}>
      <div style={{ width: SCREEN_W, height: SCREEN_H, transform: `scale(${scale})`, transformOrigin: 'top left' }}>
        <MockScreen variant={variant} />
      </div>
    </div>
  );
}

/* ───────────────────── ScreenSync Library ───────────────────── */
const LIB = { w: 720, h: 724 };
// captureProgress: per-variant entrance 0..1 (keyed by variant). selected: variant string.
function LibraryWindow({ captureProgress = {}, selected = null, syncedCount = 3 }) {
  const newCards = [
    { variant: 'travel', name: 'kyoto-explore.png', time: '0:12' },
    { variant: 'music', name: 'now-playing.png', time: '0:08' },
    { variant: 'fitness', name: 'activity-rings.png', time: '0:03' },
  ];
  const older = ['#fde2c4', '#e7d4ff', '#cdeede', '#ffd4dd', '#d3e4ff'];
  return (
    <div style={{
      width: LIB.w, height: LIB.h, background: '#fff', borderRadius: 16,
      border: '1px solid #ebebeb', boxShadow: '0 32px 80px rgba(0,0,0,0.45)',
      overflow: 'hidden', fontFamily: 'Geist, sans-serif', display: 'flex', flexDirection: 'column',
    }}>
      {/* header */}
      <div style={{ height: 58, display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 20px', borderBottom: '1px solid #ebebeb', flexShrink: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <ScreenSyncMark size={22} />
          <span style={{ fontWeight: 600, fontSize: 16, color: '#171717', letterSpacing: '-0.02em' }}>ScreenSync</span>
          <span style={{ color: '#d4d4d4', fontSize: 15 }}>/</span>
          <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 13, color: '#717171' }}>Mobile refs</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <div style={{ width: 150, height: 32, borderRadius: 8, background: '#f4f4f5', display: 'flex', alignItems: 'center', padding: '0 10px', gap: 7, color: '#a1a1a1', fontSize: 13 }}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><circle cx="11" cy="11" r="7" stroke="currentColor" strokeWidth="2"/><path d="M21 21l-4-4" stroke="currentColor" strokeWidth="2" strokeLinecap="round"/></svg>
            Search
          </div>
          <div style={{ width: 32, height: 32, borderRadius: 16, background: 'linear-gradient(135deg,#007aff,#4da3ff)' }} />
        </div>
      </div>
      {/* toolbar */}
      <div style={{ height: 46, display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 20px', borderBottom: '1px solid #f4f4f5', flexShrink: 0 }}>
        <div style={{ display: 'flex', gap: 18, fontSize: 14 }}>
          <span style={{ color: '#171717', fontWeight: 600 }}>All assets</span>
          <span style={{ color: '#a1a1a1' }}>Collections</span>
          <span style={{ color: '#a1a1a1' }}>Recent</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <span style={{ display: 'inline-flex', alignItems: 'center', gap: 6, fontSize: 12, fontFamily: 'Geist Mono, monospace', color: '#16a34a' }}>
            <span style={{ width: 7, height: 7, borderRadius: 4, background: '#16a34a' }} /> synced
          </span>
        </div>
      </div>
      {/* body */}
      <div style={{ padding: '18px 20px', flex: 1, overflow: 'hidden' }}>
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 14 }}>
          <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 12, letterSpacing: '0.06em', color: '#8f8f8f' }}>TODAY</span>
          <span style={{ fontSize: 13, color: '#a1a1a1' }}>{syncedCount} new frames</span>
        </div>
        <div style={{ display: 'flex', gap: 20 }}>
          {newCards.map((c) => {
            const p = clamp(captureProgress[c.variant] ?? 1, 0, 1);
            const sel = selected === c.variant;
            return (
              <div key={c.variant} style={{ width: 200, opacity: p, transform: `translateY(${(1 - Easing.easeOutCubic(p)) * 26}px) scale(${0.94 + 0.06 * Easing.easeOutBack(p)})`, transformOrigin: 'top center' }}>
                <FrameThumb variant={c.variant} w={200} cropH={300} ring={sel ? 'blue' : 'soft'} />
                <div style={{ marginTop: 9, display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
                  <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 11.5, color: '#3f3f3f', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{c.name}</span>
                  <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 11, color: '#a1a1a1', marginLeft: 6 }}>{c.time}</span>
                </div>
                {p > 0.6 && (
                  <span style={{ display: 'inline-block', marginTop: 4, fontFamily: 'Geist Mono, monospace', fontSize: 9.5, letterSpacing: '0.06em', color: '#007aff', background: '#ebf4ff', border: '1px solid #cfe3ff', borderRadius: 4, padding: '1px 5px' }}>NEW</span>
                )}
              </div>
            );
          })}
        </div>
        {/* older row */}
        <div style={{ marginTop: 26, display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 12 }}>
          <span style={{ fontFamily: 'Geist Mono, monospace', fontSize: 12, letterSpacing: '0.06em', color: '#8f8f8f' }}>EARLIER</span>
        </div>
        <div style={{ display: 'flex', gap: 14 }}>
          {older.map((col, i) => (
            <div key={i} style={{ width: 120, height: 86, borderRadius: 10, background: col, boxShadow: '0 0 0 1px rgba(0,0,0,0.05)' }} />
          ))}
        </div>
      </div>
    </div>
  );
}

/* ───────────────────────── Figma canvas ───────────────────────── */
const FIG = { w: 760, h: 724 };
// frameProgress: per-variant 0..1 entrance onto the canvas.
// cam: { amt:0..1, fcx } — zoom/pan the CANVAS CONTENT inside the fixed window.
function FigmaWindow({ frameProgress = {}, focus = null, cam }) {
  const c = cam || { amt: 0, fcx: 305 };
  const vcx = (FIG.w - 150) / 2;   // canvas viewport centre x
  const vcy = 330;                 // where the focused frame centre lands vertically
  const fcyLocal = 270;            // frame centre y in canvas coords
  const cs = 1 + 0.72 * c.amt;
  const ctx2 = (vcx - cs * c.fcx) * c.amt;
  const cty2 = (vcy - cs * fcyLocal) * c.amt;
  const camTransform = `translate(${ctx2}px, ${cty2}px) scale(${cs})`;
  const frames = [
    { variant: 'travel', name: 'Kyoto explore', x: 56, y: 88 },
    { variant: 'music', name: 'Now playing', x: 230, y: 88 },
    { variant: 'fitness', name: 'Activity rings', x: 404, y: 88 },
  ];
  const fw = 150; // frame width on canvas
  const fh = fw * (SCREEN_H / SCREEN_W);
  return (
    <div style={{ width: FIG.w, height: FIG.h, background: '#fff', borderRadius: 16, border: '1px solid #e6e6e6', boxShadow: '0 32px 80px rgba(0,0,0,0.45)', overflow: 'hidden', fontFamily: 'Geist, sans-serif', display: 'flex', flexDirection: 'column' }}>
      {/* menubar */}
      <div style={{ height: 40, background: '#2c2c2c', display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 12px', flexShrink: 0 }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
          <FigmaMark size={16} />
          <span style={{ color: '#b3b3b3', fontSize: 12.5 }}>Mobile references</span>
        </div>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <div style={{ display: 'flex', marginRight: 4 }}>
            {['#34e89e', '#a838ff', '#ff8a5c'].map((c, i) => <div key={i} style={{ width: 22, height: 22, borderRadius: 11, background: c, border: '2px solid #2c2c2c', marginLeft: i ? -6 : 0 }} />)}
          </div>
          <div style={{ background: '#0d99ff', color: '#fff', fontSize: 12, fontWeight: 600, padding: '5px 12px', borderRadius: 6 }}>Share</div>
        </div>
      </div>
      <div style={{ flex: 1, display: 'flex', minHeight: 0 }}>
        {/* layers panel */}
        <div style={{ width: 150, borderRight: '1px solid #ebebeb', padding: '12px 10px', flexShrink: 0 }}>
          <div style={{ fontSize: 11, fontWeight: 600, color: '#8f8f8f', marginBottom: 8 }}>Layers</div>
          {frames.map((f) => (
            <div key={f.variant} style={{ display: 'flex', alignItems: 'center', gap: 7, padding: '6px 8px', borderRadius: 6, marginBottom: 2, background: focus === f.variant ? '#e5f2ff' : 'transparent', opacity: clamp((frameProgress[f.variant] ?? 0) * 2, 0, 1) }}>
              <span style={{ width: 12, height: 12, borderRadius: 3, background: SCREENS[f.variant].accent, flexShrink: 0 }} />
              <span style={{ fontSize: 11.5, color: focus === f.variant ? '#0d99ff' : '#3f3f3f', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' }}>{f.name}</span>
            </div>
          ))}
        </div>
        {/* canvas */}
        <div style={{ flex: 1, position: 'relative', background: '#f5f5f5', overflow: 'hidden' }}>
          <div style={{ position: 'absolute', inset: 0, transformOrigin: '0 0', transform: camTransform, backgroundImage: 'radial-gradient(#e2e2e2 1px, transparent 1px)', backgroundSize: '18px 18px' }}>
          {frames.map((f) => {
            const p = clamp(frameProgress[f.variant] ?? 0, 0, 1);
            if (p <= 0.001) return null;
            const e = p; // placed frame appears at full size \u2014 no scale-up pop
            const isFocus = focus === f.variant;
            return (
              <div key={f.variant} style={{ position: 'absolute', left: f.x, top: f.y, opacity: e, transformOrigin: 'top center' }}>
                <div style={{ fontSize: 10.5, color: isFocus ? '#0d99ff' : '#8f8f8f', marginBottom: 5, fontWeight: 500 }}>{f.name}</div>
                <div style={{ boxShadow: isFocus ? '0 0 0 1.5px #0d99ff' : '0 1px 0 rgba(0,0,0,0.06)' }}>
                  <FrameThumb variant={f.variant} w={fw} radius={2} ring="none" />
                </div>
              </div>
            );
          })}
          </div>
        </div>
      </div>
      {/* bottom toolbar */}
      <div style={{ position: 'absolute', left: '50%', bottom: 18, transform: 'translateX(-50%)', display: 'flex', alignItems: 'center', gap: 4, background: '#2c2c2c', borderRadius: 12, padding: '7px 10px', boxShadow: '0 8px 24px rgba(0,0,0,0.3)' }}>
        {['move', 'frame', 'pen', 'shape', 'text'].map((t, i) => (
          <div key={t} style={{ width: 30, height: 30, borderRadius: 7, background: i === 0 ? '#0d99ff' : 'transparent', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <ToolIcon t={t} active={i === 0} />
          </div>
        ))}
      </div>
    </div>
  );
}

function ToolIcon({ t, active }) {
  const c = active ? '#fff' : '#b3b3b3';
  const map = {
    move: <path d="M5 3l14 7-6 2-2 6-6-15Z" fill={c} />,
    frame: <g stroke={c} strokeWidth="1.6"><path d="M8 4v16M16 4v16M4 8h16M4 16h16"/></g>,
    pen: <path d="M5 19l3-1 9-9-2-2-9 9-1 3Z" stroke={c} strokeWidth="1.6" fill="none"/>,
    shape: <circle cx="12" cy="12" r="7" stroke={c} strokeWidth="1.6" fill="none"/>,
    text: <text x="7" y="17" fontFamily="Geist, sans-serif" fontSize="14" fontWeight="700" fill={c}>T</text>,
  };
  return <svg width="18" height="18" viewBox="0 0 24 24" fill="none">{map[t]}</svg>;
}

/* ───────────────────── toast + sync trail dot ───────────────────── */
function Toast({ label, sub, opacity = 1, y = 0 }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 10, padding: '11px 18px', background: '#171717', borderRadius: 12, boxShadow: '0 12px 30px rgba(0,0,0,0.4)', opacity, transform: `translateY(${y}px)`, fontFamily: 'Geist, sans-serif' }}>
      <span style={{ width: 20, height: 20, borderRadius: 10, background: '#007aff', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
        <svg width="12" height="12" viewBox="0 0 24 24" fill="none"><path d="M5 12l4 4 10-10" stroke="#fff" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"/></svg>
      </span>
      <span style={{ color: '#fff', fontWeight: 600, fontSize: 14.5, letterSpacing: '-0.01em' }}>{label}</span>
      {sub && <span style={{ color: '#8f8f8f', fontFamily: 'Geist Mono, monospace', fontSize: 12 }}>{sub}</span>}
    </div>
  );
}

Object.assign(window, { ScreenSyncMark, AndroidMark, FigmaMark, Pill, FrameThumb, LibraryWindow, FigmaWindow, Toast, LIB, FIG });
