/* Design tokens. Every colour, space, radius, shadow and duration below comes from
   here; components never use raw values. Palette: cool slate neutrals with one
   indigo accent (plus a violet used only inside the brand gradient). Body text and
   button labels meet WCAG AA (4.5:1) in both themes; softness is spent on
   surfaces, borders and the background glow. */
:root {
  --color-bg: #f6f7fb;
  --color-surface: #ffffff;
  --color-surface-muted: #eef0f6;
  --color-border: #e2e5ef;
  --color-text: #1e2233;
  --color-text-muted: #5b6178;
  --color-accent: #4f4fd0;
  --color-accent-hover: #4343bd;
  --color-accent-soft: #ecebfd;
  --color-accent-soft-text: #3f3fb3;
  --color-on-accent: #ffffff;
  --color-success: #16a34a;
  --color-danger: #b42335;
  --color-danger-soft: #fdecee;
  --color-focus: #6366f1;
  --gradient-brand: linear-gradient(135deg, #5558e8, #7a4fe0);
  --glow-1: rgb(99 102 241 / 0.16);
  --glow-2: rgb(139 92 246 / 0.12);
  --composer-bg: rgb(255 255 255 / 0.82);

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-full: 999px;

  --shadow-card: 0 1px 2px rgb(30 34 51 / 0.04), 0 8px 24px rgb(30 34 51 / 0.06);
  --shadow-float: 0 2px 6px rgb(30 34 51 / 0.05), 0 16px 40px rgb(30 34 51 / 0.1);
  --shadow-accent: 0 6px 16px rgb(79 79 208 / 0.28);

  --font-body: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --text-xs: 0.75rem;
  --text-sm: 0.875rem;
  --text-md: 0.9375rem;
  --text-lg: 1.125rem;
  --text-xl: 1.5rem;
  --leading: 1.65;

  --duration-fast: 120ms;
  --duration-base: 200ms;
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  --content-width: 720px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #12141c;
    --color-surface: #1a1d29;
    --color-surface-muted: #222636;
    --color-border: #2b3042;
    --color-text: #e8eaf2;
    --color-text-muted: #a2a8bd;
    --color-accent: #8b8cf7;
    --color-accent-hover: #a2a3fa;
    --color-accent-soft: #272a4a;
    --color-accent-soft-text: #c3c4fc;
    --color-on-accent: #12141c;
    --color-success: #4ade80;
    --color-danger: #f8a3ad;
    --color-danger-soft: #3a1f26;
    --color-focus: #8b8cf7;
    --glow-1: rgb(99 102 241 / 0.18);
    --glow-2: rgb(139 92 246 / 0.12);
    --composer-bg: rgb(26 29 41 / 0.82);
    --shadow-card: 0 1px 2px rgb(0 0 0 / 0.2), 0 8px 24px rgb(0 0 0 / 0.25);
    --shadow-float: 0 2px 6px rgb(0 0 0 / 0.25), 0 16px 40px rgb(0 0 0 / 0.35);
    --shadow-accent: 0 6px 16px rgb(139 140 247 / 0.25);
  }
}

/* --- base ------------------------------------------------------------------- */

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

body {
  margin: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  font-size: var(--text-md);
  line-height: var(--leading);
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--color-accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

:focus-visible {
  outline: 2px solid var(--color-focus);
  outline-offset: 2px;
}

svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Soft brand glow behind the page. Fixed and painted once: no animation, no
   scroll cost beyond a single composited layer. */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(60rem 30rem at 15% -10%, var(--glow-1), transparent 70%),
    radial-gradient(50rem 28rem at 95% 0%, var(--glow-2), transparent 70%);
}

/* --- layout ----------------------------------------------------------------- */

.page {
  max-width: var(--content-width);
  min-height: 100dvh;
  margin: 0 auto;
  padding: var(--space-6) var(--space-4) 0;
  display: flex;
  flex-direction: column;
}

.chat {
  flex: 1;
  padding: var(--space-6) 0 var(--space-5);
}

/* --- avatar ------------------------------------------------------------------ */

.avatar {
  display: block;
  flex-shrink: 0;
  border-radius: var(--radius-full);
  object-fit: cover;
  /* Shown only while the photo loads, or if it fails to. */
  background: var(--gradient-brand);
}

.avatar--large {
  width: 52px;
  height: 52px;
  box-shadow:
    0 0 0 3px var(--color-surface),
    var(--shadow-accent);
}

.avatar--small {
  width: 32px;
  height: 32px;
  box-shadow: 0 0 0 2px var(--color-surface);
}

/* --- profile header ---------------------------------------------------------- */

.profile {
  display: flex;
  align-items: flex-start;
  gap: var(--space-4);
}

.profile-text {
  flex: 1;
  min-width: 0;
}

.profile h1 {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: 650;
  letter-spacing: -0.01em;
  line-height: 1.3;
}

.profile-role {
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.profile-links {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-4);
  margin: var(--space-1) 0 0;
  padding: 0;
  list-style: none;
  font-size: var(--text-xs);
  font-weight: 500;
}

.profile-links a {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
}

.profile-links svg {
  width: 14px;
  height: 14px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin: 0;
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  background: var(--color-surface);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: 500;
  white-space: nowrap;
}

/* On narrow screens the badge would squeeze the name and role onto extra lines;
   the welcome text already says what the page is. */
@media (max-width: 520px) {
  .status-badge {
    display: none;
  }
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: var(--radius-full);
  background: var(--color-success);
}

/* --- welcome (empty state) ------------------------------------------------------ */

.welcome h2 {
  margin: 0 0 var(--space-2);
  font-size: var(--text-xl);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.25;
}

.welcome-text {
  max-width: 34rem;
  margin: 0 0 var(--space-5);
  color: var(--color-text-muted);
}

.suggestion-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
  margin: 0;
  padding: 0;
  list-style: none;
}

@media (max-width: 520px) {
  .suggestion-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

.suggestion {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  padding: var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
  color: var(--color-text);
  font: inherit;
  font-size: var(--text-sm);
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition:
    transform var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.suggestion:hover {
  border-color: var(--color-accent);
  transform: translateY(-1px);
}

.suggestion-icon {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  padding: var(--space-2);
  border-radius: var(--radius-md);
  background: var(--color-accent-soft);
  color: var(--color-accent-soft-text);
}

/* --- conversation ------------------------------------------------------------ */

.conversation {
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
  margin: 0;
  padding: 0;
  list-style: none;
}

.exchange {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  animation: settle-in var(--duration-base) var(--ease-out);
}

.question-bubble {
  align-self: flex-end;
  max-width: 82%;
  margin: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg) var(--radius-lg) var(--radius-sm) var(--radius-lg);
  background: var(--color-accent);
  color: var(--color-on-accent);
  box-shadow: var(--shadow-accent);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.reply {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
}

.answer {
  flex: 1;
  min-width: 0;
  padding: var(--space-4) var(--space-5);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm) var(--radius-lg) var(--radius-lg) var(--radius-lg);
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
}

.answer-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.answer-text:empty {
  display: none;
}

.answer.is-error {
  border-color: transparent;
  background: var(--color-danger-soft);
  box-shadow: none;
  color: var(--color-danger);
}

/* The "!" marker means the error is not conveyed by colour alone. */
.answer.is-error .answer-text::before {
  content: "! ";
  font-weight: 700;
}

/* --- inline citations --------------------------------------------------------- */

.cite {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.35em;
  height: 1.35em;
  margin: 0 1px;
  padding: 0 0.3em;
  border-radius: var(--radius-full);
  background: var(--color-accent-soft);
  color: var(--color-accent-soft-text);
  font-size: var(--text-xs);
  font-weight: 600;
  line-height: 1;
  vertical-align: 0.15em;
  cursor: default;
}

/* --- thinking indicator (the only looping animation: a real loading state) --- */

.thinking {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.thinking[hidden] {
  display: none;
}

.thinking-dots {
  display: inline-flex;
  gap: var(--space-1);
}

.thinking-dots span {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  opacity: 0.3;
  animation: pulse 1s var(--ease-out) infinite;
}

.thinking-dots span:nth-child(2) {
  animation-delay: 150ms;
}

.thinking-dots span:nth-child(3) {
  animation-delay: 300ms;
}

/* --- sources ----------------------------------------------------------------- */

.sources {
  margin-top: var(--space-4);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}

.sources summary {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  margin-left: calc(-1 * var(--space-2));
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: 500;
  list-style: none;
  cursor: pointer;
}

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

.sources summary:hover {
  background: var(--color-surface-muted);
  color: var(--color-text);
}

.sources-icon,
.chevron {
  width: 16px;
  height: 16px;
}

.chevron {
  transition: transform var(--duration-fast) var(--ease-out);
}

.sources[open] .chevron {
  transform: rotate(90deg);
}

.source-list {
  display: grid;
  gap: var(--space-2);
  margin: var(--space-3) 0 0;
  padding: 0;
  list-style: none;
}

.source {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--color-surface-muted);
}

.source-number {
  flex-shrink: 0;
  margin-top: 1px;
}

.source-heading {
  display: block;
  font-size: var(--text-sm);
  font-weight: 550;
  line-height: 1.45;
}

.source-document {
  display: block;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

/* --- composer --------------------------------------------------------------------- */

.composer-dock {
  position: sticky;
  bottom: 0;
  padding: var(--space-3) 0 var(--space-4);
  background: linear-gradient(to bottom, transparent, var(--color-bg) 30%);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  background: var(--composer-bg);
  box-shadow: var(--shadow-float);
  backdrop-filter: blur(12px);
  transition: border-color var(--duration-fast) var(--ease-out);
}

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

.composer textarea {
  flex: 1;
  min-height: 40px;
  max-height: 160px;
  padding: var(--space-2) 0;
  border: 0;
  background: transparent;
  color: var(--color-text);
  font: inherit;
  line-height: 1.5;
  resize: none;
  /* Grows with its content (see resizeQuestionInput); scrolls only past max-height. */
  overflow-y: auto;
  scrollbar-width: thin;
}

.composer textarea::placeholder {
  color: var(--color-text-muted);
}

/* The composer shows focus via :focus-within, so an inner outline would double up. */
.composer textarea:focus-visible {
  outline: none;
}

.send-button {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: var(--color-on-accent);
  cursor: pointer;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    transform var(--duration-fast) var(--ease-out);
}

.send-button svg {
  width: 20px;
  height: 20px;
  stroke-width: 2.2;
}

.send-button:hover {
  background: var(--color-accent-hover);
}

.send-button:active {
  transform: scale(0.94);
}

.send-button:disabled {
  opacity: 0.5;
  cursor: progress;
}

.hint {
  margin: var(--space-2) var(--space-4) 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.hint.is-error {
  color: var(--color-danger);
}

.hint.is-error::before {
  content: "! ";
  font-weight: 700;
}

.footer {
  margin: var(--space-2) var(--space-4) 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

/* --- motion -------------------------------------------------------------------- */

@keyframes settle-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes pulse {
  50% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }

  .thinking-dots span {
    opacity: 0.7;
  }
}
