* {
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    margin: 40px auto;
    padding: 20px;
    background-color: #f0f2f5;
    color: #333;
    line-height: 1.6;
}

.container {
    background-color: white;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    max-width: 1200px;
    margin: 0 auto;
    position: relative;
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 40px;
}

.header-row h1 {
    margin: 0;
    text-align: left;
}

.header-actions {
    display: flex;
    gap: 12px;
}

.btn-history {
    display: flex;
    align-items: center;
    gap: 6px;
    background-color: #f1f5f9;
    border: 1px solid #e2e8f0;
    color: #475569;
    padding: 10px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    transition: all 0.2s;
}

.btn-history:hover {
    background-color: #e2e8f0;
    border-color: #cbd5e1;
}

.history-icon {
    font-size: 16px;
}

.history-badge {
    background-color: #ef4444;
    color: white;
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 10px;
    min-width: 18px;
    text-align: center;
}

.form-section {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 30px;
}

.form-group {
    margin-bottom: 24px;
}

.form-group.full-width {
    grid-column: 1 / -1;
}

label {
    display: block;
    margin-bottom: 8px;
    font-weight: 600;
    color: #374151;
    font-size: 14px;
}

textarea {
    width: 100%;
    padding: 14px;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    font-size: 15px;
    resize: vertical;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
}

textarea:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.upload-area {
    border: 2px dashed #d1d5db;
    border-radius: 8px;
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background-color: #fafafa;
}

.upload-area:hover, .upload-area.dragover {
    border-color: #3b82f6;
    background-color: #eff6ff;
}

.upload-area p {
    margin: 0;
    color: #6b7280;
    font-size: 14px;
}

.file-info {
    margin-top: 10px;
    font-size: 13px;
    color: #059669;
    font-weight: 500;
}

.btn-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-top: 30px;
}

button {
    padding: 14px 28px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 15px;
    font-weight: 600;
    transition: all 0.2s;
}

.btn-primary {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    color: white;
}

.btn-primary:hover {
    background: linear-gradient(135deg, #2563eb, #1d4ed8);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.btn-secondary {
    background-color: #6b7280;
    color: white;
}

.btn-secondary:hover {
    background-color: #4b5563;
}

.file-preview {
    margin-top: 16px;
    text-align: center;
}

.file-preview img {
    max-width: 200px;
    max-height: 150px;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    cursor: pointer;
    transition: transform 0.2s;
}

.file-preview img:hover {
    transform: scale(1.02);
}

.file-preview a {
    color: #3b82f6;
    text-decoration: none;
    font-weight: 500;
}

.file-preview a:hover {
    text-decoration: underline;
}

.file-type {
    margin-top: 8px;
    font-size: 13px;
    color: #6b7280;
    font-style: italic;
}

/* Response Section */
.response-container {
    margin-top: 40px;
    padding: 0;
    display: none;
}

.response-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.response-title {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
}

.response-title.success {
    color: #059669;
}

.response-title.error {
    color: #dc2626;
}

.download-btn {
    background: linear-gradient(135deg, #10b981, #059669);
    color: white;
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
}

.download-btn:hover {
    background: linear-gradient(135deg, #059669, #047857);
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.3);
}

.download-btn:disabled {
    background: #d1d5db;
    cursor: not-allowed;
    box-shadow: none;
}

.result-section {
    margin-bottom: 24px;
}

.result-label {
    font-weight: 600;
    color: #374151;
    margin-bottom: 12px;
    font-size: 15px;
    display: flex;
    align-items: center;
    gap: 16px;
}

.legend-high-score {
    font-size: 13px;
    color: #f59e0b;
    font-weight: 500;
}

.matched-material-details {
    background-color: #fafafa;
    border-radius: 8px;
    border: 1px solid #e5e7eb;
    overflow: hidden;
}

/* Table Styles */
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

.data-table thead {
    background-color: #f8fafc;
}

.data-table th {
    padding: 14px 12px;
    text-align: left;
    font-weight: 600;
    color: #475569;
    border-bottom: 2px solid #e2e8f0;
    white-space: nowrap;
}

.data-table td {
    padding: 12px;
    border-bottom: 1px solid #f1f5f9;
    color: #334155;
}

.data-table tbody tr {
    transition: background-color 0.15s;
}

.data-table tbody tr:hover {
    background-color: #f8fafc;
}

.data-table tbody tr.selected {
    background-color: #eff6ff;
}

.data-table tbody tr.selected:hover {
    background-color: #dbeafe;
}

/* High score row highlight (>80) */
.data-table tbody tr.high-score {
    background-color: #fef3c7 !important;
    border-left: 4px solid #f59e0b;
}

.data-table tbody tr.high-score:hover {
    background-color: #fde68a !important;
}

.data-table tbody tr.high-score.selected {
    background-color: #fef3c7 !important;
}

.data-table tbody tr.high-score.selected:hover {
    background-color: #fde68a !important;
}

.checkbox-cell {
    width: 40px;
    text-align: center;
}

.data-table input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: #3b82f6;
}

.query-group {
    margin-bottom: 24px;
}

.query-group h4 {
    margin: 0 0 12px 0;
    padding: 12px 16px;
    background-color: #f1f5f9;
    border-radius: 6px;
    font-size: 15px;
    color: #334155;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 12px;
}

.query-count {
    font-size: 13px;
    color: #f59e0b;
    font-weight: 500;
}

.low-score-toggle {
    padding: 12px 16px;
    border-top: 1px dashed #e5e7eb;
}

.toggle-btn {
    background: none;
    border: 1px solid #d1d5db;
    color: #6b7280;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.2s;
}

.toggle-btn:hover {
    background-color: #f3f4f6;
    color: #374151;
    border-color: #9ca3af;
}

.low-score-table {
    border-top: none !important;
}

.low-score-table td {
    color: #9ca3af;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.85);
}

.modal-content {
    margin: auto;
    display: block;
    max-width: 90%;
    max-height: 90%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 8px;
}

.close {
    position: absolute;
    top: 20px;
    right: 40px;
    color: #fff;
    font-size: 40px;
    font-weight: bold;
    cursor: pointer;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.close:hover {
    opacity: 1;
}

/* Loading State */
.loading {
    text-align: center;
    padding: 40px;
    color: #6b7280;
}

.loading::after {
    content: '';
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid #3b82f6;
    border-radius: 50%;
    border-top-color: transparent;
    animation: spin 0.8s linear infinite;
    margin-left: 12px;
    vertical-align: middle;
}

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

/* Responsive */
@media (max-width: 768px) {
    .container {
        padding: 24px;
    }

    .form-section {
        grid-template-columns: 1fr;
    }

    .data-table {
        font-size: 13px;
    }

    .data-table th, .data-table td {
        padding: 10px 8px;
    }

    .btn-group {
        grid-template-columns: 1fr;
    }

    .history-sidebar {
        width: 100%;
        right: -100%;
    }
}

/* Global Loading Overlay */
.global-loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.global-loading p {
    color: white;
    margin-top: 20px;
    font-size: 16px;
    font-weight: 500;
}

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

/* History Sidebar */
.history-sidebar {
    position: fixed;
    top: 0;
    right: -400px;
    width: 400px;
    height: 100%;
    background-color: white;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.15);
    z-index: 1001;
    transition: right 0.3s ease;
    display: flex;
    flex-direction: column;
}

.history-sidebar.open {
    right: 0;
}

.history-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: none;
}

.history-overlay.open {
    display: block;
}

.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid #e5e7eb;
}

.sidebar-header h3 {
    margin: 0;
    font-size: 18px;
    color: #1a1a1a;
}

.btn-clear-history {
    background-color: #ef4444;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
}

.btn-clear-history:hover {
    background-color: #dc2626;
}

.history-list {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.history-item {
    background-color: #f8fafc;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 12px;
    cursor: pointer;
    transition: all 0.2s;
}

.history-item:hover {
    background-color: #f1f5f9;
    border-color: #3b82f6;
}

.history-item.active {
    border-color: #3b82f6;
    background-color: #eff6ff;
}

.history-time {
    font-size: 12px;
    color: #6b7280;
    margin-bottom: 8px;
}

.history-title {
    font-size: 14px;
    font-weight: 600;
    color: #334155;
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-preview {
    font-size: 13px;
    color: #6b7280;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 8px;
}

.history-meta {
    display: flex;
    justify-content: space-between;
    font-size: 12px;
    color: #9ca3af;
}

.history-empty {
    text-align: center;
    padding: 40px 20px;
    color: #9ca3af;
}
