/* ===========================
   Modals & Overlays
=========================== */

/* Standardized Modal Animation - lightweight for performance */
@keyframes modalFadeIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Modal backdrop - semi-transparent overlay (no blur for performance) */
.modal-backdrop {
    background-color: rgba(0, 0, 0, var(--modal-backdrop-opacity));
}

/* Apply animation to all modals */
.modal-content {
    animation: modalFadeIn var(--modal-animation-duration) ease-out;
}

/* Modal container - sleek theme alignment */
.modal-content {
    font-family: var(--font-family);
    background-color: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid var(--border-subtle, var(--border-primary));
    border-radius: var(--modal-border-radius);
    box-shadow: var(--modal-shadow);
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: transparent;
    border-bottom: 1px solid var(--border-subtle, var(--border-primary));
    color: var(--text-primary);
    padding: var(--modal-header-padding);
}
.modal-header .btn-close {
    margin-left: auto;
    order: 2;
    flex-shrink: 0;
}
.modal-header .modal-title {
    order: 1;
    margin-right: 12px;
}

.modal-body {
    background-color: transparent;
    color: var(--text-primary);
    padding: var(--modal-body-padding);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-body);
}

.modal-footer {
    background-color: transparent;
    border-top: 1px solid var(--border-subtle, var(--border-primary));
    padding: var(--modal-footer-padding);
}

.modal-title {
    font-family: var(--font-family);
    font-size: var(--font-size-2xl);
    font-weight: 700;
    line-height: var(--line-height-tight);
    color: var(--text-primary);
}

/* Close button - round hover circle matching UI */
.modal .btn-close {
    width: 32px;
    height: 32px;
    padding: 0;
    margin: 0;
    border-radius: 50%;
    background-color: transparent;
    opacity: 0.7;
    filter: var(--btn-close-filter, none);
}
.modal .btn-close:hover {
    opacity: 1;
    background-color: var(--bg-hover);
}

/* Bootstrap Table Dark Mode Support in Modals */
.modal-body .table {
    color: var(--text-primary) !important;
    border-color: var(--border-ui) !important;
    background-color: var(--bg-primary) !important;
}

.modal-body .table thead,
.modal-body .table thead th {
    background-color: var(--bg-secondary) !important;
    color: var(--text-primary) !important;
    border-color: var(--border-ui) !important;
}

.modal-body .table tbody,
.modal-body .table tbody tr {
    background-color: var(--bg-primary) !important;
    color: var(--text-primary) !important;
}

.modal-body .table th,
.modal-body .table td {
    border-color: var(--border-ui) !important;
    color: var(--text-primary) !important;
    background-color: transparent !important;
}

.modal-body .table-hover tbody tr:hover {
    background-color: var(--bg-hover) !important;
    color: var(--text-primary) !important;
}

/* Override Bootstrap's default white backgrounds */
.modal-body .table-container {
    background-color: var(--bg-primary) !important;
}

.modal-body .table tbody tr td,
.modal-body .table tbody tr th {
    background-color: var(--bg-primary) !important;
}

/* Chart Overlay Modal */
.chart-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-color: rgba(0, 0, 0, 0.8);
    display: none;
    z-index: 1070;
    overflow: auto;
}

.chart-overlay.active {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 20px;
}

.chart-overlay-content {
    background: var(--bg-primary);
    border-radius: var(--modal-border-radius);
    padding: var(--modal-body-padding);
    max-width: 90vw;
    max-height: 90vh;
    width: 90%;
    height: 90vh;
    overflow: auto;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
    position: relative;
    animation: modalFadeIn var(--modal-animation-duration) ease-out;
}

/* Overlay Close Button - Dark mode compatible */
.overlay-close-btn {
    position: fixed;
    top: 25px;
    right: 25px;
    background: var(--bg-secondary);
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1080;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    color: var(--error-red);
    font-weight: bold;
}

.overlay-close-btn:hover {
    background: var(--bg-hover);
}

/* Expand Button - Dark mode compatible */
.expand-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-ui);
    border-radius: 4px;
    width: 28px;
    height: 28px;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    transition: all 0.2s ease;
}

.expand-btn:hover {
    background: var(--bg-hover);
}

/* Responsive Overlay Display */
@media (max-width: 768px) {
    #transactions-grid-overlay {
        display: none !important;
    }
    #transactions-cards-overlay {
        display: block !important;
    }
}

@media (min-width: 769px) {
    #transactions-grid-overlay {
        display: block !important;
    }
    #transactions-cards-overlay {
        display: none !important;
    }
}

/* Income Overlay Responsive Display */
@media (max-width: 768px) {
    #income-transactions-grid-overlay {
        display: none !important;
    }
    #income-transactions-cards-overlay {
        display: block !important;
    }
}

@media (min-width: 769px) {
    #income-transactions-grid-overlay {
        display: block !important;
    }
    #income-transactions-cards-overlay {
        display: none !important;
    }
}

/* Expenses Overlay Responsive Display */
@media (max-width: 768px) {
    #all-expenses-grid-overlay {
        display: none !important;
    }
    #expenses-transactions-cards-overlay {
        display: block !important;
    }
}

@media (min-width: 769px) {
    #all-expenses-grid-overlay {
        display: block !important;
    }
    #expenses-transactions-cards-overlay {
        display: none !important;
    }
}

/* AI Insights Modal Responsive Styling */
@media (max-width: 768px) {
    /* Stack columns vertically on mobile */
    #insight-detail-modal .modal-dialog {
        max-width: 100vw !important; /* Full width on mobile */
        margin: 0 !important; /* No margins for maximum space */
        height: 100vh !important;
    }
    
    #insight-detail-modal .modal-content {
        height: 100vh !important;
        border-radius: 0 !important; /* Edge-to-edge */
    }
    
    #insight-detail-modal .modal-body {
        padding: 10px !important; /* Minimal padding for maximum width */
        max-height: calc(100vh - 60px) !important; /* Account for header only */
        overflow-y: auto !important;
    }
    
    /* Hide footer buttons on mobile - use X button in header or swipe gestures */
    #insight-detail-modal .modal-footer {
        display: none !important;
    }
    
    /* Ensure cards stack properly with minimal spacing */
    #insight-detail-modal .row {
        margin: 0 !important; /* Remove row margins */
    }
    
    #insight-detail-modal .row > div {
        padding: 0 !important; /* Remove column padding for maximum width */
        margin-bottom: 10px !important;
    }
    
    #insight-detail-modal .row > div:last-child {
        margin-bottom: 0;
    }
    
    /* Cards take full width with minimal padding */
    #insight-detail-modal .card {
        max-width: 100% !important;
        width: 100% !important;
        overflow: hidden !important;
        border-radius: 8px !important; /* Slightly rounded */
        margin: 0 !important;
    }
    
    /* Minimal card body padding for maximum content space */
    #insight-detail-modal .card-body {
        padding: 12px !important; /* Reduced from 15px */
        overflow-x: hidden !important;
        word-wrap: break-word !important;
    }
    
    /* Ensure text doesn't overflow */
    #insight-detail-modal .card-body h2,
    #insight-detail-modal .card-body h5,
    #insight-detail-modal .card-body p,
    #insight-detail-modal .card-body div {
        max-width: 100% !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
    }
    
    /* Scale down metric display on mobile */
    #insight-detail-modal .card-body > div > div[style*="fontSize: '3rem'"] {
        font-size: 2rem !important;
    }
    
    /* Adjust title font size */
    #insight-detail-modal .card-body h2 {
        font-size: 1.2rem !important;
        line-height: 1.3 !important;
    }
    
    /* Primary metric container responsive */
    #insight-detail-modal .card-body > div > div[style*="textAlign: 'center'"] {
        padding: 15px 10px !important;
        margin: 15px 0 !important;
    }
    
    /* Breakdown/Insights section */
    #insight-detail-modal .card-body > div > div[style*="backgroundColor: '#F6F8FD'"] {
        padding: 10px !important;
        margin: 10px 0 !important;
    }
    
    /* Action section responsive */
    #insight-detail-modal .card-body > div > div[style*="borderTopLeftRadius: '16px'"] {
        padding: 15px !important;
        margin-top: 10px !important;
    }
    
    /* Ensure all divs inside card respect container width */
    #insight-detail-modal .card-body * {
        max-width: 100%;
        box-sizing: border-box;
    }
    
    /* Chart container takes full width on mobile */
    #insight-detail-modal .card-body > div {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Plotly charts take full width */
    #insight-detail-modal .js-plotly-plot {
        width: 100% !important;
        max-width: 100% !important;
    }
}

@media (min-width: 769px) and (max-width: 991px) {
    /* Tablet view - still side by side but adjusted */
    #insight-detail-modal .modal-dialog {
        max-width: 90vw !important;
    }
}

/* ===========================
   Standardized Modal Header Classes
=========================== */

/* Action modals - same black title as page (sleek) */
.modal-header-purple .modal-title {
    color: var(--text-primary);
    font-weight: var(--font-weight-bold);
}

/* Danger/error header for delete/warning modals */
.modal-header-danger .modal-title {
    color: var(--error-red);
    font-weight: var(--font-weight-bold);
}

/* Standard header (default) */
.modal-header-standard .modal-title {
    color: var(--text-primary);
    font-weight: var(--font-weight-bold);
}

/* Info header for informational modals */
.modal-header-info .modal-title {
    color: var(--info-blue);
    font-weight: var(--font-weight-bold);
}

/* ===========================
   Standardized Modal Body Classes
=========================== */

/* Scrollable body with max height */
.modal-body-scroll {
    max-height: 70vh;
    overflow-y: auto;
}

/* Compact body padding */
.modal-body-compact {
    padding: var(--spacing-md);
}

/* Full-height body for full-screen modals */
.modal-body-full {
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

/* ===========================
   Standardized Modal Footer Classes
=========================== */

/* Standard footer: secondary cancel left, primary action right */
.modal-footer {
    display: flex;
    gap: 8px;
}

.modal-footer .btn {
    margin: 0;
}

/* Centered footer (single button or centered actions) */
.modal-footer-centered {
    justify-content: center !important;
}

/* Outline secondary in modal footer - match flat button style */
.modal-footer .btn-outline-secondary {
    background-color: var(--bg-primary) !important;
    border: 1px solid var(--border-ui) !important;
    color: var(--text-primary) !important;
}
.modal-footer .btn-outline-secondary:hover {
    background-color: var(--bg-hover) !important;
    border-color: var(--border-primary) !important;
    color: var(--text-primary) !important;
}

/* ===========================
   Modal Form Styling
=========================== */

/* Labels inside modals */
.modal-body .form-label {
    color: var(--text-primary);
    font-weight: var(--font-weight-semibold);
    font-size: var(--font-size-sm);
    margin-bottom: 6px;
}

/* Inputs, textareas, dropdowns inside modals */
.modal-body .form-control,
.modal-body textarea.form-control {
    border-radius: 8px;
    border: 1px solid var(--border-ui);
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
    font-size: var(--font-size-sm);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

/* Placeholder: theme-aware so readable in both light and dark mode */
.modal-body .form-control::placeholder,
.modal-body textarea.form-control::placeholder {
    color: var(--text-secondary);
    opacity: 1;
}

.modal-body .form-control:focus,
.modal-body textarea.form-control:focus {
    border-color: var(--brand-purple);
    box-shadow: 0 0 0 2px rgba(180, 100, 255, 0.12);
}

/* ===========================
   Modal Upload Area
=========================== */

.modal-upload-area {
    width: 100%;
    min-height: 120px;
    border-width: 2px;
    border-style: dashed;
    border-radius: 8px;
    border-color: var(--border-ui);
    text-align: center;
    padding: 20px;
    cursor: pointer;
    background-color: var(--bg-secondary);
    transition: border-color 0.2s ease, background-color 0.2s ease;
}

.modal-upload-area:hover {
    border-color: var(--brand-purple);
    background-color: var(--bg-tertiary);
}

.modal-upload-area-purple {
    border-color: var(--brand-purple);
}

/* ===========================
   Modal Info/Alert Boxes
=========================== */

.modal-info-box {
    padding: 16px;
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-ui);
    border-radius: 8px;
    color: var(--text-primary);
}

.modal-info-box-purple {
    border: 2px solid var(--brand-purple);
}

.modal-info-box-danger {
    border: 2px solid var(--error-red);
    background-color: rgba(220, 38, 38, 0.04);
}

/* Warning block for destructive modals */
.modal-warning-text {
    color: var(--error-red);
    font-weight: var(--font-weight-bold);
    font-size: 1.1rem;
}

/* Success text inside modals */
.modal-success-text {
    color: var(--success-green);
    font-weight: var(--font-weight-medium);
    font-size: var(--font-size-sm);
    min-height: 20px;
    margin-bottom: 10px;
}

/* Error text inside modals */
.modal-error-text {
    color: var(--error-red);
    font-size: var(--font-size-sm);
    min-height: 20px;
    margin-bottom: 10px;
}

/* Helper/muted text inside modals */
.modal-helper-text {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    line-height: var(--line-height-body);
}

/* Instructions block (monospace hint) */
.modal-instructions-block {
    padding: 15px;
    background-color: var(--bg-secondary);
    border-radius: 8px;
    color: var(--text-primary);
}

/* ===========================
   Modal Close Button Theme
=========================== */

.btn-close {
    filter: var(--btn-close-filter, none);
}

/* Scrollable content container */
.modal-scroll-container {
    max-height: 500px;
    overflow: auto;
    border-radius: 8px;
    border: 1px solid var(--border-ui);
}

/* Custom modal close button (subcategory / bulk editor div modals) */
.custom-modal-close-btn:hover {
    background-color: var(--bg-hover) !important;
}

/* ===========================
   Retention Offer Modal (Cancel flow Step 2)
   Eyecatching offer card matching app theme
=========================== */

#cancel-sub-modal-step2 .retention-modal-eyebrow {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--brand-purple);
    margin-bottom: 8px;
}

#cancel-sub-modal-step2 .retention-modal-headline {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0 0 20px 0;
    line-height: 1.25;
}

#cancel-sub-modal-step2 .retention-offer-card {
    background: linear-gradient(135deg, color-mix(in srgb, var(--brand-purple) 6%, var(--bg-primary)) 0%, color-mix(in srgb, var(--brand-purple) 12%, var(--bg-primary)) 100%);
    border: 1.5px solid color-mix(in srgb, var(--brand-purple) 35%, transparent);
    border-radius: 14px;
    padding: 20px;
    margin-bottom: 20px;
    position: relative;
    overflow: hidden;
}

#cancel-sub-modal-step2 .retention-offer-card::before {
    content: '';
    position: absolute;
    top: -30px;
    right: -30px;
    width: 100px;
    height: 100px;
    background: radial-gradient(circle, color-mix(in srgb, var(--brand-purple) 12%, transparent) 0%, transparent 70%);
    pointer-events: none;
}

#cancel-sub-modal-step2 .retention-offer-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--brand-purple);
    color: white;
    font-size: 12px;
    font-weight: 700;
    padding: 4px 10px;
    border-radius: 0;
    margin-bottom: 12px;
    letter-spacing: 0.02em;
}

#cancel-sub-modal-step2 .retention-offer-price-row {
    display: flex;
    align-items: baseline;
    gap: 10px;
    margin-bottom: 6px;
}

#cancel-sub-modal-step2 .retention-offer-price-new {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
}

#cancel-sub-modal-step2 .retention-offer-price-period {
    font-size: 15px;
    color: var(--text-secondary);
    font-weight: 500;
}

#cancel-sub-modal-step2 .retention-offer-old-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
}

#cancel-sub-modal-step2 .retention-offer-price-old {
    font-size: 14px;
    color: var(--text-secondary);
    text-decoration: line-through;
    font-weight: 500;
}

#cancel-sub-modal-step2 .retention-savings-pill {
    display: inline-block;
    background: color-mix(in srgb, var(--success-green) 18%, transparent);
    color: var(--success-green);
    font-size: 12px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 0;
}

#cancel-sub-modal-step2 .retention-offer-body {
    font-size: 13.5px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin-top: 8px;
    margin-bottom: 0;
}

#cancel-sub-modal-step2 .retention-offer-body strong {
    color: var(--text-primary);
    font-weight: 600;
}

/* Simple “already on best rate” block (no card) */
#cancel-sub-modal-step2 .retention-already-rate {
    margin-bottom: 0;
}

/* Primary CTA in retention modal footer */
#cancel-sub-modal-step2 .modal-footer .btn-primary {
    border-radius: 12px;
    padding: 12px 20px;
    font-weight: 600;
    transition: transform 0.1s ease, background-color 0.15s ease;
}
#cancel-sub-modal-step2 .modal-footer .btn-primary:hover {
    transform: translateY(-1px);
}