/* ============================================
   KODEN — LOADING SCREEN
   ============================================ */

#koden-loader {
    position: fixed;
    inset: 0;
    z-index: 99999;
    background: #0F0F12;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 32px;
    transition: opacity 0.5s ease, transform 0.5s ease;
}

#koden-loader.fade-out {
    opacity: 0;
    transform: scale(1.02);
    pointer-events: none;
}

/* Logo */
.loader-logo {
    font-family: sans-serif;
    font-size: 2.2rem;
    font-weight: 900;
    font-style: italic;
    letter-spacing: -0.04em;
    text-transform: uppercase;
    color: white;
    display: flex;
    align-items: center;
    gap: 4px;
}
.loader-logo-bar {
    width: 6px;
    height: 36px;
    background: #8b5cf6;
    border-radius: 3px;
    animation: loaderBarPulse 1.2s ease-in-out infinite;
}
@keyframes loaderBarPulse {
    0%, 100% { opacity: 1; transform: scaleY(1); }
    50%       { opacity: 0.4; transform: scaleY(0.6); }
}

/* Spinner ring */
.loader-ring {
    position: relative;
    width: 64px;
    height: 64px;
}
.loader-ring svg {
    width: 100%; height: 100%;
    animation: loaderSpin 1.4s linear infinite;
}
@keyframes loaderSpin {
    to { transform: rotate(360deg); }
}
.loader-ring-track {
    stroke: rgba(139,92,246,0.12);
}
.loader-ring-fill {
    stroke: #8b5cf6;
    stroke-linecap: round;
    stroke-dasharray: 150;
    stroke-dashoffset: 38;
    filter: drop-shadow(0 0 6px rgba(139,92,246,0.7));
}

/* Progress bar */
.loader-progress-wrap {
    width: 200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}
.loader-bar-bg {
    width: 100%;
    height: 3px;
    background: rgba(255,255,255,0.06);
    border-radius: 2px;
    overflow: hidden;
}
.loader-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #8b5cf6, #a78bfa);
    border-radius: 2px;
    box-shadow: 0 0 8px rgba(139,92,246,0.6);
    animation: loaderFill 1.6s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
@keyframes loaderFill {
    0%   { width: 0%; }
    30%  { width: 35%; }
    60%  { width: 65%; }
    85%  { width: 88%; }
    100% { width: 100%; }
}
.loader-status {
    font-size: 0.6rem;
    font-weight: 700;
    letter-spacing: 0.25em;
    text-transform: uppercase;
    color: rgba(255,255,255,0.3);
    font-family: monospace;
}

/* Scanline deco */
.loader-scanline {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 2px,
        rgba(0,0,0,0.08) 2px,
        rgba(0,0,0,0.08) 4px
    );
    pointer-events: none;
    animation: scanMove 8s linear infinite;
}
@keyframes scanMove {
    from { background-position: 0 0; }
    to   { background-position: 0 100px; }
}


/* ============================================
   KODEN — RESPONSIVE ENGINE (V2.1 - FLUID ADAPTATION)
   Stake & Roobet Dynamic Adaptive Viewport
   ============================================ */

/* 0. BASELINE GUARDS (Strictly Desktop-Safe by Default) */
#koden-mobile-bottom-nav,
#mobile-games-drawer {
    display: none !important;
}

.mobile-tab-nav {
    display: none !important;
}

/* 1. INTERMEDIATE / TABLET / NARROW PC WINDOWS (<= 1024px) */
@media (max-width: 1024px) {
    html {
        -webkit-text-size-adjust: 100%;
        touch-action: manipulation;
    }

    /* Crucial: Allow vertical scrolling when PC window is shrunk or on laptops! */
    body {
        height: auto !important;
        min-height: 100vh !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 10px !important;
    }

    /* Navbar on shrunk window: hide non-essential links to prevent wrapping */
    nav button[onclick="toggleChangelog()"],
    nav a[href="historique.html"],
    nav a[href="challenges.html"],
    nav a[href="admin.html"],
    nav #nav-admin-link-top,
    nav .w-px.h-5,
    nav span:has(.animate-pulse) {
        display: none !important;
    }

    nav {
        padding: 0 12px !important;
    }

    /* HOMEPAGE TABS ARCHITECTURE (<= 1024px) */
    .mobile-tab-nav {
        display: flex !important;
        width: 100%;
        background: #12141c;
        border: 1px solid rgba(255, 255, 255, 0.08);
        border-radius: 16px;
        padding: 4px;
        gap: 4px;
        margin-bottom: 6px;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
    }

    .mobile-tab-btn {
        flex: 1;
        padding: 9px 4px;
        font-size: 10px;
        font-weight: 900;
        text-transform: uppercase;
        border-radius: 12px;
        color: #71717a;
        text-align: center;
        transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
        cursor: pointer;
        background: none;
        border: none;
    }

    .mobile-tab-btn.active {
        background: #8b5cf6;
        color: #ffffff;
        box-shadow: 0 0 14px rgba(139, 92, 246, 0.5);
    }

    /* Content Area stacking */
    body > div.flex-1.flex.overflow-hidden,
    body > div.flex-1.flex.gap-3,
    body > div.flex-1.flex.gap-4 {
        flex-direction: column !important;
        overflow: visible !important;
        height: auto !important;
        gap: 12px !important;
    }

    /* Tab Display Logic via CSS (Clean, no inline style bugs!) */
    /* Default / games tab */
    body:not([data-tab]) #home-col-games,
    body[data-tab="games"] #home-col-games {
        display: block !important;
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }
    body:not([data-tab]) #home-col-stats-lead,
    body:not([data-tab]) #home-col-chat,
    body[data-tab="games"] #home-col-stats-lead,
    body[data-tab="games"] #home-col-chat {
        display: none !important;
    }

    /* Leaderboard tab */
    body[data-tab="leaderboard"] #home-col-stats-lead {
        display: flex !important;
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }
    body[data-tab="leaderboard"] #home-panel-user,
    body[data-tab="leaderboard"] #home-col-games,
    body[data-tab="leaderboard"] #home-col-chat {
        display: none !important;
    }
    body[data-tab="leaderboard"] #home-panel-leaderboard {
        display: flex !important;
        width: 100% !important;
    }

    /* Stats VIP tab */
    body[data-tab="stats"] #home-col-stats-lead {
        display: flex !important;
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }
    body[data-tab="stats"] #home-panel-leaderboard,
    body[data-tab="stats"] #home-col-games,
    body[data-tab="stats"] #home-col-chat {
        display: none !important;
    }
    body[data-tab="stats"] #home-panel-user {
        display: block !important;
        width: 100% !important;
    }

    /* Live Chat tab */
    body[data-tab="chat"] #home-col-chat {
        display: flex !important;
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        min-height: 480px !important;
        overflow: visible !important;
    }
    body[data-tab="chat"] #home-col-games,
    body[data-tab="chat"] #home-col-stats-lead {
        display: none !important;
    }

    /* Column Width Resets */
    div.w-\[23\%\], div.w-\[24\%\] {
        width: 100% !important;
        max-width: 100% !important;
        height: auto !important;
        overflow: visible !important;
    }

    /* Games Grid: Generous cards */
    .grid.grid-cols-2.sm\:grid-cols-3.lg\:grid-cols-4,
    .grid.grid-cols-2.sm\:grid-cols-2.lg\:grid-cols-4,
    .grid.lg\:grid-cols-4 {
        grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)) !important;
        gap: 12px !important;
        padding: 4px !important;
    }

    /* GAME PAGES (<= 1024px): Stage First (order: 1), Sidebar Second (order: 2) */
    main {
        display: flex !important;
        flex-direction: column !important;
        height: auto !important;
        overflow: visible !important;
        gap: 12px !important;
    }

    main > section {
        order: 1 !important;
        width: 100% !important;
        min-height: 320px !important;
        height: auto !important;
        padding: 20px 16px !important;
        border-radius: 24px !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        position: relative !important;
    }

    main > aside {
        order: 2 !important;
        width: 100% !important;
        max-width: 650px !important;
        margin: 0 auto !important;
        min-height: 0 !important;
        height: auto !important;
        overflow: visible !important;
        border-radius: 24px !important;
        padding: 18px 16px !important;
    }
}

/* 2. SMARTPHONE VIEWPORT & STRICT MOBILE APP EXPERIENCE (<= 768px) */
@media (max-width: 768px) {
    body {
        padding: 6px 6px 78px 6px !important; /* Bottom pad strictly on phone for bottom bar */
        gap: 8px !important;
    }

    /* Safe area padding ONLY on phones */
    @supports (padding: max(0px)) {
        body {
            padding-left: max(6px, env(safe-area-inset-left)) !important;
            padding-right: max(6px, env(safe-area-inset-right)) !important;
            padding-bottom: max(78px, calc(68px + env(safe-area-inset-bottom))) !important;
        }
    }

    /* Ultra Compact Mobile Navbar */
    nav {
        height: 52px !important;
        min-height: 52px !important;
        padding: 0 10px !important;
        border-radius: 16px !important;
        margin-bottom: 2px !important;
    }

    /* Games Grid: 2 columns on phone */
    .grid.grid-cols-2.sm\:grid-cols-3.lg\:grid-cols-4,
    .grid.grid-cols-2.sm\:grid-cols-2.lg\:grid-cols-4,
    .grid.lg\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    /* Specific Game Elements on Phone */
    /* Roulette */
    #wheel-container {
        width: 270px !important;
        height: 270px !important;
        border-width: 6px !important;
    }
    .relative:has(#wheel-container),
    div:has(> #wheel-container) {
        width: 270px !important;
        height: 270px !important;
    }
    #wheel-center-display {
        width: 78px !important;
        height: 78px !important;
        border-width: 3px !important;
    }
    #center-win-num { font-size: 1.6rem !important; }
    #wheel-arrow { width: 22px !important; height: 30px !important; }
    .wheel-halo { width: 310px !important; height: 310px !important; }

    /* Chicken Mines */
    #chicken-grid {
        max-width: 320px !important;
        width: 100% !important;
        gap: 6px !important;
        margin: 0 auto !important;
    }
    .chicken-tile {
        height: 52px !important;
        border-radius: 12px !important;
        font-size: 1.5rem !important;
    }

    /* Crash */
    #crash-chart { max-height: 220px !important; }
    #multiplier { font-size: 3.2rem !important; }
    #crash-subtitle { font-size: 9px !important; top: 68% !important; }

    /* Dice */
    .die-face { width: 66px !important; height: 66px !important; border-radius: 14px !important; }
    .total-bubble { width: 38px !important; height: 38px !important; font-size: 14px !important; }

    /* Slots */
    .reel-container, #slots-reels { gap: 8px !important; }
    .reel { width: 82px !important; height: 180px !important; }
    .reel-symbol { font-size: 2.2rem !important; }

    /* Cards */
    .card-slot, .card-poker { width: 52px !important; height: 76px !important; }
    #dealer-cards, #player-cards { height: 88px !important; gap: 6px !important; }
    #cards-area { gap: 5px !important; }

    /* Touch Controls */
    .chip-btn {
        width: 40px !important;
        height: 40px !important;
        font-size: 0.7rem !important;
    }
    .bet-pill-btn {
        padding: 8px 10px !important;
        font-size: 11px !important;
    }

    /* NATIVE MOBILE BOTTOM BAR — SHOWN STRICTLY ON PHONE */
    #koden-mobile-bottom-nav {
        display: flex !important;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 62px;
        background: rgba(14, 16, 23, 0.95);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        border-top: 1px solid rgba(255, 255, 255, 0.08);
        z-index: 9999;
        align-items: center;
        justify-content: space-around;
        padding: 4px 6px env(safe-area-inset-bottom, 6px) 6px;
        box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.7);
    }

    .mob-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        color: #71717a;
        font-size: 8px;
        font-weight: 800;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        text-decoration: none;
        flex: 1;
        height: 100%;
        transition: color 0.15s, transform 0.15s;
        cursor: pointer;
        background: none;
        border: none;
    }
    .mob-nav-item .emoji-icon {
        font-size: 18px;
        line-height: 1;
        transition: transform 0.15s;
    }
    .mob-nav-item.active {
        color: #8b5cf6;
    }
    .mob-nav-item.active .emoji-icon {
        transform: scale(1.15);
        filter: drop-shadow(0 0 8px rgba(139, 92, 246, 0.6));
    }
    .mob-nav-item:active {
        transform: scale(0.92);
    }

    footer {
        display: none !important;
    }

    * {
        max-width: 100vw;
    }
}

/* 3. STRICT DESKTOP RESTORATION (>= 1025px) */
@media (min-width: 1025px) {
    .mobile-tab-nav,
    #koden-mobile-bottom-nav,
    #mobile-games-drawer {
        display: none !important;
    }

    nav #nav-admin-link-top.flex {
        display: flex !important;
    }

    #home-col-stats-lead {
        display: flex !important;
        width: 23% !important;
        max-width: 23% !important;
    }

    #home-col-games {
        display: block !important;
        flex: 1 1 0% !important;
        width: auto !important;
        max-width: none !important;
    }

    #home-col-chat {
        display: flex !important;
        width: 24% !important;
        max-width: 24% !important;
    }

    #home-panel-user {
        display: block !important;
    }

    #home-panel-leaderboard {
        display: flex !important;
    }

    main > aside {
        order: 1 !important;
        width: 20rem !important; /* md:w-80 */
        margin: 0 !important;
    }

    main > section {
        order: 2 !important;
        flex: 1 1 0% !important;
    }
}

