/**
 * Mobile Controls CSS - Layout Adaptativo v6
 * Argentum Survivor
 * 
 * MEJORAS v6:
 * - Portrait: Canvas ARRIBA de los controles (no superpuesto)
 * - Landscape: Inventario y botones más grandes
 * - Pantalla centrada al entrar al juego
 */

/* ============================================================================
   VARIABLES CSS
   ============================================================================ */

:root {
    --sat: env(safe-area-inset-top, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);

    --mobile-gold: #c5a059;
    --mobile-gold-bright: #ffd700;
    --mobile-copper: #8b7355;
    --mobile-dark: #1a1510;
    --mobile-panel: #14100b;
    --mobile-border: #554433;

    --btn-red-light: #cc4444;
    --btn-red-mid: #991111;
    --btn-red-dark: #661111;
    --btn-red-border: #8b4444;

    /* Tamaños de HUD landscape */
    --hud-width-landscape: max(0px, calc((100vw - 100dvh) * 0.5));

    /* Tamaño de controles en portrait */
    --controls-height-portrait: 130px;

    --hud-max-height-mobile: 60vh;
    --hud-portrait-height: clamp(110px, 22vh, 180px);

    --mobile-console-height: clamp(108px, 18vh, 220px);

    /* iOS Safari: scroll mínimo para ocultar barra */
    --ios-scroll-shim: 80px;
}

/* ============================================================================
   LOGIN - iOS SCROLL
   ============================================================================ */

body.mobile-login-mode {
    overflow-y: auto !important;
    overflow-x: hidden !important;
    height: auto !important;
    min-height: 130vh !important;
    -webkit-overflow-scrolling: touch !important;
    position: relative !important;
}

.mobile-login-mode #screen-auth {
    position: relative !important;
    height: auto !important;
    min-height: 130vh !important;
    padding: 15vh 0 30vh 0;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    width: 100% !important;
}

.mobile-scroll-hint {
    text-align: center;
    font-size: 13px;
    color: #8f8;
    margin: 15px 0;
    padding: 12px 20px;
    background: rgba(26, 74, 42, 0.4);
    border: 1px solid #3a8a5a;
    border-radius: 8px;
    animation: hint-pulse 2s infinite;
}

@keyframes hint-pulse {

    0%,
    100% {
        opacity: 0.7;
    }

    50% {
        opacity: 1;
    }
}

.mobile-login-mode .auth-card {
    width: 280px !important;
    max-width: 85vw !important;
    padding: 18px 20px !important;
}

.mobile-login-mode .auth-card input {
    font-size: 16px !important;
}

.mobile-fullscreen-btn {
    display: none !important;
}

#screen-auth.screen-hidden {
    display: none !important;
}

/* ============================================================================
   MODO JUEGO - FIJO Y CENTRADO
   ============================================================================ */

body.mobile-mode:not(.mobile-login-mode) {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    overflow: hidden !important;
    margin: 0 !important;
    padding: 0 !important;
    overscroll-behavior: none !important;
}

html.mobile-mode:not(.mobile-login-mode) {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    overflow: hidden !important;
}

html.mobile-mode.ios-scroll-fullscreen:not(.mobile-login-mode) {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    right: auto !important;
    bottom: auto !important;
    width: 100% !important;
    height: auto !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
}

body.mobile-mode.ios-scroll-fullscreen:not(.mobile-login-mode) {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    right: auto !important;
    bottom: auto !important;
    width: 100% !important;
    height: auto !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
}

body.mobile-mode.ios-scroll-fullscreen:not(.mobile-login-mode) #ios-scroll-shim-top {
    height: var(--ios-scroll-shim) !important;
}

body.mobile-mode.ios-scroll-fullscreen:not(.mobile-login-mode) .game-layout {
    position: sticky !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: auto !important;
    width: 100% !important;
    height: 100vh !important;
    height: 100dvh !important;
}

.mobile-mode {
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}

/* ============================================================================
   OCULTAR ELEMENTOS EN MÓVIL
   ============================================================================ */

.mobile-mode .minimap-section,
.mobile-mode .minimap-panel {
    display: none !important;
}

.mobile-mode .game-column-right {
    display: none !important;
}

.mobile-mode .console-panel:not(.mobile-console-visible) {
    display: none !important;
}

.mobile-mode .sidebar-footer-buttons {
    display: none !important;
}

/* Cuando hay un modal abierto, ocultar todo el HUD móvil (stats, inventario,
   barra de ataques, controles y toggle de chat) para que nada tape el modal. */
body.mobile-mode.modal-open .mobile-hud-left,
body.mobile-mode.modal-open .mobile-hud-right,
body.mobile-mode.modal-open .mobile-chat-toggle,
body.mobile-mode.modal-open .mobile-survivor-attack-slots,
body.mobile-mode.modal-open #mobile-controls-container {
    display: none !important;
}

body.mobile-mode #gold-display-btn.gold-btn-cast {
    justify-content: center;
    gap: 8px;
}

body.mobile-mode #gold-display-btn #mobile-gold-action-label {
    font-weight: 900;
    font-size: 12px;
    letter-spacing: 0.5px;
    color: #ffd700;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

/* ============================================================================
   GAME LAYOUT - BASE
   ============================================================================ */

.mobile-mode .game-layout {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    border: none !important;
    border-radius: 0 !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
    background: #000 !important;
}

.mobile-mode .game-column-left {
    width: 100% !important;
    height: 100% !important;
}

.mobile-mode .game-viewport {
    width: 100% !important;
    height: 100% !important;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
}

/* ============================================================================
   LANDSCAPE - CANVAS CENTRADO, HUD GRANDES A LOS LADOS
   ============================================================================ */

.mobile-mode.landscape .game-column-left {
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    left: var(--hud-width-landscape) !important;
    right: var(--hud-width-landscape) !important;
    width: auto !important;
}

.mobile-mode.landscape #game-canvas {
    width: 100% !important;
    height: 100% !important;
    max-width: 100%;
    object-fit: fill;
    object-position: center;
}

.mobile-mode.landscape .game-viewport {
    align-items: stretch;
    justify-content: stretch;
}

.mobile-mode.portrait .game-column-left {
    position: fixed !important;
    top: calc(var(--mobile-console-height) + 14px) !important;
    left: 0 !important;
    right: 0 !important;
    bottom: calc(var(--controls-height-portrait) + var(--sab) + 18px + var(--hud-portrait-height)) !important;
    height: auto !important;
    width: 100vw !important;
}

.mobile-mode.portrait .game-viewport {
    width: 100% !important;
    height: 100% !important;
    align-items: flex-start;
    justify-content: flex-start;
}

.mobile-mode.portrait #game-canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain;
    object-position: top center;
}

/* ============================================================================
   HUD IZQUIERDO (LANDSCAPE)
   ============================================================================ */

.mobile-hud-left {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: var(--hud-width-landscape);
    padding: calc(5px + var(--sat)) 5px calc(5px + var(--sab)) calc(5px + var(--sal));
    background: rgba(10, 8, 6, 0.95);
    border-right: 1px solid var(--mobile-copper);
    display: flex;
    flex-direction: column;
    z-index: 500;
    box-sizing: border-box;
    overflow: hidden;
    min-height: 0;
}

/* LANDSCAPE: stats a la derecha y sin invadir la zona de controles */
.mobile-mode.landscape .mobile-hud-left {
    left: auto;
    right: 0;
    top: 0;
    border-right: none;
    border-left: 1px solid var(--mobile-copper);
    bottom: auto;
    height: var(--hud-max-height-mobile);
    padding: 5px calc(5px + var(--sar)) calc(5px + var(--sab)) 5px;
}

/* PORTRAIT: mostrar HUD (stats) y recortar abajo para no tapar controles */
.mobile-mode.portrait .mobile-hud-left {
    display: flex !important;
    left: 0;
    right: auto;
    width: 42vw;
    top: auto;
    bottom: calc(var(--controls-height-portrait) + var(--sab) + 18px);
    height: var(--hud-portrait-height);
    padding: 6px 5px 8px calc(5px + var(--sal));
    z-index: 520;
}

#mobile-hud-left {
    font-family: inherit;
}

.mobile-char-info {
    margin-bottom: 6px;
    padding: 6px 6px;
    border-radius: 6px;
    background: rgba(20, 16, 11, 0.92);
    border: 1px solid rgba(85, 68, 51, 0.9);
}

.mobile-char-name {
    font-size: 13px;
    font-weight: 800;
    color: #f0e6d2;
    line-height: 1.1;
}

.mobile-char-meta {
    font-size: 11px;
    color: #b9ae9a;
    line-height: 1.1;
    margin-top: 2px;
}

.mobile-stats {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.mobile-stat-row {
    display: grid;
    grid-template-columns: 34px 1fr;
    gap: 6px;
    align-items: center;
}

.mobile-stat-label {
    font-size: 10px;
    font-weight: 800;
    color: #d4c8b0;
    text-align: left;
}

.mobile-stat-bar {
    position: relative;
    height: 14px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.45);
    border: 1px solid rgba(85, 68, 51, 0.9);
    overflow: hidden;
}

.mobile-stat-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0%;
}

.mobile-stat-fill.hp {
    background: linear-gradient(90deg, #7a1111, #cc4444);
}

.mobile-stat-fill.mana {
    background: linear-gradient(90deg, #123b7a, #4b8bff);
}

.mobile-stat-fill.sta {
    background: linear-gradient(90deg, #1e6a25, #42c44b);
}

.mobile-stat-fill.ham {
    background: linear-gradient(90deg, #6a4b1e, #c49a42);
}

.mobile-stat-fill.sed {
    background: linear-gradient(90deg, #1e5a6a, #42a9c4);
}

.mobile-stat-text {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    font-weight: 800;
    color: #f0e6d2;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
    pointer-events: none;
}

.mobile-exp-bar {
    margin-top: 8px;
}

.mobile-exp-label {
    font-size: 10px;
    font-weight: 800;
    color: #d4c8b0;
    margin-bottom: 4px;
}

.mobile-exp-track {
    position: relative;
    height: 10px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.45);
    border: 1px solid rgba(85, 68, 51, 0.9);
    overflow: hidden;
}

.mobile-exp-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #6a5a1e, #ffd700);
}

/* ============================================================================
   HUD DERECHO (LANDSCAPE) - MÁS GRANDE
   ============================================================================ */

.mobile-hud-right {
    position: fixed;
    right: 0;
    top: 0;
    bottom: 0;
    width: var(--hud-width-landscape);
    padding: calc(5px + var(--sat)) calc(5px + var(--sar)) calc(5px + var(--sab)) 5px;
    background: rgba(10, 8, 6, 0.95);
    border-left: 1px solid var(--mobile-copper);
    display: flex;
    flex-direction: column;
    z-index: 500;
    box-sizing: border-box;
    overflow: hidden;
    min-height: 0;
}

/* LANDSCAPE: inventario/hechizos a la izquierda y sin invadir la zona de controles */
.mobile-mode.landscape .mobile-hud-right {
    right: auto;
    left: 0;
    top: 0;
    border-left: none;
    border-right: 1px solid var(--mobile-copper);
    bottom: auto;
    height: var(--hud-max-height-mobile);
    padding: 5px 5px calc(5px + var(--sab)) calc(5px + var(--sal));
}

/* PORTRAIT: mostrar inventario/hechizos y recortar abajo para no tapar controles */
.mobile-mode.portrait .mobile-hud-right {
    display: flex !important;
    right: 0;
    left: auto;
    width: 58vw;
    top: auto;
    bottom: calc(var(--controls-height-portrait) + var(--sab) + 18px);
    height: var(--hud-portrait-height);
    padding: 6px calc(5px + var(--sar)) 8px 5px;
    z-index: 520;
}

.mobile-hud-right .mobile-spells-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 4px 4px;
}

.mobile-hud-right .mobile-spell-btn {
    width: 100%;
    min-height: 34px;
    padding: 6px 8px;
    font-size: 12px;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    color: #d4c8b0;
    background: rgba(30, 24, 18, 0.85);
    border: 1px solid var(--mobile-border);
    border-radius: 6px;
}

.mobile-hud-right .mobile-spell-btn.selected {
    border-color: var(--mobile-gold);
    box-shadow: 0 0 0 1px rgba(212, 172, 55, 0.5);
}

.mobile-hud-right .mobile-spell-btn:active {
    transform: translateY(1px);
    filter: brightness(1.08);
}

.mobile-mode.portrait .mobile-hud-right .mobile-inventory {
    flex: 0 0 auto;
    max-height: 88px;
}

.mobile-hud-right .mobile-inventory {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 2px 0;
    min-height: 0;
}

.mobile-hud-right .mobile-spells {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding: 2px 0;
    flex: 1;
    min-height: 0;
}

.mobile-hud-right .mobile-inventory-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    grid-auto-rows: 38px;
    gap: 4px;
    padding: 2px 4px;
}

.mobile-inv-slot {
    min-height: 38px;
    border-radius: 6px;
    background: rgba(26, 21, 16, 0.85);
    border: 1px solid var(--mobile-border);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35);
    -webkit-tap-highlight-color: transparent;
}

.mobile-inv-slot:active {
    transform: translateY(1px);
    filter: brightness(1.08);
}

.mobile-inv-slot-selected {
    border-color: var(--mobile-gold);
    box-shadow: 0 0 0 1px rgba(212, 172, 55, 0.45);
}

.joystick-container {
    position: fixed;
    bottom: calc(20px + var(--sab));
    left: calc(20px + var(--sal));
    z-index: 910;
}

.joystick-base {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    background: radial-gradient(circle, #2a2015 0%, #1a1510 70%, #0d0a08 100%);
    border: 3px solid #8b7355;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.8), 0 0 15px rgba(139, 115, 85, 0.3), inset 0 0 40px rgba(139, 115, 85, 0.1);
    position: relative;
}

.joystick-base::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 10px solid #554433;
}

.joystick-knob {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, #c5a059, #8b5a2b 60%, #5a3a1a 100%);
    border: 2px solid #ffd700;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5), inset 0 -2px 4px rgba(0, 0, 0, 0.3), inset 0 2px 4px rgba(255, 215, 0, 0.2);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: box-shadow 0.1s;
}

.joystick-knob.active {
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), inset 0 -1px 2px rgba(0, 0, 0, 0.3), 0 0 20px rgba(255, 215, 0, 0.4);
}

.mobile-mode.landscape .joystick-container {
    position: fixed !important;
    bottom: calc(18px + var(--sab)) !important;
    left: calc(20px + var(--sal)) !important;
}

.action-buttons-container {
    display: grid;
    grid-template-areas:
        "left   p     s"
        "med    t     right";
    gap: 8px;
    align-items: center;
    justify-items: center;
}

.mobile-action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    background: rgba(20, 16, 11, 0.92);
    border: 2px solid var(--mobile-border);
    color: #d4c8b0;
    box-shadow: 0 6px 14px rgba(0, 0, 0, 0.55);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.mobile-mode.landscape .action-buttons-container {
    position: fixed !important;
    bottom: calc(10px + var(--sab)) !important;
    right: calc(20px + var(--sar)) !important;
    grid-template-columns: 40px 58px 40px;
    grid-template-rows: 40px 58px;
}

.mobile-mode.portrait .action-buttons-container {
    position: fixed !important;
    bottom: calc(20px + var(--sab)) !important;
    right: calc(15px + var(--sar)) !important;
    grid-template-columns: 44px 64px 44px;
    grid-template-rows: 44px 64px;
}

.mobile-mode.landscape .action-btn-primary {
    width: 56px;
    height: 56px;
    font-size: 22px;
}

.mobile-mode.landscape .action-btn-secondary,
.mobile-mode.landscape .action-btn-tertiary {
    width: 38px;
    height: 38px;
    font-size: 16px;
}

.mobile-mode.landscape .action-btn-left,
.mobile-mode.landscape .action-btn-right,
.mobile-mode.landscape .action-btn-meditate {
    width: 34px;
    height: 34px;
    font-size: 14px;
}

.mobile-mode.portrait .action-btn-left,
.mobile-mode.portrait .action-btn-right,
.mobile-mode.portrait .action-btn-meditate {
    width: 38px;
    height: 38px;
    font-size: 14px;
}

.mobile-mode.portrait .action-btn-primary {
    width: 62px;
    height: 62px;
    font-size: 24px;
}

.mobile-mode.portrait .action-btn-secondary,
.mobile-mode.portrait .action-btn-tertiary {
    width: 42px;
    height: 42px;
    font-size: 16px;
}

.action-btn-primary {
    grid-area: p;
}

.action-btn-secondary {
    grid-area: s;
}

.action-btn-tertiary {
    grid-area: t;
}

.action-btn-left {
    grid-area: left;
}

.action-btn-right {
    grid-area: right;
}

.action-btn-meditate {
    grid-area: med;
}

.mobile-action-btn .btn-icon {
    pointer-events: none;
    text-shadow: 0 1px 2px #000;
}

.mobile-chat-toggle {
    position: fixed;
    top: calc(5px + var(--sat));
    left: 50%;
    transform: translateX(-50%);
    z-index: 700;
    width: 50px;
    height: 22px;
    background: rgba(20, 16, 11, 0.95);
    border: 2px solid var(--mobile-copper);
    border-top: none;
    border-radius: 0 0 8px 8px;
    color: var(--mobile-gold);
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.mobile-chat-toggle.open {
    top: calc(var(--mobile-console-height) - 22px);
    bottom: auto;
    border-radius: 8px 8px 0 0;
}

.mobile-mode.portrait .mobile-chat-toggle {
    display: none !important;
}

.mobile-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    padding: 4px 4px 6px 4px;
}

.mobile-tab {
    height: 30px;
    border-radius: 8px;
    background: rgba(20, 16, 11, 0.92);
    border: 1px solid var(--mobile-border);
    color: #d4c8b0;
    font-size: 12px;
    font-weight: 800;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.mobile-tab:active {
    transform: translateY(1px);
    filter: brightness(1.08);
}

.mobile-tab.active {
    border-color: var(--mobile-gold);
    box-shadow: 0 0 0 1px rgba(212, 172, 55, 0.45);
    color: #f0e6d2;
}

.mobile-gold-btn {
    margin-top: 6px;
    width: 100%;
    height: 34px;
    border-radius: 8px;
    background: linear-gradient(180deg, rgba(197, 160, 89, 0.25), rgba(20, 16, 11, 0.92));
    border: 1px solid var(--mobile-border);
    color: #f0e6d2;
    font-weight: 800;
    font-size: 12px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.mobile-gold-btn:active {
    transform: translateY(1px);
    filter: brightness(1.08);
}

/* Consola visible */
.mobile-console-visible {
    display: flex !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    height: var(--mobile-console-height) !important;
    max-height: calc(100dvh - var(--sat) - 2px) !important;
    z-index: 650 !important;
    background: rgba(10, 8, 6, 0.98) !important;
    border-bottom: 2px solid var(--mobile-copper) !important;
    flex-direction: column !important;
    padding: calc(5px + var(--sat)) 10px 5px 10px !important;
    overflow: hidden !important;
    animation: slideDown 0.2s ease-out;
}

.mobile-mode.landscape .mobile-console-visible {
    left: 0 !important;
    right: 0 !important;
    max-height: calc(100dvh - var(--sat) - 2px) !important;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
    }

    to {
        transform: translateY(0);
    }
}

.mobile-console-visible #console-messages,
.mobile-console-visible .console-history {
    flex: 1;
    overflow-y: auto;
    font-size: 12px;
}

.mobile-console-visible .console-input-row {
    display: flex !important;
    gap: 5px;
    padding: 5px 0;
}

.mobile-console-visible #console-input {
    flex: 1;
    font-size: 16px !important;
    padding: 10px !important;
    background: rgba(30, 24, 18, 0.9);
    border: 1px solid var(--mobile-border);
    border-radius: 4px;
    color: #d4c8b0;
}

/* ============================================================================
   CONTENEDOR DE CONTROLES
   ============================================================================ */

#mobile-controls-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    pointer-events: none;
    z-index: 900;
    display: none;
}

#mobile-controls-container>* {
    pointer-events: auto;
}

/* Survivor attack slots overlay (sidebar oculto en mobile).
   Anclada a la derecha: debajo de los stats y arriba de los controles. */
.mobile-survivor-attack-slots {
    position: absolute;
    left: auto;
    right: calc(12px + var(--sar));
    transform: none;
    bottom: calc(var(--sab) + 12px);
    display: flex;
    gap: 8px;
    padding: 6px 10px;
    background: rgba(13, 10, 8, 0.88);
    border: 1px solid var(--mobile-copper, #554433);
    border-radius: 8px;
    pointer-events: none !important;
    z-index: 910;
    min-width: 170px;
    justify-content: space-between;
}

.mobile-survivor-attack-slots[hidden] {
    display: none !important;
}

/* Landscape: encima de los botones de acción (que terminan ~116px + safe area) */
.mobile-mode.landscape .mobile-survivor-attack-slots {
    right: calc(10px + var(--sar));
    bottom: calc(124px + var(--sab));
}

/* Portrait: franja entre la zona de controles y la fila de HUD */
.mobile-mode.portrait .mobile-survivor-attack-slots {
    right: calc(12px + var(--sar));
    bottom: calc(var(--controls-height-portrait) + var(--sab) + 8px);
}

.mobile-survivor-attack-slots .surv-atk-slot {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    min-width: 48px;
}

.mobile-survivor-attack-slots .surv-atk-label {
    display: none !important;
}

.mobile-survivor-attack-slots .surv-atk-icon-wrap {
    position: relative;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #1a1510;
    border: 1px solid var(--mobile-border, #554433);
    border-radius: 6px;
    overflow: hidden;
}

.mobile-survivor-attack-slots .surv-atk-icon {
    font-size: 16px;
    line-height: 1;
    z-index: 1;
}

.mobile-survivor-attack-slots .surv-atk-sprite {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
    pointer-events: none;
}

.mobile-survivor-attack-slots .surv-atk-sprite[hidden] {
    display: none !important;
}

.mobile-survivor-attack-slots .surv-atk-sprite canvas {
    width: 32px;
    height: 32px;
    image-rendering: pixelated;
}

.mobile-survivor-attack-slots .surv-atk-cd {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    clip-path: inset(calc(var(--cd-fill, 0) * 100%) 0 0 0);
    border-radius: 6px;
    pointer-events: none;
}

.mobile-survivor-attack-slots .surv-atk-slot.is-ready .surv-atk-icon-wrap {
    border-color: var(--mobile-gold-bright, #ffd700);
}

/* Asegurar que se muestre cuando está activo */
.mobile-mode #mobile-controls-container {
    display: block !important;
}

/* ============================================================================
   OCULTAR EN DESKTOP
   ============================================================================ */

@media (min-width: 1024px) and (pointer: fine) {

#mobile-controls-container,
.mobile-hud-left,
.mobile-hud-right,
.mobile-chat-toggle,
.mobile-scroll-hint {
display: none !important;
}
}

/* ============================================================================
   MODALES - BASE (MÓVIL)
   ============================================================================ */

.mobile-mode .modal-overlay {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    padding: 0 !important;
    margin: 0 !important;
    align-items: stretch !important;
    justify-content: stretch !important;
    z-index: 2000;
}

.mobile-mode .modal-window {
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    border-radius: 0 !important;
    display: flex;
    flex-direction: column;
}

.mobile-mode .modal-header {
    flex: 0 0 auto;
    padding-top: calc(10px + var(--sat)) !important;
}

.mobile-mode .modal-content {
    flex: 1 1 auto;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(14px + var(--sab)) !important;
}

.mobile-mode .modal-footer {
    flex: 0 0 auto;
    padding-bottom: calc(10px + var(--sab)) !important;
}

/* ============================================================================
   MODAL DE COMERCIO (MÓVIL)
   ============================================================================ */

.mobile-mode #trade-window-overlay .modal-window {
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    border-radius: 0 !important;
}

.mobile-mode #trade-window-overlay {
    align-items: stretch;
    justify-content: stretch;
    padding: 0 !important;
}

.mobile-mode #trade-window-overlay .trade-container {
    padding: 10px !important;
    gap: 10px !important;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    display: flex !important;
    flex-wrap: nowrap;
    align-items: stretch;
    box-sizing: border-box;
}

.mobile-mode.portrait #trade-window-overlay .trade-container {
    flex-direction: column;
}

.mobile-mode.landscape #trade-window-overlay .trade-container {
    flex-direction: row;
}

.mobile-mode.landscape #trade-window-overlay .trade-container {
    padding: 8px !important;
    gap: 8px !important;
}

.mobile-mode #trade-window-overlay .trade-panel {
    flex: 1 1 50%;
    min-height: 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.mobile-mode.landscape #trade-window-overlay .trade-npc-panel,
.mobile-mode.landscape #trade-window-overlay .trade-player-panel {
    width: 50%;
}

.mobile-mode #trade-window-overlay .trade-items-list {
    max-height: none !important;
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding: 2px !important;
    gap: 3px !important;
    grid-auto-rows: 1fr;
}

.mobile-mode #trade-window-overlay .trade-item-info {
    margin: 10px 10px 0 10px !important;
    max-height: 26vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.mobile-mode #trade-window-overlay .trade-footer {
    padding-bottom: calc(10px + var(--sab)) !important;
    flex: 0 0 auto;
}

.mobile-mode.portrait #trade-window-overlay .trade-items-list {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
}

.mobile-mode #trade-window-overlay .trade-item-slot {
    width: auto !important;
    height: auto !important;
    aspect-ratio: 1 / 1;
    min-height: 44px;
    min-width: 0;
    overflow: hidden;
}

.mobile-mode #trade-window-overlay .trade-item-icon {
    width: 28px !important;
    height: 28px !important;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.mobile-mode #trade-window-overlay .trade-item-icon canvas {
    max-width: 100% !important;
    max-height: 100% !important;
}

.mobile-mode #trade-window-overlay .trade-item-amount {
    z-index: 5;
    background: rgba(0, 0, 0, 0.55);
    border-radius: 3px;
    padding: 1px 3px;
    font-size: 8px;
    line-height: 1;
    pointer-events: none;
}

.mobile-mode #trade-window-overlay .trade-npc-panel .trade-item-slot {
    min-height: 40px;
}

.mobile-mode #trade-window-overlay .trade-npc-panel .trade-item-icon {
    width: 24px !important;
    height: 24px !important;
}

.mobile-mode.portrait #trade-window-overlay {
    align-items: stretch;
    padding-top: 0;
    padding-bottom: 0;
}

.mobile-mode.portrait #trade-window-overlay .modal-window {
    margin-top: 0 !important;
    max-height: 100% !important;
}

/* ============================================================================
   VIEWPORT RESPONSIVE P0 — conserva el 4:3 nativo sin deformar el canvas
   ============================================================================ */

:root {
    --game-width-landscape: min(100vw, calc(100dvh * 1.333333));
    --hud-width-landscape: max(0px, calc((100vw - var(--game-width-landscape)) * 0.5));
    --controls-height-portrait: clamp(108px, 15dvh, 126px);
    --hud-portrait-height: clamp(96px, 15dvh, 126px);
    /* Franja para la barra clase/arma/extra durante una run (portrait) */
    --attack-bar-portrait: 54px;
    /* Alto reservado para el run HUD debajo del cuadrado del juego (portrait) */
    --run-hud-portrait-height: 44px;
    /* Alto reservado para el run HUD en la columna derecha (landscape) */
    --run-hud-landscape-height: 92px;
}

.mobile-mode.landscape .game-column-left {
    left: var(--hud-width-landscape) !important;
    right: var(--hud-width-landscape) !important;
}

.mobile-mode.landscape .game-viewport {
    width: 100% !important;
    height: 100dvh !important;
    align-items: center !important;
    justify-content: center !important;
}

.mobile-mode.landscape #game-canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    object-position: center !important;
}

.mobile-mode.portrait .game-column-left {
    top: 0 !important;
    bottom: calc(var(--controls-height-portrait) + var(--hud-portrait-height) + var(--sab) + 20px) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: flex-start !important;
    background: #000;
}

.mobile-mode.portrait .game-viewport {
    width: min(
        100vw,
        calc((100dvh - var(--controls-height-portrait) - var(--hud-portrait-height) - var(--sab) - 20px) * 1.333333)
    ) !important;
    height: auto !important;
    max-height: 100% !important;
    aspect-ratio: 4 / 3;
    margin: 0 auto;
    align-items: center !important;
    justify-content: center !important;
}

.mobile-mode.portrait #game-canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    object-position: center !important;
}

/* El chat deja de reservar altura permanente: se abre como bandeja superpuesta. */
.mobile-mode.portrait .console-panel:not(.mobile-console-visible) {
    display: none !important;
}

.mobile-mode.portrait .console-panel.mobile-console-visible {
    position: fixed !important;
    inset: var(--sat) 0 auto 0 !important;
    z-index: 900 !important;
    display: flex !important;
    width: 100vw !important;
    height: min(34dvh, 240px) !important;
}

.mobile-mode.portrait .mobile-chat-toggle {
    display: flex !important;
    top: calc(var(--sat) + 4px);
    right: calc(var(--sar) + 6px);
    left: auto;
    bottom: auto;
    width: 34px;
    height: 28px;
    align-items: center;
    justify-content: center;
    border-radius: 5px;
    z-index: 910;
}

.mobile-mode.portrait .mobile-console-open .mobile-chat-toggle {
    top: calc(min(34dvh, 240px) + var(--sat) + 4px);
}

.mobile-mode.portrait .mobile-hud-left,
.mobile-mode.portrait .mobile-hud-right {
    height: var(--hud-portrait-height);
    bottom: calc(var(--controls-height-portrait) + var(--sab) + 12px);
}

.mobile-mode.portrait .mobile-char-info {
    margin-bottom: 3px;
    padding: 4px;
}

.mobile-mode.portrait .mobile-char-meta {
    font-size: 8px;
}

.mobile-mode.portrait .mobile-stat-row {
    min-height: 15px;
    margin-bottom: 1px;
}

.mobile-mode.portrait.survivor-run-active .mobile-stat-row:nth-child(4),
.mobile-mode.portrait.survivor-run-active .mobile-stat-row:nth-child(5) {
    display: none;
}

.mobile-mode.portrait .mobile-hud-right .mobile-inventory {
    max-height: none;
}

.mobile-mode.portrait .mobile-inventory-grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
    grid-auto-rows: 31px;
    gap: 3px;
}

.mobile-mode.portrait .mobile-inv-slot {
    min-height: 31px;
}

.mobile-mode.portrait .mobile-tabs {
    padding: 2px 3px 3px;
}

.mobile-mode.portrait .mobile-tab {
    height: 24px;
    font-size: 10px;
}

.mobile-mode.portrait .mobile-gold-btn {
    min-height: 24px;
    font-size: 10px;
}

/* ============================================================================
   RUN ACTIVA (body.survivor-run-active): run HUD + barra de ataques
   ============================================================================ */

/* El run HUD (tiempo/oleada/nivel/bajas) es exclusivo de móvil */
body:not(.mobile-mode) .survivor-run-hud {
    display: none !important;
}

/* PORTRAIT: run HUD debajo del cuadrado del POV */
.mobile-mode.portrait .survivor-run-hud {
    top: calc(100% + 8px);
    bottom: auto;
    width: min(100%, 560px);
}

/* PORTRAIT con run activa: la fila de HUD sube para dejar la franja de la
   barra clase/arma/extra, y el juego reserva alto para el run HUD */
.mobile-mode.portrait.survivor-run-active .mobile-hud-left,
.mobile-mode.portrait.survivor-run-active .mobile-hud-right {
    bottom: calc(var(--controls-height-portrait) + var(--attack-bar-portrait) + var(--sab) + 14px);
}

.mobile-mode.portrait.survivor-run-active .game-column-left {
    bottom: calc(var(--controls-height-portrait) + var(--hud-portrait-height) + var(--attack-bar-portrait) + var(--run-hud-portrait-height) + var(--sab) + 28px) !important;
}

.mobile-mode.portrait.survivor-run-active .game-viewport {
    width: min(
        100vw,
        calc((100dvh - var(--controls-height-portrait) - var(--hud-portrait-height) - var(--attack-bar-portrait) - var(--run-hud-portrait-height) - var(--sab) - 28px) * 1.333333)
    ) !important;
}

/* LANDSCAPE: run HUD arriba a la derecha, en la columna de stats */
.mobile-mode.landscape .survivor-run-hud {
    position: fixed;
    top: calc(var(--sat) + 4px);
    right: calc(4px + var(--sar));
    left: auto;
    transform: none;
    width: max(calc(var(--hud-width-landscape) - 8px), 150px);
    z-index: 530;
}

.mobile-mode.landscape .run-hud-core {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.mobile-mode.landscape .run-hud-metric {
    padding: 4px 6px 3px;
    border-bottom: 1px solid rgba(118, 88, 54, 0.42);
}

.mobile-mode.landscape .run-hud-stats-btn {
    grid-column: 1 / -1;
    min-width: 0;
    min-height: 20px;
    padding: 3px 0;
    border-left: 0;
}

.mobile-mode.landscape .run-hud-boss {
    width: calc(100% - 8px);
    margin-top: 4px;
}

/* LANDSCAPE con run activa: los stats bajan para dejar lugar al run HUD y
   terminan antes de la barra de ataques + botones de acción */
.mobile-mode.landscape.survivor-run-active .mobile-hud-left {
    top: calc(var(--sat) + var(--run-hud-landscape-height));
    height: auto;
    bottom: calc(182px + var(--sab));
}

/* Durante la run, hambre y sed no aplican: liberar espacio vertical */
.mobile-mode.landscape.survivor-run-active .mobile-stat-row:nth-child(4),
.mobile-mode.landscape.survivor-run-active .mobile-stat-row:nth-child(5) {
    display: none;
}

@media (max-height: 680px) and (orientation: portrait) {
    :root {
        --controls-height-portrait: 100px;
        --hud-portrait-height: 88px;
    }

    .mobile-mode.portrait .mobile-char-meta,
    .mobile-mode.portrait .mobile-exp-label {
        display: none;
    }

    .mobile-mode.portrait .mobile-exp-bar {
        margin-top: 2px;
    }
}

@media (max-height: 420px) and (orientation: landscape) {
    .mobile-mode.landscape .mobile-char-meta,
    .mobile-mode.landscape .mobile-exp-label {
        display: none;
    }

    .mobile-mode.landscape .mobile-hud-left,
    .mobile-mode.landscape .mobile-hud-right {
        height: min(58dvh, 220px);
    }
}

/* ============================================================================
   MODAL DE OPCIONES (MÓVIL)
   ============================================================================ */

.mobile-mode #options-modal {
    z-index: 2147483647 !important;
}

.mobile-mode #options-modal .modal-content {
    padding-left: calc(10px + var(--sal)) !important;
    padding-right: calc(10px + var(--sar)) !important;
}

.mobile-mode #options-modal .options-tabs {
    flex-wrap: wrap;
}

.mobile-mode #options-modal .options-tab {
    flex: 1 1 calc(50% - 4px);
}

/* ============================================================================
   MODAL DE MISIONES (MÓVIL)
   ============================================================================ */

.mobile-mode #quests-modal .modal-content {
    padding-left: calc(10px + var(--sal)) !important;
    padding-right: calc(10px + var(--sar)) !important;
}

.mobile-mode #quests-modal .quests-layout {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.mobile-mode #quests-modal .quests-list {
    max-height: 35vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.mobile-mode #quests-modal .quests-detail-wrap {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.mobile-mode #quests-modal .quests-detail {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.mobile-mode.portrait #trade-window-overlay {
    align-items: stretch;
    padding-top: 0;
    padding-bottom: 0;
}

.mobile-mode.portrait #trade-window-overlay .modal-window {
    margin-top: 0 !important;
    max-height: 100% !important;
}