* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    height: 100vh;
    padding: 10px;
    touch-action: manipulation;
    margin: 0;
    overflow: hidden;
    box-sizing: border-box;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
}

.header {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    padding: 12px 15px;
    border-radius: 12px;
    box-shadow: 0 2px 15px rgba(0, 0, 0, 0.08);
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 99;
}

.settings-btn {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: all 0.2s ease;
    color: #667eea;
}

.settings-btn:hover {
    background: rgba(102, 126, 234, 0.1);
    transform: rotate(15deg);
}

.settings-btn:active {
    transform: rotate(15deg) scale(0.95);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
}

.modal-header h2 {
    margin: 0;
    color: #667eea;
    font-size: 24px;
}

.close-btn {
    background: none;
    border: none;
    font-size: 28px;
    cursor: pointer;
    color: #999;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.close-btn:hover {
    background: #f5f5f5;
    color: #667eea;
}

.settings-modal {
    max-width: 90%;
    width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    padding: 20px 30px;
}

/* 控制面板在modal里不需要额外的margin */
.settings-modal .controls {
    background: none;
    padding: 0;
    box-shadow: none;
    margin: 0;
}

.header h1 {
    color: #333;
    font-size: 24px;
}

.stats {
    display: flex;
    gap: 20px;
    align-items: center;
}

.stat-item {
    text-align: center;
    padding: 8px 12px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 8px;
}

.stat-label {
    display: block;
    color: #667eea;
    font-size: 11px;
    font-weight: 500;
    margin-bottom: 2px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-value {
    font-size: 18px;
    font-weight: bold;
    color: #667eea;
    min-width: 40px;
    display: inline-block;
}

.win-message {
    font-size: 18px;
    font-weight: bold;
    color: #28a745;
    opacity: 0;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(2.5);
    transition: all 0.8s cubic-bezier(0.4, 0, 0.2, 1);
    pointer-events: none;
    white-space: nowrap;
    z-index: 100;
}

.win-message.show {
    opacity: 1;
    transform: translate(-50%, -50%) scale(2.5);
    /* animation: pulse 1s ease-in-out infinite; */
}

.win-message.animate-to-header {
    opacity: 1;
    position: static;
    transform: none;
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.header-buttons {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-direction: column;
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
}

.hidden {
    display: none !important;
}

.original-image-link {
    color: #667eea;
    cursor: pointer;
    font-weight: 500;
    transition: color 0.2s;
}

.original-image-link:hover {
    color: #764ba2;
    text-decoration: underline;
}

.win-modal-center {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    pointer-events: none;
}

.win-modal-center.hidden {
    display: none !important;
}

.win-modal-center-content-wrapper {
    transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
    transform: translate(0, 0) scale(1);
}

.win-modal-center-content {
    font-size: 36px;
    font-weight: bold;
    color: #28a745;
    animation: bounce 0.5s ease-in-out infinite alternate;
    pointer-events: auto;
}

@keyframes bounce {
    0% { transform: scale(1); }
    100% { transform: scale(1.1); }
}

.controls {
    background: white;
    padding: 15px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    margin-top: 15px;
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    align-items: center;
}

.control-group {
    display: flex;
    gap: 10px;
    align-items: center;
}

.control-group.full-width {
    width: 100%;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
}

.image-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 10px;
    width: 100%;
}

.gallery-image {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 3px solid transparent;
}

.gallery-image:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.gallery-image.selected {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
}

.gallery-image:active {
    transform: scale(0.98);
}

.control-group label {
    color: #555;
    font-weight: 500;
    font-size: 14px;
}

.control-group select {
    padding: 10px 15px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    background: white;
}

.control-group select:focus {
    outline: none;
    border-color: #667eea;
}

.btn {
    padding: 12px 20px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

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

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:active {
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
}

.btn {
    background: #f0f0f0;
    color: #333;
}

.btn:active {
    background: #e0e0e0;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 14px;
}

.checkbox-label input {
    width: 22px;
    height: 22px;
    cursor: pointer;
}

.game-area {
    display: flex;
    gap: 20px;
    align-items: flex-start;
    flex: 1;
    justify-content: center;
    align-items: center;
}

.puzzle-board {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    position: relative;
}

.puzzle-piece {
    position: absolute;
    background-size: cover;
    cursor: pointer;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    border-radius: 8px;
    z-index: 1;
    box-sizing: border-box;
    border: 2px solid transparent;
    /* 硬件加速，提高移动端性能 */
    will-change: left, top, transform;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

.puzzle-piece.correct {
    border-radius: 0;
    z-index: 2;
    box-shadow: 0 0 0 1px rgba(102, 126, 234, 0.3);
}

.puzzle-piece.merged {
    border-radius: 8px;
    z-index: 3;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.5), 0 2px 8px rgba(102, 126, 234, 0.2);
}

.puzzle-piece.merged.top-left {
    border-top-left-radius: 8px;
}

.puzzle-piece.merged.top-right {
    border-top-right-radius: 8px;
}

.puzzle-piece.merged.bottom-left {
    border-bottom-left-radius: 8px;
}

.puzzle-piece.merged.bottom-right {
    border-bottom-right-radius: 8px;
}

.puzzle-piece.shaped-cell {
    border-radius: 0;
    box-shadow: 0 0 0 1px rgba(102, 126, 234, 0.25);
}

.puzzle-piece.shaped-cell.correct {
    box-shadow: 0 0 0 1px rgba(76, 175, 80, 0.25);
}

.puzzle-piece.shaped-cell.selected {
    box-shadow: 0 0 0 2px #667eea, 0 0 12px rgba(102, 126, 234, 0.5);
    transform: scale(1.03);
    z-index: 10 !important;
}

.puzzle-piece.shaped-cell.dragging {
    opacity: 0.85;
    transform: scale(1.03);
    z-index: 20 !important;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.puzzle-piece.dragging {
    opacity: 0.85;
    transform: scale(1.03);
    z-index: 100;
    cursor: grabbing;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.puzzle-piece.selected {
    z-index: 50;
    box-shadow: 0 0 0 3px #ff6b6b, 0 0 20px rgba(255, 107, 107, 0.5);
    transform: scale(1.02);
}

.puzzle-piece:active {
    transform: scale(0.98);
}

.puzzle-piece.hint {
    box-shadow: 0 0 0 3px #ff6b6b, 0 0 20px rgba(255, 107, 107, 0.5);
}

.puzzle-reference {
    background: white;
    padding: 12px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    display: none;
}

.puzzle-reference.show {
    display: block;
}

.puzzle-reference h3 {
    margin-bottom: 10px;
    color: #555;
    font-size: 14px;
    text-align: center;
}

.puzzle-reference img {
    max-width: 200px;
    border-radius: 6px;
}

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

.modal.show {
    display: flex;
}

.modal-content {
    background: white;
    padding: 30px 25px;
    border-radius: 16px;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    max-width: 90%;
    width: 450px;
}

.modal-content h2 {
    color: #667eea;
    margin-bottom: 20px;
    font-size: 24px;
}

.win-image-container {
    margin-bottom: 20px;
}

.win-image-container img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.modal-stats {
    margin-bottom: 25px;
}

.modal-stats p {
    font-size: 16px;
    color: #555;
    margin: 8px 0;
}

.modal-stats span {
    font-weight: bold;
    color: #667eea;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.modal-buttons .btn {
    flex: 1;
}

@media (max-width: 900px) {
    .game-area {
        flex-direction: column;
        align-items: center;
        width: 100%;
        padding: 0;
        flex: 1;
    }
    
    .controls {
        flex-direction: column;
        align-items: stretch;
    }
    
    .control-group {
        justify-content: center;
        flex-wrap: wrap;
    }
}

@media (max-width: 600px) {
    body {
        padding: 8px;
    }
    
    .container {
        padding: 0;
        width: 100%;
        max-width: 100%;
    }
    
    .header {
        flex-direction: row;
        padding: 8px 10px;
        margin-bottom: 10px;
    }
    
    .game-area {
        padding: 0;
    }
    
    .stats {
        gap: 12px;
    }
    
    .stat-item {
        padding: 6px 10px;
    }
    
    .stat-label {
        font-size: 10px;
    }
    
    .stat-value {
        font-size: 16px;
        min-width: 35px;
    }
    
    /* 关卡统计项移动端适配 */
    #currentLevel {
        min-width: 30px;
    }
    
    .settings-btn {
        font-size: 20px;
        padding: 6px;
    }
    
    .settings-modal {
        width: 95% !important;
        max-width: 95% !important;
        padding: 15px !important;
        max-height: 85vh !important;
    }
    
    .modal-content {
        padding: 15px !important;
        margin: 10px !important;
    }
    
    .modal-header h2 {
        font-size: 18px !important;
    }
    
    .control-group {
        width: 100% !important;
        justify-content: center !important;
        flex-direction: column !important;
        align-items: stretch !important;
    }
    
    .control-group label {
        margin-bottom: 5px;
    }
    
    .image-gallery {
        grid-template-columns: repeat(auto-fill, minmax(55px, 1fr)) !important;
        gap: 6px !important;
    }
    
    .btn {
        padding: 10px 14px !important;
        font-size: 13px !important;
        width: 100%;
    }
    
    .puzzle-reference img {
        max-width: 150px;
    }
    
    /* 拼图板将由 JavaScript 精确控制尺寸 */
    .puzzle-board {
        /* 移除固定CSS尺寸，完全由JavaScript控制 */
    }
}

@media (max-width: 400px) {
    .header h1 {
        font-size: 18px;
    }
    
    .stat-value {
        font-size: 16px;
    }
    
    .controls {
        padding: 10px;
    }
    
    .btn {
        padding: 8px 14px;
        font-size: 12px;
    }
}

.gallery-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 10px;
}

.storage-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.storage-info span {
    font-size: 13px;
    color: #666;
    font-weight: 500;
}

.storage-info span.warning {
    color: #ff6b6b;
    font-weight: bold;
}

.btn-danger {
    background: #ff6b6b !important;
    color: white !important;
    border-color: #ff6b6b !important;
}

.btn-danger:hover {
    background: #ff5252 !important;
}

.gallery-item {
    position: relative;
}

.gallery-item img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 3px solid transparent;
}

.gallery-item img:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.gallery-item img.selected {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3);
}

.delete-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #ff6b6b;
    color: white;
    border: none;
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.2s ease;
    z-index: 10;
}

.gallery-item:hover .delete-btn {
    opacity: 1;
}

.delete-btn:hover {
    background: #ff5252;
    transform: scale(1.1);
}

.empty-gallery {
    text-align: center;
    padding: 30px;
    color: #999;
    font-size: 14px;
    word-break: keep-all;
}

.custom-gallery .gallery-item {
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.reference-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 10px;
}

.reference-header h3 {
    margin: 0;
    font-size: 16px;
    color: #333;
}

.reference-close {
    width: 28px;
    height: 28px;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.puzzle-reference img {
    max-width: 200px;
    cursor: zoom-in;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.puzzle-reference img:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.win-image-container {
    position: relative;
    cursor: zoom-in;
}

.win-image-container img {
    max-width: 300px;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.win-image-container:hover img {
    transform: scale(1.05);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.win-image-container::after {
    content: '🔍 点击查看大图';
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 5px 12px;
    border-radius: 15px;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.win-image-container:hover::after {
    opacity: 1;
}

.fullscreen-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    overflow: hidden;
}

.fullscreen-modal.show {
    display: flex;
}

.fullscreen-content {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.fullscreen-close {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 10001;
    width: 40px;
    height: 40px;
    font-size: 28px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
}

.fullscreen-close:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

#fullscreenImage {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    transition: transform 0.1s ease;
    cursor: grab;
    user-select: none;
    -webkit-user-drag: none;
}

#fullscreenImage:active {
    cursor: grabbing;
}

.fullscreen-hint {
    position: fixed;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    text-align: center;
    pointer-events: none;
}

@media (max-width: 600px) {
    .fullscreen-hint {
        font-size: 12px;
        bottom: 20px;
    }
    
    .fullscreen-close {
        top: 15px;
        right: 15px;
        width: 36px;
        height: 36px;
        font-size: 24px;
    }
}

/* 通关后显示的难度标签 */
.difficulty-label {
    position: absolute;
    bottom: 5px;
    right: 5px;
    font-size: 10px;
    color: rgba(255, 255, 255, 0.8);
    background: rgba(0, 0, 0, 0.5);
    padding: 2px 6px;
    border-radius: 3px;
    pointer-events: none;
    z-index: 100;
    font-weight: bold;
    letter-spacing: 0.5px;
}
