:root {
    --primary: #9c27b0;
    --primary-dark: #7b1fa2;
    --bg: #f5f5f5;
    --card-bg: #ffffff;
    --text: #333333;
    --text-secondary: #666666;
    --border: #e0e0e0;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

/* Header */
.emulator-header {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #fff;
    padding: 16px 20px;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
}

.emulator-header h1 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 0;
    margin-right: auto;
}

.emulator-tabs {
    display: flex;
    gap: 8px;
}

.emulator-tabs a,
.emulator-tabs button {
    padding: 8px 18px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    text-decoration: none;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.emulator-tabs a:hover,
.emulator-tabs button:hover {
    background: rgba(255, 255, 255, 0.3);
}

.emulator-tabs a.active,
.emulator-tabs button.active {
    background: #fff;
    color: var(--primary);
    border-color: #fff;
}

/* Category selector */
.category-bar {
    margin-left: auto;
    flex-shrink: 0;
}

.category-select {
    padding: 8px 32px 8px 14px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    outline: none;
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%23ffffff'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    min-width: 120px;
    transition: background 0.2s ease;
}

.category-select:hover {
    background: rgba(255, 255, 255, 0.25);
}

.category-select option {
    background: #333;
    color: #fff;
}

/* Global search */
.global-search {
    position: relative;
    display: inline-block;
    margin-left: 12px;
}

.global-search input {
    padding: 8px 32px 8px 14px;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.4);
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 14px;
    outline: none;
    width: 220px;
    transition: all 0.2s ease;
}

.global-search input::placeholder {
    color: rgba(255, 255, 255, 0.7);
}

.global-search input:focus {
    background: rgba(255, 255, 255, 0.25);
    border-color: #fff;
}

.global-search button {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.3);
    color: #fff;
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 0 2px 0;
}

.global-search button:hover {
    background: rgba(255, 255, 255, 0.5);
}

/* Arcade system filter */
.arcade-system-filter {
    display: inline-flex;
    gap: 8px;
    margin-left: 12px;
    align-items: center;
}

.arcade-system-filter a {
    padding: 6px 14px;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    font-size: 13px;
    transition: all 0.2s ease;
    cursor: pointer;
}

.arcade-system-filter a:hover {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.arcade-system-filter a.active {
    background: #fff;
    color: var(--primary);
    border-color: #fff;
    font-weight: 600;
}

/* Search results grouping */
.search-group {
    margin-bottom: 24px;
}

.search-group-title {
    font-size: 16px;
    color: var(--text);
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--border);
    display: flex;
    align-items: center;
    gap: 8px;
}

.search-group-icon {
    font-size: 20px;
}

.search-count {
    font-size: 13px;
    color: var(--text-secondary);
    font-weight: normal;
}

/* Header responsive */
@media (max-width: 768px) {
    .emulator-header {
        flex-wrap: wrap;
    }
    .emulator-header h1 {
        width: 100%;
        margin-bottom: 10px;
    }
    .category-bar {
        margin-left: 0;
        margin-top: 10px;
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
    }
    .category-select {
        flex: 1;
        min-width: 120px;
    }
    .global-search {
        margin-left: 0;
        flex: 1;
        min-width: 160px;
    }
    .global-search input {
        width: 100%;
    }
}

/* Main container */
.emulator-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
}

/* Section title */
.section-title {
    font-size: 18px;
    margin-bottom: 16px;
    color: var(--text);
}

/* Game grid - 矩阵宫格布局 */
.game-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 20px;
}

@media (max-width: 1200px) {
    .game-grid { grid-template-columns: repeat(5, 1fr); }
}

@media (max-width: 992px) {
    .game-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (max-width: 768px) {
    .game-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 14px;
    }
}

@media (max-width: 480px) {
    .game-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }
}

.game-card {
    background: var(--card-bg);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: var(--shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    cursor: pointer;
    text-decoration: none;
    color: var(--text);
    display: flex;
    flex-direction: column;
}

.game-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.game-card-thumb {
    width: 100%;
    aspect-ratio: 1 / 1;
    background: linear-gradient(135deg, #e1bee7, #ce93d8);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 48px;
    color: rgba(255, 255, 255, 0.9);
}

.game-card-thumb.nes {
    background: linear-gradient(135deg, #ef5350, #c62828);
}

.game-card-thumb.gba {
    background: linear-gradient(135deg, #42a5f5, #1565c0);
}

.game-card-thumb.arcade {
    background: linear-gradient(135deg, #ff9800, #e65100);
}

.game-card-thumb.computer {
    background: linear-gradient(135deg, #667eea, #764ba2);
}

.game-card-thumb.md {
    background: linear-gradient(135deg, #00bcd4, #006064);
}

.game-card-thumb.sfc {
    background: linear-gradient(135deg, #7c4dff, #311b92);
}

.game-card-info {
    padding: 12px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.game-card-name {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.4;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-bottom: 6px;
}

.game-card-size {
    font-size: 12px;
    color: var(--text-secondary);
}

/* Player page */
.player-page {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    background: #000;
}

.player-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: #1a1a1a;
    color: #fff;
    flex-shrink: 0;
    gap: 12px;
}

.player-toolbar h2 {
    font-size: 16px;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-right: 12px;
    flex-shrink: 0;
    max-width: 220px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* 街机系统标签 */
.system-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
}

/* BIOS 状态指示 */
.bios-status {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 500;
    margin-right: 8px;
}

.bios-ok {
    background: rgba(76, 175, 80, 0.2);
    color: #4CAF50;
}

.bios-missing {
    background: rgba(244, 67, 54, 0.2);
    color: #F44336;
}

.toolbar-center {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 12px;
    min-width: 0;
}

.toolbar-center select {
    padding: 6px 28px 6px 10px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
    font-size: 13px;
    cursor: pointer;
    outline: none;
    max-width: 100%;
}

.toolbar-center select option {
    background: #333;
    color: #fff;
}

.control-selector {
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: 100%;
}

.control-selector select {
    max-width: 220px;
    flex-shrink: 0;
}

.key-hint {
    color: rgba(255, 255, 255, 0.9);
    font-size: 13px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    padding: 0 12px;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
    white-space: nowrap;
}

.player-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    align-items: center;
}

.btn {
    padding: 8px 12px;
    border-radius: 6px;
    border: none;
    background: var(--primary);
    color: #fff;
    font-size: 13px;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    transition: background 0.2s ease, box-shadow 0.2s ease;
    white-space: nowrap;
}

.btn:hover {
    background: var(--primary-dark);
}

.btn.active {
    background: #ff9800;
    color: #000;
    box-shadow: 0 0 8px rgba(255, 152, 0, 0.5);
}

.btn-secondary {
    background: #424242;
}

.btn-secondary:hover {
    background: #616161;
}

.btn-select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding-right: 26px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%23ffffff'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
    cursor: pointer;
}

.btn-select option {
    background: #333;
    color: #fff;
}

/* Custom key modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
}

.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.modal-box {
    background: #fff;
    border-radius: 12px;
    width: 90%;
    max-width: 480px;
    max-height: 90vh;
    overflow: hidden;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.modal-close {
    background: none;
    border: none;
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
    color: #999;
}

.modal-close:hover {
    color: #333;
}

.modal-body {
    padding: 16px 20px;
    max-height: 60vh;
    overflow-y: auto;
}

.key-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid #f0f0f0;
}

.key-row:last-child {
    border-bottom: none;
}

.key-label {
    flex: 1;
    font-size: 14px;
    color: var(--text);
}

.key-value {
    min-width: 80px;
    padding: 4px 10px;
    background: #f5f5f5;
    border-radius: 4px;
    text-align: center;
    font-size: 13px;
    font-weight: 600;
    color: var(--primary);
    margin-right: 12px;
}

.key-value.capturing {
    background: #fff3e0;
    color: #e65100;
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
    background: #fafafa;
}

.player-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    position: relative;
}

#game {
    width: 1024px;
    height: 768px;
    max-width: 100%;
    max-height: 100%;
    background: #000;
}

/* EmulatorJS overrides for fullscreen feel */
#game iframe,
#game canvas {
    width: 100% !important;
    height: 100% !important;
}

/* Virtual gamepad overrides */
#game .ejs_virtualGamepad_button {
    background: rgba(255, 255, 255, 0.18) !important;
    border: 2px solid rgba(255, 255, 255, 0.35) !important;
    border-radius: 50% !important;
    backdrop-filter: blur(4px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25), inset 0 0 10px rgba(255, 255, 255, 0.1) !important;
    color: rgba(255, 255, 255, 0.95) !important;
    font-weight: 600 !important;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4) !important;
    transition: transform 0.05s ease, background 0.05s ease;
}

#game .ejs_virtualGamepad_button:active,
#game .ejs_virtualGamepad_button_active {
    background: rgba(255, 152, 0, 0.45) !important;
    border-color: rgba(255, 152, 0, 0.7) !important;
    transform: scale(0.94);
}

#game .ejs_virtualGamepad_dpad {
    background: rgba(255, 255, 255, 0.12) !important;
    border: 2px solid rgba(255, 255, 255, 0.25) !important;
    border-radius: 50% !important;
    backdrop-filter: blur(4px);
}

#game .ejs_virtualGamepad_dpad_center {
    background: rgba(255, 255, 255, 0.18) !important;
}

/* Loading / empty states */
.loading,
.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}

.loading::after {
    content: '';
    display: inline-block;
    width: 24px;
    height: 24px;
    margin-left: 10px;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Download page */
.download-page {
    text-align: center;
    padding: 60px 20px;
    color: #fff;
}

.download-icon {
    font-size: 80px;
    margin-bottom: 20px;
}

.download-page h2 {
    font-size: 24px;
    margin-bottom: 16px;
    color: #fff;
}

.download-desc {
    font-size: 16px;
    color: rgba(255, 255, 255, 0.8);
    margin-bottom: 30px;
}

.btn-download {
    background: linear-gradient(135deg, #667eea, #764ba2);
    padding: 14px 32px;
    font-size: 18px;
    border-radius: 30px;
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.btn-download:hover {
    background: linear-gradient(135deg, #764ba2, #667eea);
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.5);
}

.download-info {
    margin-top: 30px;
    padding: 16px 24px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.7);
}

.download-info p {
    margin: 0;
}

/* Upload page */
.upload-form {
    max-width: 600px;
    margin: 0 auto;
    background: var(--card-bg);
    padding: 24px;
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.upload-form .form-group {
    margin-bottom: 16px;
}

.upload-form label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
}

.upload-form input[type="file"],
.upload-form input[type="password"],
.upload-form select {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 14px;
}

.upload-result {
    margin-top: 16px;
    padding: 12px;
    border-radius: 6px;
    display: none;
}

.upload-result.success {
    display: block;
    background: #e8f5e9;
    color: #2e7d32;
}

.upload-result.error {
    display: block;
    background: #ffebee;
    color: #c62828;
}

/* Footer */
.emulator-footer {
    text-align: center;
    padding: 20px;
    color: var(--text-secondary);
    font-size: 12px;
}

/* Player page footer (ICP备案) */
.player-footer {
    text-align: center;
    padding: 12px 20px;
    background: #1a1a2e;
    font-size: 12px;
}

.player-footer a {
    color: rgba(255, 255, 255, 0.5);
    text-decoration: none;
    transition: color 0.2s ease;
}

.player-footer a:hover {
    color: rgba(255, 255, 255, 0.8);
}

/* Responsive */
@media (max-width: 1024px) {
    .player-toolbar {
        flex-wrap: wrap;
        padding: 10px 12px;
    }

    .player-toolbar h2 {
        max-width: 45%;
        font-size: 14px;
    }

    .toolbar-center {
        order: 3;
        width: 100%;
        padding: 8px 0 0 0;
        justify-content: flex-start;
    }

    .control-selector {
        width: 100%;
    }

    .control-selector select {
        max-width: 45%;
    }

    .control-selector .key-hint {
        flex: 1;
        text-align: left;
        white-space: normal;
        padding: 0;
        font-size: 11px;
    }

    .player-actions {
        flex-wrap: wrap;
        justify-content: flex-end;
        max-width: 55%;
    }

    .btn {
        padding: 6px 10px;
        font-size: 12px;
    }
}

@media (max-width: 768px) {
    .emulator-header h1 {
        font-size: 18px;
    }

    .emulator-tabs a,
    .emulator-tabs button {
        padding: 6px 14px;
        font-size: 13px;
    }

    .emulator-container {
        padding: 12px;
    }

    .game-grid {
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
        gap: 10px;
    }

    .game-card-thumb {
        font-size: 36px;
    }

    .game-card-info {
        padding: 8px;
    }

    .game-card-name {
        font-size: 12px;
    }

    .player-toolbar {
        padding: 8px 10px;
    }

    .player-toolbar h2 {
        font-size: 13px;
    }

    .btn {
        padding: 6px 8px;
        font-size: 12px;
    }

    #game {
        max-width: 100%;
    }

    /* Mobile virtual gamepad larger touch targets */
    #game .ejs_virtualGamepad_button {
        min-width: 54px !important;
        min-height: 54px !important;
        font-size: 15px !important;
    }
}

@media (max-width: 480px) {
    .game-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .player-actions {
        gap: 6px;
    }

    .btn {
        padding: 5px 7px;
        font-size: 11px;
    }

    .btn .emoji {
        display: none;
    }
}
