<?php
$file = 'wall_data.txt';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $date = trim($_POST['date'] ?? '');

    if (!empty($name) && !empty($date)) {
        // Clean input to prevent multi-line injection or basic HTML formatting issues
        $cleanName = htmlspecialchars(str_replace(array("\r", "\n", "|"), '', $name));
        $cleanDate = htmlspecialchars(str_replace(array("\r", "\n", "|"), '', $date));
        
        $entry = $cleanName . "|" . $cleanDate . PHP_EOL;
        file_put_contents($file, $entry, FILE_APPEND | LOCK_EX);
    }
    
    // Redirect to prevent duplicate form submissions on refresh
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}

// Read existing entries
$bricks = [];
if (file_exists($file)) {
    $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($lines as $line) {
        $parts = explode('|', $line);
        if (count($parts) === 2) {
            $bricks[] = [
                'name' => $parts[0],
                'date' => $parts[1]
            ];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Write on the Wall</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #1a1a1a;
            font-family: Arial, sans-serif;
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        h1 {
            margin-bottom: 20px;
            letter-spacing: 2px;
            text-transform: uppercase;
            text-shadow: 2px 2px 4px #000;
        }

        /* Brick Wall Container */
        .wall-container {
            width: 100%;
            max-width: 900px;
            background-color: #3d2314;
            padding: 12px;
            border-radius: 6px;
            box-shadow: inset 0 0 10px #000, 0 8px 16px rgba(0,0,0,0.5);
            margin-bottom: 30px;
        }

        .wall {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
        }

        /* Individual Brick Styling */
        .brick {
            background: linear-gradient(145deg, #b33b24, #8c2a17);
            border: 1px solid #6b1f10;
            border-radius: 3px;
            box-shadow: inset 1px 1px 2px rgba(255,255,255,0.2), 2px 2px 4px rgba(0,0,0,0.4);
            flex: 1 1 180px;
            min-height: 70px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 8px;
            text-align: center;
            word-break: break-word;
        }

        .brick .name {
            font-weight: bold;
            font-size: 1.05rem;
            color: #fceabb;
            text-shadow: 1px 1px 2px #000;
        }

        .brick .date {
            font-size: 0.75rem;
            color: #d1c7b7;
            margin-top: 4px;
        }

        /* Form Container */
        .form-container {
            background-color: #2a2a2a;
            padding: 24px;
            border-radius: 8px;
            width: 100%;
            max-width: 450px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
            border: 1px solid #444;
        }

        .form-container h2 {
            font-size: 1.2rem;
            margin-bottom: 15px;
            text-align: center;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-size: 0.9rem;
            color: #ccc;
        }

        input[type="text"],
        input[type="date"] {
            width: 100%;
            padding: 10px;
            border-radius: 4px;
            border: 1px solid #555;
            background-color: #1a1a1a;
            color: #fff;
            font-size: 1rem;
        }

        input[type="text"]:focus,
        input[type="date"]:focus {
            outline: none;
            border-color: #b33b24;
        }

        button {
            width: 100%;
            padding: 12px;
            background-color: #b33b24;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.2s;
        }

        button:hover {
            background-color: #8c2a17;
        }
    </style>
</head>
<body>
<!-- NERGmud Two banner: paste this whole block into any page (or a WordPress "Custom HTML" block).
     Everything is self-contained; the class names all start with "nmud-" so it won't clash with your site. -->
<div class="nmud-banner">
  <style>
    .nmud-banner{--nmud-bg:#0f0e0c;--nmud-screen:#0b0a09;--nmud-line:#2e2a24;--nmud-text:#e4ddcf;--nmud-muted:#958c7d;--nmud-amber:#e2b45c;--nmud-green:#8cc07a;
      box-sizing:border-box;max-width:760px;margin:24px auto;padding:24px;border-radius:12px;background:var(--nmud-bg);
      border:1px solid var(--nmud-line);color:var(--nmud-text);font:16px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;
      display:grid;grid-template-columns:1fr 1fr;gap:24px;align-items:center}
    .nmud-banner *{box-sizing:border-box}
    .nmud-banner .nmud-kicker{margin:0 0 6px;color:var(--nmud-amber);font-size:13px;letter-spacing:.12em;text-transform:uppercase;font-weight:600}
    .nmud-banner h2{margin:0 0 10px;font:700 28px/1.2 Georgia,"Times New Roman",serif;color:var(--nmud-text)}
    .nmud-banner h2 span{color:var(--nmud-amber)}
    .nmud-banner p{margin:0 0 16px;color:var(--nmud-text)}
    .nmud-banner .nmud-small{margin:12px 0 0;color:var(--nmud-muted);font-size:14px}
    .nmud-banner a.nmud-btn{display:inline-block;padding:11px 20px;border-radius:6px;background:var(--nmud-amber);color:#1a1408;
      font-weight:700;text-decoration:none;border:1px solid var(--nmud-amber)}
    .nmud-banner a.nmud-btn:hover{filter:brightness(1.08)}
    .nmud-banner a.nmud-btn:focus-visible{outline:2px solid var(--nmud-amber);outline-offset:3px}
    .nmud-banner .nmud-screen{background:var(--nmud-screen);border:1px solid var(--nmud-line);border-radius:8px;padding:14px 16px;
      min-height:200px;font:14px/1.6 ui-monospace,"Cascadia Mono",Menlo,Consolas,monospace;color:var(--nmud-text)}
    .nmud-banner .nmud-screen div{margin:0 0 6px;white-space:pre-wrap}
    .nmud-banner .nmud-cmd{color:var(--nmud-amber)}
    .nmud-banner .nmud-good{color:var(--nmud-green)}
    .nmud-banner .nmud-cursor{display:inline-block;width:8px;height:15px;background:var(--nmud-amber);vertical-align:-2px;animation:nmud-blink 1s steps(1) infinite}
    @keyframes nmud-blink{50%{opacity:0}}
    @media (max-width:640px){.nmud-banner{grid-template-columns:1fr;padding:18px}.nmud-banner h2{font-size:24px}}
    @media (prefers-reduced-motion:reduce){.nmud-banner .nmud-cursor{animation:none}}
  </style>

  <div>
    <p class="nmud-kicker">A free text adventure</p>
    <h2>There is a door under the hill. <span>Find it together.</span></h2>
    <p>NERGmud Two is a shared, Zork-style adventure you play in your browser. Explore Hollowmere, solve puzzles,
       and meet other explorers in the tunnels: lend a torch, trade a key, push a door that needs two pairs of hands.</p>
    <a class="nmud-btn" href="https://nergtwo.com/nergmud/">Start exploring →</a>
    <p class="nmud-small">Free · No download · Plays on your phone · Nothing can kill you</p>
  </div>

  <div class="nmud-screen" aria-label="Sample of play">
    <div class="nmud-cmd">&gt; light torch</div>
    <div class="nmud-good">You hold the torch to the forge. It catches and burns bright.</div>
    <div class="nmud-cmd">&gt; kill rat with dagger</div>
    <div class="nmud-good">The rat squeals once and goes still. The way east is clear.</div>
    <div>Mara says, "Wait for me! I need your light."</div>
    <div class="nmud-cmd">&gt; <span class="nmud-cursor" aria-hidden="true"></span></div>
  </div>
</div>

    <h1>Write on the Wall</h1>

    <div class="wall-container">
        <div class="wall">
            <?php if (empty($bricks)): ?>
                <div class="brick" style="flex: 1 1 100%;">
                    <span class="name">The wall is empty. Be the first to leave your mark!</span>
                </div>
            <?php else: ?>
                <?php foreach ($bricks as $brick): ?>
                    <div class="brick">
                        <span class="name"><?php echo $brick['name']; ?></span>
                        <span class="date"><?php echo $brick['date']; ?></span>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <div class="form-container">
        <h2>Sign the Brick Wall</h2>
        <form method="POST" action="">
            <div class="form-group">
                <label for="name">Your Name:</label>
                <input type="text" id="name" name="name" required placeholder="Enter your name">
            </div>
            <div class="form-group">
                <label for="date">Date Signed:</label>
                <input type="date" id="date" name="date" required value="<?php echo date('Y-m-d'); ?>">
            </div>
            <button type="submit">Add My Brick</button>
        </form>
    </div>

</body>
</html>
