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

:root {
    --bg-primary: #212121;
    --bg-secondary: #171717;
    --bg-tertiary: #2f2f2f;
    --bg-hover: #3a3a3a;
    --text-primary: #ececec;
    --text-secondary: #b4b4b4;
    --text-muted: #8e8e8e;
    --border-color: #3a3a3a;
    --accent: #10a37f;
    --accent-hover: #0e8c6b;
    --error: #ef4444;
    --code-bg: #1a1a2e;
    --user-msg-bg: #2f2f2f;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
    --font-mono: 'SF Mono', 'Fira Code', 'Fira Mono', Menlo, Consolas, monospace;
}

html, body {
    height: 100%;
    font-family: var(--font-sans);
    font-size: 15px;
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-primary);
}

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

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 20px;
}

.login-card {
    background: var(--bg-primary);
    border-radius: 16px;
    padding: 40px 32px;
    border: 1px solid var(--border-color);
}

.login-title {
    font-size: 24px;
    font-weight: 700;
    text-align: center;
    margin-bottom: 4px;
}

.login-subtitle {
    color: var(--text-secondary);
    text-align: center;
    margin-bottom: 28px;
    font-size: 14px;
}

.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.form-group input {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 15px;
    outline: none;
    transition: border-color 0.2s;
}

.form-group input:focus {
    border-color: var(--accent);
}

.btn-login {
    width: 100%;
    padding: 12px;
    margin-top: 8px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

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

.error-message {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: var(--error);
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 16px;
}

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

/* ========== Sidebar ========== */
.sidebar {
    width: 260px;
    background: var(--bg-secondary);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
    transition: transform 0.3s ease;
    flex-shrink: 0;
}

.sidebar.hidden {
    transform: translateX(-100%);
    position: absolute;
    z-index: -1;
}

.sidebar-header {
    padding: 12px;
}

.btn-new-chat {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 14px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: background 0.2s;
}

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

.chat-history {
    flex: 1;
    overflow-y: auto;
    padding: 4px 8px;
}

.chat-history-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-secondary);
    transition: background 0.2s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 2px;
}

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

.chat-history-item.active {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.chat-history-item .chat-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-history-item .btn-delete-chat {
    display: none;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 2px 4px;
    border-radius: 4px;
    font-size: 16px;
    line-height: 1;
    flex-shrink: 0;
}

.chat-history-item:hover .btn-delete-chat {
    display: block;
}

.chat-history-item .btn-delete-chat:hover {
    color: var(--error);
    background: rgba(239, 68, 68, 0.1);
}

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

.model-selector {
    margin-bottom: 12px;
}

.model-selector label {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 4px;
}

.model-selector select {
    width: 100%;
    padding: 8px 10px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 13px;
    outline: none;
}

.user-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 13px;
}

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

.btn-logout {
    color: var(--text-muted);
    text-decoration: none;
    font-size: 12px;
}

.btn-logout:hover {
    color: var(--error);
}

/* ========== Main Area ========== */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.main-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.btn-toggle-sidebar {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    display: flex;
    align-items: center;
}

.btn-toggle-sidebar:hover {
    background: var(--bg-hover);
}

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

/* ========== Messages ========== */
.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0;
    scroll-behavior: smooth;
}

.welcome-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    color: var(--text-secondary);
    padding: 20px;
}

.welcome-message h2 {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.welcome-message p {
    font-size: 15px;
}

.message {
    padding: 16px 0;
    max-width: 768px;
    margin: 0 auto;
    width: 100%;
    padding-left: 20px;
    padding-right: 20px;
}

.message-role {
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.message.user .message-role {
    color: var(--accent);
}

.message-content {
    line-height: 1.7;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Markdown Styles */
.message-content p {
    margin-bottom: 12px;
}

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

.message-content code {
    background: var(--code-bg);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 13px;
}

.message-content pre {
    position: relative;
    background: var(--code-bg);
    border-radius: 8px;
    padding: 16px;
    margin: 12px 0;
    overflow-x: auto;
}

.message-content pre code {
    background: none;
    padding: 0;
    font-size: 13px;
    line-height: 1.5;
}

.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px 8px 0 0;
    font-size: 12px;
    color: var(--text-muted);
}

.code-header + pre {
    border-radius: 0 0 8px 8px;
    margin-top: 0;
}

.btn-copy {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 12px;
    padding: 2px 8px;
    border-radius: 4px;
}

.btn-copy:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

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

.message-content li {
    margin-bottom: 4px;
}

.message-content blockquote {
    border-left: 3px solid var(--accent);
    padding-left: 16px;
    margin: 12px 0;
    color: var(--text-secondary);
}

.message-content h1, .message-content h2, .message-content h3 {
    margin: 16px 0 8px;
    font-weight: 600;
}

.message-content h1 { font-size: 20px; }
.message-content h2 { font-size: 18px; }
.message-content h3 { font-size: 16px; }

.message-content table {
    border-collapse: collapse;
    margin: 12px 0;
    width: 100%;
}

.message-content th, .message-content td {
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    text-align: left;
}

.message-content th {
    background: var(--bg-tertiary);
}

.message-meta {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 8px;
}

/* Thinking block */
.thinking-block {
    margin: 8px 0;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
}

.thinking-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 12px;
    background: var(--bg-tertiary);
    cursor: pointer;
    font-size: 13px;
    color: var(--text-muted);
    user-select: none;
}

.thinking-header .arrow {
    transition: transform 0.2s;
    font-size: 10px;
}

.thinking-header.open .arrow {
    transform: rotate(90deg);
}

.thinking-content {
    display: none;
    padding: 12px;
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.6;
    white-space: pre-wrap;
}

.thinking-content.open {
    display: block;
}

/* Loading animation */
.loading-dots {
    display: inline-flex;
    gap: 4px;
}

.loading-dots span {
    width: 6px;
    height: 6px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: bounce 1.4s infinite;
}

.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
    0%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-8px); }
}

/* ========== Input Area ========== */
.input-area {
    padding: 12px 20px 20px;
    flex-shrink: 0;
}

.input-wrapper {
    max-width: 768px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 8px 12px;
    transition: border-color 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--accent);
}

.input-wrapper textarea {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 15px;
    font-family: var(--font-sans);
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 200px;
    min-height: 24px;
}

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

.btn-send, .btn-stop {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s;
}

.btn-send {
    background: var(--accent);
    color: #fff;
}

.btn-send:disabled {
    background: var(--bg-hover);
    color: var(--text-muted);
    cursor: not-allowed;
}

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

.btn-stop {
    background: var(--error);
    color: #fff;
}

.btn-stop:hover {
    background: #dc2626;
}

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

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--bg-hover);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ========== Login Footer ========== */
.login-footer {
    text-align: center;
    margin-top: 16px;
    font-size: 13px;
    color: var(--text-muted);
}

.login-footer a {
    color: var(--accent);
    text-decoration: none;
}

.login-footer a:hover {
    text-decoration: underline;
}

.login-divider {
    border-top: 1px solid var(--border-color);
    margin-top: 20px;
}

.login-notice {
    margin-top: 20px;
    padding: 12px 14px;
    background: rgba(245, 158, 11, 0.08);
    border: 1px solid rgba(245, 158, 11, 0.2);
    border-radius: 8px;
}

.login-notice p {
    font-size: 11px;
    line-height: 1.7;
    color: var(--text-muted);
    text-align: center;
}

/* ========== Profile & Admin ========== */
.profile-container, .admin-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 20px;
}

.profile-nav {
    display: flex;
    gap: 16px;
    margin-bottom: 20px;
}

.back-link, .admin-link {
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 14px;
}

.back-link:hover, .admin-link:hover {
    color: var(--accent);
}

.admin-title {
    font-size: 24px;
    font-weight: 700;
    margin-bottom: 20px;
}

.profile-card {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 24px;
    margin-bottom: 16px;
}

.profile-card h2 {
    font-size: 16px;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.success-message {
    background: rgba(16, 163, 127, 0.1);
    border: 1px solid rgba(16, 163, 127, 0.3);
    color: var(--accent);
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    margin-bottom: 16px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 16px;
}

.stat-item {
    text-align: center;
    padding: 12px;
    background: var(--bg-secondary);
    border-radius: 8px;
}

.stat-value {
    display: block;
    font-size: 24px;
    font-weight: 700;
    color: var(--accent);
}

.stat-label {
    display: block;
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 4px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.danger-zone {
    border-color: rgba(239, 68, 68, 0.3);
}

.danger-text {
    color: var(--text-secondary);
    font-size: 13px;
    margin-bottom: 12px;
}

.btn-danger {
    width: 100%;
    padding: 12px;
    margin-top: 8px;
    background: var(--error);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}

.btn-danger:hover {
    background: #dc2626;
}

/* ========== Admin Table ========== */
.table-wrapper {
    overflow-x: auto;
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}

.admin-table th, .admin-table td {
    padding: 10px 8px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.admin-table th {
    color: var(--text-muted);
    font-weight: 500;
    font-size: 12px;
    text-transform: uppercase;
}

.admin-table .row-disabled {
    opacity: 0.5;
}

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
}

.badge-active { background: rgba(16, 163, 127, 0.15); color: var(--accent); }
.badge-inactive { background: rgba(239, 68, 68, 0.15); color: var(--error); }
.badge-admin { background: rgba(99, 102, 241, 0.15); color: #818cf8; }

.role-select {
    background: var(--bg-tertiary);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    color: var(--text-primary);
    font-size: 12px;
    padding: 4px 6px;
}

.actions {
    display: flex;
    gap: 4px;
}

.btn-sm {
    padding: 4px 10px;
    border: none;
    border-radius: 4px;
    font-size: 11px;
    cursor: pointer;
    white-space: nowrap;
}

.btn-warn { background: rgba(245, 158, 11, 0.2); color: #f59e0b; }
.btn-warn:hover { background: rgba(245, 158, 11, 0.3); }
.btn-ok { background: rgba(16, 163, 127, 0.2); color: var(--accent); }
.btn-ok:hover { background: rgba(16, 163, 127, 0.3); }
.btn-danger-sm { background: rgba(239, 68, 68, 0.2); color: var(--error); }
.btn-danger-sm:hover { background: rgba(239, 68, 68, 0.3); }

.text-warning { color: #f59e0b; font-weight: 600; }
.text-muted { color: var(--text-muted); }

/* ========== User Info (sidebar) ========== */
.btn-admin {
    color: #818cf8;
    text-decoration: none;
    font-size: 12px;
}

.btn-admin:hover {
    color: #a5b4fc;
}

.username {
    color: var(--text-secondary);
    text-decoration: none;
    cursor: pointer;
}

.username:hover {
    color: var(--accent);
}

/* ========== Responsive ========== */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 100;
        transform: translateX(-100%);
    }

    .sidebar.visible {
        transform: translateX(0);
    }

    .message {
        padding-left: 16px;
        padding-right: 16px;
    }

    .input-area {
        padding: 8px 12px 12px;
    }
}
