/* ═══════════════════════════════════════════════════════════════
   Tanngrisnir — App Styles
   ═══════════════════════════════════════════════════════════════ */

:root {
    --sidebar-width: 260px;
    --artifact-panel-width: 300px;
    --composer-height: auto;
    --header-height: 52px;
    --sidebar-bg: #111318;
    --chat-bg: #181c24;
    --message-user-bg: #1e2433;
    --message-assistant-bg: transparent;
    --border-color: rgba(255,255,255,0.08);
    --accent: #6c8ebf;
    --text-muted: #8892a0;
}

[data-bs-theme="light"] {
    --sidebar-bg: #f5f5f7;
    --chat-bg: #ffffff;
    --message-user-bg: #f0f2f5;
    --border-color: rgba(0,0,0,0.08);
    --text-muted: #6c757d;
}

/* ── Reset / Globals ── */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }

.btn-reset {
    background: none; border: none; padding: 0; cursor: pointer;
    color: inherit; font: inherit;
}
.btn-ghost {
    background: none; border: none; padding: 4px 8px; cursor: pointer;
    color: var(--bs-body-color); border-radius: 6px; line-height: 1;
    opacity: 0.6; transition: opacity 0.15s;
}
.btn-ghost:hover { opacity: 1; background: var(--border-color); }

[x-cloak] { display: none !important; }

/* ═══════════════════════════════════════════ CHAT LAYOUT ════ */

.chat-layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
    position: relative;
}

/* ── Sidebar ── */
.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: transform 0.25s ease;
    z-index: 100;
}

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

.sidebar-brand {
    font-weight: 700;
    font-size: 1rem;
    text-decoration: none;
    color: var(--bs-body-color);
    letter-spacing: -0.02em;
}

.sidebar-close {
    border: none; background: none; padding: 2px;
    color: var(--text-muted); cursor: pointer;
}

.sidebar-section {
    padding: 10px 12px 6px;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-label {
    display: block;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 6px;
    font-weight: 600;
}

.sidebar-search .form-control {
    font-size: 0.85rem;
    border-radius: 8px;
}

/* Agents list */
.agent-list { display: flex; flex-direction: column; gap: 1px; }
.agent-item {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 8px; border-radius: 6px;
    text-decoration: none; color: var(--bs-body-color);
    font-size: 0.85rem; transition: background 0.1s;
}
.agent-item:hover { background: var(--border-color); color: var(--bs-body-color); }
.agent-item.active { background: rgba(108, 142, 191, 0.15); color: var(--accent); }
.agent-item.agent-manage { opacity: 0.6; }
.agent-icon { font-size: 0.75rem; flex-shrink: 0; opacity: 0.7; }

/* Conversation list */
.sidebar-convos {
    flex: 1;
    overflow-y: auto;
    padding: 6px 0;
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

.convo-group { margin-bottom: 4px; }
.convo-group-label {
    padding: 8px 14px 3px;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    font-weight: 600;
}

.convo-item {
    position: relative;
    display: flex;
    align-items: center;
    padding: 2px 8px 2px 12px;
    border-radius: 6px;
    margin: 0 4px;
    transition: background 0.1s;
}
.convo-item:hover { background: var(--border-color); }
.convo-item.active { background: rgba(108, 142, 191, 0.12); }

.convo-link {
    flex: 1;
    text-decoration: none;
    color: var(--bs-body-color);
    font-size: 0.82rem;
    padding: 5px 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.convo-actions { display: flex; align-items: center; }
.convo-menu-btn {
    background: none; border: none; padding: 2px 6px;
    color: var(--text-muted); cursor: pointer; font-size: 1rem;
    opacity: 0; transition: opacity 0.1s; border-radius: 4px;
}
.convo-item:hover .convo-menu-btn { opacity: 1; }
.convo-menu-btn:hover { background: var(--border-color); color: var(--bs-body-color); }

.convo-menu {
    position: absolute; right: 8px; top: 100%; z-index: 200;
    background: var(--bs-body-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px; padding: 4px;
    min-width: 120px; box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.convo-menu-item {
    display: block; width: 100%; text-align: left;
    background: none; border: none; padding: 6px 10px;
    font-size: 0.82rem; cursor: pointer; border-radius: 4px;
    color: var(--bs-body-color);
}
.convo-menu-item:hover { background: var(--border-color); }

/* Sidebar footer */
.sidebar-footer {
    padding: 10px 14px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.sidebar-footer-link {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
    display: flex; align-items: center; gap: 5px;
    transition: color 0.1s;
}
.sidebar-footer-link:hover { color: var(--bs-body-color); }

/* ── Chat Main ── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--chat-bg);
}

/* Chat header */
.chat-header {
    height: var(--header-height);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 10px;
    flex-shrink: 0;
}
.chat-header-left { flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; }
.chat-header-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.chat-title {
    font-size: 0.9rem;
    font-weight: 600;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
    border-bottom: 1px solid transparent;
    transition: border-color 0.15s;
}
.chat-title:hover { border-bottom-color: var(--border-color); }

.loop-status { display: flex; align-items: center; gap: 6px; }

/* Messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 0 8px;
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: var(--border-color) transparent;
}

.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px 20px;
    color: var(--text-muted);
}
.chat-empty-icon { font-size: 2.5rem; margin-bottom: 12px; opacity: 0.5; }

/* Messages */
.message {
    display: flex;
    gap: 12px;
    padding: 10px 20px;
    max-width: 100%;
    animation: messageIn 0.2s ease;
}
@keyframes messageIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.message-user {
    flex-direction: row-reverse;
}
.message-user .message-body {
    max-width: 72%;
    background: var(--message-user-bg);
    border-radius: 14px 14px 4px 14px;
    padding: 10px 14px;
}

.message-assistant .message-body {
    max-width: 85%;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
    flex-shrink: 0;
    margin-top: 2px;
}
.message-user .message-avatar { background: #4a7fbf; }

.message-content {
    font-size: 0.9rem;
    line-height: 1.6;
}

/* Markdown body styles */
.markdown-body h1, .markdown-body h2, .markdown-body h3 { margin-top: 1em; margin-bottom: 0.5em; }
.markdown-body h1 { font-size: 1.4em; }
.markdown-body h2 { font-size: 1.2em; }
.markdown-body h3 { font-size: 1.05em; }
.markdown-body p { margin-bottom: 0.7em; }
.markdown-body ul, .markdown-body ol { padding-left: 1.5em; margin-bottom: 0.7em; }
.markdown-body li { margin-bottom: 0.2em; }
.markdown-body code {
    background: rgba(255,255,255,0.08);
    padding: 0.15em 0.4em; border-radius: 4px;
    font-size: 0.85em; font-family: 'SF Mono', 'Cascadia Code', 'Fira Code', monospace;
}
[data-bs-theme="light"] .markdown-body code { background: rgba(0,0,0,0.06); }
.markdown-body pre {
    background: #0d1117; border-radius: 8px; padding: 14px;
    overflow-x: auto; margin: 0.8em 0;
}
.markdown-body pre code { background: none; padding: 0; font-size: 0.82em; }
[data-bs-theme="light"] .markdown-body pre { background: #f6f8fa; }
.markdown-body blockquote {
    border-left: 3px solid var(--accent); margin: 0.7em 0;
    padding: 4px 12px; color: var(--text-muted);
}
.markdown-body table { width: 100%; border-collapse: collapse; margin: 0.8em 0; font-size: 0.85em; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border-color); padding: 6px 10px; }
.markdown-body th { background: var(--message-user-bg); }
.markdown-body a { color: var(--accent); }
.markdown-body hr { border-color: var(--border-color); margin: 1em 0; }

/* Tool call blocks */
.tool-call-block {
    margin: 8px 0;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    font-size: 0.82rem;
}
.tool-call-header {
    display: flex; align-items: center; gap: 8px;
    padding: 7px 12px;
    background: rgba(255,255,255,0.04);
    border-bottom: 1px solid var(--border-color);
}
[data-bs-theme="light"] .tool-call-header { background: rgba(0,0,0,0.03); }
.tool-call-icon { font-size: 0.8rem; color: var(--accent); }
.tool-call-name { font-weight: 600; font-family: monospace; }
.tool-call-input {
    padding: 8px 12px;
    background: rgba(0,0,0,0.15);
    overflow-x: auto;
    max-height: 200px;
    white-space: pre-wrap;
    word-break: break-word;
}
[data-bs-theme="light"] .tool-call-input { background: rgba(0,0,0,0.04); }

/* Typing indicator */
.typing-indicator {
    display: flex; align-items: center; gap: 4px;
    padding: 8px 0;
}
.typing-indicator span {
    width: 6px; height: 6px; border-radius: 50%;
    background: var(--text-muted);
    animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-5px); opacity: 1; }
}

/* Message actions */
.message-actions {
    display: flex; gap: 4px; margin-top: 6px; opacity: 0;
    transition: opacity 0.15s;
}
.message-assistant:hover .message-actions { opacity: 1; }
.msg-action-btn {
    background: none; border: none; padding: 3px 6px;
    color: var(--text-muted); cursor: pointer;
    border-radius: 4px; font-size: 0.75rem;
    transition: background 0.1s, color 0.1s;
}
.msg-action-btn:hover { background: var(--border-color); color: var(--bs-body-color); }

/* HITL approval */
.hitl-approval { padding: 0 20px; }
.hitl-card {
    border: 1px solid #ffc107;
    border-radius: 10px;
    padding: 14px 16px;
    background: rgba(255, 193, 7, 0.06);
}
.hitl-card pre { background: rgba(0,0,0,0.2); border-radius: 6px; padding: 8px; font-size: 0.75rem; }

/* ── Composer ── */
.chat-composer {
    flex-shrink: 0;
    border-top: 1px solid var(--border-color);
    padding: 10px 16px 14px;
}

.composer-config {
    padding: 12px 4px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 10px;
}
.composer-config-inner { max-width: 100%; }

.composer-bar {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--message-user-bg);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: 8px 10px;
    transition: border-color 0.15s;
}
.composer-bar:focus-within { border-color: var(--accent); }

.composer-textarea {
    flex: 1;
    background: none;
    border: none;
    outline: none;
    resize: none;
    font-size: 0.9rem;
    line-height: 1.5;
    color: var(--bs-body-color);
    max-height: 200px;
    overflow-y: auto;
    padding: 2px 0;
    scrollbar-width: thin;
}
.composer-textarea::placeholder { color: var(--text-muted); }

.composer-btn {
    background: none; border: none;
    color: var(--text-muted); cursor: pointer;
    padding: 4px; border-radius: 6px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; transition: color 0.15s, background 0.15s;
    align-self: flex-end; margin-bottom: 1px;
}
.composer-btn:hover { color: var(--bs-body-color); background: var(--border-color); }

.composer-send {
    background: var(--accent);
    border: none; color: white; cursor: pointer;
    width: 34px; height: 34px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; transition: background 0.15s, opacity 0.15s;
    align-self: flex-end;
}
.composer-send:hover:not(:disabled) { background: #5a7aab; }
.composer-send:disabled { opacity: 0.4; cursor: not-allowed; }

.composer-footer {
    padding: 4px 2px 0;
    font-size: 0.75rem;
    color: var(--text-muted);
    display: flex; align-items: center;
}

/* ── Artifact Panel ── */
.artifact-panel {
    width: var(--artifact-panel-width);
    flex-shrink: 0;
    border-left: 1px solid var(--border-color);
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    position: absolute; right: 0; top: 0; height: 100%;
    z-index: 90;
}
.artifact-panel.open { transform: translateX(0); }

@media (min-width: 1200px) {
    .artifact-panel {
        position: relative;
        transform: none;
        display: none;
    }
    .artifact-panel.open { display: flex; }
}

.artifact-panel-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 14px 12px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.85rem;
}
.artifact-panel-body { flex: 1; overflow-y: auto; padding: 10px; }
.artifact-card-link { text-decoration: none; display: block; margin-bottom: 8px; }
.artifact-card {
    border: 1px solid var(--border-color);
    border-radius: 8px; padding: 10px 12px;
    transition: background 0.1s; color: var(--bs-body-color);
}
.artifact-card:hover { background: var(--border-color); }
.artifact-card-title { font-size: 0.82rem; font-weight: 500; margin-bottom: 2px; }
.artifact-card-date { font-size: 0.72rem; }

/* Sidebar backdrop (mobile) */
.sidebar-backdrop {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 99;
    opacity: 0; pointer-events: none;
    transition: opacity 0.25s;
}
.sidebar-backdrop.show { opacity: 1; pointer-events: all; }

/* ═══════════════════════════════════════ MOBILE ════ */
@media (max-width: 767.98px) {
    .sidebar {
        position: fixed; top: 0; left: 0; height: 100%;
        transform: translateX(-100%);
        z-index: 100;
    }
    .sidebar.sidebar-open { transform: translateX(0); }
    .chat-main { width: 100%; }
    .message-user .message-body { max-width: 88%; }
    .message-assistant .message-body { max-width: 95%; }
}

/* ═══════════════════════════════════════ ARTIFACT LIBRARY ════ */
.artifact-card-grid {
    transition: box-shadow 0.15s;
    color: var(--bs-body-color);
}
.artifact-card-grid:hover { box-shadow: 0 0 0 2px var(--accent); }
