// tokens.jsx — Design tokens for 우리집사기 (Bloomberg terminal style)
// Deep navy base + terminal greens/reds + signal yellow.
// Mono numerals via JetBrains Mono; Pretendard for Korean UI.

const T = {
  // Surfaces
  bg:        '#0B1220',   // deep navy base
  bgAlt:     '#0E1626',   // panel
  card:      '#131C2E',   // card
  cardHi:    '#1A2540',   // hover / selected
  cardLo:    '#0F172A',   // sunken (charts inset)
  line:      '#1F2A3E',   // border
  lineHi:    '#2A3650',   // emphasized border

  // Text
  fg:        '#E8EDF5',
  fgDim:     '#A6B0C2',
  fgMuted:   '#6B7589',
  fgFaint:   '#475063',

  // Signals
  up:        '#1FB57A',   // buy / 상승
  upBg:      'rgba(31,181,122,0.14)',
  down:      '#E64545',   // sell / 하락
  downBg:    'rgba(230,69,69,0.14)',
  hot:       '#FFB020',   // bloomberg-yellow accent
  hotBg:     'rgba(255,176,32,0.14)',
  info:      '#2D7FF9',
  infoBg:    'rgba(45,127,249,0.14)',
  ai:        '#8B5CF6',   // AI / model
  aiBg:      'rgba(139,92,246,0.14)',

  // Grades
  gradeS:    '#FFB020',   // gold
  gradeA:    '#1FB57A',
  gradeB:    '#2D7FF9',
  gradeC:    '#8A95A8',

  // Fonts
  font:      '"Pretendard Variable", Pretendard, -apple-system, system-ui, "Apple SD Gothic Neo", sans-serif',
  mono:      '"JetBrains Mono", "SF Mono", ui-monospace, monospace',
};

// Inject base CSS / fonts once
if (typeof document !== 'undefined' && !document.getElementById('uj-tokens')) {
  const link = document.createElement('link');
  link.rel = 'stylesheet';
  link.href = 'https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.9/dist/web/variable/pretendardvariable.min.css';
  document.head.appendChild(link);

  const jb = document.createElement('link');
  jb.rel = 'stylesheet';
  jb.href = 'https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600;700&display=swap';
  document.head.appendChild(jb);

  const s = document.createElement('style');
  s.id = 'uj-tokens';
  s.textContent = `
    .uj-num{font-family:${T.mono};font-feature-settings:"tnum" 1, "zero" 1;font-variant-numeric:tabular-nums}
    .uj-mono{font-family:${T.mono}}
    .uj-scroll::-webkit-scrollbar{width:6px;height:6px}
    .uj-scroll::-webkit-scrollbar-thumb{background:${T.line};border-radius:3px}
    .uj-scroll::-webkit-scrollbar-track{background:transparent}
    .uj-blink{animation:ujblink 1.6s ease-in-out infinite}
    @keyframes ujblink{0%,100%{opacity:1}50%{opacity:.35}}
    .uj-shimmer{background:linear-gradient(90deg,${T.card} 0%,${T.cardHi} 50%,${T.card} 100%);background-size:200% 100%;animation:ujshimmer 2s linear infinite}
    @keyframes ujshimmer{0%{background-position:200% 0}100%{background-position:-200% 0}}
    .uj-grid-bg{background-image:
      linear-gradient(${T.line} 1px, transparent 1px),
      linear-gradient(90deg, ${T.line} 1px, transparent 1px);
      background-size: 28px 28px;
      background-position:-1px -1px;
    }
  `;
  document.head.appendChild(s);
}

// Helpers ────────────────────────────────────────────────
// 한국식 가격 포맷: 124000 만원 → 12억 4,000
function won(man) {
  if (man == null || isNaN(man)) return '—';
  const eok = Math.floor(man / 10000);
  const rest = man % 10000;
  if (eok === 0) return rest.toLocaleString() + '만';
  if (rest === 0) return eok + '억';
  return eok + '억 ' + rest.toLocaleString();
}
function pct(n, digits = 2) {
  if (n == null || isNaN(n)) return '—';
  const sign = n > 0 ? '+' : '';
  return sign + n.toFixed(digits) + '%';
}
function delta(curr, prev) {
  if (!prev) return 0;
  return ((curr - prev) / prev) * 100;
}

Object.assign(window, { T, won, pct, delta });
