/* ============================================
   Financial Table Extraction System
   Financial Regulatory Authority
   ============================================ */

@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap');

/* ========== RESET ========== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Cairo', sans-serif;
    background: linear-gradient(135deg, #1e3c72, #2a5298);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    direction: rtl;
    padding: 20px;
}

/* ========== CONTAINER ========== */
.container {
    width: 100%;
    max-width: 1400px;
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ========== HEADER ========== */
.header {
    background: #1a2f4f;
    color: white;
    padding: 25px;
    text-align: center;
}

.header h1 {
    font-size: 26px;
    font-weight: 700;
    margin-bottom: 5px;
}

.header p {
    font-size: 14px;
    color: #a8b8d8;
}

/* ========== MAIN GRID - FIXED EQUAL COLUMNS ========== */
.main-content {
    display: grid;
    /* Use minmax(0, 1fr) to prevent content from expanding columns */
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 20px;
    padding: 30px;
    background: #f5f7fa;
    align-items: stretch;
    min-height: 400px; /* Minimum height for better UX */
    height: auto;      /* Flexible height - grows with content */
}

/* ========== SECTIONS - FIXED SIZE WITH OVERFLOW CONTROL ========== */
.upload-section,
.results-section {
    background: #ffffff;
    border-radius: 16px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    
    /* CRITICAL: These 3 properties enforce fixed size */
    height: 100%;
    min-width: 0;      /* Allow grid item to shrink below content size */
    min-height: 0;     /* Allow flex children to shrink */
    overflow: hidden;  /* Clip any overflowing content */
}

.section-title {
    font-size: 18px;
    font-weight: 700;
    color: #1a2f4f;
    border-bottom: 2px solid #e8edf3;
    padding-bottom: 10px;
    margin-bottom: 15px;
    flex-shrink: 0; /* Don't shrink */
}

/* ========== UPLOAD PANEL ========== */
.upload-section {
    /* No additional constraints needed */
}

.dropzone {
    border: 3px dashed #c5d1e0;
    border-radius: 12px;
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    background: #fafbfc;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex: 1;           /* Take available space when visible */
    min-height: 0;     /* Allow shrinking */
    min-width: 0;      /* Allow shrinking */
}

.dropzone:hover {
    border-color: #4a6fa5;
    background: #f0f4f8;
    transform: translateY(-2px);
}

.dropzone.dragover {
    border-color: #2a5298;
    background: #e8f0fe;
}

.dropzone-icon {
    width: 60px;
    height: 60px;
    color: #4a6fa5;
}

.dropzone-text {
    font-size: 16px;
    font-weight: 600;
    color: #1a2f4f;
}

.dropzone-hint {
    font-size: 13px;
    color: #6b7280;
}

#fileInput {
    display: none;
}

/* ========== PREVIEW CONTAINER - SCROLLABLE ========== */
.preview-container {
    flex: 1;           /* Take remaining space */
    min-height: 0;     /* CRITICAL: Allow flex to shrink */
    min-width: 0;      /* CRITICAL: Allow content to be clipped */
    margin-top: 15px;
    border-radius: 12px;
    overflow: auto;    /* Both horizontal and vertical scroll */
    background: #fafbfc;
    border: 2px solid #e8edf3;
    position: relative;
}

.preview-image {
    max-width: 100%;   /* Don't exceed container width */
    height: auto;
    object-fit: contain;
    display: block;
}

/* ========== SCANNING ANIMATION ========== */
.scan-line {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, transparent, #2a5298, transparent);
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.scan-line.active {
    opacity: 1;
    animation: scan 2s linear infinite;
}

.scan-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(42, 82, 152, 0.1);
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}

.scan-overlay.active {
    opacity: 1;
}

@keyframes scan {
    0% { transform: translateY(0); }
    100% { transform: translateY(480px); }
}

/* ========== RESULTS PANEL ========== */
/* Inherits from .upload-section, .results-section above */

.results-content {
    flex: 1;           /* Take remaining space */
    min-height: 0;     /* CRITICAL: Allow flex to shrink */
    min-width: 0;      /* CRITICAL: Allow content to be clipped */
    overflow: hidden;  /* Contain the table wrapper */
    display: flex;
    flex-direction: column;
}

/* ========== TABLE SCROLL - FILLS AVAILABLE SPACE ========== */
.table-wrapper {
    flex: 1;
    min-height: 0;     /* CRITICAL: Allow flex to shrink */
    min-width: 0;      /* CRITICAL: Allow content to be clipped */
    width: 100%;
    overflow: auto;    /* Enable both scrollbars when needed */
    border: 1px solid #e2e8f0;
    background: #ffffff;
    border-radius: 8px;
}

/* ========== TABLE ========== */
.editable-table {
    border-collapse: collapse;
    width: max-content; /* Allow horizontal scroll */
    min-width: 100%;
    font-size: 14px;
}

.editable-table th,
.editable-table td {
    border: 1px solid #cbd5e1;
    padding: 10px 14px;
    white-space: nowrap;
    min-width: 130px;
    text-align: right;
    transition: all 0.2s ease;
}

.editable-table th {
    background: #f8fafc;
    font-weight: 700;
    color: #1a2f4f;
    position: sticky;
    top: 0;
    z-index: 3;
}

/* Sticky first column */
.editable-table th:first-child,
.editable-table td:first-child {
    position: sticky;
    right: 0;
    background: #f1f5f9;
    z-index: 2;
    font-weight: 700;
    box-shadow: 2px 0 5px rgba(0,0,0,0.05);
}

.editable-table th:first-child {
    z-index: 4;
    background: #e2e8f0;
}

/* Editable effect */
.editable-table td:focus {
    outline: 2px solid #2563eb;
    background: #eff6ff;
}

.editable-table td:hover {
    background: #f8fafc;
}

/* ========== TABLE FOOTER ========== */
.table-footer {
    margin-top: 15px;
    display: flex;
    justify-content: center;
    padding-top: 15px;
    border-top: 2px solid #e8edf3;
    flex-shrink: 0; /* Don't shrink */
}

/* ========== BUTTONS ========== */
.button-section {
    display: flex;
    gap: 15px;
    padding: 20px;
    background: #f5f7fa;
}

.btn {
    flex: 1;
    padding: 14px 20px;
    font-size: 16px;
    font-weight: 700;
    border-radius: 12px;
    border: none;
    cursor: pointer;
    font-family: 'Cairo', sans-serif;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-detect {
    background: #2a5298;
    color: white;
}

.btn-detect:hover:not(:disabled) {
    background: #1e3c72;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(42, 82, 152, 0.3);
}

.btn-clear {
    background: #e8edf3;
    color: #1a2f4f;
}

.btn-clear:hover {
    background: #d1dbe5;
    transform: translateY(-2px);
}

.btn-download {
    background: #10b981;
    color: white;
    padding: 12px 24px;
    font-size: 15px;
}

.btn-download:hover {
    background: #059669;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

/* ========== LOADING SPINNER ========== */
.loading {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 3px solid rgba(255,255,255,0.3);
    border-radius: 50%;
    border-top-color: white;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ========== LOADING STATE ========== */
.loading-state {
    flex: 1;
    min-height: 0; /* Allow shrinking */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 15px;
    color: #6b7280;
    padding: 20px;
}

.spinner {
    border: 4px solid #f3f4f6;
    border-top: 4px solid #2a5298;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    animation: spin 1s linear infinite;
}

.loading-hint {
    font-size: 13px;
    color: #9ca3af;
}

/* ========== EMPTY STATE ========== */
.empty-state {
    flex: 1;
    min-height: 0; /* Allow shrinking */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 15px;
    color: #9ca3af;
    font-size: 15px;
    padding: 20px;
    text-align: center;
}

.empty-icon {
    width: 80px;
    height: 80px;
    opacity: 0.5;
}

/* ========== ERROR NOTIFICATION ========== */
.error-notification {
    position: fixed;
    top: 20px;
    left: 20px;
    background: #fee2e2;
    color: #991b1b;
    padding: 16px 20px;
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    z-index: 1000;
    max-width: 400px;
    animation: slideInRTL 0.3s ease;
    font-weight: 600;
    text-align: right;
    direction: rtl;
    font-family: 'Cairo', sans-serif;
    display: flex;
    align-items: center;
    gap: 12px;
    border-right: 4px solid #dc2626;
}

.error-icon {
    font-size: 24px;
    flex-shrink: 0;
}

.error-message {
    flex: 1;
}

@keyframes slideInRTL {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOutRTL {
    from {
        transform: translateX(0);
        opacity: 1;
    }
    to {
        transform: translateX(100%);
        opacity: 0;
    }
}

/* ========== CUSTOM SCROLLBAR ========== */
.preview-container::-webkit-scrollbar,
.table-wrapper::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}

.preview-container::-webkit-scrollbar-track,
.table-wrapper::-webkit-scrollbar-track {
    background: #f1f5f9;
    border-radius: 10px;
}

.preview-container::-webkit-scrollbar-thumb,
.table-wrapper::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 10px;
}

.preview-container::-webkit-scrollbar-thumb:hover,
.table-wrapper::-webkit-scrollbar-thumb:hover {
    background: #94a3b8;
}

/* ========== RESPONSIVE ========== */
@media (max-width: 1200px) {
    .main-content {
        grid-template-columns: 1fr; /* Stack on smaller screens */
        height: auto; /* Allow auto height when stacked */
    }

    .upload-section,
    .results-section {
        height: 450px; /* Fixed height for each when stacked */
    }
}

@media (max-width: 600px) {
    .header h1 {
        font-size: 20px;
    }

    .header p {
        font-size: 12px;
    }

    .button-section {
        flex-direction: column;
    }

    .editable-table th,
    .editable-table td {
        min-width: 100px;
        padding: 8px 10px;
        font-size: 13px;
    }
    
    .upload-section,
    .results-section {
        height: 400px;
    }
}