/* ==========================================
   MOBILE FIX v2 — Full Portrait Mode Support
   ==========================================
   Forces ALL dashboard pages to work vertically
   on phones in portrait mode (360–430px wide).
   ========================================== */

/* ─── PORTRAIT PHONES (≤991px) ─────────────── */
@media (max-width: 991px) {

    /* ── Hamburger Button ── */
    .mobile-menu-toggle {
        display: flex !important;
        position: fixed !important;
        top: 20px !important;
        right: 20px !important;
        left: auto !important;
        z-index: 10001 !important;
        width: 48px !important;
        height: 48px !important;
        background: #ffffff !important;
        border: 1.5px solid #eaecf0 !important;
        border-radius: 12px !important;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08) !important;
        cursor: pointer !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 5px !important;
        padding: 0 !important;
        transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275) !important;
    }

    .mobile-menu-toggle:hover,
    .mobile-menu-toggle:active {
        border-color: #D4AF37 !important;
        transform: translateY(-2px);
    }

    .mobile-menu-toggle span {
        display: block !important;
        width: 22px !important;
        height: 2px !important;
        background: #1a1a1a !important;
        border-radius: 2px !important;
        transition: all 0.3s ease !important;
    }

    .mobile-menu-toggle.active {
        background: #D4AF37 !important;
        border-color: #D4AF37 !important;
        box-shadow: 0 6px 20px rgba(212, 175, 55, 0.3) !important;
    }

    .mobile-menu-toggle.active span {
        background: #ffffff !important;
    }

    .mobile-menu-toggle.active span:nth-child(1) {
        transform: translateY(7px) rotate(45deg) !important;
    }

    .mobile-menu-toggle.active span:nth-child(2) {
        opacity: 0 !important;
    }

    .mobile-menu-toggle.active span:nth-child(3) {
        transform: translateY(-7px) rotate(-45deg) !important;
    }

    /* ── Sidebar Off-Canvas ── */
    .sidebar-wrapper {
        position: fixed !important;
        left: -100% !important;
        top: 0 !important;
        bottom: 0 !important;
        z-index: 10000 !important;
        transition: left 0.4s cubic-bezier(0.16, 1, 0.3, 1) !important;
        width: 320px !important;
        max-width: 85vw !important;
        background: #ffffff !important;
        box-shadow: 20px 0 50px rgba(0, 0, 0, 0.15) !important;
        display: flex !important;
        flex-direction: row !important;
    }

    .sidebar-wrapper.mobile-open {
        left: 0 !important;
    }

    .sidebar {
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        height: 100% !important;
        overflow-y: auto !important;
        position: relative !important;
        width: auto !important;
        min-width: 0 !important;
        left: 0 !important;
    }

    /* ── Mobile Overlay ── */
    .mobile-overlay {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        background: rgba(0, 0, 0, 0.45) !important;
        z-index: 9997 !important;
        opacity: 0 !important;
        visibility: hidden !important;
        transition: all 0.4s ease !important;
        backdrop-filter: blur(4px) !important;
        -webkit-backdrop-filter: blur(4px) !important;
        display: block !important;
    }

    .mobile-overlay.active {
        opacity: 1 !important;
        visibility: visible !important;
    }

    /* ── Body Lock ── */
    body.mobile-menu-open {
        overflow: hidden !important;
        position: fixed !important;
        width: 100% !important;
    }

    /* ═══════════════════════════════════════════
       CRITICAL: Force ALL grids to single column
       This overrides INLINE styles on HTML elements
       ═══════════════════════════════════════════ */

    /* Dashboard main content */
    .main-content,
    main.main-content {
        padding: 80px 15px 30px 15px !important;
        width: 100% !important;
        min-height: 100vh !important;
        overflow-x: hidden !important;
    }

    /* Speed up animations on mobile */
    .dash-reveal,
    .dash-reveal-left,
    .dash-reveal-right,
    .dash-reveal-scale {
        transition-duration: 0.35s !important;
    }

    /* Disable transform on scrolling containers to prevent cropping */
    .tab-container,
    .cmdmgr-tabs,
    .nav-tabs {
        transform: none !important;
        opacity: 1 !important;
        visibility: visible !important;
    }

    /* dashboard-layout must not side-by-side anything */
    .dashboard-layout {
        flex-direction: column !important;
    }

    /* ── GRID NUCLEAR OVERRIDE ──
       Any element with inline grid-template-columns
       gets forced to 1 column */
    .dashboard-wrapper,
    .dashboard-wrapper[style],
    [style*="grid-template-columns"] {
        display: flex !important;
        flex-direction: column !important;
        gap: 12px !important;
    }

    /* Kill all span overrides — everything is full width */
    [style*="grid-column: span"],
    [style*="grid-column:span"],
    .widget-overview,
    .widget-chart-sm,
    .widget-main-chart,
    .widget-list-1,
    .widget-list-2 {
        grid-column: span 1 !important;
        width: 100% !important;
    }

    /* Config grids (welcome, levels, tickets, etc.) */
    .config-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 20px !important;
    }

    /* Settings personalizer */
    .personalizer-container {
        display: flex !important;
        flex-direction: column !important;
        gap: 25px !important;
    }

    /* Position inputs / color inputs (welcome page) */
    .pos-inputs,
    .color-inputs {
        display: flex !important;
        flex-direction: column !important;
        gap: 15px !important;
    }

    /* ── Headers ── */
    .header,
    .header[style] {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 15px !important;
        margin-bottom: 25px !important;
    }

    .dashboard-title {
        font-size: 1.5rem !important;
        line-height: 1.2 !important;
        word-break: break-word !important;
    }

    /* Profile pills in headers */
    .profile-pill {
        display: none !important;
    }

    /* Status pills */
    .status-pill {
        align-self: flex-start !important;
    }

    /* ── Cards ── */
    .dash-card,
    .setting-card,
    .control-group,
    .widget,
    .premium-glass-card,
    .bento-item,
    .category-item {
        width: 100% !important;
        padding: 18px !important;
        border-radius: 20px !important;
        margin-bottom: 0 !important;
        box-sizing: border-box !important;
    }

    .category-item {
        gap: 15px !important;
    }

    /* Stat grid inside Server Identity card */
    .stat-grid-mini,
    .stat-grid-mini[style] {
        flex-direction: column !important;
        gap: 15px !important;
    }

    /* Server stats — large numbers */
    .stat-item-mini span[style*="font-size: 2.2rem"],
    .stat-item-mini span[style*="font-size: 2.4rem"],
    .stat-value {
        font-size: 1.8rem !important;
    }

    /* Module Grid Labels */
    .module-status-badge {
        padding: 4px 8px !important;
        font-size: 0.6rem !important;
    }

    /* ── Tab Containers Overflow Fix ── */
    .tab-container,
    .cmdmgr-tabs,
    .nav-tabs,
    [class*="-tabs"] {
        display: flex !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        white-space: nowrap !important;
        width: 100% !important;
        max-width: 100% !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x !important;
        padding: 10px 15px !important;
        margin-bottom: 25px !important;
        flex-wrap: nowrap !important;
        gap: 12px !important;
        border-radius: 16px !important;
        align-items: center !important;
        justify-content: flex-start !important;
        /* Enable scrollbar visually on mobile for a moment to help debugging */
        scrollbar-width: thin !important; 
    }

    .tab-container::-webkit-scrollbar,
    .cmdmgr-tabs::-webkit-scrollbar {
        height: 4px !important;
        display: block !important;
    }

    .tab-container::-webkit-scrollbar-thumb,
    .cmdmgr-tabs::-webkit-scrollbar-thumb {
        background: rgba(0,0,0,0.1) !important;
        border-radius: 2px !important;
    }

    .tab-btn,
    .cmdmgr-tab {
        padding: 10px 20px !important;
        flex-shrink: 0 !important;
    }

    /* ── Buttons ── */
    .btn,
    button:not(.mobile-menu-toggle):not(.switch):not(label) {
        max-width: 100% !important;
        padding: 14px 20px !important;
        font-size: 16px !important;
        font-weight: 700 !important;
        min-height: 52px !important;
        justify-content: center !important;
        border-radius: 14px !important;
        box-sizing: border-box !important;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08) !important;
    }

    .btn i {
        margin-right: 8px !important;
    }

    [dir="rtl"] .btn i {
        margin-right: 0 !important;
        margin-left: 8px !important;
    }

    /* Small inline buttons should NOT stretch to 100% */
    .prof-btn-outline,
    .btn.btn-secondary[style*="width: 35px"],
    .btn-secondary[style*="padding: 8px"],
    .close-btn {
        width: auto !important;
        min-height: auto !important;
        padding: 8px 16px !important;
    }

    /* Category List Actions specifically */
    .category-actions {
        flex-direction: row !important;
        gap: 8px !important;
    }

    .category-actions .btn {
        flex: 1 !important;
        min-height: 45px !important;
        padding: 10px !important;
    }

    /* ── Form Elements ── */
    input,
    select,
    textarea,
    .form-control,
    .dropdown-trigger {
        font-size: 16px !important;
        padding: 14px !important;
        min-height: 52px !important;
        width: 100% !important;
        border-radius: 14px !important;
        -webkit-appearance: none !important;
        appearance: none !important;
        box-sizing: border-box !important;
        background-color: #fcfcfc !important;
        border: 1.5px solid #ececec !important;
    }

    .premium-dropdown-container {
        width: 100% !important;
        margin-bottom: 5px !important;
    }

    /* Range inputs need special treatment */
    input[type="range"] {
        min-height: 30px !important;
        padding: 0 !important;
    }

    /* Color inputs */
    input[type="color"] {
        min-height: 60px !important;
        padding: 0 !important;
    }

    /* Prevent iOS Zoom */
    input:focus,
    select:focus,
    textarea:focus {
        font-size: 16px !important;
    }

    /* ── Tables ── */
    table,
    .prof-table,
    .dash-table {
        display: block !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }

    /* ── Modals ── */
    .modal-content,
    .modal-content[style] {
        padding: 25px 20px !important;
        border-radius: 24px !important;
        max-width: 95vw !important;
        width: 95vw !important;
    }

    .modal-header {
        margin-bottom: 20px !important;
    }

    .close-btn {
        top: 15px !important;
        right: 15px !important;
    }

    /* ── Images ── */
    img {
        max-width: 100% !important;
        height: auto !important;
    }

    /* ── Safe Area Support ── */
    .mobile-menu-toggle {
        top: max(15px, env(safe-area-inset-top, 15px)) !important;
        right: max(15px, env(safe-area-inset-right, 15px)) !important;
        left: auto !important;
    }

    /* ── Scrollbar ── */
    .sidebar::-webkit-scrollbar {
        width: 6px;
    }

    .sidebar::-webkit-scrollbar-track {
        background: #f8f9fa;
    }

    .sidebar::-webkit-scrollbar-thumb {
        background: #D4AF37;
        border-radius: 3px;
    }

    /* ═══════════════════════════════
       PAGE-SPECIFIC OVERRIDES
       ═══════════════════════════════ */

    /* ── Profile Page ── */
    .profile-main {
        padding: 80px 15px 30px !important;
    }

    .prof-stats-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .prof-stat-card {
        min-height: 90px !important;
        padding: 16px !important;
    }

    .prof-stat-value {
        font-size: 1.5rem !important;
    }

    .prof-chart-card {
        padding: 15px !important;
    }

    .prof-card-header {
        flex-direction: column !important;
        gap: 15px !important;
    }

    .prof-select.premium-select {
        width: 100% !important;
    }

    /* Transaction table cells */
    .prof-user-cell {
        flex-direction: row !important;
        gap: 8px !important;
    }

    /* ── Audit Logs Page ── */
    .gated-content {
        grid-template-columns: 1fr !important;
    }

    .master-card {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 20px !important;
    }

    .master-card label.switch {
        align-self: flex-end !important;
    }

    /* ── Leaderboard Page ── */
    .lb-item {
        padding: 15px !important;
        gap: 10px !important;
    }

    .lb-left {
        gap: 10px !important;
    }

    .lb-user-avatar {
        width: 36px !important;
        height: 36px !important;
    }

    .lb-username {
        font-size: 0.95rem !important;
        word-break: break-word !important;
    }

    .lb-value {
        font-size: 1rem !important;
    }

    /* ── Temp Voice Page ── */
    .tv-grid {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
    }

    .spell-card {
        padding: 25px 20px !important;
    }

    .btn-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 8px !important;
    }

    .action-row {
        grid-template-columns: 1fr !important;
        gap: 10px !important;
    }

    .discord-mock {
        margin: 0 -10px !important; /* Slight bleed for better look */
        border-radius: 0 !important;
    }

    /* ── Daily Streak Bar Fix ── */
    .streak-container, 
    [title="Daily Streak Progress"] {
        gap: 4px !important;
        overflow-x: auto !important;
        padding-bottom: 5px !important;
        justify-content: flex-start !important;
    }

    .streak-segment {
        min-width: 25px !important;
    }

    /* ── Settings Page (Personalizer) ── */
    .pfp-banner-group {
        grid-template-columns: 1fr !important;
        gap: 15px !important;
        justify-items: center !important;
        text-align: center !important;
    }

    .icon-upload {
        width: 120px !important;
        height: 120px !important;
        margin: 0 auto !important;
    }

    .discord-preview {
        max-width: 100% !important;
        transform: scale(0.9) !important;
        transform-origin: top center !important;
        margin-bottom: -30px !important; /* Offset for scale */
    }

    /* ── Welcome Page ── */
    .welcome-preview-container {
        padding: 20px 15px !important;
        border-radius: 20px !important;
    }

    .control-group {
        padding: 25px 20px !important;
        border-radius: 24px !important;
    }

    /* Fix canvas labels on small screens */
    .welcome-preview-container div[style*="position: absolute"] {
        font-size: 0.65rem !important;
        padding: 6px 10px !important;
        top: 10px !important;
        left: 10px !important;
    }

    /* General Touch Targets */
    .btn, .modern-input, .premium-save-btn, .setup-btn {
        min-height: 50px !important;
    }

    /* ── Moderation Page ── */
    .mod-grid {
        grid-template-columns: 1fr !important;
        gap: 15px !important;
    }

    .mod-card {
        padding: 20px !important;
        min-height: auto !important;
    }

    .bottom-settings {
        padding: 25px 20px !important;
        margin-top: 20px !important;
    }

    .bottom-settings div[style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
    }

    .select-group select[multiple] {
        height: 120px !important;
    }

    /* ── Unsaved Changes Bar ── */
    #unsaved-changes-bar {
        flex-direction: column !important;
        gap: 12px !important;
        padding: 14px 18px !important;
        width: 95% !important;
        max-width: none !important;
    }

    #unsaved-changes-bar .unsaved-content {
        font-size: 0.9rem !important;
    }

    #unsaved-changes-bar .unsaved-actions {
        width: 100% !important;
    }

    #unsaved-changes-bar .unsaved-actions button {
        flex: 1 !important;
    }

    /* ── Welcome Preview ── */
    .welcome-preview-container {
        padding: 20px !important;
    }

    /* ── Inline flex rows that need to stack ── */
    [style*="display: flex"][style*="gap: 30px"],
    [style*="display: flex"][style*="gap: 40px"],
    [style*="display: flex"][style*="gap: 20px"] {
        flex-wrap: wrap !important;
    }

    /* ── Activity grid in dashboard ── */
    [style*="grid-template-columns: repeat(2"] {
        display: flex !important;
        flex-direction: column !important;
    }

    /* ── Generic overflow prevention ── */
    * {
        max-width: 100vw;
    }

    body {
        overflow-x: hidden !important;
    }
}

/* ─── EXTRA SMALL PHONES (≤375px) ─────────── */
@media (max-width: 375px) {
    .mobile-menu-toggle {
        width: 44px !important;
        height: 44px !important;
        top: 12px !important;
        right: 12px !important;
        left: auto !important;
    }

    .sidebar-wrapper {
        max-width: 90vw !important;
    }

    .main-content {
        padding-top: 70px !important;
        padding-left: 12px !important;
        padding-right: 12px !important;
    }

    .dashboard-title {
        font-size: 1.3rem !important;
    }

    .dash-card,
    .setting-card {
        padding: 16px !important;
    }

    .btn {
        padding: 12px 18px !important;
        min-height: 48px !important;
    }

    input,
    select,
    textarea {
        padding: 12px !important;
        min-height: 48px !important;
    }
}

/* ─── RTL SUPPORT ─────────────────────────── */
@media (max-width: 991px) {
    [dir="rtl"] .mobile-menu-toggle {
        right: auto !important;
        left: max(15px, env(safe-area-inset-left, 15px)) !important;
    }
}

/* ─── LANDSCAPE PHONES ────────────────────── */
@media (orientation: landscape) and (max-height: 500px) {

    .mobile-menu-toggle {
        display: flex !important;
        position: fixed !important;
        top: max(10px, env(safe-area-inset-top, 10px)) !important;
        right: max(12px, env(safe-area-inset-right, 12px)) !important;
        left: auto !important;
        z-index: 9999 !important;
        width: 42px !important;
        height: 42px !important;
        background: #ffffff !important;
        border: 2px solid #D4AF37 !important;
        border-radius: 10px !important;
        box-shadow: 0 2px 12px rgba(212, 175, 55, 0.3) !important;
        cursor: pointer !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        gap: 4px !important;
    }

    .mobile-menu-toggle span {
        display: block !important;
        width: 20px !important;
        height: 2px !important;
        background: #1a1a1a !important;
        border-radius: 2px !important;
    }

    .sidebar-wrapper {
        position: fixed !important;
        left: -100% !important;
        top: 0 !important;
        bottom: 0 !important;
        width: 320px !important;
        max-width: 70vw !important;
        z-index: 9998 !important;
        background: #ffffff !important;
        transition: left 0.3s ease !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
    }

    .sidebar {
        display: flex !important;
        flex-direction: column !important;
    }

    .sidebar-wrapper.mobile-open {
        left: 0 !important;
        box-shadow: 5px 0 30px rgba(0, 0, 0, 0.25) !important;
    }

    .mobile-overlay {
        display: none !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        background: rgba(0, 0, 0, 0.55) !important;
        z-index: 9997 !important;
        opacity: 0 !important;
        transition: opacity 0.3s ease !important;
    }

    .mobile-overlay.active {
        display: block !important;
        opacity: 1 !important;
    }

    .main-content {
        padding-top: 60px !important;
        padding-left: max(12px, env(safe-area-inset-left, 12px)) !important;
        padding-right: max(12px, env(safe-area-inset-right, 12px)) !important;
        padding-bottom: 20px !important;
        width: 100% !important;
    }

    .dash-card,
    .setting-card {
        padding: 15px !important;
        margin-bottom: 12px !important;
    }

    .header {
        margin-bottom: 15px !important;
    }

    .dashboard-title {
        font-size: 1.4rem !important;
    }

    .form-group {
        margin-bottom: 12px !important;
    }

    /* RTL landscape */
    [dir="rtl"] .mobile-menu-toggle {
        right: auto !important;
        left: max(12px, env(safe-area-inset-left, 12px)) !important;
    }

    [dir="rtl"] .sidebar-wrapper {
        left: auto !important;
        right: -100% !important;
    }

    [dir="rtl"] .sidebar-wrapper.mobile-open {
        left: auto !important;
        right: 0 !important;
        box-shadow: -5px 0 30px rgba(0, 0, 0, 0.25) !important;
    }
}

/* ─── TOUCH IMPROVEMENTS ──────────────────── */
@media (hover: none) and (pointer: coarse) {
    .mobile-menu-toggle {
        -webkit-tap-highlight-color: transparent;
    }

    .sidebar a,
    .btn,
    button {
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    .sidebar .fri-item {
        min-height: 48px !important;
        padding: 12px 14px !important;
    }
}

/* Prevent iOS auto-zoom when focusing inputs on tablets (iPad ≥992px).
   Phones are covered by the 16px rule inside the ≤991px block above. */
@media (hover: none) and (pointer: coarse) and (min-width: 992px) {

    input,
    select,
    textarea,
    .form-control,
    .premium-select,
    .modern-input {
        font-size: max(16px, 1em);
    }
}

/* ==========================================
   DARK THEME MOBILE OVERRIDES
   ========================================== */
@media (max-width: 991px) {
    [data-theme="dark"] .mobile-menu-toggle {
        background: var(--bg-secondary) !important;
        border-color: var(--border-color) !important;
    }
    
    [data-theme="dark"] .mobile-menu-toggle span {
        background: var(--text-primary) !important;
    }
    
    [data-theme="dark"] .mobile-menu-toggle.active {
        background: var(--frieren-gold, #D4AF37) !important;
        border-color: var(--frieren-gold, #D4AF37) !important;
    }

    [data-theme="dark"] .mobile-menu-toggle.active span {
        background: #ffffff !important;
    }

    [data-theme="dark"] .sidebar-wrapper {
        background: var(--bg-primary) !important;
        box-shadow: 20px 0 50px rgba(0, 0, 0, 0.5) !important;
    }
    
    [data-theme="dark"] .mobile-overlay {
        background: rgba(0, 0, 0, 0.75) !important;
    }
}