/* ── Reset & Variables ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0f1117;
  --surface:     #1c1e26;
  --surface2:    #252833;
  --border:      #2e3145;
  --accent:      #6c63ff;
  --accent-dim:  #3f3a7a;
  --text:        #e2e4f0;
  --text-muted:  #8b8fa8;
  --user-bg:     #1a1f3a;
  --bot-bg:      #1c2030;
  --danger:      #e05260;
  --success:     #4caf82;
  --font:        'Inter', system-ui, -apple-system, sans-serif;
  --mono:        'JetBrains Mono', 'Fira Code', monospace;
  --radius:      10px;
  --shadow:      0 4px 24px rgba(0,0,0,.45);
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
}

/* ── Layout ────────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 860px;
  margin: 0 auto;
  padding: 0 16px;
}

/* ── Header ────────────────────────────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 0 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.persona-card {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
  flex: 1;
}

.persona-card-avatar {
  width: 52px;
  height: 52px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  flex-shrink: 0;
  background: #1e3a30;
  color: #6dcc9d;
  letter-spacing: .03em;
}

.persona-card-avatar-img {
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface2);
}

.persona-card-avatar-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.persona-card-body {
  min-width: 0;
}

.persona-card-name {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -.02em;
  color: var(--text);
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.persona-card-title {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 500;
  line-height: 1.35;
  margin-bottom: 2px;
}

.persona-card-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.persona-card-headline {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.app-header h1 {
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -.02em;
}

.app-header h1 span { color: var(--accent); }

.header-actions { display: flex; gap: 8px; }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.82rem;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--text); }

/* ── Messages ──────────────────────────────────────────────────── */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
  display: flex;
  flex-direction: column;
}

.msg {
  display: flex;
  gap: 10px;
  margin-bottom: 22px;
  animation: fadeIn .25s ease;
  max-width: min(520px, 88%);
  width: 100%;
}

.msg.user {
  align-self: flex-start;
}

.msg.bot {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg.user .msg-role { text-align: left; }
.msg.bot  .msg-role { text-align: right; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  flex-shrink: 0;
  letter-spacing: .03em;
}

.msg.user .msg-avatar  { background: var(--accent-dim); color: #c9c5ff; }
.msg.bot  .msg-avatar  { background: #1e3a30; color: #6dcc9d; }

.msg-avatar-img {
  padding: 0;
  overflow: hidden;
  background: var(--surface2);
}

.msg-avatar-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.msg-body { flex: 1; min-width: 0; }

.msg-role {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-muted);
  margin-bottom: 5px;
}

.msg-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  line-height: 1.7;
}

.msg.user .msg-content { background: var(--user-bg); }
.msg.bot  .msg-content { background: var(--bot-bg); }

/* markdown inside bot replies */
.msg-content p   { margin-bottom: .6em; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content ul, .msg-content ol { padding-left: 1.4em; margin-bottom: .6em; }
.msg-content li  { margin-bottom: .2em; }
.msg-content strong { color: #fff; }
.msg-content code {
  font-family: var(--mono);
  font-size: .85em;
  background: #0d0f18;
  border: 1px solid var(--border);
  padding: 1px 5px;
  border-radius: 4px;
}
.msg-content pre {
  background: #0d0f18;
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 12px 14px;
  overflow-x: auto;
  margin-bottom: .6em;
}
.msg-content pre code { background: none; border: none; padding: 0; }
.msg-content h1,.msg-content h2,.msg-content h3 {
  margin-bottom: .4em; margin-top: .8em; color: #fff;
}
.msg-content a { color: var(--accent); text-decoration: none; }
.msg-content a:hover { text-decoration: underline; }

/* ── Sources expander ──────────────────────────────────────────── */
.sources-block {
  margin-top: 10px;
}

.msg.bot .sources-block {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.sources-toggle {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.78rem;
  cursor: pointer;
  padding: 5px 12px;
  border-radius: 6px;
  transition: border-color .15s, color .15s;
  display: flex;
  align-items: center;
  gap: 6px;
}
.sources-toggle:hover { border-color: var(--accent); color: var(--text); }
.sources-toggle .chevron { transition: transform .2s; }
.sources-toggle.open .chevron { transform: rotate(180deg); }

.sources-list {
  display: none;
  margin-top: 8px;
  display: none;
  flex-direction: column;
  gap: 8px;
}
.sources-list.visible { display: flex; }

.source-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.82rem;
}
.source-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.source-index {
  background: var(--accent-dim);
  color: #c9c5ff;
  border-radius: 4px;
  padding: 1px 7px;
  font-size: 0.72rem;
  font-weight: 700;
}
.source-file { font-weight: 600; color: var(--text); }
.source-score { margin-left: auto; color: var(--text-muted); font-family: var(--mono); font-size: 0.75rem; }
.source-text { color: var(--text-muted); line-height: 1.55; }

/* ── Thinking indicator ────────────────────────────────────────── */
#thinking {
  display: none;
  align-items: center;
  gap: 10px;
  margin-bottom: 22px;
  margin-left: auto;
  color: var(--text-muted);
  font-size: 0.88rem;
}
#thinking.visible { display: flex; }

.dots { display: flex; gap: 4px; }
.dot {
  width: 6px; height: 6px;
  background: var(--accent);
  border-radius: 50%;
  animation: bounce .9s infinite;
}
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
  0%,60%,100% { transform: translateY(0); }
  30%          { transform: translateY(-6px); }
}

/* ── Input area ────────────────────────────────────────────────── */
.input-area {
  padding: 16px 0 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.input-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

#user-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.95rem;
  resize: none;
  min-height: 48px;
  max-height: 180px;
  transition: border-color .15s;
  outline: none;
}
#user-input:focus { border-color: var(--accent); }
#user-input::placeholder { color: var(--text-muted); }

#send-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 12px 22px;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
  white-space: nowrap;
}
#send-btn:hover   { opacity: .9; }
#send-btn:active  { transform: scale(.97); }
#send-btn:disabled { opacity: .4; cursor: not-allowed; }

.input-hint {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 7px;
  text-align: right;
}

/* ── Alerts ────────────────────────────────────────────────────── */
.alert {
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.85rem;
  margin-bottom: 12px;
  align-self: stretch;
}
.alert-error { background: #2a1419; border: 1px solid #6b2231; color: #f28b9a; }
.alert-success { background: #14261c; border: 1px solid #2d5c42; color: #8fd4a8; margin-bottom: 16px; }

/* ── Empty state ───────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
  align-self: center;
  width: 100%;
  max-width: none;
}
.empty-state-profile {
  width: 96px;
  height: 96px;
  margin: 0 auto 18px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--border);
  background: var(--surface2);
  box-shadow: var(--shadow);
}
.empty-state-profile img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.empty-state h2 { font-size: 1.4rem; color: var(--text); margin-bottom: 10px; }
.empty-state p  { max-width: 400px; margin: 0 auto 20px; line-height: 1.6; }

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}
.chip {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 7px 16px;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--text-muted);
  transition: border-color .15s, color .15s;
}
.chip:hover { border-color: var(--accent); color: var(--text); }

/* ── Admin styles ──────────────────────────────────────────────── */
.admin-shell {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 16px;
}
.admin-shell h1 { font-size: 1.4rem; margin-bottom: 4px; }
.admin-shell .subtitle { color: var(--text-muted); font-size: 0.88rem; margin-bottom: 28px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-bottom: 20px;
}
.card h2 { font-size: 1rem; margin-bottom: 16px; color: var(--text); }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.82rem; color: var(--text-muted); margin-bottom: 6px; }
.form-group input[type="password"],
.form-group input[type="text"] {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 0.9rem;
  outline: none;
}
.form-group input:focus { border-color: var(--accent); }

.btn-primary {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 11px 24px;
  border-radius: 8px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}
.btn-primary:hover { opacity: .88; }

.btn-danger {
  background: #b3261e;
  border: none;
  color: #fff;
  padding: 11px 24px;
  border-radius: 8px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
}
.btn-danger:hover { opacity: .88; }
.btn-danger:disabled,
.btn-primary:disabled,
.btn-ghost:disabled { opacity: .5; cursor: not-allowed; }

.status-log {
  background: #090b12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.7;
  max-height: 320px;
  overflow-y: auto;
  margin-top: 16px;
  white-space: pre-wrap;
}
.log-ok    { color: var(--success); }
.log-err   { color: var(--danger); }
.log-info  { color: var(--text-muted); }

.meta-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.meta-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); }
.meta-table td:first-child { color: var(--text-muted); width: 40%; }

.toggle-row {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  cursor: pointer;
}

.toggle-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
}

.toggle-hint {
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 999px;
  transition: background .2s, border-color .2s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform .2s, background .2s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
  background: #fff;
}

.toggle-switch input:focus-visible + .toggle-slider {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Responsive ────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .app-header { align-items: flex-start; }
  .persona-card-avatar { width: 44px; height: 44px; border-radius: 10px; }
  .persona-card-name { font-size: 0.95rem; }
  .persona-card-headline { -webkit-line-clamp: 3; }
  .header-actions .btn-ghost { padding: 5px 10px; font-size: 0.78rem; }
  #send-btn { padding: 12px 14px; }
}
