// shared.jsx — shared visual primitives for 출입통제 시스템 designs
// Loaded BEFORE all screen jsx files. Exports onto window.

// ─── Korean seal (인장) — small SVG used for approvals/QR markers
function Seal({ size = 28, label = "驗", color }) {
  return (
    <svg width={size} height={size} viewBox="0 0 40 40" style={{ display: 'block' }}>
      <rect x="2.5" y="2.5" width="35" height="35" rx="4"
        fill={color || 'var(--stamp)'} opacity="0.92" />
      <text x="20" y="27" textAnchor="middle"
        fontFamily="var(--font-serif)" fontSize="20" fontWeight="600"
        fill="#FBF9F4" letterSpacing="0">{label}</text>
    </svg>
  );
}

// ─── QR placeholder (deterministic pattern)
function QRBlock({ size = 200, seed = 'qr', dark = 'var(--ink-0)', light = 'transparent', radius = 0 }) {
  const N = 25;
  // simple deterministic pseudo-random based on seed
  const cells = React.useMemo(() => {
    let h = 0;
    for (let i = 0; i < seed.length; i++) h = (h * 31 + seed.charCodeAt(i)) >>> 0;
    const arr = [];
    for (let y = 0; y < N; y++) {
      const row = [];
      for (let x = 0; x < N; x++) {
        h = (h * 1664525 + 1013904223) >>> 0;
        row.push((h & 1) === 0);
      }
      arr.push(row);
    }
    // finder patterns at 3 corners
    const stamp = (x, y) => {
      for (let dy = 0; dy < 7; dy++) for (let dx = 0; dx < 7; dx++) {
        if (x+dx >= N || y+dy >= N) continue;
        const onEdge = dx===0||dy===0||dx===6||dy===6;
        const inner = dx>=2&&dx<=4&&dy>=2&&dy<=4;
        arr[y+dy][x+dx] = onEdge || inner;
      }
      // clear ring
      for (let dy = 1; dy < 6; dy++) for (let dx = 1; dx < 6; dx++) {
        if (!(dx>=2&&dx<=4&&dy>=2&&dy<=4)) arr[y+dy][x+dx] = false;
      }
    };
    stamp(0,0); stamp(N-7,0); stamp(0,N-7);
    return arr;
  }, [seed]);
  const cs = size / N;
  return (
    <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}
      style={{ display: 'block', borderRadius: radius }}>
      {light !== 'transparent' && (
        <rect width={size} height={size} fill={light} />
      )}
      {cells.map((row, y) => row.map((on, x) => on && (
        <rect key={`${x}-${y}`} x={x*cs} y={y*cs} width={cs} height={cs} fill={dark} />
      )))}
    </svg>
  );
}

// ─── Status pill
function Pill({ tone = 'neutral', children, dot = false, soft = true, style = {} }) {
  const tones = {
    neutral: { bg: 'var(--paper-2)', fg: 'var(--ink-1)', dot: 'var(--ink-3)' },
    approved:{ bg: 'var(--celadon-soft)', fg: 'var(--celadon-deep)', dot: 'var(--celadon)' },
    pending: { bg: 'var(--ochre-soft)', fg: '#7A5C1A', dot: 'var(--ochre)' },
    denied:  { bg: 'var(--clay-soft)', fg: 'var(--clay)', dot: 'var(--clay)' },
    info:    { bg: 'var(--paper-2)', fg: 'var(--ink-2)', dot: 'var(--ink-2)' },
  };
  const t = tones[tone] || tones.neutral;
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '3px 9px', borderRadius: 100,
      background: soft ? t.bg : 'transparent',
      color: t.fg, fontSize: 11, fontWeight: 500,
      letterSpacing: '0.02em',
      border: soft ? 'none' : `0.5px solid ${t.dot}`,
      ...style,
    }}>
      {dot && <span style={{ width: 5, height: 5, borderRadius: 100, background: t.dot }} />}
      {children}
    </span>
  );
}

// ─── Hairline divider
function Rule({ vertical = false, style = {} }) {
  return <div style={{
    [vertical ? 'width' : 'height']: '0.5px',
    [vertical ? 'height' : 'width']: '100%',
    background: 'var(--paper-stroke)',
    ...style,
  }} />;
}

// ─── Wordmark / brand mark (square seal + 출입)
function Brandmark({ size = 22, label = "출입통제", subtle = false, href = "/" }) {
  return (
    <a href={href} style={{ display: 'flex', alignItems: 'center', gap: 9, textDecoration: 'none', color: 'inherit' }}>
      <Seal size={size} label="出" color={subtle ? 'var(--ink-1)' : 'var(--stamp)'} />
      <span className="serif" style={{
        fontSize: size * 0.74, fontWeight: 500, color: 'var(--ink-0)',
        letterSpacing: '-0.01em',
      }}>{label}</span>
    </a>
  );
}

// ─── Central route map — keep nav in one place
const ROUTES = {
  home:'/', apply:'/apply.html', applyW:'/apply-wizard.html',
  admin:'/admin.html', requests:'/admin-requests.html', approve:'/admin-approve.html',
  logs:'/admin-logs.html', meals:'/admin-meals.html', gates:'/admin-gates.html',
  delivery:'/delivery.html', kiosk:'/kiosk.html', scan:'/scan.html',
  mQR:'/m-qr.html', mCanteen:'/m-canteen.html',
};
window.ROUTES = ROUTES;

// ─── Vertical Korean rail label (for left edges)
function RailLabel({ children, style = {} }) {
  return (
    <div style={{
      writingMode: 'vertical-rl', textOrientation: 'mixed',
      fontSize: 10, letterSpacing: '0.3em', textTransform: 'uppercase',
      color: 'var(--ink-3)', fontWeight: 500,
      ...style,
    }}>{children}</div>
  );
}

// ─── Inline icon set (1.5px stroke, monoline)
const Icon = ({ name, size = 16, color = 'currentColor' }) => {
  const paths = {
    qr: <g><path d="M3 3h6v6H3z M15 3h6v6h-6z M3 15h6v6H3z M15 15v3 M18 15h3 M15 18h3 M18 21h3 M21 18v3" /></g>,
    scan: <g><path d="M3 7V5a2 2 0 0 1 2-2h2 M21 7V5a2 2 0 0 0-2-2h-2 M3 17v2a2 2 0 0 0 2 2h2 M21 17v2a2 2 0 0 1-2 2h-2 M3 12h18" /></g>,
    home: <g><path d="M3 11l9-8 9 8 M5 10v10h14V10" /></g>,
    grid: <g><path d="M3 3h7v7H3z M14 3h7v7h-7z M3 14h7v7H3z M14 14h7v7h-7z" /></g>,
    bell: <g><path d="M6 9a6 6 0 1 1 12 0v4l1.5 3h-15L6 13z M10 19a2 2 0 0 0 4 0" /></g>,
    user: <g><path d="M12 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8z M5 21a7 7 0 0 1 14 0" /></g>,
    users: <g><path d="M9 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8z M2 21a7 7 0 0 1 14 0 M16 4.5a4 4 0 0 1 0 7 M22 21a7 7 0 0 0-4-6.3" /></g>,
    list: <g><path d="M8 6h13 M8 12h13 M8 18h13 M3 6h.01 M3 12h.01 M3 18h.01" /></g>,
    chart: <g><path d="M3 3v18h18 M7 14l3-3 4 4 5-7" /></g>,
    door: <g><path d="M5 21V4a1 1 0 0 1 1-1h12a1 1 0 0 1 1 1v17 M3 21h18 M14 12h.5" /></g>,
    settings: <g><path d="M12 15a3 3 0 1 0 0-6 3 3 0 0 0 0 6z M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3h0a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8v0a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z" /></g>,
    plus: <g><path d="M12 5v14 M5 12h14" /></g>,
    check: <g><path d="M5 12l5 5L20 7" /></g>,
    x: <g><path d="M6 6l12 12 M18 6L6 18" /></g>,
    arrow: <g><path d="M5 12h14 M13 5l7 7-7 7" /></g>,
    chevron: <g><path d="M9 6l6 6-6 6" /></g>,
    clock: <g><circle cx="12" cy="12" r="9" /><path d="M12 7v5l3 2" /></g>,
    mail: <g><path d="M3 6h18v12H3z M3 7l9 7 9-7" /></g>,
    msg: <g><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z" /></g>,
    location: <g><path d="M12 22s7-7.5 7-13a7 7 0 1 0-14 0c0 5.5 7 13 7 13z" /><circle cx="12" cy="9" r="2.5" /></g>,
    refresh: <g><path d="M3 12a9 9 0 0 1 15.5-6.3L21 8 M21 4v4h-4 M21 12a9 9 0 0 1-15.5 6.3L3 16 M3 20v-4h4" /></g>,
    shield: <g><path d="M12 3l8 3v6c0 5-4 9-8 10-4-1-8-5-8-10V6z M9 12l2 2 4-4" /></g>,
    food:   <g><path d="M3 4h18 M5 4v6a7 7 0 0 0 14 0V4 M12 11v10 M8 21h8" /></g>,
    search: <g><circle cx="11" cy="11" r="7" /><path d="M21 21l-4.5-4.5" /></g>,
    download: <g><path d="M12 4v12 M6 12l6 6 6-6 M4 21h16" /></g>,
    filter: <g><path d="M3 5h18l-7 9v6l-4-2v-4z" /></g>,
    eye: <g><path d="M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12z" /><circle cx="12" cy="12" r="3" /></g>,
    calendar: <g><rect x="3" y="5" width="18" height="16" rx="1" /><path d="M3 10h18 M8 3v4 M16 3v4" /></g>,
    car: <g><path d="M3 16v-4l2-5h14l2 5v4h-3 M6 16h12 M6 16v2 M18 16v2" /><circle cx="7" cy="16" r="2" /><circle cx="17" cy="16" r="2" /></g>,
    building: <g><path d="M4 21V5a1 1 0 0 1 1-1h14a1 1 0 0 1 1 1v16 M3 21h18 M9 9h.01 M9 13h.01 M9 17h.01 M15 9h.01 M15 13h.01 M15 17h.01" /></g>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24"
      fill="none" stroke={color} strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"
      style={{ display: 'block', flexShrink: 0 }}>
      {paths[name] || null}
    </svg>
  );
};

// ─── Avatar circle with korean initial
function Avatar({ name = "김", size = 28, tone = 0 }) {
  const tones = [
    ['#E8DFCD', '#7A6647'],
    ['#D9E3DD', '#4F6E62'],
    ['#E4DBE9', '#6B5577'],
    ['#DDE3EA', '#475A72'],
    ['#ECD7D0', '#9C4F3D'],
    ['#EBDFB8', '#7A5C1A'],
  ];
  const [bg, fg] = tones[Math.abs(name.charCodeAt(0)) % tones.length];
  return (
    <div style={{
      width: size, height: size, borderRadius: 100,
      background: bg, color: fg,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontSize: size * 0.42, fontWeight: 600, flexShrink: 0,
      fontFamily: 'var(--font-sans)',
    }}>{name.slice(0, 1)}</div>
  );
}

// ─── Mini sparkline (deterministic)
function Spark({ width = 80, height = 24, points, color = 'var(--celadon)' }) {
  const pts = points || [3,4,3,5,4,6,5,7,6,5,7,8,6,7,9,7,8,9,7,9];
  const max = Math.max(...pts), min = Math.min(...pts);
  const path = pts.map((p, i) => {
    const x = (i / (pts.length - 1)) * width;
    const y = height - ((p - min) / Math.max(0.1, max - min)) * (height - 4) - 2;
    return `${i === 0 ? 'M' : 'L'}${x.toFixed(1)},${y.toFixed(1)}`;
  }).join(' ');
  return (
    <svg width={width} height={height} viewBox={`0 0 ${width} ${height}`}>
      <path d={path} fill="none" stroke={color} strokeWidth="1.25" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

Object.assign(window, {
  Seal, QRBlock, Pill, Rule, Brandmark, RailLabel, Icon, Avatar, Spark,
});
