/* AIDataForager — Clean, professional dark/light theme */
:root {
    --bg-primary: #1a1b26;
    --bg-secondary: #24283b;
    --bg-tertiary: #2f3347;
    --text-primary: #c0caf5;
    --text-secondary: #a9b1d6;
    --text-muted: #565f89;
    --accent: #7aa2f7;
    --accent-hover: #89b4fa;
    --success: #9ece6a;
    --warning: #e0af68;
    --error: #f7768e;
    --border: #3b4261;
    --code-bg: #1e2030;
    --shadow: rgba(0,0,0,0.3);
    --radius: 8px;
}

[data-theme="light"] {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e8e8e8;
    --text-primary: #1a1b26;
    --text-secondary: #4a5068;
    --text-muted: #888;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --success: #16a34a;
    --warning: #d97706;
    --error: #dc2626;
    --border: #d1d5db;
    --code-bg: #f0f0f0;
    --shadow: rgba(0,0,0,0.1);
}

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

body {
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
}

/* Header */
.header {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 4px var(--shadow);
}
.header h1 { font-size: 1.4rem; font-weight: 600; }
.header h1 span { color: var(--accent); }
.header-controls { display: flex; gap: 12px; align-items: center; }
.header-controls select, .header-controls button {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 6px 12px;
    border-radius: var(--radius);
    font-size: 0.85rem;
    cursor: pointer;
}
.header-controls button:hover { background: var(--accent); color: #fff; }
.theme-toggle {
    background: none !important;
    border: none !important;
    font-size: 1.2rem;
    cursor: pointer;
}

/* Layout */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 24px;
}

/* Tabs */
.tabs {
    display: flex;
    gap: 4px;
    margin-bottom: 24px;
    border-bottom: 2px solid var(--border);
}
.tab {
    padding: 10px 20px;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.95rem;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: all 0.2s;
}
.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.cheat-link {
    margin-left: auto;
    padding: 8px 12px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 1.1rem;
    opacity: 0.4;
    transition: opacity 0.2s;
    align-self: center;
}
.cheat-link:hover { opacity: 1; color: var(--accent); }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* Search bar */
.search-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 20px;
}
.search-bar input {
    flex: 1;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}
.search-bar input:focus { border-color: var(--accent); }
.search-bar input::placeholder { color: var(--text-muted); }
.search-bar select {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 12px;
    border-radius: var(--radius);
    font-size: 0.9rem;
    cursor: pointer;
    min-width: 140px;
}
.search-bar select:focus { border-color: var(--accent); outline: none; }
.search-bar button {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 12px 24px;
    border-radius: var(--radius);
    font-size: 1rem;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.2s;
}
.search-bar button:hover { background: var(--accent-hover); }
.search-bar button:disabled { opacity: 0.6; cursor: not-allowed; }

/* Results */
.results { display: flex; flex-direction: column; gap: 12px; }
.result-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    transition: border-color 0.2s;
}
.result-card:hover { border-color: var(--accent); }
.result-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}
.result-title {
    font-weight: 600;
    color: var(--accent);
    font-size: 0.95rem;
}
.result-score {
    font-size: 0.8rem;
    color: var(--text-muted);
    background: var(--bg-tertiary);
    padding: 2px 8px;
    border-radius: 12px;
}
.result-source {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 8px;
}
.result-content {
    font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
    font-size: 0.85rem;
    background: var(--code-bg);
    padding: 12px;
    border-radius: 4px;
    overflow-x: auto;
    white-space: pre-wrap;
    max-height: 300px;
    overflow-y: auto;
    color: var(--text-secondary);
    line-height: 1.5;
}
.ref-link { color: var(--accent); text-decoration: none; border-bottom: 1px dashed var(--text-muted); transition: color 0.15s, border-color 0.15s; }
.ref-link:hover { color: var(--accent-hover); border-bottom-color: var(--accent-hover); }
.result-meta {
    display: flex;
    gap: 8px;
    margin-top: 8px;
    flex-wrap: wrap;
}
.meta-tag {
    font-size: 0.75rem;
    background: var(--bg-tertiary);
    color: var(--text-muted);
    padding: 2px 8px;
    border-radius: 4px;
}

/* Chat */
.chat-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 220px);
    min-height: 400px;
}
.chat-messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 12px 0;
}
.chat-msg {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: var(--radius);
    line-height: 1.5;
}
.chat-msg.user {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 2px;
}
.chat-msg.assistant {
    align-self: flex-start;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-bottom-left-radius: 2px;
}
.chat-msg.assistant pre {
    background: var(--code-bg);
    padding: 8px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.85rem;
}
.chat-msg .msg-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 6px;
}
.chat-msg.user .msg-meta { color: rgba(255,255,255,0.7); }
.chat-input {
    display: flex;
    gap: 8px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}
.chat-input textarea {
    flex: 1;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    padding: 12px;
    border-radius: var(--radius);
    font-size: 0.95rem;
    resize: none;
    outline: none;
    min-height: 50px;
    max-height: 120px;
    font-family: inherit;
}
.chat-input textarea:focus { border-color: var(--accent); }
.chat-input button {
    background: var(--accent);
    color: #fff;
    border: none;
    padding: 12px 20px;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.95rem;
    align-self: flex-end;
}
.chat-input button:hover { background: var(--accent-hover); }
.chat-feedback {
    display: flex;
    gap: 4px;
    margin-top: 4px;
}
.chat-feedback button {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1rem;
    opacity: 0.5;
    padding: 2px 4px;
}
.chat-feedback button:hover { opacity: 1; }

/* Compare */
.compare-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 16px;
}
.compare-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}
.compare-card h3 {
    font-size: 0.95rem;
    color: var(--accent);
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.compare-card .latency {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: normal;
}
.compare-card .answer {
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-wrap;
}
.compare-card .answer pre {
    background: var(--code-bg);
    padding: 8px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 8px 0;
    font-size: 0.85rem;
}
.compare-card.error { border-color: var(--error); }
.compare-card.loading { opacity: 0.6; }
.compare-failed {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.8rem;
    padding: 8px;
    grid-column: 1 / -1;
}

/* Dashboard */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 16px;
}
.dash-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
}
.dash-card h3 {
    font-size: 0.9rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}
.dash-item {
    display: flex;
    justify-content: space-between;
    padding: 6px 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
}
.dash-item:last-child { border-bottom: none; }
.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 6px;
}
.status-dot.connected { background: var(--success); }
.status-dot.disconnected { background: var(--error); }
.status-dot.unconfigured { background: var(--text-muted); }

/* Loading */
.spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
    color: var(--text-muted);
    font-style: italic;
    padding: 20px;
    text-align: center;
}

/* Status bar */
.status-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    padding: 6px 24px;
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* Ref-link modal */
.ref-modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.7); z-index: 1000;
    display: flex; align-items: center; justify-content: center;
    backdrop-filter: blur(2px);
}
.ref-modal-content {
    background: var(--bg-primary); border: 1px solid var(--border);
    border-radius: var(--radius); width: 90%; max-width: 800px; max-height: 80vh;
    overflow-y: auto; box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}
.ref-modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 16px 20px; border-bottom: 1px solid var(--border);
    position: sticky; top: 0; background: var(--bg-primary); z-index: 1;
}
.ref-modal-header span { font-weight: 600; color: var(--accent); font-size: 1.1rem; }
.ref-modal-close {
    background: none; border: none; color: var(--text-muted); font-size: 1.5rem;
    cursor: pointer; padding: 0 4px; line-height: 1;
}
.ref-modal-close:hover { color: var(--error); }
#refModalBody { padding: 16px 20px; }
#refModalBody .result-card { margin-bottom: 12px; }

/* Responsive */
@media (max-width: 768px) {
    .header { flex-direction: column; gap: 8px; }
    .container { padding: 12px; }
    .compare-grid { grid-template-columns: 1fr; }
    .chat-msg { max-width: 95%; }
    .ref-modal-content { width: 95%; max-height: 90vh; }
}
