:root {
    --bg: #ffffff;
    --text: #000000; /* Pure black for max contrast */
    --surface: #f4f4f4;
    --surface-hover: #e0e0e0;
    --border: #a0a0a0; /* Much darker border for clear visibility */
    --accent: #000000;
    --accent-text: #ffffff;
    --danger: #d32f2f; /* Darker red */
    --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: "SF Mono", "Monaco", "Inconsolata", "Fira Mono", "Droid Sans Mono", "Source Code Pro", monospace;
    --spacing: 1rem;
    --radius: 12px;
    --sidebar-width: 260px;
    --right-sidebar-width: 300px;
    --header-height: 60px;
}

/* Sidebar Nav (Shared) */
.nav-list {
    display: flex;
    flex-direction: column;
    padding: 0.5rem;
    gap: 0.5rem;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    color: var(--text);
    text-decoration: none;
    border-radius: 6px;
    transition: background 0.2s;
    font-weight: 500;
}

.nav-item:hover { background: var(--surface-hover); }

.nav-item.active {
    background: var(--surface-hover);
    color: var(--accent);
    font-weight: 600;
}

body.dark-mode {
    --bg: #101218; /* User requested specific dark bg */
    --text: #e0e0e0;
    --surface: #1a1c23;
    --surface-hover: #252830;
    --border: #2d313a;
    --accent: #ffffff;
    --accent-text: #101218;
}

body.light-mode {
    --bg: #ffffff;
    --text: #000000;
    --surface: #f4f4f4;
    --surface-hover: #e0e0e0;
    --border: #a0a0a0;
    --accent: #000000;
    --accent-text: #ffffff;
}

@media (prefers-color-scheme: dark) {
    :root {
        /* Default to dark mode using the same values */
        --bg: #101218; 
        --text: #e0e0e0;
        --surface: #1a1c23;
        --surface-hover: #252830;
        --border: #2d313a;
        --accent: #ffffff;
        --accent-text: #101218;
    }
}


* { box-sizing: border-box; margin: 0; padding: 0; }

.mobile-only { display: none; }


body {
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    height: 100vh; /* Fallback */
    height: 100dvh;
    overflow: hidden;
}

/* Layout */
.app-layout {
    display: flex;
    height: 100vh; /* Fallback */
    height: 100dvh;
    width: 100vw;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease, width 0.3s ease, padding 0.3s ease;
    z-index: 50;
    height: 100%;
    flex-shrink: 0;
    overflow: hidden; /* Ensure content hides when width is 0 */
}

.sidebar.closed {
    width: 0;
    border-right: none;
}

.sidebar-header {
    padding: var(--spacing);
    border-bottom: 1px solid var(--border);
}

.full-width-btn {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    border-radius: var(--radius);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    font-size: 0.95rem;
    font-weight: 500;
    transition: all 0.2s;
}

.full-width-btn:hover {
    background: var(--surface-hover);
    border-color: var(--accent);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.chat-item {
    padding: 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
    font-size: 0.9rem;
    transition: background 0.2s;
    position: relative;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid transparent;
    gap: 0.5rem; /* Add gap between title and actions */
}

.chat-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0; /* Crucial for flex truncation */
}

.chat-actions {
    display: flex;
    align-items: center;
    flex-shrink: 0; /* Prevent buttons from squashing */
    opacity: 0; /* Hidden by default */
    transition: opacity 0.2s;
}

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


.chat-item:hover { background: var(--surface-hover); }
.chat-item.active { 
    background: var(--surface-hover); 
    font-weight: 600; 
    border-color: var(--border);
}

.chat-item-delete {
    opacity: 0;
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    padding: 4px;
    border-radius: 4px;
}
.chat-item:hover .chat-item-delete { opacity: 0.5; }
.chat-item-delete:hover { opacity: 1; color: var(--danger); background: rgba(220, 38, 38, 0.1); }


/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    position: relative;
    min-width: 0; /* Important for flex child truncation */
}

/* Header */
.app-header {
    height: var(--header-height);
    padding: 0 var(--spacing);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg);
    flex-shrink: 0;
}

.logo-area { display: flex; align-items: center; gap: 1rem; }
.logo { display: flex; align-items: center; gap: 0.5rem; }
.logo h1 { font-size: 1.2rem; font-weight: 700; letter-spacing: -0.5px; }

.header-actions { display: flex; align-items: center; gap: 0.5rem; }

/* Right Sidebar */
.right-sidebar {
    width: var(--right-sidebar-width);
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100%;
    transition: transform 0.3s ease, margin-right 0.3s ease;
    z-index: 40;
    flex-shrink: 0;
    overflow-y: auto;
}

.right-sidebar.closed {
    margin-right: calc(var(--right-sidebar-width) * -1);
    transform: translateX(100%);
    display: none; /* Hide fully when closed to avoid layout issues on small screens */
}

/* On mobile, standard behavior overrides */
@media (min-width: 769px) {
    .right-sidebar.closed {
        display: flex; /* Keep it flex but moved off-screen for animation */
    }
}

.right-sidebar-content {
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    height: 100%;
    gap: 1.5rem;
}

/* Push the last section (Monitor) to bottom if needed, or just let them stack naturally.
   User asked to avoid scrolling, so compacting is key. */
   
.monitor-section {
    flex-shrink: 0;
}

.monitor-section h3 {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text);
    opacity: 0.6;
    margin-bottom: 0.75rem;
    font-weight: 700;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.4rem;
}

/* Merged Status Label */
.label-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}
.label-row label { margin-bottom: 0; }

.status-indicator-inline {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    font-weight: 600;
    background: var(--bg);
    padding: 2px 6px;
    border-radius: 4px;
    border: 1px solid var(--border);
}

/* Compact Tools Grid */
.tools-grid.compact-tools {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
    gap: 0.5rem;
}

.action-btn.vertical-btn {
    flex-direction: column;
    padding: 0.75rem 0.25rem;
    gap: 0.4rem;
    text-align: center;
    font-size: 0.75rem;
    height: auto;
}
#manage-models-btn { grid-column: auto; } /* Reset span */

/* Compact Stats Grid */
.stats-grid.compact-stats {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 0.5rem;
}

.stat-item.compact {
    padding: 0.5rem;
    align-items: center;
    text-align: center;
    gap: 0.2rem;
}

.stat-item.compact label {
    font-size: 0.65rem;
    white-space: nowrap;
}

.stat-item.compact .stat-value {
    font-size: 0.9rem;
}

.control-group { margin-bottom: 0.75rem; }
.control-group label { display: block; font-size: 0.8rem; margin-bottom: 0.4rem; font-weight: 500; }
.control-group .full-width { width: 100%; justify-content: center; }



.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

.stat-item {
    background: var(--bg);
    border: 1px solid var(--border);
    padding: 0.75rem;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.stat-item label { font-size: 0.75rem; opacity: 0.7; }
.stat-value { font-family: var(--font-mono); font-size: 1.1rem; font-weight: 600; }


.model-selector {
    width: 100%;
    padding: 0.6rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    font-size: 0.9rem;
    font-family: inherit;
    cursor: pointer;
    outline: none;
}
.model-selector:focus { border-color: var(--accent); }

/* Buttons */
.action-btn {
    padding: 0.5rem 1rem;
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}
.action-btn:hover { background: var(--surface-hover); }

.action-btn.primary { 
    background: var(--accent); 
    color: var(--accent-text); 
    border-color: var(--accent); 
}
.action-btn.primary:hover { opacity: 0.9; }

.action-btn.secondary { background: var(--surface); color: var(--text); border-color: transparent; }
.action-btn.secondary:hover { background: var(--border); }

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

.icon-btn {
    background: none;
    border: 1px solid transparent;
    cursor: pointer;
    color: var(--text);
    padding: 0.4rem;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.icon-btn:hover { background: var(--surface); border-color: var(--border); }

/* Unified Small Icon Button (Shared across apps) */
.icon-btn-sm {
    padding: 4px;
    border-radius: 4px;
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.7;
    transition: all 0.2s;
}

.icon-btn-sm:hover {
    background: var(--surface-hover);
    opacity: 1;
    color: var(--accent);
}

.icon-btn-sm.delete-btn:hover {
    color: var(--danger);
    background: rgba(220, 38, 38, 0.1);
}

/* #sidebar-toggle { display: none; }  Removed to allow toggling on desktop too */

/* Chat Area */
.chat-container {
    flex: 1;
    overflow-y: auto;
    /* Use padding to center content while keeping scrollbar at edge */
    padding: var(--spacing) max(1rem, calc((100% - 900px) / 2));
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    width: 100%;
    scroll-behavior: smooth;
}

.welcome-message { text-align: center; margin-top: 4rem; opacity: 0.8; }
.welcome-message h2 { margin-bottom: 0.5rem; }

.message {
    max-width: 85%;
    line-height: 1.6;
    animation: fadeIn 0.3s ease;
    word-wrap: break-word;
    position: relative; /* For actions */
}

.message.user {
    align-self: flex-end;
    background: var(--surface);
    padding: 0.75rem 1rem;
    border-radius: 16px 16px 2px 16px;
    border: 1px solid var(--border);
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

.message.ai {
    align-self: flex-start;
    padding-right: 1rem;
    width: 100%;
    max-width: 100%;
}

.message-actions {
    display: flex;
    justify-content: flex-start;
    gap: 0.5rem;
    margin-top: 0.5rem;
    opacity: 0;
    transition: opacity 0.2s;
}

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

.copy-response-btn {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.75rem;
    color: var(--text);
    opacity: 0.6;
    background: var(--surface);
    border: 1px solid var(--border);
    padding: 2px 8px;
    border-radius: 4px;
    cursor: pointer;
}

.copy-response-btn:hover {
    opacity: 1;
    background: var(--surface-hover);
}

/* Code Block Wrapper */
.code-block-wrapper {
    margin: 1rem 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
    background: #1d1f21;
}

.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.4rem 0.8rem;
    background: rgba(255, 255, 255, 0.05);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-family: var(--font-sans);
    font-size: 0.75rem;
    color: #a0a0a0;
}

.copy-code-btn {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #e0e0e0;
    border-radius: 4px;
    padding: 2px 6px;
    font-size: 0.7rem;
    cursor: pointer;
    transition: all 0.2s;
}

.copy-code-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: #ffffff;
}

/* Prism Code Blocks Override */
.message-content pre { 
    margin: 0 !important;
    border: none !important;
    border-radius: 0 !important;
    background: transparent !important;
}
/* Force scrollbar on code blocks */
pre[class*="language-"] {
    overflow: auto;
    max-height: 500px;
}

/* Input Area */
.input-area {
    border-top: 1px solid var(--border);
    padding: var(--spacing);
    background: var(--bg);
    position: relative;
    flex-shrink: 0;
}

.input-controls {
    max-width: 900px;
    margin: 0 auto 0.5rem;
    display: flex;
    justify-content: flex-end;
}

.token-counter {
    font-size: 0.75rem;
    opacity: 1; /* Removed opacity for better contrast */
    color: var(--text); /* Use main text color */
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.chat-form {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    gap: 0.75rem;
    background: var(--surface);
    padding: 0.5rem;
    border-radius: 24px;
    border: 1px solid var(--border);
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chat-form:focus-within { 
    border-color: var(--accent); 
    box-shadow: 0 0 0 1px var(--accent);
}

textarea {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text);
    font-family: inherit;
    font-size: 1rem;
    padding: 0.5rem 0.75rem;
    resize: none;
    max-height: 150px;
    outline: none;
}

#send-btn {
    background: var(--accent);
    color: var(--accent-text);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform 0.1s;
}

#send-btn:active { transform: scale(0.95); }
#send-btn:disabled { opacity: 0.3; cursor: not-allowed; transform: none; }

/* Progress Bar (Old - Removed, kept for reference if needed, but overridden by dialog styles below) */
.progress-bar-container { display: none; }

/* Progress Dialog */
.progress-dialog {
    border: 1px solid var(--border);
    border-radius: 16px;
    background: var(--bg);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    padding: 0;
    width: 90%;
    max-width: 400px;
    text-align: left;
    overflow: hidden;
}

.progress-dialog::backdrop {
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.progress-content {
    padding: 2.5rem 2rem;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 1.5rem;
}

.progress-dialog h3 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
}

.progress-label { 
    margin-bottom: 0.5rem; 
    color: var(--text);
    font-size: 0.95rem;
    line-height: 1.6;
    text-align: left;
    white-space: pre-wrap;
    max-width: 100%;
    word-break: break-word;
}

.progress-label small {
    display: block;
    font-size: 0.75rem;
    opacity: 0.5;
    margin-bottom: 0.5rem;
    font-family: var(--font-mono);
}

.progress-track {
    height: 8px;
    background: var(--surface);
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid var(--border);
    width: 100%;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    width: 0%;
    transition: width 0.2s ease-out;
}

/* Status Dot */
.status-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text);
    opacity: 0.8;
    font-weight: 500;
}
.dot {
    width: 8px;
    height: 8px;
    background-color: var(--border);
    border-radius: 50%;
    transition: background-color 0.3s;
}
.dot.active { background-color: #22c55e; }

/* Dialogs - Shared Styles */
dialog {
    margin: auto;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    box-shadow: 0 20px 50px rgba(0,0,0,0.2);
    color: var(--text);
    max-width: 450px;
    width: 90%;
    z-index: 100;
}
dialog.wide-dialog { max-width: 750px; }
dialog::backdrop { background: rgba(0,0,0,0.4); backdrop-filter: blur(2px); }

.dialog-content { padding: 1.5rem; }

.dialog-header { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    margin-bottom: 1.5rem; 
    border-bottom: 1px solid var(--border);
    padding-bottom: 1rem;
}
.dialog-header h3 { font-size: 1.25rem; font-weight: 600; }

.dialog-actions { 
    margin-top: 1.5rem; 
    display: flex; 
    justify-content: flex-end; 
    gap: 0.75rem;
}

.dialog-input {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
    outline: none;
    margin-top: 1rem;
}
.dialog-input:focus { border-color: var(--accent); }

/* Project Settings Specifics */
.project-settings-body { display: flex; flex-direction: column; gap: 1.5rem; }
.setting-group { display: flex; flex-direction: column; gap: 0.5rem; }
.setting-group label { font-weight: 600; font-size: 0.9rem; }
.setting-group textarea { 
    background: var(--surface); 
    border: 1px solid var(--border); 
    border-radius: 6px; 
    min-height: 80px; 
    resize: vertical;
}
.setting-group textarea:focus { border-color: var(--accent); }

.file-upload-area {
    border: 2px dashed var(--border);
    border-radius: 8px;
    padding: 2rem;
    text-align: center;
    background: var(--surface);
    transition: all 0.2s;
    cursor: pointer;
}
.file-upload-area:hover, .file-upload-area.drag-over {
    border-color: var(--accent);
    background: var(--surface-hover);
}
.text-btn {
    background: none;
    border: none;
    color: var(--accent);
    text-decoration: underline;
    cursor: pointer;
    font-weight: 600;
}

.file-list {
    list-style: none;
    margin-top: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.file-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.9rem;
}
.file-remove-btn {
    color: var(--danger);
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.1rem;
    padding: 0 0.5rem;
}

/* Models Table */
.models-list-container { max-height: 400px; overflow-y: auto; }
.models-table { width: 100%; border-collapse: collapse; text-align: left; font-size: 0.9rem; }
.models-table th { border-bottom: 2px solid var(--border); padding: 0.75rem; font-weight: 600; }
.models-table td { border-bottom: 1px solid var(--border); padding: 0.75rem; }
.models-table tr:last-child td { border-bottom: none; }
.delete-model-btn {
    color: var(--danger);
    background: none;
    border: 1px solid var(--danger);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    transition: all 0.2s;
}
.delete-model-btn:hover { background: var(--danger); color: white; }
.dialog-footer { margin-top: 1rem; font-size: 0.8rem; opacity: 0.6; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Mobile Responsive */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 280px;
        --right-sidebar-width: 85vw; /* Smaller width on mobile */
    }

    .mobile-only { display: inline-flex; }

    
    #sidebar-toggle { display: block; }
    
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        transform: translateX(-100%);
        box-shadow: 2px 0 10px rgba(0,0,0,0.1);
    }
    
    .sidebar.open { transform: translateX(0); }
    
    .right-sidebar {
        position: fixed;
        right: 0;
        top: 0;
        bottom: 0;
        transform: translateX(100%);
        box-shadow: -2px 0 10px rgba(0,0,0,0.1);
        margin-right: 0;
        z-index: 50; /* Same as left sidebar, above overlay */
        display: flex; /* Ensure it's always flex on mobile, hiding via transform */
    }

    .right-sidebar.closed {
        display: flex; /* Override desktop display:none */
        margin-right: 0; /* Override desktop margin */
    }

    .right-sidebar.open {
        transform: translateX(0);
        display: flex;
    }
    
    .main-content { width: 100%; }
    
    /* .logo h1 { display: none; } Removed to keep title visible on all screens */
    .model-selector { max-width: 100%; }
    
    .dialog-actions { flex-direction: column; }
    .action-btn { width: 100%; justify-content: center; }

    /* Mobile Overlay */
    .sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
        z-index: 45; /* Below sidebar (50) but above content */
        display: none;
        opacity: 0;
        transition: opacity 0.3s ease;
    }

    .sidebar-overlay.visible {
        display: block;
        opacity: 1;
    }

    /* Better mobile spacing */
    .chat-container {
        padding: 0.5rem;
        min-height: 0;
    }
    
    .input-area {
        padding: 0.5rem;
    }
}