/**
 * POE Chat - Complete Stylesheet
 */

/* Design Tokens */
:root {
    /* Primary */
    --primary: #667eea;
    --primary-hover: #5a6fd6;
    
    /* Semantic */
    --success: #22c55e;
    --warning: #f59e0b;
    --error: #ef4444;
    --info: #3b82f6;
    
    /* Neutrals */
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    
    /* Background */
    --bg-primary: #ffffff;
    --bg-secondary: var(--gray-50);
    --bg-sidebar: var(--gray-100);
    --bg-hover: var(--gray-100);
    
    /* Text */
    --text-primary: var(--gray-900);
    --text-secondary: var(--gray-600);
    --text-muted: var(--gray-500);
    
    /* Border */
    --border-color: var(--gray-200);
    --border-radius: 8px;
    --border-radius-sm: 4px;
    
    /* Typography */
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    --font-mono: 'Fira Code', 'Consolas', 'Monaco', monospace;
    
    /* Spacing */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-12: 48px;
    
    /* Shadows */
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
    
    /* Animations */
    --ease-out: cubic-bezier(0, 0, 0.2, 1);
    --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
}

/* Reset and Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
    background: var(--bg-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Layout */
.app-container {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

.sidebar {
    width: 280px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: transform 0.3s var(--ease-out);
}

.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

/* Sidebar Components */
.sidebar-header {
    padding: var(--space-4);
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header button {
    width: 100%;
}

.search-wrapper {
    position: relative;
    margin-top: var(--space-3);
}

.search-icon {
    position: absolute;
    left: var(--space-3);
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    padding-left: calc(var(--space-3) + 20px);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 14px;
    transition: border-color 0.15s, background-color 0.15s;
}

.search-input:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--bg-primary);
}

.search-input::placeholder {
    color: var(--text-muted);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 0 var(--space-4);
    padding-top: var(--space-2);
}

.chat-item {
    position: relative;
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-2);
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: background-color 0.15s;
}

.chat-item:hover {
    background: var(--bg-hover);
}

.chat-item.active {
    background: var(--primary);
    color: white;
}

.chat-item-title {
    font-weight: 500;
    margin-bottom: var(--space-1);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-right: 64px; /* Space for action buttons */
}

.chat-item-meta {
    font-size: 12px;
    color: var(--text-muted);
    display: flex;
    justify-content: space-between;
}

.chat-item.active .chat-item-meta {
    color: rgba(255, 255, 255, 0.8);
}

.chat-item-actions {
    position: absolute;
    top: var(--space-2);
    right: var(--space-2);
    display: flex;
    gap: var(--space-1);
    opacity: 0;
    transition: opacity 0.15s;
}

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

/* Better visibility for buttons inside chat items */
.chat-item-actions .btn-ghost {
    background: rgba(255, 255, 255, 0.9);
    color: var(--gray-700);
    box-shadow: var(--shadow-sm);
}

.chat-item-actions .btn-ghost:hover:not(:disabled) {
    background: rgba(255, 255, 255, 1);
    color: var(--gray-900);
}

.chat-item.active .chat-item-actions .btn-ghost {
    background: rgba(255, 255, 255, 0.95);
    color: var(--primary);
}

.chat-item.active .chat-item-actions .btn-ghost:hover:not(:disabled) {
    background: white;
    color: var(--primary-hover);
}

.sidebar-footer {
    padding: var(--space-4);
    border-top: 1px solid var(--border-color);
}

.user-info {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    margin-bottom: var(--space-3);
}

.user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--primary);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
}

.user-name {
    flex: 1;
    font-weight: 500;
}

/* Chat Header */
.chat-header {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: var(--space-2);
    background: var(--bg-primary);
}

.header-model-select {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 14px;
    cursor: pointer;
    min-width: 180px;
    outline: none;
    transition: border-color 0.15s;
    margin-left: var(--space-2);
    margin-right: var(--space-1);
}

.header-model-select:hover {
    border-color: var(--primary);
}

.header-model-select:focus {
    border-color: var(--primary);
}

.chat-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-6);
}

.message {
    margin-bottom: var(--space-6);
    max-width: 800px;
}

.message-user {
    margin-left: auto;
}

.message-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
    font-size: 12px;
    color: var(--text-secondary);
}

.message-role {
    font-weight: 600;
}

.message-content {
    position: relative;
    padding: var(--space-4);
    border-radius: var(--border-radius);
    background: var(--bg-secondary);
    line-height: 1.6;
}

.message-user .message-content {
    background: var(--primary);
    color: white;
}

.message-actions {
    position: absolute;
    bottom: var(--space-2);
    right: var(--space-2);
    display: flex;
    gap: var(--space-2);
    opacity: 0;
    transition: opacity 0.15s;
}

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

/* Better visibility for buttons inside message content */
.message-actions .btn-ghost {
    background: rgba(255, 255, 255, 0.9);
    color: var(--gray-700);
    box-shadow: var(--shadow-sm);
}

.message-actions .btn-ghost:hover:not(:disabled) {
    background: rgba(255, 255, 255, 1);
    color: var(--gray-900);
}

.message-user .message-actions .btn-ghost {
    background: rgba(255, 255, 255, 0.95);
    color: var(--primary);
}

.message-user .message-actions .btn-ghost:hover:not(:disabled) {
    background: white;
    color: var(--primary-hover);
}

/* Input Area */
.input-area {
    padding: var(--space-4);
    border-top: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.input-container {
    display: flex;
    gap: var(--space-3);
    align-items: flex-end;
}

.input-textarea {
    flex: 1;
    min-height: 40px;
    max-height: 200px;
    padding: 10px var(--space-3);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-family: var(--font-sans);
    font-size: 14px;
    line-height: 20px;
    resize: none;
    outline: none;
    transition: border-color 0.15s;
}

.input-textarea:focus {
    border-color: var(--primary);
}

/* Buttons */
button {
    font-family: var(--font-sans);
    font-size: 14px;
    font-weight: 500;
    padding: var(--space-2) var(--space-4);
    border: none;
    border-radius: var(--border-radius);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    transition: all 0.15s;
    outline: none;
}

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

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--gray-200);
    color: var(--text-primary);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--gray-300);
}

.btn-danger {
    background: var(--error);
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--bg-hover);
}

.btn-icon {
    padding: var(--space-2);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-icon-sm {
    padding: var(--space-1);
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-sm {
    padding: var(--space-1) var(--space-2);
    font-size: 12px;
}

/* Form Elements */
.form-input,
.form-select,
.form-textarea {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-family: var(--font-sans);
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s;
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    border-color: var(--primary);
}

.form-label {
    display: block;
    margin-bottom: var(--space-2);
    font-weight: 500;
    color: var(--text-secondary);
}

.form-label .btn-icon-sm {
    opacity: 0.7;
    transition: opacity 0.15s;
}

.form-label .btn-icon-sm:hover {
    opacity: 1;
}

.form-group {
    margin-bottom: var(--space-4);
}

/* Panels */
.settings-panel,
.knowledge-panel {
    position: fixed;
    top: 0;
    right: -400px;
    width: 400px;
    height: 100vh;
    background: var(--bg-primary);
    border-left: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    transition: right 0.3s var(--ease-out);
    z-index: 100;
    display: flex;
    flex-direction: column;
}

.settings-panel.open,
.knowledge-panel.open {
    right: 0;
}

.panel-header {
    padding: var(--space-4);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.panel-title {
    font-size: 16px;
    font-weight: 600;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-4);
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    top: var(--space-4);
    right: var(--space-4);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.toast {
    min-width: 300px;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--border-radius);
    background: white;
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    opacity: 0;
    transform: translateX(100%);
    transition: all 0.3s var(--ease-out);
}

.toast.show {
    opacity: 1;
    transform: translateX(0);
}

.toast-success {
    border-left: 4px solid var(--success);
}

.toast-error {
    border-left: 4px solid var(--error);
}

.toast-warning {
    border-left: 4px solid var(--warning);
}

.toast-info {
    border-left: 4px solid var(--info);
}

.toast-loading {
    border-left: 4px solid var(--info);
}

.toast-icon svg {
    width: 20px;
    height: 20px;
}

.toast-loading .toast-icon svg {
    animation: spin 1s linear infinite;
}

.toast-message {
    flex: 1;
}

.toast-close {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    color: var(--text-muted);
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    transition: opacity 0.3s;
}

.modal-overlay.show {
    opacity: 1;
}

.modal {
    min-width: 400px;
    max-width: 600px;
    background: white;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: scale(0.9);
    transition: all 0.3s var(--ease-out);
}

.modal.show {
    opacity: 1;
    transform: scale(1);
}

.modal-header {
    padding: var(--space-4);
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
}

.modal-body {
    padding: var(--space-4);
}

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

/* Loading States */
.spinner-container {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-12);
}

.spin {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.skeleton {
    height: 60px;
    margin-bottom: var(--space-2);
    border-radius: var(--border-radius);
    background: linear-gradient(90deg, var(--gray-200) 25%, var(--gray-100) 50%, var(--gray-200) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Login Page */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--bg-secondary);
}

.login-box {
    width: 100%;
    max-width: 400px;
    padding: var(--space-8);
    background: white;
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg);
}

.login-title {
    font-size: 24px;
    font-weight: 600;
    margin-bottom: var(--space-6);
    text-align: center;
}

/* Markdown Styles */
.message-content h1,
.message-content h2,
.message-content h3 {
    margin-top: var(--space-4);
    margin-bottom: var(--space-2);
}

.message-content h1 { font-size: 1.5em; }
.message-content h2 { font-size: 1.3em; }
.message-content h3 { font-size: 1.1em; }

.message-content p {
    margin-bottom: var(--space-3);
}

.message-content code {
    padding: 2px 6px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: var(--border-radius-sm);
    font-family: var(--font-mono);
    font-size: 0.9em;
}

.message-content pre {
    padding: var(--space-4);
    background: var(--gray-900);
    color: var(--gray-100);
    border-radius: var(--border-radius);
    overflow-x: auto;
    margin: var(--space-3) 0;
}

.message-content pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.message-content ul,
.message-content ol {
    margin-left: var(--space-6);
    margin-bottom: var(--space-3);
}

.message-content blockquote {
    border-left: 4px solid var(--gray-300);
    padding-left: var(--space-4);
    margin: var(--space-3) 0;
    color: var(--text-secondary);
}

/* Responsive */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: -100%;
        width: 100%;
        z-index: 200;
    }
    
    .sidebar.open {
        left: 0;
    }
    
    .settings-panel,
    .knowledge-panel {
        right: 0;
        top: auto;
        bottom: -100%;
        width: 100%;
        height: 80vh;
        border-radius: var(--border-radius) var(--border-radius) 0 0;
    }
    
    .settings-panel.open,
    .knowledge-panel.open {
        bottom: 0;
    }
    
    .messages-container {
        padding: var(--space-4);
    }
    
    .message {
        max-width: 100%;
    }
}

/* Accessibility */
:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

:focus:not(:focus-visible) {
    outline: none;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: var(--space-2) 0;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--gray-400);
    animation: typing-bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) {
    animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing-bounce {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.5;
    }
    30% {
        transform: translateY(-8px);
        opacity: 1;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
    background: var(--gray-300);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--gray-400);
}
