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

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    background:
        radial-gradient(ellipse at top, rgba(99, 102, 241, 0.10) 0%, transparent 60%),
        radial-gradient(ellipse at bottom, rgba(220, 38, 38, 0.08) 0%, transparent 55%),
        linear-gradient(135deg, #1e1b4b 0%, #0b1220 50%, #1a1730 100%);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    color: #fff;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: none;
}

#loading {
    position: fixed; inset: 0;
    display: flex; align-items: center; justify-content: center;
    background: linear-gradient(180deg, #1e1b4b 0%, #0b1220 100%);
    z-index: 100;
}
.loader-box { text-align: center; width: min(80vw, 380px); }
.loader-title {
    font-size: clamp(1.6rem, 6vw, 2.4rem);
    font-weight: 900;
    margin-bottom: 1.5rem;
}
.loader-bar {
    height: 14px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 1rem;
}
.loader-fill {
    height: 100%; width: 0%;
    background: linear-gradient(90deg, #ef4444, #f59e0b);
    transition: width 0.3s ease;
    border-radius: 8px;
}
.loader-tip { font-size: 0.95rem; opacity: 0.7; }

#game-wrap {
    position: fixed; inset: 0;
    height: 100vh;       /* fallback */
    height: 100dvh;      /* dynamic viewport — robusto contra toolbars/safe-areas */
    display: flex; flex-direction: column;
    overflow: hidden;    /* nunca permite scroll vertical */
}
/* Em mobile NÃO reservamos espaço pros touch-controls — eles SOBREPÕEM o canvas
   com pointer-events: auto só nos elementos interativos (joystick, bomb btn).
   Isso permite canvas usar altura inteira em iframes pequenos do Poki/CrazyGames. */
#hud {
    width: 100%;
    flex-shrink: 0;
}
#game {
    flex: 1 !important;
    min-height: 0 !important;
    width: 100% !important;
    height: auto !important;     /* sobrescreve Phaser que seta height:100% inline */
    display: flex !important;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    box-shadow: 0 0 80px rgba(0,0,0,0.6);
    /* Margem segura pra evitar canvas cortar embaixo (taskbar Windows, safe-area iOS) */
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
}
#game canvas {
    display: block !important;
    width: auto !important;
    height: auto !important;
    /* Limita absolutamente baseado em viewport, não no flex parent (que Phaser pode mexer).
       100px = HUD + padding seguro. 1.182 = aspect ratio 832/704. */
    max-height: calc(100dvh - 100px) !important;
    max-width: min(100%, calc((100dvh - 100px) * 1.182)) !important;
    aspect-ratio: 832 / 704 !important;
    margin: 0 auto !important;
    position: relative !important;
    inset: auto !important;
    transform: none !important;
    object-fit: contain;
}

/* Em mobile, touch-controls SOBREPÕEM o canvas (não reservam espaço).
   Canvas usa altura inteira pra ficar grande. Touch-controls são translúcidos. */

#hud {
    flex-shrink: 0;
    padding: 10px 14px;
    display: flex; justify-content: space-between; align-items: center;
    background: linear-gradient(180deg, rgba(8,12,22,0.97) 0%, rgba(20,16,52,0.92) 100%);
    border-bottom: 2px solid rgba(253, 224, 71, 0.22);
    box-shadow: 0 4px 18px rgba(0,0,0,0.5);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 10;
}
#hud-left { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Stage indicator (mapa atual) */
#stage-box {
    display: flex; align-items: center; gap: 8px;
    background: linear-gradient(135deg, rgba(76,29,149,0.55) 0%, rgba(11,18,32,0.85) 100%);
    border: 1px solid rgba(168, 85, 247, 0.32);
    padding: 6px 14px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
}
#stage-box .stage-emoji {
    font-size: 22px;
}
#stage-box .stage-name {
    font-weight: 900;
    font-size: 16px;
    color: #fde047;
    letter-spacing: 0.5px;
    text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

/* HUD cards: cada um com sua cor temática */
#score-box, #blocks-box {
    background: linear-gradient(135deg, rgba(31,41,55,0.92) 0%, rgba(11,18,32,0.92) 100%);
    border: 1px solid rgba(251, 191, 36, 0.28);
    border-radius: 12px;
    padding: 6px 12px;
    display: flex; flex-direction: column;
    line-height: 1.05;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
}
.hud-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 1.4px;
    opacity: 0.65;
    font-weight: 700;
}
#time-left, #blocks-left {
    font-size: 22px;
    font-weight: 900;
    color: #fbbf24;
    text-shadow: 0 1px 0 rgba(0,0,0,0.4);
    margin-top: 2px;
}

#coins-box {
    display: flex; align-items: center; gap: 6px;
    background: linear-gradient(135deg, rgba(146,64,14,0.55) 0%, rgba(11,18,32,0.85) 100%);
    border: 1px solid rgba(253, 224, 71, 0.35);
    padding: 8px 14px;
    border-radius: 12px;
    color: #fde047;
    font-weight: 900;
    font-size: 18px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
}

#lives-box {
    display: flex; gap: 4px;
    background: linear-gradient(135deg, rgba(127,29,29,0.55) 0%, rgba(11,18,32,0.85) 100%);
    border: 1px solid rgba(239, 68, 68, 0.32);
    padding: 8px 12px;
    border-radius: 12px;
    align-items: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
}
.heart-cell {
    color: #ef4444;
    font-size: 18px;
    transition: opacity 0.2s, transform 0.2s;
    text-shadow: 0 0 6px rgba(239,68,68,0.5);
}
.heart-cell.lost {
    opacity: 0.18;
    filter: grayscale(1);
    transform: scale(0.8);
    color: #94a3b8;
    text-shadow: none;
}

#stats-box {
    display: flex; gap: 8px;
    background: linear-gradient(135deg, rgba(76,29,149,0.55) 0%, rgba(11,18,32,0.85) 100%);
    border: 1px solid rgba(168, 85, 247, 0.32);
    padding: 8px 14px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 900;
    color: #fff;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
}
.stat { white-space: nowrap; }
.coin-icon {
    display: inline-block;
    width: 18px; height: 18px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, #fef3c7 0%, #fbbf24 40%, #d97706 100%);
    border: 2px solid #92400e;
    box-shadow: 0 0 8px rgba(251, 191, 36, 0.7);
    flex-shrink: 0;
}

#hud-right { display: flex; gap: 8px; pointer-events: auto; }
#mute, #home {
    width: 44px; height: 44px;
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(31,41,55,0.92) 0%, rgba(11,18,32,0.92) 100%);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4), inset 0 1px 0 rgba(255,255,255,0.06);
    transition: transform 0.1s, box-shadow 0.1s;
}
#mute:hover, #home:hover { transform: translateY(-1px); box-shadow: 0 4px 10px rgba(0,0,0,0.5); }
#mute:active, #home:active { transform: scale(0.92); }

/* Dica de teclas — visível só em Play (controlado por .visible via setHudVisible).
   Fade-out automático após 8s via classe .fade. Some completamente em mobile. */
#kbd-hint {
    display: none; /* default oculto; .visible em desktop ativa */
    position: fixed;
    bottom: 16px; right: 16px;
    background: rgba(8, 12, 22, 0.78);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.1);
    color: #cbd5e1;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 12px;
    line-height: 1.7;
    font-family: system-ui, sans-serif;
    z-index: 40;
    pointer-events: none;
    opacity: 0.92;
    transition: opacity 0.8s ease-out;
    box-shadow: 0 4px 14px rgba(0,0,0,0.4);
    flex-direction: column;
    gap: 2px;
}
#kbd-hint.visible { display: flex; }
#kbd-hint.fade { opacity: 0; }
#kbd-hint .kbd-row { white-space: nowrap; }
#kbd-hint kbd {
    display: inline-block;
    background: rgba(253, 224, 71, 0.12);
    border: 1px solid rgba(253, 224, 71, 0.28);
    border-radius: 4px;
    padding: 1px 6px;
    margin: 0 1px;
    color: #fde047;
    font-weight: 700;
    font-size: 11px;
    font-family: monospace;
    min-width: 14px;
    text-align: center;
}
@media (max-width: 1024px) {
    #kbd-hint, #kbd-hint.visible { display: none !important; }
}

/* Desktop normal/wide: força HUD em 1 linha, evita wrap quando largura é apertada */
@media (min-width: 701px) {
    #hud-left { flex-wrap: nowrap; overflow: hidden; }
}

/* Desktop estreito (700-1100px) — HUD com cards mais compactos pra caber em 1 linha */
@media (min-width: 701px) and (max-width: 1100px) {
    #hud { padding: 8px 10px; }
    #hud-left { gap: 6px; }
    #score-box, #blocks-box { padding: 4px 10px; }
    .hud-label { font-size: 9px; }
    #time-left, #blocks-left { font-size: 18px; }
    #stage-box { padding: 5px 10px; gap: 6px; }
    #stage-box .stage-emoji { font-size: 18px; }
    #stage-box .stage-name { font-size: 14px; }
    #lives-box, #stats-box, #coins-box {
        padding: 6px 10px;
        font-size: 14px;
    }
    .heart-cell { font-size: 16px; }
    .coin-icon { width: 14px; height: 14px; }
    #mute, #home {
        width: 38px; height: 38px;
        font-size: 16px;
    }
}

/* HUD compacto em mobile portrait — cards menores pra não envergar */
@media (max-width: 700px) {
    #hud { padding: 6px 8px; }
    #hud-left { gap: 4px; }
    #score-box, #blocks-box {
        padding: 4px 8px;
    }
    .hud-label { font-size: 8px; letter-spacing: 1px; }
    #time-left, #blocks-left { font-size: 16px; }
    #lives-box, #stats-box, #coins-box {
        padding: 5px 8px;
        font-size: 13px;
        border-radius: 8px;
    }
    #stage-box {
        padding: 4px 8px;
        gap: 4px;
    }
    #stage-box .stage-emoji { font-size: 16px; }
    #stage-box .stage-name { font-size: 12px; }
    .heart-cell { font-size: 14px; }
    #stats-box { gap: 4px; }
    #coins-box { gap: 4px; }
    .coin-icon { width: 14px; height: 14px; }
    #mute, #home {
        width: 36px; height: 36px;
        font-size: 14px;
    }
    #hud-right { gap: 4px; }
}

/* Touch controls — position fixed embaixo (mobile only).
   game-wrap tem padding-bottom equivalente pra evitar sobreposição. */
#touch-controls {
    position: fixed;
    left: 0; right: 0;
    bottom: 0;
    height: 170px;
    padding: 10px 16px max(10px, env(safe-area-inset-bottom)) 16px;
    box-sizing: border-box;
    display: none;
    justify-content: space-between;
    align-items: center;
    z-index: 10;
    pointer-events: none;  /* clique passa pelo container; só joystick/bomb captam */
    opacity: 0.85;          /* translúcido pra não bloquear visão do canvas atrás */
}

/* Joystick virtual — base + stick analógico */
#joystick {
    position: relative;
    width: 140px; height: 140px;
    pointer-events: auto;
    touch-action: none;
}
#joystick-base {
    position: absolute;
    inset: 10px;
    width: 120px; height: 120px;
    border-radius: 50%;
    background:
        radial-gradient(circle at 50% 50%, rgba(255,255,255,0.04) 0%, rgba(255,255,255,0.0) 70%),
        radial-gradient(circle at 50% 50%, rgba(99,102,241,0.18) 0%, rgba(11,18,32,0.6) 100%);
    border: 2px solid rgba(255,255,255,0.18);
    box-shadow:
        0 4px 16px rgba(0,0,0,0.5),
        inset 0 2px 6px rgba(0,0,0,0.4),
        inset 0 -2px 6px rgba(255,255,255,0.04);
}
#joystick-stick {
    position: absolute;
    width: 64px; height: 64px;
    top: 38px; left: 38px;  /* centralizado: (140-64)/2 = 38 */
    border-radius: 50%;
    background:
        radial-gradient(circle at 30% 30%, rgba(255,255,255,0.5) 0%, rgba(168,85,247,0.9) 50%, #4c1d95 100%);
    border: 2px solid rgba(255,255,255,0.4);
    box-shadow:
        0 4px 12px rgba(76,29,149,0.6),
        0 0 24px rgba(168,85,247,0.45),
        inset 0 -3px 6px rgba(0,0,0,0.3);
    transition: transform 0.08s ease-out;
    will-change: transform;
}

/* Botão de bomba — gradient vermelho/laranja com glow */
#bomb-btn {
    pointer-events: auto;
    width: 96px; height: 96px;
    border: 3px solid rgba(255,255,255,0.35);
    border-radius: 50%;
    background:
        radial-gradient(circle at 35% 30%, #fef3c7 0%, #f59e0b 25%, #ef4444 60%, #7f1d1d 100%);
    color: #fff;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    box-shadow:
        0 6px 0 #7f1d1d,
        0 0 24px rgba(239,68,68,0.55),
        inset 0 -4px 8px rgba(0,0,0,0.3),
        inset 0 2px 4px rgba(255,255,255,0.3);
    transition: transform 0.06s, box-shadow 0.06s;
    -webkit-tap-highlight-color: transparent;
}
#bomb-btn svg { display: block; filter: drop-shadow(0 2px 2px rgba(0,0,0,0.5)); }
#bomb-btn:active {
    transform: translateY(4px) scale(0.95);
    box-shadow:
        0 2px 0 #7f1d1d,
        0 0 32px rgba(239,68,68,0.85),
        inset 0 -2px 4px rgba(0,0,0,0.3);
}

@media (max-width: 1024px) {
    #touch-controls { display: flex !important; }
}

/* Pop-ups e toasts (mesmos do Cidade Monstro) */
.coin-popup {
    position: fixed; pointer-events: none;
    color: #fde047; font-weight: 900; font-size: 1.4rem;
    text-shadow: 0 2px 4px rgba(0,0,0,0.8);
    animation: coinFloat 0.9s ease-out forwards;
    z-index: 50;
}
@keyframes coinFloat {
    0% { opacity: 1; transform: translateY(0); }
    100% { opacity: 0; transform: translateY(-50px); }
}

.achievement-toast {
    position: fixed; top: 80px; right: 1rem;
    background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
    border: 3px solid #fde047;
    border-radius: 14px;
    padding: 0.9rem 1.1rem;
    display: flex; gap: 0.8rem; align-items: center;
    color: #fff;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5), 0 0 16px rgba(253, 224, 71, 0.4);
    transform: translateX(120%);
    transition: transform 0.4s cubic-bezier(0.18, 0.89, 0.32, 1.28);
    z-index: 60; max-width: 320px; pointer-events: none;
}
.achievement-toast.show { transform: translateX(0); }
.achievement-toast.mission-toast {
    border-color: #22c55e;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5), 0 0 16px rgba(34,197,94,0.4);
}
.achievement-toast.mission-toast .ach-title { color: #22c55e; }
.ach-icon { font-size: 2rem; flex-shrink: 0; }
.ach-text { line-height: 1.25; }
.ach-title {
    font-size: 0.7rem; text-transform: uppercase; letter-spacing: 1.5px;
    color: #fde047; font-weight: 900;
}
.ach-name { font-size: 1rem; font-weight: 900; margin-top: 2px; }
.ach-reward { font-size: 0.85rem; color: #fbbf24; margin-top: 4px; }
