// ui_primitives.jsx — テーマ対応のUI部品

const APP_FONT = "'Inter', -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', system-ui, sans-serif";

function TYPE_META(t) {
  return {
    memo:    { label: 'MEMO',    color: t.purple,  bg: t.mode === 'dark' ? '#2A1F3D' : '#F1ECFA' },
    idea:    { label: 'IDEA',    color: t.purple2, bg: t.mode === 'dark' ? '#2D1F42' : '#EEE6FC' },
    task:    { label: 'TASK',    color: t.orange,  bg: t.mode === 'dark' ? '#3A2418' : '#FFF1E5' },
    output:  { label: 'OUTPUT',  color: t.primary, bg: t.primarySoft },
    outcome: { label: 'OUTCOME', color: t.primary, bg: t.primarySoft },
  };
}

function relTime(ts) {
  const d = (Date.now() - ts) / (24 * 3600 * 1000);
  if (d < 1/(24*60)) return 'たった今';
  if (d < 1/24) return Math.round(d * 24 * 60) + '分前';
  if (d < 1) return Math.round(d * 24) + '時間前';
  if (d < 30) return Math.round(d) + '日前';
  return Math.round(d / 30) + 'ヶ月前';
}

function formatDate(ts) {
  const d = new Date(ts);
  const today = new Date();
  const y = today.getFullYear() === d.getFullYear();
  return y
    ? `${d.getMonth()+1}月${d.getDate()}日`
    : `${d.getFullYear()}/${d.getMonth()+1}/${d.getDate()}`;
}

function formatDateFull(ts) {
  const d = new Date(ts);
  const days = ['日', '月', '火', '水', '木', '金', '土'];
  return `${d.getMonth()+1}月${d.getDate()}日 ${days[d.getDay()]}`;
}

function formatTime(ts) {
  const d = new Date(ts);
  const h = String(d.getHours()).padStart(2, '0');
  const m = String(d.getMinutes()).padStart(2, '0');
  return `${h}:${m}`;
}

function isSameDay(a, b) {
  const da = new Date(a), db = new Date(b);
  return da.getFullYear() === db.getFullYear()
      && da.getMonth() === db.getMonth()
      && da.getDate() === db.getDate();
}

function dateKey(ts) {
  const d = new Date(ts);
  return `${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()}`;
}

function AppKicker({ children, color }) {
  const t = useT();
  return (
    <div style={{
      fontFamily: APP_FONT, fontSize: 10, fontWeight: 700,
      letterSpacing: '0.14em', textTransform: 'uppercase',
      color: color || t.primary,
    }}>{children}</div>
  );
}

function AppH2({ children, style = {} }) {
  const t = useT();
  return (
    <h2 style={{
      fontFamily: APP_FONT, fontSize: 22, fontWeight: 800,
      letterSpacing: '-0.035em', lineHeight: 1.2,
      color: t.ink, margin: 0, textWrap: 'balance', ...style,
    }}>{children}</h2>
  );
}

function IconBtn({ children, onClick, title, active, style = {} }) {
  const t = useT();
  return (
    <button onClick={onClick} title={title} aria-label={title} style={{
      background: active ? t.bgSoft : 'transparent',
      border: 'none', cursor: 'pointer', padding: 8,
      borderRadius: 8, color: active ? t.ink : t.ink3,
      display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
      minWidth: 36, minHeight: 36,
      WebkitTapHighlightColor: 'transparent',
      touchAction: 'manipulation',
      ...style,
    }}>{children}</button>
  );
}

// --- SVGアイコン（inline、軽量）---
const Icons = {
  plus: (s=16) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M12 5v14M5 12h14"/></svg>,
  check: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round"><polyline points="20 6 9 17 4 12"/></svg>,
  close: (s=16) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M18 6L6 18M6 6l12 12"/></svg>,
  inbox: (s=18) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><polyline points="22 12 16 12 14 15 10 15 8 12 2 12"/><path d="M5.45 5.11L2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z"/></svg>,
  timeline: (s=18) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><line x1="12" y1="20" x2="12" y2="10"/><line x1="18" y1="20" x2="18" y2="4"/><line x1="6" y1="20" x2="6" y2="16"/></svg>,
  sprout: (s=18) => <svg width={s} height={s} viewBox="0 0 32 32" fill="none"><path d="M16 27 C16 19, 10 15, 6 13 C8 21, 12 25, 16 27 Z" fill="currentColor" opacity="0.9"/><path d="M16 27 C16 19, 22 15, 26 13 C24 21, 20 25, 16 27 Z" fill="currentColor" opacity="0.55"/><path d="M16 27 L16 30" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/></svg>,
  settings: (s=18) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>,
  mic: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9"><rect x="9" y="2" width="6" height="12" rx="3"/><path d="M5 10a7 7 0 0014 0M12 17v4"/></svg>,
  cal: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9"><rect x="3" y="4" width="18" height="18" rx="2"/><path d="M16 2v4M8 2v4M3 10h18"/></svg>,
  more: (s=16) => <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor"><circle cx="5" cy="12" r="1.8"/><circle cx="12" cy="12" r="1.8"/><circle cx="19" cy="12" r="1.8"/></svg>,
  arrow: (s=12) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><polyline points="9 18 15 12 9 6"/></svg>,
  sun: (s=16) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg>,
  moon: (s=16) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M21 12.8A9 9 0 1111.2 3a7 7 0 009.8 9.8z"/></svg>,
  trash: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9"><path d="M3 6h18M8 6V4a2 2 0 012-2h4a2 2 0 012 2v2m3 0v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6h14z"/></svg>,
  download: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.9"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M7 10l5 5 5-5M12 15V3"/></svg>,
  sparkle: (s=14) => <svg width={s} height={s} viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l2.5 6.5L21 11l-6.5 2.5L12 20l-2.5-6.5L3 11l6.5-2.5z"/></svg>,
};

Object.assign(window, {
  APP_FONT, TYPE_META, relTime, formatDate, formatDateFull, formatTime, isSameDay, dateKey,
  AppKicker, AppH2, IconBtn, Icons,
});
