:root {
  --navy: #0c2340;
  --navy-700: #16324f;
  --teal: #0e9f9a;
  --teal-soft: #e3f4f3;
  --bg: #f2f5f9;
  --surface: #ffffff;
  --border: #e2e8f1;
  --text: #1c2b3a;
  --muted: #66788c;
  --danger: #b4232a;
  --warn-bg: #fdf3e0;
  --warn-text: #8a5a09;
  --radius: 14px;
  --shadow-sm: 0 1px 2px rgba(12, 35, 64, 0.06);
  --shadow-md: 0 6px 24px rgba(12, 35, 64, 0.1);
}

* { box-sizing: border-box; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  max-width: 880px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--navy), var(--navy-700));
  color: #7ce3dd;
  display: grid;
  place-items: center;
  flex: none;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-name { font-weight: 700; font-size: 16px; color: var(--navy); letter-spacing: 0.2px; }
.brand-sub { font-size: 11.5px; color: var(--muted); }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, border-color 0.15s ease, opacity 0.15s ease, transform 0.05s ease;
}

.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.btn.ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--navy);
}
.btn.ghost:hover:not(:disabled) { border-color: #c3cfdd; background: #f8fafc; }

.btn.primary {
  background: var(--navy);
  color: #fff;
}
.btn.primary:hover:not(:disabled) { background: var(--navy-700); }

.btn.small { padding: 6px 12px; font-size: 12.5px; border-radius: 8px; }

.btn.send {
  width: 40px;
  height: 40px;
  padding: 0;
  justify-content: center;
  border-radius: 12px;
  background: var(--navy);
  color: #fff;
  flex: none;
}
.btn.send:hover:not(:disabled) { background: var(--navy-700); }

/* ---------- Chat area ---------- */

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.chat-inner {
  max-width: 880px;
  margin: 0 auto;
  padding: 28px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Welcome */

.welcome {
  margin: auto 0;
  text-align: center;
  padding: 48px 8px 24px;
}

.welcome-mark {
  width: 58px;
  height: 58px;
  margin: 0 auto 18px;
  border-radius: 17px;
  background: linear-gradient(135deg, var(--navy), var(--navy-700));
  color: #7ce3dd;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-md);
}

.welcome h1 {
  font-size: 26px;
  margin: 0 0 6px;
  color: var(--navy);
}

.welcome p {
  margin: 0 0 22px;
  color: var(--muted);
  font-size: 15px;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 9px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--navy);
  border-radius: 999px;
  padding: 8px 15px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.chip:hover { border-color: var(--teal); background: var(--teal-soft); }
.chip:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

/* Messages */

.msg {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  line-height: 1.55;
  font-size: 14.5px;
}

.msg.user {
  align-self: flex-end;
  background: var(--navy);
  color: #eef4fb;
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.msg.ai {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  width: 100%;
  max-width: min(78%, 720px);
}

.msg-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 7px;
}

.msg-name { font-size: 12.5px; font-weight: 700; color: var(--navy); }

.lang-tag {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--teal);
  background: var(--teal-soft);
  padding: 2px 7px;
  border-radius: 999px;
}

.msg-body {
  white-space: normal;
  overflow-wrap: break-word;
}

.msg-body > :first-child { margin-top: 0; }
.msg-body > :last-child { margin-bottom: 0; }
.msg-body p { margin: 0 0 0.8em; }
.msg-body ul, .msg-body ol { margin: 0 0 0.8em; padding-left: 1.5em; }
.msg-body li + li { margin-top: 0.2em; }
.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4 { color: var(--navy); line-height: 1.3; margin: 1em 0 0.45em; }
.msg-body h1 { font-size: 1.35em; }
.msg-body h2 { font-size: 1.2em; }
.msg-body h3, .msg-body h4 { font-size: 1.08em; }
.msg-body blockquote { margin: 0 0 0.8em; padding: 0.15em 0 0.15em 0.9em; border-left: 3px solid var(--teal); color: var(--muted); }
.msg-body code { padding: 0.12em 0.35em; border-radius: 5px; background: #f1f5f9; color: #9b2c2c; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; }
.msg-body pre { margin: 0 0 0.8em; padding: 12px 14px; overflow-x: auto; border-radius: 9px; background: #10243a; color: #eef4fb; line-height: 1.5; }
.msg-body pre code { padding: 0; background: transparent; color: inherit; font-size: 0.88em; white-space: pre; }
.markdown-table-wrap { max-width: 100%; margin: 0 0 0.8em; overflow-x: auto; border: 1px solid var(--border); border-radius: 9px; }
.msg-body table { width: 100%; border-collapse: collapse; font-size: 0.94em; }
.msg-body th, .msg-body td { min-width: 100px; padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.msg-body th { background: #f4f7fb; color: var(--navy); font-weight: 700; }
.msg-body tr:last-child td { border-bottom: 0; }
.msg-body a { color: #087f7a; text-decoration: underline; text-underline-offset: 2px; }
.msg-body hr { border: 0; border-top: 1px solid var(--border); margin: 0.8em 0; }

.msg-tags { margin-top: 8px; }

.tag-ungrounded {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--warn-text);
  background: var(--warn-bg);
  border-radius: 8px;
  padding: 4px 9px;
}

/* Sources */

.sources {
  margin-top: 10px;
  padding-top: 9px;
  border-top: 1px dashed var(--border);
  display: none;
}
.sources.has-sources { display: block; }

.sources-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--muted);
  display: block;
  margin-bottom: 6px;
}

.source-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--navy);
  background: #f4f7fb;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 10px;
  margin: 0 6px 6px 0;
  max-width: 100%;
}
.source-chip .src-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 300px; }
.source-chip .src-ref { color: var(--muted); flex: none; }
.source-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--teal); flex: none; }

/* Error note inside chat */

.msg.ai.error-note { border-color: #f0d6d3; background: #fdf6f5; }
.error-note .msg-body { color: var(--danger); font-size: 13.5px; }

/* Typing indicator */

.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: #9db1c5;
  animation: typing 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Feedback */

.feedback {
  margin-top: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.fb-label { font-size: 12px; color: var(--muted); }

.fb-btn {
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.fb-btn:hover:not(:disabled) { border-color: #c3cfdd; color: var(--navy); }
.fb-btn:disabled { cursor: default; }
.fb-btn.selected[data-rating="helpful"] { color: #1a7f37; border-color: #b6e2c1; background: #edf9f0; }
.fb-btn.selected[data-rating="unhelpful"] { color: var(--danger); border-color: #efc4c1; background: #fdf1f0; }
.fb-btn:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.fb-note { font-size: 12px; color: var(--teal); font-weight: 600; }

.fb-comment {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #f8fafc;
}
.fb-comment-label { display: block; font-size: 12px; font-weight: 600; color: var(--navy); margin-bottom: 6px; }
.fb-comment .muted { color: var(--muted); font-weight: 400; }
.fb-comment textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  min-height: 56px;
}
.fb-comment textarea:focus { outline: 2px solid var(--teal); outline-offset: 1px; }
.fb-comment-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }

/* ---------- Composer ---------- */

.composer-wrap {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, var(--bg) 78%, rgba(242, 245, 249, 0));
  padding: 8px 0 14px;
}

.composer-inner { max-width: 880px; margin: 0 auto; padding: 0 16px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 10px 10px 10px 16px;
  box-shadow: var(--shadow-md);
  transition: border-color 0.15s ease;
}
.composer:focus-within { border-color: var(--teal); }

.composer textarea {
  flex: 1;
  border: none;
  resize: none;
  font-size: 14.5px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 180px;
  padding: 6px 0;
  background: transparent;
  color: var(--text);
}
.composer textarea:focus { outline: none; }
.composer textarea::placeholder { color: #93a5b8; }

.composer-meta {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  margin-top: 7px;
  padding: 0 4px;
}

.hint { font-size: 11.5px; color: var(--muted); }
.char-count { font-size: 11.5px; color: var(--warn-text); font-variant-numeric: tabular-nums; }
.char-count:empty { display: none; }

/* ---------- Responsive & misc ---------- */

@media (max-width: 640px) {
  .msg { max-width: 92%; }
  .msg.ai { max-width: 100%; }
  .brand-sub { display: none; }
  .composer-meta .hint { display: none; }
  .welcome h1 { font-size: 22px; }
  .source-chip .src-title { max-width: 180px; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-scroll { scroll-behavior: auto; }
  .typing span { animation: none; }
}
