        :root {
            --primary-color: #4e54c8;
            --secondary-color: #8f94fb;
            --accent-color: #ff7b54;
            --text-color: #333;
            --light-bg: #f9f9f9;
            --success-color: #4CAF50;
            --dark-color: #2c3e50;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Nunito', sans-serif;
        }

        body {
            margin: 0;
            overflow: hidden;
            background-color: #222;
            transition: background-color 0.5s;
        }     
       
       /* Loading screen */
        #loading-screen {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 1000;
            color: white;
        }

        #loading-text {
            font-size: 2.5em;
            margin-bottom: 30px;
            font-family: 'Fredoka One', cursive;
            text-shadow: 3px 3px 0 rgba(0,0,0,0.2);
        }

        #loading-bar-container {
            width: 80%;
            max-width: 500px;
            height: 30px;
            background: rgba(255, 255, 255, 0.3);
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }

        #loading-bar {
            height: 100%;
            width: 0%;
            background: linear-gradient(45deg, var(--accent-color), #ff9a76);
            border-radius: 15px;
            transition: width 0.5s;
        }

        /* Animations */
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 10; }
        }

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

        .fade-in {
            animation: fadeIn 0.5s forwards;
        }

        .slide-in {
            animation: slideIn 0.5s forwards;
        }

        /* Character Selection */
        #character-select {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: linear-gradient(135deg, #fff, #f0f0f0);
            padding: clamp(1rem, 3vw, 2.5rem);
            border-radius: 20px;
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
            text-align: center;
            width: clamp(300px, 80vw, 700px);
            max-width: 800px;
            z-index: 200;
            border: 10px solid var(--accent-color);
        }

        #character-select h1 {
            color: var(--text-color);
            font-family: 'Fredoka One', cursive;
            margin-bottom: clamp(1rem, 2.5vw, 2rem);
            font-size: clamp(1.8rem, 5vw, 2.8rem);
            text-shadow: 3px 3px 0 rgba(0,0,0,0.1);
        }

        #character-options {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
            justify-content: center;
            flex-wrap: wrap;
            gap: clamp(0.5rem, 2vw, 1.5rem);
            margin-bottom: clamp(1.5rem, 3vw, 2.5rem);
        }

        .character-option {
            background: rgba(255,255,255,0.8);
            border-radius: 15px;
            padding: clamp(0.75rem, 2vw, 1.5rem);
            cursor: pointer;
            transition: all 0.3s;
            border: 5px solid transparent;
            width: 150px;
            height: 200px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }

        .character-option:hover {
            transform: translateY(-10px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.2);
        }

        .character-option.selected {
            border-color: var(--primary-color);
            background: rgba(142, 157, 255, 0.2);
        }

        .character-icon {
            font-size: clamp(2.5rem, 8vw, 4.5rem);
            margin-bottom: clamp(0.5rem, 2vw, 1rem);
        }

        .character-name {
            font-weight: bold;
            font-size: clamp(1rem, 3.5vw, 1.4rem);
        }

        #start-game-btn {
            background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
            font-size: clamp(1rem, 4vw, 1.6rem);
            padding: clamp(0.75rem, 2.5vw, 1.25rem) clamp(1.5rem, 5vw, 3rem);
            border-radius: 50px;
            color: white;
            font-weight: bold;
            border: none;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
        }

        #start-game-btn:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.3);
        }

        #game-canvas {
            display: block;
        }

        /* Game UI Elements */
        #game-ui {
            position: absolute;
            top: 20px;
            left: 20px;
            color: white;
            font-size: clamp(1rem, 2.5vw, 1.6rem);
            padding: clamp(0.5rem, 1vw, 1rem);
            background: rgba(0, 0, 0, 0.6);
            border-radius: clamp(0.5rem, 1vw, 1rem);
            z-index: 10;
            display: flex;
            flex-direction: column;
            gap: 0.5rem;
            align-items: flex-start;
        }

        .ui-element {
            background: rgba(0, 0, 0, 0.7);
            padding: 10px 15px;
            border-radius: 50px;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        .ui-element i {
            margin-right: 10px;
            color: var(--accent-color);
        }
        @media (max-width: 480px) {
            #game-ui {
                flex-direction: row;
                top: 1rem;
                bottom: auto;
                left: 50%;
                transform: translateX(-50%);
                /* 讓 UI 元件等寬並水平分佈 */
                justify-content: space-between;
                width: 90%;
                padding: clamp(0.5rem, 2vw, 1rem);
            }
            .ui-element {
                flex: 1;
                text-align: center;
                font-size: clamp(0.9rem, 3vw, 1.2rem);
            }
            }

            /* 3. 超小或長條形螢幕，再作微調 */
            @media (max-aspect-ratio: 1/2) {
            #game-ui {
                gap: 0.25rem;
                padding: 0.5rem;
            }
            .ui-element i {
                margin-right: 0.3rem;
            }
            }

        #mission-marker {
            position: absolute;
            top: -50px;
            left: 0;
            width: 40px;
            height: 40px;
            pointer-events: none;
            z-index: 5;
            animation: bounce 0.8s infinite alternate;
        }

        @keyframes bounce {
            from { transform: translateY(0); }
            to { transform: translateY(-10px); }
        }

        /* Instructions */
        #instructions {
            position: absolute;
            bottom: 20px;
            right: 20px;
            background: rgba(0, 0, 0, 0.8);
            padding: 15px;
            border-radius: 15px;
            color: white;
            font-size: 1.1em;
            max-width: 300px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            z-index: 10;
            border-left: 5px solid var(--accent-color);
        }

        /* Game Interface Styling */
        .game-interface {
            width: clamp(90%, 80vw, 500px);
            padding: clamp(1rem, 3vw, 2rem);
            border-radius: clamp(0.75rem, 2vw, 1.5rem);
            display: flex;
            flex-direction: column;
            gap: clamp(0.75rem, 2vw, 1.5rem);
            position: absolute;
            background: linear-gradient(135deg, #fff, #f5f5f5);
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: transform 0.4s cubic-bezier(0.68, -0.55, 0.27, 1.55);
            text-align: center;
        }

        #task-interface {
            font-size: clamp(1.5rem, 4vw, 2.2rem);
            margin-bottom: clamp(0.5rem, 2vw, 1rem);
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) scale(0.95);
            background: linear-gradient(135deg, #fff, #f5f5f5);
            padding: 30px;
            display: none;
            max-width: 550px;
            border: 8px solid var(--accent-color);
            z-index: 100;
        }

        #task-interface.active {
            transform: translate(-50%, -50%) scale(1);
        }

        #digit-grid {
            font-size: clamp(2rem, 6vw, 3.5rem);
            padding: clamp(0.5rem, 2vw, 1rem);
            letter-spacing: clamp(4px, 2vw, 8px);
            margin: 25px 0;
            color: var(--dark-color);
            font-weight: bold;
            text-shadow: 2px 2px 0 rgba(0,0,0,0.1);
            background: rgba(255,255,255,0.8);
            border-radius: 15px;
            box-shadow: inset 0 0 15px rgba(0,0,0,0.1);
            white-space: nowrap;
            overflow-x: auto;
        }

        #timer {
             font-size: clamp(1.5rem, 4vw, 2.2rem);
            color: var(--accent-color);
            margin: 15px 0;
            font-weight: bold;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        #timer i {
            margin-right: clamp(0.3rem, 1vw, 0.6rem);
            animation: pulse 1s infinite;
        }

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

        .btn {
            font-size: clamp(1rem, 3vw, 1.5rem);
            padding: clamp(0.75rem, 2vw, 1.25rem) clamp(1.5rem, 4vw, 2.5rem);
            border-radius: clamp(0.75rem, 2vw, 1.25rem);
            margin: 10px;
            border: none;
            cursor: pointer;
            background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
            color: white;
            transition: all 0.3s;
            font-weight: bold;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            position: relative;
            overflow: hidden;
        }

        .btn:before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: 0.5s;
        }

        .btn:hover:before {
            left: 100%;
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }

        .btn:disabled {
            background: #cccccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        #message {
            margin-top: 20px;
            color: var(--text-color);
            background: rgba(255,255,255,0.7);
            border-radius: 10px;
            font-weight: bold;
            font-size: clamp(1rem, 3.5vw, 1.6rem);
            padding: clamp(0.5rem, 1.5vw, 1rem);
        }
        @media (max-width: 480px) {
            .game-interface {
                width: 90%;
                left: 50%;
                transform: translateX(-50%) scale(0.95);
            }
        }

        /* Results Page Styling */
        #results-page {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: linear-gradient(135deg, #fff, #f0f0f0);
            padding: clamp(1.5rem, 4vw, 3rem);
            border-radius: clamp(1rem, 2vw, 2rem);
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
            text-align: center;
            display: none;
            max-width: 700px;
            width: clamp(90%, 80vw, 600px);
            z-index: 100;
            border: 10px solid var(--accent-color);
        }

        #results-page h1 {
            color: var(--primary-color);
            font-family: 'Fredoka One', cursive;
            font-size: clamp(1.8rem, 5vw, 2.8rem);
            margin-bottom: clamp(1rem, 3vw, 2rem);
            text-shadow: 3px 3px 0 rgba(0,0,0,0.1);
        }

        .mission-time {
            display: flex;
            justify-content: space-between;
            margin: 15px 0;
            padding: clamp(1rem, 2.5vw, 2rem);
            background: linear-gradient(45deg, rgba(255,255,255,0.8), rgba(240,240,240,0.8));
            border-radius: clamp(0.75rem, 2vw, 1.5rem);
            font-size: clamp(1rem, 3vw, 1.4rem);
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            transition: transform 0.3s;
            border-left: clamp(0.3rem, 1vw, 0.6rem) solid var(--primary-color);
        }

        .mission-time:hover {
            transform: translateX(clamp(3px, 1vw, 10px));
        }

        .mission-badge {
            font-size: 1.8em;
            margin-right: 10px;
        }

        #total-time {
            font-size: clamp(1.4rem, 4vw, 2rem);
            margin: 30px 0;
            color: var(--dark-color);
            font-weight: bold;
            background: rgba(255,255,255,0.8);
            padding: clamp(0.75rem, 2vw, 1.5rem);
            border-radius: clamp(0.75rem, 2vw, 1.5rem);
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        #play-again {
            background: linear-gradient(45deg, var(--accent-color), #ff9a76);
            padding: clamp(1rem, 3vw, 2rem) clamp(1.5rem, 4vw, 3rem);
            font-size: clamp(1rem, 3.5vw, 1.6rem);
            margin-top: 30px;
            border-radius: clamp(1rem, 2vw, 2rem);
            color: white;
            font-weight: bold;
            border: none;
            cursor: pointer;
            transition: transform 0.3s, box-shadow 0.3s;
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
        }

        #play-again:hover {
            transform: translateY(-clamp(3px, 1vw, 10px));
            box-shadow: 0 15px 30px rgba(0,0,0,0.2);
        }

        @media (max-width: 400px) {
            #results-page {
                width: 95%;
                padding: 1rem;
                gap: 1rem;
            }
            .mission-time {
                flex-direction: column;
                align-items: flex-start;
                gap: 0.5rem;
            }
        }

        /* Mobile Controls */
        #mobile-controls {
            position: fixed;
            bottom: clamp(1rem, 5vw, 2rem);
            right: clamp(1rem, 5vw, 2rem);
            display: flex;
            gap: 40px;
            z-index: 20;
            display: none; /* Hidden by default */
        }

        #d-pad {
            display: grid;
            grid-template-areas: 
                ". up ."
                "left . right"
                ". down .";
            gap: clamp(0.5rem, 2vw, 1rem);
        }

        .dir-btn {
            width: clamp(40px, 12vw, 60px);
            height: clamp(40px, 12vw, 60px);
            background: rgba(0, 0, 0, 0.7);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: clamp(1rem, 4vw, 1.5rem);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            border: clamp(2px, 0.5vw, 3px) solid var(--accent-color);
            user-select: none;
        }

        #up-btn { grid-area: up; }
        #down-btn { grid-area: down; }
        #left-btn { grid-area: left; }
        #right-btn { grid-area: right; }

        .action-btn {
            width: 80px;
            height: 80px;
            background: rgba(255, 123, 84, 0.8);
            border-radius: 50%;
            display: none;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.8em;
            font-weight: bold;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            border: 3px solid white;
            user-select: none;
        }

        /* Show controls only on touch devices */
        @media (pointer: coarse) {
            #mobile-controls {
                display: flex;
            }
            
            #instructions {
                display: none; /* Hide instructions on mobile */
            }
        }
