/**
 * Custom Forms Styling
 * Fixes for double borders and mobile responsiveness
 */

/* ========================================
   1. FIX: Tom Select Double Border
   ======================================== */

/* IMPORTANT: Main Tom Select styles are in tom-select-styles.blade.php component */
/* These are supplementary styles that don't conflict */

/* Hide original select when Tom Select is initialized */
.ts-wrapper select.form-control,
.ts-wrapper select.form-select,
select.tom-select {
    position: absolute !important;
    opacity: 0 !important;
    pointer-events: none !important;
    border: none !important;
    height: 0 !important;
    width: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
    display: none !important;
}

/* Ensure ts-wrapper has no border to avoid double border */
.ts-wrapper {
    border: none !important;
    box-shadow: none !important;
    background: transparent !important;
}

/* Tom Select Input */
.ts-control input {
    border: none !important;
    box-shadow: none !important;
    outline: none !important;
}

/* Tom Select Item */
.ts-control .item {
    border: none !important;
    background: transparent !important;
}

/* ========================================
   2. Form Controls Consistency
   ======================================== */

.form-control,
.form-select {
    border-radius: 0.5rem;
    border: 1px solid #e2e8f0;
    padding: 0.625rem 1rem;
    font-size: 0.875rem;
    transition: all 0.15s ease;
    background-color: #fff;
}

.form-control:focus,
.form-select:focus {
    box-shadow: 0 0 0 3px rgba(94, 114, 228, 0.1);
    border-color: #5e72e4;
    outline: none;
}

/* Input with Icons */
.input-with-icon {
    position: relative;
}

.input-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: #a0aec0;
    z-index: 10;
    pointer-events: none;
}

.input-with-icon input,
.input-with-icon .ts-control,
.input-with-icon textarea {
    padding-left: 2.5rem !important;
}

/* ========================================
   3. MOBILE RESPONSIVENESS
   ======================================== */

/* Mobile: Smaller Screens (< 768px) */
@media (max-width: 767px) {
    /* Form Sections */
    .form-section {
        padding: 1rem;
        margin-bottom: 1rem;
        border-radius: 0.5rem;
    }

    .form-section-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    .form-section-icon {
        width: 40px;
        height: 40px;
        font-size: 1.25rem;
    }

    .form-section-title {
        font-size: 1.125rem;
    }

    /* Form Controls */
    .form-control,
    .form-select,
    .ts-control {
        font-size: 1rem !important; /* Larger for mobile readability */
        height: auto !important;
        min-height: 44px !important; /* iOS minimum touch target */
        padding: 0.75rem 1rem !important;
    }

    /* Labels */
    .form-label {
        font-size: 0.875rem;
        margin-bottom: 0.375rem;
    }

    /* Buttons */
    .btn {
        min-height: 44px;
        padding: 0.75rem 1.5rem;
        font-size: 1rem;
    }

    /* Input with Icons - Adjust for larger inputs */
    .input-with-icon input,
    .input-with-icon .ts-control,
    .input-with-icon textarea {
        padding-left: 2.75rem !important;
    }

    .input-icon {
        left: 14px;
        font-size: 1.125rem;
    }

    /* Dynamic Items */
    .dynamic-items-container {
        padding: 1rem;
    }

    .dynamic-item {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
    }

    /* Tom Select Dropdown - Full width on mobile */
    .ts-dropdown {
        max-width: 100vw !important;
        left: 0 !important;
        right: 0 !important;
    }

    /* Reduce margins and paddings */
    .row {
        margin-left: -0.5rem;
        margin-right: -0.5rem;
    }

    .col, [class*="col-"] {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
}

/* Tablet: Medium Screens (768px - 991px) */
@media (min-width: 768px) and (max-width: 991px) {
    .form-section {
        padding: 1.5rem;
    }

    .form-control,
    .form-select,
    .ts-control {
        font-size: 0.9375rem !important;
    }
}

/* ========================================
   4. TOUCH-FRIENDLY IMPROVEMENTS
   ======================================== */

/* Increase touch target sizes for mobile */
@media (max-width: 767px) {
    /* Buttons */
    .btn-sm {
        min-height: 40px;
        padding: 0.5rem 1rem;
    }

    .btn-lg {
        min-height: 52px;
        padding: 1rem 2rem;
    }

    /* Checkboxes and Radio Buttons */
    .custom-control-label::before,
    .custom-control-label::after {
        width: 1.5rem;
        height: 1.5rem;
    }

    .custom-control-label {
        padding-left: 2rem;
        min-height: 44px;
        display: flex;
        align-items: center;
    }

    /* Select2/Tom Select touch improvements */
    .ts-dropdown .option {
        min-height: 44px;
        padding: 0.75rem 1rem;
        display: flex;
        align-items: center;
    }
}

/* ========================================
   5. ACCESSIBILITY
   ======================================== */

/* Focus visible for keyboard navigation */
.form-control:focus-visible,
.form-select:focus-visible,
.ts-control:focus-visible,
.btn:focus-visible {
    outline: 2px solid #5e72e4;
    outline-offset: 2px;
}

/* Error states */
.is-invalid,
.form-control.is-invalid,
.ts-wrapper.is-invalid .ts-control {
    border-color: #fc8181 !important;
}

.is-invalid:focus,
.form-control.is-invalid:focus,
.ts-wrapper.is-invalid .ts-control:focus-within {
    box-shadow: 0 0 0 3px rgba(252, 129, 129, 0.1) !important;
    border-color: #fc8181 !important;
}

/* Success states */
.is-valid,
.form-control.is-valid,
.ts-wrapper.is-valid .ts-control {
    border-color: #48bb78 !important;
}

/* ========================================
   6. LOADING STATES
   ======================================== */

.ts-control.loading {
    opacity: 0.7;
    pointer-events: none;
}

.ts-control.loading::after {
    content: "";
    position: absolute;
    right: 12px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    border: 2px solid #e2e8f0;
    border-top-color: #5e72e4;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: translateY(-50%) rotate(360deg); }
}

/* ========================================
   7. RESPONSIVE CONTAINER
   ======================================== */

@media (max-width: 575px) {
    .container,
    .container-fluid {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Card padding adjustment */
    .card-body {
        padding: 1rem;
    }

    .card-header {
        padding: 0.75rem 1rem;
    }
}

/* ========================================
   8. ACTION BUTTONS - MOBILE HORIZONTAL LAYOUT
   ======================================== */

/* Force horizontal layout for action buttons on mobile */
@media (max-width: 767px) {
    /* DataTables responsive mode - keep buttons horizontal */
    table.dataTable tbody td .btn-group,
    .btn-group {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        gap: 0.25rem;
        justify-content: flex-start;
    }

    /* Compact button sizing for mobile */
    .btn-group .btn-sm {
        padding: 0.5rem 0.75rem !important;
        font-size: 0.875rem !important;
        min-width: 44px !important;
        min-height: 44px !important;
        border-radius: 0.375rem !important;
    }

    /* Icon-only buttons - square shape */
    .btn-group .btn-sm i {
        margin: 0;
        font-size: 1rem;
    }

    /* Action buttons specific colors - maintain visibility */
    .btn-group .btn-info {
        background-color: #17a2b8 !important;
        border-color: #17a2b8 !important;
    }

    .btn-group .btn-warning {
        background-color: #ffc107 !important;
        border-color: #ffc107 !important;
        color: #000 !important;
    }

    .btn-group .btn-danger {
        background-color: #dc3545 !important;
        border-color: #dc3545 !important;
    }

    /* Ensure forms inside button groups don't break layout */
    .btn-group form {
        display: inline-flex !important;
        margin: 0 !important;
    }

    /* DataTables child row - keep buttons horizontal there too */
    table.dataTable tbody tr.child ul.dtr-details li .btn-group {
        display: flex !important;
        flex-direction: row !important;
    }

    /* Remove rounded corners for grouped buttons on mobile */
    .btn-group .btn:not(:first-child):not(:last-child) {
        border-radius: 0.375rem !important;
    }

    .btn-group .btn:first-child {
        border-radius: 0.375rem !important;
    }

    .btn-group .btn:last-child {
        border-radius: 0.375rem !important;
    }
}

/* ========================================
   9. DARK MODE SUPPORT (Optional)
   ======================================== */

@media (prefers-color-scheme: dark) {
    .ts-control,
    .form-control,
    .form-select {
        background-color: #2d3748;
        border-color: #4a5568;
        color: #f7fafc;
    }

    .ts-dropdown {
        background-color: #2d3748;
        border-color: #4a5568;
    }

    .ts-dropdown .option {
        color: #f7fafc;
    }

    .ts-dropdown .option:hover {
        background-color: #4a5568;
    }
}
