// Daygram design tokens — warm minimal, stamp metaphor.
// Two themes (light/dark) share the same shape; `T` is a mutable view that
// `applyDaygramTheme(mode)` mutates in place so existing references like
// `T.paper` re-read the current value on next React render.
const T_LIGHT = {
  paper:   '#FAF7F2',
  surface: '#FFFFFF',
  surface2:'#F3EFE7',
  hairline:'#EAE3D5',
  hairlineSoft:'#F0E9DC',

  ink:     '#1F1B16',
  ink2:    '#4A4138',
  sub:     '#7A6F62',
  muted:   '#B0A595',

  stamp:   '#C9573D',
  stampInk:'#A8442E',
  stampSoft:'#F2DACE',
  stampWash:'#FBEFE7',

  tent:    '#D9A285',
  tentSoft:'#F6E4D7',

  ok:      '#5B7F5A',
  okSoft:  '#E2EBDC',

  // Growth-tree foliage — used by the weekly tree progress bar.
  leaf:    '#6FA04C',
  leafDk:  '#5E8A3E',
};

const T_DARK = {
  // Neutral deep grays with a very subtle warm undertone — keeps the brand
  // warm without tinting everything yellow.
  paper:   '#141416',
  surface: '#1E1E22',
  surface2:'#272730',
  hairline:'#2F2F36',
  hairlineSoft:'#26262C',

  // Near-white ink — no cream tint.
  ink:     '#F2F2F4',
  ink2:    '#C8C8CF',
  sub:     '#8C8C96',
  muted:   '#5A5A62',

  // Terracotta accent stays warm and pops against the neutral dark surface.
  stamp:   '#E2674A',
  stampInk:'#F5B59A',
  stampSoft:'#4C261A',
  stampWash:'#2C1810',

  tent:    '#D49A7E',
  tentSoft:'#382C24',

  ok:      '#76A268',
  okSoft:  '#2B3826',

  // Growth-tree foliage — brighter so it reads against the dark hero.
  leaf:    '#7DB45A',
  leafDk:  '#5E8A3E',
};

const T = {
  ...T_LIGHT,
  font: '"Pretendard", -apple-system, BlinkMacSystemFont, system-ui, sans-serif',
  // Brand wordmark only — a sleek geometric sans, distinct from the body font.
  brandFont: '"Outfit", "Pretendard", sans-serif',
  mono: '"JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace',
};

function applyDaygramTheme(mode) {
  const src = mode === 'dark' ? T_DARK : T_LIGHT;
  Object.assign(T, src);
  try {
    if (typeof document !== 'undefined' && document.body) {
      document.body.style.background = T.paper;
      document.body.style.colorScheme = mode === 'dark' ? 'dark' : 'light';
    }
    localStorage.setItem('dg-theme', mode);
  } catch (e) {}
}

// Boot-time restore: do this before React mounts so first paint matches.
try {
  const _saved = localStorage.getItem('dg-theme');
  if (_saved === 'dark') applyDaygramTheme('dark');
} catch (e) {}

// Pip (새싹) — completion mark, used everywhere a "stamp" appears
function Stamp({ size = 28, rotate = -6, faded = false, style = {} }) {
  const uid = React.useId().replace(/:/g, '');
  const op = faded ? 0.35 : 1;
  const showFace = size >= 24;
  const leafG = `pipLeaf-${uid}`;
  const bodyG = `pipBody-${uid}`;
  return (
    <div style={{
      width: size, height: size,
      transform: `rotate(${rotate}deg)`,
      opacity: op,
      display: 'inline-flex',
      flexShrink: 0,
      ...style,
    }}>
      <svg width={size} height={size} viewBox="0 0 200 200">
        <defs>
          <linearGradient id={leafG} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="#A4C97A"/>
            <stop offset="1" stopColor="#6E9544"/>
          </linearGradient>
          <radialGradient id={bodyG} cx="0.4" cy="0.3">
            <stop offset="0" stopColor="#F8C896"/>
            <stop offset="1" stopColor="#C9784A"/>
          </radialGradient>
        </defs>
        <path d="M100 75 Q70 50 65 30 Q90 40 100 75 Z" fill={`url(#${leafG})`}/>
        <path d="M100 75 Q130 55 138 32 Q115 42 100 75 Z" fill={`url(#${leafG})`} opacity="0.85"/>
        <path d="M100 30 L100 80" stroke="#5C7A3A" strokeWidth="2.5" strokeLinecap="round"/>
        <ellipse cx="100" cy="125" rx="50" ry="42" fill={`url(#${bodyG})`}/>
        {showFace && (
          <g>
            <circle cx="84" cy="122" r="5" fill="#2A1410"/>
            <circle cx="116" cy="122" r="5" fill="#2A1410"/>
            <circle cx="82.5" cy="120" r="1.5" fill="#fff"/>
            <circle cx="114.5" cy="120" r="1.5" fill="#fff"/>
            <ellipse cx="74" cy="135" rx="6" ry="4" fill="#D04A22" opacity="0.3"/>
            <ellipse cx="126" cy="135" rx="6" ry="4" fill="#D04A22" opacity="0.3"/>
            <path d="M92 140 q8 6 16 0" stroke="#2A1410" strokeWidth="3" fill="none" strokeLinecap="round"/>
          </g>
        )}
      </svg>
    </div>
  );
}

const Icon = {
  spark: (c = T.ink, s = 16) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M10 2.5l1.6 4.4 4.4 1.6-4.4 1.6L10 14.5 8.4 10.1 4 8.5l4.4-1.6L10 2.5z" stroke={c} strokeWidth="1.4" strokeLinejoin="round"/>
      <path d="M16 13.5l.7 1.8 1.8.7-1.8.7-.7 1.8-.7-1.8-1.8-.7 1.8-.7L16 13.5z" stroke={c} strokeWidth="1.2" strokeLinejoin="round"/>
    </svg>
  ),
  send: (c, s = 18) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M3 10l14-7-7 14-2-5-5-2z" stroke={c} strokeWidth="1.6" strokeLinejoin="round" strokeLinecap="round"/>
    </svg>
  ),
  chev: (dir='r', c=T.ink, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none" style={{ transform: dir==='l'?'rotate(180deg)':dir==='d'?'rotate(90deg)':dir==='u'?'rotate(-90deg)':'' }}>
      <path d="M7 4l6 6-6 6" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  plus: (c, s=18) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M10 4v12M4 10h12" stroke={c} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
  clock: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="10" cy="10" r="7.2" stroke={c} strokeWidth="1.4"/>
      <path d="M10 6v4.2L13 12" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  fire: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M10 2.5c.6 2.4 3.8 3.4 3.8 7.3 0 3.4-2.4 5.7-3.8 6.4 0 0 .8-2-.2-3.4-.7-1-2.1-1-2.1-2.8 0-.9.5-1.8 1.1-2.2-.5 2 1 2.6 1 2.6s-1.4-3.3.2-7.9z" stroke={c} strokeWidth="1.4" strokeLinejoin="round"/>
    </svg>
  ),
  check: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M4 10.5l4 4 8-9" stroke={c} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  grip: (c=T.muted, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="7" cy="6" r="1.1" fill={c}/><circle cx="13" cy="6" r="1.1" fill={c}/>
      <circle cx="7" cy="10" r="1.1" fill={c}/><circle cx="13" cy="10" r="1.1" fill={c}/>
      <circle cx="7" cy="14" r="1.1" fill={c}/><circle cx="13" cy="14" r="1.1" fill={c}/>
    </svg>
  ),
  user: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="10" cy="7" r="3" stroke={c} strokeWidth="1.4"/>
      <path d="M3.5 17c1.2-3.2 3.7-4.8 6.5-4.8s5.3 1.6 6.5 4.8" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  cal: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <rect x="3" y="4.5" width="14" height="12.5" rx="2" stroke={c} strokeWidth="1.4"/>
      <path d="M3 8.5h14M7 3v3M13 3v3" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  chart: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M3 17V8M9 17V4M15 17v-6" stroke={c} strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  tag: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M3 7.5V4a1 1 0 011-1h3.5a2 2 0 011.4.6l6.5 6.5a2 2 0 010 2.8l-3.6 3.6a2 2 0 01-2.8 0L3.6 9.9A2 2 0 013 8.5z" stroke={c} strokeWidth="1.4" strokeLinejoin="round"/>
      <circle cx="7" cy="7" r="1.2" fill={c}/>
    </svg>
  ),
  people: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="7" cy="7" r="2.5" stroke={c} strokeWidth="1.4"/>
      <circle cx="14" cy="8" r="2" stroke={c} strokeWidth="1.4"/>
      <path d="M2 16c.8-2.4 2.7-3.6 5-3.6s4.2 1.2 5 3.6" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
      <path d="M13 12.6c2 0 3.6 1 4.4 2.8" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  search: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="9" cy="9" r="5.5" stroke={c} strokeWidth="1.6"/>
      <path d="M13.5 13.5l3.5 3.5" stroke={c} strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  bell: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M5 14h10v-1.5l-1.5-1V8a3.5 3.5 0 0 0-7 0v3.5L5 12.5V14z" stroke={c} strokeWidth="1.4" strokeLinejoin="round"/>
      <path d="M8 16a2 2 0 0 0 4 0" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  settings: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <circle cx="10" cy="10" r="2.5" stroke={c} strokeWidth="1.4"/>
      <path d="M10 2v2.2M10 15.8V18M2 10h2.2M15.8 10H18M4.3 4.3l1.5 1.5M14.2 14.2l1.5 1.5M4.3 15.7l1.5-1.5M14.2 5.8l1.5-1.5" stroke={c} strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  trash: (c, s=14) => (
    <svg width={s} height={s} viewBox="0 0 20 20" fill="none">
      <path d="M4 6h12M8 6V4.5a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1V6M6 6l.7 9a1.3 1.3 0 0 0 1.3 1.2h4a1.3 1.3 0 0 0 1.3-1.2L15 6" stroke={c} strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
};

Object.assign(window, { T, Stamp, Icon, applyDaygramTheme });
