/* ── Design tokens ── */
:root {
  --navy:          #0F2144;
  --navy-border:   rgba(255, 255, 255, 0.1);
  --bg:            #F4F4F1;
  --surface:       #FFFFFF;
  --text:          #0F172A;
  --muted:         #64748B;
  --accent:        #4A7FA5;
  --accent-hover:  #3A6585;
  --synthesis-bg:  #EBF1F6;
  --border:        #DDE3EA;
  --error-bg:      #FEF2F2;
  --error-border:  #FECACA;
  --error-text:    #B91C1C;
  --radius:        4px;
  --max-w:         800px;
}

[data-theme="dark"] {
  --navy:          #0F2144;
  --navy-border:   rgba(255, 255, 255, 0.12);
  --bg:            #0B111C;
  --surface:       #14202F;
  --text:          #E2E8F0;
  --muted:         #8899A6;
  --accent:        #5B93BE;
  --accent-hover:  #4A7FA5;
  --synthesis-bg:  #122035;
  --border:        #1E2D3E;
  --error-bg:      #2D1515;
  --error-border:  #7F1D1D;
  --error-text:    #FCA5A5;
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

/* ── Base ── */
body {
  font-family: 'Lato', -apple-system, BlinkMacSystemFont, sans-serif;
  font-weight: 400;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ── Site header — full bleed navy ── */
.site-header {
  background: var(--navy);
  border-bottom: 1px solid var(--navy-border);
}

.header-inner {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 28px 24px 26px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.header-brand {
  min-width: 0;
}

#theme-toggle {
  background: none;
  border: 1px solid var(--navy-border);
  border-radius: var(--radius);
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  padding: 6px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s, border-color 0.15s;
}

#theme-toggle:hover {
  color: rgba(255, 255, 255, 0.9);
  border-color: rgba(255, 255, 255, 0.25);
}

.wordmark {
  font-family: 'EB Garamond', Georgia, serif;
  font-size: 1.55rem;
  font-weight: 600;
  color: #FFFFFF;
  letter-spacing: -0.2px;
  line-height: 1.2;
}

.tagline {
  font-size: 0.8rem;
  font-weight: 300;
  color: rgba(255, 255, 255, 0.52);
  margin-top: 5px;
  letter-spacing: 0.01em;
}

/* ── Main content ── */
main {
  flex: 1;
  padding-bottom: 80px;
}

.container {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 40px 24px 0;
}

/* ── Search box ── */
.search-box {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.search-controls {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.input-wrap {
  position: relative;
  width: 100%;
}

#query {
  width: 100%;
  padding: 13px 40px 13px 16px;
  font-family: 'Lato', sans-serif;
  font-size: 1rem;
  font-weight: 400;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

#clear-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 1.1rem;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  transition: color 0.15s;
}

#clear-btn:hover { color: var(--text); }

#query::placeholder {
  color: #A0AEC0;
  font-style: italic;
}

#query:focus {
  border-color: var(--accent);
}

#chapter-filter {
  padding: 13px 12px;
  font-family: 'Lato', sans-serif;
  font-size: 0.875rem;
  font-weight: 400;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  outline: none;
  cursor: pointer;
  flex: 1;
  transition: border-color 0.15s;
}

#chapter-filter:focus { border-color: var(--accent); }

#search-btn {
  padding: 13px 22px;
  font-family: 'Lato', sans-serif;
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

#search-btn:hover { background: var(--accent-hover); }
#search-btn:disabled { opacity: 0.55; cursor: not-allowed; }

/* ── Loading ── */
#loading {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 28px;
  color: var(--muted);
  font-size: 0.875rem;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Error ── */
#error-msg {
  margin-top: 24px;
  padding: 12px 16px;
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  border-radius: var(--radius);
  color: var(--error-text);
  font-size: 0.9rem;
}

/* ── Results ── */
#results { margin-top: 36px; }

/* Section labels — ANSWER / SOURCE SECTIONS */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.section-label {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  margin-bottom: 0;
}

#result-count {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.7rem;
  color: var(--muted);
  opacity: 0.75;
}

.mpep-revision {
  font-size: 0.7rem;
  color: var(--muted);
  opacity: 0.7;
  font-style: italic;
}

#copy-answer-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 3px 10px;
  font-family: 'Lato', sans-serif;
  font-size: 0.72rem;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}

#copy-answer-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ── Synthesis / Answer block ── */
#synthesis-section {
  margin-bottom: 40px;
}

.synthesis-body {
  background: var(--synthesis-bg);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 20px 24px;
  font-size: 1rem;
  line-height: 1.75;
  color: var(--text);
  min-height: 3rem;
}

/* Markdown inside synthesis */
.synthesis-body h1,
.synthesis-body h2,
.synthesis-body h3 {
  font-family: 'Lato', sans-serif;
  font-size: 0.9rem;
  font-weight: 700;
  margin: 1rem 0 0.3rem;
  color: var(--text);
}

.synthesis-body h1:first-child,
.synthesis-body h2:first-child,
.synthesis-body h3:first-child { margin-top: 0; }

.synthesis-body p { margin-bottom: 0.55rem; }
.synthesis-body p:last-child { margin-bottom: 0; }

.synthesis-body ul,
.synthesis-body ol {
  margin: 0.25rem 0 0.55rem 1.25rem;
}

.synthesis-body li { margin-bottom: 0.2rem; }
.synthesis-body strong { font-weight: 700; }

.synthesis-body blockquote {
  border-left: 2px solid var(--border);
  margin: 0.75rem 0;
  padding: 0.5rem 1rem;
  color: var(--muted);
  font-style: italic;
}

.synthesis-body blockquote p { margin-bottom: 0; }

/* Streaming cursor */
.synthesis-body.streaming::after {
  content: "▍";
  animation: blink 1s step-start infinite;
  color: var(--accent);
}

@keyframes blink { 50% { opacity: 0; } }

/* ── Source citation cards ── */
#sources-section { margin-bottom: 48px; }

.source-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  overflow: hidden;
}

.source-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 16px;
  cursor: pointer;
  user-select: none;
  gap: 12px;
}

.source-header:hover {
  background: #FAFAFA;
}

.source-meta {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex: 1;
  min-width: 0;
  flex-wrap: wrap;
}

.source-ref {
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  flex-shrink: 0;
}

.source-sep {
  color: var(--border);
  font-size: 0.8rem;
  flex-shrink: 0;
}

.source-title {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.source-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.source-link {
  font-size: 0.75rem;
  color: var(--muted);
  text-decoration: none;
  transition: color 0.15s;
}

.source-link:hover { color: var(--accent); text-decoration: underline; }

.copy-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2px 8px;
  font-family: 'Lato', sans-serif;
  font-size: 0.72rem;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
}

.copy-btn:hover { color: var(--accent); border-color: var(--accent); }

.source-toggle {
  font-size: 0.7rem;
  color: var(--muted);
  flex-shrink: 0;
  line-height: 1;
}

.source-body {
  display: none;
  border-top: 1px solid var(--border);
  padding: 16px 20px;
  font-size: 0.875rem;
  line-height: 1.7;
  color: var(--muted);
}

.source-body p {
  margin-bottom: 0.6rem;
}

.source-body p:last-child { margin-bottom: 0; }

.source-card.open .source-body { display: block; }

/* ── Footer ── */
footer {
  margin-top: 48px;
  padding-bottom: 32px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  opacity: 0.7;
}

kbd {
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.7rem;
  background: var(--border);
  border: 1px solid var(--muted);
  border-radius: 3px;
  padding: 1px 4px;
  opacity: 0.8;
}

/* ── Theme transition ── */
body, .site-header, .source-card, .synthesis-body,
#query, #chapter-filter, .source-body, .source-header {
  transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}

/* ── Responsive ── */
@media (max-width: 640px) {
  .header-inner { padding: 22px 20px 20px; }
  .wordmark { font-size: 1.35rem; }
  .container { padding: 28px 16px 0; }
  .search-controls { flex-direction: column; }
  #search-btn,
  #chapter-filter { width: 100%; }
  .source-title { white-space: normal; }
}
