* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg: #1a1a2e;
    --bg2: #16213e;
    --accent: #e94560;
    --gold: #f5c842;
    --green: #4ade80;
    --blue: #60a5fa;
    --text: #eaeaea;
    --text-dim: #8892a4;
    --panel: #0f3460;
    --panel-border: #1a4a7a;
}

body {
    background: var(--bg);
    color: var(--text);
    font-family: 'Baloo 2', cursive, sans-serif;
    overflow: hidden;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    user-select: none;
}

#game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 700px;
    flex: 1;
    position: relative;
}

#canvas {
    display: block;
    image-rendering: auto;
    touch-action: none;
}

#hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 8px 12px;
    background: var(--bg2);
    border-bottom: 2px solid var(--panel-border);
    gap: 8px;
    flex-shrink: 0;
}

#hud-left { font-size: 14px; font-weight: 600; color: var(--text-dim); white-space: nowrap; }
#hud-center { font-size: 20px; font-weight: 800; text-align: center; flex: 1; }
#goal-value { color: var(--gold); animation: pulse 2s ease-in-out infinite; }
#hud-right { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text-dim); white-space: nowrap; }

#hud-right button {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    color: var(--text);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    transition: background 0.15s;
    font-family: inherit;
}
#hud-right button:hover { background: var(--accent); }

#expression-panel {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg2);
    border-top: 2px solid var(--panel-border);
    text-align: center;
    min-height: 56px;
    flex-shrink: 0;
}

#expression-display {
    font-family: 'Fira Code', monospace;
    font-size: 16px;
    line-height: 1.4;
    color: var(--text);
}

#eval-steps {
    font-family: 'Fira Code', monospace;
    font-size: 13px;
    color: var(--text-dim);
    margin-top: 2px;
}

.expr-num { color: var(--blue); font-weight: 600; }
.expr-plus { color: #4ade80; font-weight: 600; }
.expr-mul { color: #f87171; font-weight: 600; }
.expr-paren { color: var(--gold); font-weight: 600; }
.expr-ok { color: var(--green); }
.expr-fail { color: var(--accent); }
.expr-err { color: #fbbf24; }

#dpad {
    display: none;
    position: fixed;
    bottom: 60px;
    right: 16px;
    width: 120px;
    height: 120px;
    z-index: 50;
}

.dpad-btn {
    position: absolute;
    width: 40px;
    height: 40px;
    background: rgba(15, 52, 96, 0.85);
    border: 1px solid var(--panel-border);
    border-radius: 10px;
    color: var(--text);
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}
.dpad-btn:active { background: var(--accent); }
#dpad-up { top: 0; left: 40px; }
#dpad-down { bottom: 0; left: 40px; }
#dpad-left { top: 40px; left: 0; }
#dpad-right { top: 40px; right: 0; }

#overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.75);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    backdrop-filter: blur(4px);
}
#overlay.hidden { display: none; }

#overlay-content {
    background: var(--bg2);
    border: 2px solid var(--panel-border);
    border-radius: 16px;
    padding: 28px 32px;
    max-width: 440px;
    width: 90%;
    text-align: center;
    font-size: 16px;
    line-height: 1.6;
}

#overlay-content h2 { color: var(--gold); margin-bottom: 12px; font-size: 24px; }
#overlay-content p { margin-bottom: 10px; }
#overlay-content button {
    background: var(--accent);
    border: none;
    color: white;
    padding: 10px 28px;
    border-radius: 10px;
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    margin: 6px;
    transition: transform 0.1s;
}
#overlay-content button:hover { transform: scale(1.05); }
#overlay-content .level-btn {
    width: 48px;
    height: 48px;
    padding: 0;
    margin: 4px;
    font-size: 18px;
    border-radius: 12px;
    position: relative;
}
#overlay-content .level-btn.locked {
    background: #333;
    cursor: not-allowed;
    opacity: 0.5;
}
#overlay-content .level-btn .stars {
    position: absolute;
    bottom: -2px;
    left: 0;
    right: 0;
    font-size: 9px;
    color: var(--gold);
}
.chapter-title {
    color: var(--green);
    font-size: 14px;
    margin-top: 14px;
    margin-bottom: 4px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
}
.level-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-bottom: 4px;
}

footer {
    text-align: center;
    padding: 8px;
    font-size: 12px;
    color: var(--text-dim);
    flex-shrink: 0;
}
footer a { color: var(--blue); text-decoration: none; }
footer a:hover { text-decoration: underline; }

@keyframes pulse {
    0%, 100% { text-shadow: 0 0 8px rgba(245,200,66,0.4); }
    50% { text-shadow: 0 0 18px rgba(245,200,66,0.8); }
}

@media (pointer: coarse) {
    #dpad { display: block; }
}

@media (max-width: 500px) {
    #hud { padding: 6px 8px; }
    #hud-center { font-size: 17px; }
    #expression-display { font-size: 13px; }
    #eval-steps { font-size: 11px; }
}