/* Conversation Styles — forked from causal_chat, adapted for memory panel */

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg-primary: #f8f9fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e9ecef;
    --text-primary: #212529;
    --text-secondary: #495057;
    --text-muted: #6c757d;
    --accent: #0066cc;
    --accent-hover: #0052a3;
    --border: #dee2e6;
    --success: #28a745;
    --user-bg: #e3f2fd;
    --assistant-bg: #ffffff;
    --memory-accent: #6f42c1;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.container {
    display: flex; flex-direction: column; height: 100vh;
    max-width: 1200px; margin: 0 auto; width: 100%;
}

/* Header */
header {
    padding: 0.75rem 1rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    display: flex; justify-content: space-between; align-items: center;
    flex-wrap: wrap; gap: 0.75rem;
}

.header-left { display: flex; align-items: center; gap: 0.75rem; }

header h1 { font-size: 1.15rem; font-weight: 600; }

.home-link {
    padding: 0.4rem 0.75rem; background: var(--bg-tertiary);
    color: var(--text-primary); text-decoration: none; border-radius: 6px;
    font-size: 0.9rem; font-weight: 600; transition: all 0.2s;
    border: 2px solid #ced4da;
}
.home-link:hover { background: var(--accent); color: white; border-color: var(--accent); }

.controls { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }
.control-group { display: flex; align-items: center; gap: 0.4rem; }
.control-group label { font-size: 0.8rem; color: var(--text-secondary); }

select {
    background: var(--bg-secondary); border: 1px solid var(--border);
    color: var(--text-primary); padding: 0.4rem; border-radius: 6px;
    font-size: 0.8rem; min-width: 140px;
}
select:focus { outline: none; border-color: var(--accent); }

.btn-secondary {
    background: var(--bg-secondary); border: 1px solid #ced4da;
    color: var(--text-primary); padding: 0.4rem 0.75rem; border-radius: 6px;
    cursor: pointer; font-size: 0.8rem; font-weight: 500; transition: all 0.2s;
    text-decoration: none;
}
.btn-secondary:hover { background: var(--bg-tertiary); border-color: var(--text-muted); }
.btn-secondary:disabled { opacity: 0.5; cursor: not-allowed; }

.session-badge {
    display: inline-block; padding: 0.2rem 0.6rem;
    background: var(--user-bg); border: 1px solid var(--accent);
    border-radius: 16px; font-size: 0.8rem; color: var(--accent); font-weight: 500;
}

.config-badge {
    display: inline-block; padding: 0.2rem 0.4rem;
    background: var(--bg-tertiary); border-radius: 4px;
    font-size: 0.7rem; color: var(--text-secondary);
}

/* Chat */
#chat-container { flex: 1; overflow-y: auto; padding: 1rem; background: var(--bg-primary); }
#messages { display: flex; flex-direction: column; gap: 1rem; }

.message {
    padding: 1rem; border-radius: 12px; max-width: 85%;
    line-height: 1.6; box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    position: relative;
}
.message.user {
    background: var(--user-bg); align-self: flex-end;
    border-bottom-right-radius: 4px; white-space: pre-wrap;
}
.message.assistant {
    background: var(--assistant-bg); border: 1px solid var(--border);
    align-self: flex-start; border-bottom-left-radius: 4px;
}
.message.system {
    background: var(--bg-secondary); border: 1px solid var(--border);
    align-self: center; text-align: center;
    font-size: 0.875rem; color: var(--text-secondary);
}
.message.loading { color: var(--text-muted); font-style: italic; }
.message.error { background: #fff5f5; border: 1px solid #feb2b2; color: #c53030; }

/* Earlier context block */
.earlier-context {
    display: flex; flex-direction: column; gap: 0.75rem;
    padding-bottom: 1rem; margin-bottom: 1rem;
    border-bottom: 1px dashed var(--border);
    opacity: 0.65;
}
.earlier-context-label {
    align-self: center;
    font-size: 0.75rem; font-weight: 600; text-transform: uppercase;
    color: var(--text-muted); letter-spacing: 0.05em;
    padding: 0.25rem 0.75rem;
    background: var(--bg-tertiary); border-radius: 12px;
}
.message.seeded {
    font-size: 0.9em;
}

/* Timestamp label */
.message .msg-timestamp {
    position: absolute; top: 0.4rem; left: 0.6rem;
    font-size: 0.65rem; color: var(--text-muted);
    pointer-events: none;
}

/* Copy button */
.message .copy-btn {
    position: absolute; top: 0.5rem; right: 0.5rem;
    padding: 0.2rem 0.4rem; background: var(--bg-tertiary);
    border: 1px solid var(--border); border-radius: 4px;
    font-size: 0.7rem; color: var(--text-muted); cursor: pointer;
    opacity: 0; transition: opacity 0.2s;
}
.message:hover .copy-btn { opacity: 1; }
.message .copy-btn.copied { background: var(--success); color: white; border-color: var(--success); }

/* Markdown formatting */
.message strong { font-weight: 600; }
.message h2 { margin-top: 1.25rem; margin-bottom: 0.75rem; font-size: 1.2em; font-weight: 600; border-bottom: 1px solid var(--border); padding-bottom: 0.5rem; }
.message h3 { margin-top: 1rem; margin-bottom: 0.5rem; font-size: 1.1em; font-weight: 600; }
.message h4 { margin-top: 0.75rem; margin-bottom: 0.5rem; font-size: 1em; font-weight: 600; }
.message ul, .message ol { margin-left: 1.5rem; margin-top: 0.5rem; }
.message blockquote {
    border-left: 3px solid var(--text-muted); padding: 0.5rem 1rem;
    margin: 0.75rem 0; color: var(--text-secondary);
    background: var(--bg-primary); border-radius: 0 6px 6px 0;
}
.message code { background: var(--bg-tertiary); padding: 0.2rem 0.4rem; border-radius: 4px; font-family: monospace; font-size: 0.875em; }
.message pre { background: var(--bg-tertiary); padding: 1rem; border-radius: 6px; overflow-x: auto; margin: 0.5rem 0; }
.message hr { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.message table { border-collapse: collapse; width: 100%; margin: 0.75rem 0; font-size: 0.9em; }
.message th, .message td { border: 1px solid var(--border); padding: 0.5rem 0.75rem; text-align: left; }
.message th { background: var(--bg-tertiary); font-weight: 600; }

/* Causal chain box */
.message .causal-chain {
    background: var(--bg-primary); border-left: 3px solid var(--accent);
    padding: 0.6rem 1rem; margin: 0.75rem 0; font-weight: 500;
    border-radius: 0 6px 6px 0; line-height: 1.8;
}

/* Footer */
footer {
    padding: 0.75rem 1rem; background: var(--bg-secondary);
    border-top: 1px solid var(--border);
}
#chat-form { display: flex; gap: 0.5rem; align-items: flex-end; }
#query-input {
    flex: 1; padding: 0.75rem 1rem; border: 1px solid var(--border);
    border-radius: 8px; background: var(--bg-secondary); color: var(--text-primary);
    font-size: 1rem; font-family: inherit; resize: none;
    min-height: 42px; max-height: 200px; line-height: 1.4; overflow-y: auto;
}
#query-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0,102,204,0.1); }
#query-input:disabled { opacity: 0.5; }
#send-btn {
    padding: 0.75rem 1.5rem; background: var(--accent); border: none;
    border-radius: 8px; color: white; font-size: 1rem; font-weight: 500;
    cursor: pointer; transition: background 0.2s;
}
#send-btn:hover { background: var(--accent-hover); }
#send-btn:disabled { background: var(--text-muted); cursor: not-allowed; }

#usage-stats {
    display: flex; gap: 1.5rem; margin-top: 0.5rem;
    font-size: 0.75rem; color: var(--text-muted); justify-content: center;
}
#usage-stats .label { color: var(--text-muted); }
#usage-stats .value { color: var(--accent); font-weight: 600; }

/* Split container */
.split-container { display: flex; height: 100vh; width: 100%; }
.chat-main { flex: 1; min-width: 0; max-width: none; }

.resize-handle {
    display: none; width: 6px; background: var(--border);
    cursor: col-resize; flex-shrink: 0; transition: background 0.2s;
}
.resize-handle:hover { background: var(--accent); }

.graph-panel {
    width: 350px; flex-shrink: 0; display: flex; flex-direction: column;
    border-left: 1px solid var(--border); background: var(--bg-primary);
}
.graph-panel.left-panel {
    border-left: none; border-right: 1px solid var(--border);
    order: -2; /* ensure it's first in the flex container */
}
#search-resize-handle { order: -1; }
.graph-panel-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 0.5rem 0.75rem; background: var(--bg-secondary);
    border-bottom: 1px solid var(--border); font-weight: 600; font-size: 0.875rem;
    flex-shrink: 0;
}
.graph-panel-close {
    background: none; border: none; font-size: 1.25rem;
    cursor: pointer; color: var(--text-muted); padding: 0 0.25rem;
}
.graph-panel-close:hover { color: var(--text-primary); }

.active-toggle { background: var(--accent) !important; color: white !important; border-color: var(--accent) !important; }

/* Scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Traces */
.trace-list { display: flex; flex-direction: column; gap: 0.5rem; }
.trace-item {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1rem; background: var(--bg-secondary); border: 1px solid var(--border);
    border-radius: 8px; cursor: pointer; transition: all 0.2s;
    text-decoration: none; color: inherit;
}
.trace-item:hover { border-color: var(--accent); }
.trace-meta { display: flex; gap: 1.5rem; font-size: 0.875rem; color: var(--text-muted); }
.trace-meta .value { color: var(--accent); font-weight: 600; }
.turn { margin-bottom: 1.5rem; padding: 1rem; background: var(--bg-primary); border-radius: 8px; }
.turn-header { display: flex; justify-content: space-between; margin-bottom: 0.75rem; padding-bottom: 0.75rem; border-bottom: 1px solid var(--border); }
.turn-query { color: var(--text-primary); font-weight: 600; }
.turn-stats { font-size: 0.75rem; color: var(--text-muted); }
.iteration { margin: 0.75rem 0; padding: 0.75rem; background: var(--bg-secondary); border: 1px solid var(--border); border-radius: 6px; font-size: 0.875rem; }
.iteration-header { display: flex; justify-content: space-between; margin-bottom: 0.5rem; color: var(--text-secondary); font-size: 0.75rem; }
.tool-call { margin: 0.5rem 0; padding: 0.5rem; background: var(--bg-primary); border-radius: 4px; border-left: 3px solid var(--accent); }
.tool-name { color: var(--accent); font-weight: 600; }
.tool-input, .tool-output { margin-top: 0.5rem; font-family: monospace; font-size: 0.75rem; white-space: pre-wrap; word-break: break-all; max-height: 100px; overflow-y: auto; color: var(--text-secondary); }
.tool-output { color: var(--success); }
.final-response { margin-top: 0.75rem; padding: 0.75rem; background: var(--bg-secondary); border-radius: 6px; border-left: 3px solid var(--success); }
.final-response-label { color: var(--success); font-weight: 600; margin-bottom: 0.5rem; }
.back-link { display: inline-block; margin-bottom: 1rem; color: var(--accent); text-decoration: none; }
.back-link:hover { text-decoration: underline; }
.empty-state { text-align: center; padding: 3rem; color: var(--text-muted); }
.resume-btn { display: inline-block; padding: 0.5rem 1rem; background: var(--accent); color: white; text-decoration: none; border-radius: 6px; font-size: 0.875rem; font-weight: 500; margin-bottom: 1rem; }
.resume-btn:hover { background: var(--accent-hover); }

/* Load More */
#load-more-container {
    display: flex; align-items: center; justify-content: center;
    gap: 0.5rem; padding: 0.4rem 0; margin-bottom: 0.5rem;
}
.load-more-label {
    font-size: 0.75rem; color: var(--text-muted); font-weight: 500;
}
.load-more-group {
    display: inline-flex; border: 1px solid var(--border); border-radius: 6px;
    overflow: hidden;
}
.load-more-opt {
    padding: 0.3rem 0.6rem; border: none; border-right: 1px solid var(--border);
    background: var(--bg-secondary); color: var(--text-secondary);
    font-size: 0.75rem; cursor: pointer; transition: all 0.15s;
}
.load-more-opt:last-child { border-right: none; }
.load-more-opt:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.load-more-opt:disabled { opacity: 0.4; cursor: not-allowed; }

/* History messages (loaded via Load More — in LLM context but visually distinct) */
.message.history-loaded {
    opacity: 0.75; font-size: 0.9em;
}

/* Session dividers */
.session-divider {
    display: flex; align-items: center; gap: 0.75rem;
    margin: 1rem 0; color: var(--text-muted); font-size: 0.7rem;
    text-transform: uppercase; letter-spacing: 0.05em;
}
.session-divider::before, .session-divider::after {
    content: ''; flex: 1; border-top: 1px dashed var(--border);
}

/* Search results */
.search-result {
    padding: 0.6rem; margin-bottom: 0.5rem;
    background: var(--bg-secondary); border: 1px solid var(--border);
    border-radius: 6px; font-size: 0.8rem;
}
.search-result-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 0.3rem;
}
.search-result-actor {
    font-weight: 600; font-size: 0.75rem; color: var(--accent);
    text-transform: capitalize;
}
.search-result-time {
    font-size: 0.7rem; color: var(--text-muted);
}
.search-result-content {
    user-select: text; -webkit-user-select: text;
    white-space: pre-wrap; word-break: break-word;
    color: var(--text-secondary); font-size: 0.8rem;
    line-height: 1.5; margin-bottom: 0.4rem;
    max-height: 200px; overflow-y: auto;
}
.search-insert-btn {
    font-size: 0.7rem; padding: 0.2rem 0.5rem;
}

/* Pin button on messages */
.message .pin-btn {
    position: absolute; top: 0.5rem; right: 3rem;
    padding: 0.2rem 0.4rem; background: var(--bg-tertiary);
    border: 1px solid var(--border); border-radius: 4px;
    font-size: 0.7rem; color: var(--text-muted); cursor: pointer;
    opacity: 0; transition: opacity 0.2s;
}
.message:hover .pin-btn { opacity: 1; }
.message .pin-btn.pinned { background: var(--memory-accent); color: white; border-color: var(--memory-accent); }

/* Reply button on messages */
.message .reply-btn {
    position: absolute; top: 0.5rem; right: 5.5rem;
    padding: 0.2rem 0.4rem; background: var(--bg-tertiary);
    border: 1px solid var(--border); border-radius: 4px;
    font-size: 0.7rem; color: var(--text-muted); cursor: pointer;
    opacity: 0; transition: opacity 0.2s;
}
.message:hover .reply-btn { opacity: 1; }

/* Scrapbook items */
.scrapbook-item {
    padding: 0.6rem; margin-bottom: 0.5rem;
    background: var(--bg-secondary); border: 1px solid var(--border);
    border-radius: 6px; font-size: 0.8rem;
}
.scrapbook-item-header {
    display: flex; align-items: center; gap: 0.4rem;
    margin-bottom: 0.3rem;
}
.scrapbook-source-badge {
    font-size: 0.65rem; font-weight: 600; text-transform: uppercase;
    padding: 0.1rem 0.35rem; border-radius: 3px;
}
.badge-chat { background: var(--user-bg); color: var(--accent); }
.badge-search { background: #f0e6ff; color: var(--memory-accent); }
.scrapbook-time {
    font-size: 0.7rem; color: var(--text-muted); flex: 1;
}
.scrapbook-insert-btn, .scrapbook-unpin {
    background: none; border: 1px solid var(--border); border-radius: 3px;
    font-size: 0.65rem; cursor: pointer; color: var(--text-muted);
    padding: 0.1rem 0.35rem; line-height: 1.2;
}
.scrapbook-insert-btn:hover { color: var(--accent); border-color: var(--accent); }
.scrapbook-unpin:hover { color: var(--text-primary); border-color: var(--text-primary); }
.scrapbook-unpin.confirming { color: #d32f2f; border-color: #d32f2f; font-weight: 600; }
.scrapbook-item-content {
    white-space: pre-wrap; word-break: break-word;
    color: var(--text-secondary); font-size: 0.8rem;
    line-height: 1.5; max-height: 100px; overflow: hidden;
}
.scrapbook-item-content.expanded { max-height: none; overflow: visible; }
.scrapbook-expand-btn {
    background: none; border: none; color: var(--accent);
    font-size: 0.7rem; cursor: pointer; padding: 0.2rem 0; font-weight: 500;
}
.scrapbook-expand-btn:hover { text-decoration: underline; }

/* Search result actions row */
.search-result-actions {
    display: flex; gap: 0.3rem;
}
.search-pin-btn {
    font-size: 0.7rem; padding: 0.2rem 0.5rem;
}

/* Context modal */
.context-modal-overlay {
    display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4); z-index: 1000;
    justify-content: center; align-items: center;
}
.context-modal-overlay.open { display: flex; }
.context-modal {
    background: var(--bg-secondary); border: 1px solid var(--border);
    border-radius: 12px; box-shadow: 0 8px 32px rgba(0,0,0,0.15);
    width: 70vw; max-width: 900px; max-height: 80vh;
    display: flex; flex-direction: column;
}
.context-modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 0.75rem 1rem; border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.context-modal-header h3 { font-size: 0.95rem; font-weight: 600; margin: 0; }
.context-modal-close {
    background: none; border: none; font-size: 1.4rem;
    cursor: pointer; color: var(--text-muted); padding: 0 0.25rem; line-height: 1;
}
.context-modal-close:hover { color: var(--text-primary); }
.context-modal-body {
    overflow-y: auto; padding: 0.5rem 0; flex: 1;
}
.context-load-more {
    display: block; width: 100%; padding: 0.4rem; border: none;
    background: var(--bg-tertiary); color: var(--text-muted);
    font-size: 0.75rem; cursor: pointer; text-align: center;
    transition: all 0.15s;
}
.context-load-more:hover { background: var(--border); color: var(--text-primary); }
.context-load-more:disabled { opacity: 0.4; cursor: not-allowed; }
.context-entry {
    padding: 0.6rem 1rem; border-left: 3px solid transparent;
    line-height: 1.6;
}
.context-entry-anchor {
    background: color-mix(in srgb, var(--accent) 10%, transparent);
    border-left-color: var(--accent);
}
.context-entry-meta {
    display: block; color: var(--text-muted); font-size: 0.7rem;
    margin-bottom: 0.2rem; font-weight: 500;
}
.context-entry-text {
    color: var(--text-secondary); font-size: 0.85rem;
}
/* Markdown inside context entries */
.context-entry-text strong { font-weight: 600; }
.context-entry-text h2, .context-entry-text h3, .context-entry-text h4 { margin-top: 0.5rem; margin-bottom: 0.3rem; font-size: 1em; font-weight: 600; }
.context-entry-text ul, .context-entry-text ol { margin-left: 1.25rem; margin-top: 0.3rem; }
.context-entry-text blockquote { border-left: 3px solid var(--text-muted); padding: 0.3rem 0.75rem; margin: 0.4rem 0; color: var(--text-secondary); }
.context-entry-text code { background: var(--bg-tertiary); padding: 0.1rem 0.3rem; border-radius: 3px; font-family: monospace; font-size: 0.85em; }
.context-entry-text pre { background: var(--bg-tertiary); padding: 0.5rem; border-radius: 4px; overflow-x: auto; margin: 0.3rem 0; }
.context-entry-text table { border-collapse: collapse; width: 100%; margin: 0.4rem 0; font-size: 0.85em; }
.context-entry-text th, .context-entry-text td { border: 1px solid var(--border); padding: 0.3rem 0.5rem; text-align: left; }
.context-entry-text th { background: var(--bg-tertiary); font-weight: 600; }
.context-session-divider {
    text-align: center; font-size: 0.7rem; color: var(--text-muted);
    margin: 0.4rem 0; border-top: 1px dashed var(--border);
    padding-top: 0.3rem;
}
.search-context-btn, .scrapbook-context-btn {
    background: none; border: 1px solid var(--border); border-radius: 3px;
    font-size: 0.65rem; cursor: pointer; color: var(--text-muted);
    padding: 0.1rem 0.35rem; line-height: 1.2;
}
.search-context-btn:hover, .scrapbook-context-btn:hover {
    color: var(--accent); border-color: var(--accent);
}

@media (max-width: 768px) {
    header { flex-direction: column; align-items: stretch; }
    .controls { flex-direction: column; align-items: stretch; }
    .message { max-width: 95%; }
    .graph-panel { position: fixed; top: 0; right: 0; bottom: 0; width: 100% !important; z-index: 1000; }
    .graph-panel.left-panel { right: auto; left: 0; }
    .resize-handle, #search-resize-handle { display: none !important; }
}
