<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chatbox</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }
        
        #chat-container {
            width: 100%;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        
        #chat-box {
            height: 300px;
            overflow-y: auto;
            padding: 10px;
            border: 1px solid #ccc;
        }
        
        /* Mobile-specific styles */
        @media only screen and (max-width: 768px) {
            #chat-container {
                padding: 10px;
            }
            
            #chat-box {
                height: 200px;
            }
        }
    </style>
</head>
<body>
    <?php if (!isset($_SESSION['username'])) { ?>
        <form method="post">
            <input type="text" id="username-input" name="username" placeholder="Choose a username" required>
            <button type="submit">Join Chat</button>
            <?php if (isset($error)) { ?>
                <div style="color: red;"><?= $error ?></div>
            <?php } ?>
        </form>
    <?php } else { ?>
        <div id="chat-container">
            <h2>Chatbox</h2>
            <form method="post">
                <input type="hidden" name="username" value="<?= $_SESSION['username'] ?>">
                <input type="text" id="message-input" name="message" placeholder="Type message">
                <button id="send-btn" type="submit">Send</button>
            </form>
            <div id="chat-box">
                <?php 
                    $chatLog = file_get_contents('chat_log.txt');
                    echo nl2br($chatLog);
                ?>
            </div>
        </div>
        <script>
            var chatBox = document.getElementById('chat-box');
            chatBox.scrollTop = chatBox.scrollHeight;
        </script>
    <?php } ?>
</body>
</html>