// Life Memory Tree — design tokens (v2 · "Cultivar").
//
// Modernized direction: crisp off-white paper, near-black green-ink, snappy
// chlorophyll greens, modern accent palette for relations. Less "antiqued
// memoir paper", more "calm contemporary product." Snappy + restrained.
//
// Type:
//   Display — Geist (tight tracking, sans, modern). Use .lmt-display.
//   Body    — Geist Sans (default).
//   Serif   — Newsreader, available via .lmt-serif for literary moments.
//   Mono    — JetBrains Mono (timestamps, IDs, technical chrome).

window.LMT = (function () {
  const color = {
    // Surfaces — cool off-whites, no more cream
    paper:        '#f4f4ee',  // page bg
    paperRaised:  '#ffffff',  // crisp cards
    paperSunken:  '#edede6',  // inputs, wells
    paperEdge:    '#dcdcd2',  // 1px borders
    paperHairline:'#e8e8df',  // subtle dividers

    // Ink — near-black, very subtle green undertone
    ink:          '#0d1410',  // primary text
    inkMuted:     '#54635c',  // secondary
    inkFaint:     '#8d958f',  // captions
    inkGhost:     '#b6bbb6',  // placeholder

    // Leaves (by recency — chlorophyll-forward, snappy)
    leafFresh:    '#15803d',  // < 7d, this week — deep emerald
    leafGreen:    '#22c55e',  // < 30d — bright green
    leafLime:     '#84cc16',  // < 90d — lime
    leafHoney:    '#eab308',  // < 365d — amber
    leafFaded:    '#a8a29e',  // older — warm stone
    leafBark:     '#1c1917',  // branches (near-black)
    leafTrunk:    '#0c0a09',  // trunk

    // Semantic accents (relations) — modern saturated palette
    causes:       '#dc2626',  // red — causal
    follows:      '#2563eb',  // blue — temporal
    similar:      '#9333ea',  // purple — semantic
    partOf:       '#0d9488',  // teal — hierarchical
    leadsTo:      '#d97706',  // amber-orange — leads-to
    inspires:     '#ea580c',  // vivid orange — inspires

    // Privacy bands (consent levels)
    privPrivate:  '#475569',  // slate
    privScene:    '#d97706',  // amber
    privPublic:   '#16a34a',  // green

    // Mood scale (1–5)
    mood1:        '#64748b',  // melancholy — slate
    mood2:        '#a8a29e',
    mood3:        '#eab308',
    mood4:        '#65a30d',
    mood5:        '#16a34a',  // joy

    // Status
    success:      '#16a34a',
    warning:      '#d97706',
    danger:       '#dc2626',
    info:         '#2563eb',
  };

  const radius = {
    xs: '4px',
    sm: '6px',
    md: '10px',
    lg: '14px',
    xl: '18px',
    pill: '999px',
  };

  const shadow = {
    leaf:   '0 1px 2px rgba(13,20,16,0.04), 0 2px 6px -2px rgba(13,20,16,0.06)',
    card:   '0 1px 0 rgba(13,20,16,0.04), 0 1px 3px rgba(13,20,16,0.04), 0 12px 32px -16px rgba(13,20,16,0.12)',
    float:  '0 8px 32px -8px rgba(13,20,16,0.18), 0 2px 8px rgba(13,20,16,0.06)',
    sheet:  '0 -8px 32px -4px rgba(13,20,16,0.16)',
    panel:  '0 0 0 1px rgba(220,220,210,0.8), 0 1px 2px rgba(13,20,16,0.04)',
    ring:   '0 0 0 1px rgba(13,20,16,0.06), 0 1px 2px rgba(13,20,16,0.04)',
  };

  // Days-since → leaf color (matches VISUALIZATION.md getLeafColor)
  function leafFor(daysSince) {
    if (daysSince == null) return color.leafFaded;
    if (daysSince < 7)   return color.leafFresh;
    if (daysSince < 30)  return color.leafGreen;
    if (daysSince < 90)  return color.leafLime;
    if (daysSince < 365) return color.leafHoney;
    return color.leafFaded;
  }

  function linkColor(relation) {
    return color[relation] || color.similar;
  }

  // Mood emoji + color
  const mood = [
    null,
    { label: 'low',     color: color.mood1, glyph: '◔' },
    { label: 'quiet',   color: color.mood2, glyph: '◑' },
    { label: 'level',   color: color.mood3, glyph: '◐' },
    { label: 'bright',  color: color.mood4, glyph: '◕' },
    { label: 'radiant', color: color.mood5, glyph: '●' },
  ];

  // Priority bucket meta (matches EnhancedTodayPanel.tsx)
  const buckets = {
    'time-bound': { label: 'Time-bound',  hint: 'has a deadline',         tone: '#dc2626', glyph: '◷' },
    'unblocker':  { label: 'Unblocker',   hint: 'unblocks others',        tone: '#d97706', glyph: '↯' },
    'quick-win':  { label: 'Quick win',   hint: 'small, immediate value', tone: '#16a34a', glyph: '✓' },
    'new':        { label: 'New',         hint: 'just landed',            tone: '#2563eb', glyph: '✦' },
  };

  // Connection-type meta (event_connections.connection_type)
  const connections = {
    'leads-to':    { label: 'leads to',   c: color.leadsTo,  arrow: '→' },
    'causes':      { label: 'causes',     c: color.causes,   arrow: '⇒' },
    'follows':     { label: 'follows',    c: color.follows,  arrow: '↪' },
    'depends-on':  { label: 'depends on', c: color.causes,   arrow: '⇠' },
    'same-theme':  { label: 'same theme', c: color.similar,  arrow: '↭' },
    'relates-to':  { label: 'relates to', c: color.similar,  arrow: '∿' },
    'part-of':     { label: 'part of',    c: color.partOf,   arrow: '⊂' },
    'inspires':    { label: 'inspires',   c: color.inspires, arrow: '✷' },
  };

  return { color, radius, shadow, leafFor, linkColor, mood, buckets, connections };
})();

// Inject the font stack + base resets once.
if (typeof document !== 'undefined' && !document.getElementById('lmt-fonts')) {
  const link = document.createElement('link');
  link.id = 'lmt-fonts';
  link.rel = 'stylesheet';
  link.href = 'https://fonts.googleapis.com/css2?family=Newsreader:ital,opsz,wght@0,6..72,400;0,6..72,500;0,6..72,600;1,6..72,400&family=Geist:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap';
  document.head.appendChild(link);

  const style = document.createElement('style');
  style.id = 'lmt-base';
  style.textContent = `
    .lmt {
      font-family: 'Geist', ui-sans-serif, system-ui, -apple-system, 'Segoe UI', sans-serif;
      color: #0d1410;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      font-feature-settings: "ss01", "cv11";
    }
    .lmt-display {
      font-family: 'Geist', ui-sans-serif, system-ui, sans-serif;
      letter-spacing: -0.028em;
      font-feature-settings: "ss01", "cv11", "cv09";
    }
    .lmt-serif {
      font-family: 'Newsreader', 'Iowan Old Style', Georgia, serif;
      font-feature-settings: "ss01";
      letter-spacing: -0.005em;
    }
    .lmt-mono {
      font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
      font-feature-settings: "ss02", "zero";
    }
    .lmt-tnum { font-variant-numeric: tabular-nums; }
    .lmt ::selection { background: #15803d33; }
    .lmt-paper { background: #f4f4ee; }
    .lmt-card { background: #ffffff; border: 1px solid #e8e8df; border-radius: 14px; }
    .lmt-card-r { background: #ffffff; border: 1px solid #e3e3d9; border-radius: 14px; box-shadow: 0 1px 0 rgba(13,20,16,0.04), 0 1px 3px rgba(13,20,16,0.04), 0 12px 32px -16px rgba(13,20,16,0.10); }
    .lmt-well { background: #edede6; border: 1px solid #dcdcd2; border-radius: 10px; }
    .lmt-hair { border-color: #e8e8df; }
    .lmt-grain { background-image: radial-gradient(circle at 1px 1px, rgba(13,20,16,0.04) 1px, transparent 0); background-size: 18px 18px; }
    .lmt-paper-noise { background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence baseFrequency='0.9' numOctaves='2' seed='3'/><feColorMatrix values='0 0 0 0 0.5 0 0 0 0 0.5 0 0 0 0 0.45 0 0 0 0.03 0'/></filter><rect width='160' height='160' filter='url(%23n)'/></svg>"); }
    .lmt-scroll::-webkit-scrollbar { width: 6px; height: 6px; }
    .lmt-scroll::-webkit-scrollbar-thumb { background: #c6cac4; border-radius: 3px; }
    .lmt-scroll::-webkit-scrollbar-track { background: transparent; }

    /* Modern focus ring */
    .lmt :focus-visible { outline: 2px solid #15803d; outline-offset: 2px; }

    /* Soft pulse for live indicators (ASR, recording) */
    @keyframes lmt-pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.55; transform: scale(0.92); } }
    .lmt-pulse { animation: lmt-pulse 1.6s ease-in-out infinite; }
    @keyframes lmt-leaf-sway { 0%, 100% { transform: rotate(-2deg); } 50% { transform: rotate(2deg); } }
    .lmt-sway { animation: lmt-leaf-sway 4s ease-in-out infinite; transform-origin: bottom center; }
    @keyframes lmt-shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } }
    .lmt-shimmer { background: linear-gradient(90deg, transparent, rgba(34,197,94,0.18), transparent); background-size: 200% 100%; animation: lmt-shimmer 1.6s linear infinite; }
  `;
  document.head.appendChild(style);
}
