<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name    = htmlspecialchars($_POST['name']);
    $email   = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    $to      = "infobhavik4@gmail.com";
    $subject = "New Inquiry - Website Contact Form";

    $body = "
    <html>
    <head>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f4f6f8;
                padding: 20px;
            }
            .container {
                max-width: 600px;
                margin: auto;
                background: #ffffff;
                border-radius: 8px;
                overflow: hidden;
                box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            }
            .header {
                background: #0d6efd;
                color: #ffffff;
                padding: 15px;
                text-align: center;
                font-size: 18px;
                font-weight: bold;
            }
            table {
                width: 100%;
                border-collapse: collapse;
            }
            td {
                padding: 12px;
                border-bottom: 1px solid #ddd;
            }
            td.label {
                font-weight: bold;
                background: #f8f9fa;
                width: 35%;
            }
            .footer {
                padding: 12px;
                font-size: 12px;
                color: #666;
                text-align: center;
                background: #f1f1f1;
            }
        </style>
    </head>
    <body>

        <div class='container'>
            <div class='header'>
                New Contact Message
            </div>

            <table>
                <tr>
                    <td class='label'>Full Name</td>
                    <td>$name</td>
                </tr>
                <tr>
                    <td class='label'>Email Address</td>
                    <td>$email</td>
                </tr>
                <tr>
                    <td class='label'>Message</td>
                    <td>$message</td>
                </tr>
            </table>

            <div class='footer'>
                This message was sent from your website contact form.
            </div>
        </div>

    </body>
    </html>
    ";

    // ✅ HEADERS (IMPORTANT FOR HTML EMAIL)
    $headers  = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: Website <no-reply@yourdomain.com>" . "\r\n";
    $headers .= "Reply-To: $email" . "\r\n";

    // ✅ SEND MAIL
    if (mail($to, $subject, $body, $headers)) {
        echo "<script>
            alert('Message sent successfully!');
            window.location.href='index.html';
        </script>";
    } else {
        echo "<script>
            alert('Failed to send message.');
            window.history.back();
        </script>";
    }
}
