/* ── Chat View Styles ───────────────────────────────────────────────
 * Gaps: 1 (Chat UI), 2 (Markdown), 17 (Code blocks), 19 (Theme-aware)
 *
 * Uses CSS custom properties for theme compatibility.
 * Dark mode ready with :root overrides.
 */

/* ── Container Layout ─────────────────────────────────────────────*/
.chat-container {
    display: flex;
    height: 100%;
    min-height: 0;
    background: var(--chat-bg, #0d1117);
    color: var(--chat-text, #e6edf3);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    border-radius: 12px;
    overflow: hidden;
}

/* ── Sidebar ──────────────────────────────────────────────────────*/
.chat-sidebar {
    width: 260px;
    min-width: 200px;
    background: var(--chat-sidebar-bg, #010409);
    border-right: 1px solid var(--chat-border, #21262d);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--chat-border, #21262d);
}

.chat-sidebar-header h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
}

.chat-agent-select {
    padding: 12px 16px;
    border-bottom: 1px solid var(--chat-border, #21262d);
}

.chat-agent-select label {
    display: block;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--chat-muted, #8b949e);
    margin-bottom: 6px;
}

.chat-agent-select select {
    width: 100%;
    padding: 6px 10px;
    background: var(--chat-input-bg, #161b22);
    color: var(--chat-text, #e6edf3);
    border: 1px solid var(--chat-border, #21262d);
    border-radius: 6px;
    font-size: 13px;
    outline: none;
}

.chat-agent-select select:focus {
    border-color: var(--chat-accent, #58a6ff);
}

.chat-sessions-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-session-item {
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-session-item:hover {
    background: var(--chat-hover, #161b22);
}

.chat-session-item.active {
    background: var(--chat-active, #1f6feb22);
    color: var(--chat-accent, #58a6ff);
}

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

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    border-bottom: 1px solid var(--chat-border, #21262d);
    background: var(--chat-header-bg, #010409);
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-header-title {
    font-size: 14px;
    font-weight: 600;
}

.chat-header-status {
    font-size: 11px;
    padding: 2px 8px;
    border-radius: 10px;
    background: var(--chat-status-bg, #161b22);
    color: var(--chat-muted, #8b949e);
}

.chat-status-connected {
    color: #3fb950;
    background: #3fb95015;
}

.chat-status-streaming {
    color: #d29922;
    background: #d2992215;
}

.chat-status-error {
    color: #f85149;
    background: #f8514915;
}

/* ── Messages ─────────────────────────────────────────────────────*/
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.chat-welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    text-align: center;
    gap: 8px;
    opacity: 0.7;
}

.chat-welcome-icon {
    font-size: 48px;
}

.chat-welcome h2 {
    margin: 0;
    font-size: 20px;
}

.chat-welcome p {
    margin: 0;
    font-size: 14px;
    color: var(--chat-muted, #8b949e);
}

.chat-msg {
    display: flex;
    gap: 12px;
    max-width: 85%;
    animation: chat-msg-in 0.2s ease;
}

@keyframes chat-msg-in {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-msg-user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.chat-msg-assistant {
    align-self: flex-start;
}

.chat-msg-system {
    align-self: center;
    max-width: 70%;
}

.chat-msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    background: var(--chat-avatar-bg, #161b22);
}

.chat-msg-body {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.chat-msg-agent-name {
    font-size: 11px;
    font-weight: 600;
    color: var(--chat-accent, #58a6ff);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.chat-msg-content {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
}

.chat-msg-user .chat-msg-content {
    background: var(--chat-user-bubble, #1f6feb);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-msg-assistant .chat-msg-content {
    background: var(--chat-assist-bubble, #161b22);
    border: 1px solid var(--chat-border, #21262d);
    border-bottom-left-radius: 4px;
}

.chat-msg-system .chat-msg-content {
    background: transparent;
    color: var(--chat-muted, #8b949e);
    font-size: 12px;
    text-align: center;
}

.chat-msg-streaming .chat-msg-content,
.chat-msg-content.chat-msg-streaming {
    border-color: var(--chat-accent, #58a6ff);
}

/* ── Markdown Styles ──────────────────────────────────────────────*/
.chat-msg-content strong {
    font-weight: 600;
}

.chat-msg-content em {
    font-style: italic;
}

.chat-msg-content a {
    color: var(--chat-accent, #58a6ff);
    text-decoration: none;
}

.chat-msg-content a:hover {
    text-decoration: underline;
}

.chat-inline-code {
    background: var(--chat-code-bg, #1a1e24);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 12px;
}

.chat-code-block {
    background: var(--chat-code-bg, #1a1e24);
    border: 1px solid var(--chat-border, #21262d);
    border-radius: 8px;
    padding: 12px 16px;
    margin: 8px 0;
    overflow-x: auto;
    font-family: 'SF Mono', 'Fira Code', monospace;
    font-size: 12px;
    line-height: 1.5;
}

/* ── Tool Call Display ────────────────────────────────────────────*/
.chat-tool-call {
    background: var(--chat-tool-bg, #161b22);
    border: 1px solid var(--chat-border, #21262d);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 12px;
    max-width: 85%;
    animation: chat-msg-in 0.2s ease;
}

.chat-tool-header {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-tool-icon {
    font-size: 14px;
}

.chat-tool-name {
    font-weight: 600;
    font-family: 'SF Mono', 'Fira Code', monospace;
}

.chat-tool-status {
    margin-left: auto;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.chat-tool-running .chat-tool-status {
    color: #d29922;
}

.chat-tool-done .chat-tool-status {
    color: #3fb950;
}

.chat-tool-args pre,
.chat-tool-result pre {
    margin: 6px 0 0;
    padding: 8px;
    background: var(--chat-code-bg, #1a1e24);
    border-radius: 6px;
    font-size: 11px;
    overflow-x: auto;
    max-height: 150px;
    overflow-y: auto;
}

/* ── Reasoning Display ────────────────────────────────────────────*/
.chat-reasoning {
    font-size: 12px;
    color: var(--chat-muted, #8b949e);
    padding: 6px 12px;
    border-left: 2px solid var(--chat-border, #21262d);
    margin: 4px 0;
    font-style: italic;
}

.chat-reasoning-icon {
    margin-right: 6px;
}

/* ── Input Area ───────────────────────────────────────────────────*/
.chat-input-area {
    padding: 16px 20px;
    border-top: 1px solid var(--chat-border, #21262d);
    background: var(--chat-header-bg, #010409);
}

.chat-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--chat-input-bg, #161b22);
    border: 1px solid var(--chat-border, #21262d);
    border-radius: 12px;
    padding: 8px 12px;
    transition: border-color 0.15s;
}

.chat-input-wrapper:focus-within {
    border-color: var(--chat-accent, #58a6ff);
}

.chat-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--chat-text, #e6edf3);
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    max-height: 150px;
    line-height: 1.4;
}

.chat-input::placeholder {
    color: var(--chat-muted, #8b949e);
}

/* ── Buttons ──────────────────────────────────────────────────────*/
.chat-btn {
    border: none;
    background: transparent;
    color: var(--chat-text, #e6edf3);
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 8px;
    font-size: 14px;
    transition: background 0.15s, opacity 0.15s;
}

.chat-btn:hover {
    background: var(--chat-hover, #21262d);
}

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

.chat-btn-send {
    background: var(--chat-accent, #1f6feb);
    color: #fff;
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    flex-shrink: 0;
}

.chat-btn-send:hover {
    background: var(--chat-accent-hover, #388bfd);
}

.chat-btn-new {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-btn-abort {
    background: var(--chat-error, #f85149);
    color: #fff;
    font-size: 12px;
    padding: 4px 10px;
}

/* ── Light Theme (Gap 19) ─────────────────────────────────────────*/
[data-theme="light"] .chat-container,
.chat-container.light {
    --chat-bg: #fff;
    --chat-text: #1f2328;
    --chat-sidebar-bg: #f6f8fa;
    --chat-border: #d0d7de;
    --chat-muted: #656d76;
    --chat-hover: #f3f4f6;
    --chat-active: #ddf4ff;
    --chat-accent: #0969da;
    --chat-accent-hover: #0550ae;
    --chat-header-bg: #f6f8fa;
    --chat-input-bg: #f6f8fa;
    --chat-avatar-bg: #eaeef2;
    --chat-user-bubble: #0969da;
    --chat-assist-bubble: #f6f8fa;
    --chat-code-bg: #f6f8fa;
    --chat-tool-bg: #f6f8fa;
    --chat-status-bg: #eaeef2;
    --chat-error: #cf222e;
}

/* ── Responsive ───────────────────────────────────────────────────*/
@media (max-width: 768px) {
    .chat-sidebar {
        display: none;
    }

    .chat-msg {
        max-width: 95%;
    }
}