/* Custom CSS for PulseGPT Chat */

:root {
    --pulse-primary: #667eea;
    --pulse-secondary: #764ba2;
    --pulse-gradient: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    
    /* Safe area insets for notched devices (iOS) */
    --safe-area-top: 0px;
    --safe-area-bottom: 0px;
    --safe-area-left: 0px;
    --safe-area-right: 0px;
}

html[data-bs-theme=dark] {
    --bs-body-bg: #212529;
    --bs-body-color: #ffffff;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    line-height: 1.6;
}

/* Chat specific styles */
.chat-layout {
    height: 100vh;
    overflow: hidden;
    /* Support for notched devices */
    padding-left: var(--safe-area-left);
    padding-right: var(--safe-area-right);
}

.chat-header {
    background: var(--bs-body-bg);
    border-bottom: 1px solid var(--bs-border-color);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    /* Support for notched devices */
    padding-top: calc(1rem + var(--safe-area-top));
}

.chat-sidebar {
    background: var(--bs-secondary-bg);
    border-right: 1px solid var(--bs-border-color);
    transition: all 0.3s ease;
}

.messages-container {
    scroll-behavior: smooth;
}

.messages-container::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-track {
    background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--bs-secondary);
    border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--bs-secondary-color);
}

/* Message bubbles */
.message-bubble {
    position: relative;
    animation: messageSlideIn 0.3s ease-out;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.user-message {
    background: var(--pulse-gradient);
    color: white;
    border-radius: 1.25rem 1.25rem 0.25rem 1.25rem;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.assistant-message {
    background: var(--bs-body-bg);
    border: 1px solid var(--bs-border-color);
    border-radius: 1.25rem 1.25rem 1.25rem 0.25rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.message-avatar {
    background: var(--pulse-gradient);
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
}

/* Form controls */
.form-control:focus {
    border-color: var(--pulse-primary);
    box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
}

.btn-primary {
    background: var(--pulse-gradient);
    border: none;
    box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
    transition: all 0.2s ease;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(102, 126, 234, 0.4);
}

.btn-primary:active {
    transform: translateY(0);
}

/* Composer */
.composer-container {
    background: var(--bs-body-bg);
    border-top: 1px solid var(--bs-border-color);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    /* Support for notched devices */
    padding-bottom: calc(1rem + var(--safe-area-bottom));
}

#messageInput {
    border: 2px solid var(--bs-border-color);
    border-radius: 1rem;
    transition: all 0.2s ease;
    resize: none;
}

#messageInput:focus {
    border-color: var(--pulse-primary);
    transform: scale(1.01);
}

/* Buttons */
.btn {
    border-radius: 0.75rem;
    font-weight: 500;
    transition: all 0.2s ease;
}

.btn-outline-primary {
    border-color: var(--pulse-primary);
    color: var(--pulse-primary);
}

.btn-outline-primary:hover {
    background: var(--pulse-gradient);
    border-color: transparent;
}

/* Loading states */
.typing-indicator::after {
    content: '●●●';
    animation: typing 1.4s infinite;
    font-size: 1.2em;
}

@keyframes typing {
    0%, 60%, 100% {
        opacity: 0.3;
    }
    30% {
        opacity: 1;
    }
}

/* Code blocks */
.code-block-container {
    margin: 0.75rem 0;
    border: 1px solid var(--bs-border-color);
    border-radius: 0.5rem;
    overflow: hidden;
}

.code-block-header {
    background: var(--bs-tertiary-bg);
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--bs-border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.875rem;
}

.code-block-label {
    color: var(--bs-secondary);
    font-weight: 500;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.copy-code-btn {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    border: 1px solid var(--bs-border-color);
    background: var(--bs-body-bg);
    color: var(--bs-secondary);
    transition: all 0.2s ease;
}

.copy-code-btn:hover {
    background: var(--bs-secondary-bg);
    color: var(--bs-emphasis-color);
    border-color: var(--pulse-primary);
}

.code-block {
    background: var(--bs-secondary-bg) !important;
    font-size: 0.875rem;
    padding: 1rem;
    margin: 0;
    overflow-x: auto;
    border-radius: 0;
}

.code-block code {
    background: none !important;
    padding: 0;
    color: var(--bs-emphasis-color);
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    line-height: 1.4;
}

.inline-code {
    background: var(--bs-secondary-bg);
    color: var(--bs-emphasis-color);
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-size: 0.9em;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    border: 1px solid var(--bs-border-color);
}

/* Message formatting styles */
.message-content strong {
    font-weight: 600;
    color: var(--bs-emphasis-color);
}

.message-content em {
    font-style: italic;
    color: var(--bs-body-color);
}

.message-content del {
    text-decoration: line-through;
    color: var(--bs-secondary);
}

.message-header {
    font-weight: 600;
    color: var(--bs-emphasis-color);
    border-bottom: 1px solid var(--bs-border-color);
    padding-bottom: 0.25rem;
}

.message-list {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.message-list-item {
    margin-bottom: 0.25rem;
    line-height: 1.5;
}

.message-list ul,
.message-list ol {
    margin-top: 0.25rem;
    margin-bottom: 0.25rem;
}

.message-blockquote {
    border-left: 4px solid var(--pulse-primary);
    background: var(--bs-secondary-bg);
    padding: 0.75rem 1rem;
    margin: 0.5rem 0;
    font-style: italic;
    border-radius: 0 0.375rem 0.375rem 0;
}

.message-divider {
    border: none;
    border-top: 2px solid var(--bs-border-color);
    margin: 1rem 0;
    opacity: 0.5;
}

.message-link {
    color: var(--pulse-primary);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: all 0.2s ease;
}

.message-link:hover {
    color: var(--pulse-secondary);
    border-bottom-color: var(--pulse-primary);
    text-decoration: none;
}

/* Dark mode adjustments */
[data-bs-theme="dark"] .code-block-container {
    border-color: #4a5568;
}

[data-bs-theme="dark"] .code-block-header {
    background: #1a202c;
    border-color: #4a5568;
}

[data-bs-theme="dark"] .copy-code-btn {
    background: #2d3748;
    border-color: #4a5568;
    color: #a0aec0;
}

[data-bs-theme="dark"] .copy-code-btn:hover {
    background: #4a5568;
    color: #e2e8f0;
    border-color: var(--pulse-primary);
}

[data-bs-theme="dark"] .code-block {
    background: #2d3748 !important;
    border-color: #4a5568;
}

[data-bs-theme="dark"] .inline-code {
    background: #2d3748;
    border-color: #4a5568;
    color: #e2e8f0;
}

[data-bs-theme="dark"] .message-blockquote {
    background: #2d3748;
    color: #e2e8f0;
}

/* Responsive text formatting */
@media (max-width: 768px) {
    .message-list {
        padding-left: 1.25rem;
    }
    
    .code-block {
        font-size: 0.8rem;
        padding: 0.75rem;
    }
    
    .message-header {
        font-size: 0.95em;
    }
}

/* ===================================
   COMPREHENSIVE RESPONSIVE DESIGN
   Mobile-First Approach
   =================================== */

/* Extra Small Devices - Phones (portrait) < 576px */
@media (max-width: 575.98px) {
    /* Chat Layout */
    .chat-layout {
        height: 100dvh; /* Use dynamic viewport height for mobile browsers */
    }
    
    /* Header - Optimized for mobile */
    .chat-header {
        padding: 0.5rem 0.75rem;
        flex-wrap: wrap;
    }
    
    .chat-header h1 {
        font-size: 1rem !important;
        margin-bottom: 0;
    }
    
    .chat-header .h4 {
        font-size: 1rem !important;
    }
    
    /* Hide conversation title on very small screens */
    #conversationTitle {
        display: none;
    }
    
    /* Hamburger menu button - larger touch target */
    #sidebarToggle {
        min-width: 44px;
        min-height: 44px;
        display: flex !important;
        align-items: center;
        justify-content: center;
    }
    
    /* Header buttons - icon only on mobile */
    .chat-header .btn {
        padding: 0.375rem 0.5rem;
        font-size: 0.875rem;
        min-width: 40px;
        min-height: 40px;
    }
    
    /* Hide button text, show only icons */
    .chat-header .btn-text {
        display: none;
    }
    
    /* Adjust icon spacing when text is hidden */
    .chat-header .btn i.me-1 {
        margin-right: 0 !important;
    }
    
    /* Messages Container */
    .messages-container {
        padding: 0.5rem;
    }
    
    /* Message Bubbles - Larger on mobile for better readability */
    .message-bubble {
        max-width: 90% !important;
        padding: 0.625rem 0.875rem;
        font-size: 0.9375rem; /* 15px */
        line-height: 1.5;
    }
    
    /* User messages - better mobile layout */
    .user-message {
        border-radius: 1.125rem 1.125rem 0.25rem 1.125rem;
    }
    
    /* Assistant messages */
    .assistant-message {
        border-radius: 1.125rem 1.125rem 1.125rem 0.25rem;
    }
    
    /* Message avatar - slightly smaller on mobile */
    .message-avatar {
        width: 28px;
        height: 28px;
        margin-right: 0.5rem;
    }
    
    /* Message time */
    .message-time {
        font-size: 0.7rem;
    }
    
    /* Message actions - stack vertically if needed */
    .message-actions {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
    }
    
    /* Copy button - larger touch target */
    .message-actions .copy-btn {
        min-width: 40px;
        min-height: 40px;
    }
    
    /* Composer Container */
    .composer-container {
        padding: 0.625rem !important;
        border-top: 2px solid var(--bs-border-color);
    }
    
    /* Message input */
    #messageInput {
        font-size: 1rem; /* Prevent zoom on iOS */
        padding: 0.625rem 0.75rem;
        border-radius: 1.25rem;
    }
    
    /* Send button - larger touch target */
    #sendBtn,
    #stopBtn {
        min-width: 48px !important;
        min-height: 48px !important;
        padding: 0 !important;
        border-radius: 1.25rem !important;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    #sendBtn i,
    #stopBtn i {
        font-size: 1.25rem;
        margin: 0;
    }
    
    /* Character counter - smaller text */
    .composer-container small {
        font-size: 0.75rem;
    }
    
    /* Jump to latest button */
    #jumpToLatest {
        bottom: 70px !important; /* Above composer */
        right: 0.5rem !important;
    }
    
    #jumpToLatest .btn {
        padding: 0.5rem 0.875rem;
        font-size: 0.8125rem;
        min-height: 40px;
    }
    
    /* Code blocks - horizontal scroll */
    .code-block-container {
        margin: 0.5rem -0.5rem; /* Extend to edges */
        border-radius: 0;
        max-width: 100%;
    }
    
    .code-block {
        font-size: 0.75rem !important;
        padding: 0.625rem !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .code-block-header {
        padding: 0.375rem 0.625rem;
        font-size: 0.75rem;
    }
    
    .copy-code-btn {
        padding: 0.25rem 0.375rem;
        font-size: 0.7rem;
        min-width: 36px;
        min-height: 36px;
    }
    
    /* Citations */
    .citations-container {
        font-size: 0.875rem;
        margin-top: 0.75rem !important;
    }
    
    .citations-header h6 {
        font-size: 0.875rem;
    }
    
    .toggle-citations {
        min-width: 36px;
        min-height: 36px;
    }
    
    .citation-item {
        padding: 0.5rem;
        margin-bottom: 0.375rem;
    }
    
    /* Lists in messages */
    .message-list {
        padding-left: 1rem;
        font-size: 0.9375rem;
    }
    
    /* Headers in messages */
    .message-header {
        font-size: 0.9375rem !important;
    }
    
    /* Blockquotes */
    .message-blockquote {
        padding: 0.5rem 0.75rem;
        font-size: 0.9375rem;
        margin-left: -0.25rem;
        margin-right: -0.25rem;
    }
    
    /* Links */
    .message-link {
        word-break: break-word;
    }
    
    /* Empty state */
    #emptyState {
        padding: 2rem 1rem !important;
    }
    
    #emptyState .display-1 {
        font-size: 3rem;
    }
    
    #emptyState h3 {
        font-size: 1.25rem;
    }
    
    #emptyState p {
        font-size: 0.9375rem;
    }
    
    /* Settings Modal */
    .modal-dialog {
        margin: 0.5rem;
    }
    
    .modal-content {
        border-radius: 0.75rem;
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 0.75rem;
    }
    
    /* Toast notifications */
    .toast-container {
        padding: 0.5rem !important;
        width: calc(100% - 1rem);
        left: 0.5rem;
        right: 0.5rem;
    }
    
    .toast {
        max-width: 100%;
        font-size: 0.875rem;
    }
}

/* Small Devices - Phones (landscape) and small tablets (576px - 767.98px) */
@media (min-width: 576px) and (max-width: 767.98px) {
    .chat-header h1 {
        font-size: 1.125rem;
    }
    
    .message-bubble {
        max-width: 85%;
    }
    
    .composer-container {
        padding: 0.75rem;
    }
    
    #messageInput {
        font-size: 1rem;
    }
    
    /* Show button text on landscape phones */
    #newChatBtn,
    #settingsBtn {
        padding: 0.375rem 0.75rem;
    }
}

/* Medium Devices - Tablets (768px - 991.98px) */
@media (min-width: 768px) and (max-width: 991.98px) {
    .message-bubble {
        max-width: 75%;
    }
    
    .composer-container {
        padding: 0.875rem;
    }
    
    /* Sidebar takes less space on tablets */
    .chat-sidebar {
        width: 240px;
    }
}

/* Large Devices - Desktops (992px - 1199.98px) */
@media (min-width: 992px) and (max-width: 1199.98px) {
    .message-bubble {
        max-width: 70%;
    }
}

/* Extra Large Devices - Large Desktops (≥ 1200px) */
@media (min-width: 1200px) {
    .message-bubble {
        max-width: 65%;
    }
    
    /* Center content on very large screens */
    .messages-container {
        max-width: 1400px;
        margin: 0 auto;
    }
}

/* Landscape Orientation Specific */
@media (max-height: 500px) and (orientation: landscape) {
    /* Optimize for landscape mobile */
    .chat-header {
        padding: 0.375rem 0.75rem;
    }
    
    .chat-header h1 {
        font-size: 0.9375rem !important;
    }
    
    .messages-container {
        padding: 0.375rem;
    }
    
    .message-bubble {
        padding: 0.5rem 0.75rem;
        margin-bottom: 0.5rem !important;
    }
    
    .composer-container {
        padding: 0.5rem !important;
    }
    
    /* Sidebar overlay in landscape */
    .chat-sidebar {
        width: 240px;
    }
}

/* Touch Device Optimizations */
@media (hover: none) and (pointer: coarse) {
    /* All interactive elements need larger touch targets */
    .btn {
        min-height: 44px;
        padding: 0.5rem 1rem;
    }
    
    .btn-sm {
        min-height: 40px;
        padding: 0.375rem 0.75rem;
    }
    
    /* Prevent text selection on buttons */
    .btn {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Larger scrollbar for touch */
    .messages-container::-webkit-scrollbar {
        width: 8px;
    }
    
    /* Make code copy button more accessible */
    .copy-code-btn,
    .copy-btn {
        min-width: 44px;
        min-height: 44px;
    }
}

/* Print Styles Enhancement */
@media print {
    .chat-header,
    .chat-sidebar,
    .composer-container,
    #jumpToLatest,
    .sidebar-overlay,
    .toast-container,
    .message-actions {
        display: none !important;
    }
    
    .chat-content {
        height: auto !important;
    }
    
    .message-bubble {
        max-width: 100% !important;
        break-inside: avoid;
        page-break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #000 !important;
        margin-bottom: 1rem !important;
    }
    
    .messages-container {
        overflow: visible !important;
        padding: 0 !important;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    .message-bubble {
        border: 2px solid currentColor !important;
    }
    
    .btn {
        border-width: 2px !important;
    }
    
    .chat-header,
    .composer-container {
        border-width: 2px !important;
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Toast notifications */
.toast {
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

/* Settings modal */
.modal-content {
    border: none;
    border-radius: 1rem;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.modal-header {
    border-bottom: 1px solid var(--bs-border-color);
}

.modal-footer {
    border-top: 1px solid var(--bs-border-color);
}

/* Sidebar animations */
@media (max-width: 768px) {
    .chat-sidebar {
        transform: translateX(-100%);
    }
    
    .chat-sidebar.show {
        transform: translateX(0);
    }
    
    .sidebar-overlay {
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
    }
}

/* Jump to latest button */
#jumpToLatest {
    animation: bounceIn 0.3s ease-out;
}

@keyframes bounceIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Empty state */
#emptyState {
    animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

/* Accessibility improvements */
.btn:focus-visible {
    outline: 2px solid var(--pulse-primary);
    outline-offset: 2px;
}

.form-control:focus-visible {
    outline: 2px solid var(--pulse-primary);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .message-bubble {
        border: 2px solid currentColor;
    }
    
    .btn {
        border-width: 2px;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Citation styles */
.citation-score {
    background-color: var(--pulse-primary);
    color: white;
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-size: 0.75rem;
    font-weight: 500;
}

.citation-confidence {
    background-color: var(--bs-success);
    color: white;
    padding: 0.125rem 0.375rem;
    border-radius: 0.25rem;
    font-size: 0.75rem;
    font-weight: 500;
}

.extracted-citation {
    border-left: 3px solid var(--bs-info);
    background: rgba(13, 202, 240, 0.05);
}

.extracted-citation .citation-title {
    color: var(--bs-info);
    font-weight: 500;
}

/* Citation type indicators */
.citation-item .badge {
    font-size: 0.65rem;
    padding: 0.25rem 0.5rem;
}

/* Enhanced citation preview */
.citation-preview-content .table td:first-child {
    font-weight: 500;
    width: 30%;
}

.citation-preview-content .alert {
    margin-top: 1rem;
    font-size: 0.875rem;
}

/* Print styles */
@media print {
    .chat-header,
    .chat-sidebar,
    .composer-container {
        display: none !important;
    }
    
    .chat-content {
        height: auto !important;
    }
    
    .message-bubble {
        break-inside: avoid;
        box-shadow: none !important;
        border: 1px solid #000 !important;
    }
}