// Shared visual atoms for The Crossing

function SeriesMark() {
  return (
    <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
      <svg width="20" height="20" viewBox="0 0 20 20">
        <circle cx="10" cy="10" r="9" fill="none" stroke="#C9A84C" strokeWidth="1" />
        <circle cx="10" cy="10" r="5" fill="none" stroke="#C9A84C" strokeWidth="1" />
        <circle cx="10" cy="10" r="1.5" fill="#C9A84C" />
        <line x1="10" y1="1" x2="10" y2="3" stroke="#C9A84C" strokeWidth="1" />
        <line x1="10" y1="17" x2="10" y2="19" stroke="#C9A84C" strokeWidth="1" />
        <line x1="1" y1="10" x2="3" y2="10" stroke="#C9A84C" strokeWidth="1" />
        <line x1="17" y1="10" x2="19" y2="10" stroke="#C9A84C" strokeWidth="1" />
      </svg>
      <div style={{ lineHeight: 1.1 }}>
        <div className="mono" style={{ color: '#C9A84C', fontSize: 11, letterSpacing: 0.25, textTransform: 'uppercase', fontWeight: 700 }}>
          One Hundred Years
        </div>
        <div className="mono" style={{ color: '#5A4E3A', fontSize: 9, letterSpacing: 0.25, textTransform: 'uppercase' }}>
          A reporting series
        </div>
      </div>
    </div>
  );
}

const Tick = ({ children, color = '#5A4E3A' }) => (
  <span className="mono" style={{ color, fontSize: 10, letterSpacing: 0.08, textTransform: 'uppercase' }}>
    {children}
  </span>
);

const Eyebrow = ({ id, title, right }) => (
  <div style={{
    display: 'flex', alignItems: 'baseline', gap: 12,
    padding: '0 0 14px', borderBottom: '1px solid #2A2218', marginBottom: 24,
  }}>
    <span className="mono" style={{ color: '#C9A84C', fontSize: 11, letterSpacing: 0.2, textTransform: 'uppercase' }}>
      {id}
    </span>
    <span style={{ flex: 1 }}>
      <span className="serif" style={{ color: '#D4C8B8', fontSize: 22, fontWeight: 400 }}>{title}</span>
    </span>
    {right && <span className="mono" style={{ color: '#5A4E3A', fontSize: 10, letterSpacing: 0.15, textTransform: 'uppercase' }}>{right}</span>}
  </div>
);

const Readout = ({ label, value, sub, accent = '#D4C8B8', size = 32 }) => (
  <div style={{
    border: '1px solid #2A2218', background: 'rgba(30,24,16,0.5)',
    padding: '14px 16px', display: 'flex', flexDirection: 'column', gap: 6, minHeight: 110,
  }}>
    <div className="mono" style={{ color: '#5A4E3A', fontSize: 10, letterSpacing: 0.18, textTransform: 'uppercase' }}>
      {label}
    </div>
    <div className="mono" style={{ color: accent, fontSize: size, fontWeight: 700, lineHeight: 1.05, letterSpacing: -0.5 }}>
      {value}
    </div>
    {sub && (
      <div className="mono" style={{ color: '#8A7E6A', fontSize: 10.5, letterSpacing: 0.06, opacity: 0.85 }}>
        {sub}
      </div>
    )}
  </div>
);

const Confidence = ({ level = 'high' }) => {
  const map = {
    high: { label: 'HIGH CONFIDENCE', color: '#C9A84C' },
    candidate: { label: 'CANDIDATE', color: '#FDAE61' },
    speculative: { label: 'SPECULATIVE', color: '#C0392B' },
  };
  const c = map[level];
  return (
    <span className="mono" style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      border: `1px solid ${c.color}66`, color: c.color, padding: '3px 7px',
      fontSize: 9, letterSpacing: 0.18, textTransform: 'uppercase',
    }}>
      <span style={{ width: 6, height: 6, background: c.color, borderRadius: '50%' }} />
      {c.label}
    </span>
  );
};

const HRule = ({ children, color = '#2A2218' }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 8, color: '#5A4E3A' }}>
    <span style={{ flex: 1, height: 1, background: color }} />
    {children && <span className="mono" style={{ fontSize: 10, letterSpacing: 0.18, textTransform: 'uppercase' }}>{children}</span>}
    <span style={{ flex: 1, height: 1, background: color }} />
  </div>
);

// Country names in small caps (per spec)
const SmallCaps = ({ children }) => (
  <span className="mono" style={{
    fontVariant: 'all-small-caps', fontSize: 12, letterSpacing: 0.15, fontWeight: 700,
  }}>
    {children}
  </span>
);

// Ellis Island manifest row
const ManifestRow = ({ record, highlight }) => (
  <div style={{
    display: 'grid', gridTemplateColumns: '2.5fr 1.2fr 0.5fr 0.6fr 1.2fr',
    padding: '6px 12px', borderBottom: '1px solid #1A1510',
    background: highlight ? 'rgba(201,168,76,0.06)' : 'transparent',
    transition: 'background 150ms',
  }}>
    <span className="mono" style={{ color: '#D4C8B8', fontSize: 12 }}>{record.name}</span>
    <span className="mono" style={{ color: '#8A7E6A', fontSize: 11, fontVariant: 'all-small-caps', letterSpacing: 0.1 }}>{record.origin}</span>
    <span className="mono" style={{ color: '#8A7E6A', fontSize: 11, textAlign: 'right' }}>{record.age}</span>
    <span className="mono" style={{ color: '#C9A84C', fontSize: 11, textAlign: 'right' }}>{record.year}</span>
    <span className="mono" style={{ color: '#5A4E3A', fontSize: 11, textAlign: 'right' }}>{record.destination}</span>
  </div>
);

// Era label badge
const EraLabel = ({ era }) => (
  <span className="display" style={{
    fontSize: 12, letterSpacing: 0.05, color: era.color, fontWeight: 400,
    fontStyle: 'italic',
  }}>
    {era.label}
  </span>
);

// Number formatting
const fmt = (n) => {
  if (n == null) return '—';
  if (Math.abs(n) >= 1_000_000) return (n / 1_000_000).toFixed(1) + 'M';
  if (Math.abs(n) >= 10_000) return (n / 1000).toFixed(1) + 'k';
  if (Math.abs(n) >= 1000) return n.toLocaleString();
  return n.toString();
};

const LiveReadout = ({ label, value, color = '#D4C8B8', pulse }) => (
  <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
    <span className="mono" style={{ color: '#5A4E3A', fontSize: 9.5, letterSpacing: 0.2, textTransform: 'uppercase' }}>{label}</span>
    <span className="mono" style={{ color, fontSize: 12, fontWeight: 700, letterSpacing: 0.04 }}>{value}</span>
    {pulse && <span style={{ width: 6, height: 6, background: color, borderRadius: '50%', animation: 'pulse 1.6s ease-in-out infinite' }} />}
  </div>
);

Object.assign(window, {
  SeriesMark, Tick, Eyebrow, Readout, Confidence, HRule,
  SmallCaps, ManifestRow, EraLabel, fmt, LiveReadout,
});
