* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4a90e2;
    --secondary-color: #7b68ee;
    --success-color: #51cf66;
    --danger-color: #ff6b6b;
    --bg-color: #f5f7fa;
    --card-bg: #ffffff;
    --text-color: #2c3e50;
    --text-secondary: #7f8c8d;
    --border-color: #e1e8ed;
    --shadow: 0 2px 8px rgba(0,0,0,0.1);
    --shadow-hover: 0 4px 16px rgba(0,0,0,0.15);
}

body.dark-theme {
    --bg-color: #1a1a2e;
    --card-bg: #16213e;
    --text-color: #eaeaea;
    --text-secondary: #a0a0a0;
    --border-color: #2d3748;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    transition: background 0.3s, color 0.3s;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* 允许特定元素选择文本 */
.stat-value,
.timer-display,
.history-item,
.completion-stats {
    -webkit-user-select: text;
    user-select: text;
}

/* 移动设备特定样式 */
body.mobile-device .grid-cell {
    -webkit-tap-highlight-color: rgba(74, 144, 226, 0.3);
}

body.landscape-mode .header {
    padding: 15px 20px;
}

body.landscape-mode .stats-overview {
    grid-template-columns: repeat(3, 1fr);
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
}

/* 页面切换 */
.page {
    display: none;
    animation: fadeIn 0.3s;
}

.page.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 首页样式 */
.header {
    text-align: center;
    margin-bottom: 40px;
    padding: 40px 20px;
}

.header h1 {
    font-size: 2.5rem;
    color: var(--primary-color);
    margin-bottom: 10px;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 1.1rem;
}

.stats-overview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 40px;
}

.stat-card {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    text-align: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover {
    transform: translateY(-5px);
    box-shadow: var(--shadow-hover);
}

.stat-value {
    font-size: 2rem;
    font-weight: bold;
    color: var(--primary-color);
    margin-bottom: 8px;
}

.stat-label {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.difficulty-selection {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    margin-bottom: 30px;
}

.difficulty-selection h2 {
    text-align: center;
    margin-bottom: 25px;
    color: var(--text-color);
}

.difficulty-buttons {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
}

.difficulty-btn {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    border: none;
    padding: 25px 20px;
    border-radius: 10px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.difficulty-btn:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-hover);
}

.difficulty-name {
    font-size: 1.2rem;
    font-weight: bold;
}

.difficulty-desc {
    font-size: 0.9rem;
    opacity: 0.9;
}

.action-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
}

/* 按钮样式 */
.btn {
    padding: 12px 30px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s;
    font-weight: 500;
}

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background: #357abd;
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.btn-secondary {
    background: var(--card-bg);
    color: var(--text-color);
    border: 2px solid var(--border-color);
}

.btn-secondary:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

.btn-danger {
    background: var(--danger-color);
    color: white;
}

.btn-danger:hover {
    background: #e55555;
}

.btn-back {
    background: none;
    border: none;
    color: var(--primary-color);
    font-size: 1rem;
    cursor: pointer;
    padding: 8px 16px;
    border-radius: 6px;
    transition: background 0.2s;
}

.btn-back:hover {
    background: var(--border-color);
}

/* 训练页面 */
.training-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding: 20px;
    background: var(--card-bg);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.timer-display {
    font-size: 2rem;
    font-weight: bold;
    color: var(--primary-color);
    font-family: 'Courier New', monospace;
}

.target-display {
    font-size: 1.2rem;
    color: var(--text-secondary);
}

.target-display span {
    color: var(--success-color);
    font-weight: bold;
    font-size: 1.5rem;
}

.grid-container {
    display: grid;
    gap: 10px;
    max-width: 600px;
    margin: 0 auto 30px;
    padding: 20px;
    background: var(--card-bg);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.grid-cell {
    aspect-ratio: 1;
    background: var(--bg-color);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: bold;
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
}

.grid-cell:hover {
    background: var(--primary-color);
    color: white;
    transform: scale(1.05);
}

.grid-cell.clicked {
    background: var(--success-color);
    color: white;
    border-color: var(--success-color);
    animation: clickEffect 0.2s;
}

@keyframes clickEffect {
    0% { transform: scale(1); }
    50% { transform: scale(0.95); }
    100% { transform: scale(1); }
}

.training-controls {
    text-align: center;
}

/* 统计页面 */
.page-header {
    display: flex;
    align-items: center;
    gap: 20px;
    margin-bottom: 30px;
}

.page-header h2 {
    flex: 1;
    text-align: center;
}

.stats-detail {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.stat-section {
    background: var(--card-bg);
    padding: 25px;
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.stat-section h3 {
    margin-bottom: 20px;
    color: var(--primary-color);
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
}

.stat-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.stat-item .stat-label {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.stat-item .stat-value {
    font-size: 1.8rem;
    font-weight: bold;
    color: var(--primary-color);
}

.best-times {
    display: grid;
    gap: 12px;
}

.best-time-item {
    display: flex;
    justify-content: space-between;
    padding: 12px;
    background: var(--bg-color);
    border-radius: 6px;
}

.history-list {
    max-height: 400px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.history-item {
    padding: 15px;
    background: var(--bg-color);
    border-radius: 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-item .time {
    font-weight: bold;
    color: var(--primary-color);
}

.history-item .date {
    color: var(--text-secondary);
    font-size: 0.85rem;
}

/* 设置页面 */
.settings-content {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 12px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 25px;
}

.setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 0;
    border-bottom: 1px solid var(--border-color);
}

.setting-item:last-child {
    border-bottom: none;
}

.setting-item label {
    font-weight: 500;
}

.setting-item select {
    padding: 8px 15px;
    border: 2px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-color);
    color: var(--text-color);
    cursor: pointer;
}

.setting-item input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
}

/* 弹窗 */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--card-bg);
    padding: 40px;
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
    text-align: center;
    max-width: 400px;
    animation: modalSlideIn 0.3s;
}

@keyframes modalSlideIn {
    from { transform: translateY(-50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.modal-content h2 {
    margin-bottom: 20px;
    color: var(--primary-color);
}

.completion-stats {
    margin: 25px 0;
    font-size: 1.2rem;
}

.completion-stats p {
    margin: 10px 0;
}

.modal-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
    margin-top: 25px;
}

/* 响应式设计 */

/* 小屏幕手机 (≤480px) - 极简模式 */
@media (max-width: 480px) {
    .container {
        padding: 10px;
    }

    .header {
        padding: 20px 10px;
        margin-bottom: 20px;
    }

    .header h1 {
        font-size: 1.5rem;
    }

    .subtitle {
        font-size: 0.9rem;
    }

    /* 首页统计卡片 - 单列 */
    .stats-overview {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .stat-card {
        padding: 20px;
    }

    .stat-value {
        font-size: 1.5rem;
    }

    /* 难度选择 - 2列 */
    .difficulty-buttons {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .difficulty-btn {
        padding: 20px 10px;
    }

    .difficulty-name {
        font-size: 1rem;
    }

    .difficulty-desc {
        font-size: 0.8rem;
    }

    /* 训练页面 - 垂直布局 */
    .training-header {
        flex-direction: column;
        gap: 10px;
        padding: 15px;
    }

    .timer-display {
        font-size: 1.5rem;
    }

    .target-display {
        font-size: 1rem;
    }

    .target-display span {
        font-size: 1.2rem;
    }

    /* 方格 - 优化触摸 */
    .grid-container {
        gap: 8px;
        padding: 15px;
        max-width: 100%;
    }

    .grid-cell {
        font-size: 1.3rem;
        min-height: 50px;
        border-width: 1px;
    }

    /* 按钮 */
    .btn {
        padding: 12px 20px;
        font-size: 0.9rem;
    }

    .action-buttons {
        flex-direction: column;
        gap: 10px;
    }

    .action-buttons .btn {
        width: 100%;
    }

    /* 统计页面 */
    .stat-grid {
        grid-template-columns: 1fr;
    }

    .stat-section {
        padding: 15px;
    }

    /* 弹窗 */
    .modal-content {
        margin: 15px;
        padding: 25px 15px;
        max-width: calc(100% - 30px);
    }

    .modal-content h2 {
        font-size: 1.3rem;
    }

    .completion-stats {
        font-size: 1rem;
    }

    .modal-buttons {
        flex-direction: column;
        width: 100%;
    }

    .modal-buttons .btn {
        width: 100%;
    }
}

/* 中等手机/小平板 (481px-768px) - 简洁模式 */
@media (min-width: 481px) and (max-width: 768px) {
    .header h1 {
        font-size: 2rem;
    }

    .stats-overview {
        grid-template-columns: repeat(2, 1fr);
        gap: 15px;
    }

    .difficulty-buttons {
        grid-template-columns: repeat(2, 1fr);
    }

    .training-header {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 15px;
    }

    .grid-container {
        max-width: 500px;
    }

    .grid-cell {
        font-size: 1.8rem;
        min-height: 60px;
    }

    .modal-content {
        margin: 20px;
        padding: 30px 20px;
    }
}

/* 平板 (769px-1024px) - 平衡模式 */
@media (min-width: 769px) and (max-width: 1024px) {
    .container {
        max-width: 900px;
    }

    .stats-overview {
        grid-template-columns: repeat(3, 1fr);
    }

    .difficulty-buttons {
        grid-template-columns: repeat(4, 1fr);
    }

    .grid-container {
        max-width: 550px;
    }

    .grid-cell {
        font-size: 2rem;
    }
}

/* 小笔记本 (1025px-1440px) - 标准模式 */
@media (min-width: 1025px) and (max-width: 1440px) {
    .container {
        max-width: 1100px;
    }

    .grid-container {
        max-width: 600px;
    }
}

/* 大屏幕 (>1440px) - 丰富模式 */
@media (min-width: 1441px) {
    .container {
        max-width: 1400px;
        display: grid;
        grid-template-columns: 1fr 300px;
        gap: 30px;
    }

    #homePage.active {
        display: grid;
        grid-template-columns: 1fr 300px;
        gap: 30px;
        grid-column: 1 / -1;
    }

    #homePage .header {
        grid-column: 1 / -1;
    }

    #homePage .stats-overview {
        grid-column: 1 / -1;
    }

    .grid-container {
        max-width: 700px;
    }

    .grid-cell {
        font-size: 2.5rem;
    }

    /* 大屏幕额外信息面板 */
    .stats-overview {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* 横屏手机优化 */
@media (max-height: 500px) and (orientation: landscape) {
    .header {
        padding: 15px 10px;
        margin-bottom: 15px;
    }

    .header h1 {
        font-size: 1.3rem;
    }

    .subtitle {
        display: none;
    }

    .stats-overview {
        grid-template-columns: repeat(3, 1fr);
        gap: 10px;
    }

    .stat-card {
        padding: 15px;
    }

    .difficulty-selection {
        padding: 20px;
    }

    .training-header {
        padding: 10px;
    }

    .grid-container {
        padding: 10px;
        max-width: 450px;
    }

    .grid-cell {
        font-size: 1.2rem;
        min-height: 40px;
    }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
    .grid-cell {
        min-height: 60px;
        min-width: 60px;
    }

    .btn {
        min-height: 44px;
        min-width: 44px;
    }

    .difficulty-btn {
        min-height: 80px;
    }

    /* 移除hover效果，使用active */
    .grid-cell:hover {
        background: var(--bg-color);
        color: var(--text-color);
        transform: none;
    }

    .grid-cell:active {
        background: var(--primary-color);
        color: white;
        transform: scale(0.95);
    }

    .btn:hover {
        transform: none;
    }

    .btn:active {
        transform: scale(0.98);
    }
}
