:root {
  --red:        #e2231a;
  --red-bright: #ff4438;
  --red-deep:   #8d1009;
  --ink:        #1c1410;
  --paper:      #fffdfb;
  --muted:      #8a7f79;
  --line:       #efe7e2;
  --bot-bg:     #f8f4f1;
  --display: "Sora", "Inter", -apple-system, "Segoe UI", sans-serif;
  --body: "Inter", -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--body);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 28px 20px;
  background:
    radial-gradient(120% 120% at 22% 8%, var(--red-bright) 0%, var(--red) 38%, var(--red-deep) 100%);
  position: relative;
  overflow: hidden;
}

/* grain + soft vignette for atmosphere */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.4'/%3E%3C/svg%3E");
  opacity: 0.06;
  mix-blend-mode: overlay;
}
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(130% 90% at 50% 40%, transparent 55%, rgba(0,0,0,0.28) 100%);
}

/* ── Centered app shell: sidebar + chat ── */
.layout {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 1040px;
  height: min(86vh, 820px);
  display: flex;
  background: var(--paper);
  border-radius: 22px;
  border: 1px solid rgba(255,255,255,0.5);
  box-shadow: 0 2px 0 rgba(255,255,255,0.4) inset, 0 30px 80px -20px rgba(60,5,2,0.6);
  overflow: hidden;
  opacity: 0;
  transform: translateY(14px);
  animation: rise 0.7s cubic-bezier(.2,.7,.2,1) forwards;
}
.sidebar {
  width: 256px;
  flex: none;
  background: #faf6f4;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 10px;
}
#new-chat {
  font-family: var(--body);
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 600;
  color: var(--red);
  cursor: pointer;
}
#new-chat:hover { border-color: var(--red); }
#conversations { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.convo {
  text-align: left;
  border: 0;
  background: none;
  font-family: var(--body);
  font-size: 13px;
  color: var(--ink);
  padding: 9px 11px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo:hover { background: #f1e8e4; }
.convo.active { background: #fdeceb; color: var(--red); font-weight: 600; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }

@media (max-width: 760px) { .sidebar { display: none; } }

header {
  padding: 20px 26px 16px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, #fff, var(--paper));
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo { height: 24px; }
.divider { width: 1px; height: 20px; background: var(--line); }
.bot-name {
  font-family: var(--display);
  font-weight: 600;
  font-size: 19px;
  letter-spacing: -0.01em;
}
.bot-name .rai-accent { color: var(--red); }

/* ── Messages ── */
#messages { flex: 1; overflow-y: auto; padding: 22px 26px 8px; scroll-behavior: smooth; }
#messages::-webkit-scrollbar { width: 9px; }
#messages::-webkit-scrollbar-thumb { background: #e7ddd7; border-radius: 8px; border: 3px solid var(--paper); }

.msg { margin: 0 0 18px; animation: pop 0.4s ease both; }

.msg.user {
  margin-left: auto;
  max-width: 78%;
  background: linear-gradient(180deg, var(--red-bright), var(--red));
  color: #fff;
  padding: 11px 15px;
  border-radius: 16px 16px 4px 16px;
  line-height: 1.5;
  box-shadow: 0 6px 16px -6px rgba(226,35,26,0.6);
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.bot { max-width: 94%; }
.msg.bot .prose {
  background: var(--bot-bg);
  border: 1px solid var(--line);
  padding: 4px 18px;
  border-radius: 16px 16px 16px 4px;
}
.msg.bot.decline .prose { background: #fff3f2; border-color: #f4c7c2; }

/* badges for special states */
.badge {
  display: inline-block;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 3px 9px;
  border-radius: 999px;
  margin: 12px 0 2px;
}
.badge.decline { background: var(--red); color: #fff; }
.badge.nosrc { background: #f0e6df; color: #8a6d2f; }

/* ── Markdown prose ── */
.prose { font-size: 15px; line-height: 1.62; color: var(--ink); }
.prose > :first-child { margin-top: 12px; }
.prose > :last-child { margin-bottom: 14px; }
.prose h1, .prose h2, .prose h3 {
  font-family: var(--display);
  font-weight: 600;
  line-height: 1.25;
  letter-spacing: -0.01em;
  margin: 20px 0 8px;
}
.prose h1 { font-size: 21px; }
.prose h2 { font-size: 18px; }
.prose h3 { font-size: 16px; }
.prose p { margin: 10px 0; }
.prose ul, .prose ol { margin: 10px 0; padding-left: 22px; }
.prose li { margin: 5px 0; }
.prose li::marker { color: var(--red); }
.prose strong { font-weight: 700; }
.prose a { color: var(--red); text-decoration: underline; text-underline-offset: 2px; }
.prose hr { border: none; border-top: 1px solid var(--line); margin: 16px 0; }
.prose code {
  font-family: "SFMono-Regular", ui-monospace, Menlo, Consolas, monospace;
  font-size: 12.8px;
  background: #f0e8e3;
  padding: 1.5px 6px;
  border-radius: 5px;
}
.prose pre {
  background: #241a16;
  color: #f3ece8;
  padding: 14px 16px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 12.8px;
  line-height: 1.5;
}
.prose pre code { background: none; padding: 0; color: inherit; }
.prose blockquote {
  margin: 12px 0;
  padding: 4px 14px;
  border-left: 3px solid var(--red);
  color: var(--muted);
  font-style: italic;
}
.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 14px 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.prose th, .prose td { padding: 8px 12px; border: 1px solid var(--line); text-align: left; }
.prose th { background: #fbeceb; color: var(--red-deep); font-weight: 700; }
.prose tr:nth-child(even) td { background: #faf6f3; }

/* sources footnote */
.sources {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed var(--line);
  font-size: 11.5px;
  color: var(--muted);
}
.sources .label {
  display: block;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
  color: var(--red);
  margin-bottom: 4px;
  font-size: 10px;
}
.sources code { font-size: 11px; background: #f3ece8; padding: 1px 5px; border-radius: 4px; }

/* rating row */
.rate { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 12.5px; color: var(--muted); }
.rate-q { margin-right: 2px; }
.thumb {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 9px;
  padding: 3px 10px;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.thumb:hover { border-color: var(--red); transform: translateY(-1px); }
.rate-comment {
  flex: 1;
  max-width: 340px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  font-family: var(--body);
  font-size: 13px;
}
.rate-comment:focus { outline: none; border-color: var(--red); }
.rate-send {
  border: 0;
  background: var(--red);
  color: #fff;
  border-radius: 9px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
}
.rate-send:hover { background: var(--red-deep); }
.rate-done { color: #1faa59; font-weight: 600; }
.rate-fail { color: var(--red); }

/* typing indicator */
.typing { display: inline-flex; gap: 5px; padding: 14px 18px; background: var(--bot-bg); border: 1px solid var(--line); border-radius: 16px 16px 16px 4px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--red); opacity: 0.4; animation: blink 1.3s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

/* ── Agent steps / loader timeline ── */
.steps { margin: 0 0 8px; }
.steps-toggle {
  border: 0;
  background: none;
  font-family: var(--body);
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 0 6px;
}
.steps-toggle:hover { color: var(--red); }
.steps-list {
  list-style: none;
  margin: 0;
  padding: 9px 14px;
  background: var(--bot-bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.step {
  display: grid;
  grid-template-columns: 16px 1fr;
  column-gap: 9px;
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.45;
}
.step .ico {
  grid-row: 1 / span 2;
  width: 16px; height: 16px;
  display: grid; place-items: center;
  font-size: 12px;
  color: var(--muted);
}
.step.done .ico { color: #1faa59; }
.step .ico.spin { color: var(--red); animation: spin 0.9s linear infinite; }
.step-label { font-weight: 600; }
.step.active .step-label { color: var(--ink); }
.step-note {
  grid-column: 2;
  font-size: 11.5px;
  font-style: italic;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-word;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Suggestions ── */
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 8px; }
.chip {
  font-family: var(--body);
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 15px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.18s ease;
}
.chip:hover { border-color: var(--red); color: var(--red); transform: translateY(-1px); }

/* ── Composer ── */
#composer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px 16px;
  border-top: 1px solid var(--line);
}
#question {
  flex: 1;
  font-family: var(--body);
  padding: 13px 18px;
  border: 1.5px solid var(--line);
  border-radius: 999px;
  font-size: 15px;
  background: #fff;
  transition: border-color 0.18s ease;
}
#question:focus { outline: none; border-color: var(--red); }
#send {
  flex: none;
  width: 46px; height: 46px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--red);
  color: #fff;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.18s ease;
}
#send:hover { background: var(--red-deep); transform: scale(1.06); }
#send:disabled { opacity: 0.45; cursor: default; transform: none; }

footer {
  position: relative;
  z-index: 1;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.85);
}

/* staggered entrance for inner blocks */
header, #messages, #composer { opacity: 0; animation: fade 0.6s ease forwards; }
header { animation-delay: 0.15s; }
#messages { animation-delay: 0.28s; }
#composer { animation-delay: 0.4s; }

@keyframes rise { to { opacity: 1; transform: none; } }
@keyframes fade { to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes blink { 0%, 60%, 100% { opacity: 0.35; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

@media (max-width: 560px) {
  body { padding: 0; }
  .layout { height: 100vh; max-width: none; border-radius: 0; }
  footer { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
  .layout { opacity: 1; transform: none; }
  header, #messages, #composer { opacity: 1; }
}
