                </main>
            </div><!-- end dbm-main -->

            <!-- SQL Console (Full Width at Bottom) -->
            <div class="dbm-console expanded">
                <div class="dbm-console-header">
                    <div class="dbm-console-title">
                        <i data-lucide="terminal" class="icon"></i>
                        SQL Console
                    </div>
                    <i data-lucide="chevron-up" class="dbm-console-toggle"></i>
                </div>

                <div class="dbm-console-body">
                    <form id="sql-form" method="post" action="<?php echo Config::get('URL'); ?>sql/execute">
                        <?php $current_database = isset($this->database_name) ? $this->database_name : Config::get('DB_NAME'); ?>
                        <input type="hidden" name="database_name" value="<?php echo htmlspecialchars($current_database); ?>">

                        <div class="dbm-sql-editor">
                            <div id="sql-highlight" class="dbm-sql-highlight"></div>
                            <textarea name="sql_query" id="sql_query" placeholder="SELECT * FROM table_name LIMIT 10;

-- Write your SQL query here
-- Press Execute or Ctrl+Enter to run"></textarea>
                        </div>

                        <div class="dbm-sql-actions">
                            <button type="submit" class="dbm-btn dbm-btn-success">
                                <i data-lucide="play"></i>
                                Execute
                            </button>
                            <button type="button" class="dbm-btn dbm-btn-secondary" onclick="document.getElementById('sql_query').value = ''; document.getElementById('sql-highlight').innerHTML = '';">
                                Clear
                            </button>
                            <select class="db-select" onchange="document.querySelector('input[name=database_name]').value = this.value;">
                                <?php foreach (DatabaseModel::getAllDatabases() as $db): ?>
                                    <option value="<?php echo htmlspecialchars($db); ?>" <?php echo $db === $current_database ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($db); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </form>

                    <div id="sql-result" class="dbm-sql-result">
                        <?php
                        // Check for session result
                        $result = Session::get('sql_result');
                        if ($result) {
                            Session::set('sql_result', null);

                            if ($result['success']) {
                                echo '<div class="dbm-sql-result success">';
                                echo '<div class="dbm-sql-result-header">';
                                echo '<i data-lucide="check-circle"></i>';
                                echo htmlspecialchars($result['message']);
                                echo '<span style="margin-left: auto; color: var(--text-muted); font-size: 12px;">' . $result['execution_time'] . 'ms</span>';
                                echo '</div>';

                                if (!empty($result['result'])) {
                                    echo '<div class="dbm-sql-result-body"><div class="dbm-table-wrapper"><table class="dbm-table"><thead><tr>';
                                    foreach (array_keys($result['result'][0]) as $col) {
                                        echo '<th>' . htmlspecialchars($col) . '</th>';
                                    }
                                    echo '</tr></thead><tbody>';
                                    foreach ($result['result'] as $row) {
                                        echo '<tr>';
                                        foreach ($row as $value) {
                                            echo '<td>' . ($value === null ? '<span class="null-value">NULL</span>' : htmlspecialchars(substr($value, 0, 100))) . '</td>';
                                        }
                                        echo '</tr>';
                                    }
                                    echo '</tbody></table></div></div>';
                                }
                                echo '</div>';
                            } else {
                                echo '<div class="dbm-sql-result error">';
                                echo '<div class="dbm-sql-result-header">';
                                echo '<i data-lucide="x-circle"></i>';
                                echo htmlspecialchars($result['message']);
                                echo '</div>';
                                if (!empty($result['error'])) {
                                    echo '<div class="dbm-sql-result-body" style="padding: 16px; font-family: monospace; font-size: 13px; color: var(--accent-red);">' . htmlspecialchars($result['error']) . '</div>';
                                }
                                echo '</div>';
                            }
                        }
                        ?>
                    </div>
                </div>
            </div>

        </div><!-- end dbm-wrapper -->

    </div><!-- end wrapper -->

    <script src="<?php echo Config::get('URL'); ?>js/dbmanager.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            lucide.createIcons();
            DBManager.init('<?php echo Config::get('URL'); ?>');
        });
    </script>
</body>
</html>
