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

body {
    font-family: 'Nunito', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

/* Mobile: prevent page scroll (use viewport units that work on iOS) */
body.mobile-device {
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    overflow: hidden;
}

body.mobile-device {
    height: 100dvh;
    overflow: hidden;
}

body.dark-mode {
    background: radial-gradient(circle at 20% 10%, #1c2a4a 0%, transparent 45%),
        radial-gradient(circle at 80% 20%, #3b1b52 0%, transparent 48%),
        linear-gradient(135deg, #0b1020 0%, #0f172a 55%, #111827 100%);
}

#app {
    width: 100%;
    max-width: 600px;
    min-height: 100vh;
    background: #fff;
    position: relative;
    overflow: hidden;
}

body.dark-mode #app {
    background: #0b1220;
    color: #e9ecef;
}

body.dark-mode #app:before {
    opacity: 0.25;
}

body.dark-mode #app:after {
    opacity: 0.18;
}

body.dark-mode h2 {
    color: #e9ecef;
}

body.dark-mode .leaderboard-item,
body.dark-mode .help-item,
body.dark-mode .final-stat,
body.dark-mode .settings-card,
body.dark-mode .answer-box {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(255, 255, 255, 0.12);
    color: #e9ecef;
}

body.dark-mode .help-item p,
body.dark-mode .final-label,
body.dark-mode .setting-desc {
    color: rgba(233, 236, 239, 0.8);
}

body.dark-mode .question {
    color: #f1f3f5;
    text-shadow: 2px 2px 0 rgba(0,0,0,0.35);
}

body.dark-mode .answer-display {
    color: #f1f3f5;
}

body.dark-mode .answer-display.has-value {
    color: #74c0fc;
}

body.dark-mode .progress-bar {
    background: rgba(255, 255, 255, 0.08);
}

body.dark-mode .config-notice {
    background: rgba(250, 176, 5, 0.12);
    color: #ffe066;
}

body.dark-mode .input-hint {
    color: rgba(233, 236, 239, 0.75);
}

body.dark-mode .plat {
    color: rgba(233, 236, 239, 0.65);
}

body.dark-mode .setting-select {
    background: rgba(0, 0, 0, 0.25);
    border-color: rgba(255, 255, 255, 0.16);
    color: #e9ecef;
}

body.dark-mode .slider {
    background: rgba(255, 255, 255, 0.18);
}

#app:before,
#app:after {
    content: '';
    position: absolute;
    width: 320px;
    height: 320px;
    border-radius: 50%;
    filter: blur(30px);
    opacity: 0.35;
    pointer-events: none;
    z-index: 0;
}

#app:before {
    background: radial-gradient(circle at 30% 30%, #74c0fc 0%, transparent 60%);
    top: -140px;
    left: -120px;
}

#app:after {
    background: radial-gradient(circle at 70% 70%, #ffd43b 0%, transparent 60%);
    bottom: -160px;
    right: -140px;
}

.screen {
    position: relative;
    z-index: 1;
}

/* Screens */
.screen {
    display: none;
    padding: 20px;
    min-height: 100vh;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.screen.active {
    display: flex;
}

/* Logo */
.logo {
    text-align: center;
    margin-bottom: 40px;
    animation: bounce 2s infinite;
}

.math {
    font-family: 'Fredoka One', cursive;
    font-size: 40px;
    color: #ff6b6b;
    text-shadow: 3px 3px 0px #c92a2a;
    display: block;
    letter-spacing: 1px;
}

.blaster {
    font-family: 'Fredoka One', cursive;
    font-size: 56px;
    color: #ffd43b;
    text-shadow: 3px 3px 0px #f08c00;
    display: block;
}

/* Text truncation helpers */
.feedback {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.leaderboard-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stars {
    font-size: 32px;
    margin-top: 10px;
}

/* Buttons */
.btn {
    font-family: 'Fredoka One', cursive;
    font-size: 24px;
    padding: 15px 40px;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.3s ease;
    margin: 10px;
    text-transform: uppercase;
    box-shadow: 0 6px 0 rgba(0,0,0,0.2);
}

.btn:active {
    transform: translateY(6px);
    box-shadow: 0 0 0 rgba(0,0,0,0.2);
}

.btn-play {
    background: linear-gradient(135deg, #51cf66 0%, #40c057 100%);
    color: white;
}

.btn-leaderboard {
    background: linear-gradient(135deg, #ffd43b 0%, #fab005 100%);
    color: #333;
}

.btn-help {
    background: linear-gradient(135deg, #74c0fc 0%, #4dabf7 100%);
    color: white;
}

.btn-back {
    background: #adb5bd;
    color: white;
    font-size: 18px;
    padding: 12px 30px;
}

.quit-btn {
    background: #ff6b6b;
    color: white;
    font-family: 'Fredoka One', cursive;
    font-size: 16px;
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 3px 0 #c92a2a;
    transition: all 0.2s ease;
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 100;
}

.quit-btn:active {
    transform: translateY(3px);
    box-shadow: 0 0 0 #c92a2a;
}

#game-screen {
    position: relative;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Game Header */
.game-header {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    gap: 10px;
    width: 100%;
    margin-bottom: 20px;
    background: linear-gradient(135deg, #74c0fc 0%, #4dabf7 100%);
    padding: 15px;
    border-radius: 20px;
    color: white;
}

.stat {
    text-align: center;
    min-width: 90px;
}

.stat-label {
    font-size: 12px;
    display: block;
    opacity: 0.9;
}

.stat-value {
    font-family: 'Fredoka One', cursive;
    font-size: 28px;
    display: block;
}

/* Progress Bar */
.progress-bar {
    width: 100%;
    height: 20px;
    background: #e9ecef;
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 30px;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #51cf66 0%, #40c057 100%);
    width: 0%;
    transition: width 0.3s ease;
}

/* Question pulse */
.question.pulse {
    animation: questionPulse 0.22s ease-in-out;
}

@keyframes questionPulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.06); }
    100% { transform: scale(1); }
}

/* Question */
.question-container {
    text-align: center;
    margin-bottom: 30px;
}

.question {
    font-family: 'Fredoka One', cursive;
    font-size: 72px;
    color: #495057;
    margin-bottom: 20px;
    text-shadow: 2px 2px 0px #dee2e6;
}

.answer-box {
    background: #f8f9fa;
    border: 4px solid #dee2e6;
    border-radius: 20px;
    padding: 20px;
    min-height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.answer-display {
    font-family: 'Fredoka One', cursive;
    font-size: 56px;
    color: #495057;
}

.answer-display.has-value {
    color: #339af0;
}

/* Numpad */
.numpad {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
}

.numpad-row {
    display: flex;
    justify-content: center;
    gap: 10px;
}

.num-btn {
    width: 80px;
    height: 80px;
    font-family: 'Fredoka One', cursive;
    font-size: 32px;
    border: none;
    border-radius: 20px;
    background: linear-gradient(135deg, #e9ecef 0%, #dee2e6 100%);
    color: #495057;
    cursor: pointer;
    transition: all 0.1s ease;
    box-shadow: 0 4px 0 #adb5bd;
}

.num-btn:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #adb5bd;
}

.btn-clear {
    background: linear-gradient(135deg, #ff6b6b 0%, #fa5252 100%);
    color: white;
    box-shadow: 0 4px 0 #c92a2a;
}

.btn-clear:active {
    box-shadow: 0 0 0 #c92a2a;
}

.btn-submit {
    background: linear-gradient(135deg, #51cf66 0%, #40c057 100%);
    color: white;
    box-shadow: 0 4px 0 #2f9e44;
}

.btn-submit:active {
    box-shadow: 0 0 0 #2f9e44;
}

/* Feedback */
.feedback {
    font-family: 'Fredoka One', cursive;
    font-size: 36px;
    height: 50px;
    text-align: center;
    margin-bottom: 20px;
}

.feedback.correct {
    color: #51cf66;
    animation: pop 0.3s ease;
}

.feedback.wrong {
    color: #ff6b6b;
    animation: shake 0.5s ease;
}

/* Game Over Screen */
#gameover-title {
    font-family: 'Fredoka One', cursive;
    font-size: 36px;
    color: #ffd43b;
    text-align: center;
    margin-bottom: 30px;
    text-shadow: 2px 2px 0px #f08c00;
}

.final-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
    width: 100%;
}

.final-stat {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    padding: 20px;
    border-radius: 20px;
    text-align: center;
}

.final-label {
    font-size: 14px;
    color: #868e96;
    display: block;
    margin-bottom: 5px;
}

.final-value {
    font-family: 'Fredoka One', cursive;
    font-size: 36px;
    color: #495057;
}

.rank-message {
    font-family: 'Fredoka One', cursive;
    font-size: 24px;
    color: #51cf66;
    text-align: center;
    margin-bottom: 30px;
    min-height: 30px;
}

/* Leaderboard */
h2 {
    font-family: 'Fredoka One', cursive;
    font-size: 32px;
    color: #495057;
    text-align: center;
    margin-bottom: 30px;
}

.leaderboard-list {
    width: 100%;
    max-height: 400px;
    overflow-y: auto;
    margin-bottom: 30px;
}

.leaderboard-item {
    display: flex;
    align-items: center;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 15px;
    margin-bottom: 10px;
    transition: transform 0.2s ease;
}

.leaderboard-item:hover {
    transform: scale(1.02);
}

.leaderboard-rank {
    font-family: 'Fredoka One', cursive;
    font-size: 24px;
    width: 50px;
    text-align: center;
}

.leaderboard-rank.gold { color: #ffd700; }
.leaderboard-rank.silver { color: #c0c0c0; }
.leaderboard-rank.bronze { color: #cd7f32; }

.leaderboard-name {
    flex: 1;
    font-weight: 700;
    font-size: 18px;
}

.plat {
    font-size: 12px;
    font-weight: 800;
    color: #868e96;
    margin-left: 6px;
}

.leaderboard-score {
    font-family: 'Fredoka One', cursive;
    font-size: 24px;
    color: #51cf66;
}

.empty-leaderboard {
    text-align: center;
    color: #adb5bd;
    font-size: 18px;
    padding: 40px;
}

.loading {
    text-align: center;
    color: #74c0fc;
    font-size: 18px;
    padding: 40px;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

.config-notice {
    text-align: center;
    color: #fab005;
    font-size: 14px;
    padding: 15px;
    background: #fff9db;
    border-radius: 10px;
    margin-top: 20px;
}

/* Help Screen */
.help-content {
    width: 100%;
    margin-bottom: 30px;
}

.help-item {
    display: flex;
    align-items: center;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 15px;
    margin-bottom: 15px;
}

.help-icon {
    font-size: 32px;
    margin-right: 15px;
}

.help-item p {
    font-size: 16px;
    color: #495057;
    line-height: 1.5;
}

/* Settings */
.settings-card {
    width: 100%;
    max-width: 520px;
    background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
    border: 3px solid #dee2e6;
    border-radius: 20px;
    padding: 18px;
    margin-bottom: 20px;
}

.setting {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 12px 0;
    border-bottom: 2px dashed #dee2e6;
}

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

.setting.small {
    justify-content: center;
}

.setting-title {
    font-family: 'Fredoka One', cursive;
    font-size: 18px;
    color: #495057;
}

.setting-desc {
    font-size: 12px;
    color: #868e96;
    margin-top: 4px;
}

.setting-select {
    width: 220px;
    padding: 12px 14px;
    border-radius: 14px;
    border: 3px solid #dee2e6;
    font-weight: 800;
    font-size: 16px;
    outline: none;
    background: #fff;
}

.setting-select:focus {
    border-color: #74c0fc;
}

.switch {
    position: relative;
    display: inline-block;
    width: 64px;
    height: 34px;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #adb5bd;
    transition: 0.2s;
    border-radius: 999px;
    box-shadow: inset 0 0 0 3px rgba(0,0,0,0.08);
}

.slider:before {
    position: absolute;
    content: "";
    height: 26px;
    width: 26px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: 0.2s;
    border-radius: 50%;
    box-shadow: 0 3px 0 rgba(0,0,0,0.15);
}

.switch input:checked + .slider {
    background: #51cf66;
}

.switch input:checked + .slider:before {
    transform: translateX(30px);
}

/* Name Input */
#player-name {
    width: 100%;
    padding: 20px;
    font-family: 'Fredoka One', cursive;
    font-size: 28px;
    text-align: center;
    border: 4px solid #dee2e6;
    border-radius: 20px;
    margin-bottom: 20px;
    outline: none;
    transition: border-color 0.3s ease;
}

/* Setup Screen name input */
#setup-name {
    width: 100%;
    padding: 16px;
    font-family: 'Fredoka One', cursive;
    font-size: 24px;
    text-align: center;
    border: 4px solid #dee2e6;
    border-radius: 18px;
    margin: 12px 0 20px;
    outline: none;
    transition: border-color 0.3s ease;
}

#setup-name:focus {
    border-color: #74c0fc;
}

/* Leaderboard filters */
.filters {
    width: 100%;
    display: flex;
    gap: 10px;
    justify-content: center;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.filters .setting-select {
    width: 220px;
}

@media (max-width: 420px) {
    .filters .setting-select {
        width: 100%;
    }
}

#player-name:focus {
    border-color: #74c0fc;
}

/* On-screen Keyboard */
.on-screen-keyboard {
    width: 100%;
    margin-bottom: 20px;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin-bottom: 5px;
}

.kb-row button {
    width: 35px;
    height: 45px;
    font-family: 'Fredoka One', cursive;
    font-size: 18px;
    border: none;
    border-radius: 10px;
    background: linear-gradient(135deg, #e9ecef 0%, #dee2e6 100%);
    color: #495057;
    cursor: pointer;
    transition: all 0.1s ease;
    box-shadow: 0 3px 0 #adb5bd;
}

.kb-row button:active {
    transform: translateY(3px);
    box-shadow: 0 0 0 #adb5bd;
}

.btn-space {
    width: 200px !important;
}

.btn-backspace {
    width: 80px !important;
    background: linear-gradient(135deg, #ff6b6b 0%, #fa5252 100%) !important;
    color: white !important;
    box-shadow: 0 3px 0 #c92a2a !important;
}

/* Animations */
@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

@keyframes pop {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-10px); }
    40% { transform: translateX(10px); }
    60% { transform: translateX(-10px); }
    80% { transform: translateX(10px); }
}

/* Confetti */
#confetti-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    overflow: hidden;
    z-index: 10;
}

.confetti {
    position: absolute;
    width: 10px;
    height: 10px;
    animation: confetti-fall 3s ease-out forwards;
}

@keyframes confetti-fall {
    0% {
        transform: translateY(-100px) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translateY(600px) rotate(720deg);
        opacity: 0;
    }
}

/* Input Hints */
.input-hint {
    font-family: 'Nunito', sans-serif;
    font-size: 16px;
    color: #868e96;
    text-align: center;
    margin-bottom: 15px;
    font-weight: 700;
}

.mobile-hint {
    display: none;
}

.desktop-hint {
    display: none;
}

.mobile-device .mobile-hint {
    display: block;
}

.desktop-device .desktop-hint {
    display: block;
}

/* Leaderboard Sections */
.leaderboard-section {
    margin-bottom: 20px;
}

.leaderboard-section h3 {
    font-family: 'Fredoka One', cursive;
    font-size: 20px;
    color: #495057;
    text-align: center;
    margin-bottom: 15px;
    padding: 10px;
    background: #f8f9fa;
    border-radius: 10px;
}

.leaderboard-section.other-device h3 {
    opacity: 0.7;
}

.leaderboard-item.other-device-item {
    opacity: 0.8;
}

/* Mobile Optimizations - No Scroll */
.mobile-device #app {
    padding: 8px;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    overflow: hidden;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    position: fixed;
    inset: 0;
    max-width: none;
}

.mobile-device .screen {
    padding: 8px;
    height: 100vh;
    height: 100svh;
    height: 100dvh;
    overflow: hidden;
    justify-content: flex-start;
}

.mobile-device #game-screen {
    display: grid;
    grid-template-rows: auto auto 1fr auto;
    align-content: stretch;
    justify-content: stretch;
    gap: 6px;
}

.mobile-device #game-screen {
    --np-gap: 4px;
    --np-btn: clamp(40px, 8.5vh, 52px);
}

.mobile-device .game-header {
    padding: 8px 10px;
    margin-bottom: 8px;
    border-radius: 12px;
    flex-wrap: nowrap;
    gap: 6px;
}

.mobile-device .stat-label {
    display: none;
}

.mobile-device #score::before {
    content: '⭐ ';
}


.mobile-device #level::before {
    content: '🎚️ ';
}

.mobile-device #q::before {
    content: '🧩 ';
}

.mobile-device #remain::before {
    content: '⏳ ';
}

.mobile-device #lives::before {
    content: '❤️ ';
}

.mobile-device .stat-label {
    font-size: 9px;
}

.mobile-device .stat-value {
    font-size: 16px;
}

.mobile-device .stat {
    min-width: 0;
    flex: 1;
}

/* Mobile: keep header tiny (Score + Lives only) */
/* hide: Q + Left on mobile */
.mobile-device #game-screen .game-header .stat:nth-child(3),
.mobile-device #game-screen .game-header .stat:nth-child(4) {
    display: none;
}

.mobile-device .progress-bar {
    display: none;
}

/* (Missions/power removed) */

.mobile-device .question-container {
    margin-bottom: 0;
}

.mobile-device .question {
    font-size: 34px;
    margin-bottom: 6px;
}

.mobile-device .answer-box {
    padding: 8px;
    min-height: 48px;
    border-radius: 12px;
    border-width: 3px;
}

.mobile-device .answer-display {
    font-size: 30px;
}

.mobile-device .feedback {
    font-size: 18px;
    height: 22px;
    margin-bottom: 0;
}

@media (max-height: 700px) {
    .mobile-device .feedback {
        display: none;
    }
}

.mobile-device .input-hint {
    display: none;
    margin-bottom: 0;
}

.mobile-device .numpad {
    gap: var(--np-gap);
    margin-bottom: 0;
    margin-top: auto;
    padding-bottom: env(safe-area-inset-bottom);
    width: 100%;
    max-width: 380px;
}

.mobile-device .numpad-row {
    gap: var(--np-gap);
}

.mobile-device .num-btn {
    width: var(--np-btn);
    height: var(--np-btn);
    font-size: clamp(18px, 4.6vw, 22px);
    border-radius: 12px;
}

@media (max-height: 680px) {
    .mobile-device .question {
        font-size: 28px;
    }
    .mobile-device .num-btn {
        width: var(--np-btn);
        height: var(--np-btn);
        font-size: clamp(16px, 4.2vw, 20px);
        border-radius: 10px;
    }
    .mobile-device .answer-display {
        font-size: 26px;
    }
}

@media (max-height: 720px) {
    .mobile-device .num-btn {
        width: var(--np-btn);
        height: var(--np-btn);
        font-size: clamp(16px, 4.4vw, 21px);
    }
    .mobile-device .question {
        font-size: 30px;
    }
}

.mobile-device .numpad-row {
    width: 100%;
}

.mobile-device .num-btn {
    flex: 1;
    max-width: 92px;
}

@media (max-height: 740px) {
    .mobile-device #game-screen {
        --np-gap: 3px;
        --np-btn: clamp(34px, 8vh, 46px);
    }
}

@media (max-height: 680px) {
    .mobile-device #game-screen {
        --np-gap: 3px;
        --np-btn: clamp(32px, 7.5vh, 44px);
    }
}

.mobile-device .btn-clear,
.mobile-device .btn-submit {
    font-size: 20px;
}

.mobile-device .quit-btn {
    width: 28px;
    height: 28px;
    font-size: 12px;
    top: 5px;
    right: 5px;
}

/* Desktop Optimizations */
.desktop-device .numpad {
    transform: scale(0.9);
}

.desktop-device .question {
    font-size: 80px;
}

/* Touch improvements */
@media (hover: none) {
    .num-btn:hover {
        transform: none;
    }
    
    .btn:hover {
        transform: none;
    }
}

/* Responsive */
@media (max-width: 480px) {
    .math { font-size: 32px; }
    .blaster { font-size: 36px; }
    
    .btn {
        font-size: 18px;
        padding: 10px 25px;
        margin: 8px;
    }
    
    .question {
        font-size: 38px;
    }
    
    .num-btn {
        width: 58px;
        height: 58px;
        font-size: 22px;
        border-radius: 10px;
    }
    
    .stat-value {
        font-size: 16px;
    }
    
    .game-header {
        padding: 8px;
        margin-bottom: 5px;
    }
    
    .stat-label {
        font-size: 9px;
    }
    
    .answer-box {
        padding: 8px;
        min-height: 50px;
    }
    
    .answer-display {
        font-size: 32px;
    }
    
    .feedback {
        font-size: 20px;
        height: 25px;
    }
    
    .numpad {
        gap: 5px;
    }
    
    .numpad-row {
        gap: 5px;
    }
    
    .quit-btn {
        width: 26px;
        height: 26px;
        font-size: 11px;
        top: 5px;
        right: 5px;
    }
}
