:root {
  /* Brand fonts (see index.html <head> comment re: free substitutes) */
  --font-serif: "Source Serif 4", Georgia, serif;
  --font-sans: "Public Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;

  /* Primary brand palette */
  --palmetto-blue: #003366; /* Pantone 533-C */
  --forum-blue: #004aad; /* Pantone 300-C */
  --coast: #f4f4f2;
  --khaki: #e4e0d4;
  --sky: #c2dfed;
  --water: #36a0e0; /* Pantone 279-C */

  /* Secondary "state inspired" palette — sparing use only */
  --state-gold: #ce8b3d;
  --state-maroon: #7f263a;

  --bg: #ffffff;
  --bg-sunken: var(--coast);
  --fg: var(--palmetto-blue);
  --fg-body: #1a1a1a;
  --muted: #5b6b7a;
  --line: #dcd8cb;
  --accent: var(--forum-blue);
  --accent-fg: #ffffff;
  --accent-secondary: var(--water);
  --bubble-user: var(--sky);
  --bubble-assistant: transparent;
  --radius: 14px;
  --header-h: 76px;
  --composer-h: auto;
  --shadow-tint: rgba(0,51,102,.06);
}

html[data-theme="dark"] {
  --bg: #12161d;
  --bg-sunken: #1b212a;
  --fg: #d8e6f3; /* lighter blue stand-in for headings; palmetto-blue is too dark on a dark bg */
  --fg-body: #e6e8eb;
  --muted: #93a4b3;
  --line: #2a323d;
  --accent: #4a90d9;
  --accent-fg: #ffffff;
  --accent-secondary: #6cc0f0;
  --bubble-user: #1f3a57;
  --bubble-assistant: transparent;
  --state-gold: #e0a458;
  --state-maroon: #e08a96;
  --shadow-tint: rgba(0,0,0,.4);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font-sans);
  color: var(--fg-body);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  transition: background-color .2s ease, color .2s ease;
}

/* ---------- Header ---------- */
.app-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 10;
  transition: background-color .2s ease, border-color .2s ease;
}

.app-header .brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  width: 120px;
  height: 120px;
  display: block;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.app-header h1 {
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--fg);
  margin: 0;
  letter-spacing: -0.01em;
}

.theme-toggle {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-sunken);
  color: var(--fg-body);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  line-height: 1;
  flex-shrink: 0;
  cursor: pointer;
  transition: background-color .2s ease, border-color .2s ease;
}

.theme-toggle:hover {
  border-color: var(--accent);
}

.app-header .tagline {
  color: var(--muted);
  font-size: .76rem;
  font-style: italic;
}

.filter-select {
  font: inherit;
  font-size: .82rem;
  font-weight: 500;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-sunken);
  color: var(--fg-body);
  cursor: pointer;
}

.filter-select:focus {
  outline: none;
  border-color: var(--accent);
}

.user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.user-email {
  font-size: .82rem;
  color: var(--muted);
}

.logout-link {
  font-size: .82rem;
  font-weight: 500;
  color: var(--accent);
  text-decoration: none;
}

.logout-link:hover {
  text-decoration: underline;
}

/* ---------- Auth pages (login / not authorized) ---------- */
.auth-page {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  max-width: 360px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.auth-card h2 {
  font-family: var(--font-serif);
  color: var(--fg);
  margin: 0;
}

.auth-card p {
  color: var(--muted);
  margin: 0;
}

.btn-google {
  margin-top: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  font-size: .92rem;
  text-decoration: none;
}

.btn-google:hover {
  background: var(--accent-secondary);
}

/* ---------- Chat area ---------- */
.chat-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
}

.chat-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  min-height: 100%;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  gap: 6px;
}

.empty-state .empty-title {
  font-family: var(--font-serif);
  font-size: 1.3rem;
  font-weight: 600;
  color: var(--fg);
}

.msg {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.msg .avatar {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .72rem;
  font-weight: 700;
  color: #fff;
}

.msg.user .avatar { background: var(--palmetto-blue); }
.msg.assistant .avatar { background: var(--accent); }

.msg .bubble-col {
  flex: 1 1 auto;
  min-width: 0;
}

.msg .role-label {
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 3px;
}

.msg.user .bubble {
  background: var(--bubble-user);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: inline-block;
}

.msg.assistant .bubble {
  background: var(--bubble-assistant);
  padding: 0;
}

/* Markdown rendering inside assistant bubble */
.bubble.markdown { line-height: 1.6; font-size: .96rem; }
.bubble.markdown > *:first-child { margin-top: 0; }
.bubble.markdown > *:last-child { margin-bottom: 0; }
.bubble.markdown p { margin: 0 0 .8em; }
.bubble.markdown h1, .bubble.markdown h2, .bubble.markdown h3 {
  margin: 1.1em 0 .5em;
  font-weight: 600;
  line-height: 1.3;
}
.bubble.markdown h1 { font-size: 1.25rem; }
.bubble.markdown h2 { font-size: 1.1rem; }
.bubble.markdown h3 { font-size: 1rem; }
.bubble.markdown ul, .bubble.markdown ol {
  margin: 0 0 .8em;
  padding-left: 1.4em;
}
.bubble.markdown li { margin: .2em 0; }
.bubble.markdown a { color: var(--accent); text-decoration: none; }
.bubble.markdown a:hover { color: var(--accent-secondary); text-decoration: underline; }
.bubble.markdown code {
  background: var(--bg-sunken);
  border-radius: 4px;
  padding: .15em .35em;
  font-size: .88em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.bubble.markdown pre {
  background: #1e1e1e;
  color: #e6e6e6;
  border-radius: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0 0 .8em;
}
.bubble.markdown pre code {
  background: none;
  padding: 0;
  color: inherit;
}
.bubble.markdown blockquote {
  border-left: 3px solid var(--line);
  margin: 0 0 .8em;
  padding: 0 0 0 .9em;
  color: var(--muted);
}
.bubble.markdown table {
  border-collapse: collapse;
  margin: 0 0 .8em;
  font-size: .9em;
}
.bubble.markdown th, .bubble.markdown td {
  border: 1px solid var(--line);
  padding: 5px 10px;
}
.bubble.markdown hr {
  border: none;
  border-top: 1px solid var(--line);
  margin: 1em 0;
}

/* Streaming caret */
.bubble.streaming::after {
  content: "▋";
  display: inline-block;
  margin-left: 2px;
  animation: blink 1s steps(1) infinite;
  color: var(--muted);
}
@keyframes blink { 50% { opacity: 0; } }

.status-line {
  color: var(--muted);
  font-size: .82rem;
  margin: 4px 0 0;
  min-height: 1.1em;
}

/* ---------- Sources ---------- */
.sources-toggle {
  margin-top: 24px;
}

.sources-toggle summary {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font-size: .82rem;
  font-weight: 500;
  color: #aaa;
  list-style: none;
  user-select: none;
}

.sources-toggle summary::-webkit-details-marker {
  display: none;
}

.sources-toggle summary::before {
  content: "^";
  display: inline-block;
  font-weight: 700;
  transform: rotate(180deg);
  transition: transform .15s ease;
}

.sources-toggle[open] summary::before {
  transform: rotate(0deg);
}

.sources {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.source-card {
  border: 1px solid var(--line);
  border-left: 3px solid var(--state-gold);
  border-radius: 0 10px 10px 0;
  padding: 8px 12px;
  background: var(--bg-sunken);
  font-size: .85rem;
}

.source-card .loc {
  color: var(--muted);
  font-size: .78rem;
  margin-bottom: 3px;
}

.source-card .quote {
  font-style: italic;
  color: var(--fg-body);
}

.source-card:target {
  border-left-color: var(--state-maroon);
  background: var(--bg);
}

.bubble.markdown a[href^="#cite-"] {
  font-size: .75em;
  font-weight: 600;
  vertical-align: super;
  text-decoration: none;
  padding: 0 1px;
}

.error-text {
  color: var(--state-maroon);
  font-size: .88rem;
}

/* ---------- Composer ---------- */
.composer-wrap {
  border-top: 1px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
  transition: background-color .2s ease, border-color .2s ease;
}

.composer {
  max-width: 760px;
  margin: 0 auto;
  padding: 14px 20px 18px;
}

.composer form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 8px 8px 8px 16px;
  background: var(--bg);
  box-shadow: 0 1px 2px var(--shadow-tint);
  transition: border-color .15s ease, background-color .2s ease, box-shadow .2s ease;
}

.composer form:focus-within {
  border-color: var(--accent);
}

.composer-topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}

.topic-label {
  font-size: .72rem;
  color: var(--muted);
  font-weight: 500;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.composer textarea {
  flex: 1 1 auto;
  border: none;
  outline: none;
  resize: none;
  font: inherit;
  font-size: .95rem;
  max-height: 200px;
  padding: 8px 0;
  background: transparent;
  color: var(--fg-body);
}

.composer button.send {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-fg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: opacity .15s;
}

.composer button.send:disabled {
  opacity: .35;
  cursor: default;
}

.composer .hint {
  text-align: center;
  color: var(--muted);
  font-size: .72rem;
  margin: 8px 0 0;
}
