// CSG shared tokens, components, copy.
// Loaded before anything else; exposes everything on window.

const CSG = {
  // Brand colors from the design sheet
  blau: '#003A70',
  cyan: '#00B4C7',
  steingrau: '#D9DFE5',
  lichtgrau: '#F5F7FA',
  anthrazit: '#1A1F26',
  schiefergrau: '#515A65',
  silbergrau: '#AEB6BF',
  weiss: '#FFFFFF',
};

// Inject the Saira font + base resets once.
if (typeof document !== 'undefined' && !document.getElementById('csg-base-css')) {
  const link = document.createElement('link');
  link.rel = 'stylesheet';
  link.href = 'https://fonts.googleapis.com/css2?family=Saira:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap';
  document.head.appendChild(link);
  const s = document.createElement('style');
  s.id = 'csg-base-css';
  s.textContent = `
    .csg-root, .csg-root * { box-sizing: border-box; }
    .csg-root { font-family: 'Saira', system-ui, sans-serif; color: ${CSG.anthrazit}; line-height: 1.45; -webkit-font-smoothing: antialiased; }
    .csg-root img { display: block; max-width: 100%; }
    .csg-root h1, .csg-root h2, .csg-root h3, .csg-root h4 { font-weight: 600; letter-spacing: -0.01em; margin: 0; text-wrap: balance; }
    .csg-root p { margin: 0; text-wrap: pretty; }
    .csg-root a { color: inherit; text-decoration: none; }
    .csg-mono { font-family: 'JetBrains Mono', ui-monospace, monospace; font-feature-settings: 'ss01'; }
    .csg-eyebrow { font-family: 'JetBrains Mono', monospace; font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; color: ${CSG.cyan}; font-weight: 500; }
    .csg-btn { display: inline-flex; align-items: center; gap: 10px; padding: 14px 22px; font-family: inherit; font-weight: 500; font-size: 15px; border-radius: 2px; cursor: pointer; transition: all .18s ease; border: 0; }
    .csg-btn--primary { background: ${CSG.blau}; color: white; }
    .csg-btn--primary:hover { background: #002a55; transform: translateY(-1px); }
    .csg-btn--cyan { background: ${CSG.cyan}; color: ${CSG.blau}; }
    .csg-btn--cyan:hover { background: #00a0b2; }
    .csg-btn--ghost { background: transparent; color: ${CSG.blau}; border: 1px solid ${CSG.steingrau}; }
    .csg-btn--ghost:hover { border-color: ${CSG.cyan}; color: ${CSG.cyan}; }
    .csg-btn--dark { background: white; color: ${CSG.blau}; }
    .csg-link { display: inline-flex; align-items: center; gap: 8px; color: ${CSG.cyan}; font-weight: 500; font-size: 14px; cursor: pointer; }
    .csg-link:hover { gap: 12px; }
    .csg-link::after { content: '→'; transition: transform .15s; }
    /* Image placeholders */
    .csg-img-ph { background:
      repeating-linear-gradient(135deg, rgba(0,58,112,0.08) 0 1px, transparent 1px 14px),
      linear-gradient(180deg, ${CSG.lichtgrau}, ${CSG.steingrau});
      display: flex; align-items: flex-end; justify-content: flex-start; padding: 14px;
      font-family: 'JetBrains Mono', monospace; font-size: 10px; color: ${CSG.schiefergrau};
      letter-spacing: 0.08em; text-transform: uppercase; position: relative; overflow: hidden;
    }
    .csg-img-ph--dark { background:
      repeating-linear-gradient(135deg, rgba(0,180,199,0.10) 0 1px, transparent 1px 14px),
      linear-gradient(180deg, #062b50, ${CSG.blau});
      color: ${CSG.silbergrau};
    }
    .csg-img-ph::before { content: ''; position: absolute; inset: 14px; border: 1px solid rgba(255,255,255,0.5); pointer-events: none; mix-blend-mode: overlay; }
    .csg-dotgrid { background-image: radial-gradient(circle, currentColor 1px, transparent 1.2px); background-size: 14px 14px; }
  `;
  document.head.appendChild(s);
}

// ------- Signature curve (recurring brand asset) -------
function SignatureCurve({ color = CSG.cyan, stroke = 2, style = {}, variant = 'rise' }) {
  // Three subtle variants of the same growth curve
  const paths = {
    rise: 'M 0 280 C 80 278, 160 270, 220 250 S 360 180, 440 130 S 600 50, 720 20',
    swoop: 'M 0 240 C 120 240, 200 230, 280 200 S 480 80, 720 40',
    arc: 'M 0 260 Q 360 260, 360 140 T 720 20',
  };
  return (
    <svg viewBox="0 0 720 300" preserveAspectRatio="none" style={{ overflow: 'visible', ...style }}>
      <path d={paths[variant] || paths.rise} fill="none" stroke={color} strokeWidth={stroke} strokeLinecap="round" />
    </svg>
  );
}

// Animated draw-on version
function SignatureCurveAnimated({ color = CSG.cyan, stroke = 2, style = {}, dur = 1.6, delay = 0.3 }) {
  return (
    <svg viewBox="0 0 720 300" preserveAspectRatio="none" style={{ overflow: 'visible', ...style }}>
      <path
        d="M 0 280 C 80 278, 160 270, 220 250 S 360 180, 440 130 S 600 50, 720 20"
        fill="none" stroke={color} strokeWidth={stroke} strokeLinecap="round"
        style={{ strokeDasharray: 1100, strokeDashoffset: 1100, animation: `csg-draw ${dur}s ${delay}s ease-out forwards` }}
      />
      <style>{`@keyframes csg-draw { to { stroke-dashoffset: 0; } }`}</style>
    </svg>
  );
}

// Image placeholder with caption explaining what should be there
function ImgPH({ label, dark = false, style = {}, children, ratio }) {
  const aspect = ratio ? { aspectRatio: ratio } : null;
  return (
    <div className={`csg-img-ph ${dark ? 'csg-img-ph--dark' : ''}`} style={{ ...aspect, ...style }}>
      <span style={{ position: 'relative', zIndex: 1 }}>▦ {label}</span>
      {children}
    </div>
  );
}

// Dot-grid decorative block
function DotGrid({ rows = 8, cols = 8, dot = 3, gap = 12, color = CSG.cyan, style = {} }) {
  const dots = [];
  for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) {
    dots.push(<circle key={`${r}-${c}`} cx={c * gap + dot} cy={r * gap + dot} r={dot / 2} fill={color} />);
  }
  return (
    <svg width={cols * gap} height={rows * gap} style={style}>
      {dots}
    </svg>
  );
}

// ------- Site copy (DE + EN) -------
const COPY = {
  nav: {
    de: {
      whoweare: 'Über uns',
      services: 'Kernleistungen',
      studies: 'Leistungsspektrum',
      sectors: 'Ecosystem',
      careers: 'Karriere',
      contact: 'Kontakt',
      cta: 'Anfrage stellen',
    },
    en: {
      whoweare: 'About us',
      services: 'Core services',
      studies: 'Scope',
      sectors: 'Ecosystem',
      careers: 'Careers',
      contact: 'Contact',
      cta: 'Request a quote',
    },
  },
  menu: {
    whoweare: ['Wer wir sind', 'Team', 'athagoras Group', 'Karriere'],
    services: ['Studienkonzept', 'Projektmanagement', 'Monitoring', 'Datenmanagement', 'Biostatistik'],
    studies: ['MedTech', 'Pharma', 'Real World Evidence', 'DiGA', 'AbD', 'Erprobungsstudien (§137e)', 'ATMP'],
    sectors: ['athagoras Group', 'IGES Institut', 'IGES Pharma', 'athagoras MedTech', 'MIGx · Tech & AI'],
  },
};

// Five core services per the CSG profile
const SERVICES = [
  { key: 'studyconcept', name: 'Studienkonzept', en: 'Study concept', icon: '◇',
    desc: 'Von der Forschungsfrage zum einreichungsreifen Konzept — Machbarkeit, Studiendesign, SAP und regulatorische Strategie für BfArM, EMA, FDA oder Ethikkommissionen, gemeinsam von Beginn an festgelegt.' },
  { key: 'pm', name: 'Projektmanagement', en: 'Project management', icon: '◫',
    desc: 'Single Point of Contact für alle Studienaktivitäten — Standorte, Vendoren, Behörden. Flache Hierarchien, schnelle Entscheidungen. Von Phase II bis zum finalen Clinical Study Report.' },
  { key: 'monitoring', name: 'Monitoring', en: 'Monitoring', icon: '◉',
    desc: 'On-site SDV, Remote EDC-Review und Risk-Based Monitoring nach ICH-GCP E6(R3). Das D-A-CH-CRA-Netzwerk spricht die lokale Sprache und kennt die deutschen Klinik-Workflows.' },
  { key: 'dm', name: 'Datenmanagement', en: 'Data management', icon: '⌘',
    desc: 'Elektronische Datenerfassung (z.B. Oracle Clinical RDC), paper-basierte CRF, hybride Ansätze, ePRO und eDiary. CDISC-konforme SDTM- und ADaM-Datensätze — einreichungsreif.' },
  { key: 'stats', name: 'Biostatistik', en: 'Biostatistics', icon: 'Σ',
    desc: 'Früh statt spät: Fallzahlplanung, Randomisierung und SAP von der ersten Protokolldiskussion an. SAS®-Programmierung, CDISC-konforme TLFs für Ihren Clinical Study Report.' },
];

// Seven specialized areas — the CSG scope of services
const SECTORS = [
  { key: 'medtech', name: 'MedTech',
    desc: 'MDR- und IVDR-konforme klinische Prüfungen und Performance Studies — pre-launch Pivotalstudien für die CE-Kennzeichnung, post-launch PMCF, PMS und Materiovigilance.',
    kpi: 'MDR · IVDR · CE-Mark' },
  { key: 'pharma', name: 'Pharma',
    desc: 'Phase II/III pre-approval mit PV-Begleitung (SAE, SUSAR, RMP, DSMB) und post-approval NIS, PASS, Phase IV — inklusive Registerbetrieb und Versorgungsforschung.',
    kpi: 'Phase II–IV · PASS · NIS' },
  { key: 'rwe', name: 'Real World Evidence',
    desc: 'Primär- und Sekundärdaten, NIS, PASS, PMS, Chart Reviews, ePRO/eDiary. Etablierte Netzwerke zu Ärzt:innen und Kliniken im D-A-CH-Raum — Antworten aus dem Versorgungsalltag.',
    kpi: 'NIS · PASS · PMS' },
  { key: 'diga', name: 'DiGA',
    desc: 'Eine der ersten CROs in Deutschland mit DiGA-Spezialisierung. Vom klinischen Evidenznachweis („Positiver Versorgungseffekt") über den BfArM Fast-Track bis zur dauerhaften Aufnahme nach §139e SGB V.',
    kpi: 'BfArM Fast-Track · §139e' },
  { key: 'abd', name: 'AbD',
    desc: 'Anwendungsbegleitende Datenerhebung nach §35a SGB V — Protokolle und SAPs an G-BA/IQWiG-Anforderungen anpassen, eCRF für Real-World-Daten, qualifiziertes Monitoring von Registerdaten.',
    kpi: '§35a SGB V · G-BA · IQWiG' },
  { key: 'erp', name: 'Erprobungsstudien',
    desc: 'Der G-BA-Pfad nach §137e SGB V zur GKV-Erstattung von Medizinprodukten. Zwei vom G-BA mandatierte Studien in unserer Bilanz — von der Protokollgestaltung bis zum finalen Studienbericht.',
    kpi: '§137e SGB V · 2 G-BA-Mandate' },
  { key: 'atmp', name: 'ATMP',
    desc: 'Advanced Therapy Medicinal Products — Gen-, Zell- und Gewebetherapien. Frühe klinische Entwicklung, Phase II/III, Logistik für lebende Zellen und virale Vektoren, Langzeit-Nachbeobachtung.',
    kpi: 'EMA · Gen · Zell · CAR-T' },
];

// Study lifecycle stages — pre-approval through post-approval
const STUDIES = [
  { key: 'concept', name: 'Studienkonzept', sub: 'Pre-Approval', desc: 'Forschungsfrage, Studiendesign, Endpunkte, SAP & Power, GCP/MDR/IVDR — vom strategischen Ziel zum belastbaren Konzept.', phases: ['RCT', 'Pragmatic', 'Observational'] },
  { key: 'protocol', name: 'Protokoll & Regulatory', sub: 'Pre-Approval', desc: 'Protokoll-Entwicklung, IMPD, Patient Information, Investigator Brochure — sowie Scientific Advice mit BfArM, PEI, EMA, FDA.', phases: ['BfArM', 'PEI', 'EMA', 'FDA'] },
  { key: 'conduct', name: 'Conduct & Monitoring', sub: 'Phase II–IV', desc: 'Hybrides Monitoring (on-site SDV, Remote, RBM), Datenmanagement und SPOC-Projektsteuerung — bis zum sauberen Database-Lock.', phases: ['SDV', 'Remote', 'RBM'] },
  { key: 'lifecycle', name: 'Lifecycle & PV', sub: 'Post-Approval', desc: 'Phase IV, PMS, PASS, PAES, Label Updates. Pharmakovigilanz (PSUR, PBRER, DSUR), Materiovigilance und Signal Detection.', phases: ['PMS', 'PSUR', 'PBRER'] },
  { key: 'rwe', name: 'Real World Evidence', sub: 'Marktzugang', desc: 'RWE-Strategie, Register, AbD nach §35a, Erprobungsstudien nach §137e — Evidenz zur Sicherung der Erstattung im GKV-Markt.', phases: ['RWE', 'AbD', '§137e'] },
];

// Service × Study × Sector matrix (subset of services applicable per study type)
const MATRIX = {
  medtech:  { concept: ['studyconcept','pm','stats'], protocol: ['studyconcept','pm','stats'], conduct: ['pm','monitoring','dm','stats'], lifecycle: ['monitoring','dm','stats'], rwe: ['studyconcept','dm','stats'] },
  pharma:   { concept: ['studyconcept','pm','stats'], protocol: ['studyconcept','pm','stats'], conduct: ['pm','monitoring','dm','stats'], lifecycle: ['monitoring','dm','stats'], rwe: ['studyconcept','dm','stats'] },
  rwe:      { concept: ['studyconcept','stats'], protocol: ['studyconcept','stats'], conduct: ['dm','stats'], lifecycle: ['dm','stats'], rwe: ['studyconcept','dm','stats'] },
  diga:     { concept: ['studyconcept','pm','dm','stats'], protocol: ['studyconcept','pm'], conduct: ['pm','dm','stats'], lifecycle: ['dm','stats'], rwe: ['studyconcept','dm','stats'] },
  abd:      { concept: ['studyconcept','stats'], protocol: ['studyconcept','stats'], conduct: ['monitoring','dm','stats'], lifecycle: ['dm','stats'], rwe: ['studyconcept','dm','stats'] },
  erp:      { concept: ['studyconcept','pm','stats'], protocol: ['studyconcept','pm','stats'], conduct: ['pm','monitoring','dm','stats'], lifecycle: ['dm','stats'], rwe: ['studyconcept','dm','stats'] },
  atmp:     { concept: ['studyconcept','pm','stats'], protocol: ['studyconcept','pm','stats'], conduct: ['pm','monitoring','dm','stats'], lifecycle: ['monitoring','dm','stats'], rwe: ['studyconcept','dm','stats'] },
};

Object.assign(window, {
  CSG, SignatureCurve, SignatureCurveAnimated, ImgPH, DotGrid,
  COPY, SERVICES, SECTORS, STUDIES, MATRIX,
});
