/* ==============================================
   PERFORMANCE OPTIMIZED CSS
   ============================================== */

/* Critical CSS - Above the fold styles */
:root {
    --transition-speed: 0.3s;
    --primary-color: #667eea;
    --secondary-color: #764ba2;
    --hover-shadow: 0 4px 16px rgba(102, 126, 234, 0.5);
    --default-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

/* ==============================================
   OPTIMIZED LOADING STATES
   ============================================== */

/* Initial state - hide content until translations are loaded */
html.translation-loading body {
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease-out, visibility 0.3s ease-out;
}

body.translation-loading {
    opacity: 0;
    visibility: hidden;
}

/* Show content after translations are loaded */
body.translation-ready {
    opacity: 1;
    visibility: visible;
}

/* Loading indicator during translation load */
body.translation-loading::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.95);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Loading spinner */
body.translation-loading::after {
    content: '';
    position: fixed;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 10000;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Prevent layout shifts during loading */
html.translation-loading {
    overflow: hidden;
}

/* Fallback: Show content after 3 seconds even if JavaScript fails */
body {
    animation: showContentFallback 0.3s ease-out 3s forwards;
}

@keyframes showContentFallback {
    to {
        opacity: 1;
        visibility: visible;
    }
}

/* Override fallback when translations are ready */
body.translation-ready {
    animation: none;
}

/* GPU-accelerated animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

/* General transitions for all interactive elements */
button, .btn, .form-control, .form-select, .badge, .card, .alert, a {
    transition: all var(--transition-speed) cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform, opacity;
}

/* Enhanced button hover states */
.btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--hover-shadow);
}

.btn:active {
    transform: translateY(0);
    transition-duration: 0.1s;
}

/* Form control focus states */
.form-control:focus, .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
}

/* Table row hover effects */
.table tbody tr {
    transition: background-color var(--transition-speed) ease;
}

.table tbody tr:hover {
    background-color: rgba(102, 126, 234, 0.05);
}

/* Badge animations */
.badge {
    animation: fadeIn 0.3s ease-out;
}

/* Alert entrance animation */
.alert {
    animation: fadeIn 0.5s ease-out;
}

/* Modal animations and fixes */
.modal.fade .modal-dialog {
    transition: transform var(--transition-speed) ease-out;
}

/* Fix modal backdrop issues */
.modal-backdrop {
    z-index: 1040;
    background-color: rgba(0, 0, 0, 0.5);
}

.modal {
    z-index: 1050;
}

.modal-dialog {
    z-index: 1060;
    margin: 1.75rem auto;
    max-width: 500px;
    position: relative;
    width: auto;
    pointer-events: auto;
}

.modal-content {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 0.375rem;
    outline: 0;
    pointer-events: auto;
}

/* Ensure modal is clickable */
.modal.show {
    display: block;
}

.modal.show .modal-dialog {
    transform: none;
}

/* Fix modal button interactions */
.modal-footer .btn {
    pointer-events: auto;
    position: relative;
    z-index: 1;
}

.modal-header .btn-close {
    pointer-events: auto;
    position: relative;
    z-index: 1;
}

/* Ensure all modal form elements are interactive */
.modal input,
.modal textarea,
.modal select,
.modal button,
.modal .btn,
.modal .form-control,
.modal .form-select,
.modal .form-check-input {
    pointer-events: auto !important;
    cursor: auto !important;
}

/* Fix for modal backdrop */
.modal-backdrop {
    pointer-events: auto;
}

/* Ensure modal body is scrollable and interactive */
.modal-body {
    pointer-events: auto;
    overflow-y: auto;
    max-height: calc(100vh - 200px);
}

/* Card hover effects */
.card {
    transition: box-shadow var(--transition-speed) ease;
}

.card:hover {
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
}

/* ==============================================
   RTL SUPPORT (Optimized)
   ============================================== */

[dir="rtl"] {
    direction: rtl;
    text-align: right;
}

[dir="rtl"] .navbar-nav,
[dir="rtl"] .btn-group,
[dir="rtl"] .pagination {
    flex-direction: row-reverse;
}

[dir="rtl"] .form-label,
[dir="rtl"] th,
[dir="rtl"] td,
[dir="rtl"] .modal-header,
[dir="rtl"] .dropdown-menu {
    text-align: right;
}

[dir="rtl"] .form-check {
    padding: 0 1.5em 0 0;
}

[dir="rtl"] .form-check-input {
    float: right;
    margin: 0 -1.5em 0 0;
}

[dir="rtl"] .alert-dismissible .btn-close {
    right: auto;
    left: 0;
}

/* RTL margin utilities */
[dir="rtl"] .ms-auto { margin-left: 0 !important; margin-right: auto !important; }
[dir="rtl"] .me-auto { margin-right: 0 !important; margin-left: auto !important; }

/* ==============================================
   LANGUAGE SWITCHER
   ============================================== */

#language-switcher {
    padding: 0.6rem 1.2rem;
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 0.5rem;
    background: transparent;
    color: rgba(255, 255, 255, 0.9);
    font-size: 0.875rem;
    font-weight: 500;
    min-width: 110px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all var(--transition-speed) ease;
    position: relative;
    overflow: hidden;
}

#language-switcher::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: left 0.5s;
}

#language-switcher:hover {
    background-color: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.8);
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

#language-switcher:hover::before {
    left: 100%;
}

/* ==============================================
   TRANSLATION LOADING STATES
   ============================================== */

.translation-loading {
    opacity: 0;
    visibility: hidden;
}

.translation-ready {
    opacity: 1;
    visibility: visible;
    transition: opacity 0.3s ease-out;
}

/* ==============================================
   AI BUTTON STYLES
   ============================================== */

.ai-generate-btn {
    background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    border: none;
    color: white;
    font-weight: 600;
    box-shadow: var(--default-shadow);
    position: relative;
    overflow: hidden;
}

.ai-generate-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
    transition: left 0.6s;
}

.ai-generate-btn:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: var(--hover-shadow);
}

.ai-generate-btn:hover::before {
    left: 100%;
}

.ai-generate-btn.loading {
    animation: pulse 2s infinite;
}

/* ==============================================
   DATASET TABLE ENHANCEMENTS
   ============================================== */

#datasetTable {
    table-layout: fixed;
}

#datasetTable td {
    vertical-align: middle;
}

/* Description column optimization */
#datasetTable td:nth-child(3) {
    max-width: 300px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    position: relative;
    transition: background-color var(--transition-speed) ease;
}

/* Active columns optimization */
#datasetTable td:nth-child(7) {
    max-width: 250px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

/* Hover states for truncated content */
#datasetTable td.has-overflow {
    cursor: pointer;
    position: relative;
}

#datasetTable td.has-overflow::after {
    content: '\f05a';
    font-family: 'Font Awesome 5 Free';
    font-weight: 900;
    position: absolute;
    right: 5px;
    top: 50%;
    transform: translateY(-50%);
    color: #6c757d;
    font-size: 14px;
    opacity: 0;
    transition: opacity var(--transition-speed) ease;
}

#datasetTable td.has-overflow:hover::after {
    opacity: 1;
}

#datasetTable td.has-overflow:hover {
    background-color: #e3f2fd;
}

/* ==============================================
   TOOLTIPS
   ============================================== */

.dataset-tooltip {
    position: fixed;
    z-index: 9999;
    background: rgba(44, 62, 80, 0.95);
    color: white;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 14px;
    max-width: 400px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    pointer-events: none;
    opacity: 0;
    transform: translateY(-10px);
    transition: all var(--transition-speed) ease;
    backdrop-filter: blur(10px);
}

.dataset-tooltip.show {
    opacity: 1;
    transform: translateY(0);
}

/* Tooltip arrow */
.dataset-tooltip::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 50%;
    transform: translateX(-50%);
    border: 8px solid transparent;
    border-top-color: rgba(44, 62, 80, 0.95);
}

/* ==============================================
   LOADING STATES
   ============================================== */

.spinner-border {
    width: 2rem;
    height: 2rem;
    border-width: 0.2em;
}

/* Progress bar animations */
.progress-bar {
    transition: width 0.6s ease;
}

/* ==============================================
   RESPONSIVE OPTIMIZATIONS
   ============================================== */

@media (max-width: 768px) {
    /* Mobile table adjustments */
    #datasetTable {
        font-size: 0.875rem;
    }
    
    #datasetTable td:nth-child(3),
    #datasetTable td:nth-child(7) {
        max-width: 150px;
    }
    
    .dataset-tooltip {
        max-width: 90vw;
        font-size: 12px;
    }
    
    /* Mobile button adjustments */
    .btn {
        padding: 0.5rem 1rem;
        font-size: 0.875rem;
    }
}

/* ==============================================
   PERFORMANCE OPTIMIZATIONS
   ============================================== */

/* Use GPU acceleration for animations */
.modal.fade .modal-dialog,
.btn,
.card {
    transform: translateZ(0);
    backface-visibility: hidden;
}

/* Optimize repaints */
.table tbody tr:hover td {
    will-change: background-color;
}

/* Reduce layout thrashing */
.badge,
.btn-sm {
    contain: layout style;
}

/* ==============================================
   CHECKBOX IMPROVEMENTS
   ============================================== */

/* Enhanced checkbox styling */
.form-check-input {
    width: 1.25em;
    height: 1.25em;
    margin-top: 0.125em;
    vertical-align: middle;
    background-color: #fff;
    border: 2px solid #dee2e6;
    border-radius: 0.25em;
    transition: all var(--transition-speed) ease;
    position: relative;
}

.form-check-input:checked {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10l3 3l6-6'/%3e%3c/svg%3e");
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
}

.form-check-input:checked::after {
    display: none;
}

.form-check-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
}

.form-check-input:hover {
    border-color: var(--primary-color);
}

/* Filter checkbox active state */
.filter-active {
    background-color: rgba(102, 126, 234, 0.1);
    border-radius: 0.375rem;
    padding: 0.25rem 0.5rem;
    margin: -0.25rem -0.5rem;
    transition: all var(--transition-speed) ease;
}

.filter-active .form-check-input {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    transform: scale(1.05);
}

.filter-active .form-check-label {
    color: var(--primary-color);
    font-weight: 600;
}

/* Enhanced checkbox states */
.form-check-input:indeterminate {
    background-color: var(--primary-color);
    border-color: var(--primary-color);
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10l14 0'/%3e%3c/svg%3e");
}

/* ==============================================
   ENHANCED EDIT MODAL STYLES
   ============================================== */

/* Modal improvements for multilingual support */
.modal-lg {
    max-width: 900px;
}

/* Enhanced form controls */
.modal-body .form-control,
.modal-body .form-select {
    font-size: 0.95rem;
    line-height: 1.5;
    padding: 0.5rem 0.75rem;
    border-radius: 0.375rem;
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.modal-body .form-control:focus,
.modal-body .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
}

/* Enhanced textarea for better text input */
.modal-body textarea.form-control {
    resize: vertical;
    min-height: 80px;
    font-family: inherit;
    line-height: 1.6;
}

/* Better form labels */
.modal-body .form-label {
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: #495057;
}

/* Enhanced switches */
.modal-body .form-switch {
    padding-left: 2.5rem;
}

.modal-body .form-switch .form-check-input {
    width: 2rem;
    height: 1rem;
    margin-left: -2.5rem;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='rgba%2833, 37, 41, 0.75%29'/%3e%3c/svg%3e");
    background-position: left center;
    border-radius: 2rem;
    transition: background-position 0.15s ease-in-out;
}

.modal-body .form-switch .form-check-input:checked {
    background-position: right center;
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e");
}

/* Enhanced table in modal */
.modal-body .table-responsive {
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
    background-color: #fff;
}

.modal-body .table-sm {
    font-size: 0.875rem;
}

.modal-body .table-sm thead th {
    background-color: #f8f9fa;
    border-bottom: 2px solid #dee2e6;
    font-weight: 600;
    color: #495057;
    vertical-align: middle;
    padding: 0.75rem 0.5rem;
}

.modal-body .table-sm tbody td {
    vertical-align: middle;
    padding: 0.5rem;
    border-bottom: 1px solid #dee2e6;
}

.modal-body .table-sm tbody tr:hover {
    background-color: #f8f9fa;
}

/* Fix column checkboxes specifically */
.modal-body .table-sm tbody td:first-child {
    text-align: center;
    width: 50px;
    padding: 0.5rem 0.25rem;
}

.modal-body .table-sm .col-checkbox {
    margin: 0 auto;
    display: block;
    position: relative;
    transform: none;
}

/* Enhanced column inputs */
.modal-body .table-sm .form-control-sm {
    font-size: 0.875rem;
    padding: 0.25rem 0.5rem;
    border-radius: 0.2rem;
}

.modal-body .table-sm .form-control-sm:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 0.1rem rgba(102, 126, 234, 0.25);
}

/* ==============================================
   MODAL HEADER IMPROVEMENTS
   ============================================== */

/* Fix close button position for all modals */
.modal-header {
    display: flex;
    flex-direction: row-reverse;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1rem;
    border-bottom: 1px solid #dee2e6;
}

.modal-header .btn-close {
    order: -1;
    margin: 0;
    padding: 0.5rem;
    box-sizing: content-box;
    width: 1em;
    height: 1em;
    background: transparent url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23000'%3e%3cpath d='m.235 1.227 1.149-1.149a.5.5 0 0 1 .708 0L8 6.086l5.908-6.008a.5.5 0 0 1 .708 0l1.149 1.149a.5.5 0 0 1 0 .708L9.757 8l6.008 5.908a.5.5 0 0 1 0 .708l-1.149 1.149a.5.5 0 0 1-.708 0L8 9.757l-5.908 6.008a.5.5 0 0 1-.708 0L.235 14.616a.5.5 0 0 1 0-.708L6.243 8 .235 2.092a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e") center/1em auto no-repeat;
    border: 0;
    border-radius: 0.375rem;
    opacity: 0.5;
}

.modal-header .btn-close:hover {
    opacity: 0.75;
}

.modal-header .btn-close:focus {
    outline: 0;
    box-shadow: 0 0 0 0.25rem rgba(102, 126, 234, 0.25);
    opacity: 1;
}

.modal-header .modal-title {
    margin: 0;
    line-height: 1.5;
    flex-grow: 1;
    text-align: left;
}

/* ==============================================
   RTL SUPPORT FOR MODALS
   ============================================== */

[dir="rtl"] .modal-header {
    text-align: right;
}

[dir="rtl"] .modal-header .modal-title {
    text-align: right;
}

[dir="rtl"] .modal-footer {
    justify-content: flex-start;
}

[dir="rtl"] .modal-body .form-label {
    text-align: right;
}

[dir="rtl"] .modal-body .form-switch {
    padding-left: 0;
    padding-right: 2.5rem;
}

[dir="rtl"] .modal-body .form-switch .form-check-input {
    margin-left: 0;
    margin-right: -2.5rem;
}

[dir="rtl"] .modal-body .table-sm th,
[dir="rtl"] .modal-body .table-sm td {
    text-align: right;
}

[dir="rtl"] .modal-body .table-sm th:first-child,
[dir="rtl"] .modal-body .table-sm td:first-child {
    text-align: center;
}

/* Fix checkbox positioning for RTL (Arabic) */
[dir="rtl"] .modal-body .table-sm .form-check-input {
    margin-left: 0;
    margin-right: 0;
    position: relative;
    float: none;
}

[dir="rtl"] .modal-body .form-check {
    text-align: right;
    padding-right: 1.5rem;
    padding-left: 0;
}

[dir="rtl"] .modal-body .form-check .form-check-input {
    margin-left: 0.5rem;
    margin-right: -1.5rem;
    float: right;
}

/* RTL specific fixes for table column checkboxes */
[dir="rtl"] .modal-body .table-sm .col-checkbox {
    margin: 0 auto;
    display: block;
    position: relative;
    float: none;
    transform: none;
    left: 0;
    right: 0;
}

[dir="rtl"] .modal-body .table-sm tbody td:first-child {
    text-align: center;
    padding: 0.5rem 0.25rem;
    direction: ltr; /* Keep checkbox direction consistent */
}

/* Fix select all checkbox for RTL */
[dir="rtl"] .modal-body .form-check {
    display: flex;
    align-items: center;
    justify-content: flex-end;
}

[dir="rtl"] .modal-body .form-check .form-check-input {
    margin-left: 0.5rem;
    margin-right: 0;
    order: 2;
}

[dir="rtl"] .modal-body .form-check .form-check-label {
    order: 1;
}

/* ==============================================
   RESPONSIVE MODAL ENHANCEMENTS
   ============================================== */

/* Mobile-specific modal improvements */
@media (max-width: 768px) {
    .modal-lg {
        max-width: 95vw;
        margin: 0.5rem auto;
    }
    
    .modal-body {
        padding: 1rem;
    }
    
    .modal-body .form-control,
    .modal-body .form-select {
        font-size: 16px; /* Prevents zoom on iOS */
        padding: 0.75rem;
    }
    
    .modal-body textarea.form-control {
        min-height: 100px;
    }
    
    .modal-body .table-responsive {
        max-height: 300px;
        font-size: 0.8rem;
    }
    
    .modal-body .table-sm thead th {
        padding: 0.5rem 0.25rem;
        font-size: 0.8rem;
    }
    
    .modal-body .table-sm tbody td {
        padding: 0.4rem 0.25rem;
    }
    
    .modal-body .table-sm .form-control-sm {
        font-size: 14px;
        padding: 0.375rem 0.5rem;
    }
    
    .modal-footer {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        width: 100%;
        margin: 0;
    }
    
    /* Enhanced mobile close button */
    .modal-header .btn-close {
        width: 1.5em;
        height: 1.5em;
        padding: 0.75rem;
        background-size: 1.2em;
    }
    
    .modal-header {
        padding: 0.75rem 1rem;
    }
    
    .modal-header .modal-title {
        font-size: 1.1rem;
    }
    
    /* Mobile RTL checkbox fixes */
    [dir="rtl"] .modal-body .table-sm .col-checkbox {
        margin: 0 auto;
        display: block;
        position: relative;
        float: none;
        transform: none;
        left: 0;
        right: 0;
    }
    
    [dir="rtl"] .modal-body .table-sm tbody td:first-child {
        text-align: center;
        direction: ltr;
        width: 60px;
    }
    
    [dir="rtl"] .modal-body .form-check {
        justify-content: center;
        padding-right: 1rem;
    }
}

/* Tablet-specific improvements */
@media (min-width: 769px) and (max-width: 1024px) {
    .modal-lg {
        max-width: 90vw;
    }
    
    .modal-body .table-responsive {
        max-height: 350px;
    }
}

/* ==============================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================== */

/* Focus indicators */
a:focus,
button:focus,
.btn:focus,
.form-control:focus,
.form-select:focus {
    outline: 3px solid rgba(102, 126, 234, 0.5);
    outline-offset: 2px;
}

/* Skip navigation link */
.skip-nav {
    position: absolute;
    top: -40px;
    left: 0;
    background: #000;
    color: #fff;
    padding: 8px;
    text-decoration: none;
    border-radius: 0 0 4px 0;
}

.skip-nav:focus {
    top: 0;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .btn {
        border: 2px solid currentColor;
    }
    
    .form-control,
    .form-select {
        border-width: 2px;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
