/* Mobile Base Styles (Cyber Handheld UI) - GLOBAL STABILIZATION v6 */

@media (max-width: 768px) {

    /* --- CORE SYSTEM: VIEWPORT PROTECTION --- */
    html,
    body {
        overflow-x: hidden !important;
        width: 100vw !important;
        margin: 0 !important;
        padding: 0 !important;
        position: relative !important;
        scrollbar-gutter: auto !important;
    }

    * {
        max-width: 100vw !important;
        box-sizing: border-box !important;
    }

    /* Hide desktop chrome */
    .top-nav,
    .side-border,
    #matrix-rain,
    .desktop-only,
    .highscore-sidebar,
    .pm-warp-core span,
    #clippy-container,
    #clippy-bubble,
    .clippy-bubble,
    .clippy-container {
        display: none !important;
        visibility: hidden !important;
        pointer-events: none !important;
        opacity: 0 !important;
        width: 0 !important;
        height: 0 !important;
        position: absolute !important;
        left: -9999px !important;
    }

    body {
        padding-top: 60px !important;
        padding-bottom: 80px !important;
        background-color: #0a0a0c !important;
    }

    /* --- SHARED LAYOUT: MODULE NEUTRAL --- */
    .container,
    .container-wide,
    .container-fluid,
    .module-container,
    .pm-app-container,
    main {
        width: 100% !important;
        max-width: 100% !important;
        padding: 10px !important;
        margin: 0 !important;
    }

    /* --- DASHBOARD SPECIFIC (SCOPED) --- */
    .dashboard-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 1.5rem !important;
        width: 100% !important;
    }

    .widget {
        width: 100% !important;
        margin: 0 !important;
        padding: 1rem !important;
        border-radius: 6px !important;
        border-left: 2px solid var(--neon-blue) !important;
    }

    .news-container {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        gap: 1rem !important;
    }

    .news-card {
        width: 100% !important;
        margin: 0 !important;
    }

    /* --- MODULE: GAMES GALLERY --- */
    .games-gallery {
        grid-template-columns: 1fr !important;
        padding: 10px !important;
        gap: 20px !important;
    }

    .gallery-title {
        font-size: 2rem !important;
        margin: 20px 0 !important;
        letter-spacing: 2px !important;
    }

    /* --- MODULE: HIGHSCORES & TABLES --- */
    .table-wrapper,
    .stats-table-wrapper,
    .timetable-wrapper {
        overflow-x: auto !important;
        width: 100% !important;
        -webkit-overflow-scrolling: touch;
        margin-bottom: 20px;
        background: rgba(0, 0, 0, 0.5) !important;
        border: 1px solid rgba(0, 242, 255, 0.2) !important;
    }

    .highscore-table,
    .stats-table,
    .timetable {
        min-width: 600px !important;
        /* Force scrollable area */
    }

    /* --- MODULE: PROFILE POLISH --- */
    .profile-header {
        flex-direction: column !important;
        text-align: center !important;
        gap: 1.5rem !important;
    }

    .info-grid,
    .stats-grid {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    .profile-nav {
        overflow-x: auto !important;
        white-space: nowrap !important;
        padding-bottom: 10px !important;
    }

    .xp-container {
        width: 100% !important;
        max-width: 300px !important;
        margin: 0 auto !important;
    }

    /* --- MODULE: PROTOCOL POLISH --- */
    .day-selector-mobile {
        display: flex !important;
        gap: 5px !important;
        margin: 15px 0 !important;
        padding: 0 5px !important;
        justify-content: center !important;
    }

    .day-tab {
        flex: 1 !important;
        background: #1a1a1f !important;
        color: #888 !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        padding: 10px 0 !important;
        border-radius: 4px !important;
        font-family: 'Orbitron' !important;
        font-size: 0.75rem !important;
        transition: all 0.3s !important;
    }

    .day-tab.active {
        background: var(--neon-blue) !important;
        color: #000 !important;
        border-color: var(--neon-blue) !important;
        box-shadow: 0 0 10px var(--neon-blue) !important;
    }

    /* Fix Subject History items being squeezed */
    .history-card {
        width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
        margin-bottom: 20px !important;
        transform: none !important;
        /* Disable hover scale on mobile to avoid layout shifts */
    }

    .hc-body {
        font-size: 0.9rem !important;
        padding: 10px !important;
        word-wrap: break-word !important;
    }

    /* --- MODULE: PROJECT MANAGEMENT (PM) --- */
    .pm-app-container {
        height: auto !important;
        min-height: calc(100vh - 140px) !important;
        background: #0d1117 !important;
        /* Solid dark background for readability */
    }

    .pm-cockpit {
        padding: 5px !important;
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 8px !important;
        background: #161b22 !important;
    }

    .pm-cockpit-left h1 {
        font-size: 1rem !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        max-width: 150px !important;
    }

    .pm-cockpit-center {
        display: flex !important;
        flex-direction: row !important;
        justify-content: space-between !important;
        width: 100% !important;
        padding: 5px !important;
        background: rgba(0, 0, 0, 0.2) !important;
        border-radius: 8px !important;
        order: 2 !important;
        font-size: 0.7rem !important;
    }

    .pm-deadlines {
        gap: 5px !important;
        flex-direction: row !important;
    }

    .pm-deadlines span i {
        font-size: 0.8rem !important;
    }

    .pm-deadlines span:last-child {
        display: none !important;
        /* Hide labels like 'Verbleibend' */
    }

    .pm-progress-bar-container {
        width: 100px !important;
        height: 12px !important;
    }

    .pm-cockpit-right {
        order: 4 !important;
        gap: 5px !important;
    }

    .pm-btn {
        padding: 6px 10px !important;
        font-size: 0.75rem !important;
    }

    .pm-warp-core {
        width: 100% !important;
        height: auto !important;
        flex-direction: row !important;
        overflow-x: auto !important;
        padding: 5px !important;
        border-right: none !important;
        border-bottom: 2px solid var(--pm-accent) !important;
        background: var(--pm-panel-bg) !important;
        position: sticky !important;
        top: 60px !important;
        z-index: 100 !important;
    }

    .pm-nav-item {
        flex: 1 !important;
        padding: 10px 5px !important;
        min-width: 60px !important;
    }

    .pm-nav-item span {
        display: none !important;
    }

    .pm-nav-item.active::before {
        display: none !important;
    }

    .pm-nav-item.active {
        background: rgba(88, 166, 255, 0.1) !important;
        border-bottom: 2px solid var(--pm-accent) !important;
    }

    .pm-stage {
        height: auto !important;
        min-height: 400px !important;
    }

    .pm-view {
        position: relative !important;
        padding: 10px !important;
    }

    .pm-kanban-board {
        padding-bottom: 40px !important;
    }

    .pm-kanban-column {
        min-width: 85vw !important;
        max-width: 85vw !important;
    }

    .pm-com-link {
        display: none !important;
        /* Hide collaboration sidebar on mobile for now */
    }

    .pm-toolbelt {
        bottom: 85px !important;
        right: 15px !important;
    }

    .pm-toolbelt-btn {
        width: 50px !important;
        height: 50px !important;
    }

    /* --- FIXED UI COMPONENTS --- */
    .mobile-header {
        display: flex !important;
        position: fixed !important;
        top: 0;
        left: 0;
        right: 0;
        height: 60px;
        background: rgba(13, 13, 15, 0.95) !important;
        backdrop-filter: blur(15px);
        z-index: 100000;
        align-items: center;
        justify-content: space-between;
        padding: 0 15px;
        border-bottom: 2px solid var(--neon-blue);
        box-shadow: 0 4px 20px rgba(0, 242, 255, 0.2);
    }

    .mobile-logo {
        font-family: 'Orbitron', sans-serif !important;
        font-size: 1.3rem !important;
        font-weight: 900 !important;
        text-decoration: none !important;
        letter-spacing: 2px !important;
        text-transform: uppercase !important;

        /* Flow Animation synchronized */
        background: linear-gradient(90deg, #fff 0%, var(--v-glow-color, var(--neon-blue)) 50%, #fff 100%);
        background-size: 200% auto;
        -webkit-background-clip: text;
        background-clip: text;
        -webkit-text-fill-color: transparent;
        filter: drop-shadow(0 0 5px var(--v-glow-color, var(--neon-blue)));
        animation: logo-flow 3s linear infinite;
    }

    .mobile-trigger {
        background: rgba(0, 0, 0, 0.5) !important;
        border: 1px solid var(--neon-blue) !important;
        color: var(--neon-blue) !important;
        width: 40px;
        height: 40px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.2rem;
        cursor: pointer;
        box-shadow: 0 0 10px rgba(0, 242, 255, 0.2);
        transition: all 0.3s ease;
    }

    .mobile-trigger:active {
        background: var(--neon-blue) !important;
        color: #000 !important;
        box-shadow: 0 0 20px var(--neon-blue);
        transform: scale(0.9);
    }

    .bottom-nav {
        display: flex !important;
        position: fixed !important;
        bottom: 0;
        left: 0;
        right: 0;
        height: 70px;
        background: #111;
        z-index: 100000;
        border-top: 2px solid var(--neon-blue);
        justify-content: space-around;
        align-items: center;
        padding: 0;
    }

    .nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 4px;
        color: #888;
        font-size: 0.65rem;
        text-decoration: none;
    }

    .nav-item i {
        font-size: 1.4rem;
    }

    .nav-item.active {
        color: var(--neon-blue);
    }

    /* --- CHAT Bubbles --- */
    #chat-bubble {
        bottom: 85px !important;
        right: 15px !important;
        z-index: 200000 !important;
    }

    .chat-wrapper {
        bottom: 160px !important;
        z-index: 210000 !important;
        width: calc(100% - 20px) !important;
        left: 10px !important;
        right: 10px !important;
    }

    /* --- SIDEBAR --- */
    .mobile-sidebar {
        position: fixed !important;
        top: 0;
        right: -100%;
        width: 85%;
        height: 100%;
        background: rgba(13, 13, 15, 0.98);
        backdrop-filter: blur(15px);
        z-index: 250000;
        transition: right 0.4s cubic-bezier(0.16, 1, 0.3, 1);
        border-left: 1px solid var(--neon-border);
        padding: 4rem 1.5rem;
        overflow-y: auto;
        box-shadow: -10px 0 30px rgba(0, 0, 0, 0.5);
    }

    .mobile-sidebar.open {
        right: 0 !important;
    }

    .sidebar-overlay {
        position: fixed !important;
        inset: 0;
        background: rgba(0, 0, 0, 0.8);
        z-index: 9999;
        display: none;
    }

    .orientation-hint .hint-content {
        text-align: center;
        background: rgba(0, 0, 0, 0.9);
        padding: 30px;
        border: 2px solid #00f2ff;
        border-radius: 15px;
        box-shadow: 0 0 30px rgba(0, 242, 255, 0.4);
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    /* Game Mode: Hide Site Headers in Landscape to maximize space */
    @media screen and (max-width: 1050px) {
        body.game-mode {
            overflow: hidden;
            /* Stop main page scroll, let containers handle it */
        }

        body.game-mode .top-nav,
        body.game-mode .mobile-header,
        body.game-mode .side-border,
        body.game-mode footer,
        body.game-mode #clippy-container,
        body.game-mode .app-banner,
        body.game-mode .app-box {
            display: none !important;
        }

        body.game-mode main.container,
        body.game-mode main.container-wide {
            padding: 0 !important;
            margin: 0 !important;
            max-width: 100vw !important;
            width: 100vw !important;
        }

        /* Selection Panel Mobile Docking */
        #selection-panel {
            /* Defaults to hiding via JS, but when block, style as dock */
            position: fixed !important;
            bottom: 50px !important;
            /* Above mobile-game-tabs */
            left: 0 !important;
            width: 100% !important;
            z-index: 10000 !important;
            border-radius: 15px 15px 0 0;
            border: 2px solid #0f0 !important;
            border-bottom: none !important;
            background: rgba(10, 10, 15, 0.98) !important;
            backdrop-filter: blur(10px);
            box-shadow: 0 -5px 30px rgba(0, 255, 159, 0.3);
            padding: 10px !important;
        }
    }

    .sidebar-overlay.open {
        display: block !important;
    }

    /* --- HIGHSCORE & STATS MOBILE OPTIMIZATION --- */
    .mobile-rotate-hint {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 10px;
        background: rgba(255, 0, 234, 0.1);
        color: var(--neon-pink);
        padding: 10px;
        border-radius: 8px;
        margin-bottom: 15px;
        font-size: 0.8rem;
        border: 1px solid rgba(255, 0, 234, 0.2);
        animation: pulse-pink 2s infinite;
    }

    @keyframes pulse-pink {
        0% {
            box-shadow: 0 0 5px rgba(255, 0, 234, 0.2);
        }

        50% {
            box-shadow: 0 0 15px rgba(255, 0, 234, 0.4);
        }

        100% {
            box-shadow: 0 0 5px rgba(255, 0, 234, 0.2);
        }
    }

    /* Hide hint when landscape or desktop */
    @media screen and (orientation: landscape) {
        .mobile-rotate-hint {
            display: none !important;
        }
    }

    @media screen and (min-width: 769px) {
        .mobile-rotate-hint {
            display: none !important;
        }
    }

    .game-leaderboard-wrapper {
        margin: 0 auto !important;
        max-width: 100% !important;
        /* Allow full use of mobile width */
        padding: 0 !important;
    }

    .game-leaderboard-wrapper .highscore-table {
        min-width: auto !important;
        /* CRITICAL: Remove forced scroll! */
        width: 100% !important;
    }

    .game-leaderboard-wrapper .highscore-table td,
    .game-leaderboard-wrapper .highscore-table th {
        padding: 12px 10px !important;
    }

    .game-leaderboard-wrapper .col-player {
        width: auto !important;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .game-leaderboard-wrapper .col-score {
        width: 90px !important;
        text-align: right !important;
        padding-right: 15px !important;
    }

    /* Hide Date on Mobile Vertical for Game Scores */
    .game-leaderboard-wrapper .col-date {
        display: none !important;
    }

    .stats-table-wrapper {
        margin: 0 -10px !important;
        /* Bleed to edges */
        width: calc(100% + 20px) !important;
        border-radius: 0 !important;
    }

    .stats-table-wrapper .highscore-table {
        min-width: 1300px !important;
    }

    .mobile-nav-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .sidebar-section {
        color: #555;
        font-family: 'Orbitron';
        font-size: 0.7rem;
        letter-spacing: 2px;
        margin: 20px 0 10px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    }

    .mobile-nav-list a {
        display: flex;
        align-items: center;
        gap: 12px;
        color: var(--neon-blue);
        text-decoration: none;
        padding: 10px 0;
        font-size: 1.1rem;
    }

    /* --- EMERGENCY MARQUEE ISOLATION --- */
    .highscore-marquee {
        --marquee-speed: 40s !important;
        width: 100% !important;
        max-width: 100vw !important;
        overflow: hidden !important;
        white-space: nowrap !important;
    }

    .marquee-content {
        max-width: none !important;
        width: max-content !important;
        display: flex !important;
        flex-wrap: nowrap !important;
    }
}

/* --- ORIENTATION HINT --- */
.orientation-hint {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(10, 10, 12, 0.95);
    z-index: 999999;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px;
}

.orientation-hint i {
    font-size: 4rem;
    color: var(--neon-blue);
    margin-bottom: 20px;
    animation: rotateDevice 2s infinite ease-in-out;
}

.orientation-hint p {
    font-family: 'Orbitron', sans-serif;
    color: #fff;
    line-height: 1.6;
}

@keyframes rotateDevice {
    0% {
        transform: rotate(0deg);
    }

    50% {
        transform: rotate(-90deg);
    }

    100% {
        transform: rotate(0deg);
    }
}

/* Show hint only in portrait for games that need it */
@media screen and (orientation: portrait) {
    .force-landscape .orientation-hint {
        display: flex !important;
    }
}

@media (min-width: 769px) {

    .mobile-header,
    .bottom-nav,
    .mobile-sidebar,
    .sidebar-overlay,
    .day-selector-mobile {
        display: none !important;
    }
}