<div class="fade-in">
    <!-- Profile Header with Cover -->
    <div class="profile-header card" style="padding: 0; overflow: hidden; margin-bottom: 2rem; position: relative;">
        <?php $cover = \App\Helpers\Asset::cover($profile['cover_image'] ?? null); ?>
        <div class="cover-photo" style="height: 180px; background: <?= (strpos($cover, 'gradient') !== false) ? $cover : "url('$cover')" ?>; background-size: cover; background-position: center <?= $profile['cover_offset'] ?? 0 ?>%;"></div>
        
        <div class="profile-header-inner" style="padding: 0 1.5rem 1.5rem; display: flex; gap: 1.5rem; align-items: flex-end; margin-top: -60px; position: relative; z-index: 2; flex-wrap: wrap;">
            <div style="position: relative;">
                <img src="<?= \App\Helpers\Asset::avatar($profile['avatar'] ?? null, $profile['username'], 120) ?>" style="width: 120px; height: 120px; border-radius: 50%; border: 5px solid var(--card); object-fit: cover; box-shadow: var(--shadow);">
                <div style="position: absolute; bottom: 15px; right: 15px; background: var(--primary); color: white; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 3px solid var(--card);">
                    <i class="fas fa-check" style="font-size: 0.7rem;"></i>
                </div>
            </div>
            
            <div style="flex: 1; padding-bottom: 1rem;">
                <!-- Admin Controls -->
            <?php if (\App\Core\Session::get('role') == 3 && \App\Core\Session::get('user_id') != $profile['id']): ?>
                <div style="background: rgba(239, 68, 68, 0.05); border: 1px solid rgba(239, 68, 68, 0.2); border-radius: 1rem; padding: 1rem; display: flex; align-items: center; justify-content: space-between; margin-bottom: 2rem;">
                    <div>
                        <h4 style="margin: 0; color: #ef4444;"><i class="fas fa-shield-alt"></i> <?= \App\Helpers\Lang::t('mod_tools') ?></h4>
                        <p style="margin: 0; font-size: 0.85rem; color: var(--text-muted);"><?= sprintf(\App\Helpers\Lang::t('manage_permissions'), $profile['username']) ?></p>
                    </div>
                    <div style="display: flex; gap: 0.5rem;">
                        <?php if ($profile['role_id'] != 0): ?>
                            <form action="/admin/users/ban/<?= $profile['id'] ?>" method="POST" onsubmit="return confirm('Restrict this user?')">
                                <?= \App\Core\CSRF::field() ?>
                                <button type="submit" class="btn" style="background: #ef4444; color: white;"><i class="fas fa-ban"></i> <?= \App\Helpers\Lang::t('ban_user') ?></button>
                            </form>
                        <?php else: ?>
                            <form action="/admin/users/unban/<?= $profile['id'] ?>" method="POST">
                                <?= \App\Core\CSRF::field() ?>
                                <button type="submit" class="btn" style="background: #10b981; color: white;"><i class="fas fa-undo"></i> <?= \App\Helpers\Lang::t('unban_user') ?></button>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>

            <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
                    <div>
                        <h1 style="margin: 0; font-size: clamp(1.4rem, 4vw, 2.5rem); font-weight: 800;"><?= $profile['username'] ?></h1>
                        <p style="color: var(--text-muted); margin: 0.25rem 0 0 0;"><?= \App\Helpers\Lang::t('member_since') ?> <?= date('M Y', strtotime($profile['created_at'])) ?></p>
                    </div>
                    <div style="display: flex; gap: 1rem;">
                        <?php if (\App\Core\Session::get('user_id') == $profile['id']): ?>
                            <a href="/profile/edit" class="btn btn-primary" style="padding: 0.8rem 1.5rem;"><i class="fas fa-edit"></i> <?= \App\Helpers\Lang::t('edit_profile') ?></a>
                        <?php else: ?>
                            <a href="/messages/<?= $profile['id'] ?>" class="btn btn-primary"><i class="fas fa-envelope"></i> <?= \App\Helpers\Lang::t('message') ?></a>
                            <button class="btn" style="background: var(--bg); border: 1px solid var(--border);"><i class="fas fa-ellipsis-h"></i></button>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: 2fr 1fr; gap: 2rem;" class="profile-main-grid">
        <div class="activity">
            <!-- Stats Dashboard -->
            <div class="profile-stats-grid" style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 2rem;">
                <div class="card" style="text-align: center; padding: 1.5rem;">
                    <div style="font-size: 1.5rem; font-weight: 800; color: var(--primary);"><?= $stats['posts'] ?></div>
                    <div style="font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;"><?= \App\Helpers\Lang::t('topics') ?></div>
                </div>
                <div class="card" style="text-align: center; padding: 1.5rem;">
                    <div style="font-size: 1.5rem; font-weight: 800; color: var(--primary);"><?= $stats['comments'] ?></div>
                    <div style="font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;"><?= \App\Helpers\Lang::t('comments') ?></div>
                </div>
                <div class="card" style="text-align: center; padding: 1.5rem;">
                    <div style="font-size: 1.5rem; font-weight: 800; color: var(--primary);"><?= $profile['reputation'] ?></div>
                    <div style="font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;"><?= \App\Helpers\Lang::t('reputation_label') ?></div>
                </div>
                <div class="card" style="text-align: center; padding: 1.5rem;">
                    <div style="font-size: 1.5rem; font-weight: 800; color: var(--primary);"><?= $stats['best_answers'] ?></div>
                    <div style="font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase;"><?= \App\Helpers\Lang::t('best_answers') ?></div>
                </div>
            </div>

            <h2 style="margin-bottom: 1.5rem;"><?= \App\Helpers\Lang::t('activity') ?></h2>
            
            <!-- Latest Posts -->
            <div class="card" style="padding: 0; margin-bottom: 2rem;">
                <div style="padding: 1.25rem; border-bottom: 1px solid var(--border); background: rgba(99, 102, 241, 0.05);">
                    <h3 style="margin: 0; font-size: 1rem;"><i class="fas fa-file-alt text-primary" style="margin-right: 0.5rem;"></i> <?= \App\Helpers\Lang::t('latest') ?> <?= \App\Helpers\Lang::t('posts') ?></h3>
                </div>
                <div style="padding: 0.5rem;">
                    <?php if (empty($posts)): ?>
                        <div style="padding: 2rem; text-align: center; color: var(--text-muted);"><?= \App\Helpers\Lang::t('no_posts_yet') ?></div>
                    <?php else: ?>
                        <?php foreach ($posts as $post): ?>
                        <a href="/post/<?= $post['slug'] ?>" class="card-hover" style="display: flex; justify-content: space-between; align-items: center; padding: 1rem; text-decoration: none; color: var(--text); border-bottom: 1px solid var(--border);">
                            <div>
                                <div style="font-weight: 600;"><?= htmlspecialchars($post['title']) ?></div>
                                <div style="font-size: 0.8rem; color: var(--text-muted);"><?= $post['category_name'] ?> • <?= date('d M, Y', strtotime($post['created_at'])) ?></div>
                            </div>
                            <div class="badge" style="background: var(--bg); border: 1px solid var(--border); color: var(--text);"><i class="fas fa-comment"></i> <?= $post['comment_count'] ?></div>
                        </a>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <!-- Latest Comments -->
            <div class="card" style="padding: 0;">
                <div style="padding: 1.25rem; border-bottom: 1px solid var(--border); background: rgba(16, 185, 129, 0.05);">
                    <h3 style="margin: 0; font-size: 1rem;"><i class="fas fa-comments text-success" style="margin-right: 0.5rem;"></i> <?= \App\Helpers\Lang::t('recent_comments') ?></h3>
                </div>
                <div style="padding: 0.5rem;">
                    <?php if (empty($comments)): ?>
                        <div style="padding: 2rem; text-align: center; color: var(--text-muted);"><?= \App\Helpers\Lang::t('no_comments_yet') ?></div>
                    <?php else: ?>
                        <?php foreach ($comments as $comment): ?>
                        <div style="padding: 1rem; border-bottom: 1px solid var(--border);">
                            <div style="font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.5rem;">
                                <?= \App\Helpers\Lang::t('commented_on') ?> <a href="/post/<?= $comment['post_slug'] ?>" style="color: var(--primary); font-weight: 600; text-decoration: none;"><?= htmlspecialchars($comment['post_title']) ?></a>
                            </div>
                            <div style="font-size: 0.95rem; line-height: 1.5;"><?= mb_strimwidth(strip_tags($comment['content']), 0, 150, "...") ?></div>
                        </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        
        <div class="sidebar">
            <h2 style="margin-bottom: 1.5rem;"><?= \App\Helpers\Lang::t('bio') ?></h2>
            <div class="card" style="margin-bottom: 2rem; line-height: 1.6;">
                <?= !empty($profile['bio']) ? nl2br(htmlspecialchars($profile['bio'])) : '<span style="color: var(--text-muted); font-style: italic;">' . \App\Helpers\Lang::t('no_bio_yet') . '</span>' ?>
            </div>

            <h2 style="margin-bottom: 1.5rem;"><?= \App\Helpers\Lang::t('achievements') ?></h2>
            <div class="card">
                <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 1rem;">
                    <div class="card" style="text-align: center; padding: 1rem; background: var(--bg);">
                        <div style="font-size: 2rem; margin-bottom: 0.5rem;">🚀</div>
                        <div style="font-size: 0.7rem; font-weight: 700; text-transform: uppercase;"><?= \App\Helpers\Lang::t('pioneer') ?></div>
                    </div>
                    <?php if ($profile['reputation'] >= 500): ?>
                        <div class="card" style="text-align: center; padding: 1rem; background: var(--bg);">
                            <div style="font-size: 2rem; margin-bottom: 0.5rem;">👑</div>
                            <div style="font-size: 0.7rem; font-weight: 700; text-transform: uppercase;"><?= \App\Helpers\Lang::t('top_contributor') ?></div>
                        </div>
                    <?php endif; ?>
                    <?php if ($stats['best_answers'] > 0): ?>
                        <div class="card" style="text-align: center; padding: 1rem; background: var(--bg);">
                            <div style="font-size: 2rem; margin-bottom: 0.5rem;">✅</div>
                            <div style="font-size: 0.7rem; font-weight: 700; text-transform: uppercase;"><?= \App\Helpers\Lang::t('solver') ?></div>
                        </div>
                    <?php endif; ?>
                    <?php if ($stats['posts'] > 10): ?>
                        <div class="card" style="text-align: center; padding: 1rem; background: var(--bg);">
                            <div style="font-size: 2rem; margin-bottom: 0.5rem;">✍️</div>
                            <div style="font-size: 0.7rem; font-weight: 700; text-transform: uppercase;"><?= \App\Helpers\Lang::t('active_writer') ?></div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>
