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

:root {
    --bg-dark: #1a1a2e;
    --bg-mid: #16213e;
    --bg-accent: #0f3460;
    --pink: #e94560;
    --cyan: #00d2ff;
    --green: #16c79a;
    --yellow: #f5c542;
    --orange: #ff6b35;
    --purple: #a855f7;
    --text: #e8e8e8;
    --text-dim: #8899aa;
    --card-bg: rgba(255, 255, 255, 0.06);
    --card-border: rgba(255, 255, 255, 0.1);
    --radius: 16px;
    --top-bar-height: 60px;
}

html, body {
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
    background: linear-gradient(135deg, var(--bg-dark) 0%, var(--bg-mid) 50%, var(--bg-accent) 100%);
    color: var(--text);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* ===================== TOP BAR ===================== */
.top-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--top-bar-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    background: rgba(15, 52, 96, 0.85);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    z-index: 100;
    border-bottom: 1px solid var(--card-border);
}

.top-bar .back-btn {
    background: none;
    border: none;
    color: var(--cyan);
    font-size: 18px;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    transition: background 0.2s;
    font-weight: 600;
}

.top-bar .back-btn:hover {
    background: rgba(0, 210, 255, 0.15);
}

.top-bar .title {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.top-bar .info {
    font-size: 16px;
    font-weight: 600;
    color: var(--yellow);
    min-width: 80px;
    text-align: right;
}

/* ===================== SCREENS ===================== */
.screen {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
    transition: opacity 0.3s, transform 0.3s;
    opacity: 0;
    pointer-events: none;
    transform: scale(0.97);
}

.screen.active {
    opacity: 1;
    pointer-events: auto;
    transform: scale(1);
}

/* ===================== MENU SCREEN ===================== */
#menu-screen {
    position: fixed;
    inset: 0;
    align-items: center;
    padding: 24px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    padding-top: 60px;
    padding-bottom: 80px;
}

.menu-header {
    text-align: center;
    margin-bottom: 36px;
}

.menu-header h1 {
    font-size: 36px;
    font-weight: 800;
    background: linear-gradient(135deg, var(--cyan), var(--pink));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 8px;
}

.menu-header p {
    color: var(--text-dim);
    font-size: 16px;
}

.mode-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
    max-width: 520px;
    width: 100%;
}

.mode-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 28px 20px;
    text-align: center;
    cursor: pointer;
    transition: transform 0.25s, box-shadow 0.25s, border-color 0.25s;
    position: relative;
    overflow: hidden;
}

.mode-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    border-radius: var(--radius) var(--radius) 0 0;
}

.mode-card:nth-child(1)::before { background: var(--pink); }
.mode-card:nth-child(2)::before { background: var(--cyan); }
.mode-card:nth-child(3)::before { background: var(--green); }
.mode-card:nth-child(4)::before { background: var(--purple); }
.mode-card:nth-child(5)::before { background: var(--orange); }

.mode-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
    border-color: rgba(255, 255, 255, 0.2);
}

.mode-card:active {
    transform: translateY(-1px) scale(0.98);
}

.mode-card .icon {
    font-size: 40px;
    margin-bottom: 12px;
    display: block;
}

.mode-card .mode-name {
    font-size: 18px;
    font-weight: 700;
    margin-bottom: 6px;
}

.mode-card .mode-desc {
    font-size: 13px;
    color: var(--text-dim);
    line-height: 1.5;
}

.mode-card .high-score {
    margin-top: 10px;
    font-size: 12px;
    color: var(--yellow);
    font-weight: 600;
}

/* Settings button on menu */
.settings-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    color: var(--text-dim);
    font-size: 22px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s, color 0.2s;
    z-index: 50;
}

.settings-toggle:hover {
    transform: rotate(60deg);
    color: var(--text);
}

/* ===================== SETTINGS PANEL ===================== */
.settings-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.settings-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.settings-panel {
    background: var(--bg-mid);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 32px;
    max-width: 400px;
    width: 90%;
    transform: translateY(20px);
    transition: transform 0.3s;
}

.settings-overlay.active .settings-panel {
    transform: translateY(0);
}

.settings-panel h2 {
    font-size: 22px;
    margin-bottom: 24px;
    text-align: center;
}

.setting-group {
    margin-bottom: 20px;
}

.setting-group label {
    display: block;
    font-size: 14px;
    color: var(--text-dim);
    margin-bottom: 8px;
    font-weight: 600;
}

.setting-options {
    display: flex;
    gap: 8px;
}

.setting-options button {
    flex: 1;
    padding: 10px 8px;
    border-radius: 10px;
    border: 1px solid var(--card-border);
    background: var(--card-bg);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.2s;
}

.setting-options button.active {
    background: var(--bg-accent);
    border-color: var(--cyan);
    color: var(--cyan);
}

.setting-options button:hover:not(.active) {
    border-color: rgba(255, 255, 255, 0.25);
}

.settings-close {
    margin-top: 24px;
    width: 100%;
    padding: 12px;
    border-radius: 10px;
    border: none;
    background: var(--cyan);
    color: var(--bg-dark);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: opacity 0.2s;
}

.settings-close:hover {
    opacity: 0.85;
}

/* Slider in settings */
.slider-row {
    display: flex;
    align-items: center;
    gap: 12px;
}

.slider-row input[type="range"] {
    flex: 1;
    -webkit-appearance: none;
    appearance: none;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.15);
    outline: none;
}

.slider-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: var(--cyan);
    cursor: pointer;
}

.slider-row .slider-val {
    min-width: 24px;
    text-align: center;
    font-weight: 700;
    color: var(--cyan);
}

/* ===================== GAME SCREEN ===================== */
#game-screen {
    padding-top: var(--top-bar-height);
}

.game-area {
    flex: 1;
    position: relative;
    overflow: hidden;
}

/* ===================== DOTS ===================== */
.dot {
    position: absolute;
    border-radius: 50%;
    cursor: pointer;
    transform: scale(0);
    animation: dotAppear 0.2s ease-out forwards;
    transition: transform 0.1s;
    box-shadow: 0 0 24px 4px rgba(255, 255, 255, 0.2), inset 0 -4px 12px rgba(0, 0, 0, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.3);
    z-index: 10;
}

.dot:hover {
    transform: scale(1.12) !important;
    box-shadow: 0 0 36px 8px rgba(255, 255, 255, 0.35), inset 0 -4px 12px rgba(0, 0, 0, 0.2);
}

.dot:active {
    transform: scale(0.95) !important;
}

.dot.pop {
    animation: dotPop 0.25s ease-out forwards;
}

.dot.memory-show {
    animation: dotAppear 0.3s ease-out forwards;
}

.dot.memory-hide {
    animation: dotFadeOut 0.3s ease-out forwards;
}

.dot.correct-click {
    animation: correctPulse 0.4s ease-out forwards;
}

.dot.wrong-click {
    animation: wrongShake 0.4s ease-out forwards;
}

/* Memory mode: sequence number indicator */
.dot .seq-num {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    font-weight: 800;
    color: rgba(255, 255, 255, 0.9);
    text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
    pointer-events: none;
}

/* Color memory: gray overlay */
.dot.grayed {
    background: radial-gradient(circle at 35% 35%, #888, #555) !important;
    box-shadow: 0 0 12px rgba(85, 85, 85, 0.4), inset 0 -4px 12px rgba(0, 0, 0, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.15) !important;
}

@keyframes dotAppear {
    0% { transform: scale(0); opacity: 0; }
    60% { transform: scale(1.1); }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes dotPop {
    0% { transform: scale(1); opacity: 1; }
    40% { transform: scale(1.3); opacity: 0.6; }
    100% { transform: scale(0); opacity: 0; }
}

@keyframes dotFadeOut {
    0% { transform: scale(1); opacity: 1; }
    100% { transform: scale(0.8); opacity: 0; }
}

@keyframes correctPulse {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(22, 199, 154, 0.6); }
    50% { transform: scale(1.2); box-shadow: 0 0 0 15px rgba(22, 199, 154, 0); }
    100% { transform: scale(0); opacity: 0; }
}

@keyframes wrongShake {
    0% { transform: translateX(0); }
    20% { transform: translateX(-8px); }
    40% { transform: translateX(8px); }
    60% { transform: translateX(-5px); }
    80% { transform: translateX(5px); }
    100% { transform: translateX(0); opacity: 0.3; }
}

/* ===================== COUNTDOWN OVERLAY ===================== */
.countdown-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    z-index: 150;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.countdown-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.countdown-num {
    font-size: 120px;
    font-weight: 900;
    color: var(--cyan);
    text-shadow: 0 0 40px rgba(0, 210, 255, 0.5);
    animation: countPulse 0.8s ease-out;
}

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

/* ===================== RESULTS SCREEN ===================== */
#result-screen {
    justify-content: center;
    align-items: center;
    padding: 24px;
}

.result-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius);
    padding: 40px 32px;
    max-width: 420px;
    width: 100%;
    text-align: center;
}

.result-card h2 {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 8px;
    background: linear-gradient(135deg, var(--cyan), var(--green));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.result-card .result-subtitle {
    color: var(--text-dim);
    font-size: 15px;
    margin-bottom: 28px;
}

.stat-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 32px;
}

.stat-item {
    background: rgba(255, 255, 255, 0.04);
    border-radius: 12px;
    padding: 16px 12px;
}

.stat-item .stat-value {
    font-size: 28px;
    font-weight: 800;
    color: var(--cyan);
    margin-bottom: 4px;
}

.stat-item .stat-label {
    font-size: 13px;
    color: var(--text-dim);
}

.stat-item.highlight .stat-value {
    color: var(--yellow);
}

.stat-item.full-width {
    grid-column: 1 / -1;
}

.result-buttons {
    display: flex;
    gap: 12px;
}

.result-buttons button {
    flex: 1;
    padding: 14px 20px;
    border-radius: 12px;
    border: none;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: transform 0.2s, opacity 0.2s;
}

.result-buttons button:hover {
    transform: translateY(-2px);
}

.result-buttons button:active {
    transform: scale(0.97);
}

.btn-primary {
    background: linear-gradient(135deg, var(--cyan), var(--green));
    color: var(--bg-dark);
}

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

/* New high score badge */
.new-record {
    display: inline-block;
    background: linear-gradient(135deg, var(--yellow), var(--orange));
    color: var(--bg-dark);
    padding: 4px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 16px;
    animation: recordBounce 0.6s ease-out;
}

@keyframes recordBounce {
    0% { transform: scale(0); }
    60% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

/* ===================== COLOR MEMORY TARGET ===================== */
.color-target {
    position: fixed;
    top: calc(var(--top-bar-height) + 16px);
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 52, 96, 0.9);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 10px 24px;
    display: flex;
    align-items: center;
    gap: 10px;
    z-index: 50;
    font-size: 16px;
    font-weight: 600;
    opacity: 0;
    transition: opacity 0.3s;
}

.color-target.visible {
    opacity: 1;
}

.color-target .color-swatch {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

/* ===================== MEMORY PHASE INDICATOR ===================== */
.phase-indicator {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15, 52, 96, 0.9);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    padding: 10px 24px;
    font-size: 15px;
    font-weight: 600;
    color: var(--yellow);
    z-index: 50;
    opacity: 0;
    transition: opacity 0.3s;
}

.phase-indicator.visible {
    opacity: 1;
}

/* Timer bar */
.timer-bar {
    position: fixed;
    top: var(--top-bar-height);
    left: 0;
    height: 4px;
    background: linear-gradient(90deg, var(--cyan), var(--green));
    z-index: 99;
    transition: width 0.1s linear;
    border-radius: 0 2px 2px 0;
}

/* ===================== TRAFFIC LIGHT MODE ===================== */
.light-container {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 24px;
}

.light-block {
    width: min(70vw, 320px);
    height: min(70vw, 320px);
    border-radius: 32px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: transform 0.1s;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
    border: 3px solid rgba(255, 255, 255, 0.15);
}

.light-block:active {
    transform: scale(0.96);
}

.light-block.red {
    background: radial-gradient(circle at 40% 38%, #ff4444, #aa0000);
    box-shadow: 0 0 60px rgba(255, 0, 0, 0.4), 0 8px 40px rgba(0, 0, 0, 0.5);
}

.light-block.green {
    background: radial-gradient(circle at 40% 38%, #44ff44, #00aa00);
    box-shadow: 0 0 60px rgba(0, 255, 0, 0.4), 0 8px 40px rgba(0, 0, 0, 0.5);
}

.light-block.orange {
    background: radial-gradient(circle at 40% 38%, #ffaa33, #cc6600);
    box-shadow: 0 0 60px rgba(255, 160, 0, 0.4), 0 8px 40px rgba(0, 0, 0, 0.5);
}

.light-block .block-text {
    font-size: 28px;
    font-weight: 800;
    color: rgba(255, 255, 255, 0.95);
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
    pointer-events: none;
}

.light-block .block-ms {
    font-size: 64px;
    font-weight: 900;
    color: #fff;
    pointer-events: none;
    text-shadow: 0 2px 16px rgba(0, 0, 0, 0.5);
}

.light-info {
    font-size: 15px;
    color: var(--text-dim);
    text-align: center;
    pointer-events: none;
}

.light-round-info {
    font-size: 14px;
    color: var(--text-dim);
    pointer-events: none;
}

/* ===================== RESPONSIVE ===================== */
@media (max-width: 480px) {
    .mode-grid {
        grid-template-columns: 1fr;
        max-width: 320px;
    }

    .mode-card {
        padding: 20px 16px;
    }

    .menu-header h1 {
        font-size: 28px;
    }

    .result-card {
        padding: 28px 20px;
    }

    .stat-grid {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }

    .stat-item .stat-value {
        font-size: 22px;
    }

    .settings-panel {
        padding: 24px;
    }
}

@media (min-width: 768px) {
    .mode-card .icon {
        font-size: 48px;
    }

    .mode-card .mode-name {
        font-size: 20px;
    }
}
