<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enterprise Mail Engine</title>
    <!-- Modern Font -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg-color: #0f172a;
            --surface: #1e293b;
            --surface-hover: #334155;
            --border: #334155;
            --text-main: #f8fafc;
            --text-muted: #94a3b8;
            --primary: #6366f1;
            --primary-hover: #818cf8;
            --success-bg: rgba(16, 185, 129, 0.15);
            --success-text: #34d399;
            --error-bg: rgba(239, 68, 68, 0.15);
            --error-text: #f87171;
            --input-bg: #0f172a;
        }

        * { box-sizing: border-box; }
        
        body { 
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif; 
            background-color: var(--bg-color);
            /* Subtle radiant glows behind the surfaces */
            background-image: 
                radial-gradient(circle at 15% 50%, rgba(99, 102, 241, 0.15), transparent 25%),
                radial-gradient(circle at 85% 30%, rgba(16, 185, 129, 0.1), transparent 25%);
            background-attachment: fixed;
            color: var(--text-main); 
            margin: 0; 
            padding: 40px 20px; 
            min-height: 100vh;
            line-height: 1.5;
        }

        .header-title {
            text-align: center;
            font-size: 2.8rem;
            margin-bottom: 40px;
            font-weight: 800;
            letter-spacing: -0.05em;
            background: linear-gradient(135deg, #818cf8 0%, #c084fc 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            text-shadow: 0 10px 30px rgba(99, 102, 241, 0.2);
        }
        
        .main-wrapper {
            display: flex;
            flex-wrap: wrap;
            max-width: 1200px;
            margin: 0 auto;
            gap: 24px;
            align-items: flex-start;
        }
        
        .form-section, .result-section {
            background: var(--surface);
            padding: 32px;
            border-radius: 16px;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
            border: 1px solid var(--border);
            backdrop-filter: blur(10px);
        }
        
        .form-section { flex: 1 1 550px; }
        
        .result-section {
            flex: 1 1 400px;
            position: sticky;
            top: 24px;
            max-height: calc(100vh - 48px);
            display: flex;
            flex-direction: column;
        }

        .results-container {
            overflow-y: auto;
            flex-grow: 1;
            padding-right: 8px; /* Space for scrollbar */
        }
        
        h2 { 
            margin-top: 0; 
            color: var(--text-main); 
            font-size: 1.25rem; 
            border-bottom: 1px solid var(--border); 
            padding-bottom: 16px; 
            margin-bottom: 24px; 
            font-weight: 600;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .form-group { margin-bottom: 24px; }
        label { 
            display: block; 
            margin-bottom: 8px; 
            font-weight: 600; 
            color: var(--text-muted); 
            font-size: 0.8rem; 
            text-transform: uppercase;
            letter-spacing: 0.05em;
        }
        
        input[type="text"], input[type="email"], textarea { 
            width: 100%; 
            padding: 14px 16px; 
            border: 1px solid var(--border); 
            border-radius: 8px; 
            font-family: inherit; 
            font-size: 0.95rem;
            background-color: var(--input-bg);
            color: var(--text-main);
            transition: all 0.2s ease;
        }
        
        input::placeholder, textarea::placeholder { color: #475569; }

        input:focus, textarea:focus { 
            border-color: var(--primary); 
            outline: none; 
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25); 
            background-color: #151f33;
        }
        
        button { 
            background: var(--primary);
            color: #fff; 
            border: none; 
            padding: 16px 24px; 
            font-size: 1rem; 
            border-radius: 8px; 
            cursor: pointer; 
            transition: all 0.2s ease; 
            width: 100%; 
            font-weight: 600; 
            margin-top: 8px;
            box-shadow: 0 4px 6px -1px rgba(99, 102, 241, 0.2);
        }
        
        button:hover { 
            background: var(--primary-hover);
            transform: translateY(-2px); 
            box-shadow: 0 10px 15px -3px rgba(99, 102, 241, 0.4); 
        }
        
        button:active { transform: translateY(0); }

        /* Minimal Badges for Results */
        .result-item { 
            padding: 12px 16px; 
            border-radius: 8px; 
            margin-bottom: 12px; 
            font-size: 0.9rem; 
            display: flex; 
            align-items: center; 
            gap: 16px;
            border: 1px solid var(--border);
            background: var(--input-bg);
        }
        
        .status-badge {
            padding: 4px 10px;
            border-radius: 6px;
            font-size: 0.75rem;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.05em;
            min-width: 65px;
            text-align: center;
        }

        .success .status-badge { background: var(--success-bg); color: var(--success-text); border: 1px solid rgba(16, 185, 129, 0.3);}
        .error .status-badge { background: var(--error-bg); color: var(--error-text); border: 1px solid rgba(239, 68, 68, 0.3);}
        
        .email-text { font-family: 'Courier New', Courier, monospace; color: var(--text-muted); }

        .summary { 
            font-weight: 600; 
            background: var(--input-bg); 
            padding: 16px; 
            border-radius: 8px; 
            margin-top: 24px; 
            display: flex;
            justify-content: space-between;
            color: var(--text-main); 
            font-size: 0.95rem;
            border: 1px solid var(--border);
        }
        
        .empty-state { 
            text-align: center; 
            color: var(--text-muted); 
            padding: 80px 0; 
            font-size: 0.95rem;
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: 16px;
            opacity: 0.7;
        }

        /* Custom Scrollbar */
        ::-webkit-scrollbar { width: 8px; }
        ::-webkit-scrollbar-track { background: transparent; }
        ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
        ::-webkit-scrollbar-thumb:hover { background: #475569; }

        @media (max-width: 900px) {
            .result-section { position: static; max-height: 500px;}
        }
    </style>
</head>
<body>
    <div class="header-title">Enterprise Mail Engine</div>

    <div class="main-wrapper">
        <!-- FORM SECTION -->
        <div class="form-section">
            <h2>
                <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z"></path></svg>
                Compose Campaign
            </h2>
            <form action="" method="POST">
                <div class="form-group">
                    <label for="sender_name">From Name</label>
                    <input type="text" id="sender_name" name="sender_name" required placeholder="e.g., Acme Corp." 
                           value="<?php echo isset($_POST['sender_name']) ? htmlspecialchars($_POST['sender_name']) : ''; ?>">
                </div>
                
                <div class="form-group">
                    <label for="sender_email">From Email</label>
                    <input type="email" id="sender_email" name="sender_email" required placeholder="e.g., secure@acme.com"
                           value="<?php echo isset($_POST['sender_email']) ? htmlspecialchars($_POST['sender_email']) : ''; ?>">
                </div>

                <div class="form-group">
                    <label for="subject">Subject</label>
                    <input type="text" id="subject" name="subject" required placeholder="Important Update [RANDOM-5]"
                           value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject']) : ''; ?>">
                </div>

                <div class="form-group">
                    <label for="message_body">Message Body (HTML Supported)</label>
                    <textarea id="message_body" name="message_body" rows="6" required placeholder="<h1>Greetings</h1><p>Your secure message...</p>"><?php echo isset($_POST['message_body']) ? htmlspecialchars($_POST['message_body']) : ''; ?></textarea>
                </div>

                <div class="form-group">
                    <label for="recipient_list">Recipient List (One email per line)</label>
                    <textarea id="recipient_list" name="recipient_list" rows="6" required placeholder="client1@domain.com&#10;client2@domain.com"><?php echo isset($_POST['recipient_list']) ? htmlspecialchars($_POST['recipient_list']) : ''; ?></textarea>
                </div>

                <button type="submit">Deploy Campaign</button>
            </form>
        </div>

        <!-- RESULT SECTION -->
        <div class="result-section">
            <h2>
                <svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
                Transmission Logs
            </h2>
            <div class="results-container">
            <?php
            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                $sender_name = filter_var($_POST['sender_name'], FILTER_SANITIZE_STRING);
                $sender_email = filter_var($_POST['sender_email'], FILTER_SANITIZE_EMAIL);
                $subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
                $message_body = $_POST['message_body'];
                $recipient_list = $_POST['recipient_list'];

                if (filter_var($sender_email, FILTER_VALIDATE_EMAIL) && !empty($subject) && !empty($message_body) && !empty($recipient_list)) {
                    
                    $headers = "MIME-Version: 1.0\r\n";
                    $headers .= "Content-type: text/html; charset=UTF-8\r\n";
                    $headers .= "From: {$sender_name} <{$sender_email}>\r\n";
                    $headers .= "Reply-To: {$sender_email}\r\n";
                    $headers .= "X-Mailer: PHP/" . phpversion();

                    $recipients = explode("\n", str_replace("\r", "", $recipient_list));
                    $recipients = array_filter(array_map('trim', $recipients));

                    $success_count = 0;
                    $error_count = 0;

                    foreach ($recipients as $email) {
                        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                            // Dynamic [RANDOM-X] tag: X represents desired length of random number.
                            $dynamic_subject = preg_replace_callback('/\[RANDOM\-(\d+)\]/', function($matches) {
                                $length = (int)$matches[1];
                                $random_str = '';
                                for ($i = 0; $i < $length; $i++) {
                                    $random_str .= mt_rand(0, 9);
                                }
                                return $random_str;
                            }, $subject);

                            // PHP Native Mail Function Check
                            if (mail($email, $dynamic_subject, $message_body, $headers)) {
                                echo "<div class='result-item success'><span class='status-badge'>Sent</span> <span class='email-text'>{$email}</span></div>";
                                $success_count++;
                            } else {
                                echo "<div class='result-item error'><span class='status-badge'>Fail</span> <span class='email-text'>{$email}</span></div>";
                                $error_count++;
                            }
                        } else {
                            echo "<div class='result-item error'><span class='status-badge'>Invalid</span> <span class='email-text'>{$email}</span></div>";
                            $error_count++;
                        }
                        usleep(100000); 
                    }
                    
                    echo "<div class='summary'>";
                    echo "<span>✅ Transmitted: <strong>{$success_count}</strong></span>";
                    echo "<span>❌ Failed: <strong>{$error_count}</strong></span>";
                    echo "</div>";
                } else {
                    echo "<div class='result-item error'><span class='status-badge'>Error</span> <span class='email-text'>Please verify all input fields.</span></div>";
                }
            } else {
                echo "<div class='empty-state'>";
                echo "<svg fill='none' stroke='currentColor' viewBox='0 0 24 24' xmlns='http://www.w3.org/2000/svg'><path stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z'></path></svg>";
                echo "<span>Logs will appear here during transmission.</span>";
                echo "</div>";
            }
            ?>
            </div>
        </div>
    </div>
</body>
</html>
