:root {
  color-scheme: light dark;
  --ink: #0e1a1d;
  --muted: #5b6a70;
  --paper: #f7f2e8;
  --panel: #ffffff;
  --accent: #c57a2b;
  --accent-2: #1f8a70;
  --accent-3: #e0b25f;
  --shadow: 0 22px 60px rgba(15, 27, 31, 0.18);
  --soft-shadow: 0 14px 36px rgba(15, 27, 31, 0.12);
  --radius: 20px;
}

.theme-dark {
  color-scheme: dark;
  --ink: #e6efef;
  --muted: #a5b3b8;
  --paper: #0d1315;
  --panel: #141c1f;
  --accent: #d89435;
  --accent-2: #3bb79d;
  --accent-3: #efc36c;
  --shadow: 0 22px 60px rgba(0, 0, 0, 0.45);
  --soft-shadow: 0 14px 36px rgba(0, 0, 0, 0.32);
}

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

body {
  margin: 0;
  font-family: "Manrope", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
  min-height: 100vh;
  overflow-x: hidden;
}

.chat-glow {
  position: fixed;
  inset: -10% -20%;
  background: radial-gradient(
      circle at 10% 20%,
      rgba(224, 178, 95, 0.25),
      transparent 60%
    ),
    radial-gradient(
      circle at 90% 10%,
      rgba(31, 138, 112, 0.25),
      transparent 55%
    ),
    linear-gradient(140deg, rgba(197, 122, 43, 0.12), transparent 60%);
  filter: blur(8px);
  z-index: -2;
}

.chat-grid {
  position: fixed;
  inset: 0;
  background-image: linear-gradient(
      rgba(15, 27, 31, 0.05) 1px,
      transparent 1px
    ),
    linear-gradient(90deg, rgba(15, 27, 31, 0.05) 1px, transparent 1px);
  background-size: 40px 40px;
  opacity: 0.2;
  z-index: -1;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem 6vw;
}

.brand {
  display: flex;
  gap: 0.9rem;
  align-items: center;
  text-decoration: none;
  color: inherit;
}

.brand-mark {
  width: 46px;
  height: 46px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #fff;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  box-shadow: var(--soft-shadow);
}

.brand-subtitle {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
}

.back-link {
  text-decoration: none;
  color: var(--accent-2);
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.theme-toggle {
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 1px solid rgba(15, 27, 31, 0.2);
  background: rgba(31, 138, 112, 0.08);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}

.chat-main {
  display: grid;
  grid-template-columns: minmax(250px, 360px) minmax(0, 1fr);
  gap: 2.5rem;
  padding: 1rem 6vw 4rem;
  align-items: start;
}

.chat-intro {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.eyebrow {
  text-transform: uppercase;
  letter-spacing: 0.2em;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent-2);
}

h1,
h2,
h3 {
  font-family: "Cinzel", "Times New Roman", serif;
  letter-spacing: 0.02em;
  margin: 0;
}

.lead {
  color: var(--muted);
  line-height: 1.6;
}

.prompt-card,
.signal-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1.6rem;
  box-shadow: var(--soft-shadow);
  border: 1px solid rgba(15, 27, 31, 0.08);
}

.prompt-grid {
  display: grid;
  gap: 0.8rem;
  margin-top: 1rem;
}

.prompt-grid button {
  text-align: left;
  border: 1px solid rgba(15, 27, 31, 0.12);
  background: rgba(248, 244, 236, 0.7);
  border-radius: 14px;
  padding: 0.8rem 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.prompt-grid button:hover {
  transform: translateY(-2px);
  box-shadow: var(--soft-shadow);
}

.signal-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.2rem;
}

.signal-pill {
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  background: rgba(31, 138, 112, 0.12);
  color: var(--accent-2);
  font-weight: 600;
  white-space: nowrap;
}

.chat-panel {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid rgba(15, 27, 31, 0.08);
  display: flex;
  flex-direction: column;
  height: calc(100vh - 180px);
  max-height: 820px;
  position: sticky;
  top: 1.5rem;
}

.chat-stream {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  min-height: 0;
  scrollbar-gutter: stable;
}

.message {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
  align-items: flex-start;
}

.message .bubble {
  padding: 1rem 1.2rem;
  border-radius: 16px;
  max-width: 78%;
  line-height: 1.5;
  white-space: pre-wrap;
}

.message.user {
  align-items: flex-end;
}

.message.user .bubble {
  background: rgba(31, 138, 112, 0.12);
  border: 1px solid rgba(31, 138, 112, 0.2);
  color: var(--ink);
}

.message.assistant .bubble {
  background: rgba(248, 244, 236, 0.9);
  border: 1px solid rgba(15, 27, 31, 0.1);
  color: var(--ink);
}

.bubble.typing::after {
  content: " ...";
  color: var(--muted);
}

.sources {
  margin-top: 1rem;
  display: grid;
  gap: 0.8rem;
}

.sources-title {
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.source-card {
  border-radius: 14px;
  border: 1px solid rgba(15, 27, 31, 0.1);
  background: rgba(255, 255, 255, 0.9);
  padding: 0.9rem 1rem;
  display: grid;
  gap: 0.5rem;
}

.source-title {
  font-weight: 700;
  color: var(--ink);
}

.source-meta {
  font-size: 0.85rem;
  color: var(--muted);
}

.source-snippet {
  color: var(--muted);
  line-height: 1.5;
}

.source-link {
  appearance: none;
  border: 1px solid rgba(15, 27, 31, 0.12);
  background: rgba(31, 138, 112, 0.12);
  color: var(--accent-2);
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-self: start;
  padding: 0.35rem 0.8rem;
  border-radius: 999px;
  text-decoration: none;
}

.chat-input {
  display: flex;
  gap: 1rem;
  padding: 1.5rem;
  border-top: 1px solid rgba(15, 27, 31, 0.08);
}

.chat-input textarea {
  flex: 1;
  border-radius: 14px;
  border: 1px solid rgba(15, 27, 31, 0.2);
  padding: 0.85rem 1rem;
  font-family: inherit;
  font-size: 1rem;
  background: var(--panel);
  color: var(--ink);
  resize: vertical;
  min-height: 54px;
}

.chat-input textarea:disabled,
.chat-input button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.chat-input button {
  padding: 0 1.6rem;
  border: none;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--accent), var(--accent-3));
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--soft-shadow);
}

.chat-meta {
  display: flex;
  justify-content: space-between;
  padding: 0 1.6rem 1.6rem;
  color: var(--muted);
  font-size: 0.85rem;
}

#chatStatus[data-state="loading"]::after {
  content: " ...";
  animation: pulseDots 1.2s infinite;
}

#chatStatus[data-state="error"] {
  color: #c04a4a;
}

@keyframes pulseDots {
  0%,
  100% {
    opacity: 0.3;
  }
  50% {
    opacity: 1;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.theme-dark body {
  background: var(--paper);
}

.theme-dark .chat-glow {
  background: radial-gradient(
      circle at 10% 20%,
      rgba(216, 148, 53, 0.2),
      transparent 60%
    ),
    radial-gradient(
      circle at 90% 10%,
      rgba(59, 183, 157, 0.2),
      transparent 55%
    ),
    linear-gradient(140deg, rgba(216, 148, 53, 0.12), transparent 60%);
}

.theme-dark .chat-grid {
  background-image: linear-gradient(
      rgba(255, 255, 255, 0.05) 1px,
      transparent 1px
    ),
    linear-gradient(90deg, rgba(255, 255, 255, 0.05) 1px, transparent 1px);
}

.theme-dark .prompt-grid button {
  background: rgba(20, 28, 31, 0.7);
  border-color: rgba(255, 255, 255, 0.12);
  color: var(--ink);
}

.theme-dark .message.user .bubble {
  background: rgba(59, 183, 157, 0.18);
  border-color: rgba(59, 183, 157, 0.3);
}

.theme-dark .message.assistant .bubble {
  background: rgba(20, 28, 31, 0.9);
  border-color: rgba(255, 255, 255, 0.1);
}

.theme-dark .source-card {
  background: rgba(20, 28, 31, 0.9);
  border-color: rgba(255, 255, 255, 0.1);
}

.theme-dark .theme-toggle {
  background: rgba(59, 183, 157, 0.12);
  border-color: rgba(255, 255, 255, 0.18);
}

@media (max-width: 1000px) {
  .chat-main {
    grid-template-columns: 1fr;
  }

  .chat-panel {
    height: auto;
    max-height: none;
    position: static;
    order: -1;
  }
}

@media (max-width: 700px) {
  .chat-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.8rem;
  }

  .chat-input {
    flex-direction: column;
    align-items: stretch;
  }

  .chat-input button {
    height: 48px;
  }
}
