/* =============================================================================
   MOBILE-FIRST RESPONSIVE ENHANCEMENTS
   Optimized for Stock Guru Dash Application
   ============================================================================= */

/* =============================================================================
   TIME RANGE SELECTOR - MOBILE SCROLL
   ============================================================================= */
.time-range-scroll-container {
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 5px;
}

.time-range-scroll-container::-webkit-scrollbar {
    height: 4px;
}

.time-range-scroll-container::-webkit-scrollbar-track {
    background: rgba(0,0,0,0.1);
    border-radius: 4px;
}

.time-range-scroll-container::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.2);
    border-radius: 4px;
}

.time-range-buttons {
    display: inline-flex !important;
    flex-wrap: nowrap !important;
}

.time-range-buttons .btn {
    flex-shrink: 0;
    white-space: nowrap;
}

@media (max-width: 576px) {
    .time-range-buttons .btn {
        padding: 0.375rem 0.75rem;
        font-size: 0.85rem;
    }
}

/* =============================================================================
   TABS - MOBILE HORIZONTAL SCROLL
   ============================================================================= */
@media (max-width: 768px) {
    .nav-tabs {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        overflow-y: hidden !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        white-space: nowrap;
        padding-bottom: 5px;
        margin-bottom: 10px;
    }
    
    .nav-tabs::-webkit-scrollbar {
        height: 4px;
    }
    
    .nav-tabs::-webkit-scrollbar-track {
        background: rgba(0,0,0,0.05);
        border-radius: 4px;
    }
    
    .nav-tabs::-webkit-scrollbar-thumb {
        background: rgba(0,0,0,0.15);
        border-radius: 4px;
    }
    
    .nav-tabs .nav-item {
        flex-shrink: 0 !important;
    }
    
    .nav-tabs .nav-link {
        white-space: nowrap !important;
        padding: 0.5rem 0.75rem !important;
        font-size: 0.85rem !important;
    }
}

/* =============================================================================
   MOBILE NAVIGATION - HAMBURGER MENU
   ============================================================================= */

/* Mobile menu toggle button */
.mobile-menu-toggle {
    display: none;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 0.5rem 0.75rem;
    color: #f8fafc;
    cursor: pointer;
    transition: all 0.2s ease;
}

.mobile-menu-toggle:hover {
    background: rgba(255, 255, 255, 0.1);
}

.mobile-menu-toggle i {
    font-size: 1.25rem;
}

@media (max-width: 991px) {
    .mobile-menu-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* =============================================================================
   RESPONSIVE GRID SYSTEM
   ============================================================================= */

/* Stock tiles grid - responsive */
.stock-tiles-grid {
    display: grid;
    gap: 1rem;
}

@media (max-width: 575px) {
    .stock-tiles-grid {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }
}

@media (min-width: 576px) and (max-width: 767px) {
    .stock-tiles-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.875rem;
    }
}

@media (min-width: 768px) and (max-width: 991px) {
    .stock-tiles-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
}

@media (min-width: 992px) and (max-width: 1199px) {
    .stock-tiles-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (min-width: 1200px) {
    .stock-tiles-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* =============================================================================
   MOBILE-OPTIMIZED STOCK TILES
   ============================================================================= */

@media (max-width: 575px) {
    /* Compact stock tiles on mobile */
    .stock-tile-hover {
        margin-bottom: 0.5rem !important;
    }
    
    .stock-tile-hover .card-header {
        padding: 0.625rem 0.75rem !important;
    }
    
    .stock-tile-hover .card-body {
        padding: 0.5rem !important;
    }
    
    /* Smaller fonts on mobile */
    .stock-tile-hover h5 {
        font-size: 0.85rem !important;
        margin-bottom: 0.25rem !important;
    }
    
    .stock-tile-hover .badge {
        font-size: 0.65rem !important;
        padding: 0.2rem 0.5rem !important;
    }
    
    /* Compact metric boxes */
    .stock-tile-hover .metric-box {
        padding: 0.4rem !important;
    }
    
    .stock-tile-hover .metric-value {
        font-size: 0.9rem !important;
    }
    
    .stock-tile-hover .metric-label {
        font-size: 0.65rem !important;
    }
    
    /* Full-width buttons on mobile */
    .stock-tile-hover .btn {
        width: 100%;
        padding: 0.5rem 0.75rem !important;
        font-size: 0.75rem !important;
    }
    
    /* Hide less important info on mobile */
    .stock-tile-hover .hide-mobile {
        display: none !important;
    }
}

/* =============================================================================
   MOBILE DASHBOARD SUMMARY CARDS
   ============================================================================= */

@media (max-width: 767px) {
    /* Horizontal scrollable summary cards on mobile */
    .summary-cards-row {
        display: flex;
        overflow-x: auto;
        scroll-snap-type: x mandatory;
        -webkit-overflow-scrolling: touch;
        gap: 0.75rem;
        padding-bottom: 0.5rem;
        margin: 0 -0.5rem;
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
    
    .summary-cards-row::-webkit-scrollbar {
        height: 4px;
    }
    
    .summary-cards-row::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.1);
        border-radius: 2px;
    }
    
    .summary-cards-row .card {
        flex: 0 0 auto;
        width: 140px;
        min-width: 140px;
        scroll-snap-align: start;
    }
    
    .summary-cards-row .card-body {
        padding: 0.75rem !important;
    }
    
    .summary-cards-row .metric-value {
        font-size: 1.25rem !important;
    }
    
    .summary-cards-row .metric-label {
        font-size: 0.7rem !important;
    }
}

/* =============================================================================
   MOBILE-OPTIMIZED MARKET INDICES
   ============================================================================= */

@media (max-width: 575px) {
    #market-indices-card {
        padding: 0.5rem !important;
    }
    
    #market-indices-card .d-flex {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    #market-indices-card .index-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem;
        background: rgba(255, 255, 255, 0.02);
        border-radius: 8px;
    }
    
    #market-indices-card span {
        font-size: 0.75rem !important;
    }
    
    #nifty-value, #sensex-value {
        font-size: 1rem !important;
    }
}

/* =============================================================================
   MOBILE SIDEBAR / OFFCANVAS
   ============================================================================= */

@media (max-width: 991px) {
    /* Hide desktop sidebar on mobile/tablet */
    #desktop-sidebar,
    .desktop-sidebar-col {
        display: none !important;
    }
    
    /* Full width content on mobile */
    #page-content,
    .content-wrapper {
        margin-left: 0 !important;
        padding: 0.75rem !important;
        width: 100% !important;
    }
    
    /* Mobile offcanvas menu */
    .offcanvas-start {
        width: 280px !important;
    }
    
    .offcanvas-header {
        padding: 1rem !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    }
    
    .offcanvas-body {
        padding: 0 !important;
    }
    
    .offcanvas-body .list-group-item {
        padding: 0.875rem 1rem !important;
        font-size: 0.9rem !important;
    }
}

/* =============================================================================
   MOBILE NAVBAR
   ============================================================================= */

@media (max-width: 575px) {
    .navbar {
        padding: 0.5rem 0.75rem !important;
    }
    
    .navbar-brand {
        font-size: 1rem !important;
    }
    
    /* Compact search on mobile */
    .navbar-search {
        width: 100%;
        max-width: 100%;
        margin: 0.5rem 0;
        order: 3;
    }
    
    .navbar-search input {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.85rem !important;
    }
    
    /* Icon buttons smaller on mobile */
    #bell-button,
    #chat-toggle-button-header {
        padding: 0.375rem !important;
    }
    
    #bell-icon,
    #chat-icon-header {
        font-size: 1.1rem !important;
    }
}

/* =============================================================================
   MOBILE CHARTS & GRAPHS
   ============================================================================= */

@media (max-width: 767px) {
    .dash-graph {
        min-height: 250px !important;
    }
    
    .chart-card {
        padding: 0.75rem !important;
        border-radius: 10px !important;
    }
    
    .chart-card .card-header {
        padding: 0.625rem 0.75rem !important;
        font-size: 0.9rem !important;
    }
    
    /* Adjust chart controls */
    .modebar {
        top: 0 !important;
        right: 0 !important;
    }
    
    .modebar-btn {
        font-size: 0.75rem !important;
    }
}

/* =============================================================================
   MOBILE TABLES
   ============================================================================= */

@media (max-width: 767px) {
    /* Responsive tables */
    .table-responsive {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .table thead th {
        font-size: 0.7rem !important;
        padding: 0.5rem 0.625rem !important;
        white-space: nowrap;
    }
    
    .table tbody td {
        font-size: 0.8rem !important;
        padding: 0.5rem 0.625rem !important;
    }
    
    /* Hide less important columns on mobile */
    .table .hide-mobile {
        display: none !important;
    }
}

/* =============================================================================
   MOBILE MODALS
   ============================================================================= */

@media (max-width: 575px) {
    .modal-dialog {
        margin: 0.5rem !important;
        max-width: calc(100% - 1rem) !important;
    }
    
    .modal-content {
        border-radius: 12px !important;
    }
    
    .modal-header {
        padding: 0.875rem 1rem !important;
    }
    
    .modal-title {
        font-size: 1rem !important;
    }
    
    .modal-body {
        padding: 1rem !important;
    }
    
    .modal-footer {
        padding: 0.75rem 1rem !important;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        flex: 1;
    }
}

/* =============================================================================
   MOBILE FORMS
   ============================================================================= */

@media (max-width: 575px) {
    .form-control,
    input[type="text"],
    input[type="number"],
    select {
        font-size: 16px !important; /* Prevents iOS zoom on focus */
        padding: 0.625rem 0.875rem !important;
    }
    
    .form-label {
        font-size: 0.85rem !important;
        margin-bottom: 0.375rem !important;
    }
    
    /* Stack form groups on mobile */
    .form-row {
        flex-direction: column;
    }
    
    .form-row > * {
        margin-bottom: 0.75rem;
    }
    
    /* Full width buttons in forms */
    .form-group .btn,
    .form-actions .btn {
        width: 100%;
        margin-bottom: 0.5rem;
    }
}

/* =============================================================================
   MOBILE TABS
   ============================================================================= */

@media (max-width: 575px) {
    /* Horizontal scrollable tabs on mobile */
    .nav-tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
    
    .nav-tabs::-webkit-scrollbar {
        display: none;
    }
    
    .nav-tabs .nav-link {
        white-space: nowrap;
        padding: 0.5rem 0.875rem !important;
        font-size: 0.8rem !important;
    }
    
    .nav-pills .nav-link {
        padding: 0.4rem 0.75rem !important;
        font-size: 0.8rem !important;
    }
}

/* =============================================================================
   MOBILE CHAT PANEL
   ============================================================================= */

@media (max-width: 575px) {
    #chat-window {
        width: 100% !important;
        right: -100% !important;
        border-radius: 0 !important;
    }
    
    #chat-window.open {
        right: 0 !important;
    }
    
    #chat-messages-container {
        height: calc(100vh - 180px) !important;
    }
    
    .chat-bubble-user,
    .chat-bubble-bot {
        max-width: 90% !important;
        padding: 0.625rem 0.875rem !important;
        font-size: 0.9rem !important;
    }
    
    #chat-input {
        font-size: 16px !important; /* Prevents iOS zoom */
    }
}

/* =============================================================================
   TOUCH-FRIENDLY INTERACTIONS
   ============================================================================= */

@media (max-width: 991px) {
    /* Larger touch targets */
    .btn {
        min-height: 44px;
        min-width: 44px;
    }
    
    .list-group-item {
        min-height: 48px;
    }
    
    /* Disable hover effects on touch devices */
    .hover-card:hover {
        transform: none;
    }
    
    /* Active states for touch */
    .hover-card:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
    
    .btn:active {
        transform: scale(0.95);
    }
    
    /* Prevent text selection on buttons */
    .btn,
    .list-group-item {
        -webkit-user-select: none;
        user-select: none;
    }
}

/* =============================================================================
   LANDSCAPE MODE OPTIMIZATIONS
   ============================================================================= */

@media (max-width: 767px) and (orientation: landscape) {
    .navbar {
        padding: 0.375rem 1rem !important;
    }
    
    /* Reduce vertical spacing in landscape */
    .card-body {
        padding: 0.75rem !important;
    }
    
    h2 {
        font-size: 1.1rem !important;
        margin-bottom: 0.5rem !important;
    }
    
    /* Smaller charts in landscape */
    .dash-graph {
        min-height: 200px !important;
    }
}

/* =============================================================================
   SAFE AREA INSETS (for notched devices)
   ============================================================================= */

@supports (padding: max(0px)) {
    .navbar {
        padding-top: max(0.5rem, env(safe-area-inset-top)) !important;
        padding-left: max(1rem, env(safe-area-inset-left)) !important;
        padding-right: max(1rem, env(safe-area-inset-right)) !important;
    }
    
    #page-content {
        padding-left: max(0.75rem, env(safe-area-inset-left)) !important;
        padding-right: max(0.75rem, env(safe-area-inset-right)) !important;
        padding-bottom: max(1rem, env(safe-area-inset-bottom)) !important;
    }
    
    .modal-dialog {
        margin-bottom: max(0.5rem, env(safe-area-inset-bottom)) !important;
    }
}

/* =============================================================================
   DARK MODE ENHANCEMENTS FOR MOBILE
   ============================================================================= */

@media (max-width: 767px) {
    /* Slightly lighter backgrounds on mobile for better readability */
    .card {
        background: rgba(20, 30, 50, 0.75) !important;
    }
    
    /* Higher contrast text on mobile */
    .text-muted {
        color: #a0aec0 !important;
    }
    
    /* Better visibility for small text */
    small,
    .small {
        font-size: 0.8rem !important;
    }
}

/* =============================================================================
   PULL-TO-REFRESH INDICATOR AREA
   ============================================================================= */

@media (max-width: 767px) {
    /* Changed from contain to auto to allow normal scrolling */
    body {
        overscroll-behavior-y: auto !important;
        /* Enable touch scrolling */
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
    }
    
    .pull-to-refresh-area {
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #64748b;
        font-size: 0.85rem;
    }
}

/* =============================================================================
   PRINT STYLES FOR MOBILE
   ============================================================================= */

@media print {
    .mobile-menu-toggle,
    .offcanvas,
    #chat-window,
    .navbar {
        display: none !important;
    }
    
    #page-content {
        margin: 0 !important;
        padding: 0 !important;
    }
    
    .card {
        break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #ddd !important;
    }
}

/* =============================================================================
   CRITICAL: MOBILE SCROLL FIXES FOR iOS AND ANDROID
   This section ensures scrolling works on all mobile devices
   ============================================================================= */

/* iOS Safari and Android specific scroll fixes */
@media (max-width: 991px) {
    /* Enable scrolling on the main scrollable area */
    html {
        overflow-y: scroll !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        height: 100% !important;
    }
    
    body {
        overflow-y: scroll !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        height: auto !important;
        min-height: 100vh !important;
        /* Enable all touch gestures */
        touch-action: manipulation !important;
        /* Prevent overscroll issues but allow normal scroll */
        overscroll-behavior-y: auto !important;
    }
    
    /* Main app container - don't block scrolling */
    #app-container,
    #react-entry-point,
    ._dash-loading,
    #_dash-app-content {
        overflow: visible !important;
        height: auto !important;
        min-height: 100vh !important;
    }
    
    /* Page content area - allow scroll through */
    #page-content {
        overflow-y: visible !important;
        overflow-x: hidden !important;
        height: auto !important;
        min-height: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }
    
    /* Content columns - don't trap scroll */
    .main-content-col,
    #main-content-column,
    .main-content-wrapper {
        overflow: visible !important;
        height: auto !important;
    }
    
    /* Cards and containers - allow content overflow */
    .card,
    .card-body,
    .container-fluid {
        overflow: visible !important;
        overflow-x: hidden !important;
    }
    
    /* Ensure modals don't break body scroll when closed */
    body:not(.modal-open) {
        overflow-y: scroll !important;
    }
}

/* Extra small devices (phones) */
@media (max-width: 575px) {
    /* Ensure touch scrolling is enabled */
    * {
        -webkit-tap-highlight-color: transparent;
    }
    
    html, body {
        /* Critical for iOS Safari */
        position: relative !important;
        overflow-y: scroll !important;
        overflow-x: hidden !important;
        -webkit-overflow-scrolling: touch !important;
        /* Prevent rubber-banding from blocking scroll */
        overscroll-behavior: auto !important;
    }
    
    /* Fix for iOS Safari address bar issues */
    body {
        min-height: 100vh !important;
        min-height: -webkit-fill-available !important;
        height: auto !important;
    }
    
    /* Prevent fixed positioning from blocking scroll */
    .app-navbar {
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
    }
}

/* iOS-specific scroll fixes using feature detection */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari specific styles */
    html, body {
        overflow-y: scroll !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-y !important;
    }
    
    /* Fix viewport height on iOS */
    body {
        min-height: 100vh !important;
        min-height: -webkit-fill-available !important;
    }
    
    /* Enable scrolling in main content */
    #page-content,
    .main-content-wrapper,
    .main-content-col {
        overflow: visible !important;
        -webkit-overflow-scrolling: touch !important;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Touch devices - ensure scroll is enabled */
    html, body {
        overflow-y: scroll !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-x pan-y !important;
    }
    
    /* Larger touch targets */
    .btn, 
    .list-group-item,
    .nav-link,
    .menu-item {
        min-height: 44px !important;
        padding-top: 10px !important;
        padding-bottom: 10px !important;
    }
    
    /* Disable hover effects on touch */
    .card:hover,
    .btn:hover {
        transform: none !important;
    }
}
