/**
 * Mobile Optimizations for Customer Portal
 * Ensures professional responsive design on mobile devices
 * Includes accessibility and touch target improvements
 */

/* ===================================
   Touch Target Improvements (iOS/Android)
   Minimum 44px for accessibility
   =================================== */

/* All interactive elements must be at least 44px */
button,
.btn,
a,
input[type="button"],
input[type="submit"],
.form-check-input,
.btn-close {
    min-height: 44px;
    min-width: 44px;
}

/* Button improvements */
.btn {
    padding: 12px 16px;
    font-size: 16px;
    line-height: 1.25;
    border-radius: 8px;
    font-weight: 500;
}

.btn-lg {
    min-height: 50px;
    padding: 15px 20px;
    font-size: 18px;
}

.btn-sm {
    min-height: 44px;
    padding: 10px 14px;
    font-size: 14px;
}

/* Form inputs */
.form-control,
.form-select {
    min-height: 44px;
    padding: 12px 16px;
    font-size: 16px; /* Prevents zoom on iOS */
    border-radius: 8px;
}

/* Checkboxes and radios */
.form-check-input {
    width: 20px;
    height: 20px;
    margin-top: 0;
    cursor: pointer;
}

.form-check-label {
    cursor: pointer;
    padding-left: 8px;
    line-height: 1.4;
    font-size: 16px;
}

/* Touch-friendly close buttons */
.btn-close {
    padding: 12px;
    margin: 0;
    opacity: 0.8;
}

/* Link improvements */
a {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 8px;
    margin: -8px;
    text-decoration: none;
}

a:hover, a:focus {
    text-decoration: underline;
}

/* Table touch targets */
.table td, .table th {
    padding: 12px 8px;
    vertical-align: middle;
}

/* Payment method buttons */
.payment-method-btn {
    min-height: 56px;
    padding: 16px;
    margin-bottom: 8px;
    border-radius: 12px;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Focus indicators for accessibility */
button:focus,
.btn:focus,
a:focus,
input:focus,
.form-check-input:focus {
    outline: 2px solid #007bff;
    outline-offset: 2px;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}

/* Validation states */
.form-control.is-invalid,
.form-control:invalid {
    border-color: #dc3545;
    box-shadow: 0 0 0 3px rgba(220, 53, 69, 0.25);
}

.form-control.is-valid,
.form-control:valid {
    border-color: #28a745;
    box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.25);
}

/* Error text styling */
.invalid-feedback {
    display: block;
    color: #dc3545;
    font-size: 14px;
    margin-top: 4px;
}

/* Loading states */
.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.spinner-border-sm {
    width: 1rem;
    height: 1rem;
}

/* ===================================
   Base Mobile Styles
   =================================== */
@media (max-width: 768px) {
    /* Appropriate padding on mobile */
    body {
        padding: 0;
        margin: 0;
        background: #f5f5f7; /* Light gray background like iOS */
    }

    .container {
        max-width: 100%;
        padding-left: 12px;
        padding-right: 12px;
        margin: 0 auto;
        padding-top: 10px;
    }

    /* Cards with better spacing */
    .card {
        border-radius: 10px;
        margin-bottom: 15px;
        margin-left: 0;
        margin-right: 0;
        border: none;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
        background: white;
    }

    .card-body {
        padding: 12px;
    }

    .card-header {
        padding: 10px 12px;
        font-size: 16px;
        background: white;
        border-bottom: 1px solid #e5e7eb;
        border-radius: 10px 10px 0 0 !important;
    }

    /* Header optimizations */
    .professional-header {
        padding: 15px;
        margin-bottom: 15px;
        background: white;
        border-radius: 10px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
        padding-left: 2rem;
    }

    .professional-header h1 {
        font-size: 1.5rem;
        margin-bottom: 8px;
    }

    /* Invoice header specific */
    .invoice-header,
    .proposal-header {
        padding: 15px !important;
        margin-bottom: 15px;
    }

    .document-type-badge {
        font-size: 12px;
        padding: 4px 8px;
        margin-bottom: 8px;
    }

    /* Company logo adjustments */
    #headerCompanyLogo {
        max-height: 40px !important;
        margin-bottom: 10px;
    }

    /* Status badges */
    .badge {
        font-size: 11px;
        padding: 4px 8px;
    }
}

/* ===================================
   Responsive Tables
   =================================== */
@media (max-width: 768px) {
    /* Convert tables to card layout on mobile */
    .table-responsive {
        border: none;
        overflow-x: visible;
        margin-bottom: 20px;
    }

    .table {
        border: none;
    }

    /* Stack table rows as cards on mobile */
    .table thead {
        display: none;
    }

    .table tbody {
        display: block;
        padding: 0;
        width: 100%;
    }

    .table tbody tr {
        display: block;
        width: calc(100% - 24px); /* Full width minus side padding */
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 12px;
        background: white;
        border: none;
        border-radius: 10px;
        padding: 15px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }

    .table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border: none;
        border-bottom: 1px solid #f0f0f0;
        text-align: right;
    }

    .table tbody td:last-child {
        border-bottom: none;
    }

    /* Add labels before each cell */
    .table tbody td:before {
        content: attr(data-label);
        font-weight: 500;
        text-align: left;
        flex: 1;
        padding-right: 10px;
        color: #6b7280;
        font-size: 14px;
    }

    .table tbody td {
        font-size: 15px;
        color: #111827;
        font-weight: 500;
    }

    /* Special handling for invoice line items */
    #lineItems tbody td:nth-child(1):before { content: "Item"; }
    #lineItems tbody td:nth-child(2):before { content: "Quantity"; }
    #lineItems tbody td:nth-child(3):before { content: "Unit Price"; }
    #lineItems tbody td:nth-child(4):before { content: "Total"; }

    /* Proposal line items */
    #proposalLineItems tbody td:nth-child(1):before { content: "Description"; }
    #proposalLineItems tbody td:nth-child(2):before { content: "Quantity"; }
    #proposalLineItems tbody td:nth-child(3):before { content: "Unit Cost"; }
    #proposalLineItems tbody td:nth-child(4):before { content: "Total"; }

    /* Summary table styling */
    .table.summary-table tbody tr {
        background: #f8f9fa;
        padding: 8px 12px;
    }

    .table.summary-table tbody td {
        padding: 6px 0;
    }

    /* Total row emphasis */
    .table tbody tr.total-row,
    .table tfoot tr {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        font-weight: 600;
    }

    .table tbody tr.total-row td,
    .table tfoot td {
        color: white;
        border-bottom: none;
    }

    .table tbody tr.total-row td:before,
    .table tfoot td:before {
        color: rgba(255, 255, 255, 0.8);
    }
}

/* ===================================
   Form and Button Optimizations
   =================================== */
@media (max-width: 768px) {
    /* Full width buttons on mobile */
    .btn {
        padding: 12px 20px;
        font-size: 16px; /* Prevent zoom on iOS */
        width: 100%;
        margin-bottom: 10px;
    }

    .btn-group {
        display: flex;
        flex-direction: column;
        width: 100%;
    }

    .btn-group .btn {
        border-radius: 8px !important;
        margin-bottom: 8px;
    }

    /* Form inputs */
    .form-control,
    .form-select {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 12px;
    }

    .form-label {
        font-size: 14px;
        margin-bottom: 6px;
    }

    /* Signature pad */
    #signaturePad {
        height: 200px;
        touch-action: none;
    }

    /* Payment forms */
    #unifiedPaymentElement,
    #payment-element {
        padding: 15px;
    }
}

/* ===================================
   Invoice/Proposal Specific
   =================================== */
@media (max-width: 768px) {
    /* Customer info section */
    .customer-info {
        font-size: 14px;
        padding: 15px;
        background: white;
        border-radius: 10px;
        margin-bottom: 15px;
    }

    .customer-info .row {
        margin: 0;
    }

    .customer-info .col-md-6 {
        padding: 10px 0;
        border-bottom: 1px solid #e5e7eb;
    }

    .customer-info .col-md-6:last-child {
        border-bottom: none;
    }

    /* Remove column borders on mobile */
    .col-md-6:first-child {
        border-right: none !important;
        padding-right: 0 !important;
    }

    /* Invoice sections */
    #invoiceDetails,
    #proposalDetails {
        padding: 0;
        background: white;
        border-radius: 10px;
        margin-bottom: 15px;
    }

    /* Invoice/Proposal details */
    .invoice-meta,
    .proposal-meta {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .invoice-meta .badge,
    .proposal-meta .badge {
        display: inline-block;
        width: auto;
    }

    /* Terms and conditions */
    .terms-container {
        max-height: 300px;
        overflow-y: auto;
        font-size: 13px;
        padding: 12px;
        background: #f8f9fa;
        border-radius: 8px;
    }

    /* Notes section */
    .notes-section {
        font-size: 14px;
        padding: 12px;
        background: #fff9e6;
        border-radius: 8px;
        border-left: 3px solid #ffc107;
    }
}

/* ===================================
   Payment Interface Mobile
   =================================== */
@media (max-width: 768px) {
    /* Payment areas */
    #invoicePaymentArea,
    #proposalPaymentArea,
    #paymentMethodChoice {
        padding: 0;
        background: white;
        border-radius: 10px;
        margin-bottom: 15px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }

    /* Remove nested card padding in payment areas */
    #invoicePaymentArea .card,
    #proposalPaymentArea .card,
    #paymentMethodChoice .card,
    #proposalResponse .card {
        box-shadow: none;
        border: none;
        margin-bottom: 0;
    }

    #invoicePaymentArea .card-body,
    #proposalPaymentArea .card-body,
    #paymentMethodChoice .card-body,
    #proposalResponse .card-body {
        padding: 12px;
    }

    /* Payment summary sections */
    .payment-summary {
        padding: 10px !important;
        margin-bottom: 12px !important;
    }

    /* Payment method selection */
    .payment-method-option {
        padding: 12px;
        margin-bottom: 10px;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.3s ease;
        border: 1px solid #e5e7eb;
    }

    .payment-method-option:hover {
        transform: none; /* Disable hover effects on mobile */
    }

    /* Payment summary */
    .payment-summary {
        position: sticky;
        bottom: 0;
        background: white;
        border-top: 2px solid #e5e7eb;
        padding: 15px;
        margin: 0 -12px;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
        z-index: 100;
    }

    .payment-summary .total-amount {
        font-size: 20px;
        font-weight: 600;
        color: #0366d6;
    }

    /* Loading states */
    .skeleton-loader {
        height: 60px;
        border-radius: 8px;
        margin-bottom: 10px;
    }

    /* Alerts and messages */
    .alert {
        font-size: 14px;
        padding: 10px 12px;
        border-radius: 8px;
        margin-bottom: 12px;
        margin-left: 0;
        margin-right: 0;
    }

    /* Fix alert success colors */
    .alert-success {
        background: #d1fae5 !important;
        color: #065f46 !important;
        border: 1px solid #a7f3d0;
    }

    .alert-success h5,
    .alert-success p,
    .alert-success hr {
        color: inherit !important;
    }

    .alert-icon {
        font-size: 18px;
        margin-right: 8px;
    }
}

/* ===================================
   Modal and Overlay Optimizations
   =================================== */
@media (max-width: 768px) {
    /* Full screen modals on mobile */
    .modal-dialog {
        margin: 0;
        max-width: 100%;
        height: 100%;
    }

    .modal-content {
        height: 100%;
        border-radius: 0;
    }

    .modal-header {
        padding: 15px;
        border-radius: 0;
    }

    .modal-body {
        padding: 15px;
        overflow-y: auto;
    }

    .modal-footer {
        padding: 15px;
        position: sticky;
        bottom: 0;
        background: white;
        border-top: 2px solid #e1e4e8;
    }

    .modal-footer .btn {
        flex: 1;
    }
}

/* ===================================
   Navigation and Scrolling
   =================================== */
@media (max-width: 768px) {
    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
    }

    /* Back to top button */
    .back-to-top {
        position: fixed;
        bottom: 20px;
        right: 20px;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        background: #0366d6;
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
        z-index: 1000;
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;
    }

    .back-to-top.show {
        opacity: 1;
        visibility: visible;
    }

    /* Sticky headers */
    .sticky-header {
        position: sticky;
        top: 0;
        background: white;
        z-index: 100;
        padding: 10px 15px;
        border-bottom: 1px solid #e1e4e8;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }
}

/* ===================================
   Dark Mode Mobile Adjustments
   =================================== */
@media (max-width: 768px) {
    [data-theme="dark"] .table tbody tr {
        background: #0d1117;
        border-color: #30363d;
    }

    [data-theme="dark"] .table tbody td {
        border-bottom-color: #21262d;
    }

    [data-theme="dark"] .payment-summary {
        background: #0d1117;
        border-top-color: #30363d;
    }

    [data-theme="dark"] .modal-content {
        background: #0d1117;
    }

    [data-theme="dark"] .modal-footer {
        background: #0d1117;
        border-top-color: #30363d;
    }

    [data-theme="dark"] .sticky-header {
        background: #0d1117;
        border-bottom-color: #30363d;
    }
}

/* ===================================
   Accessibility Improvements
   =================================== */
@media (max-width: 768px) {
    /* Larger touch targets */
    a, button, input, select, textarea {
        min-height: 44px;
        min-width: 44px;
    }

    /* Focus indicators */
    *:focus {
        outline: 3px solid #0366d6;
        outline-offset: 2px;
    }

    /* High contrast mode support */
    @media (prefers-contrast: high) {
        .card {
            border: 2px solid currentColor;
        }

        .btn {
            border: 2px solid currentColor;
        }
    }

    /* Reduced motion support */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ===================================
   iPhone Specific Fixes
   =================================== */
@supports (-webkit-touch-callout: none) {
    /* iOS specific styles */
    .container {
        padding-left: env(safe-area-inset-left, 15px);
        padding-right: env(safe-area-inset-right, 15px);
    }

    /* Fix for iOS form zoom */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    select,
    textarea {
        font-size: 16px !important;
    }

    /* Smooth scrolling on iOS */
    .scrollable-container {
        -webkit-overflow-scrolling: touch;
    }
}

/* ===================================
   Small Phone Adjustments (<380px)
   =================================== */
@media (max-width: 380px) {
    .container {
        padding-left: 16px;
        padding-right: 16px;
    }

    /* Cards on very small screens */
    .table tbody tr {
        width: calc(100% - 32px);
        padding: 16px;
    }

    .card-body {
        padding: 8px;
    }

    .professional-header h1 {
        font-size: 1.25rem;
    }

    .btn {
        font-size: 14px;
        padding: 10px 15px;
    }

    .table tbody td {
        font-size: 13px;
    }

    .table tbody td:before {
        font-size: 12px;
    }
}