/* =====================================================
   DESKTOP - No sidebar, no hamburger, full-width layout
   ===================================================== */

@media (min-width: 1024px) {
    /* Hide hamburger button on desktop */
    .hamburger-btn {
        display: none !important;
    }
    
    /* Hide sidebar completely on desktop */
    .sidebar {
        display: none !important;
    }
    
    /* Hide sidebar overlay on desktop */
    .sidebar-overlay {
        display: none !important;
    }
    
    /* Make main content full-width on desktop */
    .main-content {
        margin-left: 0 !important;
        width: 100% !important;
    }
}

        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    }

    /* Wider modals on large screens for better readability */
    .modal-content {
        max-width: 900px;
    }
}

@media (min-width: 2560px) {
    .container {
        max-width: 1800px;
    }

    header .header-content {
        max-width: 1800px;
    }

    .footer-content {
        max-width: 1800px;
    }

    .budget-health-grid {
        max-width: 1800px;
    }

    .budget-health-header {
        max-width: 1800px;
    }

    /* Even wider modals on 4K / ultrawide */
    .modal-content {
        max-width: 1000px;
    }
}

/* =====================================================
   PHASE 3: Firefox Scrollbar Compatibility (Issue #20)
   ===================================================== */

/* Firefox scrollbar styles for all scrollable elements */
.bills-list,
.sidebar-nav,
.modal-content,
.detail-list {
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

/* Any element with overflow-y: auto should get thin scrollbars in Firefox */
[style*="overflow-y: auto"],
[style*="overflow-y: scroll"] {
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

/* Dark mode scrollbar color adjustment */
[data-theme="dark"] .bills-list,
[data-theme="dark"] .sidebar-nav,
[data-theme="dark"] .modal-content,
[data-theme="dark"] .detail-list,
[data-theme="dark"] [style*="overflow-y: auto"],
[data-theme="dark"] [style*="overflow-y: scroll"] {
    scrollbar-color: #475569 transparent;
}

/* =====================================================
   PHASE 3: Form Validation Feedback (Issue #20)
   ===================================================== */

/* Invalid form field styling - shown after user interaction */
input:user-invalid,
select:user-invalid,
textarea:user-invalid {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.15);
}

/* Also support explicit aria-invalid for JS validation */
input[aria-invalid="true"],
select[aria-invalid="true"],
textarea[aria-invalid="true"] {
    border-color: var(--danger) !important;
    box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.15);
}

/* Error message styling */
.error-message {
    color: var(--danger);
    font-size: 0.875rem;
    margin-top: 4px;
    display: flex;
    align-items: center;
    gap: 4px;
}

.error-message::before {
    content: "\26A0";
    font-size: 0.8rem;
}

/* Success state for validated fields */
input:user-valid:not(:placeholder-shown),
select:user-valid:not([value=""]) {
    border-color: var(--success);
}

/* =====================================================
   PHASE 3: Keyboard Shortcuts Modal (Issue #20)
   ===================================================== */

.shortcuts-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

.shortcuts-modal-overlay.active {
    opacity: 1;
    visibility: visible;
}

.shortcuts-modal {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 24px 32px;
    max-width: 480px;
    width: 90%;
    box-shadow: var(--shadow-lg);
}

.shortcuts-modal h3 {
    margin: 0 0 20px 0;
    font-size: 1.1rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.shortcuts-modal h3 button {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.3rem;
    cursor: pointer;
    padding: 4px 8px;
    line-height: 1;
    border-radius: 4px;
}

.shortcuts-modal h3 button:hover {
    background: var(--bg-elevated);
}

.shortcut-group {
    margin-bottom: 16px;
}

.shortcut-group-title {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.shortcut-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 0;
}

.shortcut-keys {
    display: flex;
    align-items: center;
    gap: 4px;
}

.shortcut-key {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 26px;
    padding: 0 6px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-family: 'Inter', monospace;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-primary);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.shortcut-then {
    color: var(--text-secondary);
    font-size: 0.75rem;
    padding: 0 2px;
}

.shortcut-label {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

/* =====================================================
   RESPONSIVE DESIGN - Small Screens (MacBook 13")
   ===================================================== */

@media (max-width: 1366px) {
    .budget-health-container {
        max-width: 100% !important;
        padding: 16px !important;
    }

    .budget-summary-header {
        padding: 24px !important;
        margin-bottom: 24px !important;
    }

    .budget-summary-title {
        font-size: 1.5rem !important;
    }

    .budget-stat-card {
        padding: 16px !important;
    }

    .budget-stat-value {
        font-size: 1.5rem !important;
    }

    /* Grid adjustments */
    .budget-summary-stats {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)) !important;
        gap: 16px !important;
    }

    /* Category cards - reduce to 2 columns on smaller screens */
    .category-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 16px !important;
    }

    /* Buttons and inputs */
    input, select, button {
        font-size: 0.95rem !important;
        padding: 8px 12px !important;
    }

    /* Modal adjustments */
    .modal {
        width: 95% !important;
        max-width: 95% !important;
    }

    /* Tab buttons */
    .budget-sub-tab {
        padding: 8px 16px !important;
        font-size: 0.85rem !important;
    }
}

@media (max-width: 1024px) {
    .budget-health-container {
        padding: 12px !important;
    }

    /* Single column on very small screens */
    .category-grid {
        grid-template-columns: 1fr !important;
    }

    .budget-summary-header {
        padding: 16px !important;
    }

    .budget-summary-title {
        font-size: 1.25rem !important;
    }

    .budget-summary-stats {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

/* =====================================================
   RESPONSIVE DESIGN - Tablet (iPad, landscape phones)
   ===================================================== */

@media (max-width: 768px) {
    /* Header adjustments for hamburger menu */
    header .header-content {
        gap: 12px !important;
    }

    /* Hide header meta on tablets and smaller */
    header .header-meta {
        display: none !important;
    }

    /* Budget grid adjustments */
    .budget-summary-stats {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .budget-stat-value {
        font-size: 1.25rem !important;
    }

    /* Overview widgets - 2 column */
    .overview-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* --- Phase 2: Fix fixed-width grids at tablet --- */

    /* Comparison pie charts: stack vertically */
    #pieChartContainer [style*="grid-template-columns: 1fr 1fr"],
    #variancePieContainer [style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
    }

    /* 4-column grids -> 2 columns on tablet */
    [style*="grid-template-columns: repeat(4, 1fr)"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* 3-column grids -> 2 columns on tablet */
    [style*="grid-template-columns: repeat(3, 1fr)"] {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* Budget health category grids with fixed widths */
    [style*="grid-template-columns: 150px 1fr 90px"] {
        grid-template-columns: 100px 1fr 70px !important;
    }

    /* Receipt items grid (5 columns -> stacked) */
    [style*="grid-template-columns: 2fr 80px 100px 160px 30px"] {
        grid-template-columns: 1fr !important;
        gap: 4px !important;
    }

    /* Transaction detail grid (5 columns) */
    .transaction-detail-row {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }
    [style*="grid-template-columns: 100px 1fr 150px 120px 180px"] {
        grid-template-columns: 1fr !important;
        gap: 4px !important;
    }

    /* Receipt review modal: stack image and data */
    [style*="grid-template-columns: 1fr 1fr"][style*="gap: 24px"][style*="padding: 24px"] {
        grid-template-columns: 1fr !important;
    }

    /* Monthly review checklist: single column */
    [style*="grid-template-columns: repeat(2, 1fr)"][style*="gap: 10px"] {
        grid-template-columns: 1fr !important;
    }

    /* Comparison year nav: stack on smaller screens */
    #comparison [style*="justify-content: space-between"][style*="align-items: center"] {
        flex-wrap: wrap !important;
        gap: 12px !important;
    }

    /* --- Phase 2: Table responsiveness --- */

    /* Scrollable table containers with visual indicator */
    .transactions-table-container,
    [style*="overflow-x: auto"] {
        position: relative;
    }

    /* Fade hint for scrollable tables */
    .transactions-table-container::after,
    .table-scroll-container::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 30px;
        background: linear-gradient(to right, transparent, var(--bg-card));
        pointer-events: none;
        z-index: 1;
    }

    /* Tighter table cells on tablet */
    .transactions-table th,
    .transactions-table td {
        padding: 8px 6px !important;
        font-size: 0.85rem !important;
    }

    /* Linkable receipt card grid */
    .linkable-receipt-card {
        grid-template-columns: 60px 1fr auto !important;
        gap: 12px !important;
    }
}

/* =====================================================
   RESPONSIVE DESIGN - Standard Mobile (most phones)
   ===================================================== */

@media (max-width: 480px) {
    /* Container padding */
    .container {
        padding: 20px 16px !important;
    }

    /* Header - compact for mobile */
    header {
        padding: 12px 16px !important;
    }

    header h1 {
        font-size: 1rem !important;
        -webkit-text-fill-color: white !important;
    }

    header .header-content > div:first-child {
        gap: 8px !important;
    }

    /* Hamburger button sizing for touch - 44px minimum */
    .hamburger-btn {
        width: 44px !important;
        height: 44px !important;
        min-width: 44px;
        min-height: 44px;
    }

    /* Theme toggle - hide label, keep toggle */
    .theme-toggle-label {
        display: none !important;
    }

    /* Settings button - ensure 44px touch target */
    header .header-content button {
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* --- Phase 2: Touch Targets 44px Minimum --- */

    /* Info tooltips: larger tap target on mobile */
    .info-tooltip {
        min-width: 36px !important;
        min-height: 36px !important;
        width: 36px !important;
        height: 36px !important;
        font-size: 0.85rem !important;
    }

    /* Close buttons: ensure 44px */
    .close-btn,
    [onclick*="close"] {
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* Edit/delete rule buttons: 44px touch targets */
    .hover-edit-btn,
    .hover-delete-btn {
        width: 44px !important;
        height: 44px !important;
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* Sub-tab buttons: minimum height for touch */
    .tab,
    .budget-sub-tab {
        min-height: 44px !important;
        padding: 10px 14px !important;
        font-size: 0.8rem !important;
        white-space: nowrap;
    }

    /* Sidebar nav buttons: 48px for easy tapping */
    .sidebar-nav button {
        min-height: 48px !important;
        padding: 16px 20px !important;
        font-size: 1rem !important;
    }

    /* Make stat cards full width */
    .stat-card,
    .overview-widget,
    .health-card {
        width: 100% !important;
        margin-bottom: 12px;
    }

    /* Stack grids to 1 column */
    .grid-2-col,
    .grid-3-col,
    .grid-4-col,
    .overview-grid,
    .quick-stats {
        grid-template-columns: 1fr !important;
    }

    /* Budget health grid single column */
    .budget-health-grid {
        grid-template-columns: 1fr !important;
    }

    .budget-summary-stats {
        grid-template-columns: 1fr !important;
    }

    /* Category grid single column */
    .category-grid {
        grid-template-columns: 1fr !important;
    }

    /* Panels - reduce padding */
    .panel {
        padding: 12px !important;
        border-radius: 8px !important;
    }

    .panel h2 {
        font-size: 1rem !important;
    }

    /* Modals - full width on mobile */
    .modal-content {
        width: 95vw !important;
        max-width: 95vw !important;
        margin: 8px auto !important;
        padding: 16px !important;
        max-height: 90vh !important;
    }

    /* Budget sub-tabs - scrollable */
    .budget-sub-tab {
        padding: 8px 12px !important;
        font-size: 0.8rem !important;
        white-space: nowrap;
    }

    /* --- Phase 2: Fix ALL fixed-width grids on mobile --- */

    /* All multi-column inline grids -> single column */
    [style*="grid-template-columns: repeat(4, 1fr)"],
    [style*="grid-template-columns: repeat(3, 1fr)"],
    [style*="grid-template-columns: repeat(2, 1fr)"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns: repeat(5, 1fr)"] {
        grid-template-columns: 1fr !important;
    }

    /* Budget health fixed-width grids */
    [style*="grid-template-columns: 150px 1fr 90px"] {
        grid-template-columns: 1fr !important;
        gap: 4px !important;
    }

    /* Receipt item grid */
    [style*="grid-template-columns: 2fr 80px 100px 160px 30px"] {
        grid-template-columns: 1fr 1fr !important;
        gap: 6px !important;
    }

    /* Transaction detail grid */
    .transaction-detail-row,
    [style*="grid-template-columns: 100px 1fr 150px 120px 180px"] {
        grid-template-columns: 1fr !important;
        gap: 4px !important;
    }

    /* Split row grid */
    [style*="grid-template-columns: 1fr 120px 40px"] {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    /* Linkable receipt card */
    .linkable-receipt-card {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    /* --- Phase 2: Table responsiveness --- */

    /* Comparison table */
    .comparison-table {
        font-size: 0.8rem !important;
    }

    .comparison-table th,
    .comparison-table td {
        padding: 8px 6px !important;
    }

    /* Transactions table compact */
    .transactions-table {
        font-size: 0.75rem !important;
    }

    .transactions-table th,
    .transactions-table td {
        padding: 6px 4px !important;
        white-space: nowrap;
    }

    /* Hide less important table columns on mobile */
    .transactions-table th:nth-child(4),
    .transactions-table td:nth-child(4),
    .transactions-table th:nth-child(5),
    .transactions-table td:nth-child(5) {
        display: none !important;
    }

    /* Receipt cards - single column */
    .receipt-grid {
        grid-template-columns: 1fr !important;
    }

    /* Paycheck grid */
    .paycheck-grid {
        grid-template-columns: 1fr !important;
    }

    /* Footer */
    .footer {
        padding: 16px !important;
    }

    .footer-content {
        flex-direction: column !important;
        text-align: center !important;
        gap: 8px !important;
    }

    /* Sidebar adjustments for mobile */
    .sidebar {
        width: 260px !important;
        left: -260px !important;
    }

    .sidebar.open {
        left: 0 !important;
    }

    .sidebar-nav button {
        padding: 16px 20px !important;
        font-size: 1rem !important;
    }

    /* Financial advisor chat */
    .chat-input-area {
        flex-direction: column !important;
        gap: 8px !important;
    }

    .chat-input-area input {
        width: 100% !important;
    }

    .chat-input-area button {
        width: 100% !important;
    }

    /* Comparison summary cards: stack annual impact */
    .impact-card {
        min-width: 100% !important;
    }

    /* Annual impact arrows: hide on mobile */
    [style*="font-size: 2rem"][style*="display: flex"][style*="align-items: center"] {
        display: none !important;
    }

    /* Filter bar: stack filters vertically */
    .filter-bar {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    .filter-bar .filter-group {
        width: 100% !important;
    }

    .filter-bar .filter-select {
        width: 100% !important;
        min-width: unset !important;
    }

    /* Variance legend: stack on mobile */
    #comparison [style*="display: flex"][style*="gap: 16px"][style*="align-items: center"] {
        flex-wrap: wrap !important;
    }
}

/* =====================================================
   RESPONSIVE DESIGN - Small Mobile (iPhone SE, etc.)
   ===================================================== */

@media (max-width: 375px) {
    .container {
        padding: 16px 12px !important;
    }

    header {
        padding: 10px 12px !important;
    }

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

    /* Stack inline grids to single column */
    [style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
    }

    /* Even more compact stat values */
    .stat-card .value,
    .budget-stat-value {
        font-size: 1.1rem !important;
    }

    /* Sidebar narrower on tiny screens */
    .sidebar {
        width: 240px !important;
        left: -240px !important;
    }

    .sidebar.open {
        left: 0 !important;
    }

    /* Modal full screen on tiny phones */
    .modal-content {
        width: 100vw !important;
        max-width: 100vw !important;
        margin: 0 !important;
        border-radius: 0 !important;
        min-height: 100vh !important;
    }

    /* Table even more compact */
    .comparison-table th,
    .comparison-table td {
        padding: 6px 4px !important;
        font-size: 0.75rem !important;
    }

    /* Tooltips: position to left on very small screens */
    .info-tooltip .tooltip-text {
        left: auto !important;
        right: -10px !important;
        margin-left: 0 !important;
        width: 220px !important;
    }

    .info-tooltip .tooltip-text::after {
        left: auto !important;
        right: 20px !important;
    }
}

/* =====================================================
   PHASE 3: PWA & Mobile Optimizations
   ===================================================== */

/* ---- Header layout improvements for mobile ---- */
@media (max-width: 480px) {
    /* Header content - prevent overflow and ensure proper layout */
    header .header-content {
        flex-wrap: nowrap !important;
        gap: 8px !important;
        justify-content: space-between !important;
        align-items: center !important;
        min-height: 48px !important;
    }

    /* Left side - hamburger + title: allow title to truncate */
    header .header-content > div:first-child {
        min-width: 0 !important;
        flex: 1 !important;
    }

    header h1 {
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        white-space: nowrap !important;
    }

    /* Right side controls - tighter spacing, no shrink */
    header .header-content > div:last-child {
        gap: 8px !important;
        flex-shrink: 0 !important;
    }

    /* Theme toggle switch - slightly smaller on narrow screens */
    .toggle-switch {
        min-width: 44px !important;
        min-height: 32px !important;
    }
}

@media (max-width: 375px) {
    /* Very narrow: hide theme toggle text completely, shrink controls */
    header .header-content > div:last-child {
        gap: 4px !important;
    }

    /* Smaller title on tiny phones */
    header h1 {
        max-width: 140px !important;
    }
}

/* ---- Budget sub-tabs: horizontally scrollable ---- */
@media (max-width: 768px) {
    /* Sub-tabs container: horizontal scroll instead of wrap */
    #budget-health > div:first-child {
        display: flex !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        scroll-snap-type: x mandatory !important;
        scrollbar-width: none !important; /* Firefox */
        -ms-overflow-style: none !important; /* IE/Edge */
        width: 100% !important;
        padding-bottom: 4px !important;
    }

    /* Hide scrollbar on WebKit browsers */
    #budget-health > div:first-child::-webkit-scrollbar {
        display: none;
    }

    /* Sub-tab buttons: snap into place */
    #budget-health > div:first-child .budget-sub-tab {
        flex-shrink: 0 !important;
        scroll-snap-align: start !important;
    }

    /* Fade gradient on right to indicate scrollability */
    #budget-health {
        position: relative !important;
    }

    #budget-health::before {
        content: '' !important;
        position: absolute !important;
        top: 0 !important;
        right: 0 !important;
        width: 40px !important;
        height: 52px !important;
        background: linear-gradient(to right, transparent, var(--bg-card)) !important;
        pointer-events: none !important;
        z-index: 2 !important;
        border-radius: 0 12px 0 0 !important;
    }
}

/* ---- Chart containers: proper height on mobile ---- */
@media (max-width: 480px) {
    /* Ensure chart containers have minimum height on mobile */
    #barLineChartContainer {
        height: 250px !important;
    }

    /* Pie chart containers */
    #pieChartContainer div[style*="height: 250px"] {
        height: 200px !important;
    }

    /* Variance chart */
    #varianceBarLineContainer div[style*="height: 300px"] {
        height: 250px !important;
    }

    /* Category pie chart in budget health */
    div[style*="height: 280px"]:has(> #categoryPieChart) {
        height: 220px !important;
    }

    /* Chart labels - smaller on mobile */
    canvas {
        max-width: 100% !important;
    }
}


/* ===== TRANSACTIONS PANEL RESPONSIVE ===== */
@media (max-width: 1024px) {
    /* Stack table and sidebar vertically on tablets */
    #transactions > div:last-child {
        grid-template-columns: 1fr !important;
    }
    /* Sidebar moves below table */
    #transactions > div:last-child > div:last-child {
        order: -1;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 16px;
    }
}

@media (max-width: 768px) {
    /* Stack summary cards */
    #txnSummaryCards {
        grid-template-columns: 1fr 1fr !important;
    }
    /* Stack filter bar */
    #txnFilterBar > div {
        flex-direction: column !important;
    }
    #txnFilterBar input[type="number"] {
        width: 100% !important;
    }
    /* Pagination compact */
    #txnPagination {
        flex-direction: column !important;
        gap: 12px !important;
    }
    /* Hide source column on mobile */
    #transactions th:nth-child(5),
    #transactions td:nth-child(5) {
        display: none;
    }
}

@media (max-width: 480px) {
    #txnSummaryCards {
        grid-template-columns: 1fr !important;
    }
    /* Hide checkbox column on very small screens */
    #transactions th:first-child,
    #transactions td:first-child {
        display: none;
    }
    /* Bulk action bar hide */
    #txnBulkActionBar {
        display: none !important;
    }
}
