:root {
    --bg-color: #f0f9ff;
    --primary-color: #3b82f6;
    --secondary-color: #60a5fa;
    --correct-color: #22c55e;
    --wrong-color: #ef4444;
    --text-color: #1e293b;
    --key-bg: #ffffff;
    --key-shadow: 0 4px 0 #cbd5e1;
    --key-active: #cbd5e1;
    --font-main: 'Noto Sans TC', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    /* Stops iOS Safari's double-tap-to-zoom gesture on any element
       (buttons or elsewhere) without blocking normal single taps. */
    touch-action: manipulation;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

#game-container {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-width: 500px;
    max-height: 900px;
    background: white;
    display: flex;
    flex-direction: column;
    position: relative;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    border-radius: 20px;
    overflow: hidden;
    /* Lets children size themselves relative to the container's actual
       box (cqh/cqw) instead of the viewport, so the layout stays correct
       no matter how the container itself ends up sized. */
    container-type: size;
    container-name: game;
}

/* Overlays (Start and Result) */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #f0f9ff;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 20;
    padding: clamp(10px, 4vh, 20px) 20px;
    padding: clamp(10px, 4cqh, 20px) 20px;
    transition: opacity 0.3s ease;
    overflow: hidden;
}

.hidden {
    display: none !important;
}

/* Fills the overlay and distributes its children (title / level list /
   start button) across the available height so the menu can never
   overflow, no matter how short the screen is. */
#start-content {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.game-title {
    flex: 0 1 auto;
    min-height: 0;
    font-size: clamp(1.4rem, 7vh, 3.5rem);
    font-size: clamp(1.4rem, 7cqh, 3.5rem);
    font-weight: 900;
    line-height: 1.15;
    color: var(--primary-color);
    margin-bottom: clamp(6px, 3vh, 40px);
    margin-bottom: clamp(6px, 3cqh, 40px);
    text-shadow: 2px 2px 0px #fff, 4px 4px 0px rgba(0,0,0,0.1);
    text-align: center;
    overflow: hidden;
}

.level-selection {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    text-align: center;
    margin-bottom: clamp(6px, 3vh, 40px);
    margin-bottom: clamp(6px, 3cqh, 40px);
    width: 100%;
}

.level-selection p {
    flex: 0 0 auto;
    font-size: clamp(0.85rem, 3vh, 1.5rem);
    font-size: clamp(0.85rem, 3cqh, 1.5rem);
    margin-bottom: clamp(4px, 1.6vh, 15px);
    margin-bottom: clamp(4px, 1.6cqh, 15px);
    font-weight: bold;
}

#level-btn-list {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: clamp(4px, 1.4vh, 12px);
    gap: clamp(4px, 1.4cqh, 12px);
    width: 100%;
}

.level-btn {
    flex: 1 1 0;
    min-height: 0;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 15px;
    font-size: clamp(0.8rem, 2.8vh, 1.5rem);
    font-size: clamp(0.8rem, 2.8cqh, 1.5rem);
    border: 3px solid var(--primary-color);
    border-radius: 15px;
    background: white;
    cursor: pointer;
    transition: all 0.2s;
}

.level-btn.active {
    background: var(--primary-color);
    color: white;
    transform: scale(1.05);
}

.start-btn {
    flex: 0 0 auto;
    padding: clamp(8px, 3vh, 20px) clamp(30px, 10vw, 50px);
    padding: clamp(8px, 3cqh, 20px) clamp(30px, 10vw, 50px);
    font-size: clamp(1.1rem, 3.5vh, 2rem);
    font-size: clamp(1.1rem, 3.5cqh, 2rem);
    border: none;
    background: var(--correct-color);
    color: white;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 6px 0 #16a34a;
}

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

/* Status Bar */
#status-bar {
    padding: clamp(6px, 2vh, 14px) clamp(10px, 4vw, 18px);
    padding: clamp(6px, 2cqh, 14px) clamp(10px, 4cqw, 18px);
    background: #e0f2fe;
    display: flex;
    justify-content: space-between;
    font-weight: bold;
    font-size: clamp(0.75rem, 2.6vh, 1rem);
    font-size: clamp(0.75rem, 2.6cqh, 1rem);
    flex-shrink: 0;
}

/* Problem Display: flexes to fill whatever space is left between the
   status bar and keyboard, and never forces the container to grow. */
#problem-display {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    overflow: hidden;
    padding: 2px 8px;
}

#current-problem {
    font-size: clamp(1.4rem, 7vh, 3.2rem);
    font-size: clamp(1.4rem, 7cqh, 3.2rem);
    font-weight: 900;
    margin-bottom: 4px;
    line-height: 1.2;
}

#feedback-message {
    height: clamp(1rem, 3.5vh, 1.6rem);
    height: clamp(1rem, 3.5cqh, 1.6rem);
    font-size: clamp(0.8rem, 3vh, 1.3rem);
    font-size: clamp(0.8rem, 3cqh, 1.3rem);
    font-weight: bold;
}

/* Input Display */
#input-display {
    height: 0;
    overflow: hidden;
}

/* Keyboard: given a fixed height budget (relative to the container, not
   the button width) so it can never grow taller than the screen allows. */
#keyboard {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(4, minmax(0, 1fr)) minmax(0, 1.1fr);
    gap: clamp(6px, 2.2vh, 16px);
    gap: clamp(6px, 2.2cqh, 16px);
    padding: clamp(8px, 2.2vh, 18px);
    padding: clamp(8px, 2.2cqh, 18px);
    background: #f8fafc;
    flex-shrink: 0;
    height: clamp(260px, 75vh, 660px);
    height: clamp(260px, 75cqh, 660px);
}

.key {
    min-width: 0;
    min-height: 0;
    border-radius: 16px;
    font-weight: bold;
    cursor: pointer;
    background: var(--key-bg);
    box-shadow: var(--key-shadow);
    transition: transform 0.05s, box-shadow 0.05s;
    display: flex;
    justify-content: center;
    align-items: center;
}

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

/* Number/control keys are true squares: sized to the larger of
   width-100%-then-clamped-by-height, so they're always as big as
   possible while fitting both their column's width and row's height. */
.key.num,
.key.ctrl {
    height: 100%;
    max-width: 100%;
    aspect-ratio: 1 / 1;
    justify-self: center;
    align-self: center;
    font-size: clamp(1.3rem, 9vh, 3rem);
    font-size: clamp(1.3rem, 9cqh, 3rem);
}

.key.num {
    background-color: #ffffff;
    color: var(--text-color);
}

.key.ctrl {
    font-size: clamp(0.9rem, 6vh, 1.7rem);
    font-size: clamp(0.9rem, 6cqh, 1.7rem);
    background-color: #cbd5e1;
    color: var(--text-color);
}

.key.special {
    width: 100%;
    height: 100%;
    font-size: clamp(1.3rem, 7.5vh, 2.3rem);
    font-size: clamp(1.3rem, 7.5cqh, 2.3rem);
    background-color: var(--secondary-color);
    color: white;
}

/* Result Card */
.result-card {
    background: white;
    padding: clamp(16px, 6vh, 40px);
    padding: clamp(16px, 6cqh, 40px);
    border-radius: 25px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2);
    text-align: center;
    width: 80%;
}

.result-title {
    font-size: clamp(1.5rem, 6vh, 2.5rem);
    font-size: clamp(1.5rem, 6cqh, 2.5rem);
    margin-bottom: clamp(8px, 3vh, 20px);
    margin-bottom: clamp(8px, 3cqh, 20px);
}

.result-detail {
    font-size: clamp(1rem, 3.5vh, 1.5rem);
    font-size: clamp(1rem, 3.5cqh, 1.5rem);
    margin-bottom: clamp(4px, 1.5vh, 10px);
    margin-bottom: clamp(4px, 1.5cqh, 10px);
}

#restart-btn {
    padding: clamp(8px, 3vh, 15px) 40px;
    padding: clamp(8px, 3cqh, 15px) 40px;
    font-size: clamp(1rem, 3.5vh, 1.5rem);
    font-size: clamp(1rem, 3.5cqh, 1.5rem);
    border: none;
    background: var(--primary-color);
    color: white;
    border-radius: 50px;
    cursor: pointer;
}

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

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

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

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