/* Page-specific styles */

/* Chat list page */
.chat-list {
    list-style: none;
    padding: 0;
}

.chat-item {
    position: relative;
    padding: 15px;
    border-bottom: 1px solid var(--border-medium);
    cursor: pointer;
    transition: background-color 0.2s ease;
    /* NEW: give a baseline left border so source colors are visible */
    border-left: 4px solid transparent;
    padding-left: 11px; /* account for the 4px left border */
}

.chat-item:hover {
    background-color: var(--bg-hover); /* was: #f7fafc */
}

/* --- START: New Style for Active Chat Item --- */
.chat-item.active {
    background-color: var(--btn-secondary-bg);
    border-left: 4px solid var(--text-brand);
    padding-left: 11px; /* Adjust padding to account for border */
}

.chat-item.active .chat-title {
    color: var(--text-brand);
    font-weight: 700;
}
/* --- END: New Style for Active Chat Item --- */

.chat-title {
    font-weight: 600;
    font-size: 16px;
    margin-bottom: 5px;
    color: var(--text-primary); /* was: #2d3748 */
}

.chat-meta {
    display: flex;
    gap: 15px;
    font-size: 14px;
    color: var(--text-muted); /* was: #718096 */
}

.chat-date {
    font-size: 12px;
    color: var(--text-light); /* was: #a0aec0 */
}

/* Three-dot menu styles */
.chat-item-menu {
    position: absolute;
    top: 50%;
    right: 15px;
    transform: translateY(-50%);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    z-index: 10;
}

.chat-item:hover .chat-item-menu {
    opacity: 1;
    visibility: visible;
}

.menu-button {
    background: none;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-muted); /* was: #718096 */
    transition: background-color 0.2s ease, color 0.2s ease;
}

.menu-button:hover {
    background-color: var(--btn-secondary-bg); /* was: #e2e8f0 */
    color: var(--text-secondary); /* was: #4a5568 */
}

.menu-button i {
    font-size: 16px;
}

/* Dropdown menu styles */
.dropdown-menu {
    position: absolute;
    top: 0;
    right: 100%;
    margin-right: 8px;
    background: var(--bg-container); /* was: white */
    border: 1px solid var(--border-light); /* was: #e2e8f0 */
    border-radius: 6px;
    box-shadow: var(--shadow-dropdown); /* was: 0 4px 12px rgba(0, 0, 0, 0.15) */
    min-width: 120px;
    opacity: 0;
    visibility: hidden;
    transform: translateX(10px);
    transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
    z-index: 100;
}

/* Show dropdown on menu button hover */
.menu-button:hover + .dropdown-menu,
.dropdown-menu:hover {
    opacity: 1;
    visibility: visible;
    transform: translateX(0);
}

.dropdown-item {
    display: block;
    width: 100%;
    padding: 8px 12px;
    border: none;
    background: none;
    text-align: left;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary); /* was: #4a5568 */
    transition: background-color 0.2s ease;
}

.dropdown-item:hover {
    background-color: var(--bg-hover); /* was: #f7fafc */
}

.dropdown-item:first-child {
    border-radius: 6px 6px 0 0;
}

.dropdown-item:last-child {
    border-radius: 0 0 6px 6px;
}

.dropdown-item i {
    margin-right: 8px;
    width: 16px;
    color: var(--text-muted); /* was: #718096 */
}

/* Chat message page */
.chat-message-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    /* NEW: allow child flex items to shrink properly */
    min-width: 0;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid var(--border-light); /* was: #e2e8f0 */
    /* NEW: allow wrapping when narrow */
    flex-wrap: wrap;
    row-gap: 8px;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 15px;
    /* NEW: let title area shrink and truncate */
    flex: 1 1 auto;
    min-width: 0;
}

/* NEW: truncate long titles globally */
.chat-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-header-right {
    display: flex;
    gap: 10px;
    position: relative;
    align-items: center;
    /* NEW: keep buttons inside the box and wrap to next line if needed */
    flex: 0 1 auto;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: flex-end;
    margin-left: auto;
    max-width: 100%;
}

/* NEW: prevent buttons from growing oddly; keep single-line each */
.chat-header-right .btn,
.chat-header-right .btn-close-view {
    flex: 0 0 auto;
    white-space: nowrap;
}

/* Result display */
#result {
    margin-top: 20px;
    padding: 20px;
    background-color: var(--bg-light); /* was: #f7fafc */
    border-radius: 8px;
    border: 1px solid var(--border-light); /* was: #e2e8f0 */
}

.disclaimer {
    margin-top: 20px;
    padding: 15px;
    background-color: var(--bg-flash-error); /* was: #fff5f5 */
    border-left: 4px solid var(--text-danger); /* was: #f56565 */
    border-radius: 4px;
    font-size: 14px;
    color: var(--text-danger); /* was: #c53030 */
}

/* Loading indicator for infinite scroll */
.loading-indicator {
    text-align: center;
    padding: 20px;
    margin: 20px 0;
}

.loading-indicator .loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    color: var(--text-muted);
    font-size: 14px;
}

.loading-indicator .spinner {
    border: 3px solid var(--border-light);
    border-top: 3px solid var(--btn-primary-bg);
    border-radius: 50%;
    width: 24px;
    height: 24px;
    animation: spin 1s linear infinite;
}

/* Notification banner styles */
.notification-banner {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    text-align: center;
    font-weight: 600;
    z-index: 1200; /* Increased from 1000 to be higher than the navbar's 1100 */
    transition: all 0.3s ease;
    border-radius: 8px;
    max-width: fit-content;
    min-width: 200px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.notification-banner.success {
    background-color: var(--bg-flash-success);
    color: var(--text-success);
    border-bottom: 2px solid var(--border-success);
}

.notification-banner.error {
    background-color: var(--bg-flash-error);
    color: var(--text-error);
    border-bottom: 2px solid var(--border-error);
}

.notification-banner.info {
    background-color: var(--bg-flash-info);
    color: var(--text-info);
    border-bottom: 2px solid var(--border-info);
}

/* --- START: New Single Page App Layout Styles --- */
#main-view-container {
    display: flex;
    flex-grow: 1; /* Allow this container to fill the main content area */
    overflow: hidden; /* Prevent this container from scrolling */
}

/* --- START: MODIFIED - New Generic Navigation Panel --- */
#navigation-panel {
    flex: 1; /* Flex ratio is now on the parent */
    min-width: 300px; /* Minimum width is on the parent */
    
    display: flex; /* It's a flex container for its children */
    flex-direction: column;
    
    border-right: 1px solid var(--border-light);
    background-color: var(--bg-light);
    position: relative; /* For positioning floating buttons like "scroll-to-active" */
}

#chat-list-panel {
    /* The panel itself now just needs to fill its parent */
    display: flex;
    flex-direction: column;
    padding: 15px;
    flex-grow: 1; /* Ensure it fills the navigation-panel */
    overflow: hidden; /* Prevent double scrollbars */
}
/* --- END: MODIFIED --- */

/* --- START: New "Scroll to Active" Button --- */
#scroll-to-active-btn {
    position: absolute;
    bottom: 20px;
    right: 20px;
    z-index: 100;

    /* Button appearance */
    background-color: var(--btn-primary-bg);
    color: var(--btn-primary-text);
    border: none;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    font-size: 20px;
    box-shadow: var(--shadow-medium);
    cursor: pointer;
    
    /* Center the icon */
    display: flex;
    align-items: center;
    justify-content: center;

    /* Hide by default with a smooth transition */
    opacity: 0;
    visibility: hidden;
    transform: scale(0.8);
    transition: all 0.3s ease;
}

#scroll-to-active-btn.visible {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}

#scroll-to-active-btn:hover {
    background-color: var(--btn-primary-hover);
    transform: scale(1.1);
}
/* --- END: New "Scroll to Active" Button --- */

#chat-list-panel .controls {
    flex-shrink: 0;
    /* New Flexbox styles for alignment */
    display: flex;
    flex-direction: column;
    gap: 10px; /* Space between search box and button */
}

#chat-list-panel .controls .form-control {
    padding-right: 35px; /* Make space for the button */
}

/* --- START: Search Clear Button Styles (Consolidated & Refined) --- */
.search-wrapper {
    position: relative;
    display: flex;
    align-items: center;
}

.clear-search-btn {
    position: absolute;
    right: 5px; /* Give it some padding from the edge */
    top: 50%;
    transform: translateY(-50%); /* Vertically center the button */
    
    /* Shape and Size */
    width: 24px;
    height: 24px;
    /* --- MODIFIED: Change to a circle for a more modern, clickable shape --- */
    border-radius: 50%; /* Was: 4px */
    
    /* --- MODIFIED: Use theme variables for color --- */
    background-color: var(--bg-hover);
    color: var(--text-muted);
    
    /* Icon Styling */
    font-size: 16px;
    line-height: 24px; /* Center the '×' inside the button */
    text-align: center;

    /* General */
    border: 1px solid var(--border-light);
    cursor: pointer;
    padding: 0;
    
    /* --- MODIFIED: Use opacity for smooth transitions --- */
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
}

/* --- NEW: Style for when the button is visible --- */
.clear-search-btn.visible {
    opacity: 1;
    visibility: visible;
}


.clear-search-btn:hover {
    background-color: var(--btn-secondary-hover); /* Darken on hover */
    color: var(--text-primary);
}
/* --- END: Search Clear Button Styles --- */

#chat-list-panel .chat-list {
    flex-grow: 1;
    overflow-y: auto;
    margin: 0 -15px; /* Extend to panel edges */
    padding: 0 15px;
}

#working-area {
    flex: 3; /* Changed from 1 to a 3:1 ratio with the chat list */
    display: flex;
    overflow-x: auto;
    /* --- START: MODIFIED - Harmonize spacing --- */
    padding: 15px; /* Was 10px. Now matches chat-list-panel's padding. */
    gap: 15px;     /* Was 10px. Creates a consistent rhythm with the padding. */
    /* --- END: MODIFIED --- */
    background-color: var(--bg-body);
}

#working-area-placeholder {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: var(--text-muted);
    text-align: center;
}

#working-area-placeholder i {
    font-size: 48px;
    margin-bottom: 20px;
}

.chat-view-instance {
    flex: 1 0 500px; /* Base width, allows multiple views */
    min-width: 400px; /* Minimum width before horizontal scroll */
    max-width: 1200px; /* Max width for a single view */
    /* height: 100%; */ /* REMOVED: This was causing the overflow due to parent padding */
    display: flex;
    flex-direction: column;
    border: 1px solid var(--border-medium);
    border-radius: 8px;
    background-color: var(--bg-container);
    overflow: hidden;
}

.chat-view-instance .chat-message-container {
    overflow-y: auto;
    flex-grow: 1;
    position: relative; /* For absolute positioning of close button */
}

/* --- START: New styles for multiple chat views --- */
/* Removed duplicate .chat-header-right and .btn-close-view rules here to avoid conflicts */
/* --- END: New styles for multiple chat views --- */

/* --- END: New Single Page App Layout Styles --- */

/* Fixed Message Navigation Button */
.message-nav-button {
    position: fixed;
    right: 30px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    background: var(--bg-container);
    border: 1px solid var(--border-medium);
    border-radius: 12px;
    box-shadow: var(--shadow-dropdown);
    overflow: hidden;
    transition: opacity 0.3s ease;
}

.nav-arrow {
    width: 48px;
    height: 44px;
    border: none;
    background: var(--bg-container);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    font-size: 16px;
}

.nav-arrow:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    transform: scale(1.05);
}

.nav-arrow:active {
    transform: scale(0.95);
}

.nav-arrow:disabled {
    color: var(--text-light);
    cursor: not-allowed;
    opacity: 0.5;
}

.nav-arrow:disabled:hover {
    background: var(--bg-container);
    transform: none;
}

/* Individual button styles */
.nav-top {
    border-bottom: 1px solid var(--border-light);
    border-radius: 12px 12px 0 0;
}

.nav-up {
    border-bottom: 1px solid var(--border-light);
}

.nav-down {
    border-bottom: 1px solid var(--border-light);
}

.nav-end {
    border-radius: 0 0 12px 12px;
}

/* Special styling for top/end buttons */
.nav-top,
.nav-end {
    background: var(--bg-light);
    color: var(--text-brand);
    font-size: 18px;
}

.nav-top:hover,
.nav-end:hover {
    background: var(--btn-primary-bg);
    color: var(--btn-primary-text);
    transform: scale(1.08);
}

.nav-top:disabled,
.nav-end:disabled {
    background: var(--bg-container);
    color: var(--text-light);
}

.nav-top:disabled:hover,
.nav-end:disabled:hover {
    background: var(--bg-container);
    transform: none;
}

/* Hide navigation button when there are no user messages */
.message-nav-button.hidden {
    opacity: 0;
    pointer-events: none;
}

/* NEW: subtle differentiation for sync vs API in list items */
.chat-item.chat-item--sync {
    border-left-color: var(--border-medium);
}

.chat-item.chat-item--api {
    border-left-color: var(--btn-primary-bg);
    background-image: linear-gradient(180deg, transparent, color-mix(in oklab, var(--btn-primary-bg) 3%, transparent));
}

/* Responsive adjustments for mobile */
@media (max-width: 768px) {
    .message-nav-button {
        right: 15px;
        transform: translateY(-50%) scale(0.9);
    }
    
    .nav-arrow {
        width: 42px;
        height: 38px;
        font-size: 14px;
    }
    
    .nav-top,
    .nav-end {
        font-size: 16px;
    }
}

/* --- START: New Chat Container Styles --- */
#new-chat-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2000;

    display: flex;
    background-color: var(--bg-glass);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%); /* For Safari */

    /* Animation: Hidden by default, slides up from the bottom */
    transform: translateY(100%);
    visibility: hidden;
    opacity: 0;
    transition: transform 0.6s cubic-bezier(0.76, 0, 0.24, 1), opacity 0.6s ease, visibility 0.6s;
}

/* State when New Chat is active */
body.viewing-new-chat #new-chat-container {
    transform: translateY(0);
    visibility: visible;
    opacity: 1;
}

/* Input and Output Panels */
#new-chat-input-panel,
#new-chat-output-panel {
    flex: 1; /* Each takes half the space */
    padding: 30px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    overflow-y: auto;
}

/* The dividing line */
#new-chat-input-panel {
    border-right: 1px solid var(--border-glass);
}

/* Input textarea styling */
#new-chat-textarea {
    flex-grow: 1; /* Take up all available vertical space */
    width: 100%;
    padding: 15px;
    border: 1px solid var(--border-medium);
    border-radius: 8px;
    background-color: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    font-size: 16px;
    line-height: 1.6;
    resize: none; /* Disable user resizing */
    box-sizing: border-box;
    transition: all 0.2s ease;
}

#layer-3-textarea:focus {
    outline: none;
    border-color: var(--text-brand);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--text-brand) 25%, transparent);
}

/* Send button */
#new-chat-send-btn {
    flex-shrink: 0; /* Prevent button from shrinking */
}

/* Output panel placeholder */
#new-chat-output-panel .placeholder {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 100%;
    color: var(--text-muted);
    text-align: center;
    opacity: 0.6;
}

#layer-3-output-panel .placeholder i {
    font-size: 5rem;
    margin-bottom: 1.5rem;
}

/* Ensure header can wrap when the card gets narrow */
.chat-view-instance .chat-message-container .chat-header {
    display: flex;
    flex-wrap: wrap;        /* allow actions to move to next line */
    row-gap: 8px;           /* space between wrapped rows */
}

/* Let the title side flex and truncate instead of pushing buttons out */
.chat-view-instance .chat-message-container .chat-header-left {
    flex: 1 1 auto;
    min-width: 0;           /* allow flex child to shrink below content width */
}

/* Truncate long titles so actions have room */
.chat-view-instance .chat-message-container .chat-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Keep action buttons inside the box; allow wrapping into a new line */
.chat-view-instance .chat-message-container .chat-header-right {
    display: flex;
    flex: 0 1 auto;
    flex-wrap: wrap;        /* wrap buttons when space is tight */
    gap: 8px;
    justify-content: flex-end;
    align-items: center;
    margin-left: auto;      /* push group to the right */
    max-width: 100%;
}

/* Buttons shouldn’t grow weirdly; keep them compact and on a single line each */
.chat-view-instance .chat-message-container .chat-header-right .btn,
.chat-view-instance .chat-message-container .chat-header-right .btn-close-view {
    flex: 0 0 auto;
    white-space: nowrap;
}

/* Make sure the whole message container can shrink within the flex layout */
.chat-view-instance .chat-message-container {
    min-width: 0;
}

/* Mac-style close button */
.mac-close-btn {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 10;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: #ff5f57;
    border: none;
    color: #000;
    font-size: 14px;
    font-weight: bold;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
    transition: background-color 0.2s ease, transform 0.1s ease;
}

.mac-close-btn:hover {
    background-color: #e0443e;
    transform: scale(1.05);
}

.mac-close-btn:active {
    transform: scale(0.95);
}

/* ...existing code... */
.cassette-btn:focus-visible {
    outline: 2px solid var(--btn-primary-bg);
    outline-offset: 2px;
}