<?php
require_once 'config.php';

// 开启Session
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// 深色模式逻辑
$current_hour = date('H');
$is_night_time = ($current_hour >= 19 || $current_hour < 8); 
$dark_mode_cookie = isset($_COOKIE['dark_mode']) ? $_COOKIE['dark_mode'] : null;
if ($dark_mode_cookie) {
    $dark_mode = $dark_mode_cookie;
} else {
    $dark_mode = $is_night_time ? 'dark' : 'light';
}

// 获取分类
$categories = mysqli_query($conn, "SELECT * FROM categories ORDER BY sort_order, name");
$categories_array = [];
if ($categories && mysqli_num_rows($categories) > 0) {
    while ($cat = mysqli_fetch_assoc($categories)) {
        $categories_array[] = $cat;
    }
}

$current_cat = isset($_GET['category']) ? $_GET['category'] : '';

// 查询应用
$sql = "SELECT a.*, u.username as author_name FROM apps a LEFT JOIN users u ON a.user_id = u.id WHERE a.status = 1"; 

if ($current_cat) {
    $current_cat_safe = mysqli_real_escape_string($conn, $current_cat);
    $sql .= " AND a.category = '$current_cat_safe'";
}
$sql .= " ORDER BY a.is_sticky DESC, a.sticky_time DESC, a.sort_order ASC, a.update_time DESC";
$apps = mysqli_query($conn, $sql);

// 时间格式化函数
function time_ago($timestamp) {
    $time_ago = strtotime($timestamp);
    $current_time = time();
    $time_difference = $current_time - $time_ago;
    $seconds = $time_difference;
    
    $minutes = round($seconds / 60);
    $hours = round($seconds / 3600);
    $days = round($seconds / 86400);
    $weeks = round($seconds / 604800);
    $months = round($seconds / 2629440);
    $years = round($seconds / 31553280);
    if ($seconds <= 60) return "刚刚";
    else if ($minutes <= 60) return $minutes . "分钟前";
    else if ($hours <= 24) return $hours . "小时前";
    else if ($days <= 7) return $days . "天前";
    else if ($weeks <= 4.3) return $weeks . "周前";
    else if ($months <= 12) return $months . "个月前";
    else return $years . "年前";
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>WC软件 - 精品软件推荐</title>
    <style>
        /* 全局重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
        }

        body {
            background: #f5f7fa;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            padding-bottom: 70px;
            width: 100%;
            overflow-x: hidden;
            transition: background-color 0.3s ease, color 0.3s ease;
        }

        /* 深色模式样式 */
        body.dark-mode {
            background: #1a1a1a;
            color: #e0e0e0;
        }

        body.dark-mode .header {
            background: #2d2d2d;
            box-shadow: 0 2px 10px rgba(0,0,0,0.3);
            border-bottom: 1px solid #404040;
        }

        body.dark-mode .title {
            background: linear-gradient(135deg, #66b0ff, #66d9ff);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        body.dark-mode .search {
            background: #3d3d3d;
            color: #fff;
            border: 1px solid #505050;
        }

        body.dark-mode .search:focus {
            background: #4d4d4d;
            border-color: #66b0ff;
        }

        body.dark-mode .search::placeholder {
            color: #999;
        }

        body.dark-mode .category-item {
            background: #3d3d3d;
            color: #ccc;
            border: 1px solid #505050;
        }

        body.dark-mode .category-item:hover,
        body.dark-mode .category-item:active {
            background: #4d4d4d;
        }

        body.dark-mode .category-item.active {
            background: #007bff;
            color: white;
            border-color: #007bff;
        }

        body.dark-mode .item {
            background: #2d2d2d;
            border: 1px solid #404040;
            box-shadow: 0 2px 12px rgba(0,0,0,0.2);
        }

        body.dark-mode .item.sticky {
            background: #332222;
            border-left: 3px solid #ff6666;
        }

        body.dark-mode .name {
            color: #fff;
        }

        body.dark-mode .time-tag {
            color: #999;
        }

        body.dark-mode .category-tag {
            background: rgba(102, 176, 255, 0.2);
            color: #66b0ff;
        }

        body.dark-mode .empty-state {
            background: #2d2d2d;
            color: #ccc;
            border: 1px solid #404040;
        }

        body.dark-mode .empty-state a {
            color: #66b0ff;
        }

        body.dark-mode .bottom-nav {
            background: #2d2d2d;
            border-top: 1px solid #404040;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.2);
        }

        body.dark-mode .nav-item {
            color: #888;
        }

        body.dark-mode .nav-item.active {
            color: #fff;
        }

        body.dark-mode .header-icons button,
        body.dark-mode .header-right button {
            color: #aaa;
            background: #3d3d3d;
            border: 1px solid #505050;
        }

        body.dark-mode .header-icons button:hover,
        body.dark-mode .header-right button:hover {
            color: #fff;
            background: #4d4d4d;
        }

        body.dark-mode .close-modal {
            background: #333;
            color: white;
            border: 1px solid #505050;
        }

        body.dark-mode .toast-message {
            background: rgba(50, 50, 50, 0.95);
            color: #fff;
            border: 1px solid #606060;
        }

        body.dark-mode .back-to-top {
            background: #007bff;
            box-shadow: 0 4px 15px rgba(0,123,255,0.2);
        }

        body.dark-mode .download-count-badge {
            background: rgba(255, 107, 107, 0.8);
        }

        /* 顶部搜索栏 */
        .header {
            background: #fff;
            padding: 12px 16px;
            position: sticky;
            top: 0;
            z-index: 100;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            transition: background-color 0.3s ease, box-shadow 0.3s ease;
        }

        .header-top {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 12px;
            position: relative;
        }

        .title {
            font-size: 22px;
            font-weight: bold;
            color: #333;
            background: linear-gradient(135deg, #007bff, #00d4ff);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
        }

        .header-icons {
            display: flex;
            gap: 8px;
            align-items: center;
            z-index: 1;
        }

        .header-right {
            display: flex;
            gap: 8px;
            align-items: center;
            z-index: 1;
        }

        .header-icons button,
        .header-right button {
            background: #f0f2f5;
            border: none;
            font-size: 16px;
            cursor: pointer;
            padding: 6px;
            color: #666;
            transition: all 0.3s;
            border-radius: 8px;
            width: 32px;
            height: 32px;
            display: flex;
            align-items: center;
            justify-content: center;
            border: 1px solid transparent;
        }

        .header-icons button:hover,
        .header-right button:hover {
            background: #e4e6e9;
            color: #333;
            transform: scale(0.95);
        }

        .search-box {
            position: relative;
            margin-bottom: 10px;
        }

        .search {
            width: 100%;
            height: 40px;
            border-radius: 20px;
            border: none;
            background: #f0f2f5;
            padding: 0 16px 0 40px;
            font-size: 14px;
            outline: none;
            transition: all 0.3s;
        }

        .search:focus {
            background: #fff;
            box-shadow: 0 0 0 2px rgba(0,123,255,0.2);
        }

        .search-box::before {
            content: "🔍";
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #999;
            font-size: 14px;
            z-index: 1;
        }

        /* 分类导航 */
        .category-nav {
            display: flex;
            flex-wrap: nowrap;
            overflow-x: auto;
            gap: 6px;
            padding: 3px 0;
            -webkit-overflow-scrolling: touch;
            scrollbar-width: none;
            margin-top: 3px;
            scroll-behavior: smooth;
        }
        
        .category-nav::-webkit-scrollbar {
            display: none;
        }
        
        .category-item {
            padding: 4px 12px;
            background: #f0f2f5;
            border-radius: 20px;
            font-size: 13px;
            color: #666;
            white-space: nowrap;
            text-decoration: none;
            transition: all 0.3s;
            font-weight: 500;
            border: 1px solid transparent;
            position: relative;
        }
        
        .category-item:hover,
        .category-item:active {
            background: #e4e6e9;
        }
        
        .category-item.active {
            background: #007bff;
            color: white;
            box-shadow: 0 4px 10px rgba(0,123,255,0.3);
        }

        /* 软件列表区域 */
        .app-list {
            padding: 12px;
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin-bottom: 20px;
        }

        /* 单个软件卡片 */
        .item {
            background: #fff;
            border-radius: 16px;
            padding: 16px;
            display: flex;
            align-items: center;
            box-shadow: 0 2px 12px rgba(0,0,0,0.04);
            transition: all 0.3s ease;
            border: 1px solid rgba(0,0,0,0.02);
            position: relative;
            overflow: visible;
            animation: fadeIn 0.5s ease;
            animation-fill-mode: both;
        }

        .item:active {
            transform: scale(0.99);
            box-shadow: 0 4px 15px rgba(0,123,255,0.1);
        }

        /* 标签 - 免费、置顶、官方、精品 */
        .status-tag {
            position: absolute;
            top: 0;
            left: 0;
            width: 44px;
            height: 44px;
            overflow: hidden;
            z-index: 10;
            border-top-left-radius: 16px;
        }
        
        /* 免费角标：绿色 */
        .status-tag.free::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #4caf50 0%, #4caf50 50%, transparent 50%, transparent 100%);
            z-index: 1;
        }
        
        /* 置顶角标：红色 */
        .status-tag.sticky::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #ff4444 0%, #ff4444 50%, transparent 50%, transparent 100%);
            z-index: 1;
        }
        
        /* 官方角标：紫色 */
        .status-tag.official::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #9c27b0 0%, #9c27b0 50%, transparent 50%, transparent 100%);
            z-index: 1;
        }
        
        /* 精品角标：黄色 */
        .status-tag.paid::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, #ffc107 0%, #ffc107 50%, transparent 50%, transparent 100%);
            z-index: 1;
        }
        
        .status-tag span {
            position: absolute;
            top: 6px;
            left: 4px;
            color: white;
            font-size: 10px;
            font-weight: bold;
            transform: rotate(-45deg);
            z-index: 2;
            text-shadow: 0 1px 2px rgba(0,0,0,0.2);
            letter-spacing: 0.3px;
        }

        /* 图标容器 */
        .icon-container {
            width: 70px;
            height: 70px;
            margin-right: 12px;
            flex-shrink: 0;
            cursor: pointer;
            position: relative;
        }

        /* 图标样式 */
        .item img {
            width: 70px;
            height: 70px;
            border-radius: 16px;
            object-fit: cover;
            background: linear-gradient(135deg, #f5f7fa, #e4e6e9);
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
            border: 2px solid #fff;
            transition: transform 0.3s;
        }

        .item img:active {
            transform: scale(0.95);
        }

        /* 中间信息区域 */
        .info {
            flex: 1;
            min-width: 0;
            margin-left: 8px;
        }

        /* 元信息行：只显示小马币 */
        .meta-top {
            display: flex;
            align-items: center;
            gap: 4px;
            margin-bottom: 4px;
            flex-wrap: nowrap;
            overflow: hidden;
            white-space: nowrap;
            font-size: 10px;
        }
        
        /* 小马币标签：纯紫色背景，黑色文字 */
        .coin-badge { 
            background: #ffff00; 
            color: #000000; 
            padding: 1px 4px; 
            border-radius: 3px; 
            font-weight: bold; 
            display: inline-flex; 
            align-items: center; 
            gap: 1px; 
            border: 1px solid #7b1fa2; 
            flex-shrink: 0;
        }

        .name-row {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            gap: 6px;
            margin-bottom: 6px;
        }

        .name {
            font-size: 16px;
            font-weight: 600;
            color: #333;
            line-height: 1.3;
            transition: color 0.3s ease;
        }

        .download-count-badge {
            background: rgba(255, 107, 107, 0.9);
            color: white;
            font-size: 9px;
            padding: 2px 6px;
            border-radius: 10px;
            white-space: nowrap;
            font-weight: 500;
            display: inline-block;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            border: 1px solid rgba(255,255,255,0.3);
            transition: background 0.3s ease;
        }

        .meta {
            display: flex;
            align-items: center;
            gap: 6px;
            flex-wrap: wrap;
        }

        .category-tag {
            display: inline-block;
            font-size: 10px;
            color: #007bff;
            background: rgba(0, 123, 255, 0.1);
            padding: 2px 8px;
            border-radius: 20px;
            font-weight: 500;
            letter-spacing: 0.3px;
            transition: all 0.3s ease;
        }
        
        .time-tag {
            font-size: 10px;
            color: #999;
            display: flex;
            align-items: center;
            gap: 2px;
            transition: color 0.3s ease;
        }
        
        .time-tag::before {
            content: '🕒';
            font-size: 9px;
            opacity: 0.7;
        }

        /* 下载按钮基础样式 */
        .down {
            color: white;
            padding: 8px 16px;
            border-radius: 30px;
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
            flex-shrink: 0;
            margin-left: 8px;
            transition: all 0.3s;
            border: none;
            position: relative;
            overflow: hidden;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
        }
        
        /* 免费按钮：黄色 */
        .down.free-btn {
            background: linear-gradient(135deg, #FFD700, #FFA500);
            box-shadow: 0 4px 8px rgba(255, 165, 0, 0.3);
        }
        
        /* 官方按钮：红色 */
        .down.official-btn {
            background: linear-gradient(135deg, #ff4444, #d63031);
            box-shadow: 0 4px 8px rgba(255, 68, 68, 0.3);
        }
        
        /* 置顶按钮：紫色 */
        .down.sticky-btn {
            background: linear-gradient(135deg, #9c27b0, #7b1fa2);
            box-shadow: 0 4px 8px rgba(156, 39, 176, 0.3);
        }
        
        /* 精品按钮：绿色 */
        .down.paid-btn {
            background: linear-gradient(135deg, #4caf50, #388e3c);
            box-shadow: 0 4px 8px rgba(76, 175, 80, 0.3);
        }
        
        .down:active {
            transform: scale(0.95);
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #999;
            background: #fff;
            border-radius: 20px;
            margin: 20px;
            transition: all 0.3s ease;
        }
        
        .empty-state p {
            margin-top: 15px;
            font-size: 16px;
        }
        
        .empty-state::before {
            content: '📦';
            font-size: 50px;
            opacity: 0.5;
            display: block;
        }

        .empty-state a {
            color: #007bff;
            text-decoration: none;
            font-weight: bold;
        }
        
        .back-to-top {
            position: fixed;
            bottom: 80px;
            right: 20px;
            width: 40px;
            height: 40px;
            background: #007bff;
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            box-shadow: 0 4px 12px rgba(0,123,255,0.4);
            opacity: 0;
            transition: opacity 0.3s, background 0.3s;
            z-index: 99;
            font-size: 18px;
        }
        
        .back-to-top.visible {
            opacity: 1;
        }
        
        .category-stats {
            margin-top: 6px;
            font-size: 11px;
            color: #999;
            display: flex;
            align-items: center;
            gap: 5px;
            transition: color 0.3s ease;
        }
        
        .category-stats span {
            color: #007bff;
            font-weight: 600;
            margin-left: 5px;
            transition: color 0.3s ease;
        }
        
        /* 底部导航栏 */
        .bottom-nav {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 60px;
            background: #fff;
            display: flex;
            justify-content: space-around; 
            align-items: center;
            box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
            z-index: 1000;
            padding: 0 10px;
            border-top: 1px solid #eee;
            transition: all 0.3s ease;
        }
        
        .nav-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            color: #888;
            font-size: 12px;
            transition: all 0.3s;
            padding: 8px 12px;
            min-width: 50px;
        }
        
        .nav-item.active {
            color: #333;
            font-weight: 600;
        }
        
        .nav-item .nav-icon {
            font-size: 22px;
            margin-bottom: 2px;
        }
        
        .nav-item span {
            font-size: 11px;
        }

        .nav-post-btn {
            width: 56px;
            height: 56px;
            background: linear-gradient(135deg, #ff4444, #d63031);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            text-decoration: none;
            color: white;
            font-size: 32px;
            font-weight: 300;
            box-shadow: 0 4px 12px rgba(255, 68, 68, 0.4);
            margin-top: -30px; 
            border: 4px solid #fff;
            transition: all 0.3s;
            z-index: 1001;
        }
        
        .nav-post-btn:active {
            transform: scale(0.9);
            box-shadow: 0 2px 8px rgba(255, 68, 68, 0.4);
        }

        body.dark-mode .nav-post-btn {
            border-color: #1a1a1a;
        }
        
        .toast-message {
            position: fixed;
            bottom: 80px;
            left: 50%;
            transform: translateX(-50%) translateY(100px);
            background: rgba(0,0,0,0.8);
            color: white;
            padding: 10px 20px;
            border-radius: 30px;
            font-size: 14px;
            z-index: 2000;
            opacity: 0;
            transition: all 0.3s;
            white-space: nowrap;
        }
        
        .toast-message.show {
            transform: translateX(-50%) translateY(0);
            opacity: 1;
        }
        
        .image-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.9);
            z-index: 2000;
            justify-content: center;
            align-items: center;
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        
        .image-modal.active {
            display: flex;
            opacity: 1;
        }
        
        .modal-content {
            position: relative;
            max-width: 90%;
            max-height: 90%;
            animation: zoomIn 0.3s ease;
        }
        
        .modal-content img {
            width: 100%;
            height: auto;
            max-height: 80vh;
            object-fit: contain;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            background: #fff;
        }
        
        .close-modal {
            position: absolute;
            top: -40px;
            right: -40px;
            width: 40px;
            height: 40px;
            background: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            font-weight: bold;
            color: #333;
            cursor: pointer;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            transition: all 0.3s;
            border: none;
            z-index: 2001;
        }
        
        .close-modal:hover {
            transform: rotate(90deg);
            background: #ff4444;
            color: white;
        }
        
        @keyframes zoomIn {
            from {
                transform: scale(0.8);
                opacity: 0;
            }
            to {
                transform: scale(1);
                opacity: 1;
            }
        }
        
        .image-info {
            position: absolute;
            bottom: -30px;
            left: 0;
            width: 100%;
            text-align: center;
            color: white;
            font-size: 14px;
            text-shadow: 0 2px 5px rgba(0,0,0,0.5);
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .item.sticky {
            background: #fff9f9;
        }
        
        body.dark-mode .item.sticky {
            background: #332222;
            border-left: 3px solid #ff6666;
        }
        
        @media (max-width: 768px) {
            .close-modal {
                top: -50px;
                right: 0;
            }
        }
        
        @media (max-width: 380px) {
            .item {
                padding: 12px;
            }
            .icon-container, .item img {
                width: 60px;
                height: 60px;
            }
            .down {
                padding: 6px 12px;
                font-size: 12px;
            }
        }
    </style>
</head>
<body class="<?= $dark_mode == 'dark' ? 'dark-mode' : '' ?>">

<!-- 图片放大模态框 -->
<div class="image-modal" id="imageModal" onclick="closeModal()">
    <div class="modal-content" onclick="event.stopPropagation()">
        <button class="close-modal" onclick="closeModal()">×</button>
        <img id="modalImage" src="" alt="软件图标">
        <div class="image-info" id="modalImageInfo"></div>
    </div>
</div>

<!-- 提示框 -->
<div class="toast-message" id="toastMessage">复制成功</div>

<div class="header">
    <div class="header-top">
        <div class="header-icons">
            <button onclick="window.open('https://qm.qq.com/q/wPuAnMT9de', '_blank')" title="QQ群">🐧</button>
            <button onclick="window.open('https://link3.cc/wczs', '_blank')" title="礼物">🎁</button>
        </div>
        
        <div class="title">WC软件</div>
        
        <div class="header-right">
            <button onclick="shareUrl()" title="分享网址">🔗</button>
            <button onclick="toggleDarkMode()" title="切换深色模式" id="darkModeBtn">🌙</button>
        </div>
    </div>
    <div class="search-box">
        <input class="search" placeholder="搜索软件名称..." id="searchInput">
    </div>
    
    <div class="category-nav" id="categoryNav">
        <a href="index.php" class="category-item <?= !$current_cat ? 'active' : '' ?>" data-category="">
            全部
            <?php
            $total = mysqli_query($conn, "SELECT COUNT(*) as count FROM apps WHERE status = 1");
            $total_count = $total ? mysqli_fetch_assoc($total)['count'] : 0;
            ?>
            <span style="margin-left: 4px; font-size: 10px; background: rgba(0,0,0,0.05); padding: 2px 5px; border-radius: 20px;"><?= $total_count ?></span>
        </a>
        <?php foreach($categories_array as $cat): 
            $cat_count = mysqli_query($conn, "SELECT COUNT(*) as count FROM apps WHERE status = 1 AND category = '".mysqli_real_escape_string($conn, $cat['name'])."'");
            $count = $cat_count ? mysqli_fetch_assoc($cat_count)['count'] : 0;
        ?>
        <a href="?category=<?= urlencode($cat['name']) ?>" class="category-item <?= $current_cat == $cat['name'] ? 'active' : '' ?>" data-category="<?= htmlspecialchars($cat['name']) ?>">
            <?= htmlspecialchars($cat['name']) ?>
            <?php if($count > 0): ?>
            <span style="margin-left: 4px; font-size: 10px; background: rgba(0,0,0,0.05); padding: 2px 5px; border-radius: 20px;"><?= $count ?></span>
            <?php endif; ?>
        </a>
        <?php endforeach; ?>
    </div>
    
    <?php if($current_cat): ?>
    <div class="category-stats">
        📌 当前分类：<strong><?= htmlspecialchars($current_cat) ?></strong>
        <span>共 <?= mysqli_num_rows($apps) ?> 个应用</span>
    </div>
    <?php endif; ?>
</div>

<div class="app-list" id="appList">
    <?php if($apps && mysqli_num_rows($apps) > 0): ?>
        <?php 
        $delay = 0;
        while($a = mysqli_fetch_assoc($apps)): 
            $display_downloads = intval($a['download_count'] ?? 0) + intval($a['virtual_downloads'] ?? 0);
            $is_sticky = intval($a['is_sticky'] ?? 0);
            $is_official = intval($a['is_official'] ?? 0);
            $coin_price = intval($a['coin_price'] ?? 0);
            
            // 决定角标内容和下载按钮样式
            $tag_class = 'free';
            $tag_text = '免费';
            $btn_class = 'free-btn'; // 默认免费(黄色)
            
            // 优先级：置顶 > 官方 > 精品 > 免费
            if ($is_sticky) {
                $tag_class = 'sticky';
                $tag_text = '置顶';
                $btn_class = 'sticky-btn'; // 置顶(紫色)
            } elseif ($is_official) {
                $tag_class = 'official';
                $tag_text = '官方';
                $btn_class = 'official-btn'; // 官方(红色)
            } elseif ($coin_price > 0) {
                $tag_class = 'paid';
                $tag_text = '精品';
                $btn_class = 'paid-btn'; // 精品(绿色)
            }
        ?>
        <div class="item <?= $is_sticky ? 'sticky' : '' ?>" data-name="<?= htmlspecialchars($a['name']) ?>" data-category="<?= htmlspecialchars($a['category']) ?>" style="animation-delay: <?= $delay * 0.05 ?>s;">
            <?php $delay++; ?>
            
            <div class="status-tag <?= $tag_class ?>">
                <span><?= $tag_text ?></span>
            </div>
            
            <div class="icon-container" onclick="openModal('<?= htmlspecialchars($a['image']) ?>', '<?= htmlspecialchars($a['name']) ?>')">
                <img src="<?= htmlspecialchars($a['image']) ?>" alt="<?= htmlspecialchars($a['name']) ?>" onerror="this.src='https://via.placeholder.com/70?text=No+Image'">
            </div>
            
            <div class="info">
                <!-- 小马币标签：紫色背景，黑色文字，无图标 -->
                <div class="meta-top">
                    <?php if($coin_price > 0): ?>
                    <span class="coin-badge"><?= $coin_price ?> 小马币</span>
                    <?php endif; ?>
                </div>
                
                <div class="name-row">
                    <span class="name"><?= htmlspecialchars($a['name']) ?></span>
                    <span class="download-count-badge">下载<?= number_format($display_downloads) ?>次</span>
                </div>
                <div class="meta">
                    <span class="category-tag"><?= htmlspecialchars($a['category']) ?></span>
                    <span class="time-tag"><?= time_ago($a['created_at'] ?? $a['update_time']) ?></span>
                </div>
            </div>
            
            <a href="yl.php?id=<?= $a['id'] ?>" class="down <?= $btn_class ?>">
                下载
            </a>
        </div>
        <?php endwhile; ?>
    <?php else: ?>
        <div class="empty-state">
            <p>😴 暂无软件</p>
            <p style="font-size: 14px; margin-top: 10px;">快去<a href="/tg.php">投稿</a>吧！</p>
        </div>
    <?php endif; ?>
</div>

<a href="#" class="back-to-top" id="backToTop">↑</a>

<div class="bottom-nav">
    <a href="index.php" class="nav-item active">
        <span class="nav-icon">🏠</span>
        <span>首页</span>
    </a>
    <a href="fx.php" class="nav-item">
        <span class="nav-icon">🔍</span>
        <span>发现</span>
    </a>
    
    <a href="tg.php" class="nav-post-btn" title="投稿">+</a>
    
    <a href="gd.php" class="nav-item">
        <span class="nav-icon">📱</span>
        <span>更多</span>
    </a>
    <a href="user/home.php" class="nav-item">
        <span class="nav-icon">👤</span>
        <span>我的</span>
    </a>
</div>

<script>
    const currentPage = window.location.pathname.split('/').pop() || 'index.php';
    document.querySelectorAll('.nav-item').forEach(item => {
        if (item.getAttribute('href') === currentPage) item.classList.add('active');
        else item.classList.remove('active');
    });
    
    function scrollToActiveCategory() {
        const activeCategory = document.querySelector('.category-item.active');
        const categoryNav = document.getElementById('categoryNav');
        if (activeCategory && categoryNav) {
            const scrollTo = activeCategory.offsetLeft - (categoryNav.offsetWidth / 2) + (activeCategory.offsetWidth / 2);
            categoryNav.scrollTo({ left: scrollTo, behavior: 'smooth' });
        }
    }
    window.addEventListener('load', scrollToActiveCategory);
    
    const searchInput = document.getElementById('searchInput');
    const appItems = document.querySelectorAll('.item');
    
    searchInput.addEventListener('input', function(e) {
        let keyword = e.target.value.toLowerCase().trim();
        appItems.forEach(item => {
            let name = item.dataset.name.toLowerCase();
            let category = item.dataset.category.toLowerCase();
            item.style.display = (name.includes(keyword) || category.includes(keyword)) ? 'flex' : 'none';
        });
    });
    
    const backToTop = document.getElementById('backToTop');
    window.addEventListener('scroll', function() {
        backToTop.classList.toggle('visible', window.scrollY > 300);
    });
    backToTop.addEventListener('click', function(e) {
        e.preventDefault();
        window.scrollTo({ top: 0, behavior: 'smooth' });
    });
    
    const modal = document.getElementById('imageModal');
    const modalImage = document.getElementById('modalImage');
    const modalInfo = document.getElementById('modalImageInfo');
    
    function openModal(imageSrc, appName) {
        modalImage.src = imageSrc;
        modalInfo.textContent = appName;
        modal.classList.add('active');
        document.body.style.overflow = 'hidden';
    }
    
    function closeModal() {
        modal.classList.remove('active');
        document.body.style.overflow = '';
    }
    
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape' && modal.classList.contains('active')) closeModal();
    });
    
    function shareUrl() {
        const url = 'rj.99520.xyz';
        if (navigator.clipboard && navigator.clipboard.writeText) {
            navigator.clipboard.writeText(url).then(() => showToast('复制成功：' + url));
        } else {
            const textarea = document.createElement('textarea');
            textarea.value = url;
            textarea.style.position = 'fixed';
            textarea.style.opacity = '0';
            document.body.appendChild(textarea);
            textarea.select();
            document.execCommand('copy');
            document.body.removeChild(textarea);
            showToast('复制成功：' + url);
        }
    }
    
    function showToast(message) {
        const toast = document.getElementById('toastMessage');
        toast.textContent = message;
        toast.classList.add('show');
        setTimeout(() => toast.classList.remove('show'), 2000);
    }
    
    function toggleDarkMode() {
        const body = document.body;
        const darkModeBtn = document.getElementById('darkModeBtn');
        const isDark = body.classList.toggle('dark-mode');
        darkModeBtn.textContent = isDark ? '☀️' : '🌙';
        document.cookie = `dark_mode=${isDark ? 'dark' : 'light'}; path=/; max-age=${60*60*24*365}`;
        
        document.querySelectorAll('.item').forEach(item => {
            if (item.classList.contains('sticky')) {
                item.style.backgroundColor = isDark ? '#332222' : '#fff9f9';
            } else {
                item.style.backgroundColor = isDark ? '#2d2d2d' : '#fff';
            }
        });
    }
    
    document.getElementById('darkModeBtn').textContent = document.body.classList.contains('dark-mode') ? '☀️' : '🌙';
</script>
</body>
</html>
