/* ===============================
   數學冒險樂園 - 復古掌機 RPG 風格
   以正體中文介面呈現，並調整排版避免重疊
   =============================== */

* {
    box-sizing: border-box;
}

:root {
    --battle-panel-min-height: 320px;
    --battle-panel-min-height-xl: 380px;
    --fighter-avatar-size: clamp(2.8rem, 6vw, 4rem);
}

body {
    margin: 0;
    font-family: "Noto Sans TC", "Microsoft JhengHei", sans-serif;
    background:
        radial-gradient(circle at 12% 10%, #7dd3fc 0, #7dd3fc00 32%),
        radial-gradient(circle at 86% 84%, #bfdbfe 0, #bfdbfe00 36%),
        linear-gradient(180deg, #99f6e4 0%, #67e8f9 40%, #a7f3d0 100%);
    color: #172554;
    min-height: 100vh;
}

.container {
    width: min(1320px, 100%);
    margin: 0 auto;
    padding: clamp(12px, 2vh, 20px) clamp(10px, 2.2vw, 24px) clamp(16px, 2.5vh, 26px) clamp(10px, 2.2vw, 24px);
}

h1 {
    margin: 0 0 8px;
    text-align: center;
    font-size: clamp(1.5rem, 2.5vw, 1.9rem);
    color: #0f172a;
    text-shadow: 0 2px 0 #ffffffb3;
}

h2 {
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1.04rem;
    color: #0f172a;
}

.subtitle {
    margin: 0 0 10px;
    text-align: center;
    color: #1e3a8a;
    font-size: clamp(0.9rem, 1.8vw, 1rem);
    font-weight: 700;
}

.toolbar,
.actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

.mode-info {
    margin: 0;
    font-weight: 600;
    color: #1e3a8a;
}

.panel {
    background: linear-gradient(180deg, #f8fafc, #eff6ff);
    border: 3px solid #334155;
    border-radius: 12px;
    padding: 10px;
    margin-bottom: 10px;
    box-shadow: inset 0 0 0 3px #dbeafe, 0 4px 0 #1e293b33;
}

#game > * {
    min-width: 0;
}

.panel p,
.mode-info,
#leaderboard-list li,
.badge-desc {
    overflow-wrap: anywhere;
    word-break: break-word;
}

#score-board {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    gap: 8px;
}

#score-board p {
    margin: 0;
    font-weight: 700;
    border: 2px solid #33415533;
    border-radius: 8px;
    padding: 6px 8px;
    background: #ffffffcc;
}

#question {
    font-size: clamp(1.1rem, 2.1vw, 1.3rem);
    font-weight: 800;
    margin: 0 0 10px;
}

button,
input {
    font-size: clamp(0.92rem, 1.8vw, 1rem);
    border-radius: 8px;
    border: 2px solid #334155;
    padding: 8px 10px;
}

button {
    cursor: pointer;
    font-weight: 700;
    background: linear-gradient(180deg, #fef9c3, #fde68a);
    color: #1f2937;
    box-shadow: 0 2px 0 #92400e40;
    touch-action: manipulation;
}

button:hover {
    background: linear-gradient(180deg, #fef08a, #facc15);
}

input {
    width: 180px;
    background: #ffffff;
}

.feedback {
    border-color: #166534;
    background: linear-gradient(180deg, #f0fdf4, #dcfce7);
}

#feedback {
    margin: 0;
    font-weight: 700;
    color: #166534;
}

#story-title {
    margin: 0 0 8px;
    font-weight: 800;
    color: #0c4a6e;
}

#story-desc,
#story-progress {
    margin: 0 0 6px;
    color: #334155;
    font-weight: 600;
}

#story-progress {
    margin-bottom: 0;
}

#battle-panel {
    background:
        linear-gradient(180deg, #e0f2fe 0 52%, #fef3c7 52% 100%);
    border-color: #0f172a;
    min-height: var(--battle-panel-min-height);
    padding: 14px;
}

#battle-panel.player-turn {
    box-shadow: inset 0 0 0 3px #93c5fd, 0 0 0 2px #1d4ed8;
}

#battle-panel.enemy-turn {
    box-shadow: inset 0 0 0 3px #fda4af, 0 0 0 2px #be123c;
}

#battle-panel.impact {
    animation: battle-impact 260ms ease;
}

.battlefield {
    display: grid;
    grid-template-columns: repeat(2, minmax(200px, 1fr));
    gap: 12px;
    position: relative;
    overflow: hidden;
    border-radius: 10px;
    padding: 4px;
}

.battlefield::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: 10px;
    background:
        radial-gradient(circle at 18% 30%, #ffffff99 0, #ffffff00 28%),
        radial-gradient(circle at 82% 74%, #fef9c3cc 0, #fef9c300 30%);
    background-size: 120% 120%;
    background-position: 0% 50%;
    animation: battlefield-flow 2.4s linear infinite;
}

.battlefield > * {
    position: relative;
    z-index: 1;
}

.fighter-card {
    border: 3px solid #0f172a;
    border-radius: 10px;
    padding: 10px;
    background: #ffffff;
    transition: transform 180ms ease;
    box-shadow: inset 0 0 0 2px #dbeafe;
    min-height: 160px;
}

.fighter-card.hero {
    align-self: end;
    background: linear-gradient(135deg, #dbeafe, #f8fafc);
}

.fighter-card.enemy {
    align-self: start;
    background: linear-gradient(135deg, #fee2e2, #fef2f2);
}

.fighter-name {
    margin: 0 0 6px;
    font-weight: 800;
}

.fighter-avatar {
    margin: 0 0 8px;
    font-size: var(--fighter-avatar-size);
    line-height: 1;
    filter: drop-shadow(0 2px 0 #0f172a33);
}

.hp-wrap p {
    margin: 6px 0 0;
    font-weight: 700;
}

.hp-bar {
    width: 100%;
    height: 14px;
    border-radius: 999px;
    background: #dcfce7;
    border: 2px solid #166534;
    overflow: hidden;
}

.hp-bar.enemy {
    background: #ffe4e6;
    border-color: #9f1239;
}

.hp-bar span {
    display: block;
    height: 100%;
    width: 100%;
    background: #22c55e;
    transition: width 220ms ease;
}

.hp-bar.enemy span {
    background: #f43f5e;
}

#battle-log {
    margin: 10px 0 0;
    font-weight: 700;
    color: #1e3a8a;
    font-size: clamp(0.96rem, 1.8vw, 1.08rem);
}

#turn-indicator {
    margin: 0 0 10px;
    font-weight: 800;
    color: #1e40af;
}

.equipment-layout {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
}

.equip-title {
    margin: 0 0 8px;
    font-weight: 800;
}

.equip-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.slot-grid {
    display: grid;
    gap: 8px;
}

.equip-item,
.equip-slot {
    border: 2px dashed #475569;
    border-radius: 8px;
    background: #f8fafc;
    padding: 8px;
    min-height: 58px;
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.equip-item {
    cursor: grab;
    border-style: solid;
    border-color: #334155;
    font-weight: 700;
}

.equip-item small {
    display: block;
    margin-top: 4px;
    color: #475569;
    font-size: 0.8rem;
}

.equip-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
    text-align: center;
}

.drop-zone.drag-over {
    border-color: #ca8a04;
    background: #fef9c3;
}

.stats-panel {
    margin-top: 10px;
    display: grid;
    grid-template-columns: repeat(2, minmax(140px, 1fr));
    gap: 6px;
}

.stats-panel p {
    margin: 0;
    font-weight: 700;
}

.fighter-card.attacking {
    animation: attacker-sway 280ms ease;
}

.fighter-card.hit {
    animation: hit-shake 260ms ease;
}

@keyframes attacker-sway {
    0% { transform: translateX(0) scale(1); }
    40% { transform: translateX(8px) scale(1.02); }
    100% { transform: translateX(0) scale(1); }
}

@keyframes hit-shake {
    0% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(4px); }
    100% { transform: translateX(0); }
}

@keyframes battlefield-flow {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

@keyframes battle-impact {
    0% { transform: translateX(0) scale(1); }
    25% { transform: translateX(-2px) scale(1.005); }
    50% { transform: translateX(2px) scale(1.01); }
    100% { transform: translateX(0) scale(1); }
}

.touch-keypad {
    margin-top: 10px;
    display: grid;
    grid-template-columns: repeat(3, minmax(64px, 1fr));
    gap: 6px;
    max-width: 280px;
}

.touch-keypad button {
    min-height: 42px;
    font-size: 0.96rem;
}

#badge-hint,
#leaderboard-hint {
    margin-top: 0;
    color: #475569;
    font-size: 0.95rem;
}

.badge-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.badge-item {
    border: 2px dashed #64748b;
    border-radius: 8px;
    padding: 8px 10px;
    background: #f8fafc;
}

.badge-item.unlocked {
    border-style: solid;
    border-color: #ca8a04;
    background: #fef9c3;
}

.badge-name {
    margin: 0 0 4px;
    font-weight: 800;
}

.badge-desc {
    margin: 0;
    font-size: 0.92rem;
    color: #475569;
}

.leaderboard-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 8px;
}

#player-name {
    width: 250px;
}

#leaderboard-list {
    margin: 0;
    padding-left: 20px;
}

#leaderboard-list li {
    margin-bottom: 6px;
    font-weight: 700;
}

#fx-layer {
    position: relative;
    height: 0;
}

@media (min-width: 720px) {
    #game {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-template-areas:
            "actions actions"
            "toolbar toolbar"
            "score question"
            "battle question"
            "equipment story"
            "feedback feedback"
            "badge leaderboard"
            "fx fx";
        gap: 10px;
        align-items: start;
    }

    .actions {
        grid-area: actions;
        margin-bottom: 0;
    }

    .toolbar {
        grid-area: toolbar;
        margin-bottom: 0;
    }

    #score-board {
        grid-area: score;
    }

    #battle-panel {
        grid-area: battle;
    }

    #equipment-panel {
        grid-area: equipment;
    }

    #story-panel {
        grid-area: story;
    }

    #question-area {
        grid-area: question;
    }

    #feedback-area {
        grid-area: feedback;
    }

    #badge-panel {
        grid-area: badge;
    }

    #leaderboard-panel {
        grid-area: leaderboard;
    }

    #fx-layer {
        grid-area: fx;
    }

    .panel {
        margin-bottom: 0;
    }
}

@media (min-width: 980px) {
    #game {
        grid-template-columns: repeat(2, minmax(320px, 1fr));
        grid-template-areas:
            "actions actions"
            "toolbar toolbar"
            "score question"
            "battle question"
            "equipment question"
            "story feedback"
            "badge leaderboard"
            "fx fx";
        gap: 10px;
        align-items: start;
    }

    #question-area {
        position: static;
    }
}

@media (min-width: 1200px) {
    #game {
        grid-template-columns: repeat(2, minmax(360px, 1fr));
        grid-template-areas:
            "actions actions"
            "toolbar toolbar"
            "score question"
            "battle battle"
            "equipment story"
            "feedback feedback"
            "badge leaderboard"
            "fx fx";
        gap: 12px;
    }

    #battle-panel {
        min-height: var(--battle-panel-min-height-xl);
    }
}

@media (max-width: 1100px) {
    .equipment-layout,
    .badge-list {
        grid-template-columns: 1fr;
    }
}

.pop-star {
    position: absolute;
    font-size: 1.6rem;
    animation: star-pop 800ms ease-out forwards;
}

@keyframes star-pop {
    0% {
        transform: translate(0, 0) scale(0.6);
        opacity: 0;
    }
    25% {
        opacity: 1;
    }
    100% {
        transform: translate(var(--x), var(--y)) scale(1.2);
        opacity: 0;
    }
}

.level-up-flash {
    animation: level-flash 850ms ease;
}

@keyframes level-flash {
    0% {
        transform: scale(1);
        color: inherit;
    }
    40% {
        transform: scale(1.35);
        color: #b45309;
    }
    100% {
        transform: scale(1);
        color: inherit;
    }
}

@media (max-width: 620px) {
    .container {
        /* 先給固定值，再用 env() 疊加安全區，作為不支援 env() 的回退寫法 */
        padding-bottom: 14px;
        padding-bottom: calc(14px + env(safe-area-inset-bottom));
    }

    #score-board {
        grid-template-columns: 1fr;
    }

    .battlefield,
    .equipment-layout,
    .stats-panel {
        grid-template-columns: 1fr;
    }

    .badge-list {
        grid-template-columns: 1fr;
    }

    #player-name {
        width: 100%;
    }

    .touch-keypad {
        max-width: 100%;
    }
}

@media (max-height: 820px) {
    .container {
        padding-top: 8px;
    }

    .panel {
        padding: 8px;
    }
}
