/* ============================================================
   Knowledgebase AI - Styles
   Mimics QTI design language: DM Sans + JetBrains Mono,
   gradient blue header, card-based layout with soft shadows,
   purple "deep mode" accent.
   ============================================================ */

:root {
    /* Brand */
    --qti-blue:        #0a3d6b;
    --qti-blue-light:  #1565a0;
    --qti-blue-soft:   #e8f0fe;

    /* Neutrals */
    --bg:           #f7f8fa;
    --card:         #ffffff;
    --text:         #1a1a2e;
    --text-dim:     #64748b;
    --text-light:   #94a3b8;
    --border:       #e2e8f0;
    --border-light: #f1f5f9;

    /* Accents */
    --green:        #059669;
    --amber:        #d97706;
    --red:          #dc2626;
    --red-soft:     #fef2f2;
    --purple:       #7c3aed;
    --purple-light: #8b5cf6;
    --purple-soft:  #f3f0ff;

    --shadow:    0 1px 3px rgba(0,0,0,0.06), 0 4px 12px rgba(0,0,0,0.04);
    --shadow-lg: 0 4px 20px rgba(0,0,0,0.08), 0 8px 32px rgba(0,0,0,0.06);
    --radius:    12px;
}

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

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

/* ============================================================
   Header
   ============================================================ */
.site-header {
    background: linear-gradient(135deg, var(--qti-blue) 0%, #0d4f87 100%);
    padding: 14px 24px;
    display: flex;
    align-items: center;
    gap: 14px;
    box-shadow: 0 2px 12px rgba(10,61,107,0.3);
    position: sticky;
    top: 0;
    z-index: 100;
}
.site-header .logo {
    width: 36px; height: 36px;
    background: white;
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; color: var(--qti-blue); font-size: 12px;
    letter-spacing: 0.5px;
}
.site-header h1 { color: white; font-size: 17px; font-weight: 600; }
.site-header .version-badge {
    color: rgba(255,255,255,0.7);
    background: rgba(255,255,255,0.12);
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    font-weight: 500;
    padding: 2px 7px;
    border-radius: 5px;
    letter-spacing: 0.3px;
}
.site-header .nav { display: flex; gap: 4px; margin-left: 20px; }
.site-header .nav a {
    color: rgba(255,255,255,0.85);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    padding: 6px 12px;
    border-radius: 6px;
    transition: background 0.15s;
}
.site-header .nav a:hover { background: rgba(255,255,255,0.12); color: white; }
.site-header .subtitle {
    color: rgba(255,255,255,0.5);
    font-size: 12px;
    margin-left: auto;
    font-family: 'JetBrains Mono', monospace;
}

main { flex: 1; max-width: 880px; width: 100%; margin: 0 auto; padding: 28px 24px 60px; }

/* ============================================================
   Search box + suggestions
   ============================================================ */
.search-container { position: relative; margin-bottom: 24px; }
.search-box {
    width: 100%;
    padding: 16px 56px 16px 20px;
    font-family: 'DM Sans', sans-serif;
    font-size: 15px;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    background: var(--card);
    color: var(--text);
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    box-shadow: var(--shadow);
}
.search-box:focus {
    border-color: var(--qti-blue-light);
    box-shadow: var(--shadow-lg), 0 0 0 3px rgba(21,101,160,0.1);
}
.search-box::placeholder { color: var(--text-light); }
.search-btn {
    position: absolute;
    right: 8px; top: 50%;
    transform: translateY(-50%);
    width: 38px; height: 38px;
    border: none;
    background: var(--qti-blue);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.15s;
}
.search-btn:hover { background: var(--qti-blue-light); }
.search-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.search-btn svg { width: 16px; height: 16px; }

.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 28px; align-items: center; }
.suggestions-label {
    font-size: 12px;
    color: var(--text-light);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    margin-right: 4px;
}
.suggestion {
    padding: 7px 14px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 13px;
    color: var(--text-dim);
    cursor: pointer;
    transition: all 0.15s;
    font-family: inherit;
}
.suggestion:hover {
    border-color: var(--qti-blue-light);
    color: var(--qti-blue);
    background: var(--qti-blue-soft);
}

/* ============================================================
   Answer container + cards
   ============================================================ */
.answer-container { display: none; }
.answer-container.active { display: block; }

.answer-card {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    border: 1px solid var(--border-light);
}
.answer-header {
    padding: 12px 20px;
    background: linear-gradient(135deg, var(--qti-blue), var(--qti-blue-light));
    color: white;
    font-size: 11px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
    letter-spacing: 0.8px;
    text-transform: uppercase;
}
.answer-header.deep {
    background: linear-gradient(135deg, var(--purple), var(--purple-light));
}

.answer-body {
    padding: 24px 28px;
    font-size: 15px;
    line-height: 1.8;
    color: #2d3748;
}
.answer-body h1, .answer-body h2, .answer-body h3, .answer-body h4 {
    color: var(--qti-blue); line-height: 1.3;
}
.answer-body h1 { font-size: 20px; font-weight: 700; margin: 24px 0 12px; }
.answer-body h2 {
    font-size: 17px; font-weight: 700; margin: 22px 0 10px;
    padding-bottom: 6px; border-bottom: 2px solid var(--qti-blue-soft);
}
.answer-body h3 { font-size: 15px; font-weight: 700; margin: 18px 0 8px; color: #1e3a5f; }
.answer-body h1:first-child, .answer-body h2:first-child, .answer-body h3:first-child { margin-top: 0; }
.answer-body p { margin: 0 0 14px 0; }
.answer-body p:last-child { margin-bottom: 0; }
.answer-body strong, .answer-body b { font-weight: 600; color: var(--text); }
.answer-body ul { margin: 10px 0 14px 24px; padding: 0; list-style: disc; }
.answer-body ol { margin: 10px 0 14px 24px; padding: 0; list-style: decimal; }
.answer-body li { margin-bottom: 6px; padding-left: 4px; line-height: 1.7; }
.answer-body ul ul, .answer-body ol ul { list-style: circle; margin: 6px 0 6px 20px; }
.answer-body ol ol, .answer-body ul ol { margin: 6px 0 6px 20px; }
.answer-body code {
    font-family: 'JetBrains Mono', monospace;
    background: #f0f4f8;
    padding: 2px 7px;
    border-radius: 4px;
    font-size: 13px;
    color: #c7254e;
    border: 1px solid #e8ecf1;
}
.answer-body pre {
    background: #1e293b;
    color: #e2e8f0;
    padding: 16px 20px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 12px 0;
}
.answer-body pre code { background: none; border: none; padding: 0; color: inherit; }
.answer-body blockquote {
    border-left: 3px solid var(--qti-blue-light);
    padding: 10px 16px;
    margin: 12px 0;
    background: var(--qti-blue-soft);
    border-radius: 0 8px 8px 0;
}
.answer-body hr { border: none; height: 1px; background: var(--border); margin: 18px 0; }
.answer-body a { color: var(--qti-blue-light); text-decoration: underline; }
.answer-body table {
    border-collapse: collapse;
    margin: 12px 0;
    width: 100%;
    font-size: 14px;
}
.answer-body th, .answer-body td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.answer-body th { background: var(--qti-blue-soft); font-weight: 600; color: var(--qti-blue); }

.answer-footer {
    padding: 10px 20px;
    border-top: 1px solid var(--border-light);
    font-size: 12px;
    color: var(--text-light);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-family: 'JetBrains Mono', monospace;
}
.answer-footer .timer { display: flex; align-items: center; gap: 4px; }
.answer-footer .timer svg { width: 12px; height: 12px; opacity: 0.5; }

/* Think-deeper row (also hosts feedback thumbs) */
.think-deeper-wrap {
    display: none;
    padding: 14px 20px;
    border-top: 1px solid var(--border-light);
    background: linear-gradient(to right, var(--purple-soft), transparent);
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}
.think-deeper-wrap.active { display: flex; }
.think-deeper-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 18px;
    background: var(--purple);
    color: white;
    border: none;
    border-radius: 8px;
    font-family: 'DM Sans', sans-serif;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.think-deeper-btn:hover { background: #6d28d9; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(124,58,237,0.3); }
.think-deeper-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; box-shadow: none; }
.think-deeper-btn svg { width: 16px; height: 16px; }
.think-deeper-hint { font-size: 12px; color: var(--text-light); flex: 1; }

/* Deep card uses a row similar to think-deeper but without the button */
.deep-footer-row {
    display: flex;
    justify-content: flex-end;
    padding: 10px 20px;
    border-top: 1px solid var(--border-light);
    background: linear-gradient(to right, transparent, var(--purple-soft));
}

/* Feedback thumbs */
.feedback {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    font-family: 'DM Sans', sans-serif;
}
.thumb {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 5px 10px;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    transition: all 0.15s;
}
.thumb:hover:not(:disabled) { border-color: var(--qti-blue-light); background: var(--qti-blue-soft); }
.thumb:disabled { opacity: 0.5; cursor: default; }
.thumb.active { background: var(--qti-blue-soft); border-color: var(--qti-blue-light); }
.feedback .thanks { font-size: 12px; color: var(--green); font-weight: 500; }

/* ============================================================
   Thinking spinner card
   ============================================================ */
.thinking-card {
    display: none;
    margin-top: 16px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    border: 2px solid var(--purple);
    overflow: hidden;
}
.thinking-card.active { display: block; }
.thinking-banner {
    padding: 12px 20px;
    background: linear-gradient(135deg, var(--purple), var(--purple-light));
    color: white;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.8px;
    text-transform: uppercase;
    display: flex;
    align-items: center;
    gap: 8px;
}
.thinking-body { padding: 28px; display: flex; align-items: center; gap: 20px; }
.thinking-spinner { width: 48px; height: 48px; position: relative; flex-shrink: 0; }
.thinking-spinner .ring {
    width: 48px; height: 48px;
    border: 3px solid var(--purple-soft);
    border-top-color: var(--purple);
    border-right-color: var(--purple);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}
.thinking-spinner .inner-ring {
    position: absolute; top: 10px; left: 10px;
    width: 28px; height: 28px;
    border: 2px solid var(--purple-soft);
    border-bottom-color: var(--purple-light);
    border-left-color: var(--purple-light);
    border-radius: 50%;
    animation: spin 0.7s linear infinite reverse;
}
.thinking-spinner .dot {
    position: absolute; top: 20px; left: 20px;
    width: 8px; height: 8px;
    background: var(--purple);
    border-radius: 50%;
    animation: pulse-dot 1s ease-in-out infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse-dot {
    0%, 100% { transform: scale(0.8); opacity: 0.5; }
    50%      { transform: scale(1.2); opacity: 1; }
}
.thinking-info { display: flex; flex-direction: column; gap: 4px; }
.thinking-info .title { font-size: 15px; font-weight: 600; color: var(--purple); }
.thinking-info .detail { font-size: 13px; color: var(--text-light); line-height: 1.4; }
.thinking-dots::after { content: ''; animation: dots 1.5s steps(4, end) infinite; }
@keyframes dots {
    0% { content: ''; } 25% { content: '.'; }
    50% { content: '..'; } 75% { content: '...'; }
}

/* ============================================================
   Deep card
   ============================================================ */
.deep-card {
    display: none;
    margin-top: 16px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    border: 1px solid var(--purple);
}
.deep-card.active { display: block; }

/* ============================================================
   Sources + related
   ============================================================ */
.sources-section, .related-section { margin-top: 20px; }
.related-section { display: none; }
.related-section.active { display: block; }

.sources-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 6px;
}
.sources-title svg { width: 14px; height: 14px; opacity: 0.5; }

.source-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 16px;
    margin-bottom: 6px;
    transition: all 0.15s;
}
.source-card:hover { border-color: var(--qti-blue-light); box-shadow: 0 2px 8px rgba(21,101,160,0.08); }

/* Clickable variant: whole card is an <a> */
a.source-card-link {
    display: block;
    text-decoration: none;
    color: inherit;
    cursor: pointer;
}
a.source-card-link:hover {
    border-color: var(--qti-blue-light);
    box-shadow: 0 2px 10px rgba(21,101,160,0.12);
    transform: translateY(-1px);
}
a.source-card-link:hover .source-title { text-decoration: underline; }
.source-title { font-weight: 600; font-size: 13.5px; color: var(--qti-blue); margin-bottom: 4px; }
.admin-badge {
    display: inline-block;
    margin-left: 8px;
    padding: 1px 7px;
    font-size: 10.5px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--purple);
    background: var(--purple-soft);
    border-radius: 4px;
    vertical-align: middle;
}
.source-snippet { font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; line-height: 1.5; }
.source-meta { display: flex; gap: 14px; font-size: 11.5px; color: var(--text-light); align-items: center; }
.relevance-bar {
    display: inline-block;
    width: 36px; height: 3px;
    background: #e2e8f0;
    border-radius: 2px;
    overflow: hidden;
    vertical-align: middle;
    margin-left: 3px;
}
.relevance-fill { height: 100%; border-radius: 2px; }
.rel-high { background: var(--green); }
.rel-mid  { background: var(--amber); }
.rel-low  { background: var(--text-light); }

/* ============================================================
   Loading + empty + error
   ============================================================ */
.loading { display: none; text-align: center; padding: 48px; }
.loading.active { display: block; }
.spinner {
    width: 28px; height: 28px;
    border: 3px solid var(--border);
    border-top-color: var(--qti-blue);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
    margin: 0 auto 12px;
}
.loading-text { font-size: 13px; color: var(--text-light); }

.empty-state { text-align: center; padding: 56px 24px; color: var(--text-light); }
.empty-state .icon { font-size: 40px; margin-bottom: 14px; opacity: 0.5; }
.empty-state p { font-size: 14px; max-width: 380px; margin: 0 auto; line-height: 1.6; }
.empty-state a { color: var(--qti-blue-light); }

.cursor {
    display: inline-block;
    width: 2px; height: 1em;
    background: var(--qti-blue);
    animation: blink 1s step-end infinite;
    vertical-align: text-bottom;
    margin-left: 2px;
}
.cursor.deep { background: var(--purple); }
@keyframes blink { 50% { opacity: 0; } }

.error-card {
    display: none;
    margin-top: 16px;
    padding: 14px 18px;
    background: var(--red-soft);
    border: 1px solid #fecaca;
    color: var(--red);
    border-radius: 10px;
    font-size: 14px;
}
.error-card.active { display: block; }

.admin-error {
    margin-top: 12px;
    padding: 12px 16px;
    background: var(--red-soft);
    border: 1px solid #fecaca;
    color: var(--red);
    border-radius: 8px;
    font-size: 13px;
}

.hidden { display: none !important; }

/* ============================================================
   Admin page
   ============================================================ */
.admin h1 { margin: 0 0 24px; font-size: 22px; color: var(--qti-blue); }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 24px;
    margin-bottom: 16px;
}
.card h2 {
    margin: 0 0 16px;
    font-size: 13px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.6px;
    font-weight: 600;
}
.muted { color: var(--text-dim); font-size: 14px; }
.kv { width: 100%; border-collapse: collapse; font-size: 14px; }
.kv th { text-align: left; padding: 8px 0; color: var(--text-dim); font-weight: 500; width: 200px; }
.kv td { padding: 8px 0; }
.kv code {
    font-family: 'JetBrains Mono', monospace;
    background: var(--border-light);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 12.5px;
}

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
    padding: 9px 16px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card);
    color: var(--text);
    font-family: 'DM Sans', sans-serif;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
}
.btn:hover { background: var(--border-light); border-color: var(--text-light); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--qti-blue); color: white; border-color: var(--qti-blue); }
.btn.primary:hover { background: var(--qti-blue-light); }
.btn.danger { color: var(--red); border-color: #fecaca; }
.btn.danger:hover { background: var(--red-soft); }

.job-meta { display: flex; gap: 24px; margin-bottom: 12px; font-size: 13px; color: var(--text-dim); }
.job-meta strong { color: var(--text); }

.progress-wrap { display: flex; align-items: center; gap: 12px; margin: 12px 0; }
.progress-bar {
    flex: 1; height: 12px;
    background: var(--border-light);
    border-radius: 999px;
    overflow: hidden;
}
.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--qti-blue), var(--qti-blue-light));
    transition: width 0.3s ease;
}
.progress-text {
    font-size: 12px;
    color: var(--text-dim);
    min-width: 80px;
    text-align: right;
    font-family: 'JetBrains Mono', monospace;
}

.job-log { margin-top: 16px; }
.job-log summary { cursor: pointer; color: var(--text-dim); font-size: 13px; }
.job-log pre {
    background: var(--border-light);
    padding: 12px;
    border-radius: 8px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11.5px;
    max-height: 240px;
    overflow: auto;
    margin: 8px 0 0;
}

.inline-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.inline-form input[type=number] {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 120px;
    font-family: 'DM Sans', sans-serif;
    font-size: 14px;
}

/* API key rows */
.apikey-row {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 10px;
}
.apikey-row:last-child { margin-bottom: 0; }
.apikey-row label {
    width: 90px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-dim);
}
.apikey-input {
    flex: 1;
    min-width: 220px;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 13px;
    background: var(--border-light);
    color: var(--text);
}
.apikey-input:not([readonly]) {
    background: var(--card);
    border-color: var(--qti-blue-light);
}
.apikey-status { font-size: 12px; }
.apikey-status.ok { color: var(--green); }
.apikey-status.err { color: var(--red); }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 640px) {
    main { padding: 16px 14px 40px; }
    .search-box { font-size: 14px; padding: 14px 50px 14px 14px; }
    .site-header h1 { font-size: 15px; }
    .site-header .subtitle { display: none; }
    .answer-body { padding: 18px 16px; }
    .kv th { width: 140px; }
    .actions { flex-direction: column; align-items: stretch; }
    .think-deeper-wrap { flex-direction: column; align-items: stretch; }
    .feedback { margin-left: 0; }
}
