/* Reset and base styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'SF Pro Rounded', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.app {
    width: 100%;
    max-width: 500px;
    height: 100vh;
    max-height: 900px;
    display: flex;
    flex-direction: column;
    padding: 20px;
    position: relative;
}

/* Screen management */
.screen {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.screen.hidden {
    display: none;
}

/* Stats bar */
.stats-bar {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin-bottom: 10px;
}

.stat-item {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.2);
    padding: 8px 16px;
    border-radius: 20px;
}

.stat-icon {
    font-size: 20px;
}

.stat-num {
    font-size: 18px;
    font-weight: 700;
    color: white;
}

/* Start Screen */
.start-screen {
    justify-content: center;
    align-items: center;
    text-align: center;
    gap: 15px;
}

.title {
    font-size: 48px;
    font-weight: 800;
    color: white;
    text-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    animation: bounceIn 0.8s ease-out;
    margin-bottom: 5px;
}

.subtitle {
    font-size: 20px;
    color: rgba(255, 255, 255, 0.9);
    line-height: 1.4;
}

/* Character */
.character {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 10px 0;
}

.character-body {
    font-size: 60px;
    animation: bounce 2s ease-in-out infinite;
}

.character-speech {
    background: white;
    padding: 8px 16px;
    border-radius: 15px;
    font-size: 16px;
    font-weight: 600;
    color: #5a67d8;
    margin-top: 5px;
    position: relative;
}

.character-speech::before {
    content: '';
    position: absolute;
    top: -8px;
    left: 50%;
    transform: translateX(-50%);
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 8px solid white;
}

/* Game character */
.game-character {
    position: absolute;
    top: 60px;
    left: 20px;
    z-index: 10;
}

.game-character .character-body {
    font-size: 40px;
    animation: bounce 2s ease-in-out infinite;
}

.game-character.happy .character-body {
    animation: jump 0.5s ease-out;
}

.game-character.sad .character-body {
    animation: shake 0.5s ease-in-out;
}

/* Result character */
.result-character {
    margin-bottom: 10px;
}

.result-character .character-body {
    font-size: 70px;
    animation: jump 0.6s ease-out infinite;
}

.result-character.sad .character-body {
    animation: sway 1s ease-in-out infinite;
}

/* Mode toggle */
.mode-toggle {
    margin: 10px 0;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.2);
    padding: 12px 20px;
    border-radius: 30px;
}

.toggle-label input {
    display: none;
}

.toggle-switch {
    width: 50px;
    height: 28px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 14px;
    position: relative;
    transition: background 0.3s;
}

.toggle-switch::before {
    content: '';
    position: absolute;
    top: 4px;
    left: 4px;
    width: 20px;
    height: 20px;
    background: white;
    border-radius: 50%;
    transition: transform 0.3s;
}

.toggle-label input:checked + .toggle-switch {
    background: #84fab0;
}

.toggle-label input:checked + .toggle-switch::before {
    transform: translateX(22px);
}

.toggle-text {
    font-size: 18px;
    font-weight: 600;
    color: white;
}

/* Difficulty select */
.difficulty-select {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 5px 0;
}

.diff-btn {
    padding: 10px 20px;
    border: none;
    border-radius: 20px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.3);
    color: white;
    transition: all 0.2s;
}

.diff-btn.active {
    background: white;
    color: #5a67d8;
}

.diff-btn[data-diff="easy"].active { color: #38a169; }
.diff-btn[data-diff="medium"].active { color: #d69e2e; }
.diff-btn[data-diff="hard"].active { color: #e53e3e; }

/* Start button */
.start-btn {
    background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
    border: none;
    padding: 20px 50px;
    font-size: 28px;
    font-weight: 800;
    color: #276749;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 10px 40px rgba(132, 250, 176, 0.4);
    transition: all 0.3s ease;
    margin-top: 15px;
}

.start-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 15px 50px rgba(132, 250, 176, 0.5);
}

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

.history-btn {
    background: rgba(255, 255, 255, 0.25);
    border: none;
    padding: 12px 30px;
    font-size: 18px;
    font-weight: 700;
    color: white;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-top: 10px;
}

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

/* History Screen */
.history-screen {
    justify-content: flex-start;
    align-items: center;
    text-align: center;
    padding-top: 40px;
    gap: 20px;
}

.history-title {
    font-size: 36px;
    font-weight: 800;
    color: white;
    text-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.history-list {
    flex: 1;
    width: 100%;
    max-width: 350px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 10px;
}

.history-item {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.history-date {
    font-size: 14px;
    color: #718096;
    text-align: left;
}

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

.history-time {
    font-size: 18px;
    font-weight: 700;
    color: #5a67d8;
}

.history-score {
    font-size: 18px;
    font-weight: 700;
    color: #38a169;
}

.no-history {
    color: rgba(255, 255, 255, 0.8);
    font-size: 18px;
    margin-top: 40px;
}

/* Game Screen */
.game-screen {
    position: relative;
}

/* Header with timer, streak, progress */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0 20px;
}

.timer {
    background: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    padding: 10px 20px;
    border-radius: 50px;
    font-size: 24px;
    font-weight: 700;
    color: white;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.timer.warning {
    background: rgba(255, 100, 100, 0.5);
    animation: pulse 0.5s ease-in-out infinite;
}

.streak {
    background: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    padding: 8px 16px;
    border-radius: 50px;
    font-size: 18px;
    font-weight: 700;
    color: white;
}

.streak.hot {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    animation: glow 1s ease-in-out infinite alternate;
}

.progress {
    background: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    padding: 10px 20px;
    border-radius: 50px;
    font-size: 18px;
    font-weight: 700;
    color: white;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

/* Problem Area */
.problem-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: center;
    padding: 20px 20px 10px 20px;
}

.problem {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 30px;
    padding: 30px 40px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 48px;
    font-weight: 700;
    color: #333;
    transition: transform 0.3s ease;
}

.problem.shake {
    animation: shake 0.5s ease-in-out;
}

.problem.celebrate {
    animation: celebrate 0.5s ease-in-out;
}

.number {
    color: #5a67d8;
    min-width: 50px;
    text-align: center;
}

.operator {
    color: #ed64a6;
    font-size: 42px;
}

.equals {
    color: #9f7aea;
}

.answer-display {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
    min-width: 70px;
    text-align: center;
    padding: 8px 16px;
    border-radius: 15px;
    color: #c53030;
}

/* Feedback */
.feedback {
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32px;
    margin-top: 15px;
    margin-bottom: 10px;
}

.feedback-correct {
    animation: popIn 0.3s ease-out;
}

.feedback-wrong {
    animation: popIn 0.3s ease-out;
    color: white;
}

/* Controls */
.controls {
    padding: 0 0 30px 0;
}

/* Number Pad */
.numpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    max-width: 300px;
    margin: 0 auto;
}

.num-btn {
    height: 65px;
    border: none;
    border-radius: 18px;
    font-size: 30px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
    background: rgba(255, 255, 255, 0.9);
    color: #4a5568;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.num-btn:active {
    transform: scale(0.92);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* Colorful number buttons */
.num-btn[data-num="1"] { background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%); color: #c53030; }
.num-btn[data-num="2"] { background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%); color: #c05621; }
.num-btn[data-num="3"] { background: linear-gradient(135deg, #fff5b7 0%, #fce38a 100%); color: #b7791f; }
.num-btn[data-num="4"] { background: linear-gradient(135deg, #c1ffc1 0%, #a8edea 100%); color: #276749; }
.num-btn[data-num="5"] { background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%); color: #2c7a7b; }
.num-btn[data-num="6"] { background: linear-gradient(135deg, #96e6a1 0%, #d4fc79 100%); color: #276749; }
.num-btn[data-num="7"] { background: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%); color: #2b6cb0; }
.num-btn[data-num="8"] { background: linear-gradient(135deg, #c3cfe2 0%, #cfd9df 100%); color: #4a5568; }
.num-btn[data-num="9"] { background: linear-gradient(135deg, #e0c3fc 0%, #8ec5fc 100%); color: #6b46c1; }
.num-btn[data-num="0"] { background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); color: #4a5568; }

.clear-btn {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%) !important;
    color: #c05621 !important;
}

.ok-btn {
    background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%) !important;
    color: #276749 !important;
}

/* Results Screen */
.results-screen {
    justify-content: center;
    align-items: center;
    text-align: center;
    gap: 15px;
}

.results-title {
    font-size: 42px;
    font-weight: 800;
    color: white;
    text-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.new-record {
    background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
    padding: 10px 25px;
    border-radius: 25px;
    font-size: 20px;
    font-weight: 700;
    color: #c05621;
    animation: popIn 0.5s ease-out, glow 1s ease-in-out infinite alternate;
}

.star-earned {
    background: linear-gradient(135deg, #fff5b7 0%, #fce38a 100%);
    padding: 10px 25px;
    border-radius: 25px;
    font-size: 20px;
    font-weight: 700;
    color: #b7791f;
    animation: popIn 0.5s ease-out 0.2s both;
}

.results-stats {
    display: flex;
    gap: 15px;
    justify-content: center;
    flex-wrap: wrap;
}

.stat {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 20px 25px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    gap: 5px;
    animation: popIn 0.5s ease-out;
}

.stat-label {
    font-size: 14px;
    color: #718096;
    font-weight: 600;
}

.stat-value {
    font-size: 28px;
    font-weight: 800;
    color: #5a67d8;
}

/* Time's Up Screen */
.timesup-screen {
    justify-content: center;
    align-items: center;
    text-align: center;
    gap: 20px;
}

.timesup-title {
    font-size: 48px;
    font-weight: 800;
    color: white;
    text-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.timesup-subtitle {
    font-size: 24px;
    color: rgba(255, 255, 255, 0.9);
}

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

.confetti {
    position: absolute;
    width: 12px;
    height: 12px;
    border-radius: 2px;
    animation: confetti-fall 3s ease-out forwards;
}

.star {
    position: absolute;
    font-size: 40px;
    animation: star-pop 0.8s ease-out forwards;
    pointer-events: none;
}

/* Animations */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-8px); }
    20%, 40%, 60%, 80% { transform: translateX(8px); }
}

@keyframes celebrate {
    0% { transform: scale(1); }
    50% { transform: scale(1.08); }
    100% { transform: scale(1); }
}

@keyframes popIn {
    0% { transform: scale(0); opacity: 0; }
    70% { transform: scale(1.2); }
    100% { transform: scale(1); opacity: 1; }
}

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

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

@keyframes jump {
    0% { transform: translateY(0) scale(1); }
    50% { transform: translateY(-30px) scale(1.1); }
    100% { transform: translateY(0) scale(1); }
}

@keyframes sway {
    0%, 100% { transform: rotate(-5deg); }
    50% { transform: rotate(5deg); }
}

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

@keyframes glow {
    from { box-shadow: 0 0 10px rgba(255, 255, 255, 0.3); }
    to { box-shadow: 0 0 20px rgba(255, 255, 255, 0.6); }
}

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

@keyframes star-pop {
    0% {
        transform: scale(0) rotate(0deg);
        opacity: 1;
    }
    50% {
        transform: scale(1.5) rotate(180deg);
        opacity: 1;
    }
    100% {
        transform: scale(0) rotate(360deg) translateY(-50px);
        opacity: 0;
    }
}

/* Music Toggle Button */
.music-toggle {
    position: fixed;
    bottom: 20px;
    left: 20px;
    width: 50px;
    height: 50px;
    border: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.25);
    backdrop-filter: blur(10px);
    cursor: pointer;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.music-toggle:hover {
    transform: scale(1.1);
    background: rgba(255, 255, 255, 0.35);
}

.music-toggle:active {
    transform: scale(0.95);
}

.music-toggle.playing {
    background: linear-gradient(135deg, #84fab0 0%, #8fd3f4 100%);
    animation: pulse 2s ease-in-out infinite;
}

.music-icon {
    font-size: 24px;
}

/* Responsive adjustments */
@media (max-height: 700px) {
    .title {
        font-size: 36px;
    }

    .character-body {
        font-size: 50px;
    }

    .start-btn {
        padding: 16px 40px;
        font-size: 24px;
    }

    .problem {
        font-size: 36px;
        padding: 20px 30px;
    }

    .num-btn {
        height: 55px;
        font-size: 26px;
    }

    .timer, .progress {
        font-size: 18px;
        padding: 8px 14px;
    }
}

@media (min-width: 768px) {
    .numpad {
        max-width: 350px;
        gap: 12px;
    }

    .num-btn {
        height: 75px;
        font-size: 34px;
    }
}
