:root {
  --ink: #1a2c2c;
  --muted: #5e8e91;
  --panel: #e8e2d9;
  --panel-strong: #ffffff;
  --edge: #cfc8bd;
  --accent: #133f41;
  --user-bubble: #133f41;
  --user-text: #f0f4f4;
  --bot-bubble: #ffffff;
  --bot-text: #1a2c2c;
  --safety-bg: #fff1ef;
  --safety-border: #d4a7a2;
  --safety-text: #7a2720;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Sora", "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 10%, #ffffff 0%, transparent 42%),
    radial-gradient(circle at 88% 12%, #f4f0ea 0%, transparent 38%),
    linear-gradient(110deg, #ece6dc 0%, #e8e2d9 40%, #e3ddd4 100%);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.app-shell {
  max-width: 780px;
  margin: 0 auto;
  padding: 20px clamp(12px, 3vw, 40px) 16px;
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* ── Header ── */

.hero {
  margin-bottom: 16px;
  flex-shrink: 0;
}

.hero-top {
  display: flex;
  align-items: flex-start;
  gap: 16px;
}

.hero-title {
  flex: 1;
}

.hero-logo {
  height: 36px;
  width: auto;
  object-fit: contain;
  flex-shrink: 0;
}

.kicker {
  margin: 0;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.72rem;
}

.hero h1 {
  margin: 2px 0 4px;
  font-size: clamp(1.3rem, 2.2vw, 1.8rem);
  line-height: 1.15;
}

.subtitle {
  margin: 0;
  color: #56665f;
  font-size: 0.92rem;
}

/* ── Login ── */

.login-card {
  border: 1px solid #b8cdcf;
  border-radius: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #f0ece4 100%);
  box-shadow: 0 6px 18px #5b898c20;
  padding: 28px 32px;
  max-width: 380px;
  margin: 0 auto;
  width: 100%;
}

.login-card h2 {
  margin: 0 0 20px;
  font-size: 1.2rem;
  color: var(--accent);
}

.login-card label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 4px;
}

.login-card input {
  display: block;
  width: 100%;
  border: 1px solid #9bb8ba;
  border-radius: 8px;
  padding: 10px 12px;
  background: #ffffff;
  color: var(--ink);
  font-size: 0.95rem;
  font-family: inherit;
  margin-bottom: 16px;
}

.login-card input:focus {
  outline: 2px solid #6d9ea1;
  outline-offset: 1px;
}

.login-card button {
  display: block;
  width: 100%;
  border: none;
  border-radius: 8px;
  padding: 12px;
  background: var(--accent);
  color: #f0f4f4;
  font-size: 0.95rem;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s;
}

.login-card button:hover {
  background: #1a5558;
}

.login-card button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.login-error {
  margin: 12px 0 0;
  padding: 8px 12px;
  border: 1px solid var(--safety-border);
  background: var(--safety-bg);
  color: var(--safety-text);
  border-radius: 8px;
  font-size: 0.85rem;
}

/* ── Chat screen ── */

.chat-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ── Welcome ── */

.welcome-msg {
  border: 1px solid #b8cdcf;
  border-radius: 14px;
  background: linear-gradient(180deg, #ffffff 0%, #f0ece4 100%);
  box-shadow: 0 6px 18px #5b898c20;
  padding: 20px 24px;
  font-size: 0.92rem;
  line-height: 1.5;
  color: #374151;
}

.welcome-msg p {
  margin: 0 0 8px;
}

.welcome-msg p:last-child {
  margin-bottom: 0;
}

.welcome-hint {
  color: var(--muted);
  font-size: 0.82rem;
  font-style: italic;
}

/* ── Message bubbles ── */

.msg {
  max-width: 85%;
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 0.92rem;
  line-height: 1.55;
  word-wrap: break-word;
}

.msg-user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}

.msg-bot {
  align-self: flex-start;
  background: var(--bot-bubble);
  color: var(--bot-text);
  border: 1px solid #d6deda;
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 8px #5b898c10;
}

.msg-safety {
  align-self: flex-start;
  background: var(--safety-bg);
  color: var(--safety-text);
  border: 1px solid var(--safety-border);
  border-bottom-left-radius: 4px;
  font-weight: 600;
}

.msg-bot h1, .msg-bot h2, .msg-bot h3 {
  margin: 12px 0 6px;
  color: var(--accent);
}

.msg-bot h1:first-child, .msg-bot h2:first-child, .msg-bot h3:first-child {
  margin-top: 0;
}

.msg-bot h1 { font-size: 1.05rem; }
.msg-bot h2 { font-size: 0.98rem; }
.msg-bot h3 { font-size: 0.92rem; }

.msg-bot p {
  margin: 0 0 8px;
}

.msg-bot p:last-child {
  margin-bottom: 0;
}

.msg-bot ul, .msg-bot ol {
  margin: 4px 0 8px;
  padding-left: 20px;
}

.msg-bot li {
  margin-bottom: 4px;
}

.msg-bot hr {
  border: none;
  border-top: 1px solid #d6deda;
  margin: 12px 0;
}

.msg-bot strong {
  color: var(--accent);
}

.msg-bot em {
  color: var(--muted);
  font-size: 0.85rem;
}

.msg-bot code {
  background: #f4f0ea;
  padding: 1px 4px;
  border-radius: 4px;
  font-size: 0.85em;
}

/* ── Message actions row ── */

.msg-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}

/* Feedback buttons */

.feedback-wrap {
  display: inline-flex;
  gap: 2px;
  margin-left: auto;
}

.feedback-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: none;
  color: #b0bec5;
  cursor: pointer;
  transition: all 0.15s;
  padding: 0;
  font-family: inherit;
}

.feedback-btn:hover {
  color: var(--accent);
  background: #f4f8f8;
  border-color: #b8cdcf;
}

.feedback-btn.selected {
  color: var(--accent);
  background: #e0f0f0;
  border-color: var(--accent);
}

.feedback-wrap.voted .feedback-btn:not(.selected) {
  opacity: 0.3;
  pointer-events: none;
}

/* Sources button */

.sources-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 4px 10px;
  border: 1px solid #b8cdcf;
  border-radius: 6px;
  background: #f4f8f8;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.12s;
}

.sources-btn:hover {
  background: #e8f0f0;
  color: var(--accent);
}

/* ── Sources panel ── */

.sources-panel {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 50%;
  overflow-y: auto;
  background: #ffffff;
  border: 1px solid #b8cdcf;
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -4px 20px #5b898c18;
  padding: 16px 20px;
  z-index: 10;
}

.sources-panel h3 {
  margin: 0 0 12px;
  font-size: 0.92rem;
  color: var(--accent);
}

.sources-close {
  position: absolute;
  top: 12px;
  right: 16px;
  border: none;
  background: none;
  font-size: 1.4rem;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
}

.sources-close:hover {
  color: var(--ink);
}

.source-item {
  border-left: 3px solid #cbd5e1;
  background: #f8fafc;
  border-radius: 0 8px 8px 0;
  padding: 8px 12px;
  margin-bottom: 8px;
}

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

.source-grade {
  display: inline-block;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border-radius: 999px;
  padding: 2px 8px;
  margin-bottom: 4px;
}

.grade-high {
  background: #1a1a2e;
  color: #ffffff;
}

.grade-moderate {
  background: #e5e7eb;
  color: #374151;
}

.grade-low {
  background: #f3f4f6;
  color: #9ca3af;
}

.source-title {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: #111827;
  line-height: 1.3;
}

.source-title a {
  color: inherit;
  text-decoration: none;
}

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

.source-journal {
  margin: 2px 0 0;
  font-size: 0.72rem;
  color: #6b7280;
}

/* ── Typing indicator ── */

.typing-indicator {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  background: var(--bot-bubble);
  border: 1px solid #d6deda;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing 1.2s ease-in-out infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ── Chat input ── */

.chat-input-bar {
  display: flex;
  gap: 8px;
  padding: 12px 0 4px;
  flex-shrink: 0;
}

.chat-input-bar input {
  flex: 1;
  border: 1px solid #9bb8ba;
  border-radius: 10px;
  padding: 12px 14px;
  background: #ffffff;
  color: var(--ink);
  font-size: 0.95rem;
  font-family: inherit;
}

.chat-input-bar input:focus {
  outline: 2px solid #6d9ea1;
  outline-offset: 1px;
}

.chat-input-bar button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: #f0f4f4;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}

.chat-input-bar button:hover {
  background: #1a5558;
}

.chat-input-bar button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── User Badge (logged in as) ── */

.user-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: rgba(19, 63, 65, 0.08);
  border: 1px solid rgba(19, 63, 65, 0.15);
  border-radius: 999px;
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 600;
  line-height: 1;
  white-space: nowrap;
}

.user-badge::before {
  content: "●";
  color: #16a34a;
  font-size: 0.6rem;
  line-height: 1;
}

.user-badge .user-badge-label {
  color: var(--muted);
  font-weight: 500;
}

.hero-top .user-badge {
  margin-left: auto;
  margin-right: 12px;
  align-self: center;
}

/* ── Logout ── */

.logout-btn {
  display: block;
  margin: 8px auto 0;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  padding: 4px 12px;
}

.logout-btn:hover {
  color: var(--ink);
}

/* ── Footer ── */

.meta {
  margin-top: 12px;
  color: #5f6f67;
  font-size: 0.78rem;
  text-align: center;
  flex-shrink: 0;
}

.meta p {
  margin: 2px 0;
}

.meta .disclaimer {
  margin-top: 4px;
  font-size: 0.72rem;
  color: #7a8a82;
  font-style: italic;
}

/* ── Responsive ── */

@media (max-width: 600px) {
  .app-shell {
    padding: 14px 10px 10px;
  }

  .hero-top {
    flex-direction: row;
    gap: 8px;
  }

  .hero-logo {
    height: 24px;
  }

  .hero h1 {
    font-size: 1.15rem;
  }

  .kicker {
    font-size: 0.6rem;
  }

  .subtitle {
    font-size: 0.78rem;
  }

  .login-card {
    padding: 20px;
  }

  .msg {
    max-width: 92%;
    font-size: 0.88rem;
  }

  .welcome-msg {
    padding: 16px;
  }

  .sources-panel {
    max-height: 60%;
    padding: 14px 16px;
  }
}