---
// src/components/AIQueryInterface.astro

import { getToolsData } from '../utils/dataService.js';

const data = await getToolsData();
const tools = data.tools;
const phases = data.phases;
const domainAgnosticSoftware = data['domain-agnostic-software'] || [];
---

<section id="ai-interface" class="ai-interface hidden">
  <div class="ai-query-section">
    <div class="content-center-lg">
      <h2 style="margin-bottom: 1rem; color: var(--color-primary);">
        <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-right: 0.75rem; vertical-align: middle;">
          <path d="M9 11H5a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7a2 2 0 0 0-2-2h-4"/>
          <path d="M9 11V7a3 3 0 0 1 6 0v4"/>
        </svg>
        Forensic AI
      </h2>
      <p id="ai-description" class="text-muted" style="max-width: 700px; margin: 0 auto; line-height: 1.6;">
        Beschreiben Sie Ihr forensisches Szenario und erhalten Sie maßgeschneiderte Workflow-Empfehlungen 
        basierend auf bewährten DFIR-Workflows und der verfügbaren Software-Datenbank.
      </p>
    </div>

    <div class="ai-input-container" style="max-width: 1000px; margin: 0 auto;">
      <!-- Mode Toggle -->
      <div class="ai-mode-toggle">
        <span id="workflow-label" class="toggle-label active">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="mr-2 align-middle">
            <polyline points="9,11 12,14 22,4"/>
            <path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/>
          </svg>
          Workflow-Empfehlung
        </span>
        
        <div class="toggle-switch">
          <div class="toggle-slider"></div>
        </div>
        
        <span id="tool-label" class="toggle-label">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="mr-2 align-middle">
            <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>
          </svg>
          Spezifische Software oder Methode
        </span>
      </div>

      <!-- Input Layout -->
      <div class="ai-input-layout">
        <div class="ai-textarea-section">
          <textarea 
            id="ai-query-input" 
            placeholder="Beschreiben Sie Ihr forensisches Szenario..."
            style="min-height: 220px; resize: vertical; font-size: 0.9375rem; line-height: 1.5;"
            maxlength="2000"
          ></textarea>
          <div id="ai-char-counter" style="font-size: 0.75rem; color: var(--color-text-secondary); text-align: right; margin-top: 0.25rem;">0/2000</div>
        </div>
        
        <div class="ai-suggestions-section">
          <div id="smart-prompting-hint" class="smart-prompting-hint">
            <div class="hint-card">
              <div class="hint-icon">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="var(--color-accent)" stroke-width="2">
                  <circle cx="12" cy="12" r="10"/>
                  <path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/>
                  <line x1="12" y1="17" x2="12.01" y2="17"/>
                </svg>
              </div>
              <div class="hint-content">
                <h4 class="hint-title">Intelligente Hilfe</h4>
                <p class="hint-description">
                  Während Sie tippen, analysiert die KI Ihre Eingabe und schlägt gezielten Fragen vor.
                </p>
                <div class="hint-trigger">
                  <span class="hint-label">Aktiviert ab:</span>
                  <span class="hint-value">40+ Zeichen</span>
                </div>
              </div>
            </div>
          </div>

          <div id="smart-prompting-container" class="smart-prompting-container hidden">
            <div class="prompting-card">
              <div id="prompting-status" class="prompting-status">
                <div class="status-icon">💡</div>
                <span class="status-text">Analysiere Eingabe...</span>
                <div id="prompting-spinner" class="prompting-spinner hidden">
                  <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="var(--color-accent)" stroke-width="2">
                    <path d="M21 12a9 9 0 11-6.219-8.56"/>
                  </svg>
                </div>
              </div>
              
              <div id="suggested-questions" class="suggested-questions hidden">
                <div class="suggestions-header">
                  <span class="suggestions-label">Zur besseren Analyse:</span>
                </div>
                <div id="questions-list" class="questions-list"></div>
                <button id="dismiss-suggestions" class="dismiss-button">
                  <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <line x1="18" y1="6" x2="6" y2="18"/>
                    <line x1="6" y1="6" x2="18" y2="18"/>
                  </svg>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- Privacy Notice -->
      <div style="margin-top: 0.5rem; margin-bottom: 1rem;">
        <p style="font-size: 0.75rem; color: var(--color-text-secondary); text-align: center; line-height: 1.4;">
          <svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-right: 0.25rem; vertical-align: middle;">
            <circle cx="12" cy="12" r="10"/>
            <line x1="12" y1="8" x2="12" y2="12"/>
            <line x1="12" y1="16" x2="12.01" y2="16"/>
          </svg>
          Ihre Anfrage wird über die API von mistral.ai übertragen.
          <a href="https://mistral.ai/privacy-policy/" target="_blank" rel="noopener noreferrer" style="color: var(--color-primary);">Datenschutzrichtlinien</a>
        </p>
      </div>
      
      <!-- Submit Button -->
      <div style="display: flex; justify-content: center; gap: 1rem;">
        <button id="ai-submit-btn" class="btn btn-accent" style="padding: 0.75rem 2rem;">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="margin-right: 0.5rem;">
            <path d="M14.828 14.828a4 4 0 0 1-5.656 0"/>
            <path d="M9 9a3 3 0 1 1 6 0c0 .749-.269 1.433-.73 1.96L11 14v1a1 1 0 0 1-1 1h-1a1 1 0 0 1-1-1v-1l-3.27-3.04A3 3 0 0 1 5 9a3 3 0 0 1 6 0"/>
          </svg>
          <span id="submit-btn-text">Empfehlungen generieren</span>
        </button>
      </div>
    </div>

    <!-- Loading State -->
    <div id="ai-loading" class="ai-loading hidden">
      <div style="text-align: center; padding: 2rem;">
        <div style="margin-bottom: 1rem;">
          <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="var(--color-primary)" stroke-width="2" style="animation: pulse 2s ease-in-out infinite;">
            <path d="M14.828 14.828a4 4 0 0 1-5.656 0"/>
            <path d="M9 9a3 3 0 1 1 6 0c0 .749-.269 1.433-.73 1.96L11 14v1a1 1 0 0 1-1 1h-1a1 1 0 0 1-1-1v-1l-3.27-3.04A3 3 0 0 1 5 9a3 3 0 0 1 6 0"/>
          </svg>
        </div>
        <p id="loading-text" class="text-secondary">Analysiere Szenario und generiere Empfehlungen...</p>
        
        <!-- Queue Status -->
        <div id="queue-status" class="queue-status-card hidden">
          <div class="queue-header">
            <div class="queue-position-display">
              <div id="queue-position-badge" class="position-badge">1</div>
              <span class="position-label">Position in Warteschlange</span>
            </div>
          </div>
          
          <div class="queue-details">
            <div class="queue-stat">
              <span class="stat-label">Warteschlange:</span>
              <span id="queue-length" class="stat-value">0</span>
            </div>
            <div class="queue-stat">
              <span class="stat-label">Geschätzte Zeit:</span>
              <span id="estimated-time" class="stat-value">--</span>
            </div>
          </div>
          
          <!-- Micro-task Progress -->
          <div id="micro-task-progress" class="micro-task-progress hidden">
            <div class="micro-task-header">
              <span class="micro-task-label">🔬 micro-Agent-Analysis</span>
              <span id="micro-task-counter" class="micro-task-counter">1/6</span>
            </div>
            <div class="micro-task-steps">
              <div class="micro-step" data-step="scenario">📋 Problemanalyse</div>
              <div class="micro-step" data-step="approach">🎯 Ermittlungsansatz</div>
              <div class="micro-step" data-step="considerations">⚠️ Herausforderungen</div>
              <div class="micro-step" data-step="tools">🔧 Methoden</div>
              <div class="micro-step" data-step="knowledge">📚 Evaluation</div>
              <div class="micro-step" data-step="final">✅ Audit-Trail</div>
            </div>
          </div>
          
          <div class="queue-progress-container">
            <div class="queue-progress-track">
              <div id="queue-progress" class="queue-progress-fill"></div>
            </div>
            <div class="task-id-display">
              <span class="task-label">Task:</span>
              <code id="current-task-id" class="task-id">--</code>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- Error State -->
    <div id="ai-error" class="ai-error hidden">
      <div style="text-align: center; padding: 2rem;">
        <div style="background-color: var(--color-error); color: white; padding: 1rem; border-radius: 0.5rem; max-width: 600px; margin: 0 auto;">
          <h3 style="margin-bottom: 0.5rem;">Fehler bei der KI-Anfrage</h3>
          <p id="ai-error-message" style="margin: 0;">Ein unerwarteter Fehler ist aufgetreten.</p>
        </div>
      </div>
    </div>

    <!-- Results -->
    <div id="ai-results" class="ai-results hidden">     
      <div id="audit-trail-container"></div>
    </div>
  </div>
</section>

<script type="module" define:vars={{ tools, phases, domainAgnosticSoftware }}>

const Utils = {
  phaseConfig: {
    'initialization': { icon: '🚀', displayName: 'Initialisierung' },
    'retrieval': { icon: '🔍', displayName: 'Datensuche' },
    'selection': { icon: '🎯', displayName: 'Tool-Auswahl' },
    'micro-task': { icon: '⚡', displayName: 'Detail-Analyse' },
    'validation': { icon: '✓', displayName: 'Validierung' },
    'completion': { icon: '✅', displayName: 'Finalisierung' }
  },

  actionTranslations: {
    'pipeline-start': 'Analyse gestartet',
    'embeddings-search': 'Ähnliche Tools gesucht', 
    'ai-tool-selection': 'Tools automatisch ausgewählt',
    'ai-analysis': 'KI-Analyse durchgeführt',
    'phase-tool-selection': 'Phasen-Tools evaluiert',
    'tool-evaluation': 'Tool-Bewertung erstellt',
    'background-knowledge-selection': 'Hintergrundwissen ausgewählt',
    'confidence-scoring': 'Vertrauenswertung berechnet',
    'pipeline-end': 'Analyse abgeschlossen'
  },

  getActionDisplayName(action) {
    return this.actionTranslations[action] || action;
  },

  formatDuration(ms) {
    if (ms < 1000) return '< 1s';
    if (ms < 60000) return `${Math.ceil(ms / 1000)}s`;
    const minutes = Math.floor(ms / 60000);
    const seconds = Math.ceil((ms % 60000) / 1000);
    return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
  },

  getConfidenceColor(confidence) {
    if (confidence >= 80) return 'var(--color-accent)';
    if (confidence >= 60) return 'var(--color-warning)';
    return 'var(--color-error)';
  },

  escapeHtml(text) {
    if (typeof text !== 'string') return String(text);
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
  },

  truncateText(text, maxLength) {
    if (!text || text.length <= maxLength) return text;
    return text.slice(0, maxLength) + '...';
  },

  sanitizeText(text) {
    if (typeof text !== 'string') return '';
    
    return text
      .replace(/^#{1,6}\s+/gm, '')
      .replace(/^\s*[-*+]\s+/gm, '')
      .replace(/^\s*\d+\.\s+/gm, '')
      .replace(/\*\*(.+?)\*\*/g, '$1')
      .replace(/\*(.+?)\*/g, '$1')
      .replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
      .replace(/```[\s\S]*?```/g, '[CODE BLOCK]')
      .replace(/`([^`]+)`/g, '$1')
      .replace(/<[^>]+>/g, '')
      .replace(/\n\s*\n\s*\n/g, '\n\n')
      .trim();
  },

  summarizeData(data) {
    if (data === null || data === undefined) return 'null';
    if (typeof data === 'string') {
      return data.length > 100 ? data.slice(0, 100) + '...' : data;
    }
    if (typeof data === 'number' || typeof data === 'boolean') {
      return data.toString();
    }
    if (Array.isArray(data)) {
      if (data.length === 0) return '[]';
      if (data.length <= 3) return JSON.stringify(data);
      return `[${data.slice(0, 3).map(i => typeof i === 'string' ? i : JSON.stringify(i)).join(', ')}, ...+${data.length - 3}]`;
    }
    if (typeof data === 'object') {
      const keys = Object.keys(data);
      if (keys.length === 0) return '{}';
      if (keys.length <= 3) {
        return '{' + keys.map(k => `${k}: ${typeof data[k] === 'string' ? data[k].slice(0, 20) + (data[k].length > 20 ? '...' : '') : JSON.stringify(data[k])}`).join(', ') + '}';
      }
      return `{${keys.slice(0, 3).join(', ')}, ...+${keys.length - 3} keys}`;
    }
    return String(data);
  },

  showElement(element) {
    if (element) {
      element.style.display = 'block';
      element.classList.remove('hidden');
    }
  },

  hideElement(element) {
    if (element) {
      element.style.display = 'none';
      element.classList.add('hidden');
    }
  }
};

class AuditTrailProcessor {
  static processAuditTrail(rawAuditTrail) {
    if (!rawAuditTrail || !Array.isArray(rawAuditTrail) || rawAuditTrail.length === 0) {
      console.log('[AUDIT] No audit trail data provided');
      return null;
    }

    try {
      console.log('[AUDIT] Processing', rawAuditTrail.length, 'audit entries');

      const totalTime = rawAuditTrail.reduce((sum, entry) => sum + (entry.processingTimeMs || 0), 0);
      const validConfidenceEntries = rawAuditTrail.filter(entry => typeof entry.confidence === 'number');
      const avgConfidence = validConfidenceEntries.length > 0 
        ? Math.round(validConfidenceEntries.reduce((sum, entry) => sum + entry.confidence, 0) / validConfidenceEntries.length)
        : 0;
      
      const highConfidenceSteps = rawAuditTrail.filter(entry => (entry.confidence || 0) >= 80).length;
      const lowConfidenceSteps = rawAuditTrail.filter(entry => (entry.confidence || 0) < 60).length;

      const groupedEntries = rawAuditTrail.reduce((groups, entry) => {
        const phase = entry.phase || 'unknown';
        if (!groups[phase]) groups[phase] = [];
        groups[phase].push(entry);
        return groups;
      }, {});

      const phases = Object.entries(groupedEntries).map(([phase, entries]) => {
        const phaseConfig = Utils.phaseConfig[phase] || { icon: '📋', displayName: phase };
        const validEntries = entries.filter(entry => entry && typeof entry === 'object');
        
        const phaseAvgConfidence = validEntries.length > 0 
          ? Math.round(validEntries.reduce((sum, entry) => sum + (entry.confidence || 0), 0) / validEntries.length)
          : 0;
        
        const phaseTotalTime = validEntries.reduce((sum, entry) => sum + (entry.processingTimeMs || 0), 0);

        return {
          name: phase,
          icon: phaseConfig.icon,
          displayName: phaseConfig.displayName,
          avgConfidence: phaseAvgConfidence,
          totalTime: phaseTotalTime,
          entries: validEntries.map(e => this.compressEntry(e)).filter(e => e !== null)
        };
      }).filter(phase => phase.entries.length > 0);

      return {
        totalTime,
        avgConfidence,
        stepCount: rawAuditTrail.length,
        highConfidenceSteps,
        lowConfidenceSteps,
        phases,
        summary: this.generateSummary(rawAuditTrail, avgConfidence, lowConfidenceSteps)
      };
      
    } catch (error) {
      console.error('[AUDIT] Error processing audit trail:', error);
      return null;
    }
  }

  static compressEntry(entry) {
    if (!entry || typeof entry !== 'object') {
      console.warn('[AUDIT] Invalid audit entry:', entry);
      return null;
    }

    try {
      return {
        timestamp: entry.timestamp || Date.now(),
        phase: entry.phase || 'unknown',
        action: entry.action || 'unknown',
        inputSummary: Utils.summarizeData(entry.input),
        outputSummary: Utils.summarizeData(entry.output),
        confidence: entry.confidence || 0,
        processingTimeMs: entry.processingTimeMs || 0,
        metadata: entry.metadata || {}
      };
    } catch (error) {
      console.error('[AUDIT] Error compressing entry:', error);
      return null;
    }
  }

  static generateSummary(entries, avgConfidence, lowConfidenceSteps) {
    let analysisQuality;
    if (avgConfidence >= 85 && lowConfidenceSteps === 0) {
      analysisQuality = 'excellent';
    } else if (avgConfidence >= 70 && lowConfidenceSteps <= 1) {
      analysisQuality = 'good';
    } else if (avgConfidence >= 60 && lowConfidenceSteps <= 3) {
      analysisQuality = 'fair';
    } else {
      analysisQuality = 'poor';
    }

    const keyInsights = [];
    const embeddingsUsed = entries.some(e => e.action === 'embeddings-search');
    if (embeddingsUsed) {
      keyInsights.push('Semantische Suche wurde erfolgreich eingesetzt');
    }

    const toolSelectionEntries = entries.filter(e => e.action === 'ai-tool-selection');
    if (toolSelectionEntries.length > 0) {
      const avgSelectionConfidence = toolSelectionEntries.reduce((sum, e) => sum + e.confidence, 0) / toolSelectionEntries.length;
      if (avgSelectionConfidence >= 80) {
        keyInsights.push('Hohe Konfidenz bei der Tool-Auswahl');
      }
    }

    const potentialIssues = [];
    if (lowConfidenceSteps > 2) {
      potentialIssues.push(`${lowConfidenceSteps} Analyseschritte mit niedriger Konfidenz`);
    }

    const longSteps = entries.filter(e => e.processingTimeMs > 5000);
    if (longSteps.length > 0) {
      potentialIssues.push(`${longSteps.length} Schritte benötigten mehr als 5 Sekunden`);
    }

    return { analysisQuality, keyInsights, potentialIssues };
  }
}

class AuditTrailRenderer {
  constructor(containerId, options = {}) {
    this.containerId = containerId;
    this.options = {
      title: options.title || 'KI-Entscheidungspfad',
      collapsible: options.collapsible !== false,
      defaultExpanded: options.defaultExpanded || false,
      ...options
    };
    this.componentId = `audit-trail-${Date.now()}-${Math.random().toString(36).substr(2, 6)}`;
  }

  render(rawAuditTrail) {
    const container = document.getElementById(this.containerId);
    if (!container) {
      console.error(`[AUDIT RENDERER] Container ${this.containerId} not found`);
      return;
    }

    if (!rawAuditTrail || !Array.isArray(rawAuditTrail) || rawAuditTrail.length === 0) {
      this.renderEmpty(container);
      return;
    }

    try {
      const processedAudit = AuditTrailProcessor.processAuditTrail(rawAuditTrail);
      
      if (processedAudit && processedAudit.phases && processedAudit.phases.length > 0) {
        this.renderProcessed(container, processedAudit);
        setTimeout(() => this.attachEventHandlers(), 0);
      } else {
        this.renderEmpty(container);
      }
    } catch (error) {
      console.error('[AUDIT RENDERER] Failed to render audit trail:', error);
      this.renderError(container, error);
    }
  }

  renderProcessed(container, processedAudit) {
    const detailsId = `${this.componentId}-details`;
    
    container.innerHTML = `
      <div class="audit-trail-container">
        <div class="audit-trail-header ${this.options.collapsible ? 'clickable' : ''}" 
             ${this.options.collapsible ? `data-target="${detailsId}"` : ''}>
          <div class="audit-trail-title">
            <div class="audit-icon">
              <div class="audit-icon-gradient">✓</div>
              <h4>${this.options.title}</h4>
            </div>
            
            <div class="audit-stats">
              <div class="stat-item">
                <div class="stat-dot stat-time"></div>
                <span>${Utils.formatDuration(processedAudit.totalTime)}</span>
              </div>
              <div class="stat-item">
                <div class="stat-dot" style="background-color: ${Utils.getConfidenceColor(processedAudit.avgConfidence)}"></div>
                <span>${processedAudit.avgConfidence}% Vertrauen</span>
              </div>
              <div class="stat-item">
                <span>${processedAudit.stepCount} Schritte</span>
              </div>
            </div>
          </div>
          
          ${this.options.collapsible ? `
            <div class="toggle-icon">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <polyline points="6 9 12 15 18 9"/>
              </svg>
            </div>
          ` : ''}
        </div>
        
        <div id="${detailsId}" class="audit-trail-details ${this.options.collapsible && !this.options.defaultExpanded ? 'collapsed' : ''}">
          ${this.renderSummary(processedAudit)}
          ${this.renderProcessFlow(processedAudit)}
          ${this.renderTechnicalDetails(processedAudit)}
        </div>
      </div>
    `;
  }

  renderSummary(audit) {
    return `
      <div class="audit-summary">
        <div class="summary-header">📊 Analyse-Qualität</div>
        <div class="summary-grid">
          <div class="summary-stat">
            <div class="summary-value success">${audit.highConfidenceSteps}</div>
            <div class="summary-label">Hohe Sicherheit</div>
          </div>
          <div class="summary-stat">
            <div class="summary-value ${audit.lowConfidenceSteps > 0 ? 'warning' : 'success'}">
              ${audit.lowConfidenceSteps}
            </div>
            <div class="summary-label">Unsichere Schritte</div>
          </div>
          <div class="summary-stat">
            <div class="summary-value">${Utils.formatDuration(audit.totalTime)}</div>
            <div class="summary-label">Verarbeitungszeit</div>
          </div>
        </div>
        
        ${audit.summary.keyInsights && audit.summary.keyInsights.length > 0 ? `
          <div class="insights-section">
            <div class="insights-header success">✓ Erkenntnisse:</div>
            <ul class="insights-list">
              ${audit.summary.keyInsights.map(insight => `<li>${Utils.escapeHtml(insight)}</li>`).join('')}
            </ul>
          </div>
        ` : ''}
        
        ${audit.summary.potentialIssues && audit.summary.potentialIssues.length > 0 ? `
          <div class="insights-section">
            <div class="insights-header warning">⚠ Hinweise:</div>
            <ul class="insights-list">
              ${audit.summary.potentialIssues.map(issue => `<li>${Utils.escapeHtml(issue)}</li>`).join('')}
            </ul>
          </div>
        ` : ''}
      </div>
    `;
  }

  renderProcessFlow(audit) {
    if (!audit.phases || audit.phases.length === 0) {
      return '<div class="audit-process-flow"><p>Keine Phasen verfügbar</p></div>';
    }

    return `
      <div class="audit-process-flow">
        ${audit.phases.map((phase, index) => `
          <div class="phase-group ${index === audit.phases.length - 1 ? 'last-phase' : ''}">
            <div class="phase-header" style="display: flex; align-items: center; gap: 1rem; margin-bottom: 0.75rem;">
              <div class="phase-info" style="display: flex; align-items: center; gap: 0.75rem;">
                <span class="phase-icon" style="font-size: 1rem;">${phase.icon || '📋'}</span>
                <span class="phase-name" style="font-size: 0.875rem; font-weight: 500; color: var(--color-text);">${phase.displayName || phase.name}</span>
              </div>
              <div class="phase-divider" style="flex: 1; height: 1px; background-color: var(--color-border);"></div>
              <div class="phase-stats" style="display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0;">
                <div class="confidence-bar" style="width: 48px; height: 8px; background-color: var(--color-bg-tertiary); border-radius: 4px; overflow: hidden;">
                  <div class="confidence-fill" 
                      style="height: 100%; width: ${phase.avgConfidence || 0}%; background-color: ${Utils.getConfidenceColor(phase.avgConfidence || 0)}; border-radius: 4px; transition: var(--transition-fast);">
                  </div>
                </div>
                <span class="confidence-text" style="font-size: 0.75rem; color: var(--color-text-secondary); min-width: 28px;">${phase.avgConfidence || 0}%</span>
              </div>
            </div>
            
            <div class="phase-entries" style="margin-left: 1.5rem; display: grid; gap: 0.5rem;">
              ${(phase.entries || []).map(entry => this.renderAuditEntry(entry)).join('')}
            </div>
          </div>
        `).join('')}
      </div>
    `;
  }

  renderAuditEntry(entry) {
    return `
      <div class="audit-entry" style="background-color: var(--color-bg); border: 1px solid var(--color-border); border-radius: 0.375rem; padding: 0.75rem; transition: var(--transition-fast);" 
           onmouseover="this.style.backgroundColor='var(--color-bg-secondary)'" 
           onmouseout="this.style.backgroundColor='var(--color-bg)'">
        <div class="entry-main" style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.5rem;">
          <span class="entry-action" style="font-size: 0.875rem; font-weight: 500; color: var(--color-text);">
            ${Utils.getActionDisplayName(entry.action)}
          </span>
          <div class="entry-meta" style="display: flex; align-items: center; gap: 0.5rem; font-size: 0.75rem; color: var(--color-text-secondary);">
            <div class="confidence-indicator" 
                style="width: 8px; height: 8px; border-radius: 50%; background-color: ${Utils.getConfidenceColor(entry.confidence || 0)};">
            </div>
            <span class="confidence-value" style="min-width: 28px; text-align: right;">${entry.confidence || 0}%</span>
            <span class="processing-time" style="min-width: 40px; text-align: right;">${entry.processingTimeMs || 0}ms</span>
          </div>
        </div>
        
        ${(entry.inputSummary && entry.inputSummary !== 'null') || (entry.outputSummary && entry.outputSummary !== 'null') ? `
          <div class="entry-details" style="font-size: 0.75rem; color: var(--color-text-secondary); padding-top: 0.5rem; border-top: 1px solid var(--color-border);">
            ${entry.inputSummary && entry.inputSummary !== 'null' ? `
              <div class="detail-item" style="margin-bottom: 0.25rem; word-break: break-word;">
                <strong>Input:</strong> ${Utils.escapeHtml(entry.inputSummary)}
              </div>
            ` : ''}
            ${entry.outputSummary && entry.outputSummary !== 'null' ? `
              <div class="detail-item" style="margin-bottom: 0.25rem; word-break: break-word;">
                <strong>Output:</strong> ${Utils.escapeHtml(entry.outputSummary)}
              </div>
            ` : ''}
          </div>
        ` : ''}
      </div>
    `;
  }

  renderTechnicalDetails(audit) {
    const technicalId = `${this.componentId}-technical`;
    
    return `
      <div class="technical-toggle">
        <button class="technical-toggle-btn" data-target="${technicalId}">
          🔧 Technische Details anzeigen
        </button>
        <div id="${technicalId}" class="technical-details collapsed">
          ${(audit.phases || []).map(phase => 
            (phase.entries || []).map(entry => `
              <div class="technical-entry">
                <div class="technical-header">
                  <span class="technical-phase">${entry.phase}/${entry.action}</span>
                  <span class="technical-time">
                    ${new Date(entry.timestamp).toLocaleTimeString('de-DE', { 
                      hour: '2-digit', 
                      minute: '2-digit', 
                      second: '2-digit' 
                    })} • ${entry.processingTimeMs || 0}ms
                  </span>
                </div>
                <div class="technical-content">
                  <div class="technical-row">
                    <strong>Confidence:</strong> ${entry.confidence || 0}%
                  </div>
                  ${entry.metadata && Object.keys(entry.metadata).length > 0 ? `
                    <div class="technical-row">
                      <strong>Metadata:</strong> ${Utils.escapeHtml(JSON.stringify(entry.metadata))}
                    </div>
                  ` : ''}
                </div>
              </div>
            `).join('')
          ).join('')}
        </div>
      </div>
    `;
  }

  renderEmpty(container) {
    container.innerHTML = `
      <div class="audit-trail-container">
        <div class="audit-trail-header">
          <div class="audit-trail-title">
            <div class="audit-icon">
              <div class="audit-icon-gradient">ⓘ</div>
              <h4>Kein Audit-Trail verfügbar</h4>
            </div>
          </div>
        </div>
      </div>
    `;
  }

  renderError(container, error) {
    container.innerHTML = `
      <div class="audit-trail-container">
        <div class="audit-trail-header">
          <div class="audit-trail-title">
            <div class="audit-icon">
              <div class="audit-icon-gradient" style="background: var(--color-error);">✗</div>
              <h4>Audit-Trail Fehler</h4>
            </div>
          </div>
        </div>
        <div class="audit-summary">
          <p style="color: var(--color-error);">
            Fehler beim Laden der Audit-Informationen: ${Utils.escapeHtml(error.message)}
          </p>
        </div>
      </div>
    `;
  }

  attachEventHandlers() {
    if (this.options.collapsible) {
      const header = document.querySelector(`[data-target="${this.componentId}-details"]`);
      const details = document.getElementById(`${this.componentId}-details`);
      const toggleIcon = header?.querySelector('.toggle-icon svg');
      
      if (header && details && toggleIcon) {
        header.addEventListener('click', () => {
          const isCollapsed = details.classList.contains('collapsed');
          details.classList.toggle('collapsed');
          toggleIcon.style.transform = isCollapsed ? 'rotate(180deg)' : 'rotate(0deg)';
        });
      }
    }
    
    const technicalBtn = document.querySelector(`[data-target="${this.componentId}-technical"]`);
    const technicalDetails = document.getElementById(`${this.componentId}-technical`);
    
    if (technicalBtn && technicalDetails) {
      technicalBtn.addEventListener('click', () => {
        const isCollapsed = technicalDetails.classList.contains('collapsed');
        technicalDetails.classList.toggle('collapsed');
        technicalBtn.textContent = isCollapsed ? 
          '🔧 Technische Details ausblenden' : 
          '🔧 Technische Details anzeigen';
      });
    }
  }
}

class AIQueryInterface {
  constructor() {
    this.currentMode = 'workflow';
    this.currentRecommendation = null;
    this.enhancementTimeout = null;
    this.enhancementAbortController = null;
    this.statusInterval = null;
    this.microTaskInterval = null;
    this.currentMicroTaskStep = 0;
    
    this.initializeElements();
    this.setupEventListeners();
    this.updateModeUI();
  }

  initializeElements() {
    this.elements = {
      input: document.getElementById('ai-query-input'),
      submitBtn: document.getElementById('ai-submit-btn'),
      submitBtnText: document.getElementById('submit-btn-text'),
      description: document.getElementById('ai-description'),
      loading: document.getElementById('ai-loading'),
      loadingText: document.getElementById('loading-text'),
      error: document.getElementById('ai-error'),
      errorMessage: document.getElementById('ai-error-message'),
      results: document.getElementById('ai-results'),
      charCounter: document.getElementById('ai-char-counter'),
      
      toggleSwitch: document.querySelector('.toggle-switch'),
      toggleSlider: document.querySelector('.toggle-slider'),
      workflowLabel: document.getElementById('workflow-label'),
      toolLabel: document.getElementById('tool-label'),
      
      promptingContainer: document.getElementById('smart-prompting-container'),
      promptingStatus: document.getElementById('prompting-status'),
      promptingSpinner: document.getElementById('prompting-spinner'),
      suggestedQuestions: document.getElementById('suggested-questions'),
      questionsList: document.getElementById('questions-list'),
      dismissSuggestions: document.getElementById('dismiss-suggestions'),
      smartHint: document.getElementById('smart-prompting-hint'),
      
      queueStatus: document.getElementById('queue-status'),
      queueLength: document.getElementById('queue-length'),
      estimatedTime: document.getElementById('estimated-time'),
      positionBadge: document.getElementById('queue-position-badge'),
      progressBar: document.getElementById('queue-progress'),
      taskIdDisplay: document.getElementById('current-task-id'),
      microTaskProgress: document.getElementById('micro-task-progress'),
      microTaskCounter: document.getElementById('micro-task-counter')
    };

    return Object.values(this.elements).some(el => el !== null);
  }

  setupEventListeners() {
    this.elements.toggleSwitch?.addEventListener('click', () => this.toggleMode());
    this.elements.workflowLabel?.addEventListener('click', () => this.setMode('workflow'));
    this.elements.toolLabel?.addEventListener('click', () => this.setMode('tool'));

    this.elements.input?.addEventListener('input', () => this.handleInputChange());
    this.elements.input?.addEventListener('keydown', (e) => {
      if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
        e.preventDefault();
        this.handleSubmit();
      }
    });

    this.elements.submitBtn?.addEventListener('click', () => this.handleSubmit());
    this.elements.dismissSuggestions?.addEventListener('click', () => this.hideSmartPrompting());
  }

  getModeConfig() {
    return {
      workflow: {
        placeholder: "Beschreiben Sie Ihr forensisches Szenario... z.B. 'Verdacht auf Ransomware-Angriff auf Windows-Domänencontroller'",
        description: "Beschreiben Sie Ihre Untersuchungssituation und erhalten Empfehlungen für alle Phasen der Untersuchung.",
        submitText: "Empfehlungen generieren",
        loadingText: "Analysiere Szenario und generiere Empfehlungen..."
      },
      tool: {
        placeholder: "Beschreiben Sie Ihr Problem... z.B. 'Analyse von Android-Backups mit WhatsApp-Nachrichten'",
        description: "Beschreiben Sie Ihre Untersuchungssituation und erhalten Empfehlungen für eine spezifische Aufgabenstellung.",
        submitText: "Empfehlungen finden",
        loadingText: "Analysiere Anforderungen und suche passende Methode..."
      }
    };
  }

  toggleMode() {
    this.setMode(this.currentMode === 'workflow' ? 'tool' : 'workflow');
  }

  setMode(mode) {
    if (this.currentMode === mode) return;
    
    this.currentMode = mode;
    this.hideSmartPrompting();
    this.hideResults();
    this.updateModeUI();
  }

  updateModeUI() {
    const config = this.getModeConfig()[this.currentMode];
    
    if (this.elements.input) this.elements.input.placeholder = config.placeholder;
    if (this.elements.description) this.elements.description.textContent = config.description;
    if (this.elements.submitBtnText) this.elements.submitBtnText.textContent = config.submitText;
    if (this.elements.loadingText) this.elements.loadingText.textContent = config.loadingText;
    
    const isWorkflow = this.currentMode === 'workflow';
    const primaryColor = isWorkflow ? 'var(--color-primary)' : 'var(--color-accent)';
    const secondaryColor = 'var(--color-text-secondary)';
    
    if (this.elements.toggleSlider) {
      this.elements.toggleSlider.style.transform = isWorkflow ? 'translateX(0)' : 'translateX(26px)';
    }
    if (this.elements.toggleSwitch) {
      this.elements.toggleSwitch.style.backgroundColor = primaryColor;
    }
    
    if (this.elements.workflowLabel) {
      this.elements.workflowLabel.style.color = isWorkflow ? primaryColor : secondaryColor;
      this.elements.workflowLabel.classList.toggle('active', isWorkflow);
    }
    if (this.elements.toolLabel) {
      this.elements.toolLabel.style.color = isWorkflow ? secondaryColor : primaryColor;
      this.elements.toolLabel.classList.toggle('active', !isWorkflow);
    }
  }

  handleInputChange() {
    this.updateCharacterCount();
    
    clearTimeout(this.enhancementTimeout);
    if (this.enhancementAbortController) {
      this.enhancementAbortController.abort();
    }
    
    const inputLength = this.elements.input.value.trim().length;
    
    if (inputLength < 40) {
      this.hideSmartPrompting();
      return;
    }
    
    this.enhancementTimeout = setTimeout(() => {
      if (this.elements.input.value.trim().length >= 50) {
        this.triggerSmartPrompting();
      }
    }, 1000);
  }

  updateCharacterCount() {
    if (!this.elements.input || !this.elements.charCounter) return;
    
    const length = this.elements.input.value.length;
    const maxLength = 2000;
    
    this.elements.charCounter.textContent = `${length}/${maxLength}`;
    this.elements.charCounter.style.color = length > maxLength * 0.9 ? 
      'var(--color-warning)' : 'var(--color-text-secondary)';
  }

  async triggerSmartPrompting() {
    const inputText = this.elements.input.value.trim();
    
    if (inputText.length < 50) {
      this.hideSmartPrompting();
      return;
    }
    
    this.enhancementAbortController = new AbortController();
    
    try {
      this.showPromptingStatus('analyzing');
      
      const response = await fetch('/api/ai/enhance-input', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ input: inputText }),
        signal: this.enhancementAbortController.signal
      });
      
      if (!response.ok) {
        this.showPromptingStatus(response.status === 429 ? 'rate-limited' : 'error');
        return;
      }
      
      const data = await response.json();
      
      if (data.success && data.questions?.length > 0) {
        this.displaySuggestions(data.questions);
      } else {
        this.hideSmartPrompting();
      }
      
    } catch (error) {
      if (error.name !== 'AbortError') {
        console.warn('[AI Interface] Smart prompting failed:', error);
        this.showPromptingStatus('error');
      }
    }
  }

  showPromptingStatus(state) {
    if (!this.elements.promptingContainer || !this.elements.promptingStatus) return;
    
    const statusText = this.elements.promptingStatus.querySelector('.status-text');
    if (!statusText) return;
    
    const statusMap = {
      analyzing: {
        text: 'Analysiere Eingabe...',
        showSpinner: true,
        showSuggestions: false,
        showContainer: true,
        hideHint: true
      },
      suggestions: {
        text: 'Verbesserungsvorschläge verfügbar',
        showSpinner: false,
        showSuggestions: true,
        showContainer: true,
        hideHint: true
      },
      'rate-limited': {
        text: 'Nach Hauptabfrage verfügbar',
        showSpinner: false,
        showSuggestions: false,
        showContainer: true,
        hideHint: true
      },
      error: {
        showContainer: false,
        hideHint: false
      }
    };

    const config = statusMap[state];
    if (!config) return;

    statusText.textContent = config.text || '';
    
    if (config.showContainer) Utils.showElement(this.elements.promptingContainer);
    else Utils.hideElement(this.elements.promptingContainer);
    
    if (config.hideHint) Utils.hideElement(this.elements.smartHint);
    else Utils.showElement(this.elements.smartHint);
    
    if (config.showSpinner) Utils.showElement(this.elements.promptingSpinner);
    else Utils.hideElement(this.elements.promptingSpinner);
    
    if (config.showSuggestions) Utils.showElement(this.elements.suggestedQuestions);
    else Utils.hideElement(this.elements.suggestedQuestions);
  }

  displaySuggestions(suggestions) {
    if (!this.elements.questionsList || !suggestions?.length) return;
    
    this.elements.questionsList.innerHTML = suggestions.map(question => 
      `<div class="suggestion-item">${Utils.escapeHtml(question)}</div>`
    ).join('');
    
    this.showPromptingStatus('suggestions');
  }

  hideSmartPrompting() {
    if (this.elements.promptingContainer) Utils.hideElement(this.elements.promptingContainer);
    if (this.elements.smartHint) Utils.showElement(this.elements.smartHint);
  }

  async handleSubmit() {
    const query = this.elements.input.value.trim();
    
    if (!query) {
      alert('Bitte geben Sie eine Beschreibung ein.');
      return;
    }
    
    if (query.length < 10) {
      alert('Bitte geben Sie eine detailliertere Beschreibung ein (mindestens 10 Zeichen).');
      return;
    }

    const taskId = `ai_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`;
    
    this.hideSmartPrompting();
    this.hideResults();
    this.hideError();
    this.showLoading();
    this.startQueueMonitoring(taskId);
    
    this.elements.submitBtn.disabled = true;
    this.elements.submitBtnText.textContent = this.currentMode === 'workflow' ? 
      'Generiere Empfehlungen...' : 'Suche passende Methode...';

    try {
      const response = await fetch('/api/ai/query', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
          query,
          mode: this.currentMode,
          taskId
        })
      });
      
      const data = await response.json();
      
      if (!response.ok) {
        throw new Error(data.error || `HTTP ${response.status}`);
      }
      
      if (!data.success) {
        throw new Error(data.error || 'Unknown error');
      }

      this.currentRecommendation = data.recommendation;
      await this.displayResults(data.recommendation, query);
      
    } catch (error) {
      console.error('[AI Interface] Request failed:', error);
      this.showError(this.getErrorMessage(error));
    } finally {
      this.stopQueueMonitoring();
      this.hideLoading();
      this.resetSubmitButton();
    }
  }

  getErrorMessage(error) {
    const errorMap = {
      '429': 'Zu viele Anfragen. Bitte warten Sie einen Moment.',
      '401': 'Authentifizierung erforderlich. Bitte melden Sie sich an.',
      '503': 'KI-Service vorübergehend nicht verfügbar.'
    };
    
    for (const [code, message] of Object.entries(errorMap)) {
      if (error.message.includes(code)) return message;
    }
    
    return `Fehler: ${error.message}`;
  }

  resetSubmitButton() {
    if (this.elements.submitBtn) this.elements.submitBtn.disabled = false;
    if (this.elements.submitBtnText) {
      const config = this.getModeConfig()[this.currentMode];
      this.elements.submitBtnText.textContent = config.submitText;
    }
  }

  startQueueMonitoring(taskId) {
    if (this.elements.queueStatus) Utils.showElement(this.elements.queueStatus);
    if (this.elements.taskIdDisplay) {
      this.elements.taskIdDisplay.textContent = taskId.slice(-8);
    }
    
    this.startMicroTaskProgress();
    
    setTimeout(() => {
      this.updateQueueStatus(taskId);
      this.statusInterval = setInterval(() => this.updateQueueStatus(taskId), 1000);
    }, 500);
  }

  async updateQueueStatus(taskId) {
    try {
      const response = await fetch(`/api/ai/queue-status?taskId=${taskId}`);
      if (!response.ok) return;
      
      const data = await response.json();
      
      if (this.elements.queueLength) this.elements.queueLength.textContent = data.queueLength || 0;
      if (this.elements.estimatedTime) {
        this.elements.estimatedTime.textContent = data.estimatedWaitTime > 0 ? 
          Utils.formatDuration(data.estimatedWaitTime) : 'Verarbeitung läuft...';
      }
      
      if (this.elements.positionBadge) {
        if (data.currentPosition) {
          this.elements.positionBadge.textContent = data.currentPosition;
          if (this.elements.progressBar && data.queueLength > 0) {
            const progress = Math.max(0, ((data.queueLength - data.currentPosition + 1) / data.queueLength) * 100);
            this.elements.progressBar.style.width = `${progress}%`;
          }
        } else {
          const stateIcons = { processing: '⚡', completed: '✅', failed: '❌' };
          this.elements.positionBadge.textContent = stateIcons[data.taskStatus] || '?';
          if (this.elements.progressBar) {
            this.elements.progressBar.style.width = data.taskStatus === 'processing' ? '100%' : '0%';
          }
        }
      }
      
    } catch (error) {
      console.error('[AI Interface] Queue status update failed:', error);
    }
  }

  stopQueueMonitoring() {
    if (this.statusInterval) {
      clearInterval(this.statusInterval);
      this.statusInterval = null;
    }
    
    if (this.microTaskInterval) {
      clearInterval(this.microTaskInterval);
      this.microTaskInterval = null;
    }
    
    if (this.elements.queueStatus) Utils.hideElement(this.elements.queueStatus);
    if (this.elements.microTaskProgress) Utils.hideElement(this.elements.microTaskProgress);
  }

  startMicroTaskProgress() {
    if (!this.elements.microTaskProgress) return;
    
    Utils.showElement(this.elements.microTaskProgress);
    this.currentMicroTaskStep = 0;
    
    const steps = ['scenario', 'approach', 'considerations', 'tools', 'knowledge', 'final'];
    const stepElements = this.elements.microTaskProgress.querySelectorAll('.micro-step');
    
    stepElements.forEach(step => {
      step.classList.remove('active', 'completed', 'failed');
    });
    
    this.microTaskInterval = setInterval(() => {
      if (this.currentMicroTaskStep < steps.length) {
        if (this.currentMicroTaskStep > 0) {
          const prevStep = this.elements.microTaskProgress.querySelector(`[data-step="${steps[this.currentMicroTaskStep - 1]}"]`);
          if (prevStep) {
            prevStep.classList.remove('active');
            prevStep.classList.add('completed');
          }
        }
        
        const currentStep = this.elements.microTaskProgress.querySelector(`[data-step="${steps[this.currentMicroTaskStep]}"]`);
        if (currentStep) {
          currentStep.classList.add('active');
        }
        
        if (this.elements.microTaskCounter) {
          this.elements.microTaskCounter.textContent = `${this.currentMicroTaskStep + 1}/${steps.length}`;
        }
        
        this.currentMicroTaskStep++;
      } else {
        const lastStep = this.elements.microTaskProgress.querySelector(`[data-step="${steps[steps.length - 1]}"]`);
        if (lastStep) {
          lastStep.classList.remove('active');
          lastStep.classList.add('completed');
        }
        
        if (this.elements.microTaskCounter) {
          this.elements.microTaskCounter.textContent = `${steps.length}/${steps.length}`;
        }
        
        clearInterval(this.microTaskInterval);
        this.microTaskInterval = null;
      }
    }, 2000);
  }

  async displayResults(recommendation, originalQuery) {
    console.log('[AI Interface] Displaying results for mode:', this.currentMode);
    
    if (this.currentMode === 'workflow') {
      await this.displayWorkflowResults(recommendation, originalQuery);
    } else {
      await this.displayToolResults(recommendation, originalQuery);
    }
    
    this.showResults();
    
    setTimeout(() => {
      try {
        this.renderAuditTrail(recommendation.auditTrail);
      } catch (error) {
        console.error('[AI Interface] Failed to render audit trail:', error);
      }
    }, 100);
  }

  async displayWorkflowResults(recommendation, originalQuery) {
    const toolsByPhase = {};
    const phaseOrder = phases.map(phase => phase.id);
    const phaseNames = phases.reduce((acc, phase) => {
      acc[phase.id] = phase.name;
      return acc;
    }, {});

    phaseOrder.forEach(phase => {
      toolsByPhase[phase] = [];
    });

    recommendation.recommended_tools?.forEach(recTool => {
      if (toolsByPhase[recTool.phase]) {
        const fullTool = tools.find(t => t.name === recTool.name);
        if (fullTool) {
          toolsByPhase[recTool.phase].push({
            ...fullTool,
            recommendation: recTool,
            confidence: recTool.confidence, 
            justification: recTool.justification,
            priority: recTool.priority
          });
        }
      }
    });

    const html = `
      <div class="workflow-container">
        ${this.renderHeader('Untersuchungsansatz', originalQuery)}
        ${this.renderContextualAnalysis(recommendation, 'workflow')}
        ${this.renderBackgroundKnowledge(recommendation.background_knowledge)}
        ${this.renderWorkflowPhases(toolsByPhase, phaseOrder, phaseNames)}
        ${this.renderWorkflowSuggestion(recommendation.workflow_suggestion)}
      </div>
      <div id="audit-trail-container"></div>
    `;
    
    this.elements.results.innerHTML = html;
  }

  async displayToolResults(recommendation, originalQuery) {
    const html = `
      <div class="workflow-container">
        ${this.renderHeader('Handlungsempfehlung', originalQuery)}
        ${this.renderContextualAnalysis(recommendation, 'tool')}
        ${this.renderBackgroundKnowledge(recommendation.background_knowledge)}
        ${this.renderToolRecommendations(recommendation.recommended_tools)}
        ${this.renderAdditionalConsiderations(recommendation.additional_considerations)}
      </div>
      <div id="audit-trail-container"></div>
    `;
    
    this.elements.results.innerHTML = html;
  }

  renderAuditTrail(rawAuditTrail) {
    const container = document.getElementById('audit-trail-container');
    if (!container) {
      console.error('[AI Interface] Audit container not found');
      return;
    }

    container.innerHTML = '';

    if (!rawAuditTrail || !Array.isArray(rawAuditTrail) || rawAuditTrail.length === 0) {
      console.log('[AI Interface] No audit trail data to render');
      return;
    }

    try {
      const renderer = new AuditTrailRenderer('audit-trail-container', {
        title: 'KI-Entscheidungspfad',
        collapsible: true,
        defaultExpanded: false
      });
      
      renderer.render(rawAuditTrail);
    } catch (error) {
      console.error('[AI Interface] Failed to render audit trail:', error);
    }
  }

  renderHeader(title, query) {
    return `
      <div style="text-align: center; margin-bottom: 2rem; padding: 1.5rem; background: linear-gradient(135deg, var(--color-primary) 0%, #525252 100%); color: white; border-radius: 0.75rem;">
        <h3 style="margin: 0 0 0.75rem 0; font-size: 1.5rem;">${title}</h3>
        <p style="margin: 0; opacity: 0.9; line-height: 1.5;">
          Basierend auf Ihrer Anfrage: "<em>${Utils.truncateText(query, 100)}</em>"
        </p>
      </div>
    `;
  }

  renderContextualAnalysis(recommendation, mode) {
    let html = '';
    
    const analysisField = mode === 'workflow' ? recommendation.scenario_analysis : recommendation.problem_analysis;
    if (analysisField) {
      html += `
        <div class="card contextual-analysis-card scenario">
          <h4 style="margin: 0 0 1rem 0; color: var(--color-primary);">
            ${mode === 'workflow' ? 'Szenario-Analyse' : 'Problem-Analyse'}
          </h4>
          <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(analysisField)}</div>
        </div>
      `;
    }

    if (recommendation.investigation_approach) {
      html += `
        <div class="card contextual-analysis-card approach">
          <h4 style="margin: 0 0 1rem 0; color: var(--color-accent);">
            ${mode === 'workflow' ? 'Untersuchungsansatz' : 'Lösungsansatz'}
          </h4>
          <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(recommendation.investigation_approach)}</div>
        </div>
      `;
    }

    if (recommendation.critical_considerations) {
      html += `
        <div class="card contextual-analysis-card critical">
          <h4 style="margin: 0 0 1rem 0; color: var(--color-warning);">
            ${mode === 'workflow' ? 'Kritische Überlegungen' : 'Wichtige Voraussetzungen'}
          </h4>
          <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(recommendation.critical_considerations)}</div>
        </div>
      `;
    }

    return html;
  }

  renderBackgroundKnowledge(backgroundKnowledge) {
    if (!backgroundKnowledge?.length) return '';
    
    const conceptLinks = backgroundKnowledge.map(concept => `
      <div style="background-color: var(--color-concept-bg); border: 1px solid var(--color-concept); border-radius: 0.5rem; padding: 1rem; margin-bottom: 0.75rem;">
        <div style="display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.5rem;">
          <button onclick="window.showToolDetails('${concept.concept_name}', 'secondary')" 
                  style="background: none; border: none; color: var(--color-concept); font-weight: 600; cursor: pointer; text-decoration: underline;">
            📚 ${concept.concept_name}
          </button>
          <span class="badge" style="background-color: var(--color-concept); color: white; font-size: 0.625rem;">Hintergrundwissen</span>
        </div>
        <div style="margin: 0; font-size: 0.8125rem; line-height: 1.5; color: var(--color-text-secondary); white-space: pre-wrap; word-wrap: break-word;">
          ${Utils.sanitizeText(concept.relevance)}
        </div>
      </div>
    `).join('');
    
    return `
      <div class="card" style="margin-bottom: 2rem; border-left: 4px solid var(--color-concept);">
        <h4 style="margin: 0 0 1rem 0; color: var(--color-concept);">Empfohlenes Hintergrundwissen</h4>
        ${conceptLinks}
      </div>
    `;
  }

  renderWorkflowPhases(toolsByPhase, phaseOrder, phaseNames) {
    const phaseColors = [
      'var(--color-primary)', 
      'var(--color-accent)', 
      'var(--color-warning)',
      'var(--color-method)',
      'var(--color-hosted)',
      'var(--color-concept)'
    ];

    return phaseOrder.map((phase, index) => {
      const phaseTools = toolsByPhase[phase];
      if (phaseTools.length === 0) return '';

      const phaseColor = phaseColors[index % phaseColors.length];

      return `
        <div class="workflow-phase" style="margin-bottom: 2rem;">
          <div class="phase-header" style="display: flex; align-items: flex-start; gap: 1rem; padding: 1.5rem; background-color: var(--color-bg); border: 2px solid var(--color-border); border-radius: 0.75rem; transition: var(--transition-medium);" 
               onmouseover="this.style.borderColor='${phaseColor}'; this.style.boxShadow='var(--shadow-md)';"
               onmouseout="this.style.borderColor='var(--color-border)'; this.style.boxShadow='';">
            
            <div class="phase-number" style="width: 2rem; height: 2rem; background: ${phaseColor}; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 600; flex-shrink: 0; box-shadow: var(--shadow-sm);">
              ${index + 1}
            </div>
            
            <div class="phase-info" style="display: flex; flex-direction: column; gap: 0.75rem; flex: 1; min-width: 0;">
              <h3 class="phase-title" style="font-weight: 600; margin: 0; color: var(--color-text); font-size: 1.25rem;">
                ${phaseNames[phase]}
              </h3>
              
              <div class="phase-tools" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem;">
                ${phaseTools.map(tool => this.renderWorkflowTool(tool)).join('')}
              </div>
            </div>
          </div>
          
          ${index < phaseOrder.length - 1 ? `
            <div class="workflow-arrow" style="display: flex; justify-content: center; margin: 1rem 0; color: ${phaseColor};">
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="12" y1="5" x2="12" y2="19"/>
                <polyline points="19 12 12 19 5 12"/>
              </svg>
            </div>
          ` : ''}
        </div>
      `;
    }).join('');
  }

  renderWorkflowTool(tool) {
    const priorityColors = {
      high: 'var(--color-error)',
      medium: 'var(--color-warning)', 
      low: 'var(--color-accent)'
    };

    const priority = tool.recommendation ? tool.recommendation.priority : tool.priority;
    const confidenceTooltip = tool.confidence ? this.renderConfidenceTooltip(tool.confidence) : '';
    
    const cardClass = this.getToolClass(tool, 'recommendation');
    
    return `
      <div class="tool-recommendation ${cardClass}" onclick="window.showToolDetails('${tool.name}')" 
           style="position: relative; cursor: pointer; transition: var(--transition-fast);"
           onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='var(--shadow-md)';"
           onmouseout="this.style.transform='translateY(0)'; this.style.boxShadow='';">
        
        <div class="tool-rec-header" style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 0.75rem;">
          <h4 class="tool-rec-name" style="font-weight: 600; font-size: 1rem; margin: 0; color: var(--color-text); flex: 1; margin-right: 0.75rem;">
            ${tool.icon ? `<span style="margin-right: 0.5rem; font-size: 1.125rem;">${tool.icon}</span>` : ''}
            ${tool.name}
          </h4>
          <div style="display: flex; align-items: center; gap: 0.375rem; flex-shrink: 0;">
            <span class="tool-rec-priority ${priority}" 
                  style="background-color: ${priorityColors[priority]}; color: white; padding: 0.25rem 0.5rem; border-radius: 1rem; font-size: 0.75rem; font-weight: 500; text-transform: uppercase; position: relative; display: flex; align-items: center; gap: 0.25rem;">
              ${priority}
              ${confidenceTooltip}
            </span>
          </div>
        </div>
        
        <div class="tool-rec-justification" style="background-color: var(--color-bg-tertiary); padding: 0.75rem; border-radius: 0.375rem; border-left: 3px solid var(--color-primary); margin: 0.75rem 0; font-style: italic; white-space: pre-wrap; word-wrap: break-word; font-size: 0.875rem; line-height: 1.5; color: var(--color-text-secondary);">
          "${Utils.sanitizeText(tool.justification || (tool.recommendation && tool.recommendation.justification) || `Empfohlen für ${tool.phase}`)}"
        </div>
        
        <div style="font-size: 0.75rem; color: var(--color-text-secondary); margin-top: auto;">
          <div style="display: flex; flex-wrap: wrap; gap: 0.375rem; margin-bottom: 0.5rem;">
            ${this.renderToolBadges(tool)}
          </div>
          <div style="display: flex; justify-content: space-between; align-items: center;">
            <span>${tool.type === 'method' ? 'Methode' : tool.platforms.slice(0, 2).join(', ')}</span>
            <span>${tool.skillLevel}</span>
          </div>
        </div>
      </div>
    `;
  }

  renderToolRecommendations(recommendedTools) {
    if (!recommendedTools?.length) return '';
    
    return `
      <div class="tool-recommendations-grid" style="display: grid; gap: 1.5rem;">
        ${recommendedTools.map((toolRec, index) => {
          const fullTool = tools.find(t => t.name === toolRec.name);
          if (!fullTool) return '';
          
          return this.renderDetailedTool(fullTool, toolRec, index + 1);
        }).join('')}
      </div>
    `;
  }

  renderDetailedTool(tool, recommendation, rank) {
    const rankColors = { 1: 'var(--color-accent)', 2: 'var(--color-primary)', 3: 'var(--color-warning)' };
    const suitabilityColors = { high: 'var(--color-accent)', medium: 'var(--color-warning)', low: 'var(--color-text-secondary)' };
    
    const confidenceTooltip = recommendation.confidence ? this.renderConfidenceTooltip(recommendation.confidence) : '';
    
    return `
      <div class="card ${this.getToolClass(tool, 'card')}" style="cursor: pointer; position: relative;" onclick="window.showToolDetails('${tool.name}')">
        <div style="position: absolute; top: -8px; right: -8px; width: 32px; height: 32px; background-color: ${rankColors[rank]}; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 1.125rem;">
          ${rank}
        </div>

        <div style="margin-bottom: 1rem;">
          <h3 style="margin: 0 0 0.5rem 0;">${tool.name}</h3>
          <div style="display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem;">
            <span class="badge" style="background-color: ${suitabilityColors[recommendation.suitability_score]}; color: white; position: relative; display: flex; align-items: center; gap: 0.25rem;">
              ${this.getSuitabilityText(recommendation.suitability_score)}
              ${confidenceTooltip}
            </span>
            ${this.renderToolBadges(tool)}
          </div>
        </div>

        <div style="margin-bottom: 1.5rem;">
          <h4 style="margin: 0.8rem 0 0.75rem 0; color: var(--color-accent);">Warum diese Methode?</h4>
          <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(recommendation.detailed_explanation)}</div>
          
          ${recommendation.implementation_approach ? `
            <h4 style="margin: 0.8rem 0 0.75rem 0; color: var(--color-primary);">Anwendungsansatz</h4>
            <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(recommendation.implementation_approach)}</div>
          ` : ''}
        </div>

        ${this.renderProsAndCons(recommendation.pros, recommendation.cons)}
        ${this.renderToolMetadata(tool)}
        ${recommendation.alternatives ? this.renderAlternatives(recommendation.alternatives) : ''}
      </div>
    `;
  }

  renderConfidenceTooltip(confidence) {
    if (!confidence || typeof confidence.overall !== 'number') return '';
    
    const confidenceColor = Utils.getConfidenceColor(confidence.overall);
    const tooltipId = `tooltip-${Math.random().toString(36).substr(2, 9)}`;
    
    return `
      <span class="confidence-tooltip-trigger" 
            style="display: inline-flex; align-items: center; gap: 0.125rem; cursor: help; margin-left: 0.25rem; position: relative;"
            onmouseenter="this.querySelector('.confidence-tooltip').style.display = 'block'"
            onmouseleave="this.querySelector('.confidence-tooltip').style.display = 'none'"
            onclick="event.stopPropagation();">
        <div style="width: 6px; height: 6px; border-radius: 50%; background-color: ${confidenceColor}; flex-shrink: 0;"></div>
        <span style="font-size: 0.625rem; color: white; font-weight: 600; text-shadow: 0 1px 2px rgba(0,0,0,0.5);">${confidence.overall}%</span>
        
        <div class="confidence-tooltip" 
             style="display: none; position: absolute; top: 100%; right: 0; z-index: 1001; background: var(--color-bg); border: 1px solid var(--color-border); border-radius: 0.5rem; padding: 1rem; min-width: 320px; max-width: 400px; box-shadow: var(--shadow-lg); font-size: 0.75rem; color: var(--color-text); margin-top: 0.5rem;">
          <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem;">
            <strong style="font-size: 0.875rem; color: var(--color-primary);">KI-Vertrauenswertung</strong>
            <span style="background-color: ${confidenceColor}; color: white; font-weight: 600; padding: 0.125rem 0.375rem; border-radius: 0.25rem; font-size: 0.625rem;">${confidence.overall}%</span>
          </div>
          
          <div style="display: grid; grid-template-columns: 1fr; gap: 0.625rem; margin-bottom: 0.75rem;">
            <div style="background: var(--color-bg-secondary); padding: 0.5rem; border-radius: 0.375rem; border-left: 3px solid var(--color-accent);">
              <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.25rem;">
                <span style="font-weight: 600; font-size: 0.6875rem; color: var(--color-text);">🔍 Semantische Relevanz</span>
                <strong style="color: var(--color-accent);">${confidence.semanticRelevance}%</strong>
              </div>
              <div style="font-size: 0.625rem; color: var(--color-text-secondary); line-height: 1.3;">
                Wie gut die Tool-Beschreibung semantisch zu Ihrer Anfrage passt
              </div>
            </div>
            
            <div style="background: var(--color-bg-secondary); padding: 0.5rem; border-radius: 0.375rem; border-left: 3px solid var(--color-primary);">
              <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.25rem;">
                <span style="font-weight: 600; font-size: 0.6875rem; color: var(--color-text);">🎯 Aufgaben-Eignung</span>
                <strong style="color: var(--color-primary);">${confidence.taskSuitability}%</strong>
              </div>
              <div style="font-size: 0.625rem; color: var(--color-text-secondary); line-height: 1.3;">
                KI-bewertete Eignung für Ihre spezifische Aufgabenstellung
              </div>
            </div>
          </div>
          
          ${confidence.strengthIndicators && confidence.strengthIndicators.length > 0 ? `
            <div style="margin-bottom: 0.75rem; padding: 0.5rem; background: var(--color-oss-bg); border-radius: 0.375rem; border-left: 3px solid var(--color-accent);">
              <strong style="color: var(--color-accent); font-size: 0.6875rem; display: flex; align-items: center; gap: 0.25rem; margin-bottom: 0.375rem;">
                <span>✓</span> Stärken dieser Empfehlung:
              </strong>
              <ul style="margin: 0; padding-left: 1rem; font-size: 0.625rem; line-height: 1.4; color: var(--color-text);">
                ${confidence.strengthIndicators.slice(0, 3).map(s => `<li style="margin-bottom: 0.25rem;">${Utils.sanitizeText(s)}</li>`).join('')}
              </ul>
            </div>
          ` : ''}
          
          ${confidence.uncertaintyFactors && confidence.uncertaintyFactors.length > 0 ? `
            <div style="padding: 0.5rem; background: var(--color-hosted-bg); border-radius: 0.375rem; border-left: 3px solid var(--color-warning);">
              <strong style="color: var(--color-warning); font-size: 0.6875rem; display: flex; align-items: center; gap: 0.25rem; margin-bottom: 0.375rem;">
                <span>⚠</span> Mögliche Einschränkungen:
              </strong>
              <ul style="margin: 0; padding-left: 1rem; font-size: 0.625rem; line-height: 1.4; color: var(--color-text);">
                ${confidence.uncertaintyFactors.slice(0, 3).map(f => `<li style="margin-bottom: 0.25rem;">${Utils.sanitizeText(f)}</li>`).join('')}
              </ul>
            </div>
          ` : ''}
          
          <div style="margin-top: 0.75rem; padding-top: 0.75rem; border-top: 1px solid var(--color-border); font-size: 0.625rem; color: var(--color-text-secondary); text-align: center;">
            Forensisch fundierte KI-Analyse
          </div>
        </div>
      </span>
    `;
  }

  renderWorkflowSuggestion(suggestion) {
    if (!suggestion) return '';
    
    return `
      <div class="card" style="margin-top: 2rem; border-left: 4px solid var(--color-accent);">
        <h4 style="margin: 0 0 1rem 0; color: var(--color-accent);">Workflow-Empfehlung</h4>
        <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(suggestion)}</div>
      </div>
    `;
  }

  renderAdditionalConsiderations(considerations) {
    if (!considerations) return '';
    
    return `
      <div class="card" style="margin-top: 2rem; background-color: var(--color-bg-secondary); border-left: 4px solid var(--color-text-secondary);">
        <h4 style="margin: 0 0 1rem 0; color: var(--color-text-secondary);">Zusätzliche Überlegungen</h4>
        <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(considerations)}</div>
      </div>
    `;
  }

  renderProsAndCons(pros, cons) {
    if (!pros?.length && !cons?.length) return '';
    
    return `
      <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-bottom: 1.5rem;">
        ${pros?.length ? `
          <div style="background-color: var(--color-oss-bg); padding: 1rem; border-radius: 0.5rem; border-left: 3px solid var(--color-accent);">
            <h5 style="margin: 0 0 0.5rem 0; color: var(--color-accent); font-size: 0.875rem;">✓ Vorteile</h5>
            <ul style="margin: 0; padding-left: 1rem;">
              ${pros.map(pro => `<li style="margin-bottom: 0.25rem;">${Utils.sanitizeText(pro)}</li>`).join('')}
            </ul>
          </div>
        ` : ''}
        ${cons?.length ? `
          <div style="background-color: var(--color-hosted-bg); padding: 1rem; border-radius: 0.5rem; border-left: 3px solid var(--color-warning);">
            <h5 style="margin: 0 0 0.5rem 0; color: var(--color-warning); font-size: 0.875rem;">✗ Nachteile</h5>
            <ul style="margin: 0; padding-left: 1rem;">
              ${cons.map(con => `<li style="margin-bottom: 0.25rem;">${Utils.sanitizeText(con)}</li>`).join('')}
            </ul>
          </div>
        ` : ''}
      </div>
    `;
  }

  renderToolMetadata(tool) {
    const isMethod = tool.type === 'method';
    
    return `
      <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0.75rem; font-size: 0.8125rem; color: var(--color-text-secondary); margin-bottom: 1rem; padding: 0.75rem; background-color: var(--color-bg-secondary); border-radius: 0.375rem;">
        ${!isMethod ? `<div><strong>Plattformen:</strong> ${tool.platforms.join(', ')}</div>` : ''}
        <div><strong>Skill Level:</strong> ${tool.skillLevel}</div>
        ${!isMethod ? `<div><strong>Lizenz:</strong> ${tool.license}</div>` : ''}
        <div><strong>Typ:</strong> ${isMethod ? 'Methode' : tool.accessType}</div>
      </div>
    `;
  }

  renderAlternatives(alternatives) {
    return `
      <div style="background-color: var(--color-bg-secondary); padding: 1rem; border-radius: 0.5rem; margin-bottom: 1rem;">
        <h5 style="margin: 0 0 0.5rem 0; color: var(--color-text-secondary); font-size: 0.875rem;">Alternative Ansätze</h5>
        <div style="margin: 0; line-height: 1.6; white-space: pre-wrap; word-wrap: break-word;">${Utils.sanitizeText(alternatives)}</div>
      </div>
    `;
  }

  renderToolBadges(tool) {
    const isMethod = tool.type === 'method';
    const hasValidProjectUrl = isToolHosted(tool);
    
    let badges = '';
    if (isMethod) {
      badges += '<span class="badge" style="background-color: var(--color-method); color: white;">Methode</span>';
    } else if (hasValidProjectUrl) {
      badges += '<span class="badge badge-primary">CC24-Server</span>';
    } else if (tool.license !== 'Proprietary') {
      badges += '<span class="badge badge-success">Open Source</span>';
    }
    
    if (tool.knowledgebase === true) {
      badges += '<span class="badge badge-error">📖</span>';
    }
    
    return badges;
  }

  getToolClass(tool, context = 'card') {
    const isMethod = tool.type === 'method';
    const hasValidProjectUrl = isToolHosted(tool);
    
    if (context === 'recommendation') {
      if (isMethod) return 'method';
      if (hasValidProjectUrl) return 'hosted';
      if (tool.license !== 'Proprietary') return 'oss';
      return '';
    } else {
      if (isMethod) return 'card-method';
      if (hasValidProjectUrl) return 'card-hosted';
      if (tool.license !== 'Proprietary') return 'card-oss';
      return '';
    }
  }

  getSuitabilityText(score) {
    const texts = {
      high: 'GUT GEEIGNET',
      medium: 'GEEIGNET', 
      low: 'VIELLEICHT GEEIGNET'
    };
    return texts[score] || 'GEEIGNET';
  }

  showLoading() {
    Utils.showElement(this.elements.loading);
  }

  hideLoading() {
    Utils.hideElement(this.elements.loading);
  }

  showResults() {
    Utils.showElement(this.elements.results);
  }

  hideResults() {
    Utils.hideElement(this.elements.results);
    Utils.hideElement(this.elements.error);
  }

  showError(message) {
    if (this.elements.errorMessage) this.elements.errorMessage.textContent = message;
    Utils.showElement(this.elements.error);
  }

  hideError() {
    Utils.hideElement(this.elements.error);
  }

  restoreAIResults() {
    if (this.currentRecommendation && this.elements.results) {
      this.showResults();
      
      if (this.currentRecommendation.auditTrail) {
        setTimeout(() => {
          try {
            this.renderAuditTrail(this.currentRecommendation.auditTrail);
            console.log('[AI Interface] Audit trail restored successfully');
          } catch (error) {
            console.error('[AI Interface] Audit trail restore failed:', error);
          }
        }, 100);
      }
      
      this.hideLoading();
      this.hideError();
    }
  }
}

document.addEventListener('DOMContentLoaded', () => {
  const aiInterface = new AIQueryInterface();
  
  window.restoreAIResults = () => aiInterface.restoreAIResults();
  window.isToolHosted = window.isToolHosted || isToolHosted;
  
  console.log('[AI Interface] Initialized successfully');
});