// ============================================
// SHARED THEME — Dark UI for Google Ads MCP Apps
// ============================================

export const THEME_CSS = `
  :root {
    --bg-primary: #1a1a2e;
    --bg-card: #16213e;
    --bg-card-hover: #1a2744;
    --bg-accent: #0f3460;
    --bg-input: #0d1b2a;
    --highlight: #e94560;
    --highlight-dim: rgba(233, 69, 96, 0.15);
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0b8;
    --text-muted: #6c6c8a;
    --border: rgba(255, 255, 255, 0.06);
    --border-light: rgba(255, 255, 255, 0.1);
    --green: #00c897;
    --green-dim: rgba(0, 200, 151, 0.15);
    --yellow: #f0c040;
    --yellow-dim: rgba(240, 192, 64, 0.15);
    --red: #e94560;
    --red-dim: rgba(233, 69, 96, 0.15);
    --blue: #4ea8de;
    --blue-dim: rgba(78, 168, 222, 0.15);
    --purple: #9b5de5;
    --purple-dim: rgba(155, 93, 229, 0.15);
    --orange: #f48c06;
    --orange-dim: rgba(244, 140, 6, 0.15);
    --radius: 10px;
    --radius-sm: 6px;
    --shadow: 0 2px 12px rgba(0,0,0,0.25);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.35);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --mono: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  }

  * { margin: 0; padding: 0; box-sizing: border-box; }

  body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    padding: 20px;
  }

  .app-container {
    max-width: 1200px;
    margin: 0 auto;
  }

  /* ---- Header ---- */
  .app-header {
    margin-bottom: 24px;
  }
  .app-header h1 {
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.3px;
    color: var(--text-primary);
  }
  .app-header .subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    margin-top: 4px;
  }

  /* ---- Cards ---- */
  .card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    transition: border-color 0.2s, box-shadow 0.2s;
  }
  .card:hover {
    border-color: var(--border-light);
    box-shadow: var(--shadow);
  }
  .card-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 12px;
  }

  /* ---- Grid Layouts ---- */
  .grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
  .grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
  .grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
  .grid-6 { display: grid; grid-template-columns: repeat(6, 1fr); gap: 16px; }

  @media (max-width: 900px) {
    .grid-6 { grid-template-columns: repeat(3, 1fr); }
    .grid-4 { grid-template-columns: repeat(2, 1fr); }
    .grid-3 { grid-template-columns: repeat(2, 1fr); }
  }
  @media (max-width: 600px) {
    .grid-6, .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
  }

  /* ---- KPI Cards ---- */
  .kpi-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
    text-align: center;
  }
  .kpi-value {
    font-size: 26px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text-primary);
    line-height: 1.2;
  }
  .kpi-label {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
    margin-top: 6px;
  }
  .kpi-delta {
    font-size: 12px;
    font-weight: 600;
    margin-top: 6px;
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
  }
  .kpi-delta.up { color: var(--green); background: var(--green-dim); }
  .kpi-delta.down { color: var(--red); background: var(--red-dim); }
  .kpi-delta.neutral { color: var(--text-muted); background: var(--border); }

  /* ---- Tables ---- */
  .table-wrap {
    overflow-x: auto;
    border-radius: var(--radius);
    border: 1px solid var(--border);
  }
  table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
  }
  thead th {
    background: var(--bg-accent);
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    padding: 10px 14px;
    text-align: left;
    white-space: nowrap;
    border-bottom: 1px solid var(--border);
  }
  thead th.right, tbody td.right { text-align: right; }
  tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    white-space: nowrap;
  }
  tbody tr:last-child td { border-bottom: none; }
  tbody tr:hover { background: rgba(255,255,255,0.02); }

  /* ---- Pills / Badges ---- */
  .pill {
    display: inline-block;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 3px 10px;
    border-radius: 12px;
    line-height: 1.4;
  }
  .pill-green  { color: var(--green);  background: var(--green-dim); }
  .pill-yellow { color: var(--yellow); background: var(--yellow-dim); }
  .pill-red    { color: var(--red);    background: var(--red-dim); }
  .pill-blue   { color: var(--blue);   background: var(--blue-dim); }
  .pill-purple { color: var(--purple); background: var(--purple-dim); }
  .pill-orange { color: var(--orange); background: var(--orange-dim); }
  .pill-muted  { color: var(--text-muted); background: var(--border); }

  /* ---- Progress Bars ---- */
  .progress-track {
    background: var(--bg-input);
    border-radius: 6px;
    height: 8px;
    overflow: hidden;
    width: 100%;
  }
  .progress-fill {
    height: 100%;
    border-radius: 6px;
    transition: width 0.3s ease;
  }
  .progress-fill.green  { background: var(--green); }
  .progress-fill.yellow { background: var(--yellow); }
  .progress-fill.red    { background: var(--red); }
  .progress-fill.blue   { background: var(--blue); }
  .progress-fill.accent { background: var(--highlight); }

  /* ---- Section Spacing ---- */
  .section { margin-bottom: 24px; }
  .section-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 12px;
  }

  /* ---- Metric Inline ---- */
  .metric-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
  }
  .metric-row:last-child { border-bottom: none; }
  .metric-label { color: var(--text-secondary); font-size: 13px; }
  .metric-value { color: var(--text-primary); font-weight: 600; font-size: 14px; font-family: var(--mono); }

  /* ---- Bar Chart (CSS-only) ---- */
  .bar-chart {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    height: 100px;
    padding-top: 8px;
  }
  .bar-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
  }
  .bar {
    width: 100%;
    border-radius: 4px 4px 0 0;
    background: var(--highlight);
    min-height: 2px;
    transition: height 0.3s;
  }
  .bar-label {
    font-size: 10px;
    color: var(--text-muted);
    text-align: center;
  }

  /* ---- Quality Score Bar ---- */
  .qs-bar {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 100px;
  }
  .qs-track {
    flex: 1;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    overflow: hidden;
  }
  .qs-fill {
    height: 100%;
    border-radius: 3px;
  }
  .qs-num {
    font-size: 12px;
    font-weight: 700;
    font-family: var(--mono);
    min-width: 18px;
    text-align: right;
  }

  /* ---- Utility ---- */
  .text-green  { color: var(--green); }
  .text-yellow { color: var(--yellow); }
  .text-red    { color: var(--red); }
  .text-blue   { color: var(--blue); }
  .text-purple { color: var(--purple); }
  .text-muted  { color: var(--text-muted); }
  .text-mono   { font-family: var(--mono); }
  .mt-16 { margin-top: 16px; }
  .mt-24 { margin-top: 24px; }
  .mb-8  { margin-bottom: 8px; }
  .mb-16 { margin-bottom: 16px; }
  .flex { display: flex; }
  .flex-between { display: flex; justify-content: space-between; align-items: center; }
  .gap-8  { gap: 8px; }
  .gap-12 { gap: 12px; }
  .gap-16 { gap: 16px; }
  .wrap { flex-wrap: wrap; }
`;

/**
 * Wrap inner HTML in a full HTML document with the shared theme CSS
 */
export function wrapHtml(title: string, body: string): string {
  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>${escapeHtml(title)}</title>
  <style>${THEME_CSS}</style>
</head>
<body>
  <div class="app-container">
    ${body}
  </div>
</body>
</html>`;
}

/**
 * Escape HTML entities
 */
export function escapeHtml(str: string): string {
  return str
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}

/**
 * Format micros to currency string
 */
export function fmtMoney(micros: string | number): string {
  const num = typeof micros === "string" ? parseInt(micros, 10) : micros;
  if (isNaN(num)) return "$0.00";
  return "$" + (num / 1_000_000).toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
}

/**
 * Format number with commas
 */
export function fmtNum(value: string | number): string {
  const num = typeof value === "string" ? parseInt(value, 10) : value;
  if (isNaN(num)) return "0";
  return num.toLocaleString("en-US");
}

/**
 * Format percentage (input as decimal, e.g. 0.0534 → "5.34%")
 */
export function fmtPct(value: number): string {
  if (isNaN(value)) return "0.00%";
  return (value * 100).toFixed(2) + "%";
}

/**
 * Get status pill HTML
 */
export function statusPill(status: string): string {
  const s = status.toUpperCase();
  if (s === "ENABLED" || s === "ACTIVE") return `<span class="pill pill-green">${s}</span>`;
  if (s === "PAUSED") return `<span class="pill pill-yellow">${s}</span>`;
  if (s === "REMOVED" || s === "DISABLED") return `<span class="pill pill-red">${s}</span>`;
  return `<span class="pill pill-muted">${s}</span>`;
}

/**
 * Build a progress bar HTML
 */
export function progressBar(pct: number, color: string = "accent"): string {
  const clamped = Math.max(0, Math.min(100, pct));
  return `<div class="progress-track"><div class="progress-fill ${color}" style="width:${clamped}%"></div></div>`;
}
