:root {
    /* Rayman Legends Inspired Palette */
    --primary-color: #ffd700;
    /* Gold */
    --secondary-color: #76b900;
    /* Rayman Green */
    --accent-color: #9b59b6;
    /* Magical Purple */
    --danger-color: #e74c3c;
    /* Red */
    --text-color: #2c3e50;
    /* Dark Blue/Grey */
    --wood-light: #e67e22;
    --wood-dark: #d35400;
    --stone-light: #ecf0f1;
    --stone-dark: #bdc3c7;

    /* Backgrounds */
    --bg-gradient: radial-gradient(circle at center, #4834d4 0%, #130f40 100%);
    --container-bg: #f1f2f6;

    /* Fonts */
    --font-heading: 'Bangers', cursive;
    --font-body: 'Patrick Hand', cursive;
    --font-digits: 'Fredoka', sans-serif;

    /* Effects */
    --shadow-hard: 4px 4px 0px rgba(0, 0, 0, 0.3);
    --glow: 0 0 20px rgba(255, 215, 0, 0.6);
    --border-radius: 15px;
}

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

body {
    font-family: var(--font-body);
    background: var(--bg-gradient);
    min-height: 100vh;
    display: flex;
    /* justify-content: center; */
    /* align-items: center; */
    padding: 1rem;
    color: var(--text-color);
    overflow-y: auto;
    overflow-x: hidden;
    background-image:
        radial-gradient(circle at 20% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 20%),
        radial-gradient(circle at 80% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 20%),
        var(--bg-gradient);
}

.app-container {
    margin: auto;
    background: #fdfbf7;
    /* Parchment color */
    border: 8px solid #5d4037;
    /* Dark wood border */
    border-radius: 20px;
    box-shadow:
        0 0 0 4px #8d6e63,
        /* Lighter wood outline */
        0 20px 50px rgba(0, 0, 0, 0.5);
    width: 90%;
    max-width: 500px;
    padding: 2rem;
    text-align: center;
    position: relative;
    animation: popIn 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    transform: rotate(-1deg);
    /* Slight tilt for playfulness */
}

/* Decorative corners */
.app-container::before,
.app-container::after {
    content: '';
    position: absolute;
    width: 15px;
    height: 15px;
    background: #ffd700;
    border-radius: 50%;
    box-shadow: inset -2px -2px 5px rgba(0, 0, 0, 0.2);
    z-index: 10;
}

.app-container::before {
    top: 10px;
    left: 10px;
}

.app-container::after {
    top: 10px;
    right: 10px;
}

@keyframes popIn {
    0% {
        transform: scale(0.5) rotate(-5deg);
        opacity: 0;
    }

    100% {
        transform: scale(1) rotate(-1deg);
        opacity: 1;
    }
}

header h1 {
    font-family: var(--font-heading);
    color: #d35400;
    font-size: 3.5rem;
    margin-bottom: 1.5rem;
    text-shadow: 3px 3px 0px #f1c40f;
    letter-spacing: 2px;
    transform: rotate(2deg);
}

/* Screens */
.screen {
    display: none;
    flex-direction: column;
    align-items: center;
    animation: fadeIn 0.3s ease-in-out;
}

.screen.active {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Selection Screen */
.mode-selection {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    width: 100%;
    margin-top: 1rem;
}

.mode-card {
    background: rgba(255, 255, 255, 0.6);
    padding: 1.5rem;
    border-radius: 15px;
    border: 2px dashed #bdc3c7;
    transition: all 0.2s;
}

.mode-card:hover {
    background: rgba(255, 255, 255, 0.9);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.mode-card h3 {
    font-family: var(--font-heading);
    color: var(--wood-dark);
    font-size: 1.8rem;
    margin-bottom: 0.5rem;
}

.mode-card p {
    color: var(--text-color);
    margin-bottom: 1rem;
    font-size: 1.1rem;
}

.mode-divider {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--wood-light);
    font-family: var(--font-heading);
    font-size: 1.5rem;
}

.mode-divider::before,
.mode-divider::after {
    content: '';
    height: 2px;
    background: var(--wood-light);
    flex: 1;
    margin: 0 1rem;
    opacity: 0.5;
}

/* Number Grid */
.number-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.8rem;
    width: 100%;
}

.number-btn {
    background: white;
    border: 2px solid var(--wood-light);
    color: var(--wood-dark);
    font-family: var(--font-heading);
    font-size: 1.5rem;
    padding: 0.5rem;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.1s;
    box-shadow: 0 4px 0 rgba(230, 126, 34, 0.3);
}

.number-btn:hover {
    transform: translateY(-2px);
    background: var(--primary-color);
    color: #d35400;
    border-color: #d35400;
}

.number-btn:active {
    transform: translateY(2px);
    box-shadow: none;
}

.number-btn.wide {
    grid-column: span 4;
    background: var(--accent-color);
    color: white;
    border-color: #8e44ad;
    box-shadow: 0 4px 0 #8e44ad;
}

.number-btn.wide:hover {
    background: #9b59b6;
    filter: brightness(1.1);
}

.large-btn {
    font-family: var(--font-heading);
    font-size: 2.5rem;
    padding: 1rem 3rem;
    background: linear-gradient(to bottom, #2ecc71, #27ae60);
    color: white;
    border: 4px solid #1e8449;
    border-radius: 15px;
    box-shadow: 0 6px 0 #1e8449, 0 10px 10px rgba(0, 0, 0, 0.2);
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.2);
    transition: all 0.1s;
    cursor: pointer;
}

.large-btn:hover {
    transform: translateY(-2px) scale(1.02);
    filter: brightness(1.1);
}

.large-btn:active {
    transform: translateY(4px);
    box-shadow: 0 2px 0 #1e8449;
}

.secondary-btn {
    background: linear-gradient(to bottom, #3498db, #2980b9);
    border: 4px solid #2980b9;
    box-shadow: 0 6px 0 #1c5980;
    font-size: 1.5rem;
    margin-top: 1rem;
}

.secondary-btn:active {
    box-shadow: 0 2px 0 #1c5980;
}

/* Game Screen */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 2rem;
}

.icon-btn {
    background: #e74c3c;
    border: 3px solid #c0392b;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0.5rem 1rem;
    border-radius: 10px;
    box-shadow: 0 4px 0 #c0392b;
    font-family: var(--font-heading);
    transition: all 0.1s;
}

.icon-btn:active {
    transform: translateY(4px);
    box-shadow: none;
}

.stats-display {
    display: flex;
    gap: 1rem;
}

.stat-box {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    color: #f1c40f;
    background: #2c3e50;
    padding: 0.5rem 1rem;
    border-radius: 15px;
    border: 3px solid #34495e;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
    min-width: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
}

.problem-container {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
    font-size: 4rem;
    font-weight: 400;
    color: #2c3e50;
    margin-bottom: 2rem;
    font-family: var(--font-heading);
    min-height: 160px;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.1);
}

/* Vertical Layout */
.problem-container.vertical {
    flex-direction: column;
    align-items: flex-end;
    width: fit-content;
    margin: 0 auto 2rem auto;
    padding: 1.5rem;
    background: rgba(255, 255, 255, 0.5);
    border-radius: 15px;
    border: 2px dashed #bdc3c7;
    transform: rotate(1deg);
}

.problem-container.vertical .row {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: 100%;
}

.problem-container.vertical .operator {
    margin-right: 1rem;
    color: #e67e22;
}

.problem-container.vertical .divider {
    width: 100%;
    height: 6px;
    background-color: #2c3e50;
    margin: 0.5rem 0;
    border-radius: 3px;
}

/* Digit Box System */
.digit-row {
    display: flex;
    justify-content: flex-end;
    gap: 0.4rem;
}

.digit-box {
    min-width: 60px;
    width: auto;
    padding: 0 15px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2.5rem;
    font-family: var(--font-digits);
    color: #2c3e50;
    border-radius: 10px;
    border: 3px solid transparent;
    background: transparent;
}

.digit-box.input-mode {
    background: #fff;
    border: 3px solid #bdc3c7;
    color: #2c3e50;
    cursor: pointer;
    box-shadow: inset 0 4px 4px rgba(0, 0, 0, 0.1);
}

.digit-box.input-mode.active {
    border-color: #3498db;
    box-shadow: 0 0 0 4px rgba(52, 152, 219, 0.3);
    transform: scale(1.1);
    z-index: 5;
}

.digit-box.input-mode.filled {
    background: #e8f6f3;
    color: #16a085;
}

.digit-box.operator-box {
    width: 40px;
}

/* Keypad */
.keypad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
    width: 100%;
    padding: 1rem;
    background: rgba(0, 0, 0, 0.05);
    border-radius: 20px;
}

.key {
    background: linear-gradient(to bottom, #ecf0f1, #bdc3c7);
    border: none;
    padding: 1rem;
    font-size: 2rem;
    font-family: var(--font-digits);
    color: #2c3e50;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 5px 0 #95a5a6;
    transition: all 0.1s;
    text-shadow: 1px 1px 0 white;
}

.key:active {
    transform: translateY(5px);
    box-shadow: 0 0 0 #95a5a6;
}

.key[data-action="submit"] {
    background: linear-gradient(to bottom, #2ecc71, #27ae60);
    color: white;
    box-shadow: 0 5px 0 #1e8449;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.2);
}

.key[data-action="submit"]:active {
    box-shadow: 0 0 0 #1e8449;
}

.key[data-action="clear"] {
    background: linear-gradient(to bottom, #e74c3c, #c0392b);
    color: white;
    box-shadow: 0 5px 0 #922b21;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.2);
}

.key[data-action="clear"]:active {
    box-shadow: 0 0 0 #922b21;
}

/* Game Over Screen */
.results-card {
    background: white;
    padding: 2rem;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    margin-bottom: 2rem;
    width: 100%;
    border: 4px solid #bdc3c7;
}

.final-score-container {
    margin-bottom: 2rem;
}

.score-label {
    font-size: 1.5rem;
    color: #7f8c8d;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.final-score {
    font-family: var(--font-heading);
    font-size: 5rem;
    color: var(--primary-color);
    text-shadow: 3px 3px 0 #d35400;
    line-height: 1;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.stat-item {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.stat-label {
    font-size: 1.2rem;
    color: #95a5a6;
}

.stat-value {
    font-family: var(--font-heading);
    font-size: 2rem;
    color: #2c3e50;
}

.leaderboard-container {
    width: 100%;
    margin-bottom: 2rem;
}

.leaderboard-container h3 {
    font-family: var(--font-heading);
    color: #f1c40f;
    font-size: 2rem;
    margin-bottom: 1rem;
    text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.2);
}

.leaderboard-list {
    list-style: none;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 15px;
    padding: 1rem;
    border: 2px solid #bdc3c7;
}

.leaderboard-item {
    display: flex;
    justify-content: space-between;
    padding: 0.8rem;
    border-bottom: 2px dashed #ecf0f1;
    font-size: 1.2rem;
    font-weight: bold;
}

.leaderboard-item:last-child {
    border-bottom: none;
}

.leaderboard-rank {
    color: #e67e22;
    margin-right: 1rem;
}

.leaderboard-score {
    color: #27ae60;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 100%;
}

/* Animations */
.shake {
    animation: shake 0.5s cubic-bezier(.36, .07, .19, .97) both;
}

@keyframes shake {

    10%,
    90% {
        transform: translate3d(-2px, 0, 0);
    }

    20%,
    80% {
        transform: translate3d(4px, 0, 0);
    }

    30%,
    50%,
    70% {
        transform: translate3d(-6px, 0, 0);
    }

    40%,
    60% {
        transform: translate3d(6px, 0, 0);
    }
}

.bounce {
    animation: bounce 0.6s cubic-bezier(0.280, 0.840, 0.420, 1);
}

@keyframes bounce {
    0% {
        transform: scale(1, 1) translateY(0);
    }

    10% {
        transform: scale(1.1, .9) translateY(0);
    }

    30% {
        transform: scale(.9, 1.1) translateY(-20px);
    }

    50% {
        transform: scale(1.05, .95) translateY(0);
    }

    57% {
        transform: scale(1, 1) translateY(-7px);
    }

    64% {
        transform: scale(1, 1) translateY(0);
    }

    100% {
        transform: scale(1, 1) translateY(0);
    }
}

/* Responsive */
@media (max-width: 400px) {
    .problem-container {
        font-size: 2.5rem;
    }

    .key {
        padding: 0.8rem;
        font-size: 1.5rem;
    }
}