<?php include "../autoload.php"; ?>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap');
        .gradient-bg {
            background: linear-gradient(135deg, #1e40af 0%, #60a5fa 100%);
        }
        .result-card {
            background: linear-gradient(145deg, #ffffff, #f0f7ff);
            box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
            transition: transform 0.3s ease, opacity 0.3s ease;
        }
        .result-card.hidden {
            transform: translateY(20px);
            opacity: 0;
        }
        .result-card:not(.hidden) {
            transform: translateY(0);
            opacity: 1;
        }
        .input-container {
            background: linear-gradient(145deg, #ffffff, #f9fafb);
            transition: all 0.3s ease;
        }
        .input-container:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
        }
        .range-input::-webkit-slider-thumb {
            -webkit-appearance: none;
            appearance: none;
            width: 20px;
            height: 20px;
            background: #1e40af;
            border-radius: 50%;
            cursor: pointer;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
        }
        .range-input::-moz-range-thumb {
            width: 20px;
            height: 20px;
            background: #1e40af;
            border-radius: 50%;
            cursor: pointer;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
        }
        .btn-primary {
            transition: all 0.3s ease;
        }
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
        }
        .visual-container {
            background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect fill="%23f3f4f6" width="100" height="100"/><circle cx="50" cy="50" r="30" fill="%23dbeafe" opacity="0.5"/></svg>') repeat;
            border-radius: 12px;
            box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.05);
        }
        select {
            appearance: none;
            background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24"><path fill="%234b5563" d="M7 10l5 5 5-5H7z"/></svg>');
            background-repeat: no-repeat;
            background-position: right 0.75rem center;
            background-size: 12px;
        }
    </style>

    <div class="x">
        <div class="x">
            <!-- Input Form -->
            <div class="mb-12">
                <h2 class="text-3xl font-semibold text-gray-800 mb-6">Enter Your Bathtub/Shower & Bathroom Space Details</h2>
                <p class="text-gray-500 mb-8 text-lg">Input dimensions to check if your bathtub or shower unit fits in your bathroom space.</p>
                <form id="bathtubShowerFitForm" class="space-y-8">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                        <div class="space-y-3">
                            <label for="unitType" class="block text-sm font-medium text-gray-700">Unit Type</label>
                            <div class="relative flex items-center">
                                <i data-lucide="shower-head" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <select id="unitType" class="pl-12 pr-10 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" required>
                                    <option value="Bathtub" selected>Bathtub</option>
                                    <option value="Shower">Shower</option>
                                </select>
                            </div>
                        </div>
                        <div class="space-y-3">
                            <label for="unitWidth" class="block text-sm font-medium text-gray-700">Unit Width (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="ruler" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="unitWidth" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="5.0" step="0.1" min="2.5" max="6" value="5.0" required>
                            </div>
                            <input type="range" id="unitWidthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="2.5" max="6" step="0.1" value="5">
                            <p id="unitWidthValue" class="text-sm text-gray-500">5.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="unitDepth" class="block text-sm font-medium text-gray-700">Unit Depth (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="expand" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="unitDepth" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="2.5" step="0.1" min="2" max="4" value="2.5" required>
                            </div>
                            <input type="range" id="unitDepthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="2" max="4" step="0.1" value="2.5">
                            <p id="unitDepthValue" class="text-sm text-gray-500">2.5 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="unitHeight" class="block text-sm font-medium text-gray-700">Unit Height (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="ruler" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="unitHeight" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="2.0" step="0.1" min="1.5" max="7" value="2.0" required>
                            </div>
                            <input type="range" id="unitHeightRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="1.5" max="7" step="0.1" value="2">
                            <p id="unitHeightValue" class="text-sm text-gray-500">2.0 ft</p>
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                        <div class="space-y-3">
                            <label for="spaceWidth" class="block text-sm font-medium text-gray-700">Bathroom Space Width (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="bath" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="spaceWidth" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="7.0" step="0.1" min="4" max="12" value="7.0" required>
                            </div>
                            <input type="range" id="spaceWidthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="4" max="12" step="0.1" value="7">
                            <p id="spaceWidthValue" class="text-sm text-gray-500">7.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="spaceDepth" class="block text-sm font-medium text-gray-700">Bathroom Space Depth (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="expand" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="spaceDepth" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="4.0" step="0.1" min="3" max="10" value="4.0" required>
                            </div>
                            <input type="range" id="spaceDepthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="3" max="10" step="0.1" value="4">
                            <p id="spaceDepthValue" class="text-sm text-gray-500">4.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="clearanceSpace" class="block text-sm font-medium text-gray-700">Clearance Space (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="footprints" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="clearanceSpace" class="pl-12 pr-4 py-3 block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-600 focus:ring focus:ring-blue-600 focus:ring-opacity-50 text-lg" placeholder="1.0" step="0.1" min="0.5" max="3" value="1.0" required>
                            </div>
                            <input type="range" id="clearanceSpaceRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="0.5" max="3" step="0.1" value="1">
                            <p id="clearanceSpaceValue" class="text-sm text-gray-500">1.0 ft</p>
                        </div>
                    </div>
                    <button type="submit" class="btn-primary w-full bg-blue-600 text-white py-4 px-8 rounded-xl hover:bg-blue-700 flex items-center justify-center text-lg">
                        <i data-lucide="search" class="mr-3 h-5 w-5"></i>
                        Check Bathtub/Shower Fit
                    </button>
                </form>
            </div>

            <!-- Result Section -->
            <div id="resultSection" class="result-card rounded-2xl p-10 hidden">
                <h2 class="text-3xl font-semibold text-gray-800 mb-6 flex items-center">
                    <i data-lucide="check-circle-2" class="mr-3 text-green-500 h-7 w-7"></i>
                    Your Bathtub/Shower Fit Assessment
                </h2>
                <div id="resultOutput" class="text-gray-700 space-y-6 text-base">
                    <!-- Results will be dynamically inserted here -->
                </div>
                <div class="mt-10 flex flex-col md:flex-row justify-between items-center gap-6">
                    <div class="flex items-center text-blue-600">
                        <i data-lucide="info" class="mr-2 h-5 w-5"></i>
                        <p class="text-sm font-medium">Based on standard bathroom design and plumbing guidelines.</p>
                    </div>
                    <button id="downloadPdf" class="btn-primary bg-green-600 text-white py-3 px-8 rounded-xl hover:bg-green-700 flex items-center text-base">
                        <i data-lucide="download" class="mr-2 h-5 w-5"></i>
                        Download PDF Report
                    </button>
                </div>
                <!-- Visual Representation -->
                <div class="mt-10 visual-container p-8">
                    <h3 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <i data-lucide="shower-head" class="mr-3 text-blue-500 h-6 w-6"></i>
                        Interactive Bathtub/Shower & Bathroom Space Visualization
                    </h3>
                    <div class="relative w-full h-96 bg-gray-200 rounded-xl overflow-hidden shadow-lg">
                        <canvas id="bathroomCanvas" class="w-full h-full"></canvas>
                    </div>
                    <p class="text-sm text-gray-600 mt-4">Visualization showing the bathtub or shower unit and clearance space scaled to the bathroom area.</p>
                </div>
                <!-- Expert Insights -->
                <div class="mt-10 bg-blue-50 p-8 rounded-xl shadow-sm">
                    <h3 class="text-xl font-medium text-gray-800 mb-4 flex items-center">
                        <i data-lucide="lightbulb" class="mr-3 text-yellow-500 h-6 w-6"></i>
                        Expert Insights for Bathroom Fixture Placement
                    </h3>
                    <ul class="list-none space-y-4 text-gray-700 text-sm">
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Clearance:</strong> Allow at least 1 ft clearance for access and maintenance.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Plumbing:</strong> Ensure proximity to existing drain and water lines.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Ventilation:</strong> Place near a window or vent for moisture control.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Door Swing:</strong> Avoid obstructing door or shower door swing.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Safety:</strong> Ensure non-slip surfaces and grab bars for accessibility.</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Initialize Lucid Icons
        lucide.createIcons();

        // Sync range and number inputs
        function syncInputs(numberId, rangeId, valueId, unit = 'ft') {
            const numberInput = document.getElementById(numberId);
            const rangeInput = document.getElementById(rangeId);
            const valueDisplay = document.getElementById(valueId);

            numberInput.addEventListener('input', () => {
                rangeInput.value = numberInput.value;
                valueDisplay.textContent = `${parseFloat(numberInput.value).toFixed(1)} ${unit}`;
            });
            rangeInput.addEventListener('input', () => {
                numberInput.value = rangeInput.value;
                valueDisplay.textContent = `${parseFloat(rangeInput.value).toFixed(1)} ${unit}`;
            });
        }

        syncInputs('unitWidth', 'unitWidthRange', 'unitWidthValue');
        syncInputs('unitDepth', 'unitDepthRange', 'unitDepthValue');
        syncInputs('unitHeight', 'unitHeightRange', 'unitHeightValue');
        syncInputs('spaceWidth', 'spaceWidthRange', 'spaceWidthValue');
        syncInputs('spaceDepth', 'spaceDepthRange', 'spaceDepthValue');
        syncInputs('clearanceSpace', 'clearanceSpaceRange', 'clearanceSpaceValue');

        // Form Submission
        document.getElementById('bathtubShowerFitForm').addEventListener('submit', function (e) {
            e.preventDefault();
            calculateBathtubShowerFit();
        });

        function calculateBathtubShowerFit() {
            const unitType = document.getElementById('unitType').value;
            const unitWidth = parseFloat(document.getElementById('unitWidth').value);
            const unitDepth = parseFloat(document.getElementById('unitDepth').value);
            const unitHeight = parseFloat(document.getElementById('unitHeight').value);
            const spaceWidth = parseFloat(document.getElementById('spaceWidth').value);
            const spaceDepth = parseFloat(document.getElementById('spaceDepth').value);
            const clearanceSpace = parseFloat(document.getElementById('clearanceSpace').value);

            if (!unitType || !unitWidth || !unitDepth || !unitHeight || !spaceWidth || !spaceDepth || !clearanceSpace ||
                unitWidth < 2.5 || unitDepth < 2 || unitHeight < 1.5 || spaceWidth < 4 || spaceDepth < 3 || clearanceSpace < 0.5) {
                alert('Please enter valid values (Unit Width ≥ 2.5 ft, Depth ≥ 2 ft, Height ≥ 1.5 ft, Space Width ≥ 4 ft, Depth ≥ 3 ft, Clearance ≥ 0.5 ft).');
                return;
            }

            // Calculate fit: Unit against wall with clearance
            const totalWidth = unitWidth + (clearanceSpace * 2); // Clearance on both sides
            const totalDepth = unitDepth + clearanceSpace; // Clearance in front
            const widthFit = totalWidth <= spaceWidth;
            const depthFit = totalDepth <= spaceDepth;
            const heightFit = unitHeight <= 8; // Standard ceiling height
            const overallFit = widthFit && depthFit && heightFit ? 'Yes' : 'No';

            // Calculate clearances
            const widthClearance = spaceWidth - totalWidth;
            const depthClearance = spaceDepth - totalDepth;
            const heightClearance = 8 - unitHeight;

            // Generate output
            const output = `
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6 bg-white p-6 rounded-xl shadow-sm">
                    <div>
                        <p class="font-medium text-gray-700">Unit Type</p>
                        <p class="text-xl text-blue-600 font-semibold">${unitType}</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Unit Dimensions</p>
                        <p class="text-lg">${unitWidth.toFixed(1)} x ${unitDepth.toFixed(1)} x ${unitHeight.toFixed(1)} ft</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Bathroom Space Dimensions</p>
                        <p class="text-lg">${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} ft</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Clearance Space</p>
                        <p class="text-lg">${clearanceSpace.toFixed(1)} ft</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Fit Assessment</p>
                        <p class="text-lg ${overallFit === 'Yes' ? 'text-green-600' : 'text-red-600'} font-semibold">
                            ${overallFit === 'Yes' ? 'Perfect Fit' : 'May Not Fit Comfortably'}
                        </p>
                        <p class="text-sm text-gray-600">${
                            overallFit === 'Yes'
                                ? `The ${unitType.toLowerCase()} fits with ${widthClearance.toFixed(1)} ft width clearance, ${depthClearance.toFixed(1)} ft depth clearance, and ${heightClearance.toFixed(1)} ft height clearance.`
                                : `Issues: ${
                                    !widthFit ? `Insufficient width clearance (need ≥${totalWidth.toFixed(1)} ft, have ${spaceWidth.toFixed(1)} ft). ` : ''
                                }${
                                    !depthFit ? `Insufficient depth clearance (need ≥${totalDepth.toFixed(1)} ft, have ${spaceDepth.toFixed(1)} ft). ` : ''
                                }${
                                    !heightFit ? `Excessive height (need ≤8 ft, have ${unitHeight.toFixed(1)} ft).` : ''
                                }`
                        }</p>
                    </div>
                </div>
                <div class="bg-blue-50 p-4 rounded-lg">
                    <p class="text-sm"><span class="font-medium">Tip:</span> Verify plumbing locations and door swing clearance before finalizing your bathroom layout.</p>
                </div>
            `;

            document.getElementById('resultOutput').innerHTML = output;
            document.getElementById('resultSection').classList.remove('hidden');

            // Draw visualization
            drawVisualization(unitType, unitWidth, unitDepth, unitHeight, spaceWidth, spaceDepth, clearanceSpace);

            // Setup PDF download
            setupPdfDownload(unitType, unitWidth, unitDepth, unitHeight, spaceWidth, spaceDepth, clearanceSpace, overallFit, widthClearance, depthClearance, heightClearance);
        }

        function drawVisualization(unitType, unitWidth, unitDepth, unitHeight, spaceWidth, spaceDepth, clearanceSpace) {
            const canvas = document.getElementById('bathroomCanvas');
            const ctx = canvas.getContext('2d');
            canvas.width = canvas.offsetWidth;
            canvas.height = canvas.offsetHeight;

            // Clear canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // Scale factors
            const scale = canvas.width / (spaceWidth * 1.5); // Scale to fit canvas
            const unitW = unitWidth * scale;
            const unitD = unitDepth * scale;
            const spaceW = spaceWidth * scale;
            const spaceH = spaceDepth * scale;
            const clearanceW = clearanceSpace * scale;

            // Draw background
            const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
            gradient.addColorStop(0, '#e5e7eb');
            gradient.addColorStop(1, '#d1d5db');
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            // Draw tiled floor
            ctx.fillStyle = 'rgba(120, 113, 108, 0.2)';
            ctx.fillRect(0, canvas.height * 0.7, canvas.width, canvas.height * 0.3);
            ctx.strokeStyle = '#d1d5db';
            ctx.lineWidth = 1;
            for (let x = 0; x < canvas.width; x += 20) {
                ctx.beginPath();
                ctx.moveTo(x, canvas.height * 0.7);
                ctx.lineTo(x, canvas.height);
                ctx.stroke();
            }
            for (let y = canvas.height * 0.7; y < canvas.height; y += 20) {
                ctx.beginPath();
                ctx.moveTo(0, y);
                ctx.lineTo(canvas.width, y);
                ctx.stroke();
            }

            // Draw bathroom space outline (dashed)
            ctx.strokeStyle = '#1e40af';
            ctx.lineWidth = 2;
            ctx.setLineDash([5, 5]);
            ctx.strokeRect(canvas.width * 0.55, canvas.height * 0.2, spaceW, spaceH);
            ctx.fillStyle = '#bfdbfe';
            ctx.globalAlpha = 0.3;
            ctx.fillRect(canvas.width * 0.55, canvas.height * 0.2, spaceW, spaceH);
            ctx.globalAlpha = 1.0;
            ctx.setLineDash([]);

            // Draw unit with realistic effects
            ctx.shadowColor = 'rgba(0, 0, 0, 0.4)';
            ctx.shadowBlur = 15;
            ctx.shadowOffsetX = 8;
            ctx.shadowOffsetY = 8;
            ctx.fillStyle = unitType === 'Bathtub' ? '#0284c7' : '#1e3a8a'; // Blue for bathtub, darker blue for shower
            const unitX = canvas.width * 0.15 + clearanceW;
            const unitY = canvas.height * 0.2 + (spaceH - unitD) / 2;
            if (unitType === 'Bathtub') {
                ctx.beginPath();
                ctx.moveTo(unitX + 20, unitY);
                ctx.lineTo(unitX + unitW - 20, unitY);
                ctx.quadraticCurveTo(unitX + unitW, unitY, unitX + unitW, unitY + 20);
                ctx.lineTo(unitX + unitW, unitY + unitD - 20);
                ctx.quadraticCurveTo(unitX + unitW, unitY + unitD, unitX + unitW - 20, unitY + unitD);
                ctx.lineTo(unitX + 20, unitY + unitD);
                ctx.quadraticCurveTo(unitX, unitY + unitD, unitX, unitY + unitD - 20);
                ctx.lineTo(unitX, unitY + 20);
                ctx.quadraticCurveTo(unitX, unitY, unitX + 20, unitY);
                ctx.fill();
            } else {
                ctx.fillRect(unitX, unitY, unitW, unitD);
            }

            // Draw unit texture
            const unitGradient = ctx.createLinearGradient(unitX, unitY, unitX + unitW, unitY + unitD);
            unitGradient.addColorStop(0, 'rgba(255, 255, 255, 0.15)');
            unitGradient.addColorStop(1, 'rgba(255, 255, 255, 0.05)');
            ctx.fillStyle = unitGradient;
            if (unitType === 'Bathtub') {
                ctx.beginPath();
                ctx.moveTo(unitX + 25, unitY + 5);
                ctx.lineTo(unitX + unitW - 25, unitY + 5);
                ctx.quadraticCurveTo(unitX + unitW - 5, unitY + 5, unitX + unitW - 5, unitY + 25);
                ctx.lineTo(unitX + unitW - 5, unitY + unitD - 25);
                ctx.quadraticCurveTo(unitX + unitW - 5, unitY + unitD - 5, unitX + unitW - 25, unitY + unitD - 5);
                ctx.lineTo(unitX + 25, unitY + unitD - 5);
                ctx.quadraticCurveTo(unitX + 5, unitY + unitD - 5, unitX + 5, unitY + unitD - 25);
                ctx.lineTo(unitX + 5, unitY + 25);
                ctx.quadraticCurveTo(unitX + 5, unitY + 5, unitX + 25, unitY + 5);
                ctx.fill();
            } else {
                ctx.fillRect(unitX + 5, unitY + 5, unitW - 10, unitD - 10);
            }

            // Draw text
            ctx.shadowColor = 'transparent';
            ctx.fillStyle = '#ffffff';
            ctx.font = 'bold 18px Poppins';
            ctx.textAlign = 'center';
            ctx.fillText(`${unitType} (${unitWidth.toFixed(1)} x ${unitDepth.toFixed(1)} ft)`, unitX + unitW / 2, unitY + unitD / 2);
            ctx.fillStyle = '#1e40af';
            ctx.fillText(`Bathroom Space (${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} ft)`, canvas.width * 0.55 + spaceW / 2, canvas.height * 0.2 + spaceH / 2);
            ctx.font = '14px Poppins';
            ctx.fillStyle = '#4b5563';
            ctx.fillText(`${unitType} vs. Bathroom Space`, canvas.width / 2, canvas.height - 30);
        }

        function setupPdfDownload(unitType, unitWidth, unitDepth, unitHeight, spaceWidth, spaceDepth, clearanceSpace, overallFit, widthClearance, depthClearance, heightClearance) {
            document.getElementById('downloadPdf').onclick = function () {
                const { jsPDF } = window.jspdf;
                const doc = new jsPDF();

                // Header
                doc.setFont('helvetica', 'bold');
                doc.setFontSize(20);
                doc.setTextColor(31, 64, 175);
                doc.text('HomeFit Tools - Bathtub/Shower Fit Assessment Report', 20, 20);

                doc.setFont('helvetica', 'normal');
                doc.setFontSize(10);
                doc.setTextColor(100, 100, 100);
                doc.text(`Generated on: ${new Date().toLocaleString()}`, 20, 28);

                // Results
                doc.setFontSize(14);
                doc.setTextColor(0, 0, 0);
                doc.text('Your Bathtub/Shower Fit Assessment', 20, 40);

                doc.setFontSize(11);
                doc.text(`Unit Type: ${unitType}`, 20, 50);
                doc.text(`Unit Dimensions: ${unitWidth.toFixed(1)} x ${unitDepth.toFixed(1)} x ${unitHeight.toFixed(1)} ft`, 20, 58);
                doc.text(`Bathroom Space Dimensions: ${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} ft`, 20, 66);
                doc.text(`Clearance Space: ${clearanceSpace.toFixed(1)} ft`, 20, 74);
                doc.text(`Fit Assessment: ${overallFit === 'Yes' ? 'Perfect Fit' : 'May Not Fit Comfortably'}`, 20, 82);
                doc.text(`Clearances: ${widthClearance.toFixed(1)} ft (width), ${depthClearance.toFixed(1)} ft (depth), ${heightClearance.toFixed(1)} ft (height)`, 20, 90);
                doc.text(`Recommendation: ${
                    overallFit === 'Yes'
                        ? `The ${unitType.toLowerCase()} fits well in the bathroom space.`
                        : 'Consider a smaller unit or adjust bathroom layout.'
                }`, 20, 98);

                // Expert Insights
                doc.setFont('helvetica', 'bold');
                doc.setFontSize(12);
                doc.text('Expert Insights', 20, 110);
                doc.setFont('helvetica', 'normal');
                doc.setFontSize(10);
                doc.text('• Clearance: Allow at least 1 ft for access and maintenance.', 20, 118);
                doc.text('• Plumbing: Ensure proximity to existing drain and water lines.', 20, 126);
                doc.text('• Ventilation: Place near a window or vent for moisture control.', 20, 134);
                doc.text('• Door Swing: Avoid obstructing door or shower door swing.', 20, 142);
                doc.text('• Safety: Ensure non-slip surfaces and grab bars for accessibility.', 20, 150);

                doc.save(`HomeFit_${unitType}_Fit_Assessment.pdf`);
            };
        }
    </script>