/* ============================================================
   RAG UI — refreshed stylesheet
   Drop-in replacement for static/css/main.css
   Keeps all existing class names from chat.html / search.html /
   login.html / chat.js / search.js — no markup changes required.
   ============================================================ */

:root {
  --bg:            #F6F4EF;
  --surface:       #FFFFFF;
  --surface-alt:   #FBFAF6;
  --ink:           #16181C;
  --ink-2:         #3A3E46;
  --muted:         #6B7280;
  --muted-2:       #9AA0AA;
  --line:          #E7E4DC;
  --line-strong:   #D6D2C8;
  --accent:        oklch(0.62 0.14 45);      /* warm terracotta */
  --accent-ink:    oklch(0.38 0.09 45);
  --accent-soft:   oklch(0.96 0.025 55);
  --danger:        oklch(0.55 0.18 25);
  --danger-soft:   oklch(0.96 0.03 25);
  --info:          oklch(0.55 0.11 245);
  --info-soft:     oklch(0.97 0.02 245);
  --radius:        10px;
  --radius-sm:     7px;
  --radius-lg:     14px;
  --shadow-sm:     0 1px 0 rgba(20,20,30,0.04);
  --shadow-md:     0 1px 2px rgba(20,20,30,0.05), 0 8px 24px -16px rgba(20,20,30,0.18);
  --mono:          "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans:          "Inter Tight", "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

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

html, body { height: 100%; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------- Header ---------- */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  padding: 0 28px;
  display: flex;
  align-items: center;
  gap: 28px;
  height: 56px;
  flex-shrink: 0;
}
header .title {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
  padding: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
header .title::before {
  content: "";
  width: 18px; height: 18px;
  border-radius: 5px;
  background:
    linear-gradient(135deg, var(--accent), oklch(0.72 0.12 65));
  display: inline-block;
}
nav { display: flex; gap: 2px; height: 100%; }
nav a {
  color: var(--muted);
  padding: 0 14px;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  display: flex;
  align-items: center;
  position: relative;
  transition: color .15s ease;
}
nav a::after {
  content: "";
  position: absolute;
  left: 12px; right: 12px; bottom: -1px;
  height: 2px;
  background: transparent;
  border-radius: 2px;
}
nav a.active { color: var(--ink); }
nav a.active::after { background: var(--ink); }
nav a:hover { color: var(--ink); }

header > div[style*="margin-left:auto"],
header .user-chip {
  margin-left: auto !important;
  font-size: 13px !important;
  opacity: 1 !important;
  color: var(--muted) !important;
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
  flex-shrink: 0;
}
header a[href*="logout"],
header a[style*="color:white"],
header a[style*="color: white"] {
  color: var(--ink) !important;
  text-decoration: none;
  font-weight: 500;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  transition: border-color .15s ease, background .15s ease;
  white-space: nowrap;
}
header a[href*="logout"]:hover,
header a[style*="color:white"]:hover,
header a[style*="color: white"]:hover {
  border-color: var(--line-strong);
  background: var(--surface-alt);
}

/* ---------- Layout ---------- */
.container {
  display: flex;
  gap: 16px;
  width: 100%;
  padding: 16px 20px 20px;
  flex: 1;
  overflow: hidden;
}
.main-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: hidden;
  min-width: 0;
}

/* ---------- Thread sidebar (left) ---------- */
.thread-sidebar {
  width: 248px;
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 4px 0 4px 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  flex-shrink: 0;
}
.new-thread-btn {
  padding: 9px 12px;
  background: var(--ink);
  color: #fff;
  border: 1px solid var(--ink);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background .15s ease, transform .05s ease;
}
.new-thread-btn:hover { background: #000; }
.new-thread-btn:active { transform: translateY(1px); }

.thread-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  overflow-y: auto;
  padding-top: 4px;
}
.thread-list::before {
  content: "Conversations";
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted-2);
  padding: 8px 10px 6px;
  font-weight: 600;
}
.thread-item {
  position: relative;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  color: var(--ink-2);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  transition: background .12s ease, color .12s ease;
}
.thread-item:hover { background: rgba(22,24,28,0.05); }
.thread-item.active {
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
  box-shadow: var(--shadow-sm), inset 0 0 0 1px var(--line);
}
.thread-item .thread-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thread-item .thread-actions { display: none; gap: 4px; }
.thread-item:hover .thread-actions { display: flex; }
.thread-item .thread-actions button {
  padding: 2px 7px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
}
.thread-item .thread-actions button:hover {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

/* ---------- Chat card ---------- */
.chat-card {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.conversation {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 28px 32px;
  scroll-behavior: smooth;
}
.chat-form {
  padding: 14px 16px;
  border-top: 1px solid var(--line);
  background: var(--surface-alt);
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ---------- Messages ---------- */
.msg {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: 14px;
  line-height: 1.6;
  font-size: 14.5px;
  white-space: pre-wrap;
}
.msg.user {
  align-self: flex-end;
  background: var(--ink);
  color: #fff;
  border-bottom-right-radius: 5px;
  font-weight: 450;
}
.msg.assistant {
  align-self: flex-start;
  background: transparent;
  border: none;
  border-bottom-left-radius: 5px;
  white-space: normal;
  color: var(--ink);
  padding: 0 2px;
  max-width: 100%;
  position: relative;
}
.msg.assistant + .msg.assistant { margin-top: -4px; }

.msg.assistant h1,
.msg.assistant h2,
.msg.assistant h3 {
  margin: 18px 0 8px;
  letter-spacing: -0.015em;
  line-height: 1.3;
}
.msg.assistant h1 { font-size: 20px; font-weight: 600; }
.msg.assistant h2 { font-size: 17px; font-weight: 600; }
.msg.assistant h3 { font-size: 15px; font-weight: 600; color: var(--ink); }
.msg.assistant h3:first-child { margin-top: 4px; }

.msg.assistant p { margin: 8px 0; }
.msg.assistant ul,
.msg.assistant ol { margin: 8px 0; padding-left: 22px; }
.msg.assistant li { margin: 4px 0; }
.msg.assistant li::marker { color: var(--muted); }

.msg.assistant strong { color: var(--ink); font-weight: 600; }
.msg.assistant em { color: var(--ink-2); }

.msg.assistant a {
  color: var(--accent-ink);
  text-decoration: none;
  border-bottom: 1px solid oklch(0.62 0.14 45 / 0.3);
  transition: border-color .15s;
}
.msg.assistant a:hover { border-bottom-color: var(--accent); }

.msg.assistant table {
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
  width: 100%;
}
.msg.assistant th,
.msg.assistant td {
  border: 1px solid var(--line);
  padding: 6px 10px;
  text-align: left;
}
.msg.assistant th { background: var(--surface-alt); font-weight: 600; }

.msg.assistant code {
  background: var(--surface-alt);
  border: 1px solid var(--line);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 12.5px;
  font-family: var(--mono);
  color: var(--accent-ink);
}
.msg.assistant hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 14px 0;
}

/* status messages */
.msg.searching {
  align-self: flex-start;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 2px;
}
.msg.searching::before {
  content: "";
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(0.9); }
  50%      { opacity: 1;   transform: scale(1.1); }
}

.msg.error {
  align-self: stretch;
  max-width: 100%;
  background: var(--danger-soft);
  border: 1px solid oklch(0.55 0.18 25 / 0.3);
  color: var(--danger);
  font-size: 13px;
  border-radius: var(--radius);
}
.msg.info {
  align-self: stretch;
  max-width: 100%;
  background: var(--info-soft);
  border: 1px solid oklch(0.55 0.11 245 / 0.25);
  color: var(--info);
  font-size: 13px;
  border-radius: var(--radius);
}

/* HITL */
.msg.hitl-request {
  align-self: stretch;
  max-width: 100%;
  background: var(--surface-alt);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.hitl-intro { margin: 0; font-weight: 600; font-size: 14px; color: var(--ink); }
.hitl-question { display: flex; flex-direction: column; gap: 8px; }
.hitl-question-text { margin: 0; font-weight: 500; color: var(--ink-2); font-size: 13.5px; }
.hitl-choice {
  align-self: flex-start;
  padding: 7px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  color: var(--ink);
  transition: all .15s ease;
}
.hitl-choice:hover {
  border-color: var(--ink);
  background: var(--surface);
}
.hitl-choice.selected {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.hitl-text {
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-family: inherit;
  outline: none;
  background: var(--surface);
  transition: border-color .15s;
}
.hitl-text:focus { border-color: var(--ink); }
.hitl-submit {
  align-self: flex-end;
  padding: 8px 18px;
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s;
}
.hitl-submit:hover { background: #000; }
.hitl-submit:disabled { background: var(--muted-2); cursor: not-allowed; }

/* ---------- Sources sidebar (right) ---------- */
.sidebar {
  width: 380px;
  background: transparent;
  border: none;
  border-radius: 0;
  padding: 4px 4px 4px 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  flex-shrink: 0;
}
.sidebar h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted-2);
  padding: 4px 4px 2px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.sidebar h2::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}
.sidebar .source-card { background: var(--surface); }

/* ---------- Source cards (shared) ---------- */
.source-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 15px;
  transition: border-color .15s ease, box-shadow .15s ease, transform .05s;
}
.source-card:hover {
  border-color: var(--line-strong);
  box-shadow: var(--shadow-sm);
}
.source-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 8px;
}
.source-card .title {
  font-weight: 600;
  font-size: 13.5px;
  letter-spacing: -0.005em;
  color: var(--ink);
  line-height: 1.35;
}
.source-card .score {
  font-size: 11px;
  font-family: var(--mono);
  font-weight: 500;
  color: var(--ink);
  background: var(--accent-soft);
  padding: 2px 7px;
  border-radius: 4px;
  white-space: nowrap;
  flex-shrink: 0;
}
.source-meta {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.source-meta span {
  font-size: 11px;
  font-family: var(--mono);
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--surface-alt);
  border: 1px solid var(--line);
  color: var(--muted);
}
.chunk-content {
  font-size: 12.5px;
  color: var(--ink-2);
  line-height: 1.55;
  margin-bottom: 10px;
  border-left: 2px solid var(--line-strong);
  padding: 2px 0 2px 12px;
}
.doc-link {
  font-size: 12px;
  color: var(--accent-ink);
  text-decoration: none;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.doc-link::after { content: "→"; transition: transform .15s ease; }
.doc-link:hover::after { transform: translateX(2px); }

/* ---------- Search ---------- */
.search-sidebar {
  width: 268px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 4px 4px 4px 0;
  flex-shrink: 0;
}
.search-sidebar h2 {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted-2);
  padding: 4px 4px 2px;
}
.search-results {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.search-results .empty {
  color: var(--muted-2);
  text-align: center;
  margin-top: 80px;
  font-size: 13px;
}

.filter-group { display: flex; flex-direction: column; gap: 6px; }
.filter-group label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 2px;
}
.filter-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.filter-tag {
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  color: var(--ink-2);
  font-family: inherit;
  transition: all .12s ease;
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.filter-tag.active {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.filter-tag:hover { border-color: var(--ink); }
.filter-tag .count {
  opacity: 0.55;
  font-size: 11px;
  font-family: var(--mono);
}
.filter-input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 13px;
  outline: none;
  background: var(--surface);
  font-family: inherit;
  color: var(--ink);
  transition: border-color .15s, box-shadow .15s;
}
.filter-input:focus {
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(22,24,28,0.06);
}
.filter-input::placeholder { color: var(--muted-2); }

/* ---------- Shared form controls ---------- */
form input[type="text"] {
  flex: 1;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
  transition: border-color .15s, box-shadow .15s;
}
form input[type="text"]:focus {
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(22,24,28,0.06);
}
form input[type="text"]::placeholder { color: var(--muted-2); }

form button {
  padding: 11px 22px;
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 13.5px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s, transform .05s;
  letter-spacing: 0.01em;
}
form button:hover { background: #000; }
form button:active { transform: translateY(1px); }
form button:disabled { background: var(--muted-2); cursor: not-allowed; }

/* scrollbars */
.conversation::-webkit-scrollbar,
.sidebar::-webkit-scrollbar,
.thread-sidebar::-webkit-scrollbar,
.thread-list::-webkit-scrollbar,
.search-sidebar::-webkit-scrollbar,
.search-results::-webkit-scrollbar { width: 8px; }
.conversation::-webkit-scrollbar-track,
.sidebar::-webkit-scrollbar-track,
.thread-sidebar::-webkit-scrollbar-track,
.thread-list::-webkit-scrollbar-track,
.search-sidebar::-webkit-scrollbar-track,
.search-results::-webkit-scrollbar-track { background: transparent; }
.conversation::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb,
.thread-sidebar::-webkit-scrollbar-thumb,
.thread-list::-webkit-scrollbar-thumb,
.search-sidebar::-webkit-scrollbar-thumb,
.search-results::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border-radius: 8px;
}
.conversation::-webkit-scrollbar-thumb:hover,
.sidebar::-webkit-scrollbar-thumb:hover { background: var(--muted-2); }

/* ---------- Login ---------- */
.login-container {
  max-width: 380px;
  margin: 80px auto;
  padding: 36px 32px 32px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-md);
}
.login-container h1 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.015em;
  margin-bottom: 6px;
  text-align: left;
}
.login-container h1 + *,
.login-container form { margin-top: 20px; }
.login-container .form-group { margin-bottom: 14px; }
.login-container .form-group label {
  display: block;
  margin-bottom: 5px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0.01em;
}
.login-container .form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--ink);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.login-container .form-group input:focus {
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(22,24,28,0.06);
}
.login-container button {
  width: 100%;
  padding: 11px 16px;
  margin-top: 4px;
}
.login-container .error {
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid oklch(0.55 0.18 25 / 0.25);
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 14px;
}
