        // ===== 저장/출력 =====
        function saveHtml() {
            if (!generatedHTML) {
                alert('먼저 문서를 생성해주세요.');
                return;
            }
            
            const frame = document.getElementById('previewFrame');
            const html = frame.contentDocument ? 
                '<!DOCTYPE html>' + frame.contentDocument.documentElement.outerHTML : 
                generatedHTML;
            
            const blob = new Blob([html], { type: 'text/html' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = `report_${new Date().toISOString().slice(0,10)}.html`;
            a.click();
            URL.revokeObjectURL(url);
        }

        async function exportHwp() {
            if (!generatedHTML) {
                alert('먼저 문서를 생성해주세요.');
                return;
            }
            
            const frame = document.getElementById('previewFrame');
            const html = frame.contentDocument ? 
                '<!DOCTYPE html>' + frame.contentDocument.documentElement.outerHTML : 
                generatedHTML;
            
            setStatus('HWP 변환 중...', true);
            
            try {
                const response = await fetch('/export-hwp', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        html: html,
                        doc_type: currentDocType,
                        style_grouping: true
                    })
                });
                
                if (!response.ok) {
                    const error = await response.json();
                    throw new Error(error.error || 'HWP 변환 실패');
                }
                
                const blob = await response.blob();
                const url = URL.createObjectURL(blob);
                const a = document.createElement('a');
                a.href = url;
                a.download = `report_${new Date().toISOString().slice(0,10)}.hwp`;
                a.click();
                URL.revokeObjectURL(url);
                
                setStatus('HWP 변환 완료', true);
                
            } catch (error) {
                alert('HWP 변환 오류: ' + error.message);
                setStatus('오류 발생', false);
            }
        }

        function printDoc() {
            const frame = document.getElementById('previewFrame');
            if (frame.contentWindow) {
                frame.contentWindow.print();
            }
        }