/* ============================================================
   VistaPanel AI Chat - Main Stylesheet
   Modern ChatGPT-like UI with dark/light theme support
   ============================================================ */

/* --- CSS Variables (Dark theme default) --- */
:root {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-surface: #0f3460;
    --bg-hover: #1a1a3e;
    --bg-input: #1e2a4a;
    --text-primary: #e4e6eb;
    --text-secondary: #b0b3b8;
    --text-muted: #6c757d;
    --border-color: #2a3a5c;
    --accent: #6c5ce7;
    --accent-hover: #7d6ff0;
    --accent-light: rgba(108, 92, 231, 0.15);
    --danger: #e74c3c;
    --danger-hover: #c0392b;
    --success: #2ecc71;
    --shadow: 0 2px 16px rgba(0, 0, 0, 0.3);
    --radius: 12px;
    --radius-sm: 8px;
    --transition: 0.2s ease;
    --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', Consolas, monospace;
}

/* --- Light theme --- */
body.light {
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    --bg-surface: #e9ecef;
    --bg-hover: #f0f2f5;
    --bg-input: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #495057;
    --text-muted: #868e96;
    --border-color: #dee2e6;
    --accent: #6c5ce7;
    --accent-hover: #5a4bd1;
    --accent-light: rgba(108, 92, 231, 0.08);
    --shadow: 0 2px 16px rgba(0, 0, 0, 0.08);
}

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    height: 100vh;
    overflow: hidden;
    transition: background var(--transition), color var(--transition);
}

/* --- Layout --- */
#app {
    display: flex;
    height: 100vh;
}

/* --- Sidebar --- */
.sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: width var(--transition), min-width var(--transition), opacity var(--transition);
    z-index: 10;
}
.sidebar.collapsed {
    width: 0;
    min-width: 0;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
}
/* Floating sidebar toggle when collapsed */
.sidebar-floating-toggle {
    display: none;
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 20;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
#btn-floating-toggle {
    display: none;
}

.sidebar-header {
    padding: 16px;
    display: flex;
    gap: 10px;
    align-items: center;
}
.sidebar-header .btn-primary {
    flex: 1;
    justify-content: center;
    gap: 8px;
}
.sidebar-actions {
    display: flex;
    gap: 4px;
}

.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.conv-list::-webkit-scrollbar { width: 4px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 4px; }

.conv-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition);
    position: relative;
    text-decoration: none;
    color: var(--text-primary);
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--accent-light); }

.conv-item .conv-icon {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    opacity: 0.6;
}
.conv-item.active .conv-icon { opacity: 1; color: var(--accent); }

.conv-item .conv-title-text {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 14px;
    font-weight: 500;
}
.conv-item .conv-date {
    font-size: 11px;
    color: var(--text-muted);
    flex-shrink: 0;
}

.conv-item .conv-actions {
    display: none;
    gap: 2px;
    flex-shrink: 0;
}
.conv-item:hover .conv-actions { display: flex; }
.conv-item .conv-actions .btn-icon {
    width: 28px;
    height: 28px;
    padding: 4px;
    opacity: 0.6;
}
.conv-item .conv-actions .btn-icon:hover { opacity: 1; background: var(--bg-surface); }

.sidebar-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 10px;
}
.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
}
.user-name {
    font-size: 14px;
    font-weight: 500;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.sidebar-footer-actions { display: flex; gap: 4px; }

/* --- Main Chat Area --- */
.main-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    height: 100vh;
}

/* --- Auth Screen --- */
.auth-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: var(--bg-primary);
}
.auth-card {
    width: 420px;
    max-width: 90vw;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 40px 32px;
    box-shadow: var(--shadow);
    text-align: center;
}
.auth-logo { font-size: 56px; margin-bottom: 12px; }
.auth-card h1 {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 6px;
}
.auth-subtitle {
    color: var(--text-secondary);
    font-size: 14px;
    margin-bottom: 28px;
}

.auth-tabs {
    display: flex;
    gap: 0;
    margin-bottom: 24px;
    border-bottom: 2px solid var(--border-color);
}
.auth-tab {
    flex: 1;
    padding: 10px;
    border: none;
    background: none;
    color: var(--text-muted);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: color var(--transition), border-color var(--transition);
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
}
.auth-tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.auth-form { display: none; }
.auth-form.active { display: block; }

.form-group {
    margin-bottom: 16px;
    text-align: left;
}
.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
}
.form-group input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 15px;
    transition: border-color var(--transition);
}
.form-group input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}
.form-error {
    color: var(--danger);
    font-size: 13px;
    margin-bottom: 12px;
    min-height: 20px;
}

/* --- Chat Interface --- */
.chat-interface {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Chat Header */
.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 20px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-secondary);
    flex-shrink: 0;
}
.chat-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.chat-header-actions { display: flex; gap: 6px; }

/* Messages */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    scroll-behavior: smooth;
}
.messages-container::-webkit-scrollbar { width: 6px; }
.messages-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 6px;
}

.messages-list {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px 16px 40px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.welcome-message {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-secondary);
}
.welcome-icon { font-size: 64px; margin-bottom: 16px; }
.welcome-message h3 { font-size: 22px; font-weight: 600; margin-bottom: 8px; color: var(--text-primary); }
.welcome-message p { font-size: 15px; }

/* Message bubbles */
.message {
    display: flex;
    gap: 14px;
    padding: 16px 20px;
    border-radius: var(--radius);
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}
.message.user .message-avatar { background: var(--accent); color: white; }
.message.assistant .message-avatar { background: #10a37f; color: white; }

.message-body { flex: 1; min-width: 0; }
.message-role {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 4px;
    color: var(--text-muted);
}
.message.user { background: var(--accent-light); }
.message.assistant { background: transparent; }

.message-content {
    font-size: 15px;
    line-height: 1.7;
    word-wrap: break-word;
    color: var(--text-primary);
}

/* Markdown in messages */
.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
    margin: 16px 0 8px;
    font-weight: 600;
    line-height: 1.4;
}
.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.15em; }

.message-content p { margin-bottom: 10px; }
.message-content p:last-child { margin-bottom: 0; }

.message-content ul, .message-content ol {
    margin: 8px 0;
    padding-left: 24px;
}
.message-content li { margin-bottom: 4px; }

.message-content a {
    color: var(--accent);
    text-decoration: underline;
}
.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding: 4px 14px;
    margin: 10px 0;
    color: var(--text-secondary);
    background: var(--bg-hover);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.message-content table {
    border-collapse: collapse;
    margin: 10px 0;
    width: 100%;
    font-size: 14px;
}
.message-content th, .message-content td {
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    text-align: left;
}
.message-content th { background: var(--bg-surface); font-weight: 600; }

.message-content img {
    max-width: 100%;
    border-radius: var(--radius-sm);
    margin: 8px 0;
}

/* Code blocks */
.message-content code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background: var(--bg-surface);
    padding: 2px 6px;
    border-radius: 4px;
}
.message-content pre {
    background: #0d1117;
    border-radius: var(--radius-sm);
    padding: 16px;
    margin: 12px 0;
    overflow-x: auto;
    position: relative;
}
.message-content pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    color: #e6edf3;
}
.message-content pre::-webkit-scrollbar { height: 4px; }
.message-content pre::-webkit-scrollbar-thumb { background: #30363d; border-radius: 4px; }

/* Copy button for code blocks */
.code-block-wrapper { position: relative; }
.code-copy-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 4px 10px;
    font-size: 12px;
    background: #30363d;
    color: #c9d1d9;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    opacity: 0;
    transition: opacity var(--transition);
}
.code-block-wrapper:hover .code-copy-btn { opacity: 1; }
.code-copy-btn:hover { background: #484f58; }

/* Image in messages */
.message-image {
    max-width: 300px;
    max-height: 300px;
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
    cursor: pointer;
}
.message-image:hover { opacity: 0.9; }

/* Typing indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}
.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typing 1.4s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); } 30% { opacity: 1; transform: scale(1); } }

/* --- Input Area --- */
.input-area {
    padding: 12px 20px 16px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    flex-shrink: 0;
}
.input-row {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    max-width: 800px;
    margin: 0 auto;
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    padding: 6px 6px 6px 16px;
    transition: border-color var(--transition);
}
.input-row:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-light);
}

#chat-input {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 15px;
    line-height: 1.5;
    resize: none;
    outline: none;
    padding: 8px 0;
    max-height: 200px;
    font-family: inherit;
}
#chat-input::placeholder { color: var(--text-muted); }

.btn-send {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    transition: background var(--transition), opacity var(--transition);
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.image-preview-container {
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: 800px;
    margin: 0 auto 8px;
    padding: 8px 10px;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
}
.image-preview-container img {
    max-height: 80px;
    max-width: 150px;
    border-radius: 4px;
    object-fit: cover;
}
.image-preview-container .btn-icon {
    background: var(--danger);
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    font-size: 16px;
    line-height: 1;
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition), opacity var(--transition);
    font-family: inherit;
}
.btn-primary {
    background: var(--accent);
    color: white;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary {
    background: var(--bg-surface);
    color: var(--text-primary);
}
.btn-secondary:hover { background: var(--bg-hover); }
.btn-block { width: 100%; justify-content: center; }

.btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background var(--transition), color var(--transition);
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text-primary); }
.btn-danger { color: #fff; background: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-icon.btn-danger { color: var(--danger); background: transparent; }
.btn-icon.btn-danger:hover { color: #fff; background: var(--danger); }

/* --- Modal --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeInOverlay 0.2s ease;
}
@keyframes fadeInOverlay { from { opacity: 0; } to { opacity: 1; } }

.modal {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius);
    width: 520px;
    max-width: 90vw;
    box-shadow: var(--shadow);
    animation: modalIn 0.3s ease;
}
@keyframes modalIn { from { opacity: 0; transform: scale(0.95) translateY(-10px); } to { opacity: 1; transform: scale(1) translateY(0); } }

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}
.modal-header h3 { font-size: 16px; font-weight: 600; }
.modal-close { font-size: 22px; width: 32px; height: 32px; }

.modal-body { padding: 20px; }
.modal-hint { font-size: 13px; color: var(--text-muted); margin-bottom: 12px; }
.modal-body textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    resize: vertical;
    font-family: var(--font-mono);
    line-height: 1.6;
}
.modal-body textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.modal-footer {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    padding: 12px 20px;
    border-top: 1px solid var(--border-color);
}

/* --- Toast --- */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.toast {
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
    box-shadow: var(--shadow);
    max-width: 380px;
}
.toast.error { background: var(--danger); color: white; }
.toast.success { background: var(--success); color: white; }
@keyframes toastIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; } }

/* --- API Error Diagnostics --- */
.api-error {
    border: 1px solid var(--danger);
    border-radius: var(--radius-sm);
    background: color-mix(in srgb, var(--danger) 10%, var(--bg-secondary));
    color: var(--text-primary);
    padding: 12px;
}
.api-error > strong { color: var(--danger); }
.api-error p { margin: 4px 0 0; white-space: pre-wrap; }
.api-error-details { margin-top: 10px; color: var(--text-secondary); }
.api-error-details summary { cursor: pointer; color: var(--accent); font-weight: 600; }
.api-error-details dl {
    display: grid;
    grid-template-columns: max-content minmax(0, 1fr);
    gap: 4px 12px;
    margin: 10px 0;
    font-size: 13px;
}
.api-error-details dt { font-weight: 600; }
.api-error-details dd { margin: 0; overflow-wrap: anywhere; }
.api-error-section { margin-top: 10px; }
.api-error-section ul { margin: 4px 0 0; padding-left: 20px; }
.api-error-section pre {
    max-height: 220px;
    margin: 4px 0 0;
    padding: 10px;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-size: 12px;
}

.error-log-window {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 300;
    width: min(620px, calc(100vw - 40px));
    max-height: min(70vh, 680px);
    display: flex;
    flex-direction: column;
    background: var(--bg-secondary);
    border: 1px solid var(--danger);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow);
}
.error-log-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    color: var(--danger);
    border-bottom: 1px solid var(--border-color);
}
.error-log-close { flex-shrink: 0; }
.error-log-content {
    overflow: auto;
    padding: 12px;
}
.error-log-window .api-error { border: none; padding: 0; background: transparent; }
.error-log-empty { color: var(--text-secondary); font-size: 14px; }
.btn-error-log { color: var(--danger); }
.btn-error-log:hover { color: #fff; background: var(--danger); }

/* --- Mobile --- */
.mobile-only { display: none !important; }
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        z-index: 50;
        transform: translateX(-100%);
        transition: transform var(--transition);
    }
    .sidebar.mobile-open { transform: translateX(0); }
    .mobile-only { display: flex !important; }
    .messages-list { padding: 12px 10px 32px; }
    .message { padding: 12px 14px; }
    .input-row { padding: 4px 4px 4px 12px; }
    #chat-input { font-size: 16px; } /* Prevent iOS zoom */
    .error-log-window { right: 12px; bottom: 12px; width: calc(100vw - 24px); max-height: 60vh; }
}