## Enhanced Mode v2 - Smart Section-Based Editing (routes/ai.js)
### Server-Side Section Replacement Architecture
- Add cleanMarkdownFromTitle() to normalize section titles for matching
- Add extractHeaders() to list all document sections for debugging
- Add replaceSection() to surgically replace only modified sections
- AI now returns ONLY the modified section, not entire document
- Server automatically replaces section in original document
- Automatic header level correction if AI changes ## to ### or vice versa
- Section boundary detection based on header hierarchy
### Enhanced Prompt and Response Format
- Modified prompt to explicitly request ONLY modified section
- New closing format: document (mandatory)
- Added fallback regex for old format with warnings
- Explicit rules: keep exact header level (## stays ##)
- Clear section boundary definition in prompt
- Examples with proper formatting guidelines
### Comprehensive Logging System
- Log all API requests with method, endpoint, payload size
- Log AI responses with length and preview
- Log section matching and replacement operations
- Log header level corrections
- Log section not found errors with available sections list
- Track modified sections across iterations
## AI Button Mutex and Preview Mode Controls (assets/js/app.js)
### AI Button Mutex (Prevent API Overload)
- Add disableAIButtons() to disable all AI buttons during operations
- Add enableAIButtons() to re-enable after completion or error
- Disable all AI buttons at start of any AI operation
- Re-enable in finally blocks to ensure cleanup even on errors
- Re-enable on validation failures (e.g., no text selected for rephrase)
- Re-enable when user clicks Apply/Cancel in rephrase mode
### Preview Mode Button Restrictions
- Disable Preview button during Enhanced Mode operation
- Disable all AI buttons in preview mode (rephrase, inconsistencies, duplications, advice, liberty)
- Disable Save and Load buttons in preview mode
- Re-enable all buttons when returning to edit mode
- Proper cleanup with finally blocks
## Mermaid Auto-Fix System - Complete Removal
### Removed from assets/js/app.js
- Remove mermaidFixAttempts Set from constructor
- Remove setupMessageListener() and postMessage handler
- Remove fixMermaidDiagramBackground() function
- Simplify Mermaid error display to inline messages only
- Remove hash-based tracking mechanism
### Removed from routes/index.js (Present Mode)
- Remove entire auto-fix fetch and retry logic
- Remove status div updates and fix notifications
- Remove postMessage to parent window
- Simplify to display styled error message only
### Current Behavior
- Preview mode: Shows inline error with simple message
- Present mode: Shows styled error box with instructions
- No automatic fix attempts - manual correction only
## Additional Improvements
- Clean markdown formatting (##, **, etc.) from section titles in UI badges
- Proper section title matching ignoring markdown syntax
- Enhanced error handling with detailed logging
- Better user feedback during Enhanced Mode iterations
This release improves Enhanced Mode reliability, prevents API overload through button mutex,
simplifies Mermaid error handling, and adds comprehensive logging for debugging.