/* Mobile Responsiveness CSS */
/* Optimized for landscape mobile gaming */

/* ======================================
   CSS Variables
   ====================================== */
:root {
    --mobile-action-btn-size: 48px;
    --mobile-joystick-size: 112px;
    --mobile-hotbar-height: 60px;
    --mobile-header-height: 36px;
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* ======================================
   Utility Classes
   ====================================== */
.desktop-only {
    display: block;
}

.mobile-only {
    display: none !important;
}

/* ======================================
   Touch Device Detection
   Use max-height for landscape detection
   ====================================== */

/* Landscape mobile: height <= 500px catches most phones in landscape */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    .desktop-only {
        display: none !important;
    }

    .mobile-only {
        display: flex !important;
    }

    /* Hide desktop PK lock button on mobile — use mobile-pk-lock-btn instead */
    #pk-lock-btn {
        display: none !important;
    }

    /* Body - prevent scrolling and bouncing */
    body {
        overflow: hidden;
        touch-action: none;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        user-select: none;
        position: fixed;
        width: 100%;
        height: 100%;
    }

    /* Game wrapper fills entire screen (display controlled by JS showGameInterface) */
    #game-wrapper {
        flex-direction: row !important;
        min-width: unset !important;
        min-height: unset !important;
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
        /* iOS safe area: home indicator bar at bottom, Dynamic Island on sides */
        padding-bottom: var(--safe-area-bottom);
        box-sizing: border-box;
    }

    /* Allow oogwrap to be visible for equipment but hide layout impact */
    #game-wrapper .oogwrap {
        display: block !important;
        position: absolute;
        top: 0;
        right: 0;
        width: 0;
        height: 0;
        overflow: visible;
        pointer-events: none;
        z-index: 9990;
    }

    /* Hide specific desktop elements inside oogwrap */
    .oogwrap .minimap-container,
    .oogwrap .character-status {
        display: none !important;
    }

    /* Ensure equipment wrapper is visible */
    .equipment-options-wrapper {
        display: block !important;
        pointer-events: auto;
    }

    /* Main game area - full width/height */
    #game-wrapper .main {
        flex: 1 1 auto;
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 100%;
        min-height: 0;
        overflow: hidden;
        position: relative;
    }

    /* Hide middle section (desktop hotbar area) */
    #game-wrapper .main .middle {
        display: none !important;
    }

    /* Canvas takes full space */
    #game-wrapper .main .upper {
        flex: 1 1 auto;
        min-height: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        overflow: hidden;
    }

    .canvas-wrapper {
        width: 100% !important;
        height: 100% !important;
        position: relative;
    }

    .canvas-notification {
        z-index: 10002 !important;
    }

    #screen {
        width: 100% !important;
        height: 100% !important;
        display: block;
    }

    /* Hide chat resizer on mobile */
    #chat-resizer {
        display: none !important;
    }

    /* Hide original hotbar */
    .hotbar {
        display: none !important;
    }

    /* Context menus - allow on mobile (opened/closed by JS) */

    /* Windows positioned on right side for mobile */
    .window {
        pointer-events: auto !important;
        position: fixed !important;
        top: 170px !important;
        left: auto !important;
        right: 5px !important;
        width: 190px !important;
        max-width: 60vw;
        max-height: 60vh;
        z-index: 9995;
        overflow: visible;
        font-size: 11px;
    }

    /* Battle window stays on the right side */
    #battle-window {
        z-index: 9990 !important;
        top: 170px !important;
        right: 5px !important;
        left: auto !important;
    }

    /* Trade window centered on mobile */
    #trade-window {
        top: 50% !important;
        left: 50% !important;
        right: auto !important;
        transform: translate(-50%, -50%) !important;
        z-index: 99999 !important;
        width: 220px !important;
    }

    /* Container windows hidden on mobile — replaced by tabbed panel (except depot) */
    .window[containerIndex] {
        display: none !important;
    }

    /* Depot gets its own floating window on mobile */
    .window[containerIndex="1"] {
        display: flex !important;
        position: fixed !important;
        z-index: 9997 !important;
        width: 180px !important;
        top: 50% !important;
        left: 50% !important;
        right: auto !important;
        transform: translate(-50%, -50%) !important;
        transform-origin: center center !important;
        border: 1px solid #555 !important;
        max-height: 70vh !important;
    }

    .window[containerIndex="1"] > .header {
        cursor: default;
        touch-action: none;
        min-height: 28px;
    }

    .window[containerIndex="1"] > .header > .title {
        font-size: 11px;
    }

    .window[containerIndex="1"] > .header > button {
        min-width: 28px;
        min-height: 28px;
        font-size: 14px;
    }

    .window[containerIndex="1"] > .body {
        max-height: 50vh !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .window[containerIndex="1"] .slot {
        width: 40px !important;
        height: 40px !important;
        background-size: 100% 100% !important;
    }

    .window[containerIndex="1"] .slot canvas {
        width: 40px !important;
        height: 40px !important;
    }

    /* Mobile Container Tabbed Panel */
    #mobile-container-panel {
        position: fixed;
        z-index: 9996;
        width: 150px;
        background-image: url("/png/bg3.png");
        transform: scale(0.85);
        flex-direction: column;
        overflow: hidden;
        pointer-events: auto;
        left: 5px;
        right: auto;
        top: 37px;
        transform-origin: top left;
        border: 1px solid #333;
    }

    .mobile-container-tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        background-image: url("/png/bg4.png");
        min-height: 22px;
        gap: 0;
        scrollbar-width: none;
    }

    .mobile-container-tabs::-webkit-scrollbar {
        display: none;
    }

    .mobile-container-tab {
        flex: 0 0 auto;
        padding: 2px 5px;
        border: none;
        border-right: 1px solid #555;
        background: transparent;
        color: #999;
        font-size: 11px;
        white-space: nowrap;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 3px;
        height: 22px;
    }

    .mobile-container-tab.active {
        color: #ffd700;
        border-bottom: 2px solid #d4a017;
    }

    .mobile-container-tab-close {
        font-size: 14px;
        color: #888;
        cursor: pointer;
        padding: 0 2px;
        min-width: 20px;
        min-height: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .mobile-container-body {
        overflow-y: auto;
        max-height: 40vh;
    }

    .container-content .body {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        padding: 2px;
    }

    .container-content .slot {
        width: 32px !important;
        height: 32px !important;
        margin: 1px !important;
        border: 1px solid #444;
    }

    .container-content .slot canvas {
        width: 32px !important;
        height: 32px !important;
    }

    .window .header {
        height: 20px !important;
        line-height: 20px !important;
        font-size: 10px !important;
    }

    .window .body {
        max-height: 40vh !important;
        overflow-y: auto !important;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        padding: 2px !important;
        min-height: 0 !important;
    }

    .window .footer {
        display: none !important;
    }

    .window .slot {
        width: 32px !important;
        height: 32px !important;
        margin: 1px !important;
        border: 1px solid #444;
        /* Add border to see slots better */
    }

    /* Larger touch targets */
    .slot {
        width: 36px !important;
        height: 36px !important;
    }

    .slot canvas {
        width: 36px !important;
        height: 36px !important;
    }

    /* Equipment panel - show in top right on mobile, styled like .window */
    .equipment.wrapper {
        position: fixed !important;
        top: 37px !important;
        right: 5px !important;
        left: auto !important;
        z-index: 9998;
        background-image: url("/png/bg3.png");
        padding: 0 2px 2px 2px !important;
        width: 140px !important;
        flex-wrap: wrap !important;
    }

    .equipment.wrapper > .mobile-panel-header {
        flex: 0 0 100%;
        margin: 0 -2px 0 -2px;
    }

    .equipment-options-wrapper {
        display: block !important;
    }

    .equipment-column {
        gap: 2px !important;
    }

    .equipment.wrapper .fight-mode-selector {
        margin-left: 2px !important;
        padding: 2px !important;
    }

    .equipment.wrapper .slot {
        width: 30px !important;
        height: 30px !important;
    }

    .equipment.wrapper .slot canvas {
        width: 30px !important;
        height: 30px !important;
    }

    #player-capacity {
        font-size: 10px !important;
        padding: 2px !important;
    }

    .equipment-buttons {
        display: none !important;
    }
}

/* ======================================
   Mobile Tab Bar (Inside Status Bar)
   ====================================== */
#mobile-tab-bar {
    display: flex;
    flex-direction: row;
    gap: 0;
    flex-shrink: 0;
}

.mobile-tab {
    height: 100%;
    padding: 0 8px;
    border: none;
    border-left: 1px solid #444;
    background: transparent;
    color: #999;
    font-size: 13px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    border-radius: 0;
}

.mobile-tab:first-child {
    border-radius: 0;
}

.mobile-tab:last-child {
    border-radius: 0;
}

/* ======================================
   Swap Button (Tab Bar)
   ====================================== */
.mobile-tab-swap {
    height: 100%;
    padding: 0 6px;
    border: none;
    border-left: 2px solid #666;
    background: transparent;
    color: #777;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
    border-radius: 0;
}

.mobile-tab-swap:active {
    color: #fff;
    background: rgba(60, 60, 60, 0.95);
}

/* When swap button exists, the last tab no longer gets the right radius */
.mobile-tab:nth-last-child(2) {
    border-radius: 0;
}

.mobile-tab.active {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

/* ======================================
   Mobile Tab Panel Positioning
   ====================================== */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    /* Panels shown via tab bar get this class */
    .mobile-tab-panel {
        display: block !important;
        position: fixed !important;
        top: calc(37px + var(--safe-area-top)) !important;
        right: calc(5px + var(--safe-area-right)) !important;
        left: auto !important;
        z-index: 9998 !important;
        pointer-events: auto !important;
    }

    /* Stats/friends panels — wider when opened via tab */
    #skill-window.mobile-tab-panel,
    #friend-window.mobile-tab-panel {
        width: 150px !important;
        max-height: 70vh !important;
        font-size: 12px !important;
    }

    #skill-window.mobile-tab-panel .body {
        display: block !important;
        overflow-y: auto !important;
        height: auto !important;
        max-height: calc(70vh - 26px) !important;
    }

    /* Friend window footer with add button on mobile */
    #friend-window .footer {
        display: block !important;
        height: auto !important;
        cursor: default !important;
        padding: 2px !important;
    }

    /* Minimap override — normally hidden on mobile */
    .minimap-container.mobile-tab-panel {
        display: block !important;
        position: fixed !important;
        pointer-events: auto !important;
        background-image: url("/png/bg3.png");
        width: auto !important;
        margin: 0 !important;
        padding: 4px !important;
        padding-right: 4px !important;
    }

    /* Minimap canvas and wrapper fill available space */
    .minimap-container.mobile-tab-panel .minimap-wrapper {
        width: 128px !important;
        height: 128px !important;
    }

    .minimap-container.mobile-tab-panel .minimap-wrapper canvas {
        width: 128px !important;
        height: 128px !important;
    }

    /* Minimap buttons horizontal row underneath the map */
    .minimap-container.mobile-tab-panel .button-stack {
        position: relative !important;
        right: auto !important;
        top: auto !important;
        width: 100% !important;
        flex-direction: row !important;
        justify-content: center !important;
        margin-top: 4px !important;
    }

    .minimap-container.mobile-tab-panel .button-stack .symbol-button {
        width: 22px !important;
        height: 22px !important;
        font-size: 11px !important;
    }

    /* Minimap crosshair centered for canvas */
    .minimap-container.mobile-tab-panel .crosshair {
        left: 60px !important;
        top: 56px !important;
    }

    /* Skill window touch-friendly */
    #skill-window.mobile-tab-panel .skill-wrapper {
        padding: 3px 4px !important;
        font-size: 12px !important;
    }

    #skill-window.mobile-tab-panel .bar-wrapper > .bar {
        height: 6px !important;
    }

    #skill-window.mobile-tab-panel .bar-wrapper > .bar > .value {
        height: 6px !important;
    }
}

/* Landscape tab panel adjustments */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .mobile-tab-panel {
        top: 33px !important;
    }

}

/* ======================================
   Mobile Status Bar (Top)
   ====================================== */
#mobile-status-bar {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--mobile-header-height);
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.85), rgba(0, 0, 0, 0.6));
    border-bottom: 1px solid #444;
    z-index: 10001;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    padding: 2px calc(12px + var(--safe-area-right)) 2px calc(12px + var(--safe-area-left));
    box-sizing: border-box;
    gap: 12px;
}

@media screen and (max-height: 500px),
screen and (max-width: 768px) {
    #mobile-status-bar {
        display: flex;
    }
}

.mobile-bar-container {
    flex: 1 1 auto;
    max-width: 400px;
    display: flex;
    flex-direction: row;
    gap: 8px;
    align-items: center;
}

.mobile-bar {
    flex: 1;
    height: 18px;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 4px;
    overflow: hidden;
    position: relative;
}

.mobile-bar-fill {
    height: 100%;
    transition: width 0.2s ease;
}

.mobile-bar-fill.health {
    background: linear-gradient(to bottom, #22cc22, #118811);
}

.mobile-bar-fill.mana {
    background: linear-gradient(to bottom, #3388ff, #2255aa);
}

.mobile-bar-text {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 11px;
    line-height: 18px;
    color: white;
    text-shadow: 1px 1px 2px black, -1px -1px 2px black;
    font-weight: bold;
}

/* Magic Shield indicator icon — persistent on mobile health bars */
.magic-shield-indicator {
    position: absolute;
    right: 2px;
    top: 50%;
    transform: translateY(-50%);
    width: 12px;
    height: 12px;
    image-rendering: pixelated;
    z-index: 2;
    pointer-events: none;
    filter: drop-shadow(0 0 2px rgba(50, 140, 255, 0.8));
    animation: magic-shield-pulse 2s ease-in-out infinite;
}

@keyframes magic-shield-pulse {
    0%, 100% { filter: drop-shadow(0 0 2px rgba(50, 140, 255, 0.8)); }
    50% { filter: drop-shadow(0 0 5px rgba(50, 140, 255, 1)); }
}

/* Portrait-only HP/Mana bars (below canvas in portrait mode only) */
#portrait-bars {
    display: none !important;
}

.portrait-bar {
    flex: 1;
    height: 18px;
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 4px;
    overflow: hidden;
    position: relative;
}

.portrait-bar-fill {
    height: 100%;
    transition: width 0.2s ease;
}

.portrait-bar-fill.health {
    background: linear-gradient(to bottom, #22cc22, #118811);
}

.portrait-bar-fill.mana {
    background: linear-gradient(to bottom, #3388ff, #2255aa);
}

.portrait-bar-text {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    text-align: center;
    font-size: 11px;
    line-height: 18px;
    color: white;
    text-shadow: 1px 1px 2px black, -1px -1px 2px black;
    font-weight: bold;
}

#mobile-menu-btn {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    font-size: 16px;
    flex-shrink: 0;
    min-height: 32px;
    padding: 0;
}

/* ======================================
   Mobile Action Bar (Bottom Overlay)
   Overlay style - doesn't take space from game
   ====================================== */
#mobile-action-bar {
    display: none;
    position: fixed;
    bottom: calc(15px + var(--safe-area-bottom));
    left: 10px;
    right: 10px;
    height: auto;
    background: transparent;
    z-index: 10001;
    flex-direction: row;
    align-items: flex-end;
    justify-content: space-between;
    padding: 0;
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
    box-sizing: border-box;
    pointer-events: none;
}

#mobile-action-bar>* {
    pointer-events: auto;
}


@media screen and (max-height: 500px),
screen and (max-width: 768px) {
    #mobile-action-bar {
        display: flex;
    }
}

/* ======================================
   Virtual Joystick (Left Side)
   ====================================== */
#mobile-joystick-zone {
    width: var(--mobile-joystick-size);
    height: var(--mobile-joystick-size);
    position: relative;
    flex-shrink: 0;
}

/* Mobile PK Lock button — above chat button */
#mobile-pk-lock-btn {
    position: fixed;
    bottom: calc(62px + var(--safe-area-bottom));
    right: 170px;
    z-index: 10001;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 2px solid #555;
    border-radius: 50%;
    background-color: rgba(30, 30, 30, 0.9);
    cursor: pointer;
    color: #888;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
#mobile-pk-lock-btn svg {
    pointer-events: none;
}

/* Icon toggle — same as desktop */
#mobile-pk-lock-btn .pk-icon-skull { display: none; }
#mobile-pk-lock-btn .pk-icon-shield { display: block; }
#mobile-pk-lock-btn:not(.active) .pk-icon-skull { display: block; }
#mobile-pk-lock-btn:not(.active) .pk-icon-shield { display: none; }

/* PK Lock ON (safe) — green glow */
#mobile-pk-lock-btn.active {
    border-color: #4CAF50;
    box-shadow: 0 0 8px rgba(76, 175, 80, 0.6);
    color: #c8e6c9;
    background-color: rgba(51, 51, 51, 0.9);
}

/* PK Lock OFF (PVP enabled) — red glow */
#mobile-pk-lock-btn:not(.active) {
    border-color: #ff3333;
    box-shadow: 0 0 8px rgba(255, 51, 51, 0.6);
    color: #ffcccc;
    background-color: rgba(51, 51, 51, 0.9);
}

#virtual-joystick {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(40, 40, 40, 0.85) 0%, rgba(20, 20, 20, 0.7) 100%);
    border: 3px solid rgba(100, 100, 100, 0.6);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
}

#joystick-knob {
    position: absolute;
    width: 45%;
    height: 45%;
    top: 27.5%;
    left: 27.5%;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, #888 0%, #444 50%, #333 100%);
    border: 2px solid #999;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
    transition: transform 0.05s ease-out;
}

/* ======================================
   Virtual D-Pad
   ====================================== */
#virtual-dpad {
    position: absolute;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(40, 40, 40, 0.85) 0%, rgba(20, 20, 20, 0.7) 100%);
    border: 3px solid rgba(100, 100, 100, 0.6);
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
}

.dpad-arrow {
    position: absolute;
    color: rgba(180, 180, 180, 0.7);
    font-size: 16px;
    text-align: center;
    pointer-events: none;
    transition: color 0.1s ease, text-shadow 0.1s ease;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
}

.dpad-arrow.active {
    color: rgba(255, 200, 60, 0.95);
    text-shadow: 0 0 8px rgba(255, 200, 60, 0.5);
}

.dpad-up {
    top: 6%;
    left: 50%;
    transform: translateX(-50%);
}

.dpad-right {
    right: 6%;
    top: 50%;
    transform: translateY(-50%);
}

.dpad-down {
    bottom: 6%;
    left: 50%;
    transform: translateX(-50%);
}

.dpad-left {
    left: 6%;
    top: 50%;
    transform: translateY(-50%);
}

.dpad-center {
    position: absolute;
    width: 20%;
    height: 20%;
    top: 40%;
    left: 40%;
    border-radius: 50%;
    background: rgba(60, 60, 60, 0.6);
    border: 1px solid rgba(100, 100, 100, 0.4);
    transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.dpad-center.turn-active {
    background: rgba(255, 200, 60, 0.3);
    border-color: rgba(255, 200, 60, 0.7);
    box-shadow: 0 0 8px rgba(255, 200, 60, 0.5);
}

/* ======================================
   Mobile Hotbar (Center) - Skill slots
   ====================================== */
#mobile-hotbar {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 5px;
    padding: 0;
    background: none;
    border: none;
    border-radius: 0;
    max-width: none;
}

.mobile-hotbar-slot {
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    background: radial-gradient(circle at 35% 35%, #3a3a3a, #1a1a1a);
    border: 2px solid #555;
    border-radius: 50%;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.mobile-hotbar-slot:active {
    transform: scale(0.95);
    border-color: #888;
}

.mobile-hotbar-slot canvas {
    width: 32px;
    height: 32px;
}

.hotbar-count {
    position: absolute;
    bottom: 1px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 10px;
    font-weight: bold;
    color: #88ff88;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
    pointer-events: none;
    z-index: 1;
}

.mobile-hotbar-duration {
    position: absolute;
    top: 1px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    font-weight: bold;
    color: #ffcc00;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
    pointer-events: none;
    z-index: 2;
    white-space: nowrap;
}

.mobile-slot-key {
    position: absolute;
    bottom: 2px;
    right: 2px;
    font-size: 10px;
    color: #999;
    font-weight: bold;
    text-shadow: 1px 1px 1px black;
}

/* Edit mode toggle button - styled as a hotbar slot */
#mobile-hotbar-edit-btn {
    font-size: 18px;
    color: #999;
}

#mobile-hotbar-edit-btn.active {
    background: radial-gradient(circle at 35% 35%, #4a3000, #1a1000) !important;
    border-color: #ffa000 !important;
    color: #ffa000;
}

/* Hotbar slot feedback animations */
.mobile-hotbar-slot.hotbar-success {
    animation: mobile-hotbar-success 0.2s ease-out;
}

.mobile-hotbar-slot.hotbar-reject {
    animation: mobile-hotbar-reject 0.3s ease-out;
}

@keyframes mobile-hotbar-success {
    0% { transform: scale(1); border-color: #555; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); }
    40% { transform: scale(1.3); border-color: #ffaa00; box-shadow: 0 0 14px 3px rgba(255, 170, 0, 0.8); }
    100% { transform: scale(1); border-color: #555; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    15% { transform: translateX(-5px); }
    30% { transform: translateX(5px); }
    45% { transform: translateX(-4px); }
    60% { transform: translateX(4px); }
    75% { transform: translateX(-2px); }
    90% { transform: translateX(0); }
}

@keyframes mobile-hotbar-reject {
    0%, 100% { transform: translateX(0); border-color: #555; }
    15% { transform: translateX(-4px); border-color: #ff4444; }
    30% { transform: translateX(4px); border-color: #ff4444; }
    45% { transform: translateX(-3px); border-color: #ff3333; }
    60% { transform: translateX(3px); border-color: #ff3333; }
    75% { transform: translateX(-1px); border-color: #ff2222; }
    90% { transform: translateX(0); border-color: #555; }
}

/* Edit mode visual indicator on slots */
.mobile-hotbar-slot.edit-mode {
    border-color: #ffa000 !important;
    animation: edit-pulse 1.5s ease-in-out infinite;
}

@keyframes edit-pulse {
    0%, 100% { box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5); }
    50% { box-shadow: 0 0 8px rgba(255, 160, 0, 0.4); }
}

/* Selected slot in edit mode */
.mobile-hotbar-slot.edit-selected {
    border-color: #fff !important;
    box-shadow: 0 0 12px rgba(255, 255, 255, 0.6) !important;
    animation: none !important;
}

/* Extra Mobile Hotbar — 3 additional slots (toggle via Settings > Controls) */
#mobile-extra-hotbar {
    display: none;
    grid-template-columns: repeat(3, 1fr);
    gap: 5px;
    padding: 0;
    background: none;
    border: none;
    position: fixed;
    z-index: 10000;
    pointer-events: auto;
}

#mobile-extra-hotbar.visible {
    display: grid;
}

/* Landscape: stack extra hotbar vertically */
@media screen and (orientation: landscape) {
    #mobile-extra-hotbar {
        grid-template-columns: 1fr !important;
    }
}

/* Portrait: row layout */
@media screen and (orientation: portrait) {
    #mobile-extra-hotbar {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

.mobile-extra-hotbar-slot {
    flex-shrink: 0;
    width: 48px;
    height: 48px;
    background: radial-gradient(circle at 35% 35%, #2a2a3a, #0a0a1a);
    border: 2px solid #6655aa;
    border-radius: 50%;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

.mobile-extra-hotbar-slot:active {
    transform: scale(0.95);
    border-color: #9988cc;
}

.mobile-extra-hotbar-slot canvas {
    width: 32px;
    height: 32px;
}

.mobile-extra-hotbar-slot .mobile-hotbar-duration {
    position: absolute;
    top: 1px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 9px;
    font-weight: bold;
    color: #ffcc00;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
    pointer-events: none;
    z-index: 2;
    white-space: nowrap;
}

.mobile-extra-hotbar-slot .mobile-slot-key {
    position: absolute;
    bottom: 2px;
    right: 2px;
    font-size: 10px;
    color: #7766bb;
    font-weight: bold;
    text-shadow: 1px 1px 1px black;
}

/* Extra hotbar feedback animations */
.mobile-extra-hotbar-slot.hotbar-success {
    animation: mobile-hotbar-success 0.2s ease-out;
}

.mobile-extra-hotbar-slot.hotbar-reject {
    animation: mobile-hotbar-reject 0.3s ease-out;
}

.mobile-extra-hotbar-slot.edit-mode {
    border-color: #ffa000 !important;
    animation: edit-pulse 1.5s ease-in-out infinite;
}

.mobile-extra-hotbar-slot.edit-selected {
    border-color: #fff !important;
    box-shadow: 0 0 12px rgba(255, 255, 255, 0.6) !important;
    animation: none !important;
}

.mobile-action-btn {
    width: var(--mobile-action-btn-size);
    height: var(--mobile-action-btn-size);
    min-width: var(--mobile-action-btn-size);
    min-height: var(--mobile-action-btn-size);
    border-radius: 50%;
    border: 3px solid;
    color: white;
    font-size: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.5);
    padding: 0;
}

#mobile-fullscreen-btn {
    border: none;
    border-radius: 0;
    box-shadow: none;
    background: transparent;
}

.mobile-action-btn:active {
    transform: scale(0.9);
    filter: brightness(1.2);
}


/* ======================================
   Mobile Chat Button
   ====================================== */
#mobile-chat-btn {
    position: fixed;
    bottom: calc(12px + var(--safe-area-bottom));
    right: 170px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #3a3a3a, #1a1a1a);
    border: 2px solid #555;
    font-size: 20px;
    z-index: 9999;
    align-items: center;
    justify-content: center;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
    padding: 0;
}

#mobile-chat-btn:active {
    transform: scale(0.95);
    border-color: #888;
}

/* ======================================
   Mobile Use-With Targeting Indicator
   ====================================== */
#mobile-use-indicator {
    display: none !important;
    position: fixed;
    top: 42px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.55);
    color: #ddb944;
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 10px;
    z-index: 9999;
    font-family: "Martel", serif;
    -webkit-tap-highlight-color: transparent;
}

#mobile-use-indicator.active {
    display: block !important;
}

.mobile-chat-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ff4444;
    color: white;
    font-size: 9px;
    font-weight: bold;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
    box-sizing: border-box;
}

.mobile-dm-badge {
    right: auto;
    left: -4px;
    background: #44aaff;
}

.mobile-guild-badge {
    top: auto;
    bottom: -4px;
    right: -4px;
    background: #d4a017;
}

/* ======================================
   Mobile Chat — Desktop .lower is hidden on mobile
   (New dedicated mobile chat system uses #mobile-chat instead)
   ====================================== */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    /* Desktop chat panel hidden on mobile — replaced by #mobile-chat */
    #game-wrapper .main .lower {
        display: none !important;
    }

}

/* Menu button in action bar */
#mobile-menu-btn.mobile-action-btn {
    background: linear-gradient(145deg, #555, #333);
    border-color: #888;
    font-size: 18px;
}

/* ======================================
   Landscape Specific Optimizations
   ====================================== */
@media screen and (max-height: 500px) and (orientation: landscape) {

    :root {
        --mobile-action-btn-size: 44px;
        --mobile-joystick-size: 115px;
        --mobile-hotbar-height: 50px;
        --mobile-header-height: 32px;
    }

    /* Compact status bar */
    #mobile-status-bar {
        height: 28px;
    }


    .mobile-bar {
        height: 16px;
    }

    .mobile-bar-text {
        font-size: 10px;
        line-height: 16px;
    }

    #mobile-menu-btn {
        order: 1;
        width: 28px;
        height: 28px;
        min-height: 28px;
        font-size: 14px;
    }

    #mobile-fullscreen-btn {
        order: 2;
        width: 28px;
        height: 28px;
        min-height: 28px;
        font-size: 14px;
    }

    .mobile-bar-container {
        order: 3;
    }

    #mobile-tab-bar {
        order: 4;
    }

    /* Action bar positioning for landscape */
    #mobile-action-bar {
        bottom: calc(12px + var(--safe-area-bottom));
        left: 8px;
        right: 8px;
    }

    /* Smaller touch targets in compact landscape */
    .mobile-action-btn {
        font-size: 16px;
    }

    .mobile-hotbar-slot {
        width: 42px;
        height: 42px;
    }

    .mobile-hotbar-slot canvas {
        width: 29px;
        height: 29px;
    }

    .mobile-extra-hotbar-slot {
        width: 42px;
        height: 42px;
    }

    .mobile-extra-hotbar-slot canvas {
        width: 29px;
        height: 29px;
    }

    #mobile-hotbar {
        gap: 4px;
    }

    #mobile-extra-hotbar {
        gap: 4px;
    }

    /* Canvas: maintain aspect ratio — fill height, auto width */
    #screen {
        width: auto !important;
        height: 100% !important;
        aspect-ratio: 480 / 352;
        max-width: 100%;
    }

    .canvas-wrapper {
        width: auto !important;
        height: 100% !important;
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
    }
}

/* ======================================
   Portrait Mode (Phone Upright)
   ====================================== */
@media screen and (max-width: 768px) and (orientation: portrait) {

    :root {
        --mobile-action-btn-size: 56px;
        --mobile-joystick-size: 110px;
    }

    /* Swap login background to portrait version */
    #login-wrapper #login-inner img.bg-landscape { display: none !important; }
    #login-wrapper #login-inner img.bg-portrait { display: block !important; }

    /* Portrait layout: column with canvas centered vertically */
    #game-wrapper .main {
        background-color: #444;
        background-image: url("/png/bg.png");
        height: 100dvh !important;
        min-height: 100dvh !important;
        justify-content: center !important;
    }

    /* Canvas: maintain landscape aspect ratio instead of stretching */
    #screen {
        width: 100% !important;
        height: auto !important;
        aspect-ratio: 480 / 352;
    }

    /* Canvas wrapper: don't force full height, let canvas size naturally */
    .canvas-wrapper {
        width: 100% !important;
        height: auto !important;
    }

    /* Upper area: shrink to fit canvas, don't stretch to fill */
    #game-wrapper .main .upper {
        flex: 0 0 auto !important;
    }

    /* Hide HP/mana from top status bar in portrait (shown below canvas instead) */
    #mobile-status-bar .mobile-bar-container {
        display: none !important;
    }

    .mobile-container-body {
        max-height: 172px !important;
    }

    /* Portrait bars: show below canvas */
    #portrait-bars {
        display: flex !important;
        flex-direction: row;
        gap: 8px;
        padding: 4px 8px;
        background: #111;
        border-top: 1px solid #333;
        flex: 0 0 auto;
    }

    /* Bottom bar positions — more room in portrait */
    #mobile-action-bar {
        bottom: calc(20px + var(--safe-area-bottom));
    }

    /* Smaller hotbar in portrait — 10% reduction */
    .mobile-hotbar-slot {
        width: 43px;
        height: 43px;
    }

    .mobile-hotbar-slot canvas {
        width: 29px;
        height: 29px;
    }

    .mobile-extra-hotbar-slot {
        width: 43px;
        height: 43px;
    }

    .mobile-extra-hotbar-slot canvas {
        width: 29px;
        height: 29px;
    }

    #mobile-hotbar {
        gap: 4px;
    }

    #mobile-extra-hotbar {
        gap: 4px;
    }

}

/* ======================================
   iOS Safari / Notch Support
   ====================================== */
@supports (padding: env(safe-area-inset-top)) {
    #mobile-status-bar {
        padding-top: env(safe-area-inset-top);
        height: calc(var(--mobile-header-height) + env(safe-area-inset-top));
        /* Opaque background so iOS status bar text (clock/battery) is readable over it */
        background: linear-gradient(to bottom, rgba(0, 0, 0, 0.95), rgba(0, 0, 0, 0.8));
    }
}

/* Prevent iOS rubber banding */
@supports (-webkit-touch-callout: none) {

    html,
    body {
        height: 100%;
        overflow: hidden;
    }

    #game-wrapper {
        height: -webkit-fill-available;
    }
}

/* ======================================
   iPhone Notch/Home Indicator Fallback
   JS adds .iphone-notch class when env() safe area values are unreliable
   Status bar moved to BOTTOM to avoid iOS top gesture zone conflicts
   ====================================== */
.iphone-notch {
    background: #000 !important;
}

/* Move status bar to bottom */
.iphone-notch #mobile-status-bar {
    top: auto;
    bottom: 0;
    padding-bottom: 14px;
    height: calc(var(--mobile-header-height) + 14px);
    border-bottom: none;
    border-top: 1px solid #444;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.95), rgba(0, 0, 0, 0.8));
}

/* Panels that normally sit below the top bar — move to top edge */
.iphone-notch #mobile-container-panel {
    top: 5px !important;
}

.iphone-notch .equipment.wrapper {
    top: 5px !important;
}

.iphone-notch .mobile-tab-panel {
    top: 5px !important;
}

/* Bottom elements sit above the status bar */
.iphone-notch #mobile-action-bar {
    bottom: calc(15px + var(--mobile-header-height) + 14px) !important;
}

.iphone-notch #mobile-chat-btn {
    bottom: calc(12px + var(--mobile-header-height) + 14px) !important;
}

.iphone-notch #mobile-pk-lock-btn {
    bottom: calc(62px + var(--mobile-header-height) + 14px) !important;
}

/* ======================================
   Layout Tip Toast
   ====================================== */
#layout-tip-toast {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10002;
    background: rgba(0, 0, 0, 0.85);
    border: 1px solid #555;
    border-radius: 8px;
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
    color: #ccc;
    white-space: nowrap;
    animation: toast-fade-in 0.4s ease;
    pointer-events: auto;
    touch-action: manipulation;
}

#layout-tip-toast.toast-hiding {
    animation: toast-fade-out 0.4s ease forwards;
}

#layout-tip-btn {
    background: #cc8800;
    color: #fff;
    border: none;
    border-radius: 4px;
    padding: 6px 12px;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

#layout-tip-close {
    background: none;
    border: none;
    color: #888;
    font-size: 16px;
    cursor: pointer;
    padding: 2px 4px;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

@keyframes toast-fade-in {
    from { opacity: 0; transform: translate(-50%, -50%) scale(0.95); }
    to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

@keyframes toast-fade-out {
    from { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    to { opacity: 0; transform: translate(-50%, -50%) scale(0.95); }
}

/* ======================================
   Debug Statistics - Make Smaller on Mobile
   ====================================== */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {
    #debug-statistics {
        font-size: 8px !important;
        top: 40px !important;
        left: 4px !important;
        max-width: 200px;
        background: rgba(0, 0, 0, 0.7);
        padding: 4px;
        border-radius: 4px;
    }
    #debug-statistics:empty {
        display: none;
    }
}

/* ======================================
   Left-Side Tab Position (.mobile-tab-left)
   ====================================== */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    /* Equipment panel: flip to left */
    .equipment.wrapper.mobile-tab-left {
        right: auto !important;
        left: 5px !important;
        transform-origin: top left !important;
    }

    /* Tab panels: flip to left */
    .mobile-tab-panel.mobile-tab-left {
        right: auto !important;
        left: 5px !important;
        transform-origin: top left;
    }

}

/* ======================================
   Mobile Panel Headers (Close/Minimize)
   ====================================== */
.mobile-panel-header {
    display: none;
}

@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    .mobile-panel-header {
        display: flex;
        align-items: center;
        height: 22px;
        padding-left: 4px;
        background-image: url("/png/bg4.png");
        color: grey;
    }

    .mobile-panel-header > .symbol-button {
        flex: 0 0 auto;
    }

    .mobile-panel-header > .title {
        flex: 1 1 auto;
        padding-left: 8px;
        pointer-events: none;
    }

    /* Minimized state - hide all content except header */
    .mobile-minimized > :not(.mobile-panel-header) {
        display: none !important;
    }

    /* Item drag highlight on drop target slot */
    .slot.mobile-drag-target {
        outline: 2px solid #44ff44 !important;
        outline-offset: -2px;
        background-color: rgba(68, 255, 68, 0.15) !important;
    }
}

/* ======================================
   Mobile Login Screen
   ====================================== */
@media screen and (max-height: 500px),
screen and (max-width: 768px) {

    /* Remove min-width constraints */
    #login-wrapper {
        min-width: unset !important;
        min-height: unset !important;
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    #login-wrapper #login-inner {
        min-width: unset !important;
        min-height: unset !important;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }

    /* Background image covers screen */
    #login-wrapper #login-inner > img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
        z-index: 0;
    }

    /* Version info - smaller, top-left */
    #login-wrapper #login-inner .login-version {
        z-index: 2;
        font-size: 10px;
        top: 4px;
        left: 6px;
        text-shadow: 1px 1px 3px black, -1px -1px 3px black;
    }

    /* Buttons - centered on screen */
    #login-wrapper #login-inner .login-settings {
        z-index: 2;
        position: absolute;
        left: 50% !important;
        top: 50% !important;
        bottom: auto !important;
        transform: translate(-50%, -50%);
        width: auto !important;
        padding: 8px !important;
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 4px;
        background-color: rgba(0, 0, 0, 0.6);
        border-radius: 8px;
        max-width: 90vw;
    }

    #login-wrapper #login-inner .login-settings button {
        width: auto !important;
        min-width: 80px;
        height: 28px !important;
        font-size: 11px;
        margin: 0 !important;
    }

    #login-wrapper #login-inner .login-settings #login-display-mode {
        width: auto !important;
        min-width: 80px;
        height: 28px !important;
        font-size: 11px;
        margin: 0 !important;
    }

    /* Login modals - fit mobile screen */
    #login-wrapper .modal {
        max-width: 85vw !important;
        z-index: 30000;
    }

    #login-wrapper .modal .modal-body {
        padding: 8px;
    }

    #login-wrapper .modal .modal-body input,
    #login-wrapper .modal .modal-body select {
        width: 100%;
        box-sizing: border-box;
        font-size: 14px;
        padding: 6px;
    }

    #login-wrapper .modal .modal-body button {
        font-size: 12px;
        padding: 6px 12px;
    }
}

/* ======================================
   Tablet-Specific Overrides
   Touch device with screen larger than phone
   ====================================== */
body.force-tablet {

    /* Canvas: don't stretch to fill — use CSS transform scaling instead.
       This prevents double-scaling (CSS stretch + transform) and preserves aspect ratio.
       The transform is applied by JS getResolutionScale() via setScale(). */
    #screen {
        width: auto !important;
        height: auto !important;
        image-rendering: pixelated;
        image-rendering: -moz-crisp-edges;
        image-rendering: crisp-edges;
    }

    /* Center the canvas within its wrapper */
    .canvas-wrapper {
        display: flex !important;
        justify-content: center !important;
        align-items: center !important;
    }

    /* --- CSS Variable Overrides --- */
    :root {
        --mobile-action-btn-size: 60px;
        --mobile-joystick-size: 120px;
        --mobile-header-height: 44px;
    }

    /* --- Status Bar (taller, bigger text) --- */
    #mobile-status-bar {
        height: 44px;
        padding: 4px 16px;
        gap: 16px;
    }

    .mobile-bar {
        height: 24px;
    }

    .mobile-bar-text {
        font-size: 13px;
        line-height: 24px;
    }

    #mobile-menu-btn {
        width: 36px;
        height: 36px;
        min-height: 36px;
        font-size: 18px;
    }

    /* Tab bar buttons */
    .mobile-tab {
        padding: 0 12px;
        font-size: 15px;
    }

    /* --- Panels: remove phone cramping --- */
    .window {
        width: 250px !important;
        transform: none !important;
        top: 50px !important;
        font-size: 12px;
    }

    .equipment.wrapper {
        display: grid !important;
        grid-template-columns: auto auto auto auto;
        width: max-content !important;
        transform: none !important;
        top: 50px !important;
        align-items: start !important;
    }

    .equipment.wrapper > .mobile-panel-header {
        grid-column: 1 / -1;
    }

    #mobile-container-panel {
        width: 210px;
        transform: none !important;
        top: 50px;
        left: 5px;
        right: auto;
    }

    .minimap-container.mobile-tab-panel {
        width: auto !important;
    }

    #battle-window {
        top: 50px !important;
    }

    /* Panel headers */
    .mobile-panel-header {
        height: 26px;
    }

    /* --- Slot Sizes (larger touch targets) --- */
    .slot {
        width: 44px !important;
        height: 44px !important;
    }

    .slot canvas {
        width: 44px !important;
        height: 44px !important;
    }

    /* Equipment slots */
    .equipment.wrapper .slot {
        width: 40px !important;
        height: 40px !important;
    }

    .equipment.wrapper .slot canvas {
        width: 40px !important;
        height: 40px !important;
    }

    /* Container / window slots */
    .container-content .slot {
        width: 40px !important;
        height: 40px !important;
        margin: 2px !important;
    }

    .container-content .slot canvas {
        width: 40px !important;
        height: 40px !important;
    }

    .window .slot {
        width: 40px !important;
        height: 40px !important;
        margin: 2px !important;
    }

    .window .slot canvas {
        width: 40px !important;
        height: 40px !important;
    }

    /* --- Hotbar Slots (bigger for tablet) --- */
    .mobile-hotbar-slot {
        width: 56px;
        height: 56px;
    }

    #mobile-hotbar {
        gap: 8px;
    }

    .mobile-hotbar-slot canvas {
        width: 36px;
        height: 36px;
    }

    /* --- Chat Button (bigger, repositioned) --- */
    #mobile-chat-btn {
        width: 50px;
        height: 50px;
        font-size: 22px;
        right: 200px;
    }

    /* --- PK Lock Button (above chat) --- */
    #mobile-pk-lock-btn {
        bottom: 68px;
        right: 200px;
        width: 50px;
        height: 50px;
    }

    .mobile-chat-badge {
        min-width: 18px;
        height: 18px;
        font-size: 10px;
    }

    /* --- Mobile Container Tabs (bigger touch targets) --- */
    .mobile-container-tab {
        padding: 3px 6px;
        font-size: 11px;
        height: 24px;
    }

    /* --- Capacity text --- */
    #player-capacity {
        font-size: 12px !important;
    }

    /* --- Equipment column spacing --- */
    .equipment-column {
        gap: 4px !important;
    }

    /* --- Left-side overrides for tablet --- */
    .equipment.wrapper.mobile-tab-left {
        transform: none !important;
    }

    .mobile-tab-panel.mobile-tab-left {
        transform: none !important;
    }

    /* --- Login screen: bigger buttons for tablet --- */
    #login-wrapper #login-inner .login-settings button {
        min-width: 100px;
        height: 36px !important;
        font-size: 13px;
    }

    #login-wrapper .modal {
        max-width: 500px !important;
    }

    #login-wrapper .modal .modal-body input,
    #login-wrapper .modal .modal-body select {
        font-size: 16px;
        padding: 8px;
    }

    #login-wrapper .modal .modal-body button {
        font-size: 14px;
        padding: 8px 16px;
    }
}

/* ======================================
   Layout Editor Mode
   ====================================== */

/* Toolbar at top of screen — hidden by default, shown via JS */
#layout-editor-toolbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 44px;
    background: rgba(0, 0, 0, 0.92);
    border-bottom: 2px solid #ffa000;
    display: none;
    align-items: center;
    justify-content: center;
    gap: 12px;
    z-index: 20000;
    color: #fff;
    font-size: 14px;
    font-family: "Martel", serif;
}

#layout-editor-toolbar button {
    padding: 6px 16px;
    border: 1px solid #666;
    background: #333;
    color: #fff;
    border-radius: 4px;
    font-size: 13px;
    cursor: pointer;
}

#layout-editor-toolbar #layout-save-btn {
    border-color: #ffa000;
    color: #ffa000;
}

#layout-editor-toolbar #layout-reset-btn {
    border-color: #ff4444;
    color: #ff4444;
}

/* Drag handle overlay on each configurable element */
.layout-editor-handle {
    position: absolute;
    top: -4px;
    left: -4px;
    right: -4px;
    bottom: -4px;
    border: 2px dashed #ffa000;
    border-radius: 8px;
    animation: edit-pulse 1.5s ease-in-out infinite;
    z-index: 20001;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    touch-action: none;
    background: rgba(255, 160, 0, 0.08);
}

.layout-editor-handle .handle-label {
    color: #ffa000;
    font-size: 11px;
    font-weight: bold;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
    margin-bottom: 4px;
    pointer-events: none;
}

.layout-editor-handle .handle-scale-controls {
    display: flex;
    gap: 10px;
    pointer-events: auto;
}

.layout-editor-handle .handle-scale-btn {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    border: 2px solid #ffa000;
    background: rgba(0, 0, 0, 0.8);
    color: #ffa000;
    font-size: 18px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    line-height: 1;
}

/* Allow handle border to show outside container during editing */
body.layout-edit-mode #mobile-container-panel,
body.force-mobile.layout-edit-mode #mobile-container-panel {
    overflow: visible !important;
}

/* Dim game canvas during layout editing */
body.layout-edit-mode #screen {
    opacity: 0.3;
    pointer-events: none;
}

/* Hide status bar interaction during editing */
body.layout-edit-mode #mobile-status-bar {
    opacity: 0.3;
    pointer-events: none;
}

/* Custom layout: make joystick and hotbar independently positioned */
body.layout-custom #mobile-joystick-zone {
    position: fixed !important;
    z-index: 10001;
    pointer-events: auto;
}

body.layout-custom #mobile-hotbar {
    position: fixed !important;
    z-index: 10001;
    pointer-events: auto;
}

body.layout-custom #mobile-extra-hotbar {
    z-index: 10001;
}

body.layout-custom #mobile-equipment-panel {
    position: fixed !important;
    z-index: 10001;
    pointer-events: auto;
    top: auto !important;
    right: auto !important;
    transform-origin: bottom left !important;
}

body.layout-custom #mobile-container-panel {
    position: fixed !important;
    z-index: 10001;
    pointer-events: auto;
    top: auto !important;
    right: auto !important;
    transform-origin: bottom left !important;
}

/* ======================================
   Forced Display Mode Overrides
   Applied via JS body class when user manually selects display mode
   ====================================== */

/* --- Force Desktop: undo mobile CSS even when media queries match --- */
body.force-desktop .desktop-only { display: block !important; }
body.force-desktop .mobile-only { display: none !important; }
body.force-desktop { overflow: auto !important; touch-action: auto !important; position: static !important; }
body.force-desktop #game-wrapper { flex-direction: column !important; min-width: 960px !important; min-height: 600px !important; width: auto !important; height: auto !important; }
body.force-desktop #game-wrapper .oogwrap { display: flex !important; position: static !important; width: auto !important; height: auto !important; overflow: visible !important; pointer-events: auto !important; z-index: auto !important; }
body.force-desktop .oogwrap .minimap-container,
body.force-desktop .oogwrap .character-status { display: block !important; }
body.force-desktop #game-wrapper .main .middle { display: flex !important; }
body.force-desktop .hotbar { display: flex !important; }
body.force-desktop #screen { width: auto !important; height: auto !important; }
body.force-desktop .canvas-wrapper { width: auto !important; height: auto !important; }
body.force-desktop .window { position: relative !important; top: auto !important; left: auto !important; right: auto !important; transform: none !important; width: auto !important; max-width: none !important; max-height: none !important; font-size: inherit !important; z-index: auto !important; }
body.force-desktop .window[containerIndex] { display: block !important; }
body.force-desktop #mobile-status-bar { display: none !important; }
body.force-desktop #mobile-action-bar { display: none !important; }
body.force-desktop #mobile-container-panel { display: none !important; }
body.force-desktop #game-wrapper .main .lower { display: flex !important; }
body.force-desktop .mobile-panel-header { display: none !important; }
body.force-desktop #chat-resizer { display: block !important; }
body.force-desktop .equipment-buttons { display: flex !important; }
body.force-desktop .equipment.wrapper { position: static !important; top: auto !important; right: auto !important; left: auto !important; width: auto !important; transform: none !important; }
body.force-desktop #battle-window { position: relative !important; top: auto !important; right: auto !important; left: auto !important; }
body.force-desktop .mobile-tab-panel { display: none !important; }
body.force-desktop #debug-statistics { font-size: inherit !important; top: auto !important; left: auto !important; }
body.force-desktop #login-wrapper { min-width: 960px !important; min-height: 600px !important; width: auto !important; height: auto !important; }
body.force-desktop #login-wrapper #login-inner { min-width: 640px !important; min-height: 480px !important; }
body.force-desktop #login-wrapper #login-inner img.bg-landscape { display: block !important; }
body.force-desktop #login-wrapper #login-inner img.bg-portrait { display: none !important; }

/* --- Force Mobile: apply mobile CSS even when media queries don't match --- */
body.force-mobile .desktop-only { display: none !important; }
body.force-mobile .mobile-only { display: flex !important; }
body.force-mobile #portrait-bars { display: none !important; }

body.force-mobile {
    overflow: hidden !important;
    touch-action: none !important;
    -webkit-touch-callout: none !important;
    -webkit-user-select: none !important;
    user-select: none !important;
    position: fixed !important;
    width: 100% !important;
    height: 100% !important;
}

body.force-mobile #game-wrapper {
    flex-direction: row !important;
    min-width: unset !important;
    min-height: unset !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    overflow: hidden !important;
    padding-bottom: var(--safe-area-bottom) !important;
    box-sizing: border-box !important;
}

body.force-mobile #game-wrapper .oogwrap {
    display: block !important;
    position: absolute !important;
    top: 0 !important;
    right: 0 !important;
    width: 0 !important;
    height: 0 !important;
    overflow: visible !important;
    pointer-events: none !important;
    z-index: 9990 !important;
}

body.force-mobile .oogwrap .minimap-container,
body.force-mobile .oogwrap .character-status { display: none !important; }
body.force-mobile .equipment-options-wrapper { display: block !important; pointer-events: auto !important; }

body.force-mobile #game-wrapper .main {
    flex: 1 1 auto !important;
    display: flex !important;
    flex-direction: column !important;
    width: 100% !important;
    height: 100% !important;
    min-height: 0 !important;
    overflow: hidden !important;
    position: relative !important;
}

body.force-mobile #game-wrapper .main .middle { display: none !important; }

body.force-mobile #game-wrapper .main .upper {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    overflow: hidden !important;
}

body.force-mobile .canvas-wrapper { width: 100% !important; height: 100% !important; position: relative !important; }
body.force-mobile #pk-lock-btn { display: none !important; }
body.force-mobile .canvas-notification { z-index: 10002 !important; }
body.force-mobile #screen { width: 100% !important; height: 100% !important; display: block !important; }
body.force-mobile #chat-resizer { display: none !important; }
body.force-mobile .hotbar { display: none !important; }

body.force-mobile .window {
    pointer-events: auto !important;
    position: fixed !important;
    top: 170px !important;
    left: auto !important;
    right: 5px !important;
    width: 190px !important;
    max-width: 60vw !important;
    max-height: 60vh !important;
    z-index: 9995 !important;
    overflow: visible !important;
    font-size: 11px !important;
}

body.force-mobile #battle-window { z-index: 9990 !important; top: 170px !important; right: 5px !important; left: auto !important; }
body.force-mobile #trade-window { top: 50% !important; left: 50% !important; right: auto !important; transform: translate(-50%, -50%) !important; z-index: 99999 !important; width: 220px !important; }
body.force-mobile .window[containerIndex] { display: none !important; }
body.force-mobile .window[containerIndex="1"] { display: flex !important; position: fixed !important; z-index: 9997 !important; width: 180px !important; top: 50% !important; left: 50% !important; right: auto !important; transform: translate(-50%, -50%) !important; max-height: 70vh !important; border: 1px solid #555 !important; }
body.force-mobile .window[containerIndex="1"] > .header > button { min-width: 28px; min-height: 28px; }
body.force-mobile .window[containerIndex="1"] > .body { max-height: 50vh !important; overflow-y: auto !important; }
body.force-mobile .window[containerIndex="1"] .slot { width: 40px !important; height: 40px !important; }
body.force-mobile .window[containerIndex="1"] .slot canvas { width: 40px !important; height: 40px !important; }

body.force-mobile #mobile-container-panel {
    position: fixed !important;
    z-index: 9996 !important;
    width: 150px !important;
    background-image: url("/png/bg3.png") !important;
    transform: scale(0.85) !important;
    flex-direction: column !important;
    overflow: hidden !important;
    pointer-events: auto !important;
    left: 5px !important;
    right: auto !important;
    top: 37px !important;
    transform-origin: top left !important;
    border: 1px solid #333 !important;
}

body.force-mobile .window .header { height: 20px !important; line-height: 20px !important; font-size: 10px !important; }
body.force-mobile .window .body { max-height: 40vh !important; overflow-y: auto !important; display: flex !important; flex-wrap: wrap !important; justify-content: center !important; padding: 2px !important; min-height: 0 !important; }
body.force-mobile .window .footer { display: none !important; }
body.force-mobile #friend-window .footer { display: block !important; }
body.force-mobile .window .slot { width: 32px !important; height: 32px !important; margin: 1px !important; border: 1px solid #444 !important; }
body.force-mobile .container-content .slot { width: 32px !important; height: 32px !important; margin: 1px !important; border: 1px solid #444 !important; }
body.force-mobile .container-content .slot canvas { width: 32px !important; height: 32px !important; }
body.force-mobile .slot { width: 36px !important; height: 36px !important; }
body.force-mobile .slot canvas { width: 36px !important; height: 36px !important; }

body.force-mobile .equipment.wrapper {
    position: fixed !important;
    top: 37px !important;
    right: 5px !important;
    left: auto !important;
    z-index: 9998 !important;
    background-image: url("/png/bg3.png") !important;
    padding: 0 2px 2px 2px !important;
    width: 140px !important;
    flex-wrap: wrap !important;
}

body.force-mobile .equipment.wrapper .slot { width: 30px !important; height: 30px !important; }
body.force-mobile .equipment.wrapper .slot canvas { width: 30px !important; height: 30px !important; }
body.force-mobile #player-capacity { font-size: 10px !important; padding: 2px !important; }
body.force-mobile .equipment-buttons { display: none !important; }
body.force-mobile .equipment-column { gap: 2px !important; }

body.force-mobile #mobile-status-bar { display: flex !important; }
body.force-mobile #mobile-action-bar { display: flex !important; }

/* Desktop .lower hidden in force-mobile — replaced by #mobile-chat */
body.force-mobile #game-wrapper .main .lower { display: none !important; }

body.force-mobile .mobile-panel-header {
    display: flex !important;
    align-items: center !important;
    height: 22px !important;
    padding-left: 4px !important;
    background-image: url("/png/bg4.png") !important;
    color: grey !important;
}

body.force-mobile .mobile-tab-panel {
    display: block !important;
    position: fixed !important;
    top: 37px !important;
    right: 5px !important;
    left: auto !important;
    z-index: 9998 !important;
    pointer-events: auto !important;
}

body.force-mobile .minimap-container.mobile-tab-panel {
    display: block !important;
    position: fixed !important;
    pointer-events: auto !important;
    background-image: url("/png/bg3.png") !important;
    width: auto !important;
    margin: 0 !important;
    padding: 4px !important;
}

body.force-mobile .mobile-minimized > :not(.mobile-panel-header) { display: none !important; }
body.force-mobile .slot.mobile-drag-target { outline: 2px solid #44ff44 !important; outline-offset: -2px; background-color: rgba(68, 255, 68, 0.15) !important; }

body.force-mobile #debug-statistics { font-size: 8px !important; top: 40px !important; left: 4px !important; max-width: 200px !important; background: rgba(0, 0, 0, 0.7) !important; padding: 4px !important; border-radius: 4px !important; }
body.force-mobile #debug-statistics:empty { display: none !important; }

body.force-mobile #login-wrapper { min-width: unset !important; min-height: unset !important; width: 100vw !important; height: 100vh !important; height: 100dvh !important; overflow: hidden !important; }

/* --- Force Tablet: override phone-sized force-mobile with tablet sizing --- */
/* Applied alongside force-mobile for larger panels, no scaling, bigger touch targets */
body.force-tablet #screen { width: auto !important; height: auto !important; image-rendering: pixelated; image-rendering: -moz-crisp-edges; image-rendering: crisp-edges; }
body.force-tablet .canvas-wrapper { display: flex !important; justify-content: center !important; align-items: center !important; }
body.force-tablet #mobile-status-bar { height: 44px !important; padding: 4px 16px !important; gap: 16px !important; }
body.force-tablet .mobile-bar { height: 24px !important; }
body.force-tablet .mobile-bar-text { font-size: 13px !important; line-height: 24px !important; }
body.force-tablet #mobile-menu-btn { width: 36px !important; height: 36px !important; min-height: 36px !important; font-size: 18px !important; }
body.force-tablet .mobile-tab { padding: 0 12px !important; font-size: 15px !important; }
body.force-tablet .window { width: 250px !important; transform: none !important; top: 50px !important; font-size: 12px !important; }
body.force-tablet .equipment.wrapper { display: grid !important; grid-template-columns: auto auto auto auto; width: max-content !important; transform: none !important; top: 50px !important; align-items: start !important; }
body.force-tablet .equipment.wrapper > .mobile-panel-header { grid-column: 1 / -1; }
body.force-tablet #mobile-container-panel { width: 210px !important; transform: none !important; top: 50px !important; left: 5px !important; right: auto !important; }
body.force-tablet .mobile-container-body { max-height: 50vh !important; overflow-y: auto !important; -webkit-overflow-scrolling: touch !important; }
body.force-tablet .minimap-container.mobile-tab-panel { width: auto !important; padding: 4px !important; }
body.force-tablet .minimap-container.mobile-tab-panel .minimap-wrapper { width: 150px !important; height: 150px !important; }
body.force-tablet .minimap-container.mobile-tab-panel .minimap-wrapper canvas { width: 150px !important; height: 150px !important; }
body.force-tablet .minimap-container.mobile-tab-panel .button-stack { position: relative !important; right: auto !important; top: auto !important; width: 100% !important; flex-direction: row !important; justify-content: center !important; margin-top: 4px !important; }
body.force-tablet .minimap-container.mobile-tab-panel .button-stack .symbol-button { width: 26px !important; height: 26px !important; font-size: 13px !important; }
body.force-tablet .minimap-container.mobile-tab-panel .crosshair { left: 71px !important; top: 67px !important; }
body.force-tablet #battle-window { top: 50px !important; }
body.force-tablet .mobile-panel-header { height: 26px !important; }
body.force-tablet .slot { width: 44px !important; height: 44px !important; background-size: 100% 100% !important; }
body.force-tablet .slot canvas { width: 44px !important; height: 44px !important; }
body.force-tablet .equipment.wrapper .slot { width: 40px !important; height: 40px !important; background-size: 100% 100% !important; }
body.force-tablet .equipment.wrapper .slot canvas { width: 40px !important; height: 40px !important; }
body.force-tablet .container-content .body { display: flex !important; flex-wrap: wrap !important; justify-content: center !important; padding: 2px !important; }
body.force-tablet .container-content .slot { width: 44px !important; height: 44px !important; margin: 2px !important; }
body.force-tablet .container-content .slot canvas { width: 44px !important; height: 44px !important; }
body.force-tablet .window .slot { width: 40px !important; height: 40px !important; margin: 2px !important; }
body.force-tablet .window .slot canvas { width: 40px !important; height: 40px !important; }
body.force-tablet .mobile-hotbar-slot { width: 56px; height: 56px; }
body.force-tablet #mobile-hotbar { gap: 8px; }
body.force-tablet .mobile-hotbar-slot canvas { width: 36px; height: 36px; }
body.force-tablet .mobile-action-btn { width: 60px !important; height: 60px !important; min-width: 60px !important; min-height: 60px !important; }
body.force-tablet #mobile-joystick-zone { width: 120px !important; height: 120px !important; }
body.force-tablet #mobile-chat-btn { width: 50px !important; height: 50px !important; font-size: 22px !important; right: 200px !important; }
body.force-tablet #mobile-pk-lock-btn { bottom: 68px !important; right: 200px !important; width: 50px !important; height: 50px !important; }
body.force-tablet .mobile-chat-badge { min-width: 18px !important; height: 18px !important; font-size: 10px !important; }
body.force-tablet .mobile-container-tab { padding: 3px 6px !important; font-size: 11px !important; height: 24px !important; }
body.force-tablet #player-capacity { font-size: 12px !important; }
body.force-tablet .equipment-column { gap: 4px !important; }
body.force-tablet #login-wrapper .modal { max-width: 500px !important; }
body.force-tablet #login-wrapper #login-inner .login-settings button { min-width: 100px !important; height: 36px !important; font-size: 13px !important; }

/* --- Force Mobile: portrait aspect ratio fix --- */
@media screen and (orientation: portrait) {
    body.force-mobile #game-wrapper .main { background-color: #444 !important; background-image: url("/png/bg.png") !important; }
    body.force-mobile #screen { width: 100% !important; height: auto !important; aspect-ratio: 480 / 352; }
    body.force-mobile .canvas-wrapper { width: 100% !important; height: auto !important; }
    body.force-mobile #game-wrapper .main .upper { flex: 0 0 auto !important; }
    body.force-mobile #mobile-status-bar .mobile-bar-container { display: none !important; }
    body.force-mobile #portrait-bars { display: flex !important; flex-direction: row; gap: 8px; padding: 4px 8px; background: #111; border-top: 1px solid #333; flex: 0 0 auto; }
    body.force-mobile #mobile-action-bar { bottom: 20px !important; }
    body.force-mobile #login-wrapper #login-inner img.bg-landscape { display: none !important; }
    body.force-mobile #login-wrapper #login-inner img.bg-portrait { display: block !important; }
}

/* --- Force Mobile: landscape aspect ratio fix --- */
@media screen and (orientation: landscape) {
    body.force-mobile #screen { width: auto !important; height: 100% !important; aspect-ratio: 480 / 352; max-width: 100%; }
    body.force-mobile .canvas-wrapper { width: auto !important; height: 100% !important; display: flex !important; justify-content: center !important; align-items: center !important; }
}

/* --- Force Tablet: override force-mobile media queries for transform-based scaling ---
   Tablet uses CSS transform: scale(N) on a 480x352 canvas, centered in wrapper.
   Force-mobile rules set width/height/aspect-ratio for CSS-based stretching which
   conflicts with the transform approach. Override them back to auto. */
@media screen and (orientation: portrait) {
    body.force-tablet #screen { width: auto !important; height: auto !important; aspect-ratio: unset !important; }
    body.force-tablet .canvas-wrapper { width: 100% !important; height: 100% !important; }
    body.force-tablet #game-wrapper .main .upper { flex: 1 1 auto !important; }
}

@media screen and (orientation: landscape) {
    body.force-tablet #screen { width: auto !important; height: auto !important; aspect-ratio: unset !important; max-width: unset !important; }
    body.force-tablet .canvas-wrapper { width: 100% !important; height: 100% !important; }
}

/* ======================================
   Aspect Ratio: Stretch Mode Override
   When body.canvas-stretch is set, revert to full stretch (no aspect-ratio)
   ====================================== */
body.canvas-stretch #screen {
    aspect-ratio: unset !important;
    width: 100% !important;
    height: 100% !important;
    max-width: none !important;
}

body.canvas-stretch .canvas-wrapper {
    width: 100% !important;
    height: 100% !important;
}

body.canvas-stretch #game-wrapper .main .upper {
    flex: 1 1 auto !important;
}

body.canvas-stretch #game-wrapper .main {
    background-color: unset !important;
    background-image: unset !important;
}

/* --- Force Tablet Stretch: disable CSS transform, let width/height 100% handle sizing --- */
body.canvas-stretch.force-tablet #screen {
    transform: none !important;
}

/* ======================================
   Mobile Push Indicator (Creature Drag)
   Shown when long-press dragging a creature
   ====================================== */
.mobile-push-float {
    background: rgba(255, 170, 0, 0.25);
    border: 2px solid rgba(255, 170, 0, 0.7);
    border-radius: 4px;
}

/* ======================================
   Mobile Condition Bubble
   Floating popup showing active conditions
   when tapping the health bar
   ====================================== */
#condition-bubble {
    position: fixed;
    transform: translateX(-50%) translateY(-100%);
    margin-top: -6px;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 4px;
    padding: 5px 8px;
    background: rgba(15, 15, 15, 0.92);
    border: 1px solid #555;
    border-radius: 6px;
    z-index: 10002;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

/* Landscape: show below the health bar */
#condition-bubble.below {
    transform: translateX(-50%);
    margin-top: 6px;
}

#condition-bubble.visible {
    opacity: 1;
    visibility: visible;
}

#condition-bubble img {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* Arrow pointing down toward the health bar (portrait/above) */
#condition-bubble::after {
    content: "";
    position: absolute;
    bottom: -5px;
    left: 50%;
    transform: translateX(-50%);
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 5px solid rgba(15, 15, 15, 0.92);
    border-bottom: none;
}

/* Arrow pointing up toward the health bar (landscape/below) */
#condition-bubble.below::after {
    bottom: auto;
    top: -5px;
    border-top: none;
    border-bottom: 5px solid rgba(15, 15, 15, 0.92);
}

