/*
 * RAG Application Styles for Shiny Core
 * Custom CSS for document chat interface
 * Uses Bootstrap CSS variables for theme-aware colors
 */

:root {
    --primary-color: #ff6b35;
    --secondary-color: #004e89;
    --border-radius: 8px;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.1);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.15);
}

.table {
    --bs-table-striped-bg: rgba(255, 255, 255, 0.05);
}

/* Sidebar Styles - use Bootstrap variables for theme awareness */
.sidebar {
    background-color: var(--bs-body-bg);
    border-right: 1px solid var(--bs-border-color);
}

.sidebar h5 {
    color: var(--primary-color);
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.bslib-sidebar-resize-handle {
    display: none;
}

/* Document List */
.document-list {
    max-height: 300px;
    overflow-y: auto;
}

.hide_ai_files_switch {
    font-size: 0.75rem;
}
.hide_ai_files_switch .form-group {
    margin-bottom: 0px;
}

.bulk_delete_switch {
    font-size: 0.75rem;
    margin-bottom: 0.5rem;
}

.bulk_delete_switch .form-group {
    margin-bottom: 0px;
}


.document-item {
    display: flex;
    align-items: center;
    padding: 1px;
    background: var(--bs-tertiary-bg);
    transition: background-color 0.2s;
}

.document-item:hover {
    background: var(--bs-secondary-bg);
}

.document-item .doc-name {
    flex-grow: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.75rem;
}

.document-item .doc-actions {
    display: flex;
    gap: 0.25rem;
    flex-shrink: 0;
}

.document-item.bulk-mode {
    padding-left: 0.5rem;
}

/* Fix RAW HTML checkbox alignment within document items */
.document-item.bulk-mode .bulk-selector-checkbox {
    margin: 0 !important;
    cursor: pointer;
}

.document-item .bulk-selector-cell {
    margin-right: 0.75rem;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.document-item .doc-name-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100%;
}

.document-item .bulk-selector-cell {
    margin-right: 0.75rem;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.document-item .doc-actions button {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
}

.bulk-actions-container {
    margin-top: 0.5rem;
}

.hint {
    font-size: 0.75rem;

}

/* Chat Container - flex-based layout */
#chat_area_wrapper {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;  /* Critical for nested flex scroll */
    overflow: hidden;  /* Prevent wrapper from causing page scroll */
}

.chat-container {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;  /* Allow shrinking below content size */
    overflow: hidden;  /* Let Shiny chat handle internal scroll */
}

/* Shiny Chat component styling - ensure chat fills and scrolls */
.chat-container > * {
    flex: 1;
    min-height: 0;
    word-break: break-word; /* Force break long words */
    overflow-x: hidden;  /* Prevent horizontal scroll */
    /*overflow-y: auto;     Show vertical scrollbar when needed */
}

shiny-chat-messages {
    margin-right: initial !important;
}

/* Chat controls at bottom - fixed, no flex-grow */
#chat_area_wrapper > .shiny-html-output:last-child {
    flex-shrink: 0;
}

.chat-buttons .form-group {
    margin-bottom: 0;
}

.chat-history {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1rem;
    background: var(--bs-tertiary-bg);
    border-radius: var(--border-radius);
    margin-bottom: 1rem;
}

/* Chat Messages */
.chat-message {
    max-width: 80%;
    padding: 0.75rem 1rem;
    margin-bottom: 0.75rem;
    border-radius: var(--border-radius);
    position: relative;
}

.chat-message.user {
    background: var(--secondary-color);
    color: white;
    margin-left: auto;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant {
    background: var(--bs-tertiary-bg);
    color: var(--bs-body-color);
    border: 1px solid var(--bs-border-color);
    border-bottom-left-radius: 4px;
}

.chat-message .message-actions {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    opacity: 0;
    transition: opacity 0.2s;
}

.chat-message:hover .message-actions {
    opacity: 1;
}

.chat-message .bookmark-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.875rem;
    opacity: 0.7;
}

.chat-message .bookmark-btn:hover {
    opacity: 1;
}

.chat-container h1 {
    font-size: 1.5rem;
}

.chat-container h2 {
    font-size: 1.4rem;
}

.chat-container h3 {
    font-size: 1.3rem;
}

.chat-container h4 {
    font-size: 1.2rem;
}

.chat-container h5 {
    font-size: 1.1rem;
}

.chat-container h6 {
    font-size: 1rem;
    font-weight: 500;
}

/* Analysis Container */
.analysis-container {
    padding: 1rem;
}

.example-prompts .btn {
    margin-bottom: 0.5rem;
}

/* Error Display */
.error-container {
    position: sticky;
    top: 0;
    z-index: 1000;
}

.error-container .alert {
    margin-bottom: 0.5rem;
}

.error-container details {
    font-size: 0.875rem;
}

.error-container pre {
    font-size: 0.75rem;
    max-height: 200px;
    overflow-y: auto;
    background: var(--bs-tertiary-bg);
    padding: 0.5rem;
    border-radius: 4px;
}

/* input containers */
.shiny-input-container:not(.shiny-input-container-inline) {
    min-width: 300px !important;
    width: auto !important;
}

/* Excel preview: override min-width so inputs fit side-by-side */
.excel-preview-container .shiny-input-container:not(.shiny-input-container-inline) {
    min-width: 0 !important;
    width: 100% !important;
}

/* Progress Indicators */
.progress-container {
    margin: 1rem 0;
}

.progress-status {
    font-size: 0.875rem;
    color: var(--bs-secondary-color);
    margin-top: 0.5rem;
}

/* Modal Dialogs */
.modal-content {
    border-radius: var(--border-radius);
}

.modal-header {
    background: var(--secondary-color);
    color: white;
    border-radius: var(--border-radius) var(--border-radius) 0 0;
}

/* Financial upload modal - warning-colored header for distinction */
/* Uses :has() to detect modal-financial class inside modal-body */
.modal-content:has(.modal-financial) .modal-header {
    background: var(--bs-warning);
    color: white;
}

/* Upload modal icon styling */
.modal-upload svg,
.modal-financial svg {
    opacity: 0.8;
}

.modal-title .form-group {
    margin-bottom: 0;
}

/* Force Data Explorer modal header to full width */
.modal-content:has(.data-explorer-header) .modal-title,
.modal-content:has(.data-explorer-header) .modal-header {
    flex: 1;
    width: 100%;
}

.modal-body {
    max-height: 70vh;
    overflow-y: auto;
}

.bslib-value-box .value-box-area {
    padding: 1rem;
    white-space: nowrap;
}

.bslib-value-box .value-box-showcase .fa {
    min-width: auto;
}

/* Ensure modal inputs are full width */
.modal-body input[type="text"],
.modal-body textarea,
.modal-body select {
    width: 100% !important;
}

/* Prevent textarea from scrolling weirdly */
/* it gets auto-resized by shiny */
.modal-body .textarea-autoresize {
    overflow-y: hidden; 
}

/* Prevent file input from breaking/wrapping in modals */
.modal-body .form-control.input-file {
    flex-wrap: nowrap;
}
.modal-body .input-group {
    flex-wrap: nowrap;
}

/* Sidebar subtitle styling */
.sidebar-subtitle {
    margin-top: -1rem;
}

/* Sidebar app logo */
.logo-container {
    margin-top: -2.5rem !important;
    height: 48px;
    overflow: hidden;
}

.app-logo-text {
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
    
    /* Gradient text */
    background: #3C85B2;
    background: linear-gradient(0deg, #3C85B2, #7CA1B3);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}

.app-logo-divider {
    width: 100%;
    border-color: #7CA1B3 !important;
    opacity: 1 !important;
}

.app-logo-subtitle {
    font-size: 0.65rem;
    font-weight: 500;
    line-height: 1;
    color: var(--bs-emphasis-color);
    opacity: 0.8;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    white-space: nowrap;
}

/* File Upload */
.file-upload-area {
    border: 2px dashed #ccc;
    border-radius: var(--border-radius);
    padding: 2rem;
    text-align: center;
    transition: border-color 0.2s, background-color 0.2s;
}

.file-upload-area:hover {
    border-color: var(--primary-color);
    background-color: rgba(255, 107, 53, 0.05);
}

.file-upload-area.dragging {
    border-color: var(--primary-color);
    background-color: rgba(255, 107, 53, 0.1);
}

/* Badges */
.badge {
    font-weight: 500;
}

/* Responsive Adjustments */
@media (max-width: 768px) {
    .chat-message {
        max-width: 95%;
    }
    
    .sidebar {
        width: 100% !important;
    }
}

/* Loading States */
.loading-spinner {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 2px solid #f3f3f3;
    border-top: 2px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* Accessibility */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Focus styles */
button:focus, 
input:focus, 
select:focus, 
textarea:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* Print styles */
@media print {
    .sidebar,
    .chat-input,
    .document-actions,
    .error-container {
        display: none;
    }
    
    .chat-history {
        height: auto;
        overflow: visible;
    }
}
/* Animations */
@keyframes pulse-red {
    0% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0.7); }
    70% { box-shadow: 0 0 0 10px rgba(220, 53, 69, 0); }
    100% { box-shadow: 0 0 0 0 rgba(220, 53, 69, 0); }
}

.animate-pulse {
    animation: pulse-red 2s infinite;
    border-radius: 10px;
}

/* ASR Heartbeat Styles */
.heartbeat-icon {
    display: inline-block;
    vertical-align: middle;
    margin-left: 5px;
    opacity: 0.3;
    transition: opacity 0.3s ease, transform 0.3s ease, color 0.3s ease;
    transform: scale(0.8);
    color: inherit; /* Ensures the color transition works smoothly */
}

.heartbeat-active {
    /* Only trigger animation; don't set static opacity/color here 
       so it returns to base (.heartbeat-icon) state after animation ends */
    animation: heartbeatFade 2.0s ease-out;
}

@keyframes heartbeatFade {
    0% {
        color: inherit;
        opacity: 0.3;
        transform: scale(0.8);
    }
    20% {
        color: var(--primary-color);
        opacity: 1;
        transform: scale(1.1);
    }
    40% {
        color: var(--primary-color);
        opacity: 0.8;
        transform: scale(1.0);
    }
    60% {
        color: var(--primary-color);
        opacity: 1;
        transform: scale(1.1);
    }
    100% {
        color: inherit;
        opacity: 0.3;
        transform: scale(0.8);
    }
}

/* Shiny Notification / Toast customization */
#shiny-notification-panel {
    width: auto;
    max-width: 450px;
}

.shiny-notification-container {
    width: 450px !important;
    max-width: 95vw !important;
}

.shiny-notification {
    background-color: var(--bs-tertiary-bg) !important;
    border: 1px solid var(--bs-border-color) !important;
    color: var(--bs-body-color) !important;
    border-radius: var(--border-radius) !important;
    box-shadow: var(--shadow-md) !important;
    word-break: break-word !important;
    padding: 15px !important;
}

.shiny-notification-message {
    font-size: 0.9rem !important;
}

/* --- Login & Auth Styles --- */
.login-wrapper {
    background: var(--bs-body-bg);
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.3s ease;
}

/* Optional: Keep the gradient for light mode, but use a darker one for dark mode */
[data-bs-theme="light"] .login-wrapper {
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
[data-bs-theme="dark"] .login-wrapper {
    background: linear-gradient(135deg, #1a1c1e 0%, #2c3e50 100%);
}

.login-container, .register-container {
    padding: 30px;
    background: var(--bs-tertiary-bg);
    border: 1px solid var(--bs-border-color); /* Corrected: used --bs-border-color */
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    width: 100%;
}

@media (min-width: 768px) {
    .login-container {
        max-width: 450px;
        padding: 40px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
    }
    
    .register-container {
        max-width: 750px;
        padding: 40px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
    }
}

@media (max-width: 767px) {
    .auth-wrapper {
        padding: 20px;
        align-items: flex-start !important;
        background: var(--bs-body-bg) !important; /* fallback for mobile */
    }
    .login-container, .register-container {
        border-radius: 12px;
        box-shadow: none;
        border: none;
        padding: 20px 10px;
    }
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-logo {
    font-size: 3rem;
    color: var(--bs-emphasis-color);
    margin-bottom: 10px;
}

.btn-login {
    width: 100%;
    padding: 12px;
    font-weight: 600;
    border-radius: 10px;
    margin-top: 20px;
    transition: all 0.3s ease;
}

.btn-login:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.auth-error {
    color: #e74c3c;
    background: rgba(231, 76, 60, 0.1);
    padding: 10px;
    border-radius: 8px;
    margin-bottom: 20px;
    text-align: center;
}
