/* Main layout structure */
.chat-page-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 80px); /* Full viewport height minus navbar, with extra buffer */
    background-color: #1a1a1a; /* Dark background for the page */
}

/* Desktop refinements: reduce page side gutters around the chat area */
@media (min-width: 992px) {
    /* Narrow the left/right padding that sits OUTSIDE the message bubbles */
    .chat-page-container { padding-left: 8px; padding-right: 8px; }
    .chat-header { padding-left: 12px; padding-right: 12px; }
    .chat-messages-wrapper { padding-left: 8px; padding-right: 8px; }
    .chat-footer { padding-left: 12px; padding-right: 12px; }
}

/* Sticky Header */
.chat-header {
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 1020; /* Above content */
    background: rgba(27, 22, 45, 0.9);
    backdrop-filter: blur(6px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding: 12px 16px;
}

/* Ensure dropdown menus overlay and do not increase header height */
.chat-header .dropdown { position: relative; }
.chat-header .dropdown-menu {
    position: absolute !important;
    z-index: 1056 !important;
    top: 100% !important;
    right: 0 !important;
    left: auto !important;
}
/* Messages Area */
.chat-messages-wrapper {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1rem;
}

/* Sticky Footer */
.chat-footer {
    flex-shrink: 0;
    position: sticky;
    bottom: 0;
    padding: 1rem 1.5rem;
    background-color: #212529;
    border-top: 1px solid #343a40;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* Spirit Guide Dropdown */
.chat-page-container .spirit-dropdown-menu {
    max-height: 400px;
    overflow-y: auto;
    overflow-x: hidden; /* Prevent horizontal scroll */
    width: 320px;
}

/* Chart Container - responsive sizing for Chart.js */
.chart-container {
    position: relative;
    width: 100%;
    max-width: 900px;
    min-height: 400px;
    height: 400px;
    margin: 1rem auto;
    padding: 1rem;
    background: rgba(30, 30, 40, 0.5);
    border-radius: 12px;
}

.chart-container canvas {
    max-width: 100% !important;
    max-height: 100% !important;
}

/* Mermaid Diagram Container - similar styling */
.mermaid-container {
    position: relative;
    width: 100%;
    max-width: 900px;
    margin: 1rem auto;
    padding: 1rem;
    background: rgba(30, 30, 40, 0.5);
    border-radius: 12px;
    overflow-x: auto;
}

/* Image Upload Preview */
.image-preview-container {
    position: relative;
    padding: 10px;
    background-color: rgba(33, 37, 41, 0.5);
    border-radius: 10px;
    margin-bottom: 10px;
    display: none;
}

/* Thumbnails row shown above the input */
.image-thumbs {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 4px 2px 2px;
    scrollbar-width: thin;
}
.image-thumb-wrap {
    position: relative;
    flex: 0 0 auto;
    width: 72px;
    height: 72px;
}
.image-thumb {
    width: 72px;
    height: 72px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    background-color: #2b2f36;
    display: block;
}

/* Inline row used while persisting images */
.image-loading {
    font-size: 0.85rem;
    color: #adb5bd;
}

/* Ensure input bar stretches full width below the preview */
.chat-footer .input-group { width: 100%; }

.image-preview {
    max-width: 100%;
    max-height: 200px;
    border-radius: 8px;
    display: block;
    margin: 0 auto;
}

.remove-image-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: none;
    border-radius: 50%;
    width: 25px;
    height: 25px;
    font-size: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2;
}
.spirit-dropdown-menu .dropdown-item > div {
    white-space: normal; /* Allow text to wrap */
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Tool Execution Indicators - compact height */
.tool-execution-indicator {
    max-height: 100px;
    overflow: hidden;
    padding: 4px 8px !important;
    margin: 4px 0 !important;
}
.tool-execution-indicator pre {
    max-height: 60px !important;
    overflow: auto !important;
    font-size: 0.75rem !important;
    line-height: 1.2 !important;
    padding: 4px 6px !important;
    margin: 2px 0 !important;
}

/* Ghost Processing Indicator - compact */
.ghost-thinking-indicator {
    max-height: 50px;
    padding: 6px 10px !important;
    margin: 4px 0 !important;
}

/* Tool Result Cards - constrained height */
.tool-result-card pre {
    max-height: 80px !important;
    overflow: auto !important;
}

/* Moltbot Action Cards - COMPACT single-line style */
.moltbot-action-card {
    margin: 4px 0 !important;
}
.moltbot-action-card > div {
    padding: 6px 10px !important;
    margin: 2px 0 !important;
}

/* Tool Output Display - remove excessive whitespace */
.tool-output-container,
.tool-execution-summary {
    padding: 8px 12px !important;
    margin: 4px 0 !important;
    max-height: 150px;
    overflow: auto;
}

/* Code blocks in tool outputs - compact */
.message-content pre:has(code) {
    margin: 4px 0 !important;
    padding: 8px !important;
    max-height: 120px;
    overflow: auto;
}

/* Reduce whitespace around tool-related elements */
.message-content .tool-result-card,
.message-content .moltbot-action-card {
    margin-top: 4px !important;
    margin-bottom: 4px !important;
}

/* Enhanced Audio Controls */
.enhanced-audio-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: rgba(119, 0, 255, 0.651);
    border-radius: 8px;
    border: 1px solid rgba(62, 6, 131, 0.97);
}

.enhanced-audio-controls .btn {
    padding: 0.25rem 0.5rem;
    font-size: 0.875rem;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.enhanced-audio-controls .tts-play-button:hover {
    background-color: #0056b3;
    border-color: #0056b3;
}

.enhanced-audio-controls .tts-pause-button:hover {
    background-color: #5a6268;
    border-color: #545b62;
}

.enhanced-audio-controls .tts-download-button:hover {
    background-color: #138496;
    border-color: #117a8b;
}

.enhanced-audio-controls .audio-status {
    font-style: italic;
    color: #6c757d;
    width: 60px;
}
/* Message Styling */
.chat-page-container .message-group {
    display: flex;
    margin-bottom: 1rem;
    max-width: 90%; /* Keep message bubble width stable; width changes happen outside the bubble */
    align-items: flex-end;
}
.chat-page-container .message-group.user {
    margin-left: auto;
    flex-direction: row-reverse;
}
.chat-page-container .message-group.assistant {
    margin-right: auto;
}
.chat-page-container .message-avatar {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #343a40;
    color: white;
    flex-shrink: 0;
    margin: 0 10px;
    overflow: hidden;
}
.chat-page-container .message-bubble {
    padding: 10px 15px;
    border-radius: 18px;
    word-wrap: break-word;
    overflow-wrap: break-word;
    position: relative;
    max-width: 100%;
    overflow: hidden;
}
.chat-page-container .message-group.user .message-bubble {
    background-color: #007bff;
    color: white;
}
.chat-page-container .message-group.assistant .message-bubble {
    background-color: #495057;
    color: white;
}
.chat-page-container .message-bubble.bg-danger-soft {
    background-color: rgba(220, 53, 69, 0.2);
    color: #dc3545 !important;
}

.chat-page-container .message-content {
    white-space: normal; /* Default: render HTML normally */
    font-size: 1rem;
    line-height: 1.6;
}
/* While streaming, preserve raw newlines for textContent updates */
.chat-page-container .message-content.streaming {
    white-space: pre-wrap;
}

/* Constrain images in chat bubbles */
.chat-page-container .message-content img {
    max-width: 100%;
    max-height: 320px;
    width: auto;
    height: auto;
    object-fit: contain;
    border-radius: 8px;
    display: block;
    margin: 8px 0;
}

/* Grid layout when user attaches multiple images in a single message */
.message-image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
    gap: 8px;
}
.message-image-grid img {
    width: 100%;
    height: 160px;
    object-fit: cover;
}
.chat-page-container .message-content pre code {
    padding: 0;
    font-size: inherit;
    color: inherit;
    background-color: transparent;
    border-radius: 0;
}

/* Improve table readability inside message bubbles */
.chat-page-container .message-content .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0.5rem 0;
}

.chat-page-container .message-content table {
    color: #ffffff; /* Ensure high contrast text */
    --bs-table-color: #ffffff;
    --bs-table-bg: transparent;
    --bs-table-border-color: rgba(255,255,255,0.2);
    border-collapse: collapse;
    width: 100%;
    max-width: 100%;
    font-size: 0.9rem;
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 4px;
    overflow: hidden;
}

.chat-page-container .message-content table thead {
    background-color: rgba(66, 135, 245, 0.3); /* Blue tint for headers */
    border-bottom: 2px solid rgba(255,255,255,0.3);
}

.chat-page-container .message-content table td,
.chat-page-container .message-content table th {
    border: 1px solid rgba(255,255,255,0.15);
    padding: 0.5rem 0.75rem;
    text-align: left;
    vertical-align: top;
}

.chat-page-container .message-content table th {
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.85rem;
    letter-spacing: 0.5px;
}
.chat-page-container .message-content h1,
.chat-page-container .message-content h2,
.chat-page-container .message-content h3,
.chat-page-container .message-content h4 {
    font-weight: 600;
    line-height: 1.3;
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid rgba(255,255,255,0.15);
    padding-bottom: 0.4rem;
}
.chat-page-container .message-content h1 { font-size: 1.8rem; }
.chat-page-container .message-content h2 { font-size: 1.5rem; }
.chat-page-container .message-content h3 { font-size: 1.2rem; }
.chat-page-container .message-content h4 { font-size: 1.05rem; border-bottom: none; }

/* Better spacing for lists and paragraphs */
.chat-page-container .message-content p {
    margin-bottom: 1rem;
    line-height: 1.6;
}

.chat-page-container .message-content ul,
.chat-page-container .message-content ol {
    padding-left: 1.5rem;
    margin-bottom: 1rem;
}

.chat-page-container .message-content li {
    margin-bottom: 0.5rem;
    line-height: 1.6;
}

.chat-page-container .message-content strong,
.chat-page-container .message-content b {
    color: #ffffff;
    font-weight: 600;
}

.chat-page-container .message-content hr {
    border: none;
    border-top: 2px solid rgba(255,255,255,0.25);
    margin: 2rem 0;
}

/* Text size utilities for chat */
.chat-page-container.text-size-small .message-content { font-size: 0.95rem; }
.chat-page-container.text-size-medium .message-content { font-size: 1rem; }
.chat-page-container.text-size-large .message-content { font-size: 1.125rem; }
/* Zebra striping for better readability */
.chat-page-container .message-content table tbody tr:nth-child(odd) {
    background-color: rgba(255,255,255,0.08);
}
.chat-page-container .message-content table tbody tr:nth-child(even) {
    background-color: rgba(255,255,255,0.03);
}

/* Hover effect for table rows */
.chat-page-container .message-content table tbody tr:hover {
    background-color: rgba(66, 135, 245, 0.15);
}

.chat-page-container .message-meta {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-top: 5px;
    font-size: 0.75rem;
    color: #adb5bd;
}
.chat-page-container .message-group.user .message-meta { justify-content: flex-end; }



.chat-page-container .message-input { resize: none; }

/* Typing Indicator */
.chat-page-container #typing-indicator { display: none; }
.chat-page-container .typing-dot { height: 8px; width: 8px; background-color: #6c757d; border-radius: 50%; display: inline-block; margin: 0 2px; animation: typing-animation 1.4s infinite; }
.chat-page-container .typing-dot:nth-child(2) { animation-delay: 0.2s; }
.chat-page-container .typing-dot:nth-child(3) { animation-delay: 0.4s; }

/* Stop Button for Typing Indicator */
.chat-page-container .stop-generating-btn {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    background: rgba(220, 53, 69, 0.8);
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    color: white;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
}

.chat-page-container .message-bubble:hover .stop-generating-btn {
    opacity: 1;
}

.chat-page-container .stop-generating-btn:hover {
    background: rgba(220, 53, 69, 1);
    transform: translateY(-50%) scale(1.1);
}

/* Stop state for main send button */
.chat-page-container #send-button.btn-stop-stream {
    background: rgba(220, 53, 69, 0.9) !important;
    border-color: rgba(220, 53, 69, 0.9) !important;
    animation: pulse-stop 1.5s ease-in-out infinite;
}

.chat-page-container #send-button.btn-stop-stream:hover {
    background: rgba(220, 53, 69, 1) !important;
    border-color: rgba(220, 53, 69, 1) !important;
    transform: scale(1.05);
}

@keyframes pulse-stop {
    0%, 100% { opacity: 0.9; }
    50% { opacity: 1; }
}

.chat-page-container .message-bubble {
    position: relative;
}

/* Spirit Guides dropdown: light purple styling */
#spiritGuideDropdown.btn.btn-outline-secondary {
    color: #d6c9ff;
    border-color: #b388ff;
    background-color: transparent;
}
#spiritGuideDropdown.btn.btn-outline-secondary:hover,
#spiritGuideDropdown.btn.btn-outline-secondary:focus {
    color: #ffffff;
    border-color: #c19bff;
    background-color: rgba(179, 136, 255, 0.12);
    box-shadow: 0 0 0 0.2rem rgba(179, 136, 255, 0.25);
}
.spirit-dropdown-menu {
    background-color: #1f1b2e;
    border: 1px solid #b388ff;
}
.spirit-dropdown-menu .dropdown-header {
    color: #cdb7ff;
}
.spirit-dropdown-menu .dropdown-item {
    color: #e6e0ff;
}
.spirit-dropdown-menu .dropdown-item:hover,
.spirit-dropdown-menu .dropdown-item.active {
    background-color: rgba(179, 136, 255, 0.18);
    color: #ffffff;
}
@keyframes typing-animation { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }

/* Feedback buttons styling */
.message-feedback {
    display: flex;
    gap: 10px;
    margin-left: 10px;
}

/* Enhanced audio controls inline with timestamp */
.enhanced-audio-controls {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: nowrap;
}

.enhanced-audio-controls .btn {
    font-size: 12px;
    padding: 4px 8px;
}

.action-button {
    background: none;
    border: none;
    color: #9ca3af;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s ease;
}

.action-button:hover {
    background-color: rgba(156, 163, 175, 0.2);
    color: #d1d5db;
}

.copy-message-btn:active {
    color: #28a745;
}

.action-button.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* TTS Button Styling */
.tts-button {
    background: none;
    border: none;
    color: #ced4da; /* Lighter gray for better visibility */
    cursor: pointer;
    padding: 0 5px;
    margin-left: 8px;
    font-size: 1.6rem; /* Significantly larger icon */
    opacity: 0.75; /* More visible by default */
    transition: all 0.2s ease-in-out; /* Transition all properties */
    vertical-align: middle;
}

.tts-button:hover {
    opacity: 1;
    color: #ffffff;
    transform: scale(1.15); /* Pop effect on hover */
}

/* Auto Speech Toggle Styles */
.auto-speech-toggle {
    font-size: 0.875rem;
    padding: 0.375rem 0.75rem;
    border-radius: 0.375rem;
    transition: all 0.2s ease-in-out;
    white-space: nowrap;
}

.auto-speech-toggle.active {
    background-color: #198754;
    border-color: #198754;
    color: white;
}

.auto-speech-toggle.active:hover {
    background-color: #157347;
    border-color: #146c43;
}

.auto-speech-toggle:not(.active) {
    background-color: transparent;
    border-color: #6c757d;
    color: #6c757d;
}

.auto-speech-toggle:not(.active):hover {
    background-color: #198754;
    border-color: #198754;
    color: white;
}



/* Mobile-first refinements */
@media (max-width: 576px) {
    /* Use small viewport height to account for mobile browser UI */
    .chat-page-container {
        height: 100svh;
    }
    
    /* MOBILE: Make header compact and collapsible instead of completely hidden */
    .chat-header {
        padding: 8px 12px;
        min-height: 48px;
        transition: transform 0.3s ease;
    }
    
    /* Hide header on scroll down */
    .chat-header.hidden {
        transform: translateY(-100%);
    }
    
    /* Stack header controls vertically for better touch targets */
    .chat-header .d-flex {
        flex-wrap: wrap;
        gap: 8px;
        row-gap: 6px;
    }
    
    /* Hide conversation title on mobile to save space */
    .chat-header h4,
    .chat-header #conversation-title-input {
        display: none !important;
    }
    
    /* Model selector: show icon only on mobile */
    #modelSelectorDropdown .model-name-text {
        display: none;
    }
    #modelSelectorDropdown {
        padding: 0.35rem 0.6rem;
        font-size: 0.875rem;
    }
    
    /* Hide auto speech toggle label on mobile */
    .auto-speech-toggle .auto-speech-text {
        display: none;
    }
    .auto-speech-toggle {
        padding: 0.35rem 0.6rem;
    }
    
    /* Hide spirit guide label and make dropdown compact */
    #current-spirit-label {
        display: none;
    }
    
    #spiritGuideDropdown {
        font-size: 0.85rem;
        padding: 0.35rem 0.6rem;
    }
    
    /* Compact all header buttons */
    .chat-header .btn { 
        padding: 0.35rem 0.6rem; 
        font-size: 0.85rem; 
    }
    
    /* Improve dropdown positioning on mobile */
    .chat-header .dropdown-menu {
        max-width: 90vw !important;
        font-size: 0.9rem;
    }
    
    /* MAJOR FIX: Increase messages area space */
    .chat-messages-wrapper {
        padding: 0.5rem;
        /* This gives more room for messages */
        flex-grow: 1;
        overflow-y: auto;
    }
    
    /* Hide mic button on small screens; mobile keyboards handle voice */
    .chat-footer #mic-button { display: none !important; }
    
    /* Make the footer respect safe areas and remain visible */
    .chat-footer {
        padding: 0.75rem;
        padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
    }
    
    /* Compact preview container & thumbs on mobile */
    .image-preview-container { padding: 8px; }
    .image-thumbs { gap: 6px; }
    .image-thumb-wrap { width: 56px; height: 56px; }
    .image-thumb { width: 56px; height: 56px; }
    
    /* MAJOR FIX: Give messages more width on small screens */
    .chat-page-container .message-group {
        max-width: 95%;
    }
    
    /* Narrower readable measure for message text on small screens */
    .message-group .message-bubble .message-content {
        max-width: 100%;
        line-height: 1.5;
        font-size: 0.95rem;
    }
    
    /* Ensure audio controls sit below bubble and align nicely */
    .enhanced-audio-controls { margin-left: 0; flex-wrap: wrap; }
    
    /* Prevent table columns from getting squished; allow horizontal scroll */
    .chat-page-container .message-content table { display: block; overflow-x: auto; white-space: nowrap; }
}

/* ============================================= */
/* == Custom Table Styles for SpiritBox Theme == */
/* ============================================= */

/* Target ALL tables generated by Markdown inside a message bubble */
.message-content table {
    width: 100%;
    table-layout: auto;
    border-collapse: collapse;
    border-spacing: 0;
    margin: 1rem 0;
    background-color: #1e1e1e;
    border: 1px solid #4a4a4a;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* Strong header styling */
.message-content table thead,
.message-content table th {
    background: linear-gradient(135deg, rgba(66, 135, 245, 0.4), rgba(88, 101, 242, 0.4)) !important;
    color: #ffffff !important;
    font-weight: 700 !important;
    font-size: 0.95rem !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    padding: 12px 16px !important;
    border-bottom: 3px solid rgba(66, 135, 245, 0.6) !important;
    text-align: left !important;
}

/* All table cells */
.message-content table td,
.message-content table th {
    padding: 10px 14px !important;
    border: 1px solid rgba(255, 255, 255, 0.12) !important;
    vertical-align: top;
    color: #e8e8e8;
}

/* Body cells specific styling */
.message-content table tbody td {
    background-color: rgba(30, 30, 30, 0.6);
    color: #e0e0e0 !important; /* Light gray text for readability */
    font-size: 0.95rem;
}

/* Zebra striping */
.message-content table tbody tr:nth-child(odd) {
    background-color: rgba(255, 255, 255, 0.05);
}

.message-content table tbody tr:nth-child(even) {
    background-color: rgba(255, 255, 255, 0.02);
}

/* Hover effect */
.message-content table tbody tr:hover {
    background-color: rgba(66, 135, 245, 0.15) !important;
    transition: background-color 0.2s ease;
}

.message-content table tbody tr:hover td {
    color: #ffffff !important; /* Bright white on hover for maximum contrast */
}

/* Right border for cells except last in row */
.message-content table td:not(:last-child),
.message-content table th:not(:last-child) {
    border-right: 1px solid rgba(255, 255, 255, 0.15);
}

/* ============================================
   CODE BLOCK STYLING - Production Quality
   ============================================ */

/* Code block container */
.message-content pre {
    background: #282c34 !important;
    border-radius: 8px;
    margin: 1rem 0;
    padding: 0;
    overflow: hidden;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
    position: relative;
}

/* Code content inside pre */
.message-content pre code {
    display: block;
    padding: 1.25rem;
    overflow-x: auto;
    font-family: 'Fira Code', 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    color: #abb2bf;
    background: transparent !important;
}

/* Language badge (top-right corner) - Simplified using data-language */
.message-content pre code[data-language]::before {
    content: attr(data-language);
    position: absolute;
    top: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.3);
    color: #61afef;
    padding: 0.25rem 0.75rem;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: lowercase;
    border-bottom-left-radius: 4px;
    font-family: 'Segoe UI', system-ui, sans-serif;
}

/* Inline code (not in pre blocks) */
.message-content code:not(pre code) {
    background: rgba(110, 118, 129, 0.25);
    color: #e06c75;
    padding: 0.2em 0.4em;
    border-radius: 3px;
    font-family: 'Fira Code', 'Consolas', 'Monaco', monospace;
    font-size: 0.9em;
}

/* Scrollbar for code blocks */
.message-content pre code::-webkit-scrollbar {
    height: 8px;
}

.message-content pre code::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 4px;
}

.message-content pre code::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
}

.message-content pre code::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* Code block wrapper and copy button styling */
.code-block-wrapper {
    position: relative;
    margin: 1rem 0;
}

.copy-code-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    padding: 0.375rem 0.75rem;
    background: rgba(76, 29, 149, 0.85);
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s ease;
    z-index: 10;
    opacity: 0.7;
}

.copy-code-btn:hover {
    opacity: 1;
    background: rgba(76, 29, 149, 1);
    transform: translateY(-1px);
}

.copy-code-btn.copied {
    background: #28a745;
    opacity: 1;
}

.copy-code-btn i {
    margin-right: 0.25rem;
}

/* Adjust pre to accommodate copy button */
.code-block-wrapper pre {
    margin: 0;
    padding-top: 3rem; /* Space for copy button */
}

/* =============================================================
   COVERSCREEN FAB & BOTTOM SHEET
   =============================================================
   Floating Action Button and Bottom Sheet for coverscreen-only controls
   ============================================================= */

/* Hide by default on all devices */
.coverscreen-fab,
.coverscreen-sheet {
    display: none;
}

/* =============================================================
   Z-FLIP 5 COVERSCREEN FOCUS MODE
   =============================================================
   This media query targets ONLY the Z-Flip 5 coverscreen.
   
   Dimensions:
   - Keyboard closed: 720x748 (aspect ~0.96)
   - Keyboard open: 720x~400 (aspect ~1.8)
   
   Strategy: Use height constraints to differentiate from phones/tablets
   - Max width 760px (catches coverscreen)
   - Max height 800px (excludes tall phones/tablets)
   - This catches both keyboard states without affecting other devices
   
   CRITICAL: Uses maximum CSS specificity to override ALL other rules.
   ============================================================= */

@media screen and (max-width: 760px) and (max-height: 800px) {
    
    /* Show FAB only on coverscreen */
    .coverscreen-fab {
        display: flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        bottom: 90px; /* Above chat footer */
        right: 20px;
        width: 56px;
        height: 56px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border: none;
        border-radius: 50%;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        cursor: pointer;
        z-index: 1000;
        transition: all 0.3s ease;
    }
    
    .coverscreen-fab:hover {
        transform: scale(1.1);
        box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
    }
    
    .coverscreen-fab:active {
        transform: scale(0.95);
    }
    
    .coverscreen-fab i {
        font-size: 1.5rem;
    }
    
    /* Bottom Sheet Styles */
    .coverscreen-sheet {
        display: block; /* Changed from none - controlled by transform instead */
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.5);
        z-index: 2000;
        max-height: 80vh;
        overflow-y: auto;
        transform: translateY(100%); /* Hidden by default */
        transition: transform 0.3s ease-in-out;
    }
    
    .coverscreen-sheet.open {
        transform: translateY(0); /* Slides up */
    }
    
    .coverscreen-sheet-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem 1.5rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    .coverscreen-sheet-header h5 {
        margin: 0;
        color: white;
        font-weight: 600;
    }
    
    .coverscreen-sheet-content {
        padding: 1.5rem;
    }
    
    .coverscreen-sheet .form-label {
        font-weight: 600;
        margin-bottom: 0.5rem;
    }
    
    .coverscreen-sheet .form-select {
        background-color: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(255, 255, 255, 0.2);
        color: white;
    }
    
    .coverscreen-sheet .form-select:focus {
        background-color: rgba(255, 255, 255, 0.15);
        border-color: #667eea;
        box-shadow: 0 0 0 0.25rem rgba(102, 126, 234, 0.25);
        color: white;
    }
    
    .coverscreen-sheet .form-select option {
        background-color: #1a1a2e;
        color: white;
    }
    
    .coverscreen-sheet .form-check-input {
        cursor: pointer;
    }
    
    .coverscreen-sheet .form-check-label {
        cursor: pointer;
    }

    /* 1. NUCLEAR NAVBAR HIDING
     * Using every possible selector combination with maximum specificity.
     * This MUST override navbar-consolidated.css and all Bootstrap defaults.
     */
    body #sidebar-nav,
    body .sidebar,
    body .main-sidebar,
    body .navbar,
    body nav.navbar,
    body #SpiritBoxNavbar,
    body nav#SpiritBoxNavbar,
    body nav#SpiritBoxNavbar.navbar,
    body nav#SpiritBoxNavbar.navbar-expand-lg,
    body nav.navbar-expand-lg,
    body .spirit-gradient-navbar,
    body nav.spirit-gradient-navbar,
    body #SpiritBoxNavbar.spirit-gradient-navbar,
    body .chat-header,
    body .navbar-dark,
    body nav.navbar-dark {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        height: 0 !important;
        min-height: 0 !important;
        max-height: 0 !important;
        width: 0 !important;
        min-width: 0 !important;
        max-width: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        overflow: hidden !important;
        position: absolute !important;
        top: -99999px !important;
        left: -99999px !important;
        z-index: -9999 !important;
        pointer-events: none !important;
        clip: rect(0,0,0,0) !important;
        clip-path: inset(100%) !important;
    }

    /* 2. Make the chat container fill the whole screen
     * (to counteract the missing sidebar/headers)
     */
    .chat-page-container {
        margin-left: 0 !important;
        width: 100% !important;
        height: 100vh; /* Fill the full viewport height */
        padding: 0 !important;
    }

    /* 3. Adjust message wrapper padding
     * (since ALL headers are gone, we just need a tiny bit of top padding)
     */
    #chat-messages-wrapper {
        padding-top: 5px !important;
        
        /* Make padding-bottom large enough to scroll the last message
         * above the footer + browser nav bar.
         * (Footer height + 80px dead space)
         */
        padding-bottom: 140px !important;
    }

    /* 4. Adjust the footer
     * We'll add 80px of "dead space" (via the `bottom` property)
     * to push it up above the browser's bottom navigation buttons.
     */
    .chat-footer {
        bottom: 80px !important; /* Pushes it 80px up from the very bottom */
        margin-left: 0 !important;
        width: 100% !important;
    }
    
    /* 5. Ensure body and html take full viewport */
    html, body {
        margin: 0 !important;
        padding: 0 !important;
        padding-top: 0 !important; /* Override base.html padding-top: 75px */
        height: 100% !important;
        overflow-x: hidden !important;
    }
    
    /* Remove padding from body that's added for navbar */
    body.spirit-theme {
        padding-top: 0 !important;
    }
    
    /* 6. Remove any container constraints */
    .container,
    .container-fluid {
        max-width: 100% !important;
    }
    
    /* Hide cockpit on mobile */
    .project-cockpit,
    .cockpit-toggle {
        display: none !important;
    }
}

/* ==========================================================================
   CONTEXT SENTRY - Molt Counter
   ========================================================================== */
.context-sentry {
    position: sticky;
    top: 60px;
    z-index: 1010;
    background: rgba(27, 22, 45, 0.95);
    backdrop-filter: blur(8px);
    padding: 8px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid rgba(187, 134, 252, 0.2);
}

.sentry-bar {
    flex: 1;
    height: 6px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 3px;
    overflow: hidden;
}

.sentry-fill {
    height: 100%;
    background: linear-gradient(90deg, #03dac6, #BB86FC);
    border-radius: 3px;
    transition: width 0.5s ease;
}

.sentry-fill.warning {
    background: linear-gradient(90deg, #ff9800, #f44336);
}

.sentry-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    min-width: 80px;
}

.sentry-count {
    font-size: 0.9rem;
    font-weight: 600;
    color: #BB86FC;
}

.sentry-label {
    font-size: 0.65rem;
    color: rgba(255, 255, 255, 0.5);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.molt-warning {
    position: absolute;
    right: 16px;
    background: rgba(255, 152, 0, 0.2);
    color: #ff9800;
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 0.75rem;
    animation: molt-pulse 1.5s ease-in-out infinite;
}

@keyframes molt-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* ==========================================================================
   PROJECT COCKPIT SIDEBAR
   ========================================================================== */
.cockpit-toggle {
    position: fixed;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1050;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, #BB86FC, #03dac6);
    border: none;
    color: #121212;
    font-size: 1.2rem;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(187, 134, 252, 0.4);
    transition: all 0.3s ease;
}

.cockpit-toggle:hover {
    transform: translateY(-50%) scale(1.1);
    box-shadow: 0 6px 30px rgba(187, 134, 252, 0.6);
}

.project-cockpit {
    position: fixed;
    right: -320px;
    top: 0;
    width: 300px;
    height: 100vh;
    background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%);
    z-index: 1100;
    transition: right 0.3s ease;
    box-shadow: -4px 0 30px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
}

.project-cockpit.open {
    right: 0;
}

.cockpit-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    background: rgba(187, 134, 252, 0.1);
    border-bottom: 1px solid rgba(187, 134, 252, 0.2);
}

.cockpit-header h5 {
    margin: 0;
    color: #BB86FC;
    font-size: 1rem;
}

.cockpit-content {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.cockpit-section {
    margin-bottom: 20px;
}

.cockpit-section h6 {
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.7);
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.cockpit-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.cockpit-item {
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    padding: 10px 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    border-left: 3px solid transparent;
}

.cockpit-item:hover {
    background: rgba(187, 134, 252, 0.1);
    border-left-color: #BB86FC;
}

.cockpit-item.task {
    border-left-color: #ff9800;
}

.cockpit-item.file {
    border-left-color: #03dac6;
}

.cockpit-item-title {
    font-size: 0.85rem;
    color: #ffffff;
    margin-bottom: 2px;
}

.cockpit-item-meta {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.5);
}

.cockpit-empty {
    color: rgba(255, 255, 255, 0.3);
    font-size: 0.8rem;
    font-style: italic;
    text-align: center;
    padding: 20px;
}

.cockpit-summary {
    background: rgba(187, 134, 252, 0.05);
    border-radius: 8px;
    padding: 12px;
    border: 1px solid rgba(187, 134, 252, 0.1);
}

.cockpit-summary p {
    margin: 0;
    font-size: 0.8rem;
    line-height: 1.5;
}

/* Cockpit overlay for mobile */
.cockpit-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1090;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.cockpit-overlay.show {
    opacity: 1;
    visibility: visible;
}

/* Inject Button Styles */
.inject-btn {
    background: linear-gradient(135deg, #BB86FC, #03dac6);
    border: none;
    color: #121212;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    transition: all 0.2s ease;
    margin-left: auto;
}

.inject-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 2px 10px rgba(187, 134, 252, 0.4);
}

.inject-btn-sm {
    background: rgba(187, 134, 252, 0.2);
    border: none;
    color: #BB86FC;
    width: 20px;
    height: 20px;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6rem;
    transition: all 0.2s ease;
}

.inject-btn-sm:hover {
    background: #BB86FC;
    color: #121212;
}

.cockpit-item-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 6px;
}

.cockpit-item-meta .badge {
    font-size: 0.65rem;
    padding: 2px 6px;
}

.cockpit-item-meta .similarity {
    font-size: 0.65rem;
    color: #03dac6;
}

.memory-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.memory-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 6px;
}

.memory-type {
    font-size: 0.6rem;
    text-transform: uppercase;
    color: #BB86FC;
    background: rgba(187, 134, 252, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
}

.memory-text {
    flex: 1;
    font-size: 0.75rem;
    color: rgba(255, 255, 255, 0.7);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Cockpit Item Styles */
.cockpit-item {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.03);
    border-radius: 8px;
    margin-bottom: 8px;
    transition: all 0.2s ease;
    cursor: pointer;
}

.cockpit-item:hover {
    background: rgba(187, 134, 252, 0.1);
    transform: translateX(2px);
}

.cockpit-item-icon {
    width: 24px;
    text-align: center;
    flex-shrink: 0;
}

.cockpit-item-content {
    flex: 1;
    min-width: 0;
}

.cockpit-item-title {
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.9);
    line-height: 1.3;
    word-break: break-word;
}

.cockpit-item.task .cockpit-item-title {
    font-weight: 500;
}

.cockpit-item.file .cockpit-item-title {
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 0.75rem;
}

.cockpit-empty {
    text-align: center;
    color: rgba(255, 255, 255, 0.4);
    font-size: 0.8rem;
    padding: 1rem;
    font-style: italic;
}

/* Style for hovering over rows */
