/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f5f7fa;
    color: #333;
    line-height: 1.6;
}

/* App Container */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* Header */
.header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1rem 2rem;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    z-index: 100;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    max-width: 1400px;
    margin: 0 auto;
}

.header h1 {
    font-size: 1.8rem;
    font-weight: 600;
}

.header h1 i {
    margin-right: 0.5rem;
    color: #ffd700;
}

.header-actions {
    display: flex;
    gap: 0.5rem;
}

/* Buttons */
.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-primary {
    background-color: #4f46e5;
    color: white;
}

.btn-primary:hover {
    background-color: #4338ca;
    transform: translateY(-1px);
}

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

.btn-secondary:hover {
    background-color: #4b5563;
    transform: translateY(-1px);
}

.btn-danger {
    background-color: #dc2626;
    color: white;
}

.btn-danger:hover {
    background-color: #b91c1c;
}

.btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 350px;
    background: white;
    border-right: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.search-section {
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.search-box {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.search-box input {
    flex: 1;
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.9rem;
}

.search-box input:focus {
    outline: none;
    border-color: #4f46e5;
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.search-filters {
    display: flex;
    gap: 0.5rem;
}

.search-filters select {
    flex: 1;
    padding: 0.5rem;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 0.8rem;
}

.search-filters .btn-small {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    white-space: nowrap;
    min-width: fit-content;
}

.results-section {
    flex: 1;
    padding: 1rem;
    overflow-y: auto;
}

.results-section h3 {
    margin-bottom: 1rem;
    color: #374151;
    font-size: 1.1rem;
}

.results-count {
    color: #6b7280;
    font-size: 0.9rem;
    margin-bottom: 1rem;
    padding: 0.5rem;
    background-color: #f9fafb;
    border-radius: 4px;
}

/* Search Results */
.search-results {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.result-item {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.result-item:hover {
    border-color: #4f46e5;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    transform: translateY(-2px);
}

.result-item.active {
    border-color: #4f46e5;
    background-color: #f0f9ff;
}

.result-item h4 {
    color: #1f2937;
    margin-bottom: 0.5rem;
    font-size: 1rem;
    line-height: 1.4;
}

.result-item .meta {
    display: flex;
    gap: 1rem;
    font-size: 0.8rem;
    color: #6b7280;
    margin-bottom: 0.5rem;
}

.result-item .description {
    color: #4b5563;
    font-size: 0.9rem;
    line-height: 1.5;
}

/* Article Viewer */
.article-viewer {
    flex: 1;
    background: white;
    overflow-y: auto;
    padding: 2rem;
}

.article-container {
    max-width: 800px;
    margin: 0 auto;
}

.welcome-message {
    text-align: center;
    color: #6b7280;
    padding: 4rem 2rem;
}

.welcome-message i {
    font-size: 4rem;
    margin-bottom: 1rem;
    color: #d1d5db;
}

.welcome-message h2 {
    margin-bottom: 1rem;
    color: #374151;
}

/* Article Display */
.article-header {
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 2px solid #e5e7eb;
}

.article-title {
    font-size: 2rem;
    color: #1f2937;
    margin-bottom: 1rem;
    line-height: 1.3;
}

.article-meta {
    display: flex;
    gap: 2rem;
    color: #6b7280;
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

.article-actions {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.article-content {
    line-height: 1.8;
    color: #374151;
    font-size: 1.1rem;
}

.article-content p {
    margin-bottom: 1.5rem;
}

.article-content .highlighted {
    background-color: #fef3c7;
    padding: 0.1rem 0.2rem;
    border-radius: 3px;
}

/* Modals */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
    backdrop-filter: blur(4px);
}

.modal.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: white;
    margin: 2rem;
    border-radius: 12px;
    width: 90%;
    max-width: 700px;
    overflow: hidden;
    box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
}

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

.modal-header h2 {
    color: #1f2937;
    font-size: 1.5rem;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #6b7280;
    padding: 0.5rem;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.close-btn:hover {
    background-color: #f3f4f6;
    color: #374151;
}

.modal-body {
    padding: 2rem;
    overflow-y: auto;
    max-height: calc(80vh);
}

/* Settings */
.settings-section {
    margin-bottom: 2rem;
}

.settings-section h3 {
    color: #1f2937;
    margin-bottom: 1rem;
    font-size: 1.2rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.settings-section h3 i {
    color: #8b5cf6;
    font-size: 1.1rem;
}

.prompt-list {
    margin-bottom: 1.5rem;
}

.prompt-item {
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    padding: 1rem;
    margin-bottom: 0.75rem;
}

.prompt-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.prompt-name {
    font-weight: 600;
    color: #1f2937;
}

.prompt-content {
    color: #4b5563;
    font-size: 0.9rem;
    line-height: 1.5;
}

.prompt-text {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 4px;
    padding: 0.75rem;
    margin: 0.5rem 0;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    line-height: 1.4;
    white-space: pre-wrap;
    word-wrap: break-word;
    max-height: 200px;
    overflow-y: auto;
    color: #495057;
}

.prompt-preview {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 4px;
    padding: 0.75rem;
    margin: 0.5rem 0;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    line-height: 1.4;
    white-space: pre-wrap;
    word-wrap: break-word;
    max-height: 150px;
    overflow-y: auto;
    color: #495057;
}

/* User Prompt Templates */
.user-prompt-templates {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.template-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    transition: all 0.2s ease;
}

.template-item:hover {
    background: #f3f4f6;
    border-color: #d1d5db;
}

.template-name {
    font-weight: 500;
    color: #374151;
    font-size: 0.9rem;
}

.add-prompt {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.add-prompt input,
.add-prompt textarea {
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.9rem;
}

.add-prompt textarea {
    min-height: 100px;
    resize: vertical;
}

.db-info p {
    margin-bottom: 0.5rem;
    color: #4b5563;
}

/* AI Settings */
.ai-settings {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.api-key-input {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.api-key-input label {
    font-weight: 500;
    color: #374151;
}

.api-key-input input {
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.9rem;
}

.model-selection {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.model-selection label {
    font-weight: 500;
    color: #374151;
}

.model-selection select {
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.9rem;
}

.api-status {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.status-indicator {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.8rem;
    font-weight: 500;
    position: relative; /* Add positioning context for loading spinner */
}

.status-indicator.connected {
    background-color: #dcfce7;
    color: #166534;
}

.status-indicator.error {
    background-color: #fee2e2;
    color: #991b1b;
}

.status-indicator.not-configured {
    background-color: #f3f4f6;
    color: #6b7280;
}

.status-indicator.loading {
    background-color: #dbeafe;
    color: #1e40af;
}

/* Import/Export */
.import-export-tabs {
    display: flex;
    border-bottom: 1px solid #e5e7eb;
    margin-bottom: 1.5rem;
}

.tab-btn {
    padding: 0.75rem 1.5rem;
    border: none;
    background: none;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    color: #6b7280;
    transition: all 0.2s ease;
}

.tab-btn.active {
    color: #4f46e5;
    border-bottom-color: #4f46e5;
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

.file-upload {
    margin-bottom: 1.5rem;
}

.file-upload input[type="file"] {
    display: none;
}

.file-upload-label {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2rem;
    border: 2px dashed #d1d5db;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    color: #6b7280;
}

.file-upload-label:hover {
    border-color: #4f46e5;
    color: #4f46e5;
}

.file-upload-label i {
    font-size: 2rem;
    margin-bottom: 0.5rem;
}

.export-options {
    margin-bottom: 1.5rem;
}

.export-options label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    color: #4b5563;
}

/* AI Chat */
.prompt-selector {
    margin-bottom: 0.25rem;
}

.prompt-selector-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.1rem;
}

.prompt-selector label {
    color: #374151;
    font-weight: 500;
    margin: 0;
}

.toggle-prompt-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: #6b7280;
    padding: 0.25rem;
    border-radius: 4px;
    transition: all 0.2s ease;
    font-size: 0.9rem;
}

.toggle-prompt-btn:hover {
    background-color: #f3f4f6;
    color: #374151;
}

.toggle-prompt-btn.active {
    color: #4f46e5;
}

.prompt-selector select {
    width: 100%;
    padding: 0.15rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 0.9rem;
}

.chat-container {
    display: flex;
    flex-direction: column;
    height: vh;
    max-height: vh;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    background: #f9fafb;
    border-radius: 6px;
    margin-bottom: 1rem;
    max-height: 300px;
    min-height: 200px;
}

.chat-message {
    margin-bottom: 1rem;
    padding: 1rem;
    border-radius: 8px;
    line-height: 1.6;
}

.chat-message:last-child {
    margin-bottom: 0;
}

.chat-message.user {
    background: #dbeafe;
    color: #1e40af;
    margin-left: 2rem;
}

.chat-message.assistant {
    background: #f3f4f6;
    color: #374151;
    margin-right: 2rem;
}

.loading-message {
    background: #f0f9ff !important;
    color: #1e40af !important;
    border: 1px solid #dbeafe;
}

.loading-content {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-style: italic;
}

.loading-content i {
    color: #3b82f6;
}

/* Chat Content Blocks */
.text-block {
    margin-bottom: 0.5rem;
    line-height: 1.5;
}

.json-block, .markdown-block, .code-block {
    margin: 1rem 0;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    overflow: hidden;
    background: white;
}

.json-header, .markdown-header, .code-header {
    background: #f8fafc;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #e5e7eb;
    font-weight: 600;
    color: #374151;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.json-header {
    background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    color: #0369a1;
    border-bottom: 1px solid #0ea5e9;
}

.json-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.json-table th {
    background: #f1f5f9;
    padding: 0.15rem;
    text-align: left;
    font-weight: 600;
    color: #374151;
    border-bottom: 1px solid #e5e7eb;
}

.json-table td {
    padding: 0.15rem;
    border-bottom: 1px solid #f1f5f9;
    vertical-align: top;
}

.json-key {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    color: #1e40af;
    font-weight: 500;
    word-break: break-all;
    max-width: 200px;
}

.json-value {
    color: #374151;
    word-break: break-word;
    max-width: 300px;
}

/* JSON Value Type Styling */
.json-value.json-string {
    color: #059669;
    font-weight: 500;
}

.json-value.json-number {
    color: #dc2626;
    font-weight: 600;
}

.json-value.json-boolean {
    color: #7c3aed;
    font-weight: 600;
}

.json-value.json-null {
    color: #6b7280;
    font-style: italic;
}

.json-value.json-array {
    color: #ea580c;
    font-weight: 500;
}

.json-value.json-object {
    color: #0891b2;
    font-weight: 500;
}

.markdown-content {
    padding: 1rem;
    line-height: 1.6;
}

.markdown-content h1, .markdown-content h2, .markdown-content h3 {
    margin: 1rem 0 0.5rem 0;
    color: #1f2937;
}

.markdown-content h1 {
    font-size: 1.5rem;
    border-bottom: 2px solid #e5e7eb;
    padding-bottom: 0.5rem;
}

.markdown-content h2 {
    font-size: 1.3rem;
    border-bottom: 1px solid #e5e7eb;
    padding-bottom: 0.25rem;
}

.markdown-content h3 {
    font-size: 1.1rem;
}

.markdown-content p {
    margin: 0.5rem 0;
}

.markdown-content ul, .markdown-content ol {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.markdown-content li {
    margin: 0.25rem 0;
}

.markdown-content code {
    background: #f1f5f9;
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.9em;
    color: #dc2626;
}

.markdown-content a {
    color: #2563eb;
    text-decoration: none;
}

.markdown-content a:hover {
    text-decoration: underline;
}

.code-block pre {
    margin: 0;
    padding: 1rem;
    background: #1f2937;
    color: #f9fafb;
    overflow-x: auto;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.85rem;
    line-height: 1.4;
}

.code-block code {
    background: none;
    padding: 0;
    color: inherit;
}

.chat-input {
    display: flex;
    gap: 0.5rem;
    flex-shrink: 0;
}

.chat-input textarea {
    flex: 1;
    padding: 0.75rem;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    resize: none;
    min-height: 60px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        gap: 1rem;
    }
    
    .header-actions {
        flex-wrap: wrap;
        justify-content: center;
    }
    
    .main-content {
        flex-direction: column;
    }
    
    .sidebar {
        width: 100%;
        height: 300px;
    }
    
    .modal-content {
        margin: 1rem;
        width: 95%;
    }
    
    .article-viewer {
        padding: 1rem;
    }
    
    .article-title {
        font-size: 1.5rem;
    }
    
    /* Chat content responsive adjustments */
    .json-table {
        font-size: 0.75rem;
    }
    
    .json-table th,
    .json-table td {
        padding: 0.5rem;
    }
    
    .json-key,
    .json-value {
        max-width: 120px;
    }
    
    .markdown-content h1 {
        font-size: 1.3rem;
    }
    
    .markdown-content h2 {
        font-size: 1.1rem;
    }
    
    .markdown-content h3 {
        font-size: 1rem;
    }
    
    /* Chat responsive adjustments */
    .chat-container {
        height: 300px;
        max-height: 300px;
    }
    
    .chat-messages {
        max-height: 200px;
        min-height: 150px;
    }
    
    .modal-content {
        max-width: 95%;
        margin: 1rem;
    }
}

/* Loading States */
.loading {
    opacity: 0.6;
    pointer-events: none;
    position: relative; /* Ensure positioning context */
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #f3f3f3;
    border-top: 2px solid #4f46e5;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 10; /* Ensure spinner appears above content */
}

/* Specific loading states for different elements */
.status-indicator.loading {
    /* Override general loading opacity for status indicators */
    opacity: 1;
    pointer-events: auto;
}

.status-indicator.loading::after {
    /* Ensure spinner is properly positioned within status indicator */
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    margin: 0;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Bookmarks */
.bookmark-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: #6b7280;
    transition: all 0.2s ease;
    padding: 0.5rem;
    border-radius: 4px;
}

.bookmark-btn:hover {
    background-color: #f3f4f6;
}

.bookmark-btn.bookmarked {
    color: #f59e0b;
}

.bookmark-btn.bookmarked:hover {
    color: #d97706;
}

/* Highlighting */
.highlight-controls {
    margin-bottom: 1rem;
    padding: 1rem;
    background: #f9fafb;
    border-radius: 6px;
    border: 1px solid #e5e7eb;
}

.highlight-controls input {
    padding: 0.5rem;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    margin-right: 0.5rem;
}

.highlight-controls button {
    margin-right: 0.5rem;
}

.ai-assistant-buttons {
    display: inline-flex;
    gap: 0.5rem;
    margin-left: 0.5rem;
    align-items: center;
}

.ai-assistant-btn {
    white-space: nowrap;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
}
