/* ─────────────────────────────────────────────────────────────────────────────
   ClinicalRAG — styles.css
   Sections:
     1. Variables & Reset
     2. App shell & Header
     3. Workspace & Query input
     4. Chips
     5. State A → B: Loading (dots + skeleton)
     6. State C: Answer + Source cards
     7. State D: Error card
     8. Shared utilities
     9. Animations
    10. Mobile
───────────────────────────────────────────────────────────────────────────── */

/* ── 1. Variables & Reset ────────────────────────────────────────────────── */
:root {
  --bg:            #0f0f14;
  --surface:       #141418;
  --surface-2:     #1a1a22;
  --input-bg:      #161622;
  --border:        #2a2a38;
  --border-hover:  #3d3d52;
  --text:          #e2e2f0;
  --text-muted:    #8888a8;
  --text-dim:      #4a4a68;
  --accent:        #818cf8;
  --accent-dim:    rgba(129, 140, 248, 0.12);
  --accent-glow:   rgba(129, 140, 248, 0.22);
  --success:       #34d399;
  --error:         #ef4444;
  --error-bg:      #1a1012;
  --error-border:  rgba(239, 68, 68, 0.25);
  --radius:        10px;
  --radius-sm:     6px;
  --font:          'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --transition:    0.2s ease;
}

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

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ── 2. App shell & Header ───────────────────────────────────────────────── */
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0 1rem;
}

.header {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 1.125rem 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

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

.logo {
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
  transition: background var(--transition), box-shadow var(--transition);
  animation: pulse-dot 2.8s ease-in-out infinite;
}

.status-text {
  font-size: 0.6875rem;
  color: var(--text-muted);
  font-weight: 400;
  letter-spacing: 0.01em;
}

.header-link {
  font-size: 0.8125rem;
  color: var(--text-dim);
  text-decoration: none;
  transition: color var(--transition);
}
.header-link:hover { color: var(--text-muted); }

/* ── 3. Workspace & Query input ──────────────────────────────────────────── */
.workspace {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding-top: 18vh;
  padding-bottom: 5rem;
  flex: 1;
}

.query-section { margin-bottom: 1.75rem; }

.input-wrapper {
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow:
    0 0 0 3px var(--accent-glow),
    0 4px 24px var(--accent-dim);
}

.input-wrapper.dimmed {
  opacity: 0.55;
  pointer-events: none;
}

.query-input {
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9375rem;
  line-height: 1.65;
  padding: 1rem 1rem 0.5rem;
  min-height: 84px;
  max-height: 240px;
  overflow-y: auto;
}

.query-input::placeholder { color: var(--text-dim); }

.input-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0.875rem 0.75rem;
}

.char-hint {
  font-size: 0.6875rem;
  color: var(--text-dim);
  user-select: none;
}

.ask-btn {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.4375rem 0.875rem;
  font-size: 0.8125rem;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  transition: opacity var(--transition), transform 0.12s ease;
  white-space: nowrap;
}
.ask-btn svg { width: 13px; height: 13px; flex-shrink: 0; }
.ask-btn:hover:not(:disabled) { opacity: 0.85; }
.ask-btn:active:not(:disabled) { transform: scale(0.96); }
.ask-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ── 4. Chips ────────────────────────────────────────────────────────────── */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.875rem;
}

.chip {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 999px;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-family: var(--font);
  padding: 0.3125rem 0.75rem;
  cursor: pointer;
  transition:
    background var(--transition),
    border-color var(--transition),
    color var(--transition),
    transform 0.12s ease;
}
.chip:hover {
  background: rgba(129, 140, 248, 0.07);
  border-color: rgba(129, 140, 248, 0.28);
  color: var(--accent);
  transform: translateY(-1px);
}

/* ── 5. State B: Loading ─────────────────────────────────────────────────── */
.loading-state { padding: 1.5rem 0; }

.loading-dots {
  display: flex;
  gap: 0.375rem;
  margin-bottom: 1.375rem;
}

.loading-dots span {
  display: block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce-dot 1.2s ease-in-out infinite;
}
.loading-dots span:nth-child(2) { animation-delay: 0.18s; }
.loading-dots span:nth-child(3) { animation-delay: 0.36s; }

.skeleton-lines {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.skeleton-line {
  height: 13px;
  border-radius: 4px;
  background: linear-gradient(
    90deg,
    var(--surface-2) 25%,
    var(--border)    50%,
    var(--surface-2) 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.6s ease-in-out infinite;
}
.skeleton-line.long   { width: 88%; }
.skeleton-line.medium { width: 68%; }
.skeleton-line.short  { width: 48%; }

/* ── 6. State C: Answer + Source cards ───────────────────────────────────── */
.answer-state {
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.35s ease, transform 0.35s ease;
}
.answer-state.visible {
  opacity: 1;
  transform: translateY(0);
}

.answer-header,
.sources-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.section-label {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.badge {
  font-size: 0.6875rem;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
}

.answer-text {
  font-size: 0.9375rem;
  line-height: 1.78;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.5rem;
  word-break: break-word;
}

/* Markdown prose styles */
.answer-text > *:first-child { margin-top: 0; }
.answer-text > *:last-child  { margin-bottom: 0; }

.answer-text h1,
.answer-text h2,
.answer-text h3 {
  color: var(--text);
  font-weight: 600;
  line-height: 1.35;
  margin: 1.25rem 0 0.4rem;
}
.answer-text h1 { font-size: 1rem; }
.answer-text h2 { font-size: 0.9375rem; }
.answer-text h3 {
  font-size: 0.8125rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.answer-text p  { margin: 0.6rem 0; }

.answer-text ul,
.answer-text ol { padding-left: 1.375rem; margin: 0.5rem 0; }
.answer-text li { margin: 0.25rem 0; }

.answer-text strong { color: var(--text); font-weight: 600; }
.answer-text em     { color: var(--text-muted); }

.answer-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}

.answer-text blockquote {
  border-left: 3px solid var(--accent);
  background: var(--accent-dim);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  padding: 0.625rem 0.875rem;
  margin: 0.75rem 0;
  color: var(--text-muted);
  font-size: 0.875rem;
}
.answer-text blockquote p { margin: 0; }

.answer-text code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0.1em 0.35em;
  font-size: 0.8125rem;
  font-family: 'Fira Code', 'JetBrains Mono', monospace;
}

/* Inline source citations */
.source-cite {
  display: inline-block;
  font-size: 0.7rem;
  font-style: normal;
  color: var(--accent);
  background: var(--accent-dim);
  border: 1px solid rgba(129, 140, 248, 0.22);
  border-radius: 4px;
  padding: 0.05rem 0.45rem;
  margin-left: 0.25rem;
  white-space: nowrap;
  vertical-align: middle;
  line-height: 1.6;
}

/* Source grid */
.sources-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.625rem;
}

.source-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.875rem;
  transition: border-color var(--transition);
}
.source-card:hover { border-color: var(--border-hover); }

.source-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.2rem;
}

.source-name {
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.rank-badge {
  flex-shrink: 0;
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-dim);
  border: 1px solid rgba(129, 140, 248, 0.22);
  border-radius: 4px;
  padding: 0.05rem 0.4rem;
  letter-spacing: 0.02em;
  line-height: 1.6;
}

.source-page {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.625rem;
}

.relevance-track {
  height: 3px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.relevance-fill {
  height: 100%;
  width: 0%;                              /* animated to real value via JS */
  background: linear-gradient(90deg, var(--accent), #a78bfa);
  border-radius: 999px;
  transition: width 0.55s cubic-bezier(0.4, 0, 0.2, 1) 0.25s;
}

.relevance-score {
  font-size: 0.6875rem;
  color: var(--text-dim);
  margin-top: 0.375rem;
}

/* Sources legend */
.sources-legend {
  margin-top: 0.75rem;
  font-size: 0.6875rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.legend-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-dot--score {
  background: linear-gradient(90deg, var(--accent), #a78bfa);
}

.legend-dot--rank {
  background: var(--accent-dim);
  border: 1px solid rgba(129, 140, 248, 0.3);
}

/* ── 7. State D: Error card ──────────────────────────────────────────────── */
.error-state { padding: 0.25rem 0; }

.error-card {
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: var(--radius);
  padding: 1.125rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.error-icon { font-size: 1rem; }

.error-message {
  font-size: 0.875rem;
  color: #f87171;
  line-height: 1.55;
}

.retry-btn {
  align-self: flex-start;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.8125rem;
  font-family: var(--font);
  cursor: pointer;
  padding: 0;
  text-underline-offset: 3px;
  text-decoration: underline;
  text-decoration-color: transparent;
  transition: color var(--transition), text-decoration-color var(--transition);
}
.retry-btn:hover {
  color: var(--text);
  text-decoration-color: var(--text-muted);
}

/* ── 8. Shared utilities ─────────────────────────────────────────────────── */
.hidden { display: none !important; }

.divider {
  height: 1px;
  background: var(--border);
  margin: 1.375rem 0 1.25rem;
}

/* ── 9. Animations ───────────────────────────────────────────────────────── */
@keyframes pulse-dot {
  0%, 100% { opacity: 1;   box-shadow: 0 0 5px var(--success); }
  50%       { opacity: 0.5; box-shadow: 0 0 10px var(--success); }
}

@keyframes bounce-dot {
  0%, 80%, 100% { transform: translateY(0);   opacity: 0.35; }
  40%           { transform: translateY(-6px); opacity: 1;    }
}

@keyframes shimmer {
  0%   { background-position:  200% 0; }
  100% { background-position: -200% 0; }
}

/* ── 10. Mobile ──────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .workspace       { padding-top: 8vh; }
  .sources-grid    { grid-template-columns: 1fr; }
  .chips           { gap: 0.375rem; }
  .char-hint       { display: none; }  /* hide on very small screens */
}
