/* =========================================================================
   Mobile Responsiveness (Screens up to 768px wide)
   This file ensures PRODATA Analytics PRO looks and works perfectly on mobile
   WITHOUT affecting any desktop styling.
   ========================================================================= */

@media (max-width: 768px) {
    /* =============================================================
       Global Resets & Constraints 
       ============================================================= */
    body {
        padding: 10px !important;
        overflow-x: hidden !important;
    }

    .container {
        padding: 15px !important;
        border-radius: 12px !important;
        margin: 5px !important;
    }

    /* =============================================================
       Header & Logos
       ============================================================= */
    .company-logo-top {
        position: relative !important;
        display: block !important;
        margin: 0 auto 10px auto !important;
        left: unset !important;
        top: unset !important;
        height: 80px !important;
    }

    .header h1 {
        font-size: 1.6rem !important;
    }

    .header .animated-subtitle {
        font-size: 1rem !important;
    }
    
    .animated-prodata-wrapper svg {
        height: 80px !important;
    }
    
    .animated-prodata-wrapper text {
        font-size: 260px !important; 
    }

    /* =============================================================
       Top Action Buttons (Risk Wizard, Mix Generator, etc.)
       ============================================================= */
    .risk-top-bar .grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }
    
    .risk-top-bar button {
        min-width: 0 !important;
        height: 90px !important;
        padding: 10px !important;
    }

    .risk-top-bar button span {
        font-size: 0.85rem !important;
        margin-top: 5px !important;
    }

    /* =============================================================
       Filters wrapper (Glassmorphism card)
       ============================================================= */
    .controls-wrapper > div > div {
        border-radius: 15px !important;
    }

    /* Filters internal padding */
    .controls-wrapper > div > div > div:nth-child(2) {
        padding: 15px 10px !important;
    }

    /* Main filters Grid (Columns) -> Stack */
    .filters-row {
        display: flex !important;
        flex-direction: column !important;
        gap: 15px !important;
    }

    /* Override dynamic filter sizes to use screen width */
    .calc-v5-fixed-box,
    .calc-v5-row-filters {
        height: auto !important;
        min-height: 40px !important;
        flex-direction: column !important;
        gap: 12px !important;
        overflow: visible !important;
    }

    .filter-header-row {
        flex-direction: row !important; /* Keep label and toggle on same row if possible */
        justify-content: space-between !important;
        align-items: center !important;
        text-align: right !important;
    }

    .calc-v5-row-filters .select2-container {
        width: 100% !important;
        display: block !important;
    }

    /* Exposure filters (Stocks, Foreign, Forex) -> Stack internally */
    #exposureFilterContainer > div {
        grid-template-columns: 1fr !important;
        gap: 10px !important;
        height: auto !important;
    }

    #exposureFilterContainer > div > div {
        border-right: none !important;
        border-bottom: 1px solid #e2e8f0 !important;
        padding-bottom: 8px !important;
        padding-right: 0 !important;
    }
    #exposureFilterContainer > div > div:last-child {
        border-bottom: none !important;
    }

    /* =============================================================
       Search & Action Row
       ============================================================= */
    .search-row-container {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 10px !important;
    }

    #btnRun {
        width: 100% !important;
        height: 50px !important;
        font-size: 1.1rem !important;
        margin-top: 10px !important;
    }
    
    /* Search select container height */
    #fundSelect + .select2-container .select2-selection--multiple {
        min-height: 50px !important;
        max-height: none !important; 
    }

    /* =============================================================
       Dashboard Grid (3 Mini-tables or Main tables)
       ============================================================= */
    .dashboard-grid {
        grid-template-columns: 1fr !important;
        gap: 15px !important;
    }

    /* Global tables wrapper for scrolling */
    .mini-table-card,
    #reportContent .table-responsive,
    #reportContent .card,
    .comparison-table-v5-wrapper {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important; /* Smooth scrolling */
    }

    /* Make text in tables slightly smaller to fit more data */
    .mini-table-card table td,
    .mini-table-card table th,
    .table-card td,
    .table-card th {
        font-size: 0.8rem !important;
        padding: 8px 5px !important;
        white-space: nowrap !important;
    }

    /* Limit table width to not break viewport */
    table {
        width: 100% !important;
        min-width: 600px !important; /* Force internal scrolling instead of body scrolling */
    }

    /* Revert min-width for simple stats tables if present */
    .stats-table table {
        min-width: 100% !important;
    }

    /* =============================================================
       Modals
       ============================================================= */
    .modal-content,
    .premium-modal-content,
    .calc-modal-content,
    .synth-modal-content,
    .mix-modal-content {
        width: 95% !important;
        max-width: 100% !important;
        margin: 20px auto !important;
        padding: 15px !important;
        border-radius: 12px !important;
        max-height: 90vh !important;
        overflow-y: auto !important;
    }

    /* Calculator Modal Specific Overrides */
    .calc-grid-filters {
        grid-template-columns: 1fr !important; /* Stack columns in mobile */
    }
    .path-dropdown-columns.select2-dropdown {
        min-width: unset !important;
    }
    .path-dropdown-columns .select2-results > .select2-results__options {
        flex-direction: column !important;
    }
    .path-dropdown-columns .select2-results__option[role="group"] {
        width: 100% !important;
        border-left: none !important;
        border-bottom: 2px solid #e2e8f0 !important;
        margin-bottom: 10px !important;
    }

    /* =============================================================
       Login Page Specifcs
       ============================================================= */
    .login-card {
        width: 90% !important;
        padding: 30px 20px !important;
        max-width: none !important;
        margin: 10px !important;
    }
    
    .login-logo img {
        width: 180px !important;
    }
    
    .login-title {
        font-size: 1.5rem !important;
    }

    .login-card .subtitle {
        font-size: 1.1rem !important;
        margin-bottom: 20px !important;
    }

    /* =============================================================
       Stats Page Specifics 
       ============================================================= */
    .chart-grid {
        grid-template-columns: 1fr !important;
    }
    .data-section {
        grid-template-columns: 1fr !important;
    }
    .kpi-grid {
        grid-template-columns: 1fr 1fr !important; /* Two small KPIs side by side */
    }
    .kpi-value {
        font-size: 1.5rem !important; /* Slightly smaller for mobile */
    }
    .navbar {
        padding: 1rem !important;
        flex-direction: column !important;
        gap: 10px !important;
        text-align: center !important;
    }

    /* =============================================================
       Market Research Table Views Toggle
       ============================================================= */
    .desktop-only-flex { display: none !important; }
    .mobile-only-block { display: block !important; }

    /* Simple Mode Cards Shrink */
    .simple-results-v5 {
        zoom: 0.85 !important;
        gap: 10px !important;
    }
    .simple-card-v5 {
        padding: 12px 15px !important;
        border-radius: 20px !important;
    }
    .simple-card-v5 .text-2xl {
        font-size: 1.1rem !important;
        margin-bottom: 10px !important;
    }
    .simple-card-v5 button {
        width: 28px !important;
        height: 28px !important;
    }
    .simple-card-v5 .mb-6, .simple-card-v5 .mb-8 {
        margin-bottom: 10px !important;
    }
    
    /* New Simple Mode Mobile Styles (Symmetrical Squares) */
    .simple-input-group {
        gap: 4px !important;
    }
    .simple-label {
        font-size: 9px !important;
        letter-spacing: 0 !important;
        margin-bottom: 2px !important;
        opacity: 0.8;
    }
    .simple-inputs-grid {
        display: flex !important;
        flex-direction: row !important;
        justify-content: center !important;
        gap: 16px !important;
        width: 100% !important;
        padding: 0 5px !important;
    }
    .yield-box, .fee-box {
        width: 95px !important;   
        height: 50px !important;  
        border-radius: 12px !important;
        border-width: 2px !important;
        margin: 0 auto !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        box-shadow: 0 4px 10px rgba(0,0,0,0.03) !important;
    }
    .yield-box {
        background: #f5f8ff !important;
        border-color: #dbe4ff !important;
    }
    .fee-box {
        background: #fffdf5 !important;
        border-color: #fef3c7 !important;
    }
    .simple-input-v5 {
        font-size: 1.4rem !important;
        font-weight: 900 !important;
        height: 100% !important;
        width: 100% !important;
        color: #1e1b4b !important;
    }
    .simple-label {
        font-size: 8px !important;
        font-weight: 800 !important;
        color: #475569 !important;
        height: 20px !important;
        line-height: 1.1 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        text-align: center !important;
    }
    .simple-pct {
        font-size: 10px !important;
        left: 6px !important;
        top: 15px !important;
        transform: none !important;
    }

    /* Fix fund name visibility in advanced mode */
    .calc-v5-fixed-box, 
    .calc-select2-container, 
    .calc-select2-container .select2-selection,
    .calc-v5-fixed-box .select2-container {
        height: auto !important;
        min-height: 52px !important;
        overflow: visible !important;
    }
    .calc-v5-fixed-box .select2-selection__rendered,
    .calc-select2-container .select2-selection__rendered {
        font-size: 11px !important;
        font-weight: 800 !important;
        color: #1e1b4b !important;
        line-height: 1.1 !important;
        white-space: normal !important;
        padding: 8px 5px !important;
        display: block !important;
        text-align: center !important;
    }



    /* Calculator Fee Inputs Overlap Fix */
    #v5Table [data-row-id="fees"] .flex.gap-6 {
        gap: 8px !important;
    }
    #v5Table [data-row-id="fees"] input {
        width: 65px !important; /* Slightly narrower */
        height: 40px !important;
        padding: 5px !important;
        font-size: 0.85rem !important;
    }

    /* Select2 Dropdown Readability (Normal size on mobile) */
    .select2-container--default .select2-results > .select2-results__options {
        font-size: 1rem !important; /* Reset from scaled down table parent */
    }
    .select2-dropdown {
        border-radius: 12px !important;
        box-shadow: 0 10px 25px -5px rgba(0,0,0,0.2) !important;
    }
    .select2-results__option {
        padding: 10px 15px !important;
    }

    /* =============================================================
       Modal Advanced Fixes (Mix Generator, Portfolio, Synthetic, Calc)
       ============================================================= */
    .mix-generator-layout, 
    .modal-portfolio .calc-layout,
    .calc-inputs-v2, 
    .synth-controls {
        flex-direction: column !important;
        height: auto !important;
    }
    
    .synth-controls > div {
        width: 100% !important;
    }

    /* Portfolio Row Mobile Overrides */
    .portfolio-row-card {
        flex-direction: column !important;
        gap: 15px !important;
    }
    .portfolio-filters-row {
        flex-direction: column !important;
        gap: 10px !important;
    }
    .portfolio-row-card > div {
        flex: auto !important;
        border-left: none !important;
        padding-left: 0 !important;
        width: 100% !important;
    }
    .portfolio-row-card > div:first-child {
        flex-direction: row !important;
        justify-content: flex-end !important;
    }

    #portfolioModal .portfolio-kpis {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .portfolio-actions-row {
        flex-direction: column !important;
        gap: 15px !important;
    }

    .portfolio-actions-row > * {
        width: 100% !important;
        text-align: center !important;
        justify-content: center !important;
    }

    /* Extra zoom for calculator comparison */
    .calculator-extra-zoom {
        zoom: 0.75 !important;
    }
    @-moz-document url-prefix() {
        .calculator-extra-zoom {
            transform: scale(0.75) !important;
            transform-origin: top center !important;
            width: 133.33% !important;
        }
    }

    /* =============================================================
       SYMMETRIC SCALING (Using root REM instead of Zoom to prevent Dropdown bugs)
       Shrink the entire system symmetrically without breaking Select2 coordinates
       ============================================================= */
    html {
        font-size: 13.5px !important; /* ~85% of default 16px, scales all Tailwind rems */
    }

    body {
        font-size: 0.95rem !important;
    }

    /* Additional Extra Zoom exclusively for deep data tables */
    .modern-table-container, #tableContainer {
        zoom: 0.85 !important;
    }
    @-moz-document url-prefix() {
        .modern-table-container, #tableContainer {
            transform: scale(0.85) !important;
            transform-origin: top center !important;
            width: 117.64% !important;
        }
    }

    /* Dashboard Header Grid */
    .risk-top-bar .grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }
    .risk-top-bar .grid > * {
        min-width: 0 !important;
    }

    /* Shrink Control Buttons */
    .controls-wrapper .info-icon {
        width: 24px !important;
        height: 24px !important;
        font-size: 0.8rem !important;
    }
    .controls-wrapper .reset-tiny-btn {
        padding: 4px 8px !important;
    }
    .controls-wrapper .reset-tiny-btn svg,
    .controls-wrapper .reset-tiny-btn i {
        width: 12px !important;
        height: 12px !important;
    }
    .controls-wrapper .reset-tiny-btn span {
        font-size: 0.6rem !important;
    }
    .analysis-main-icon {
        width: 30px !important;
        height: 30px !important;
        border-radius: 0.5rem !important;
    }
    .analysis-main-icon svg,
    .analysis-main-icon i {
        width: 15px !important;
        height: 15px !important;
    }

    /* Shrink Period Pickers & Dates */
    .period-selector button {
        padding: 6px 10px !important;
        font-size: 0.75rem !important;
    }
    .date-controls input[type="date"] {
        padding: 4px 8px !important;
        font-size: 0.8rem !important;
        height: 30px !important;
    }

    /* AI Analyst Stacking */
    .ai-header-wrapper {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 12px !important;
    }
    .ai-action-buttons {
        width: 100% !important;
        justify-content: flex-start !important;
    }

    /* =============================================================
       GLOBAL MOBILE DENSITY & SCALING
       ============================================================= */
    
    /* Tables: Maximum density for mobile */
    .mass-table th, .mass-table td,
    .modern-table th, .modern-table td {
        font-size: 0.8rem !important;
        padding: 8px 5px !important;
    }
    
    .mass-view-header {
        padding: 10px !important;
    }

    /* Smaller icons and texts in KPIs */
    .kpi-value {
        font-size: 1.25rem !important; 
    }
    
    .kpi-label {
        font-size: 0.75rem !important;
        min-height: auto !important;
    }

    /* Main container padding */
    .container, .dashboard-container {
        padding: 5px !important;
    }

    /* Calculator V2 Adjustments */

    .calc-input-group-v2 {
        padding: 15px !important;
        gap: 10px !important;
    }
    .calc-input-group-v2 .input-row {
        flex: 1 1 100% !important;
        margin-bottom: 15px !important;
    }
    .calc-header-v2 button {
        padding: 8px 12px !important;
        font-size: 0.85rem !important;
    }
    #calcCompareBtnV5 {
        width: 100% !important;
    }

    /* Shrink Interest Calculator Elements */
    #interestCalculatorModal .modal-content {
        max-width: 95% !important;
        width: 95% !important;
        margin: 10px auto !important;
    }
    #interestCalculatorModal .modal-header {
        padding: 15px !important;
    }
    #interestCalculatorModal h2 {
        font-size: 1.4rem !important;
        line-height: 1.2 !important;
        gap: 8px !important;
    }
    #interestCalculatorModal .modal-body {
        padding: 15px !important;
    }
    .calc-inputs-v2 .input-wrapper input {
        font-size: 1.1rem !important;
        padding: 10px !important;
    }
    .calc-section-v2 h4 {
        font-size: 0.95rem !important;
    }
}

/* Extra small devices */
@media (max-width: 400px) {
    .kpi-grid {
        grid-template-columns: 1fr !important; /* Stack KPIs if very small screen */
    }
    .risk-top-bar .grid {
        grid-template-columns: 1fr !important; /* Stack action buttons */
    }
}

/* Driver.js RTL & Custom Theme Override */
.driverjs-theme-prodata {
    font-family: 'Rubik', sans-serif !important;
    direction: rtl !important;
    text-align: right !important;
    border-radius: 24px !important;
    box-shadow: 0 30px 60px -12px rgba(99, 102, 241, 0.3) !important;
    padding: 30px !important;
    border: 1px solid rgba(255, 255, 255, 0.6) !important;
    background: linear-gradient(145deg, #ffffff, #f8fafc) !important;
}

.driverjs-theme-prodata .driver-popover-title {
    font-size: 1.4rem !important;
    font-weight: 900 !important;
    color: #1e1b4b !important;
    margin-bottom: 15px !important;
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
}

.driverjs-theme-prodata .driver-popover-description {
    font-size: 1.05rem !important;
    color: #475569 !important;
    line-height: 1.7 !important;
    margin-bottom: 25px !important;
}

.driverjs-theme-prodata .driver-popover-footer {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    margin-top: 10px !important;
    padding: 0 !important;
    flex-direction: row-reverse !important; /* RTL */
}

.driverjs-theme-prodata .driver-popover-progress-text {
    font-weight: 700 !important;
    color: #94a3b8 !important;
    font-size: 0.85rem !important;
}

.driverjs-theme-prodata .driver-popover-next-btn, 
.driverjs-theme-prodata .driver-popover-prev-btn {
    border-radius: 10px !important;
    font-weight: 800 !important;
    padding: 8px 16px !important;
    transition: all 0.2s !important;
    border: none !important;
    box-shadow: none !important;
}

.driverjs-theme-prodata .driver-popover-next-btn {
    background-color: #4f46e5 !important;
    color: white !important;
    text-shadow: none !important;
}

.driverjs-theme-prodata .driver-popover-next-btn:hover {
    background-color: #4338ca !important;
    transform: translateY(-1px) !important;
}

.driverjs-theme-prodata .driver-popover-prev-btn {
    background-color: #f1f5f9 !important;
    color: #475569 !important;
    text-shadow: none !important;
}

.driverjs-theme-prodata .driver-popover-prev-btn:hover {
    background-color: #e2e8f0 !important;
}

.driverjs-theme-prodata .driver-popover-close-btn {
    right: auto !important;
    left: 15px !important;
    top: 15px !important;
    color: #cbd5e1 !important;
}

.driverjs-theme-prodata .driver-popover-close-btn:hover {
    color: #ef4444 !important;
}
