// Palette · v2 design system (loaded once, scoped via .v2-app)
// Adapted from Keyword 3.2 mockup. Provides CSS custom properties + base
// utility classes (chip, btn, scroll, mono, tag) used across v2 stages.

(() => {
  if (document.getElementById('v2-design-system')) return;
  const css = `
    .v2-app {
      /* === v3.4 Steadfast Reliability palette === */
      --primary:      #006747;
      --primary-2:    #004D35;
      --primary-3:    #00583D;
      --primary-tint: rgba(0,103,71,.08);
      --primary-tint-2: rgba(0,103,71,.16);
      --secondary:    #FFB600;
      --secondary-tint: rgba(255,182,0,.18);
      --tertiary:     #004D35;

      --ink:    #1B2A24;
      --ink-2:  #2A3A33;
      --sub:    #4D5A53;
      --muted:  #7B867F;
      --rule:   #EAE6E1;
      --rule-2: #D6D1C8;

      --bg:     #FCFBF9;
      --bg-2:   #F4F1EC;
      --paper:  #FFFFFF;
      --paper-2: rgba(255,255,255,.7);
      --glass:  rgba(255,255,255,.7);
      --glass-2: rgba(255,255,255,.55);

      /* accent aliases — keep v2 names so existing components don't break */
      --accent:   #006747;
      --accent-2: #004D35;
      --accent-3: rgba(0,103,71,.08);
      --accent-soft: rgba(0,103,71,.12);

      /* monotone keyword type tokens — distinguished by stripe + METAPHOR gradient */
      --literal-bg:   #F4F1EC; --literal-ink:   #1B2A24; --literal-line:   #D6D1C8;
      --sensory-bg:   #F4F1EC; --sensory-ink:   #1B2A24; --sensory-line:   #D6D1C8;
      --emotion-bg:   #F4F1EC; --emotion-ink:   #1B2A24; --emotion-line:   #D6D1C8;
      --universal-bg: #F4F1EC; --universal-ink: #1B2A24; --universal-line: #D6D1C8;
      --metaphor-bg:  #F4F1EC; --metaphor-ink:  #1B2A24; --metaphor-line:  #D6D1C8;

      /* Workplace Coverage shadow recipes (multi-layer) */
      --shadow-1: rgba(0,0,0,.06) 0 0 0 1px, rgba(0,0,0,.04) 0 1px 1px -.5px;
      --shadow-2: rgba(0,0,0,.06) 0 0 0 1px, rgba(0,0,0,.06) 0 1px 1px -.5px, rgba(0,0,0,.06) 0 3px 3px -1.5px, rgba(0,0,0,.06) 0 6px 6px -3px, rgba(0,0,0,.04) 0 12px 12px -6px;
      --shadow-3: rgba(0,0,0,.035) 0 2.8px 2.2px, rgba(0,0,0,.047) 0 6.7px 5.3px, rgba(0,0,0,.06) 0 12.5px 10px;

      --radius-sm: 8px;
      --radius:    12px;
      --radius-lg: 16px;
      --radius-pill: 9999px;

      --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
      font-family: 'Pretendard', 'Inter', -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo', system-ui, sans-serif;
      font-feature-settings: "ss01", "cv11";
      color: var(--ink);
      background:
        radial-gradient(1200px 600px at 100% -10%, rgba(0,103,71,.05), transparent 60%),
        radial-gradient(900px 500px at -10% 110%, rgba(255,182,0,.06), transparent 55%),
        var(--bg);
    }
    .v2-app * { box-sizing: border-box; }
    .v2-app .mono { font-family: var(--font-mono); letter-spacing: .02em; }
    .v2-app .tag { font-family: var(--font-mono); font-size: 10px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; }
    .v2-app .scroll::-webkit-scrollbar { width: 8px; height: 8px; }
    .v2-app .scroll::-webkit-scrollbar-thumb { background: var(--rule-2); border-radius: 4px; }
    .v2-app .scroll::-webkit-scrollbar-track { background: transparent; }

    .v2-app .btn {
      display: inline-flex; align-items: center; justify-content: center; gap: 6px;
      height: 32px; padding: 0 14px; border-radius: var(--radius-pill);
      background: var(--paper); color: var(--ink);
      border: 1px solid var(--rule-2); font-size: 12px; font-weight: 600;
      cursor: pointer; font-family: inherit;
      transition: all .15s cubic-bezier(.4,0,.2,1);
    }
    .v2-app .btn:hover { background: var(--bg-2); border-color: var(--primary); color: var(--primary); }
    .v2-app .btn.sm { height: 26px; padding: 0 10px; font-size: 11px; gap: 4px; }
    .v2-app .btn.xs { height: 22px; padding: 0 8px; font-size: 10.5px; }
    .v2-app .btn.ghost { background: transparent; border-color: transparent; }
    .v2-app .btn.ghost:hover { background: var(--primary-tint); color: var(--primary); }
    .v2-app .btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
    .v2-app .btn.primary:hover { background: var(--primary-2); border-color: var(--primary-2); color: #fff; }
    .v2-app .btn.gold { background: var(--secondary); color: var(--primary-2); border-color: var(--secondary); }
    .v2-app .btn.gold:hover { background: #E8A600; border-color: #E8A600; }
    .v2-app .btn.link { border: none; background: transparent; color: var(--primary); border-radius: 0; padding: 2px 0; height: auto; border-bottom: 2px solid var(--primary); }
    .v2-app .btn.link:hover { color: var(--primary-2); background: transparent; border-bottom-color: var(--primary-2); }
    .v2-app .btn:disabled { opacity: .5; cursor: not-allowed; }

    .v2-app .chip {
      display: inline-flex; align-items: center; gap: 6px;
      padding: 5px 10px 5px 9px; border-radius: var(--radius-pill);
      font-size: 12px; line-height: 1; font-weight: 500; cursor: pointer;
      border: 1px solid var(--rule); background: var(--paper); color: var(--ink);
      transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease, color .15s ease;
      white-space: nowrap; user-select: none;
    }
    .v2-app .chip:hover { transform: translateY(-1px); box-shadow: 0 2px 6px rgba(0,103,71,.15); border-color: var(--primary); color: var(--primary); }
    .v2-app .chip .chip-type { font-family: var(--font-mono); font-size: 9px; letter-spacing: .08em; opacity: .6; font-weight: 600; }
    .v2-app .chip .chip-en { font-weight: 600; }
    .v2-app .chip .chip-ko { opacity: .65; font-size: 11px; font-weight: 400; }

    /* Monotone tiers — only METAPHOR gets gradient; others differentiate by stripe */
    .v2-app .chip.t-LITERAL,
    .v2-app .chip.t-SENSORY,
    .v2-app .chip.t-EMOTION,
    .v2-app .chip.t-UNIVERSAL { background: var(--paper); color: var(--ink); border-color: var(--rule); }
    .v2-app .chip.t-METAPHOR {
      background: linear-gradient(135deg, rgba(0,103,71,.06), rgba(255,182,0,.10));
      color: var(--primary-2); border-color: rgba(0,103,71,.35);
    }
    .v2-app .chip.t-METAPHOR:hover { background: linear-gradient(135deg, rgba(0,103,71,.12), rgba(255,182,0,.18)); }

    .v2-app .stripe { width: 3px; border-radius: 3px; align-self: stretch; }
    .v2-app .stripe.t-LITERAL,
    .v2-app .stripe.t-SENSORY,
    .v2-app .stripe.t-EMOTION   { background: var(--rule-2); }
    .v2-app .stripe.t-UNIVERSAL { background: var(--muted); }
    .v2-app .stripe.t-METAPHOR  { background: var(--primary); }

    /* glass surfaces (from v3.4) */
    .v2-app .glass {
      background: var(--glass);
      backdrop-filter: blur(12px) saturate(1.1);
      -webkit-backdrop-filter: blur(12px) saturate(1.1);
      border: 1px solid var(--rule);
      border-radius: var(--radius-lg);
    }
    .v2-app .glass-strong {
      background: var(--paper);
      border: 1px solid var(--rule);
      border-radius: var(--radius-lg);
      box-shadow: var(--shadow-2);
    }
    .v2-app .frame {
      background: var(--glass);
      backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
      border: 1px solid var(--rule);
      border-radius: var(--radius-lg);
    }

    /* gradient-bordered shell (decorative) */
    .v2-app .shell {
      padding: 1px;
      border-radius: calc(var(--radius-lg) + 1px);
      background: linear-gradient(135deg, rgba(0,103,71,.45), rgba(255,182,0,.25) 50%, rgba(0,103,71,.15));
    }
    .v2-app .shell > .inner { background: var(--paper); border-radius: var(--radius-lg); height: 100%; }

    /* corner ticks decoration */
    .v2-app .tick-corner { position: relative; }
    .v2-app .tick-corner::before, .v2-app .tick-corner::after {
      content: ''; position: absolute; width: 10px; height: 10px;
      border-color: var(--primary); border-style: solid;
    }
    .v2-app .tick-corner::before { top: -1px; left: -1px; border-width: 1.5px 0 0 1.5px; }
    .v2-app .tick-corner::after  { bottom: -1px; right: -1px; border-width: 0 1.5px 1.5px 0; }

    /* segmented control */
    .v2-app .seg {
      display: inline-flex; gap: 2px; padding: 3px;
      background: rgba(0,103,71,.06); border-radius: 9999px;
      border: 1px solid var(--rule);
    }
    .v2-app .seg button {
      border: none; background: transparent;
      padding: 5px 14px; border-radius: 9999px;
      font-size: 11px; font-weight: 500; color: var(--sub);
      cursor: pointer; font-family: inherit;
      transition: all .15s;
      display: inline-flex; align-items: center; gap: 6px;
    }
    .v2-app .seg button.active { background: var(--primary); color: #fff; font-weight: 700; }
    .v2-app .seg button:not(.active):hover { color: var(--primary); }

    /* tag pill — for tag-based sources view */
    .v2-app .tagpill {
      display: inline-flex; align-items: center; gap: 6px;
      padding: 4px 10px; border-radius: var(--radius-pill);
      border: 1px solid var(--rule); background: var(--paper);
      font-family: var(--font-mono); font-size: 10.5px; font-weight: 600;
      letter-spacing: .04em; color: var(--sub);
      cursor: pointer; transition: all .12s;
    }
    .v2-app .tagpill:hover { border-color: var(--primary); color: var(--primary); }
    .v2-app .tagpill.active { background: var(--primary); color: #fff; border-color: var(--primary); }
    .v2-app .tagpill .count {
      font-size: 9.5px; padding: 1px 5px; border-radius: 9999px;
      background: rgba(0,0,0,.08); color: inherit; opacity: .85;
    }
    .v2-app .tagpill.active .count { background: rgba(255,255,255,.18); }

    /* timeline rail — for timeline sources view */
    .v2-app .tl-rail { position: relative; padding-left: 18px; }
    .v2-app .tl-rail::before { content:''; position: absolute; left: 6px; top: 8px; bottom: 8px; width: 1px; background: var(--rule-2); }
    .v2-app .tl-dot {
      position: absolute; left: 0; top: 50%; transform: translateY(-50%);
      width: 13px; height: 13px; border-radius: 50%;
      background: var(--paper); border: 2px solid var(--rule-2);
    }
    .v2-app .tl-dot.done { background: var(--primary); border-color: var(--primary); }
    .v2-app .tl-dot.live { background: var(--secondary); border-color: var(--secondary); box-shadow: 0 0 0 4px rgba(255,182,0,.20); }

    .v2-app ::selection { background: rgba(255,182,0,.35); color: var(--primary-2); }
    .v2-app kbd { font-family: var(--font-mono); font-size: 9.5px; padding: 1px 5px; background: var(--paper); border: 1px solid var(--rule-2); border-radius: 3px; color: var(--ink); }

    @keyframes v2-pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
    @keyframes v2-slidein { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
    @keyframes v2-spin { to { transform: rotate(360deg); } }
    @keyframes v2-shimmer { 0% { background-position: -200% 0; } 100% { background-position: 200% 0; } }
    @keyframes v2-fade { from { opacity: 0; } to { opacity: 1; } }
    .v2-app [style*="animation: pulse"] { animation-name: v2-pulse !important; }
    .v2-app [style*="animation: slideIn"] { animation-name: v2-slidein !important; }
    .v2-app .v2-spinner { display: inline-block; width: 14px; height: 14px;
      border: 2px solid currentColor; border-right-color: transparent;
      border-radius: 50%; animation: v2-spin .8s linear infinite; vertical-align: middle; }
    .v2-app .v2-spinner.lg { width: 28px; height: 28px; border-width: 2.5px; }
    .v2-app .v2-spinner.sm { width: 11px; height: 11px; border-width: 1.6px; }
    .v2-app .v2-skeleton {
      background: linear-gradient(90deg, var(--paper-2) 0%, var(--bg-2) 50%, var(--paper-2) 100%);
      background-size: 200% 100%;
      animation: v2-shimmer 1.4s ease-in-out infinite;
      border-radius: 6px;
    }
    .v2-app .v2-fade-in { animation: v2-fade .2s ease-out; }
    .v2-app .v2-overlay {
      position: absolute; inset: 0; background: rgba(248,247,244,.78);
      backdrop-filter: blur(2px); display: flex; align-items: center; justify-content: center;
      flex-direction: column; gap: 12px; z-index: 10;
      animation: v2-fade .15s ease-out;
    }
    .v2-app .v2-overlay .v2-spinner { color: var(--accent); width: 32px; height: 32px; border-width: 3px; }
    .v2-app .v2-overlay .v2-overlay-msg { font-size: 12px; color: var(--sub); font-family: var(--font-mono); letter-spacing: .04em; }
  `;
  const tag = document.createElement('style');
  tag.id = 'v2-design-system';
  tag.textContent = css;
  document.head.appendChild(tag);
})();
