<div class="forum-view-container">
    <div class="forum-breadcrumb">
        <a href="/forums">Форуми</a> > <span><?= htmlspecialchars($forum['name']) ?></span>
    </div>

    <div class="forum-title-section">
        <div class="forum-title-header">
            <div class="icon-badge">
                <i class="fas fa-<?= htmlspecialchars($forum['icon'] ?? 'comments') ?>"></i>
            </div>
            <div class="title-content">
                <h1><?= htmlspecialchars($forum['name']) ?></h1>
                <p><?= htmlspecialchars($forum['description']) ?></p>
            </div>
        </div>
        <a href="/post/create?category=<?= $forum['id'] ?>" class="btn btn-primary btn-lg">
            <i class="fas fa-plus"></i> Нова тема
        </a>
    </div>

    <div class="forum-stats-bar">
        <div class="stat-badge">
            <i class="fas fa-comments"></i>
            <div>
                <div class="badge-value"><?= number_format($stats['total_topics']) ?></div>
                <div class="badge-label">Теми</div>
            </div>
        </div>
        <div class="stat-badge">
            <i class="fas fa-message"></i>
            <div>
                <div class="badge-value"><?= number_format($stats['total_replies']) ?></div>
                <div class="badge-label">Відповіді</div>
            </div>
        </div>
        <div class="stat-badge">
            <i class="fas fa-users"></i>
            <div>
                <div class="badge-value"><?= number_format($stats['members']) ?></div>
                <div class="badge-label">Учасники</div>
            </div>
        </div>
        <div class="stat-badge">
            <i class="fas fa-circle" style="color: #10b981;"></i>
            <div>
                <div class="badge-value"><?= $stats['active_today'] ?></div>
                <div class="badge-label">Онлайн</div>
            </div>
        </div>
    </div>

    <div class="forum-content">
        <div class="topics-area">
            <div class="topics-header">
                <h2>Обговорення</h2>
                <div class="view-options">
                    <button class="view-btn active" data-sort="latest">Свіжі</button>
                    <button class="view-btn" data-sort="popular">Популярні</button>
                    <button class="view-btn" data-sort="unanswered">Без відповідей</button>
                </div>
            </div>

            <div class="topics-list">
                <?php if (!empty($posts)): ?>
                    <?php foreach ($posts as $post): ?>
                    <div class="topic-item" data-topic-id="<?= $post['id'] ?>">
                        <div class="topic-left">
                            <div class="vote-box">
                                <button class="vote-btn" onclick="voteOnTopic(<?= $post['id'] ?>, 1)">
                                    <i class="fas fa-arrow-up"></i>
                                </button>
                                <div class="vote-count" id="score-<?= $post['id'] ?>"><?= $post['score'] ?? 0 ?></div>
                                <button class="vote-btn" onclick="voteOnTopic(<?= $post['id'] ?>, -1)">
                                    <i class="fas fa-arrow-down"></i>
                                </button>
                            </div>
                        </div>

                        <div class="topic-main">
                            <div class="topic-type-badge">
                                <?php if ($post['type'] === 'question'): ?>
                                <span class="badge question">❓ Питання</span>
                                <?php endif; ?>
                            </div>
                            
                            <h3><a href="/post/<?= htmlspecialchars($post['slug']) ?>"><?= htmlspecialchars($post['title']) ?></a></h3>
                            
                            <div class="topic-meta">
                                <span class="author-mini">
                                    <img src="<?= \App\Helpers\Asset::avatar($post['avatar'], $post['username'], 20) ?>" alt="">
                                    <a href="/u/<?= htmlspecialchars($post['username']) ?>"><?= htmlspecialchars($post['username']) ?></a>
                                </span>
                                <span class="time"><?= date('d M Y H:i', strtotime($post['created_at'])) ?></span>
                                <span class="category"><?= htmlspecialchars($post['category_name']) ?></span>
                            </div>
                        </div>

                        <div class="topic-stats">
                            <div class="stat replies">
                                <div class="stat-num"><?= $post['comment_count'] ?? 0 ?></div>
                                <div class="stat-name">відповідей</div>
                            </div>
                            <div class="stat views">
                                <div class="stat-num"><?= number_format($post['view_count'] ?? 0) ?></div>
                                <div class="stat-name">переглядів</div>
                            </div>
                        </div>

                        <a href="/post/<?= htmlspecialchars($post['slug']) ?>" class="topic-link-arrow">
                            <i class="fas fa-arrow-right"></i>
                        </a>
                    </div>
                    <?php endforeach; ?>
                <?php else: ?>
                <div class="empty-topics">
                    <i class="fas fa-inbox"></i>
                    <h3>Нетем для обговорення</h3>
                    <p>Будьте першим, хто запустить обговорення!</p>
                    <a href="/post/create?category=<?= $forum['id'] ?>" class="btn btn-primary">
                        Почати тему
                    </a>
                </div>
                <?php endif; ?>
            </div>
        </div>

        <div class="forum-sidebar">
            <div class="sidebar-widget trending-widget">
                <div class="widget-title">🔥 Тренди в цьому форумі</div>
                <div class="trending-list">
                    <?php foreach ($trending as $item): ?>
                    <div class="trending-item">
                        <a href="/post/<?= htmlspecialchars($item['slug']) ?>"><?= htmlspecialchars(substr($item['title'], 0, 50)) ?>...</a>
                        <div class="trending-meta">
                            <span><i class="fas fa-comments"></i> <?= $item['comment_count'] ?? 0 ?></span>
                            <span><i class="fas fa-arrow-up"></i> <?= $item['score'] ?? 0 ?></span>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="sidebar-widget activity-widget">
                <div class="widget-title">⚡ Нова активність</div>
                <div class="activity-list">
                    <?php foreach (array_slice($recentActivity, 0, 5) as $activity): ?>
                    <div class="activity-item">
                        <img src="<?= \App\Helpers\Asset::avatar($activity['avatar'] ?? null, $activity['username'], 28) ?>" alt="">
                        <div class="activity-content">
                            <strong><?= htmlspecialchars($activity['username']) ?></strong>
                            <div class="activity-action">
                                <?= $activity['type'] === 'post' ? 'почав тему' : 'відповів' ?>
                            </div>
                            <small><?= date('d M Y', strtotime($activity['activity_date'])) ?></small>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="sidebar-widget rules-widget">
                <div class="widget-title">📋 Правила форуму</div>
                <ul class="rules-list">
                    <li>Поважайте думку інших</li>
                    <li>По-перше шукайте розв'язок</li>
                    <li>Запобігайте спаму та CAPS</li>
                    <li>Не розміщуйте зловмісні посилання</li>
                    <li>Дякуйте корисним постам</li>
                </ul>
            </div>
        </div>
    </div>
</div>

<style>
.forum-view-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem 1rem;
}

.forum-breadcrumb {
    margin-bottom: 2rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.forum-breadcrumb a {
    color: #6366f1;
    text-decoration: none;
}

.forum-title-section {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2.5rem;
    padding: 2rem;
    background: linear-gradient(135deg, rgba(99,102,241,0.1), rgba(139,92,246,0.05));
    border: 1px solid rgba(99,102,241,0.2);
    border-radius: 1.5rem;
    gap: 2rem;
}

.forum-title-header {
    display: flex;
    gap: 1.5rem;
    flex: 1;
    align-items: flex-start;
}

.icon-badge {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(99,102,241,0.2), rgba(139,92,246,0.15));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    color: #6366f1;
    flex-shrink: 0;
}

.title-content h1 {
    font-size: 2rem;
    font-weight: 800;
    margin-bottom: 0.5rem;
}

.title-content p {
    color: var(--text-muted);
}

.forum-stats-bar {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 1rem;
    margin-bottom: 2.5rem;
}

.stat-badge {
    padding: 1.5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: all 0.3s;
}

.stat-badge:hover {
    border-color: rgba(99,102,241,0.5);
}

.stat-badge i {
    font-size: 1.75rem;
    color: #6366f1;
}

.badge-value {
    font-weight: 700;
    font-size: 1.5rem;
}

.badge-label {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.forum-content {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 2rem;
}

.topics-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 2px solid var(--border);
}

.topics-header h2 {
    font-size: 1.5rem;
    font-weight: 700;
}

.view-options {
    display: flex;
    gap: 0.5rem;
}

.view-btn {
    padding: 0.5rem 1rem;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.3s;
    font-size: 0.9rem;
}

.view-btn:hover, .view-btn.active {
    background: #6366f1;
    border-color: #6366f1;
    color: white;
}

.topics-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.topic-item {
    display: flex;
    gap: 1rem;
    padding: 1.5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    transition: all 0.3s;
}

.topic-item:hover {
    border-color: rgba(99,102,241,0.5);
    box-shadow: 0 8px 16px rgba(99,102,241,0.1);
}

.topic-left {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.vote-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: rgba(99,102,241,0.1);
    border-radius: 0.5rem;
}

.vote-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.3s;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.vote-btn:hover {
    color: #6366f1;
    transform: scale(1.2);
}

.vote-count {
    font-weight: 700;
    color: #6366f1;
    min-width: 30px;
    text-align: center;
}

.topic-main {
    flex: 1;
    min-width: 0;
}

.topic-type-badge {
    margin-bottom: 0.5rem;
}

.badge {
    font-size: 0.75rem;
    padding: 0.25rem 0.75rem;
    border-radius: 0.5rem;
    background: rgba(99,102,241,0.1);
    color: #6366f1;
}

.topic-main h3 {
    margin-bottom: 0.75rem;
}

.topic-main a {
    color: var(--text);
    text-decoration: none;
    transition: color 0.3s;
}

.topic-main a:hover {
    color: #6366f1;
}

.topic-meta {
    display: flex;
    gap: 1.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
    flex-wrap: wrap;
}

.author-mini {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.author-mini img {
    width: 20px;
    height: 20px;
    border-radius: 50%;
}

.author-mini a {
    color: #6366f1;
    text-decoration: none;
}

.topic-stats {
    display: flex;
    gap: 2rem;
    min-width: 120px;
    text-align: center;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-num {
    font-weight: 700;
    font-size: 1.25rem;
    color: #6366f1;
}

.stat-name {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.topic-link-arrow {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(99,102,241,0.1);
    color: #6366f1;
    transition: all 0.3s;
    cursor: pointer;
    border: none;
    text-decoration: none;
}

.topic-link-arrow:hover {
    background: #6366f1;
    color: white;
}

.empty-topics {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

.empty-topics i {
    font-size: 3rem;
    margin-bottom: 1rem;
    opacity: 0.5;
}

.forum-sidebar {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.sidebar-widget {
    padding: 1.5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 1rem;
}

.widget-title {
    font-size: 1rem;
    font-weight: 700;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border);
}

.trending-list, .activity-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.trending-item {
    padding: 0.75rem;
    background: rgba(99,102,241,0.05);
    border-radius: 0.5rem;
    transition: all 0.3s;
}

.trending-item:hover {
    background: rgba(99,102,241,0.1);
}

.trending-item a {
    color: var(--text);
    text-decoration: none;
    font-size: 0.9rem;
    display: block;
    margin-bottom: 0.5rem;
    transition: color 0.3s;
}

.trending-item a:hover {
    color: #6366f1;
}

.trending-meta {
    display: flex;
    gap: 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.activity-item {
    display: flex;
    gap: 0.75rem;
    padding: 0.75rem;
    background: rgba(99,102,241,0.05);
    border-radius: 0.5rem;
}

.activity-item img {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    flex-shrink: 0;
}

.activity-content {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.85rem;
}

.activity-content strong {
    display: block;
}

.activity-action {
    color: var(--text-muted);
    font-size: 0.8rem;
}

.rules-list {
    list-style: none;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.rules-list li {
    padding-left: 1.5rem;
    position: relative;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.rules-list li::before {
    content: '✓';
    position: absolute;
    left: 0;
    color: #10b981;
    font-weight: 700;
}

@media (max-width: 1024px) {
    .forum-content {
        grid-template-columns: 1fr;
    }

    .topic-stats {
        display: none;
    }
}

@media (max-width: 768px) {
    .forum-title-section {
        flex-direction: column;
    }

    .topics-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .topic-item {
        flex-direction: column;
    }

    .topic-stats {
        display: flex;
    }
}

@media (max-width: 480px) {
    .forum-view-container {
        padding: 1rem;
    }

    .forum-title-section {
        padding: 1rem;
    }

    .stat-badge {
        flex-direction: column;
        min-width: 100%;
    }

    .icon-badge {
        width: 60px;
        height: 60px;
        font-size: 1.5rem;
    }
}
</style>

<script>
function voteOnTopic(postId, type) {
    fetch('/post/' + postId + '/vote', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-Requested-With': 'XMLHttpRequest'
        },
        body: 'type=' + type
    })
    .then(r => r.json())
    .then(data => {
        if (data.score !== undefined) {
            document.getElementById('score-' + postId).textContent = data.score;
        }
    });
}
</script>
