/* ===== Reset & Base ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

:root {
    --bg-gradient-start: #0f0c29;
    --bg-gradient-mid: #302b63;
    --bg-gradient-end: #24243e;
    --panel-bg: rgba(255, 255, 255, 0.08);
    --panel-border: rgba(255, 255, 255, 0.15);
    --text-primary: #ffffff;
    --text-secondary: rgba(255, 255, 255, 0.6);
    --accent-green: #00d97e;
    --accent-green-glow: rgba(0, 217, 126, 0.4);
    --accent-red: #ff4757;
    --accent-orange: #ffa502;
    --accent-blue: #00a8ff;
    --accent-purple: #a55eea;
    --overlay-bg: rgba(15, 12, 41, 0.88);
    --canvas-bg: #1a1a2e;
    --grid-line: rgba(255, 255, 255, 0.03);
    --kbd-bg: rgba(255, 255, 255, 0.12);
    --kbd-border: rgba(255, 255, 255, 0.25);
    --opt-bg: rgba(255, 255, 255, 0.1);
    --opt-border: rgba(255, 255, 255, 0.25);
}

html, body {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, var(--bg-gradient-start) 0%, var(--bg-gradient-mid) 50%, var(--bg-gradient-end) 100%);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* ===== Game Wrapper ===== */
.game-wrapper {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    overflow: hidden;
}

/* ===== Back to Home (floating, above overlays) ===== */
.homebtn {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
    z-index: 60;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    background: rgba(255, 255, 255, 0.85);
    color: #2b3a55;
    text-decoration: none;
    font-size: 13px;
    font-weight: 800;
    padding: 8px 13px;
    border-radius: 999px;
    box-shadow: 0 3px 0 rgba(0, 0, 0, 0.12);
    font-family: "ZCOOL KuaiLe", "PingFang SC", sans-serif;
}
.homebtn svg { width: 16px; height: 16px; display: block; }
.homebtn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12); }

/* ===== Score Panel ===== */
.score-panel {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    /* 左端让出左上角“返回主页”按钮，避免重叠 */
    left: max(104px, calc(env(safe-area-inset-left) + 100px));
    right: max(10px, env(safe-area-inset-right));
    transform: none;
    z-index: 50;
    display: flex;
    gap: 12px;
    width: auto;
}

.score-item {
    flex: 1;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    padding: 8px 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.score-item.bump {
    transform: scale(1.08);
    box-shadow: 0 0 24px var(--accent-green-glow);
}

.score-label {
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 2px;
    color: var(--text-secondary);
}

.score-value {
    font-size: 24px;
    font-weight: 800;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    transition: color 0.3s ease;
}

.score-value.highlight {
    color: var(--accent-green);
}

/* ===== Canvas Container ===== */
.canvas-container {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    background: transparent;
    overflow: visible;
    touch-action: none;
}

#gameCanvas {
    position: fixed;
    inset: 0;
    width: 100dvw;
    height: 100dvh;
    display: block;
    background: var(--canvas-bg);
    touch-action: none;
    z-index: 0;
}

/* ===== Overlays ===== */
.overlay {
    position: absolute;
    inset: 0;
    background: var(--overlay-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 10;
    overflow-y: auto;
    scrollbar-width: none;        /* Firefox 隐藏滚动条 */
    -ms-overflow-style: none;
}

.overlay::-webkit-scrollbar { display: none; }  /* WebKit 隐藏滚动条 */

.overlay.hidden {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
}

.overlay-content {
    text-align: center;
    padding: 16px;
    animation: fadeInUp 0.4s ease;
    width: 100%;
    max-height: 100%;
}

@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.game-logo {
    font-size: 44px;
    margin-bottom: 6px;
    animation: bounce 2s ease infinite;
}

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

.overlay-title {
    font-size: clamp(26px, 6.5vw, 34px);
    font-weight: 800;
    margin-bottom: 4px;
    letter-spacing: -1px;
    background: linear-gradient(135deg, var(--accent-green), var(--accent-blue));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.overlay-subtitle {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.choose-tip {
    font-size: 14px;
    font-weight: 600;
    color: var(--accent-orange);
    margin-bottom: 10px;
}

/* ===== Subject Select ===== */
.subject-grid {
    display: flex;
    gap: 10px;
    justify-content: center;
    align-items: stretch;
    margin: 0 auto 14px;
}

.subject-btn {
    flex: 1 1 0;
    max-width: 130px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 14px 8px;
    font-family: inherit;
    background: var(--opt-bg);
    border: 1px solid var(--opt-border);
    border-radius: 16px;
    color: var(--text-primary);
    cursor: pointer;
    text-align: center;
    transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
    touch-action: manipulation;
}

.subject-btn:active {
    transform: scale(0.97);
}

.subject-math:active, .subject-math:hover { background: rgba(255, 165, 2, 0.2); box-shadow: 0 0 20px rgba(255, 165, 2, 0.3); }
.subject-chinese:active, .subject-chinese:hover { background: rgba(255, 71, 87, 0.2); box-shadow: 0 0 20px rgba(255, 71, 87, 0.3); }
.subject-english:active, .subject-english:hover { background: rgba(0, 168, 255, 0.2); box-shadow: 0 0 20px rgba(0, 168, 255, 0.3); }

.subject-icon { width:38px; height:38px; margin:0 auto 6px; display:flex; align-items:center; justify-content:center; } .subject-icon svg { width:100%; height:100%; }

.subject-name {
    font-size: 17px;
    font-weight: 700;
}

.subject-desc {
    font-size: 11px;
    color: var(--text-secondary);
}

/* ===== Speed selector ===== */
.speed-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-bottom: 16px;
}

.speed-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-right: 4px;
}

.speed-btn {
    font-family: inherit;
    font-size: 14px;
    font-weight: 700;
    color: var(--text-primary);
    background: var(--opt-bg);
    border: 1px solid var(--opt-border);
    border-radius: 999px;
    padding: 7px 18px;
    cursor: pointer;
    touch-action: manipulation;
    transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.speed-btn:active { transform: scale(0.95); }

.speed-btn.active {
    background: rgba(0, 217, 126, 0.25);
    border-color: var(--accent-green);
    color: var(--accent-green);
}

/* ===== Controls Hint ===== */
.controls-hint {
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
}

.control-row {
    font-size: 12px;
    color: var(--text-secondary);
}

/* ===== Question Overlay ===== */
.question-badge {
    display: inline-block;
    padding: 6px 18px;
    border-radius: 999px;
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 14px;
    letter-spacing: 2px;
}

.question-badge.subject-math { background: rgba(255, 165, 2, 0.25); color: var(--accent-orange); }
.question-badge.subject-chinese { background: rgba(255, 71, 87, 0.25); color: var(--accent-red); }
.question-badge.subject-english { background: rgba(0, 168, 255, 0.25); color: var(--accent-blue); }

.question-card {
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 20px;
    padding: 22px 18px;
    max-width: 380px;
    margin: 0 auto;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    transition: box-shadow 0.2s ease;
}

.question-card.correct-glow {
    box-shadow: 0 0 30px rgba(0, 217, 126, 0.5);
    border-color: var(--accent-green);
}

.question-card.wrong-shake {
    animation: shake 0.4s ease;
    box-shadow: 0 0 30px rgba(255, 71, 87, 0.5);
    border-color: var(--accent-red);
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-8px); }
    40% { transform: translateX(8px); }
    60% { transform: translateX(-5px); }
    80% { transform: translateX(5px); }
}

.question-text {
    font-size: clamp(20px, 5.5vw, 26px);
    font-weight: 700;
    line-height: 1.5;
    margin-bottom: 18px;
    min-height: 2em;
    display: flex;
    align-items: center;
    justify-content: center;
}

.options-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.option-btn {
    padding: 15px 10px;
    font-family: inherit;
    font-size: 20px;
    font-weight: 800;
    color: var(--text-primary);
    background: var(--opt-bg);
    border: 3px solid var(--opt-border);
    border-radius: 14px;
    cursor: pointer;
    transition: transform 0.1s ease, background 0.2s ease, border-color 0.2s ease;
    touch-action: manipulation;
    word-break: break-word;
}

.option-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.option-btn:disabled {
    cursor: default;
    opacity: 0.55;
}

.option-btn.correct {
    background: rgba(0, 217, 126, 0.3);
    border-color: var(--accent-green);
    opacity: 1;
}

.option-btn.wrong {
    background: rgba(255, 71, 87, 0.3);
    border-color: var(--accent-red);
    opacity: 1;
}

.question-tip {
    font-size: 14px;
    color: var(--text-secondary);
    margin-top: 14px;
    min-height: 1.4em;
}

.question-tip.tip-success { color: var(--accent-green); font-weight: 600; }
.question-tip.tip-fail { color: var(--accent-red); font-weight: 600; }

/* ===== Buttons ===== */
.btn-primary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 13px 34px;
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    color: var(--text-primary);
    background: linear-gradient(135deg, var(--accent-green), #00b894);
    border: none;
    border-radius: 14px;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.2s ease, filter 0.2s ease;
    box-shadow: 0 4px 20px var(--accent-green-glow);
    touch-action: manipulation;
}

.btn-primary:hover { transform: translateY(-2px); filter: brightness(1.1); }
.btn-primary:active { transform: scale(0.97); }

.btn-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 13px 34px;
    font-family: inherit;
    font-size: 16px;
    font-weight: 700;
    color: var(--text-primary);
    background: var(--opt-bg);
    border: 1px solid var(--opt-border);
    border-radius: 14px;
    cursor: pointer;
    transition: transform 0.15s ease, background 0.2s ease;
    touch-action: manipulation;
}

.btn-secondary:active { transform: scale(0.97); }

.btn-revive {
    background: linear-gradient(135deg, var(--accent-orange), #ff6348);
    box-shadow: 0 4px 20px rgba(255, 165, 2, 0.4);
}

.gameover-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: stretch;
    max-width: 280px;
    margin: 0 auto;
}

.btn-revive:disabled {
    opacity: 0.4;
    cursor: default;
    filter: grayscale(1);
}

.revive-count {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 12px;
}

/* ===== Final Score ===== */
.final-score {
    display: flex;
    gap: 32px;
    justify-content: center;
    margin-bottom: 14px;
}

.final-score-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.final-score-label {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 1.5px;
    color: var(--text-secondary);
}

.final-score-value {
    font-size: 34px;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
}

.new-record {
    font-size: 16px;
    font-weight: 700;
    color: var(--accent-orange);
    margin-bottom: 16px;
    animation: pulse 1s ease infinite;
}

.new-record.hidden { display: none; }

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

/* ===== Touch D-Pad ===== */
.dpad {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: max(30px, calc(env(safe-area-inset-bottom) + 28px));
    z-index: 50;
    display: none;
    grid-template-columns: repeat(3, 72px);
    grid-template-rows: repeat(2, 60px);
    gap: 10px;
    justify-content: center;
}

.dpad-btn {
    font-size: 20px;
    color: var(--text-primary);
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    border-radius: 14px;
    cursor: pointer;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    touch-action: manipulation;
    transition: background 0.1s ease, transform 0.1s ease;
}

.dpad-btn:active {
    background: rgba(0, 217, 126, 0.25);
    transform: scale(0.92);
}

.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }

/* Show D-pad only during gameplay (when body has .in-game), on touch devices and small screens */
@media (pointer: coarse), (max-width: 820px) {
    body.in-game .dpad { display: grid; }
    body.in-game .touch-hint { display: block; }
}

/* ===== Footer ===== */
.footer {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: max(4px, env(safe-area-inset-bottom));
    z-index: 50;
    font-size: 11px;
    color: var(--text-secondary);
    text-align: center;
    width: 100%;
}

/* ===== Touch hint (mobile) ===== */
.touch-hint {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: max(170px, calc(env(safe-area-inset-bottom) + 168px));
    z-index: 50;
    display: none;
    width: 100%;
    font-size: 13px;
    font-weight: 600;
    color: var(--accent-green);
    text-align: center;
}

/* ===== Screen Shake (game over) ===== */
.canvas-container.shake { animation: shake 0.4s ease; }

/* ===== Responsive ===== */
@media (max-width: 560px) {
    .score-value { font-size: 20px; }
    .final-score-value { font-size: 28px; }
}

/* ===== Spell（单词拼写填空） ===== */
.spell-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    width: 100%;
    grid-column: 1 / -1;
}
.spell-prompt {
    font-size: clamp(18px, 5vw, 24px);
    font-weight: 800;
    color: var(--text-primary);
    margin-bottom: 2px;
}
.spell-input {
    width: 100%;
    max-width: 280px;
    font-family: inherit;
    font-size: 26px;
    font-weight: 800;
    text-align: center;
    letter-spacing: 4px;
    color: var(--accent-green);
    background: rgba(255, 255, 255, 0.1);
    border: 3px solid var(--accent-green);
    border-radius: 14px;
    padding: 10px;
    outline: none;
}
.spell-kbd {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 6px;
    width: 100%;
    max-width: 340px;
}
.spell-key {
    font-family: inherit;
    font-size: 18px;
    font-weight: 800;
    color: var(--text-primary);
    background: var(--opt-bg);
    border: 2px solid var(--opt-border);
    border-radius: 10px;
    padding: 11px 0;
    cursor: pointer;
    touch-action: manipulation;
}
.spell-key:active { transform: scale(0.93); background: rgba(0, 217, 126, 0.25); }
.spell-row { display: flex; gap: 10px; margin-top: 4px; width: 100%; max-width: 340px; }
.spell-del { flex: 1; min-height: 46px; }
.spell-submit-row { display: flex; width: 100%; max-width: 340px; margin-top: 8px; }
.spell-submit { flex: 1; min-height: 58px; padding: 14px 10px; font-size: 22px; font-weight: 800;
    color: #fff; background: var(--accent-green); border: none; border-radius: 14px;
    cursor: pointer; touch-action: manipulation; box-shadow: 0 4px 0 rgba(0,0,0,.18); }
.spell-submit:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,.18); }

/* 拼写填空：缺失字母模板（已知字母绿底 / 缺失位虚线框） */
.spell-mask { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; margin: 4px 0 2px; max-width: 340px; }
.spell-cell { width: 32px; height: 40px; display: flex; align-items: center; justify-content: center;
    font-size: 24px; font-weight: 800; color: var(--accent-green); background: rgba(0, 217, 126, 0.12);
    border: 2px solid rgba(0, 217, 126, 0.4); border-radius: 8px; }
.spell-cell.miss { background: #fff; border: 2px dashed #f0a93a; }
/* 选择题里的下划线占位框 */
.u-box { display: inline-block; min-width: 0.7em; border-bottom: 3px solid currentColor; margin: 0 2px; }

/* 短视口（横屏）下拼写题中元素压缩，避免确认键被挤出屏幕 */
@media (max-height: 560px) {
    .question-card {
        max-height: calc(100vh - 20px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding: 10px 12px;
    }
    .question-card.spell-mode .question-text { display: none; }
    .question-text { font-size: 18px; margin-bottom: 10px; min-height: auto; }
    .spell-area { gap: 6px; }
    .spell-prompt { font-size: 16px; }
    .spell-mask { gap: 4px; margin: 2px 0; }
    .spell-cell { width: 26px; height: 32px; font-size: 18px; }
    .spell-input { font-size: 20px; padding: 6px; max-width: 240px; }
    .spell-kbd { gap: 4px; max-width: 320px; grid-template-columns: repeat(9, 1fr); }
    .spell-key { padding: 5px 0; font-size: 15px; }
    .spell-row { margin-top: 2px; gap: 6px; }
    .spell-del { min-height: 38px; }
    .spell-submit-row { margin-top: 4px; }
    .spell-submit { min-height: 38px; font-size: 18px; padding: 8px; }
    .question-tip { margin-top: 8px; display: none; }
}

/* 横屏/短视口：开始界面紧凑，避免底部控制提示溢出 */
@media (max-height: 460px) {
  .game-logo { font-size: 32px; margin-bottom: 2px; }
  .overlay-content { padding: 10px 14px; }
  .overlay-title { margin-bottom: 2px; }
  .overlay-subtitle { margin-bottom: 4px; }
  .choose-tip { margin-bottom: 6px; font-size: 13px; }
  .speed-row { margin-bottom: 8px; gap: 6px; }
  .speed-btn { padding: 5px 12px; font-size: 12px; }
  .subject-grid { gap: 8px; margin-bottom: 10px; }
  .subject-btn { padding: 10px 6px; gap: 4px; border-radius: 12px; }
  .subject-icon { width: 32px; height: 32px; margin-bottom: 3px; }
  .subject-name { font-size: 14px; }
  .subject-desc { font-size: 10px; }
  .controls-hint { gap: 2px; }
  .control-row { font-size: 11px; }
}
