:root {
    --grid-cell-size: 40px;
    --primary-blue: #b0c4de;
    --grid-line: #e6f2ff;
    --active-color: #FFD700;
    --success-color: #4CAF50;
    --failure-color: #f44336;
    --text-color: #333;
    --bg-color: #f0f4f8;
    --card-bg: #ffffff;
    --font-mono: 'Courier New', Courier, monospace;
}

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

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.hidden {
    display: none !important;
}

/* Start Screen */
#startScreen {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 40px;
    max-width: 600px;
    width: 90%;
    text-align: center;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

#startScreen h1 {
    font-size: 2rem;
    margin-bottom: 8px;
    color: var(--text-color);
}

.subtitle {
    color: #666;
    margin-bottom: 30px;
    font-size: 1.1rem;
}

.operation-select {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 24px;
}

.op-btn {
    padding: 20px 12px;
    border: 2px solid #e0e0e0;
    border-radius: 16px;
    background: #fff;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.op-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.op-btn.selected {
    border-width: 3px;
    background: #f8faff;
}

.op-btn[data-operation="addition"].selected { border-color: #4CAF50; }
.op-btn[data-operation="subtraction"].selected { border-color: #2196F3; }
.op-btn[data-operation="multiplication"].selected { border-color: #FF9800; }
.op-btn[data-operation="division"].selected { border-color: #9C27B0; }

.op-symbol {
    font-size: 2.2rem;
    font-weight: bold;
    line-height: 1;
}

.op-label {
    font-size: 0.9rem;
    color: #666;
}

.difficulty-select {
    display: flex;
    gap: 16px;
    justify-content: center;
    margin-bottom: 28px;
}

.digit-picker {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.digit-picker label {
    font-size: 0.85rem;
    color: #666;
}

.digit-picker select {
    padding: 8px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    font-size: 1rem;
    background: #fff;
    cursor: pointer;
}

.start-btn {
    padding: 16px 48px;
    font-size: 1.2rem;
    font-weight: 600;
    border: none;
    border-radius: 14px;
    background: #4CAF50;
    color: #fff;
    cursor: pointer;
    transition: all 0.2s;
}

.start-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(76,175,80,0.4);
}

.start-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

#highscorePreview {
    margin-top: 16px;
    font-size: 0.9rem;
    color: #888;
}

#footerInfo {
    margin-top: 24px;
    font-size: 0.8rem;
    color: #aaa;
}

#imprintLink {
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

#imprintLink:hover {
    color: #666;
}

/* Imprint Modal */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 20px;
}

.modal-content {
    background: var(--card-bg);
    border-radius: 16px;
    padding: 32px;
    max-width: 480px;
    width: 100%;
    position: relative;
    text-align: left;
    line-height: 1.6;
}

.modal-content h2 {
    margin-bottom: 16px;
    font-size: 1.4rem;
}

.modal-content p {
    margin-bottom: 12px;
    font-size: 0.95rem;
}

.modal-content a {
    color: var(--text-color);
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    font-size: 1.8rem;
    cursor: pointer;
    color: #999;
    line-height: 1;
}

.modal-close:hover {
    color: #333;
}

/* Game Screen */
#gameScreen {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    padding: 12px;
    background: var(--card-bg);
    overflow-y: auto;
}

#gameHeader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    font-size: 1.1rem;
    font-weight: 600;
    flex-shrink: 0;
}

#abortBtn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: #e0e0e0;
    color: #666;
    font-size: 1.3rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    line-height: 1;
}

#abortBtn:hover {
    background: var(--failure-color);
    color: #fff;
}

#timerDisplay {
    font-family: var(--font-mono);
    font-size: 1.3rem;
    color: #555;
}

#taskDisplay {
    text-align: center;
    font-size: 1.5rem;
    font-weight: 600;
    padding: 12px;
    font-family: var(--font-mono);
    flex-shrink: 0;
}

#gridContainer {
    flex: 0 0 auto;
    overflow: auto;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 4px 6px;
    min-height: 0;
}

.math-grid {
    display: grid;
    grid-template-columns: repeat(20, var(--grid-cell-size));
    grid-auto-rows: var(--grid-cell-size);
    gap: 0;
    background-color: #ffffff;
    border: 1px solid var(--primary-blue);
    width: fit-content;
}

.math-cell {
    width: var(--grid-cell-size);
    height: var(--grid-cell-size);
    border: 0.5px solid var(--grid-line);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18pt;
    font-family: var(--font-mono);
    color: var(--text-color);
    user-select: none;
    cursor: pointer;
    transition: background-color 0.15s;
    line-height: 1;
}

.math-cell.active {
    background-color: #fff8dc;
    border-color: var(--active-color);
    border-width: 2px;
}

.math-cell.task-cell {
    font-weight: bold;
    color: #1a1a1a;
}

#resultArea {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 6px 16px;
    flex-shrink: 0;
}

#resultArea label {
    font-size: 1.1rem;
    font-weight: 600;
}

.result-display {
    padding: 10px 16px;
    font-size: 1.3rem;
    font-family: var(--font-mono);
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    width: 160px;
    text-align: center;
    background: #fff;
    color: var(--text-color);
    min-height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: text;
    outline: none;
}

.result-display:focus {
    background-color: #fff8dc;
    border-color: var(--active-color);
    border-width: 2px;
}

#checkBtn {
    padding: 10px 28px;
    font-size: 1.1rem;
    font-weight: 600;
    border: none;
    border-radius: 10px;
    background: var(--success-color);
    color: #fff;
    cursor: pointer;
    transition: all 0.2s;
}

#checkBtn:hover {
    transform: translateY(-1px);
    box-shadow: 0 3px 12px rgba(76,175,80,0.3);
}

/* On-Screen Keyboard */
#onscreenKeyboard {
    flex-shrink: 0;
    padding: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.kb-row {
    display: flex;
    gap: 6px;
}

.kb-btn {
    width: 64px;
    height: 56px;
    font-size: 1.3rem;
    font-weight: 600;
    border: 1px solid #ddd;
    border-radius: 10px;
    background: #f8f8f8;
    cursor: pointer;
    transition: all 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-mono);
}

.kb-btn:hover {
    background: #e8e8e8;
}

.kb-btn:active {
    transform: scale(0.95);
    background: #ddd;
}

.kb-backspace {
    background: #ffecb3 !important;
}

.kb-equals {
    background: #c8e6c9 !important;
}

/* Result Screen */
#resultScreen {
    background: var(--card-bg);
    border-radius: 20px;
    padding: 50px 40px;
    max-width: 500px;
    width: 90%;
    text-align: center;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

#resultScreen h1 {
    font-size: 2.2rem;
    margin-bottom: 20px;
}

#finalTime {
    font-size: 2rem;
    font-family: var(--font-mono);
    color: var(--success-color);
    margin-bottom: 16px;
}

#highscoreMessage {
    font-size: 1.1rem;
    color: #FF9800;
    margin-bottom: 28px;
    font-weight: 600;
}

/* Feedback Animations */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-8px); }
    75% { transform: translateX(8px); }
}

@keyframes flash-green {
    0% { background-color: transparent; }
    50% { background-color: #c8e6c9; }
    100% { background-color: transparent; }
}

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

.flash-success {
    animation: flash-green 0.5s ease;
}

/* Responsive */
@media (max-width: 768px) {
    :root {
        --grid-cell-size: 32px;
    }

    #startScreen {
        padding: 24px 16px;
    }

    #startScreen h1 {
        font-size: 1.5rem;
    }

    .operation-select {
        gap: 8px;
    }

    .op-btn {
        padding: 14px 8px;
    }

    .op-symbol {
        font-size: 1.8rem;
    }

    .kb-btn {
        width: 52px;
        height: 48px;
        font-size: 1.1rem;
    }

    #resultInput {
        width: 130px;
        font-size: 1.1rem;
    }

    #taskDisplay {
        font-size: 1.2rem;
    }
}

@media (max-width: 480px) {
    :root {
        --grid-cell-size: calc((100vw - 28px) / 20);
    }

    #gameScreen {
        padding: 6px 4px;
    }

    #gameHeader {
        padding: 4px 8px;
        font-size: 0.9rem;
    }

    #timerDisplay {
        font-size: 1rem;
    }

    #taskDisplay {
        font-size: 1rem;
        padding: 6px;
    }

    #gridContainer {
        padding: 2px 4px;
        overflow: hidden;
    }

    .math-grid {
        border-left: 1px solid var(--primary-blue);
        border-right: 1px solid var(--primary-blue);
    }

    .math-cell {
        font-size: 14pt;
    }

    .kb-btn {
        width: calc((100vw - 40px) / 4);
        height: 44px;
        font-size: 1rem;
        padding: 0;
    }

    #onscreenKeyboard {
        padding: 4px;
        gap: 4px;
    }

    .kb-row {
        gap: 4px;
    }

    #resultArea {
        padding: 4px 8px;
        gap: 4px;
    }

    #resultInput {
        width: 100px;
        font-size: 1rem;
        padding: 6px 10px;
        min-height: 40px;
    }

    #checkBtn {
        padding: 6px 16px;
        font-size: 0.95rem;
    }

    .difficulty-select {
        flex-direction: column;
        gap: 10px;
    }

    #splashContent {
        padding: 24px 32px;
    }

    #splashIcon {
        font-size: 3.5rem;
    }

    #splashText {
        font-size: 1.3rem;
    }
}

/* Splash Overlay */
#splashOverlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.3);
    z-index: 1000;
    animation: splashFadeIn 0.15s ease;
}

#splashContent {
    background: var(--card-bg);
    border-radius: 28px;
    padding: 40px 60px;
    text-align: center;
    box-shadow: 0 8px 40px rgba(0,0,0,0.15);
    animation: splashPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#splashIcon {
    font-size: 5rem;
    line-height: 1.2;
}

#splashOverlay.success #splashIcon {
    color: var(--success-color);
}

#splashOverlay.failure #splashIcon {
    color: var(--failure-color);
}

#splashText {
    font-size: 1.8rem;
    font-weight: 700;
    margin-top: 12px;
}

#splashOverlay.success #splashText {
    color: var(--success-color);
}

#splashOverlay.failure #splashText {
    color: var(--failure-color);
}

@keyframes splashFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

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