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

:root {
  --saffron: #e67e22;
  --saffron-dark: #d4741e;
  --saffron-light: #f5cba7;
  --saffron-bg: rgba(230, 126, 34, 0.08);
  --deep-red: #c0392b;
  --gold: #d4a017;
  --cream: #fef9ef;
  --cream-dark: #f0e4d0;
  --brown: #3e2723;
  --brown-light: #5d4037;
  --brown-muted: #8d6e63;
  --gray: #9e9e9e;
  --gray-light: #e0e0e0;
  --gray-bg: #f5f5f5;
  --white: #ffffff;
  --green: #27ae60;
  --amber: #f39c12;
  --red: #e74c3c;
  --shadow-sm: 0 1px 4px rgba(62, 39, 35, 0.06);
  --shadow: 0 2px 12px rgba(62, 39, 35, 0.08);
  --shadow-lg: 0 8px 30px rgba(62, 39, 35, 0.12);
  --radius: 16px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --max-width: 960px;
}

html, body {
  height: 100%;
}

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--cream);
  color: var(--brown);
  line-height: 1.6;
  display: flex;
  justify-content: center;
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--max-width);
  height: 100vh;
  padding: 0 1.2em;
  background: var(--white);
  box-shadow: var(--shadow-lg);
}

/* ─── Header ─── */
header {
  padding: 0.6em 0;
  border-bottom: 1px solid var(--cream-dark);
  flex-shrink: 0;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75em;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75em;
}

.logo {
  height: 36px;
  width: auto;
}

.header-text h1 {
  font-size: 1.1em;
  font-weight: 600;
  color: var(--deep-red);
  letter-spacing: -0.01em;
}

.header-tagline {
  font-size: 0.75em;
  color: var(--gray);
  font-style: italic;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.6em;
}

/* ─── Maharshi Banner ─── */
.maharshi-banner {
  display: flex;
  align-items: center;
  gap: 0.75em;
  margin: 0.5em 0.5em 0;
  padding: 0.7em 1em;
  background: linear-gradient(135deg, #2d1810, #4a2c1a);
  border: 1px solid rgba(212, 160, 23, 0.2);
  border-radius: 12px;
  text-decoration: none;
  color: var(--cream);
  transition: all 0.25s;
  flex-shrink: 0;
}

.maharshi-banner:hover {
  background: linear-gradient(135deg, #3e2723, #5d4037);
  border-color: var(--saffron);
  transform: translateY(-1px);
  box-shadow: 0 4px 20px rgba(230, 126, 34, 0.15);
}

.maharshi-icon {
  font-size: 1.8em;
  line-height: 1;
  flex-shrink: 0;
}

.maharshi-banner-text {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.maharshi-banner-text strong {
  font-size: 0.95em;
  font-weight: 600;
  color: var(--gold);
}

.maharshi-banner-text span {
  font-size: 0.75em;
  color: rgba(255,255,255,0.55);
}

.maharshi-arrow {
  font-size: 1.2em;
  color: var(--gold);
  transition: transform 0.2s;
  flex-shrink: 0;
}

.maharshi-banner:hover .maharshi-arrow {
  transform: translateX(4px);
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--cream-dark);
  background: var(--white);
  color: var(--brown-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.icon-btn:hover { background: var(--gray-bg); color: var(--deep-red); }
.icon-btn:disabled { opacity: 0.3; cursor: not-allowed; }

/* ─── Status ─── */
.status {
  display: flex;
  align-items: center;
  gap: 0.35em;
  font-size: 0.72em;
  color: var(--gray);
  white-space: nowrap;
  padding: 0.3em 0.6em;
  border-radius: 50px;
  background: var(--gray-bg);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--gray);
  transition: background 0.3s;
}

.status-dot.online { background: var(--green); box-shadow: 0 0 5px rgba(39, 174, 96, 0.35); }
.status-dot.connecting { background: var(--amber); animation: pulse 1s infinite; }
.status-dot.offline { background: var(--red); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ─── Layout ─── */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.8em 0.5em 0.5em;
  scroll-behavior: smooth;
}

#messages::-webkit-scrollbar { width: 5px; }
#messages::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb { background: var(--cream-dark); border-radius: 3px; }

/* ─── Welcome ─── */
.welcome {
  text-align: center;
  padding: 2.5em 1em;
}

.welcome-icon { font-size: 3.5em; margin-bottom: 0.4em; opacity: 0.45; }

.welcome h2 {
  font-size: 1.25em;
  font-weight: 500;
  color: var(--brown-light);
  margin-bottom: 0.3em;
}

.welcome p { font-size: 0.9em; color: var(--gray); margin-bottom: 1.5em; }

.suggestions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5em;
}

.chip {
  background: var(--white);
  border: 1px solid var(--cream-dark);
  padding: 0.5em 1em;
  border-radius: 50px;
  font-family: inherit;
  font-size: 0.8em;
  color: var(--brown-light);
  cursor: pointer;
  transition: all 0.2s;
}

.chip:hover {
  background: var(--saffron-bg);
  border-color: var(--saffron);
  color: var(--brown);
}

/* ─── Messages ─── */
.msg {
  display: flex;
  gap: 0.65em;
  margin-bottom: 1em;
  animation: msgIn 0.3s ease-out;
  position: relative;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 0.8em;
  margin-top: 2px;
}

.msg.bot .msg-avatar { background: var(--saffron); font-size: 0.85em; }
.msg.user .msg-avatar { background: var(--deep-red); color: var(--white); font-weight: 600; font-size: 0.78em; }

.msg-bubble {
  max-width: 80%;
  padding: 0.65em 1em;
  border-radius: var(--radius);
  font-size: 0.9em;
  line-height: 1.7;
  position: relative;
}

.msg.bot .msg-bubble {
  background: var(--cream);
  border: 1px solid var(--cream-dark);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.msg.user .msg-bubble {
  background: var(--saffron);
  color: var(--white);
  border-bottom-right-radius: 4px;
}

/* ─── Bubble footer (timestamp + actions) ─── */
.bubble-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4em;
  margin-top: 0.3em;
  font-size: 0.72em;
  opacity: 0;
  transition: opacity 0.2s;
}

.msg:hover .bubble-footer { opacity: 1; }

.msg.user .bubble-footer { justify-content: flex-start; }

.bubble-time {
  color: var(--gray);
}

.msg.user .bubble-time { color: rgba(255,255,255,0.7); }

.bubble-action {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--gray);
  padding: 2px;
  display: flex;
  align-items: center;
  border-radius: 4px;
  transition: all 0.15s;
}

.bubble-action:hover { color: var(--saffron-dark); background: rgba(0,0,0,0.04); }

.msg.user .bubble-action { color: rgba(255,255,255,0.7); }
.msg.user .bubble-action:hover { color: var(--white); background: rgba(255,255,255,0.12); }

/* ─── Markdown ─── */
.msg-bubble p { margin: 0.3em 0; }
.msg-bubble p:first-child { margin-top: 0; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble strong { font-weight: 600; }
.msg.user .msg-bubble strong { color: var(--white); }

.msg-bubble code {
  font-family: "JetBrains Mono", "Consolas", monospace;
  font-size: 0.88em;
  background: rgba(230, 126, 34, 0.08);
  padding: 0.08em 0.35em;
  border-radius: 4px;
}

.msg.user .msg-bubble code { background: rgba(255,255,255,0.15); }

.msg-bubble pre {
  background: var(--cream);
  border: 1px solid var(--cream-dark);
  border-radius: var(--radius-sm);
  padding: 0.8em 1em;
  overflow-x: auto;
  margin: 0.5em 0;
}

.msg-bubble pre code { background: none; padding: 0; }

.msg-bubble a { color: var(--saffron-dark); text-decoration: underline; text-underline-offset: 2px; }
.msg.user .msg-bubble a { color: rgba(255,255,255,0.9); }

.msg-bubble ul, .msg-bubble ol { padding-left: 1.3em; margin: 0.3em 0; }
.msg-bubble li { margin: 0.15em 0; }

/* ─── Sources ─── */
.sources-toggle {
  margin-top: 0.4em;
  font-size: 0.82em;
}

.sources-toggle summary {
  cursor: pointer;
  color: var(--brown-muted);
  user-select: none;
  font-size: 0.9em;
}

.sources-toggle summary:hover { color: var(--saffron-dark); }

.sources-list { list-style: none; padding: 0.3em 0 0 0; }
.sources-list li { margin: 0.15em 0; }
.sources-list a { color: var(--saffron-dark); font-size: 0.9em; text-decoration: none; }
.sources-list a:hover { text-decoration: underline; }

/* ─── Typing ─── */
.typing-indicator {
  display: flex;
  gap: 0.65em;
  margin-bottom: 1em;
  animation: msgIn 0.2s ease-out;
}

.typing-dots {
  background: var(--cream);
  border: 1px solid var(--cream-dark);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  padding: 0.7em 1.2em;
  display: flex;
  gap: 5px;
  align-items: center;
  box-shadow: var(--shadow-sm);
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--saffron);
  animation: dotBounce 1.4s infinite;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.3; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ─── Input ─── */
.input-area {
  flex-shrink: 0;
  padding: 0.6em 0 0.8em;
  border-top: 1px solid var(--cream-dark);
}

.input-row {
  display: flex;
  gap: 0.5em;
  background: var(--cream);
  border: 2px solid var(--cream-dark);
  border-radius: 50px;
  padding: 0.3em;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-row:focus-within {
  border-color: var(--saffron);
  box-shadow: 0 0 0 3px rgba(230, 126, 34, 0.1);
}

.input-row.disabled { opacity: 0.5; }

#input {
  flex: 1;
  border: none;
  outline: none;
  padding: 0.5em 1em;
  font-family: inherit;
  font-size: 0.9em;
  background: transparent;
  color: var(--brown);
}

#input::placeholder { color: var(--brown-muted); }

#send {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--saffron);
  color: var(--white);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s;
  flex-shrink: 0;
}

#send:hover { background: var(--saffron-dark); }
#send:active { transform: scale(0.92); }
#send:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.input-hint {
  font-size: 0.7em;
  color: var(--gray-light);
  text-align: right;
  margin-top: 4px;
  padding-right: 4px;
}

/* ─── Error ─── */
.msg.error .msg-bubble {
  background: #fff5f5;
  border-color: #f5c6cb;
  color: var(--deep-red);
}
.msg.error .msg-avatar { background: var(--red); }

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--brown);
  color: var(--white);
  padding: 0.5em 1.2em;
  border-radius: 8px;
  font-size: 0.82em;
  opacity: 0;
  transition: all 0.3s;
  z-index: 100;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ─── Connecting overlay ─── */
.connecting-overlay {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 1em;
  padding: 2em;
}

.connecting-overlay .spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--cream-dark);
  border-top-color: var(--saffron);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.connecting-overlay p { color: var(--gray); font-size: 0.9em; }

/* ─── Desktop enhancements ─── */
@media (min-width: 769px) {
  #app {
    padding: 0 2em;
    border-left: 1px solid var(--cream-dark);
    border-right: 1px solid var(--cream-dark);
  }

  body {
    background: #f8f4ec;
  }

  .msg-bubble { max-width: 75%; }
  .msg-bubble { font-size: 0.92em; }

  .bubble-footer { opacity: 1; }

  .welcome { padding: 3.5em 2em; }
  .welcome h2 { font-size: 1.4em; }
}

@media (min-width: 1200px) {
  #app { max-width: 1000px; }
  .msg-bubble { max-width: 70%; }
}

/* ─── Mobile ─── */
@media (max-width: 600px) {
  #app { padding: 0 0.6em; }
  .msg-bubble { max-width: 88%; font-size: 0.85em; }
  .welcome { padding: 1.5em 0.5em; }
  .welcome h2 { font-size: 1em; }
  .chip { font-size: 0.75em; padding: 0.4em 0.7em; }
  .logo { height: 30px; }
  .header-text h1 { font-size: 1em; }
  .status { font-size: 0.65em; padding: 0.2em 0.5em; }
  .bubble-footer { opacity: 1; }
  .input-hint { display: none; }
}
