/* ===== King's Defense - Style ===== */
:root {
    --bg-primary: #0a0a1a;
    --bg-secondary: #12122a;
    --text-primary: #ffffff;
    --accent: #a855f7;
    --accent-gold: #ffd700;
    --accent-cyan: #00d4ff;
    --accent-pink: #ff6b9d;
    --accent-orange: #ff8c00;
    --accent-green: #22c55e;
    --accent-red: #ef4444;
}

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

body {
    font-family: 'Noto Sans KR', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    padding-bottom: 80px;
}

/* ===== Background Effects ===== */
.bg-effects { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 0; overflow: hidden; }
.bg-orb { position: absolute; border-radius: 50%; filter: blur(100px); opacity: 0.3; }
.bg-orb:nth-child(1) { width: 300px; height: 300px; background: #a855f7; top: -100px; right: -100px; animation: float 15s ease-in-out infinite; }
.bg-orb:nth-child(2) { width: 250px; height: 250px; background: #00d4ff; bottom: -80px; left: -80px; animation: float 18s ease-in-out infinite reverse; }

@keyframes float {
    0%, 100% { transform: translate(0, 0) scale(1); }
    33% { transform: translate(30px, -30px) scale(1.05); }
    66% { transform: translate(-20px, 20px) scale(0.95); }
}

/* ===== Container ===== */
.container { position: relative; z-index: 1; max-width: 900px; margin: 0 auto; padding: 12px; }

/* ===== Header ===== */
header { text-align: center; margin-bottom: 12px; }
.header-top { display: flex; align-items: center; justify-content: center; gap: 12px; margin-bottom: 4px; }
.back-btn { display: flex; align-items: center; gap: 4px; color: var(--accent-cyan); text-decoration: none; font-family: 'Jua', sans-serif; font-size: 0.9rem; position: absolute; left: 12px; transition: opacity 0.3s; }
.back-btn:hover { opacity: 0.7; }
.back-btn svg { width: 20px; height: 20px; }
h1 { font-family: 'Black Han Sans', sans-serif; font-size: 1.8rem; background: linear-gradient(135deg, var(--accent-gold), var(--accent-orange)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text; }
.subtitle { font-family: 'Jua', sans-serif; color: #94a3b8; font-size: 0.9rem; }

/* ===== Game Container ===== */
.game-container { width: 100%; margin: 0 auto; position: relative; }
#gameWrapper { position: relative; width: 100%; aspect-ratio: 5/4; background: #0f0f2a; border-radius: 8px; overflow: hidden; border: 1px solid rgba(168, 85, 247, 0.2); touch-action: none; }
#gameCanvas { width: 100%; height: 100%; display: block; image-rendering: pixelated; touch-action: none; }

/* ===== HUD ===== */
.hud { position: absolute; top: 0; left: 0; right: 0; pointer-events: none; z-index: 10; }
.hud-top { display: flex; justify-content: space-around; padding: 6px 8px; background: rgba(10, 10, 26, 0.85); border-bottom: 1px solid rgba(255, 255, 255, 0.1); pointer-events: auto; }
.hud-stat { display: flex; align-items: center; gap: 4px; font-family: 'Jua', sans-serif; font-size: 0.85rem; }
.hud-icon { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; border-radius: 4px; font-size: 0.7rem; font-weight: bold; }
.gold-icon { background: linear-gradient(135deg, #ffd700, #ff8c00); color: #0a0a1a; }
.life-icon { background: linear-gradient(135deg, #ef4444, #ff6b9d); color: #fff; font-size: 0.6rem; }
.wave-icon { background: linear-gradient(135deg, #a855f7, #c084fc); color: #fff; }
.score-icon { background: linear-gradient(135deg, #00d4ff, #7dd3fc); color: #0a0a1a; }

/* Speed Button */
.speed-btn {
    background: rgba(30, 30, 60, 0.8);
    color: #94a3b8;
    border: 1px solid rgba(100, 100, 150, 0.4);
    border-radius: 4px;
    padding: 2px 8px;
    font-family: 'Jua', sans-serif;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s;
    min-height: 24px;
    pointer-events: auto;
    touch-action: manipulation;
}
.speed-btn:hover { border-color: var(--accent-cyan); }
.speed-btn.speed-active {
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-orange));
    color: #0a0a1a;
    border-color: var(--accent-gold);
    font-weight: bold;
}

.hud-prepare { display: flex; align-items: center; justify-content: center; gap: 8px; padding: 4px 8px; background: rgba(10, 10, 26, 0.85); border-bottom: 1px solid rgba(255, 255, 255, 0.1); font-family: 'Jua', sans-serif; font-size: 0.85rem; pointer-events: auto; }
.wave-btn { background: linear-gradient(135deg, var(--accent-green), #16a34a); color: #fff; border: none; padding: 4px 12px; border-radius: 6px; font-family: 'Jua', sans-serif; font-size: 0.8rem; cursor: pointer; transition: all 0.3s; min-height: 32px; touch-action: manipulation; }
.wave-btn:hover { transform: scale(1.05); box-shadow: 0 0 12px rgba(34, 197, 94, 0.4); }

/* ===== Tower Panel ===== */
.tower-panel {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    z-index: 9999;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 8px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    background: rgba(10, 10, 26, 0.95);
    border-top: 1px solid rgba(168, 85, 247, 0.3);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.tower-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 56px;
    min-height: 52px;
    padding: 4px;
    background: rgba(30, 30, 60, 0.8);
    border: 2px solid rgba(100, 100, 150, 0.3);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    flex-shrink: 0;
    color: #fff;
    touch-action: manipulation;
}
.tower-btn:hover, .tower-btn.selected { border-color: var(--accent-cyan); box-shadow: 0 0 10px rgba(0, 212, 255, 0.3); }
.tower-btn.disabled { opacity: 0.4; cursor: not-allowed; }
.tower-preview { image-rendering: pixelated; }
.tower-name { font-family: 'Jua', sans-serif; font-size: 0.6rem; color: #e2e8f0; line-height: 1; }
.tower-cost { font-family: 'Jua', sans-serif; font-size: 0.7rem; color: var(--accent-gold); }
.tower-desc { font-family: 'Noto Sans KR', sans-serif; font-size: 0.5rem; color: #94a3b8; line-height: 1.1; text-align: center; max-width: 64px; }

/* Hero Abilities */
.hero-abilities { display: flex; gap: 4px; margin-left: auto; }
.ability-btn {
    position: relative;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(30, 30, 60, 0.8);
    border: 2px solid rgba(168, 85, 247, 0.4);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    overflow: hidden;
    flex-shrink: 0;
    touch-action: manipulation;
}
.ability-btn:hover { border-color: var(--accent-gold); box-shadow: 0 0 10px rgba(255, 215, 0, 0.3); }
.ability-btn.on-cooldown { opacity: 0.5; cursor: not-allowed; }
.ability-btn.active-targeting { border-color: var(--accent-gold); box-shadow: 0 0 15px rgba(255, 215, 0, 0.5); animation: pulse 0.8s ease-in-out infinite; }
.ability-icon { font-size: 1.1rem; z-index: 1; }
.ability-cd {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.7);
    height: 0%;
    transition: height 0.1s linear;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 10px rgba(255, 215, 0, 0.3); }
    50% { box-shadow: 0 0 20px rgba(255, 215, 0, 0.6); }
}

/* Active Skills */
.active-skills { display: flex; gap: 4px; margin-left: 4px; padding-left: 4px; border-left: 1px solid rgba(168, 85, 247, 0.3); }
.skill-btn {
    position: relative;
    width: 44px;
    height: 44px;
    border-radius: 8px;
    background: rgba(20, 20, 50, 0.9);
    border: 2px solid rgba(255, 140, 0, 0.4);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    overflow: hidden;
    flex-shrink: 0;
    touch-action: manipulation;
    gap: 1px;
}
.skill-btn:hover { border-color: #ff8c00; box-shadow: 0 0 10px rgba(255, 140, 0, 0.4); }
.skill-btn.on-cooldown { opacity: 0.4; cursor: not-allowed; }
.skill-btn.not-enough-gold { opacity: 0.5; }
.skill-btn.skill-active { border-color: #22c55e; box-shadow: 0 0 12px rgba(34, 197, 94, 0.5); }
.skill-icon { font-size: 1rem; z-index: 1; line-height: 1; }
.skill-cost { font-family: 'Jua', sans-serif; font-size: 0.5rem; color: var(--accent-gold); z-index: 1; line-height: 1; }

/* ===== Tower Popup ===== */
.tower-popup {
    position: absolute;
    z-index: 100;
    background: rgba(18, 18, 42, 0.95);
    border: 1px solid rgba(168, 85, 247, 0.4);
    border-radius: 8px;
    padding: 8px;
    min-width: 160px;
    backdrop-filter: blur(8px);
    pointer-events: auto;
}
.popup-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-family: 'Jua', sans-serif; }
#popupName { color: var(--accent-cyan); font-size: 0.9rem; }
#popupLevel { color: var(--accent-gold); font-size: 0.8rem; }
.popup-stats { margin-bottom: 6px; }
.popup-stat { display: flex; justify-content: space-between; font-size: 0.75rem; padding: 2px 0; color: #94a3b8; }
.popup-stat span:last-child { color: #fff; }
.popup-buttons { display: flex; gap: 4px; }
.popup-btn {
    flex: 1;
    padding: 4px 6px;
    border: none;
    border-radius: 6px;
    font-family: 'Jua', sans-serif;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 36px;
    justify-content: center;
    touch-action: manipulation;
}
.upgrade-btn { background: linear-gradient(135deg, var(--accent), #7c3aed); color: #fff; }
.upgrade-btn:hover { transform: scale(1.05); }
.upgrade-btn.max-level { background: #475569; cursor: not-allowed; }
.sell-btn { background: linear-gradient(135deg, var(--accent-red), #b91c1c); color: #fff; }
.sell-btn:hover { transform: scale(1.05); }

/* ===== Overlay Screens ===== */
.overlay-screen {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(10, 10, 26, 0.92);
    z-index: 50;
    backdrop-filter: blur(4px);
}
.hidden { display: none !important; }

.start-content, .gameover-content { text-align: center; padding: 20px; }
.game-logo {
    font-family: 'Black Han Sans', sans-serif;
    font-size: 2.5rem;
    line-height: 1.1;
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-orange), var(--accent-gold));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: none;
    margin-bottom: 8px;
    animation: shimmer 3s ease-in-out infinite;
}
@keyframes shimmer {
    0%, 100% { filter: brightness(1); }
    50% { filter: brightness(1.3); }
}
.start-sub { font-family: 'Jua', sans-serif; color: #94a3b8; margin-bottom: 16px; }

/* Map Select */
.map-select { display: flex; gap: 8px; justify-content: center; margin-bottom: 16px; flex-wrap: wrap; }
.map-card {
    width: 100px;
    padding: 10px 8px;
    background: rgba(30, 30, 60, 0.8);
    border: 2px solid rgba(100, 100, 150, 0.3);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    text-align: center;
}
.map-card:hover, .map-card.selected { border-color: var(--accent-gold); box-shadow: 0 0 12px rgba(255, 215, 0, 0.3); transform: translateY(-2px); }
.map-card.locked { opacity: 0.4; cursor: not-allowed; }
.map-name { font-family: 'Jua', sans-serif; font-size: 0.8rem; color: var(--accent-cyan); margin-bottom: 4px; }
.map-diff { font-size: 0.65rem; color: #94a3b8; }
.map-stars { color: var(--accent-gold); font-size: 0.7rem; margin-top: 4px; }
.map-lock { font-size: 1.5rem; }

.start-btn {
    background: linear-gradient(135deg, var(--accent), #7c3aed);
    color: #fff;
    border: none;
    padding: 10px 32px;
    border-radius: 8px;
    font-family: 'Jua', sans-serif;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.3s;
    min-height: 44px;
}
.start-btn:hover { transform: scale(1.05); box-shadow: 0 0 20px rgba(168, 85, 247, 0.4); }

/* Game Over / Victory */
.gameover-title { font-family: 'Black Han Sans', sans-serif; font-size: 2rem; color: var(--accent-red); margin-bottom: 12px; }
.victory-title { color: var(--accent-gold) !important; }
.gameover-stats { margin-bottom: 12px; }
.stat-row { display: flex; justify-content: space-between; padding: 4px 16px; font-family: 'Jua', sans-serif; font-size: 0.9rem; color: #94a3b8; }
.stat-row span:last-child { color: #fff; }
.gameover-score { font-family: 'Black Han Sans', sans-serif; font-size: 1.5rem; color: var(--accent-gold); margin-bottom: 16px; }
.gameover-score span:last-child { margin-left: 8px; }

.star-rating { font-size: 2rem; margin-bottom: 8px; letter-spacing: 4px; }

/* ===== Article ===== */
.game-content { background: var(--bg-secondary); border-radius: 12px; padding: 20px; margin-top: 16px; border: 1px solid rgba(255, 255, 255, 0.05); }
.game-content h2 { font-family: 'Black Han Sans', sans-serif; color: var(--accent-cyan); margin-bottom: 8px; font-size: 1.1rem; }
.game-content p { color: #94a3b8; line-height: 1.6; font-size: 0.85rem; margin-bottom: 8px; }
.tip-box { background: rgba(168, 85, 247, 0.1); border-left: 3px solid var(--accent); padding: 12px; border-radius: 0 8px 8px 0; }
.tip-box p { margin-bottom: 4px; }

/* ===== Footer ===== */
footer { text-align: center; padding: 16px; color: #475569; font-size: 0.8rem; margin-top: 16px; }
footer a { color: var(--accent-cyan); text-decoration: none; }

/* ===== Mobile ===== */
@media (max-width: 600px) {
    .container { padding: 8px; }
    header { margin-bottom: 8px; }
    h1 { font-size: 1.4rem; }
    .subtitle { font-size: 0.8rem; }
    #gameWrapper { aspect-ratio: auto; height: calc(100vh - 130px); height: calc(100dvh - 130px); border-radius: 4px; }
    .game-logo { font-size: 1.8rem; }
    .map-card { width: 80px; padding: 8px 6px; }
    .map-name { font-size: 0.7rem; }
    .hud-stat { font-size: 0.75rem; }
    .hud-icon { width: 16px; height: 16px; font-size: 0.6rem; }
    .tower-btn { min-width: 48px; min-height: 48px; }
    .tower-cost { font-size: 0.6rem; }
    .ability-btn { width: 40px; height: 40px; }
    .skill-btn { width: 40px; height: 40px; }
    .skill-cost { font-size: 0.45rem; }
    .instructions, .game-content, footer { display: none; }
    body { padding-bottom: 66px; }
}

@media (max-width: 400px) {
    .tower-btn { min-width: 42px; min-height: 44px; padding: 2px; }
    .tower-preview { width: 28px !important; height: 28px !important; }
    .ability-btn { width: 36px; height: 36px; }
    .skill-btn { width: 36px; height: 36px; }
}

/* ===== Fullscreen ===== */
body.fullscreen-active header,
body.fullscreen-active .game-content,
body.fullscreen-active footer,
body.fullscreen-active .subtitle { display: none; }
body.fullscreen-active .container { max-width: 100%; padding: 0; }
body.fullscreen-active #gameWrapper { aspect-ratio: auto; width: 100vw; height: 100vh; border-radius: 0; }

/* ===== Mobile Landscape: Flex row — game + tower sidebar ===== */
@media (orientation: landscape) and (max-height: 500px) {
    /* Hide non-game elements to use full viewport */
    body {
        padding: 0;
        overflow: hidden;
    }
    header, .subtitle, footer, .game-content, .bg-effects { display: none !important; }

    .container {
        max-width: 100%;
        padding: 0;
        margin: 0;
    }

    /* Override fullscreen.css centering so flex row works */
    body.fullscreen-active .container {
        display: block !important;
        padding: 0 !important;
        height: 100dvh !important;
        overflow: hidden !important;
        align-items: initial !important;
        justify-content: initial !important;
    }

    /* Flex row: HUD | canvas | tower panel */
    .game-container {
        display: flex;
        flex-direction: row;
        width: 100%;
        height: 100dvh;
        height: 100svh;
        padding-top: env(safe-area-inset-top, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* HUD: left sidebar */
    .hud {
        position: relative !important;
        top: auto !important;
        left: auto !important;
        right: auto !important;
        flex: 0 0 3rem;
        width: 3rem;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        background: rgba(10, 10, 26, 0.95);
        border-right: 1px solid rgba(168, 85, 247, 0.3);
        padding: 0.5rem 0.125rem;
        padding-left: calc(0.125rem + env(safe-area-inset-left, 0px));
        gap: 0.375rem;
        overflow-y: auto;
        overflow-x: hidden;
        pointer-events: auto;
        z-index: 10;
    }

    .hud-top {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 0.375rem;
        background: none;
        border-bottom: none;
        padding: 0;
        width: 100%;
        justify-content: flex-start;
    }

    .hud-stat {
        flex-direction: column;
        font-size: 0.6rem;
        text-align: center;
        gap: 1px;
        width: 100%;
        justify-content: center;
    }

    .hud-icon {
        width: 1.375rem;
        height: 1.375rem;
    }

    .speed-btn {
        font-size: 0.6rem;
        padding: 0.125rem 0.25rem;
        min-height: 1.375rem;
        width: 100%;
    }

    .hud-prepare {
        display: flex;
        flex-direction: column;
        align-items: center;
        font-size: 0.55rem;
        padding: 0.25rem 0.125rem;
        background: none;
        border-bottom: none;
        gap: 0.25rem;
        width: 100%;
    }

    .wave-btn {
        font-size: 0.55rem;
        padding: 0.25rem 0.25rem;
        min-height: 1.5rem;
        width: 100%;
    }

    /* Game canvas fills remaining width */
    #gameWrapper,
    body.fullscreen-active #gameWrapper {
        flex: 1 1 auto !important;
        width: auto !important;
        height: 100% !important;
        aspect-ratio: auto !important;
        border-radius: 0;
        min-width: 0;
    }

    /* Tower panel: right sidebar via flex (no fixed positioning needed) */
    .tower-panel {
        position: relative !important;
        top: auto !important;
        bottom: auto !important;
        left: auto !important;
        right: auto !important;
        flex: 0 0 4.5rem;
        width: 4.5rem;
        height: 100%;
        flex-direction: column;
        overflow-y: auto;
        overflow-x: hidden;
        border-top: none;
        border-left: 1px solid rgba(168, 85, 247, 0.3);
        padding: 0.375rem 0.25rem;
        padding-right: calc(0.25rem + env(safe-area-inset-right, 0px));
        align-items: center;
        gap: 0.25rem;
    }

    /* Tower buttons — rem-based sizing */
    .tower-btn {
        min-width: 3.25rem;
        width: 3.25rem;
        min-height: 2.75rem;
        padding: 0.125rem;
    }
    .tower-preview { width: 1.75rem !important; height: 1.75rem !important; }
    .tower-name { font-size: 0.5rem; }
    .tower-cost { font-size: 0.55rem; }
    .tower-desc { display: none; }

    /* Hero abilities: push to bottom */
    .hero-abilities {
        margin-left: 0;
        margin-top: auto;
        flex-direction: column;
        gap: 0.25rem;
    }
    .ability-btn { width: 2.5rem; height: 2.5rem; }
    .active-skills {
        margin-left: 0;
        flex-direction: column;
        gap: 0.25rem;
        padding-left: 0;
        border-left: none;
        padding-top: 0.25rem;
        border-top: 1px solid rgba(255, 140, 0, 0.3);
    }
    .skill-btn { width: 2.5rem; height: 2.5rem; }

    /* Fullscreen button: bottom-left of game canvas, reachable thumb */
    .fullscreen-btn {
        top: auto;
        right: auto;
        bottom: 0.5rem;
        left: 0.5rem;
    }
}
