:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #dbeafe;
    --bg-primary: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-tertiary: #f1f5f9;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-tertiary: #94a3b8;
    --border-color: #e2e8f0;
    --border-light: #f1f5f9;
    --success-color: #10b981;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
    --sidebar-width: 280px;
    --header-height: 64px;
    --input-height: 80px;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    --transition: all 0.2s ease;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow: hidden;
}

/* App Container */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--bg-primary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: var(--transition);
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 16px;
}

.logo svg {
    color: var(--primary-color);
}

.new-chat-btn {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 16px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
}

.new-chat-btn:hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.history-section {
    margin-bottom: 16px;
}

.history-label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.history-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    margin-top: 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.history-item:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

.history-item.active {
    background-color: var(--primary-light);
    color: var(--primary-color);
}

.history-item svg {
    flex-shrink: 0;
}

.history-item span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
}

.model-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.813rem;
    color: var(--text-tertiary);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Header */
.main-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 24px;
    background-color: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    gap: 16px;
}

.menu-toggle {
    display: none;
    padding: 8px;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary);
    transition: var(--transition);
}

.menu-toggle:hover {
    background-color: var(--bg-tertiary);
    color: var(--text-primary);
}

.main-header h1 {
    flex: 1;
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text-primary);
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background-color: var(--bg-tertiary);
    border-radius: 20px;
    font-size: 0.813rem;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--success-color);
    animation: pulse 2s infinite;
}

.status-dot.error {
    background-color: var(--error-color);
    animation: none;
}

.status-dot.loading {
    background-color: var(--warning-color);
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.status-text {
    color: var(--text-secondary);
}

/* Chat Area */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
}

/* Welcome Screen */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    text-align: center;
    padding: 40px 20px;
    max-width: 700px;
    margin: 0 auto;
}

.welcome-screen.hidden {
    display: none;
}

.welcome-icon {
    width: 80px;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary-light), #e0e7ff);
    border-radius: 20px;
    margin-bottom: 24px;
    color: var(--primary-color);
}

.welcome-screen h2 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 12px;
}

.welcome-screen p {
    font-size: 1rem;
    color: var(--text-secondary);
    margin-bottom: 32px;
    max-width: 480px;
}

.suggestion-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
}

.chip {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background-color: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    font-size: 0.875rem;
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition);
}

.chip:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
    background-color: var(--primary-light);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.chip svg {
    flex-shrink: 0;
}

/* Messages Container */
.messages-container {
    max-width: 900px;
    width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

/* Message */
.message {
    display: flex;
    gap: 16px;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.message.user .message-avatar {
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: white;
}

.message.assistant .message-avatar {
    background: linear-gradient(135deg, var(--primary-color), #3b82f6);
    color: white;
}

.message-content {
    flex: 1;
    min-width: 0;
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 8px;
}

.message-author {
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-primary);
}

.message-time {
    font-size: 0.75rem;
    color: var(--text-tertiary);
}

.message-text {
    background-color: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 16px;
    font-size: 0.938rem;
    line-height: 1.7;
    color: var(--text-primary);
}

.message.user .message-text {
    background-color: var(--primary-light);
    border-color: transparent;
}

.message-text p {
    margin-bottom: 12px;
}

.message-text p:last-child {
    margin-bottom: 0;
}

.message-text h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin: 20px 0 12px 0;
    color: var(--text-primary);
}

.message-text h3 {
    font-size: 1rem;
    font-weight: 600;
    margin: 16px 0 8px 0;
    color: var(--text-primary);
}

.message-text h4 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 16px 0 8px 0;
    padding-bottom: 4px;
    border-bottom: 1px solid var(--border-light);
}

.message-text h4:first-child {
    margin-top: 0;
}

.message-text ul, .message-text ol {
    margin: 12px 0;
    padding-left: 24px;
}

.message-text li {
    margin-bottom: 8px;
}

.message-text code {
    background-color: var(--bg-tertiary);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.875rem;
}

.message-text pre {
    background-color: #1e293b;
    color: #e2e8f0;
    padding: 16px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}

.message-text pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.message-text strong {
    font-weight: 600;
    color: var(--text-primary);
}

.message-text a {
    color: var(--primary-color);
    text-decoration: none;
}

.message-text a:hover {
    text-decoration: underline;
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 8px 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--text-tertiary);
    animation: typing 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

/* Input Area */
.input-area {
    padding: 16px 24px 24px;
    background-color: var(--bg-secondary);
}

.input-container {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background-color: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 12px 16px;
    box-shadow: var(--shadow-sm);
    transition: var(--transition);
}

.input-container:focus-within {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.input-container textarea {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-family: inherit;
    font-size: 0.938rem;
    line-height: 1.5;
    color: var(--text-primary);
    background: transparent;
    max-height: 150px;
    min-height: 24px;
}

.input-container textarea::placeholder {
    color: var(--text-tertiary);
}

.send-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background-color: var(--primary-hover);
    transform: scale(1.05);
}

.send-btn:disabled {
    background-color: var(--bg-tertiary);
    color: var(--text-tertiary);
    cursor: not-allowed;
}

.input-footer {
    max-width: 900px;
    margin: 8px auto 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    color: var(--text-tertiary);
}

.char-count {
    font-variant-numeric: tabular-nums;
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--text-tertiary);
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        height: 100%;
        z-index: 1000;
        box-shadow: var(--shadow-lg);
    }

    .sidebar.open {
        left: 0;
    }

    .menu-toggle {
        display: flex;
    }

    .main-header h1 {
        font-size: 1rem;
    }

    .chat-area {
        padding: 16px;
    }

    .input-area {
        padding: 12px 16px 16px;
    }

    .suggestion-chips {
        flex-direction: column;
    }

    .chip {
        width: 100%;
        justify-content: flex-start;
    }
}

/* Overlay for mobile sidebar */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
}

.sidebar-overlay.active {
    display: block;
}

/* Error Message */
.error-message {
    background-color: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    gap: 8px;
}

.error-message svg {
    flex-shrink: 0;
}

/* Source References */
.source-reference {
    display: inline;
    font-size: 0.75rem;
    color: var(--primary-color);
    background-color: var(--primary-light);
    padding: 2px 6px;
    border-radius: 4px;
    margin: 0 2px;
    word-break: break-all;
}

/* Sources Section - Nueva sección de fuentes */
.sources-section {
    margin-top: 20px;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
}

.sources-header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.sources-header svg {
    color: var(--primary-color);
}

.sources-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.source-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    background-color: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    cursor: pointer;
    transition: var(--transition);
}

.source-item:hover {
    background-color: var(--primary-light);
    border-color: var(--primary-color);
    transform: translateX(4px);
}

.source-icon {
    font-size: 1.25rem;
    flex-shrink: 0;
}

.source-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.source-name {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.source-page {
    font-size: 0.75rem;
    color: var(--text-tertiary);
}

.source-index {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary-color);
    background-color: var(--primary-light);
    padding: 2px 8px;
    border-radius: 12px;
    flex-shrink: 0;
}

/* Botón de fuente inline */
.source-btn {
    display: inline;
    padding: 1px 6px;
    margin: 0 1px;
    background: #eef2ff;
    border: 1px solid #c7d2fe;
    border-radius: 3px;
    font-size: inherit;
    font-weight: 500;
    color: #4338ca;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
    text-decoration: none;
    font-family: inherit;
}

.source-btn:hover {
    background: #4338ca;
    border-color: #4338ca;
    color: white;
}

.source-btn svg {
    display: none;
}

.source-btn span {
    font-size: 0.85em;
}

/* Modal de fuente */
.source-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10000;
    animation: fadeIn 0.2s ease;
}

.source-modal-overlay.closing {
    animation: fadeOut 0.2s ease forwards;
}

@keyframes fadeOut {
    from { opacity: 1; }
    to { opacity: 0; }
}

.source-modal {
    background: var(--bg-primary);
    border-radius: 16px;
    box-shadow: var(--shadow-lg);
    max-width: 420px;
    width: 90%;
    overflow: hidden;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.source-modal-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 20px 24px;
    background: linear-gradient(135deg, var(--primary-color), #3b82f6);
    color: white;
}

.source-modal-icon {
    font-size: 2rem;
    line-height: 1;
}

.source-modal-header h3 {
    flex: 1;
    font-size: 1.125rem;
    font-weight: 600;
    margin: 0;
}

.source-modal-close {
    padding: 8px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 8px;
    color: white;
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.source-modal-close:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

.source-modal-body {
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.source-modal-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.source-modal-field label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.source-modal-value {
    font-size: 1rem;
    color: var(--text-primary);
    font-weight: 500;
    padding: 12px 16px;
    background-color: var(--bg-tertiary);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.source-modal-page {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: fit-content;
    min-width: 60px;
    background: linear-gradient(135deg, var(--primary-light), #e0e7ff);
    border-color: var(--primary-color);
    color: var(--primary-color);
    font-weight: 700;
    font-size: 1.25rem;
}

.source-modal-footer {
    padding: 16px 24px;
    background-color: var(--bg-tertiary);
    border-top: 1px solid var(--border-color);
}

.source-modal-note {
    font-size: 0.813rem;
    color: var(--text-tertiary);
    margin: 0;
    text-align: center;
}

/* Link de fuente inline (para abrir PDFs) */
.source-link {
    display: inline;
    padding: 2px 8px;
    margin: 0 2px;
    background: linear-gradient(135deg, #eef2ff 0%, #e0e7ff 100%);
    border: 1px solid #c7d2fe;
    border-radius: 4px;
    font-size: 0.85em;
    font-weight: 500;
    color: #4338ca;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    text-decoration: none;
    font-family: inherit;
}

.source-link:hover {
    background: linear-gradient(135deg, #4338ca 0%, #3730a3 100%);
    border-color: #4338ca;
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 2px 8px rgba(67, 56, 202, 0.3);
}

.source-link:active {
    transform: translateY(0);
}

/* Toast notifications */
.toast {
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 20px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    z-index: 10001;
    animation: toastSlideUp 0.3s ease;
}

.toast-loading {
    background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    border-color: #7dd3fc;
}

.toast-error {
    background: linear-gradient(135deg, #fef2f2 0%, #fee2e2 100%);
    border-color: #fca5a5;
    color: #b91c1c;
}

.toast-success {
    background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
    border-color: #86efac;
    color: #15803d;
}

.toast-hiding {
    animation: toastSlideDown 0.3s ease forwards;
}

@keyframes toastSlideUp {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

@keyframes toastSlideDown {
    from {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
    to {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
}

.toast-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid #0ea5e9;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}
