/* Mobile Responsive Adjustments */

@media (max-width: 768px) {
    /* Secret Sidebar Drawer — must sit ABOVE the fixed bottom nav (z=10001) */
    .sidebar {
        position: fixed;
        inset-inline-start: 0;
        transform: translateX(calc(100% + 40px)); /* closed: slid off the start (right) edge, shadow included */
        top: 0;
        height: 100vh;
        height: 100dvh;
        width: 280px;
        box-shadow: -5px 0 20px rgba(0,0,0,0.15);
        transition: transform var(--mo-slow) var(--ease-out);
        z-index: 10100;
        padding-top: 0 !important;
        overflow-y: auto !important;
    }

    .sidebar.open {
        transform: none;
    }

    /* Shrink Main Content Padding */
    .main-content {
        padding: 50px 10px 10px 10px;
    }

    /* Reduce dashboard card padding */
    .card {
        padding: 10px !important;
    }

    /* ========== DASHBOARD - COMPACT LAYOUT ========== */
    .dashboard-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        margin-bottom: 12px !important;
    }

    /* Tier1 hero: 2x2 grid, compact */
    .dashboard-grid[style*="repeat(4, 1fr)"] {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        margin-bottom: 10px !important;
    }
    .dashboard-grid[style*="repeat(4, 1fr)"] .card {
        padding: 10px !important;
    }
    .dashboard-grid[style*="repeat(4, 1fr)"] .card span {
        font-size: 0.65rem !important;
        margin-bottom: 2px !important;
    }
    .dashboard-grid[style*="repeat(4, 1fr)"] .card h3 {
        font-size: 1rem !important;
        margin: 0 !important;
    }
    .dashboard-grid[style*="repeat(4, 1fr)"] .card i {
        font-size: 0.7rem !important;
        margin-inline-end: 3px !important;
    }

    /* Tier2: summary cards side-by-side, full cards stacked below */
    #tier2-grid {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        margin-bottom: 10px !important;
    }
    #tier2-grid .tier2-summary {
        grid-column: auto !important;
    }
    #tier2-grid .tier2-full {
        grid-column: 1 / -1 !important;
    }
    /* Summary cards: stack inner items vertically on mobile */
    #tier2-grid .tier2-summary {
        flex-direction: column !important;
        gap: 6px !important;
        padding: 10px 12px !important;
    }
    #tier2-grid .tier2-summary > div {
        border-inline-end: none !important;
        padding-inline-end: 0 !important;
        border-bottom: 1px solid var(--c-border);
        padding-bottom: 6px;
    }
    #tier2-grid .tier2-summary > div:last-child {
        border-bottom: none;
        padding-bottom: 0;
    }
    #tier2-grid .tier2-summary span {
        font-size: 0.7rem !important;
    }
    #tier2-grid .tier2-summary strong {
        font-size: 0.9rem !important;
    }
    /* Other 1fr 1fr grids: stacked */
    .dashboard-grid[style*="1fr 1fr"]:not(#tier2-grid) {
        grid-template-columns: 1fr !important;
        gap: 10px !important;
        margin-bottom: 10px !important;
    }
    /* Savings + Investments row: side by side compact */
    .dashboard-grid .card[style*="display:flex"][style*="gap:16px"] {
        flex-direction: row !important;
        gap: 0 !important;
        padding: 8px 12px !important;
    }
    .dashboard-grid .card[style*="display:flex"][style*="gap:16px"] > div {
        border-bottom: none !important;
        padding-bottom: 0 !important;
        flex: 1;
    }
    .dashboard-grid .card[style*="display:flex"][style*="gap:16px"] > div span {
        font-size: 0.7rem !important;
    }
    .dashboard-grid .card[style*="display:flex"][style*="gap:16px"] > div strong {
        font-size: 0.9rem !important;
    }

    /* Cash accounts & Debts cards: compact headers */
    .dashboard-grid .card[style*="border:1px solid"] {
        margin-bottom: 0 !important;
    }
    .dashboard-grid .card[style*="border:1px solid"] > div[style*="padding:16px 20px"] {
        padding: 10px 12px !important;
    }
    .dashboard-grid .card[style*="border:1px solid"] > div[style*="padding:16px 20px"] h4 {
        font-size: 0.8rem !important;
    }
    .dashboard-grid .card[style*="border:1px solid"] > div[style*="padding:16px 20px"] h3 {
        font-size: 0.85rem !important;
    }
    .dashboard-grid .card[style*="border:1px solid"] > div[style*="padding:16px"] {
        padding: 8px 12px !important;
    }

    /* Performance section: force single column so perf-grid takes full width */
    .dashboard-grid[style*="grid-template-columns: 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 10px !important;
    }
    /* ===== NUCLEAR: Performance tables (الاحتياجات & الرغبات) full width ===== */
    .perf-grid,
    #performance-tables-container {
        display: flex !important;
        flex-direction: column !important;
        gap: 10px !important;
        width: 100% !important;
        min-width: 100% !important;
        max-width: none !important;
        margin-inline-end: 0 !important;
        margin-inline-start: 0 !important;
        padding: 0 !important;
    }
    .perf-grid > div,
    #performance-tables-container > div {
        width: 100% !important;
        min-width: 100% !important;
        max-width: none !important;
        flex: 0 0 auto !important;
        display: block !important;
        margin-inline-end: 0 !important;
        margin-inline-start: 0 !important;
    }
    .perf-grid table,
    #performance-tables-container table {
        width: 100% !important;
        min-width: 100% !important;
    }
    .perf-grid table td,
    .perf-grid table th,
    #performance-tables-container table td,
    #performance-tables-container table th {
        word-break: break-word !important;
    }

    /* ===== NUCLEAR: Deposit mode hide source selectors ===== */
    body.mode-deposit #source-wallet-row,
    body.mode-deposit #wallet-select-mobile,
    body.mode-deposit #wallet-card-select-mobile,
    body.mode-deposit #ledger-cat-display-wrapper,
    body.mode-deposit #ledger-category-row {
        display: none !important;
        visibility: hidden !important;
    }
    /* Budget performance card: full width, less padding */
    .card.full-width {
        width: 100% !important;
    }
    .card.full-width .card-header {
        padding: 10px 12px 0 12px !important;
    }
    .card.full-width .card-header h3 {
        font-size: 0.9rem !important;
    }
    .card.full-width .table-responsive {
        padding: 8px 10px !important;
    }
    
    /* Make tables scroll horizontally */
    .table-responsive {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-inline-start: 1px solid var(--c-border);
        border-inline-end: 1px solid var(--c-border);
    }
    
    /* Specifically for matrix */
    .matrix-table {
        min-width: 100%;
        width: 100%;
    }

    /* Category history chart */

    div[style*="height:280px"] {
        height: 200px !important;
    }
    
    /* Freeze active column in matrix for easier mobile viewing */
    .matrix-table th:first-child, .matrix-table td:first-child {
        position: sticky;
        inset-inline-start: 0;
        background: var(--c-bg);
        z-index: 5;
        border-inline-end: 2px solid var(--border);
    }
    
    /* Chart container */
    #chart-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding: 10px !important;
    }
    #chart-wrapper canvas {
        min-width: 100%;
        max-width: 100%;
        height: 250px !important;
        display: block;
        margin: 0 auto;
    }
    .chart-container {
        position: relative;
        width: 100%;
        height: auto;
    }

    /* ========== LEDGER FORM - MOBILE REDESIGN ========== */

    #view-ledger > div {
        padding: 0 !important;
    }

    /* Form takes only the space its content needs — no forced full-height */
    #ledger-form {
        padding: 8px 10px !important;
        max-height: none !important;
        overflow-y: visible !important;
        overflow-x: hidden !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
    }

    /* Mode toggle: single row, compact but readable */
    #ledger-form > div:first-child {
        margin-bottom: 6px !important;
        padding-bottom: 6px !important;
        border-bottom: 1px solid var(--c-border) !important;
        flex-shrink: 0 !important;
    }
    #ledger-form > div:first-child > div {
        display: flex !important;
        flex-direction: row !important;
        gap: 2px !important;
        background: var(--c-surface-2);
        padding: 2px;
        border-radius: 8px;
        width: 100% !important;
    }
    .ledger-tab-btn {
        font-size: 0.6rem !important;
        padding: 6px 2px !important;
        background: var(--c-surface) !important;
        border-radius: 6px !important;
        border: 1px solid var(--c-border) !important;
        white-space: nowrap !important;
        text-align: center !important;
        flex: 1 !important;
        font-weight: 600 !important;
        line-height: 1.2 !important;
    }
    .ledger-tab-btn i {
        display: none !important;
    }
    .ledger-tab-btn.active {
        background: var(--primary) !important;
        color: var(--c-on-primary) !important;
        border-color: var(--primary) !important;
    }

    /* Category picker: takes ALL remaining space */
    #ledger-category-row {
        display: flex;
        flex-direction: column !important;
        margin-bottom: 8px !important;
        margin-inline-end: 0 !important;
        margin-inline-start: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
        min-width: 0 !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    #ledger-category-row .cat-picker {
        flex: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        margin-bottom: 0 !important;
        overflow: hidden !important;
        min-width: 0 !important;
    }
    /* Flatten the grid: ignore column grouping, use CSS grid for equal sizing */
    #ledger-category-row .cat-picker__grid {
        display: grid !important;
        grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
        gap: 3px !important;
        flex: 1 !important;
        max-height: none !important;
        overflow: hidden !important;
        align-content: start !important;
        min-width: 0 !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    /* Each col group takes full row width, flatten its pills into the grid */
    #ledger-category-row .cat-col {
        display: contents !important;
    }
    /* Hide category group labels on mobile (saves space) */
    #ledger-category-row .cat-col__label {
        display: none !important;
    }
    #ledger-category-row .cat-col__pills {
        display: contents !important;
    }
    /* All pills: fixed equal size, light style with colored right+bottom border */
    #ledger-category-row .cat-pill {
        font-size: 0.7rem !important;
        padding: 3px 22px 3px 6px !important;
        gap: 0 !important;
        width: 100% !important;
        min-width: 0 !important;
        height: 26px !important;
        justify-content: flex-end !important;
        text-align: start !important;
        line-height: 1.2 !important;
        display: flex !important;
        align-items: center !important;
        box-sizing: border-box !important;
        overflow: visible !important;
        position: relative !important;
        background: var(--c-surface) !important;
        border: 1px solid var(--c-border) !important;
        border-bottom-width: 2px !important;
        border-radius: 4px !important;
    }
    /* Color only the right+bottom borders per type */
    #ledger-category-row .cat-pill--need {
        border-bottom-color: var(--c-danger-line) !important;
        color: var(--c-danger) !important;
    }
    #ledger-category-row .cat-pill--want {
        border-bottom-color: var(--c-warning-line) !important;
        color: var(--c-warning) !important;
    }
    #ledger-category-row .cat-pill--saving,
    #ledger-category-row .cat-pill--account,
    #ledger-category-row .cat-pill--invest {
        border-bottom-color: var(--c-primary-line) !important;
        color: var(--c-primary) !important;
    }
    #ledger-category-row .cat-pill--visa {
        border-bottom-color: var(--c-primary-line) !important;
        color: var(--st-new) !important;
    }
    #ledger-category-row .cat-pill.cat-pill--active {
        background: var(--c-primary-soft) !important;
        border-color: var(--c-primary) !important;
        border-bottom-width: 2px !important;
    }

    #ledger-category-row .cat-pill .pill-text {
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
        display: block !important;
        text-align: start !important;
        width: 100% !important;
        margin-inline-start: 4px !important;
    }

    /* Wallet rows: two dropdowns (accounts + cards) on mobile */
    #source-wallet-row {
        flex-shrink: 0 !important;
        margin-bottom: 6px !important;
        padding: 6px 8px !important;
        gap: 4px !important;
        flex-wrap: wrap !important;
    }
    #ledger-dest-wallet-row {
        flex-shrink: 0 !important;
        margin-bottom: 6px !important;
        padding: 6px 8px !important;
        gap: 4px !important;
    }
    #source-wallet-row > span,
    #ledger-dest-wallet-row > span {
        display: none !important;
    }
    /* Hide pills, show dropdowns on mobile */
    .wallet-pills-desktop {
        display: none !important;
    }
    .wallet-select-mobile {
        display: block !important;
        flex: 1 !important;
    }

    /* Two dropdowns side by side */
    #source-wallet-row {
        display: flex !important;
        flex-direction: row !important;
        gap: 6px !important;
        padding: 6px 8px !important;
        align-items: center !important;
        background: var(--c-bg) !important;
    }

    #wallet-select-mobile,
    #wallet-card-select-mobile {
        flex: 1 !important;
        padding: 6px 8px !important;
        font-size: 0.85rem !important;
        border: 1px solid var(--c-border-strong) !important;
        border-radius: 7px !important;
        font-family: var(--font-ui) !important;
        background: var(--c-surface) !important;
    }
    #dest-wallet-select-mobile {
        width: 100% !important;
        padding: 6px 8px !important;
        font-size: 0.85rem !important;
        border: 1px solid var(--c-primary-line) !important;
        border-radius: 7px !important;
        font-family: var(--font-ui) !important;
        background: var(--c-surface) !important;
    }

    /* Form fields: fixed at bottom, 2-column grid */
    #ledger-fields-row {
        flex-shrink: 0 !important;
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }
    #ledger-fields-row > div {
        width: 100% !important;
        flex: none !important;
    }
    #ledger-fields-row > div:first-child {
        grid-column: 1 / -1 !important;
    }
    #ledger-fields-row > div:nth-child(4) {
        grid-column: 1 / -1 !important;
    }
    #ledger-fields-row label {
        font-size: 0.65rem !important;
        margin-bottom: 1px !important;
    }
    #ledger-fields-row input {
        width: 100% !important;
        padding: 6px 8px !important;
        font-size: 0.78rem !important;
    }
    #ledger-fields-row button {
        width: 100% !important;
        padding: 8px !important;
        font-size: 0.8rem !important;
        grid-column: 1 / -1 !important;
    }
    #selected-cat-display {
        width: 100% !important;
        display: block !important;
        padding: 6px 8px !important;
        font-size: 0.75rem !important;
    }

    /* Link to transactions */
    #view-ledger > div > div:last-child {
        padding: 8px 10px !important;
        margin-top: 4px !important;
    }

    /* Buttons row: inline, small */
    #view-transactions > div:first-child {
        margin-bottom: 6px !important;
        gap: 4px !important;
    }
    #view-transactions > div:first-child .btn {
        font-size: 0.7rem !important;
        padding: 4px 10px !important;
    }

    /* Filters: compact grid */
    #view-transactions > div:nth-child(2) {
        padding: 6px 8px !important;
        margin-bottom: 8px !important;
        gap: 4px !important;
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
    }
    #view-transactions > div:nth-child(2) > div {
        width: 100% !important;
    }
    #view-transactions > div:nth-child(2) > div:nth-child(4) {
        grid-column: 1 / -1 !important;
    }
    #view-transactions > div:nth-child(2) select,
    #view-transactions > div:nth-child(2) input {
        width: 100% !important;
        padding: 4px 6px !important;
        font-size: 0.72rem !important;
    }
    #view-transactions > div:nth-child(2) i {
        font-size: 0.7rem !important;
    }
    #ledger-month-total {
        font-size: 0.68rem !important;
        padding: 3px 8px !important;
        grid-column: 1 / -1 !important;
        text-align: center !important;
    }
    #ledger-filter-count {
        font-size: 0.65rem !important;
        grid-column: 1 / -1 !important;
        text-align: center !important;
    }

    /* Transactions table */
    #view-transactions .card {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .data-table {
        font-size: 0.72rem !important;
    }
    .data-table th,
    .data-table td {
        padding: 5px 3px !important;
    }

    /* Tier 2 split cards: keep side-by-side but compact */
    .dashboard-grid .card[style*="display:flex"][style*="gap:16px"] > div {
        border-inline-end: none !important;
        padding-inline-end: 0 !important;
    }

    /* ========== WALLETS VIEW ========== */
    #view-wallets .card div[style*="display:flex"][style*="gap:8px"] {
        flex-wrap: wrap !important;
    }
    #view-wallets .card .btn-sm {
        font-size: 0.75rem !important;
        padding: 5px 8px !important;
    }

    /* ========== LOANS VIEW ========== */
    #view-loans .card div[style*="display:flex"] {
        flex-wrap: wrap !important;
    }

    /* ========== MODALS ========== */
    .modal-card {
        width: 95% !important;
        max-width: 95% !important;
    }
    .modal-body {
        /* Removed max-height:70vh too. Same reason: short forms shouldn't
           reserve scroll space. The overlay (with overflow-y:auto) handles
           overflow for genuinely tall modals. */
        overflow-y: visible !important;
    }

    /* ========== BUDGET/SETTINGS VIEW ========== */
    #view-budget .card div[style*="display:flex"] {
        flex-wrap: wrap !important;
    }
    #view-budget table {
        font-size: 0.8rem !important;
    }
    #view-budget table th,
    #view-budget table td {
        padding: 6px 8px !important;
    }

    /* ========== GENERAL TABLE FIXES ========== */
    table {
        font-size: 0.82rem;
    }
    .data-table th, .data-table td {
        padding: 8px 6px !important;
    }

    /* ========== CATEGORY PICKER ========== */
    .cat-picker__grid {
        max-height: 100px !important;
        overflow-y: auto !important;
    }
    .cat-pill {
        font-size: 0.65rem !important;
        padding: 3px 6px !important;
    }

    /* ========== PERFORMANCE TABLES ========== */
    #performance-tables-container table {
        font-size: 0.72rem !important;
    }
    #performance-tables-container table th,
    #performance-tables-container table td {
        padding: 4px 3px !important;
    }

    /* Performance category cards compact */
    #performance-tables-container > div > div[style*="display:flex"] {
        flex-wrap: wrap !important;
        gap: 4px !important;
    }

    /* ========== SAVINGS & INVESTMENTS ========== */
    #savings-view-container {
        width: 100% !important;
        max-width: 100% !important;
        padding: 0 !important;
    }
    #savings-view-container > div {
        width: 100% !important;
        max-width: 100% !important;
    }
    #savings-view-container div[style*="border:1px"] {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        padding: 14px !important;
    }

    /* ========== CASH/DEBT SECTION FONT SIZES ========== */
    .dash-name {
        font-size: 13px !important;
    }
    .dash-balance {
        font-size: 13px !important;
    }
    .dash-badge {
        font-size: 11px !important;
    }

    /* ========== WANTS TABLE VISIBILITY FIX ========== */
    #perf-group-want,
    #perf-group-need,
    .perf-group {
        display: block !important;
        visibility: visible !important;
        opacity: 1 !important;
        width: 100% !important;
        min-width: 100% !important;
        overflow: hidden !important;
        height: auto !important;
        max-height: none !important;
        flex-shrink: 0 !important;
        margin-bottom: 10px !important;
    }
}

