<?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 Pet Crate/Carrier & Space Details</h2>
                <p class="text-gray-500 mb-8 text-lg">Input dimensions to check if your pet crate or carrier fits in a vehicle, home space, or airline compartment.</p>
                <form id="petCrateFitForm" class="space-y-8">
                    <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                        <div class="space-y-3">
                            <label for="spaceType" class="block text-sm font-medium text-gray-700">Space Type</label>
                            <div class="relative flex items-center">
                                <i data-lucide="car" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <select id="spaceType" 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="Vehicle" selected>Vehicle</option>
                                    <option value="Home Space">Home Space</option>
                                    <option value="Airline Compartment">Airline Compartment</option>
                                </select>
                            </div>
                        </div>
                        <div class="space-y-3">
                            <label for="crateWidth" class="block text-sm font-medium text-gray-700">Crate Width (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="package" class="absolute left-4 top-1/2 transform -translate-y-1/2 text-gray-400"></i>
                                <input type="number" id="crateWidth" 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="0.8" max="4" value="2.0" required>
                            </div>
                            <input type="range" id="crateWidthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="0.8" max="4" step="0.1" value="2.0">
                            <p id="crateWidthValue" class="text-sm text-gray-500">2.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="crateDepth" class="block text-sm font-medium text-gray-700">Crate 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="crateDepth" 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="1" max="4" value="2.5" required>
                            </div>
                            <input type="range" id="crateDepthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="1" max="4" step="0.1" value="2.5">
                            <p id="crateDepthValue" class="text-sm text-gray-500">2.5 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="crateHeight" class="block text-sm font-medium text-gray-700">Crate 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="crateHeight" 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.5" step="0.1" min="0.8" max="3" value="1.5" required>
                            </div>
                            <input type="range" id="crateHeightRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="0.8" max="3" step="0.1" value="1.5">
                            <p id="crateHeightValue" class="text-sm text-gray-500">1.5 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">Space Width (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="car" 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="3.0" step="0.1" min="1" max="6" value="3.0" required>
                            </div>
                            <input type="range" id="spaceWidthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="1" max="6" step="0.1" value="3.0">
                            <p id="spaceWidthValue" class="text-sm text-gray-500">3.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="spaceDepth" class="block text-sm font-medium text-gray-700">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="3.0" step="0.1" min="1" max="6" value="3.0" required>
                            </div>
                            <input type="range" id="spaceDepthRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="1" max="6" step="0.1" value="3.0">
                            <p id="spaceDepthValue" class="text-sm text-gray-500">3.0 ft</p>
                        </div>
                        <div class="space-y-3">
                            <label for="spaceHeight" class="block text-sm font-medium text-gray-700">Space 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="spaceHeight" 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" max="4" value="2.0" required>
                            </div>
                            <input type="range" id="spaceHeightRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="1" max="4" step="0.1" value="2.0">
                            <p id="spaceHeightValue" class="text-sm text-gray-500">2.0 ft</p>
                        </div>
                    </div>
                    <div class="grid grid-cols-1 md:grid-cols-1 gap-6">
                        <div class="space-y-3">
                            <label for="clearanceSpace" class="block text-sm font-medium text-gray-700">Clearance for Access (ft)</label>
                            <div class="relative flex items-center">
                                <i data-lucide="hand" 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="0.2" step="0.1" min="0.1" max="0.5" value="0.2" required>
                            </div>
                            <input type="range" id="clearanceSpaceRange" class="range-input w-full h-2 bg-gray-200 rounded-lg cursor-pointer" min="0.1" max="0.5" step="0.1" value="0.2">
                            <p id="clearanceSpaceValue" class="text-sm text-gray-500">0.2 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 Pet Crate/Carrier 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 Pet Crate/Carrier 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 pet crate sizing and airline/vehicle/home space 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="package" class="mr-3 text-blue-500 h-6 w-6"></i>
                        Interactive Pet Crate/Carrier & Space Visualization
                    </h3>
                    <div class="relative w-full h-96 bg-gray-200 rounded-xl overflow-hidden shadow-lg">
                        <canvas id="petCrateCanvas" class="w-full h-full"></canvas>
                    </div>
                    <p class="text-sm text-gray-600 mt-4">Visualization showing the pet crate/carrier in the selected space with clearance for access.</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 Pet Crate/Carrier Fit
                    </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 0.1-0.5 ft for easy placement and removal.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Airline Compliance:</strong> Check airline-specific size restrictions for cabin or cargo.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Comfort:</strong> Ensure the crate is large enough for your pet to stand and turn.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Accessibility:</strong> Choose crates with secure doors for easy pet access.</span>
                        </li>
                        <li class="flex items-start">
                            <i data-lucide="check" class="mr-2 text-green-500 h-5 w-5"></i>
                            <span><strong>Stability:</strong> Secure the crate in vehicles to prevent sliding.</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('crateWidth', 'crateWidthRange', 'crateWidthValue');
        syncInputs('crateDepth', 'crateDepthRange', 'crateDepthValue');
        syncInputs('crateHeight', 'crateHeightRange', 'crateHeightValue');
        syncInputs('spaceWidth', 'spaceWidthRange', 'spaceWidthValue');
        syncInputs('spaceDepth', 'spaceDepthRange', 'spaceDepthValue');
        syncInputs('spaceHeight', 'spaceHeightRange', 'spaceHeightValue');
        syncInputs('clearanceSpace', 'clearanceSpaceRange', 'clearanceSpaceValue');

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

        function calculatePetCrateFit() {
            const spaceType = document.getElementById('spaceType').value;
            const crateWidth = parseFloat(document.getElementById('crateWidth').value);
            const crateDepth = parseFloat(document.getElementById('crateDepth').value);
            const crateHeight = parseFloat(document.getElementById('crateHeight').value);
            const spaceWidth = parseFloat(document.getElementById('spaceWidth').value);
            const spaceDepth = parseFloat(document.getElementById('spaceDepth').value);
            const spaceHeight = parseFloat(document.getElementById('spaceHeight').value);
            const clearanceSpace = parseFloat(document.getElementById('clearanceSpace').value);

            if (!spaceType || !crateWidth || !crateDepth || !crateHeight || !spaceWidth || !spaceDepth || !spaceHeight || !clearanceSpace ||
                crateWidth < 0.8 || crateDepth < 1 || crateHeight < 0.8 || spaceWidth < 1 || spaceDepth < 1 || spaceHeight < 1 || clearanceSpace < 0.1) {
                alert('Please enter valid values (Crate Width/Height ≥ 0.8 ft, Depth ≥ 1 ft, Space Width/Depth/Height ≥ 1 ft, Clearance ≥ 0.1 ft).');
                return;
            }

            // Calculate fit: Crate in space with clearance
            const totalWidth = crateWidth + (clearanceSpace * 2); // Clearance on both sides
            const totalDepth = crateDepth + clearanceSpace; // Clearance at front
            const totalHeight = crateHeight + clearanceSpace; // Clearance above
            const widthFit = totalWidth <= spaceWidth;
            const depthFit = totalDepth <= spaceDepth;
            const heightFit = totalHeight <= spaceHeight;
            const overallFit = widthFit && depthFit && heightFit ? 'Yes' : 'No';

            // Calculate clearances
            const widthClearance = spaceWidth - crateWidth;
            const depthClearance = spaceDepth - crateDepth;
            const heightClearance = spaceHeight - crateHeight;

            // 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">Space Type</p>
                        <p class="text-xl text-blue-600 font-semibold">${spaceType}</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Crate/Carrier Dimensions</p>
                        <p class="text-lg">${crateWidth.toFixed(1)} x ${crateDepth.toFixed(1)} x ${crateHeight.toFixed(1)} ft</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Space Dimensions</p>
                        <p class="text-lg">${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} x ${spaceHeight.toFixed(1)} ft</p>
                    </div>
                    <div>
                        <p class="font-medium text-gray-700">Clearance for Access</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 pet crate/carrier fits in the ${spaceType.toLowerCase()} 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 ? `Insufficient height clearance (need ≥${totalHeight.toFixed(1)} ft, have ${spaceHeight.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> For airline compartments, verify specific airline size restrictions. Ensure the crate is secure in vehicles.</p>
                </div>
            `;

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

            // Draw visualization
            drawVisualization(spaceType, crateWidth, crateDepth, crateHeight, spaceWidth, spaceDepth, spaceHeight, clearanceSpace);

            // Setup PDF download
            setupPdfDownload(spaceType, crateWidth, crateDepth, crateHeight, spaceWidth, spaceDepth, spaceHeight, clearanceSpace, overallFit, widthClearance, depthClearance, heightClearance);
        }

        function drawVisualization(spaceType, crateWidth, crateDepth, crateHeight, spaceWidth, spaceDepth, spaceHeight, clearanceSpace) {
            const canvas = document.getElementById('petCrateCanvas');
            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 crateW = crateWidth * scale;
            const crateD = crateDepth * scale;
            const crateH = crateHeight * scale * 2; // Amplify height for visibility
            const spaceW = spaceWidth * scale;
            const spaceD = spaceDepth * scale;
            const spaceH = spaceHeight * scale * 2;
            const clearanceS = 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 floor
            ctx.fillStyle = 'rgba(120, 113, 108, 0.2)';
            ctx.fillRect(0, canvas.height * 0.7, canvas.width, canvas.height * 0.3);

            // Draw space outline (vehicle, home, or airline compartment)
            ctx.fillStyle = spaceType === 'Airline Compartment' ? '#4b5563' : '#374151';
            ctx.beginPath();
            ctx.moveTo(canvas.width * 0.2, canvas.height * 0.4);
            ctx.lineTo(canvas.width * 0.2 + spaceW, canvas.height * 0.4);
            ctx.lineTo(canvas.width * 0.2 + spaceW * 0.9, canvas.height * 0.4 + spaceD);
            ctx.lineTo(canvas.width * 0.2 + spaceW * 0.1, canvas.height * 0.4 + spaceD);
            ctx.closePath();
            ctx.fill();

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

            // Draw pet crate/carrier
            ctx.shadowColor = 'rgba(0, 0, 0, 0.4)';
            ctx.shadowBlur = 15;
            ctx.shadowOffsetX = 8;
            ctx.shadowOffsetY = 8;
            ctx.fillStyle = '#0284c7';
            const crateX = canvas.width * 0.2 + clearanceS;
            const crateY = canvas.height * 0.4 + (spaceD - crateD) / 2;
            ctx.fillRect(crateX, crateY, crateW, crateD);

            // Draw crate texture
            const crateGradient = ctx.createLinearGradient(crateX, crateY, crateX + crateW, crateY + crateD);
            crateGradient.addColorStop(0, 'rgba(255, 255, 255, 0.15)');
            crateGradient.addColorStop(1, 'rgba(255, 255, 255, 0.05)');
            ctx.fillStyle = crateGradient;
            ctx.fillRect(crateX + 5, crateY + 5, crateW - 10, crateD - 10);

            // Draw side view for height
            ctx.fillStyle = '#0284c7';
            ctx.fillRect(canvas.width * 0.8, canvas.height * 0.4, crateW * 0.3, crateH);
            ctx.fillStyle = '#4b5563';
            ctx.fillRect(canvas.width * 0.8, canvas.height * 0.4 - spaceH, crateW * 0.3, spaceH);
            ctx.fillStyle = 'rgba(34, 197, 94, 0.3)';
            ctx.fillRect(canvas.width * 0.8, canvas.height * 0.4 - spaceH, crateW * 0.3, clearanceS);

            // Draw text
            ctx.shadowColor = 'transparent';
            ctx.fillStyle = '#ffffff';
            ctx.font = 'bold 18px Poppins';
            ctx.textAlign = 'center';
            ctx.fillText(`Crate (${crateWidth.toFixed(1)} x ${crateDepth.toFixed(1)} ft)`, crateX + crateW / 2, crateY + crateD / 2);
            ctx.fillStyle = '#1e40af';
            ctx.fillText(`${spaceType} (${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} ft)`, canvas.width * 0.2 + spaceW / 2, canvas.height * 0.4 + spaceD / 2 + 20);
            ctx.font = '14px Poppins';
            ctx.fillStyle = '#4b5563';
            ctx.fillText(`Clearance: ${clearanceSpace.toFixed(1)} ft, Height: ${crateHeight.toFixed(1)} ft`, canvas.width / 2, canvas.height - 30);
        }

        function setupPdfDownload(spaceType, crateWidth, crateDepth, crateHeight, spaceWidth, spaceDepth, spaceHeight, 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 - Pet Crate/Carrier 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 Pet Crate/Carrier Fit Assessment`, 20, 40);

                doc.setFontSize(11);
                doc.text(`Space Type: ${spaceType}`, 20, 50);
                doc.text(`Crate/Carrier Dimensions: ${crateWidth.toFixed(1)} x ${crateDepth.toFixed(1)} x ${crateHeight.toFixed(1)} ft`, 20, 58);
                doc.text(`Space Dimensions: ${spaceWidth.toFixed(1)} x ${spaceDepth.toFixed(1)} x ${spaceHeight.toFixed(1)} ft`, 20, 66);
                doc.text(`Clearance for Access: ${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 pet crate/carrier fits well in the ${spaceType.toLowerCase()}.`
                        : `Consider a smaller crate or a larger ${spaceType.toLowerCase()}.`
                }`, 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 0.1-0.5 ft for easy placement and removal.', 20, 118);
                doc.text('• Airline Compliance: Check airline-specific size restrictions for cabin or cargo.', 20, 126);
                doc.text('• Comfort: Ensure the crate is large enough for your pet to stand and turn.', 20, 134);
                doc.text('• Accessibility: Choose crates with secure doors for easy pet access.', 20, 142);
                doc.text('• Stability: Secure the crate in vehicles to prevent sliding.', 20, 150);

                doc.save(`HomeFit_Pet_Crate_Fit_Assessment.pdf`);
            };
        }
    </script>