/* ===== Root Variables ===== */
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --secondary-color: #64748b;
    --danger-color: #dc2626;
    --danger-hover: #b91c1c;
    --success-color: #16a34a;
    --background: #f8fafc;
    --surface: #ffffff;
    --surface-hover: #f1f5f9;
    --border: #e2e8f0;
    --text-primary: #0f172a;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --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);
    --radius: 8px;
    --radius-sm: 4px;
    --radius-lg: 12px;
}

/* ===== Base Styles ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

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

.container {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ===== Header ===== */
.header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 1.5rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: var(--shadow-sm);
}

.header-content h1 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0.25rem;
}

.subtitle {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.status-badge {
    padding: 0.25rem 0.75rem;
    background: var(--success-color);
    color: white;
    border-radius: var(--radius);
    font-size: 0.75rem;
    font-weight: 600;
}

.version {
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* ===== Tab Navigation ===== */
.tab-nav {
    background: var(--surface);
    border-bottom: 2px solid var(--border);
    padding: 0 2rem;
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
}

.tab-button {
    padding: 1rem 1.5rem;
    background: transparent;
    border: none;
    border-bottom: 3px solid transparent;
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

.tab-button:hover {
    color: var(--text-primary);
    background: var(--surface-hover);
}

.tab-button.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

/* ===== Content Area ===== */
.content {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
}

.tab-content {
    display: none;
    animation: fadeIn 0.3s ease-in;
}

.tab-content.active {
    display: block;
}

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

.tab-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 1rem;
}

.tab-header h2 {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
}

.tab-actions {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.content-area {
    background: var(--surface);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}

/* ===== Chat Container ===== */
.chat-container {
    display: grid;
    grid-template-columns: 320px 1fr;
    gap: 1.5rem;
    height: calc(100vh - 200px);
    min-height: 600px;
}

@media (max-width: 1024px) {
    .chat-container {
        grid-template-columns: 1fr;
        height: auto;
    }
}

/* ===== Chat List Panel ===== */
.chat-list-panel {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-list-header {
    padding: 1.25rem;
    border-bottom: 1px solid var(--border);
}

.chat-list-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
}

.chat-search {
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border);
}

.chat-search input {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
}

.chat-item {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.2s;
}

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

.chat-item.active {
    background: #eff6ff;
    border-left: 3px solid var(--primary-color);
}

.chat-item-id {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-family: monospace;
    margin-bottom: 0.25rem;
}

.chat-item-preview {
    font-size: 0.875rem;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 0.25rem;
}

.chat-item-time {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ===== Chat Interface Panel ===== */
.chat-interface-panel {
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.no-chat-selected {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
}

.empty-state h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.chat-interface {
    display: flex;
    flex-direction: column;
    height: 100%;
}

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

.chat-header-info h3 {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.chat-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.message {
    display: flex;
    gap: 0.75rem;
    animation: slideIn 0.3s ease-out;
}

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

.message.tool .message-bubble {
    background: #e0f2fe;
    border: 1px solid #7dd3fc;
    color: var(--text-primary);
}

.message.system .message-bubble {
    background: #fef3c7;
    border: 1px solid #fbbf24;
    color: var(--text-primary);
}

.message-content {
    flex: 1;
    max-width: 100%;
}

.message-bubble {
    padding: 0.75rem 1rem;
    padding-top: 0.5rem;
    border-radius: var(--radius-lg);
    word-wrap: break-word;
    position: relative;
    cursor: pointer;
    transition: all 0.2s;
}

.message-bubble:hover {
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.message.user .message-bubble {
    background: #dbeafe;
    border: 2px solid #60a5fa;
    color: var(--text-primary);
}

.message.assistant .message-bubble {
    background: #f3e8ff;
    border: 2px solid #c084fc;
    color: var(--text-primary);
}

.message.tool-call .message-bubble {
    background: #ffedd5;
    border: 2px solid #fb923c;
    color: var(--text-primary);
}

.message.tool-call .message-bubble .message-label {
    border: 2px solid #fb923c;
}

.message.tool .message-bubble .message-label {
    border: 1px solid #7dd3fc;
}

.message.system .message-bubble .message-label {
    border: 1px solid #fbbf24;
}

.message.user .message-bubble .message-label {
    border: 2px solid #60a5fa;
}

.message.assistant .message-bubble .message-label {
    border: 2px solid #c084fc;
}

.message-label {
    position: absolute;
    top: -0.65rem;
    left: 1rem;
    padding: 0.1rem 0.5rem;
    font-weight: 600;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.35rem;
    line-height: 1.2;
    border-radius: 4px;
    cursor: pointer;
    background: white;
}

.message.user .message-label {
    background: #60a5fa;
    color: white;
}

.message.assistant .message-label {
    left: auto;
    right: 1rem;
    background: #c084fc;
    color: white;
}

.message.tool-call .message-label {
    left: auto;
    right: 1rem;
    background: #fb923c;
    color: white;
}

.message.tool .message-label {
    background: #7dd3fc;
    color: white;
}

.message.system .message-label {
    background: #fbbf24;
    color: white;
}

.message-label .collapse-indicator {
    font-size: 0.65rem;
    transition: transform 0.2s;
}

.message-bubble.collapsed .collapse-indicator {
    transform: rotate(-90deg);
}

.message-bubble.collapsed .message-body {
    max-height: 1.5em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.message-bubble.collapsed .message-body * {
    display: inline;
    margin: 0;
    padding: 0;
}

.message-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
    padding: 0 0.25rem;
}

.message.user .message-time {
    text-align: right;
    color: var(--text-muted);
}

.input-area {
    padding: 1.25rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.75rem;
}

#message-input {
    flex: 1;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: inherit;
    font-size: 0.875rem;
    resize: vertical;
    max-height: 150px;
}

#message-input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* ===== Buttons ===== */
.btn {
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    white-space: nowrap;
}

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

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

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

.btn-secondary:hover {
    background: #475569;
}

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

.btn-danger:hover {
    background: var(--danger-hover);
}

.btn-danger-outline {
    background: transparent;
    color: var(--danger-color);
    border: 1px solid var(--danger-color);
}

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

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

/* ===== Forms ===== */
.search-input,
.filter-select {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.filter-select {
    background: var(--surface);
    cursor: pointer;
}

.search-input:focus,
.filter-select:focus {
    outline: none;
    border-color: var(--primary-color);
}

/* ===== Tables ===== */
.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th {
    text-align: left;
    padding: 0.4rem 0.75rem;
    background: var(--surface-hover);
    font-weight: 600;
    font-size: 0.875rem;
    border-bottom: 2px solid var(--border);
}

.data-table td {
    padding: 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
}

.data-table tr:hover {
    background: var(--surface-hover);
}

/* ===== Cards ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    margin-bottom: 1rem;
    box-shadow: var(--shadow-sm);
}

.card-header {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.card-body {
    color: var(--text-secondary);
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1rem;
}

/* ===== Stats ===== */
.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    text-align: center;
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--primary-color);
    margin-bottom: 0.5rem;
}

.stat-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

/* ===== Modals ===== */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--surface);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
    animation: modalIn 0.3s ease-out;
}

@keyframes modalIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

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

.modal-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.close-modal {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
}

.close-modal:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    padding: 1.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 0.75rem;
    justify-content: flex-end;
}

.form-group {
    margin-bottom: 1.25rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.875rem;
}

.form-control {
    width: 100%;
    padding: 0.625rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

/* ===== Loading & Empty States ===== */
.loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-muted);
}

.loading::after {
    content: '...';
    animation: dots 1.5s steps(4, end) infinite;
}

@keyframes dots {
    0%, 20% { content: '.'; }
    40% { content: '..'; }
    60%, 100% { content: '...'; }
}

.error-message {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.success-message {
    background: #f0fdf4;
    border: 1px solid #bbf7d0;
    color: #166534;
    padding: 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

/* ===== Markdown Tables ===== */
.markdown-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0.5rem 0;
    font-size: 0.875rem;
}

.markdown-table th {
    text-align: left;
    padding: 0.5rem;
    background: rgba(0, 0, 0, 0.05);
    font-weight: 600;
    border: 1px solid var(--border);
}

.markdown-table td {
    padding: 0.5rem;
    border: 1px solid var(--border);
}

.markdown-table tr:nth-child(even) {
    background: rgba(0, 0, 0, 0.02);
}

/* ===== Badges ===== */
.badge {
    display: inline-block;
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius-sm);
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-success {
    background: #dcfce7;
    color: #166534;
}

.badge-danger {
    background: #fee2e2;
    color: #991b1b;
}

.badge-warning {
    background: #fef3c7;
    color: #92400e;
}

.badge-info {
    background: #dbeafe;
    color: #1e40af;
}

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

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

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

::-webkit-scrollbar-thumb:hover {
    background: var(--secondary-color);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .header {
        padding: 1rem;
    }
    
    .tab-nav {
        padding: 0 1rem;
    }
    
    .content {
        padding: 1rem;
    }
    
    .chat-container {
        grid-template-columns: 1fr;
        height: auto;
    }
    
    .tab-actions {
        width: 100%;
        flex-wrap: wrap;
    }
    
    .message-content {
        max-width: 85%;
    }
}
