/* NITI Aayog Sahayak — Internal Staff Assistant
   Government-portal aesthetic: Ashoka Chakra navy + saffron accent.
   Fully responsive, light/dark theme aware. */

/* ===================== THEME TOKENS ===================== */
:root {
  --navy-900: #0b2452;
  --navy-800: #12305f;
  --navy-700: #1a3a6b;
  --navy-600: #26497f;
  --saffron: #f0a500;
  --saffron-dark: #c98600;
  --saffron-light: #ffc94d;

  --bg: #eef1f7;
  --surface: #ffffff;
  --surface-2: #f6f8fc;
  --border: #dde3ee;
  --text: #16213a;
  --text-muted: #5b6577;
  --text-faint: #8993a8;

  --user-bubble-bg: linear-gradient(135deg, var(--navy-700), var(--navy-900));
  --user-bubble-text: #f5f8ff;
  --assistant-bubble-bg: var(--surface);
  --assistant-bubble-border: var(--border);

  --danger: #b3261e;
  --danger-bg: #fdecea;
  --success: #1e7a46;

  --shadow-sm: 0 1px 2px rgba(11, 36, 82, 0.06), 0 1px 1px rgba(11, 36, 82, 0.04);
  --shadow-md: 0 4px 14px rgba(11, 36, 82, 0.10);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a1220;
    --surface: #121b2e;
    --surface-2: #17223a;
    --border: #253355;
    --text: #e7ecf7;
    --text-muted: #a6b0c6;
    --text-faint: #6d7893;

    --navy-700: #2c4c86;
    --navy-900: #0d1b38;
    --user-bubble-bg: linear-gradient(135deg, #2c4c86, #16294f);
    --user-bubble-text: #f5f8ff;
    --assistant-bubble-bg: #121b2e;
    --assistant-bubble-border: #253355;

    --danger-bg: #3a1a19;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0a1220;
  --surface: #121b2e;
  --surface-2: #17223a;
  --border: #253355;
  --text: #e7ecf7;
  --text-muted: #a6b0c6;
  --text-faint: #6d7893;
  --navy-700: #2c4c86;
  --navy-900: #0d1b38;
  --user-bubble-bg: linear-gradient(135deg, #2c4c86, #16294f);
  --user-bubble-text: #f5f8ff;
  --assistant-bubble-bg: #121b2e;
  --assistant-bubble-border: #253355;
  --danger-bg: #3a1a19;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #eef1f7;
  --surface: #ffffff;
  --surface-2: #f6f8fc;
  --border: #dde3ee;
  --text: #16213a;
  --text-muted: #5b6577;
  --text-faint: #8993a8;
  --navy-700: #1a3a6b;
  --navy-900: #0b2452;
  --user-bubble-bg: linear-gradient(135deg, var(--navy-700), var(--navy-900));
  --user-bubble-text: #f5f8ff;
  --assistant-bubble-bg: #ffffff;
  --assistant-bubble-border: #dde3ee;
  --danger-bg: #fdecea;
  --shadow-sm: 0 1px 2px rgba(11, 36, 82, 0.06), 0 1px 1px rgba(11, 36, 82, 0.04);
  --shadow-md: 0 4px 14px rgba(11, 36, 82, 0.10);
  color-scheme: light;
}

/* ===================== RESET / BASE ===================== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Noto Sans", "Noto Sans Devanagari", "Segoe UI", system-ui, -apple-system,
    "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
button { font-family: inherit; }
textarea, input, select { font-family: inherit; }
::selection { background: var(--saffron); color: #1a1200; }

button {
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}
button:disabled { cursor: not-allowed; opacity: 0.55; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

/* ===================== APP SHELL ===================== */
.app-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===================== HEADER ===================== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: linear-gradient(120deg, var(--navy-900), var(--navy-700));
  color: #f5f8ff;
  flex: 0 0 auto;
  z-index: 20;
}
.header-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.header-right { display: flex; align-items: center; gap: 14px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  color: #f5f8ff;
  transition: background 0.15s ease;
  flex: 0 0 auto;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.12); }
.only-mobile { display: none; }

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.chakra-logo { flex: 0 0 auto; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25)); }
.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text .subtitle {
  margin: 0;
  font-size: 0.72rem;
  color: rgba(255, 255, 255, 0.72);
  white-space: nowrap;
}

.disclaimer {
  font-size: 0.72rem;
  color: #1a1200;
  background: var(--saffron);
  padding: 4px 10px;
  border-radius: 999px;
  font-weight: 600;
  white-space: nowrap;
}

.saffron-strip {
  height: 3px;
  flex: 0 0 auto;
  background: linear-gradient(90deg, var(--saffron-dark), var(--saffron), var(--saffron-light), var(--saffron), var(--saffron-dark));
}

/* ===================== BODY LAYOUT ===================== */
.app-body {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
  position: relative;
}

/* ===================== SIDEBAR ===================== */
.sidebar {
  width: 280px;
  flex: 0 0 auto;
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 10px;
  overflow: hidden;
  transition: transform 0.22s ease;
  z-index: 15;
}
.sidebar-scrim {
  display: none;
}

.new-chat-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--navy-700);
  color: #fff;
  font-weight: 600;
  font-size: 0.88rem;
  box-shadow: var(--shadow-sm);
  transition: background 0.15s ease, transform 0.05s ease;
}
.new-chat-btn:hover { background: var(--navy-900); }
.new-chat-btn:active { transform: scale(0.98); }

.sidebar-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  font-weight: 700;
  padding: 4px 6px 0;
}

.conversation-list {
  flex: 1 1 auto;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 2px;
}
.sidebar-empty {
  color: var(--text-faint);
  font-size: 0.82rem;
  padding: 14px 8px;
  text-align: center;
}

.conversation-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  text-align: left;
  width: 100%;
  color: var(--text);
  transition: background 0.12s ease;
  border: 1px solid transparent;
}
.conversation-item:hover { background: var(--surface); }
.conversation-item.active {
  background: var(--surface);
  border-color: var(--border);
  box-shadow: var(--shadow-sm);
}
.conversation-item .conv-title {
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conversation-item .conv-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.7rem;
  color: var(--text-faint);
}
.conv-mode-tag {
  background: var(--navy-700);
  color: #fff;
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* ===================== MAIN PANEL ===================== */
.main-panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

/* ---- mode bar ---- */
.mode-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}

.segmented-control {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
  overflow-x: auto;
  max-width: 100%;
}
.mode-btn {
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active {
  background: var(--saffron);
  color: #1a1200;
  box-shadow: var(--shadow-sm);
}

.reasoning-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 600;
}
.reasoning-control select {
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
}

/* ---- mode-specific fields ---- */
.mode-inputs:empty { display: none; }
.mode-field {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.mode-field label {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
}
.mode-field input,
.mode-field select {
  flex: 1 1 260px;
  min-width: 180px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
}
.mode-field input:focus,
.mode-field select:focus {
  outline: 2px solid var(--saffron);
  outline-offset: 1px;
}

/* ===================== MESSAGES ===================== */
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.empty-state {
  margin: auto;
  max-width: 420px;
  text-align: center;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 30px;
}
.empty-state h2 { margin: 0; color: var(--text); font-size: 1.2rem; }
.empty-state p { margin: 0; font-size: 0.9rem; line-height: 1.5; }
.empty-chakra { opacity: 0.85; }

.msg { display: flex; gap: 10px; max-width: 78%; }
.msg-user { align-self: flex-end; flex-direction: row-reverse; max-width: 72%; }
.msg-assistant { align-self: flex-start; }

.msg-avatar {
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2px;
}

.msg-col { display: flex; flex-direction: column; gap: 6px; min-width: 0; }

.msg-bubble {
  padding: 11px 15px;
  border-radius: var(--radius-lg);
  font-size: 0.92rem;
  line-height: 1.55;
  word-wrap: break-word;
  white-space: normal;
}
.msg-user .msg-bubble {
  background: var(--user-bubble-bg);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 4px;
}
.msg-assistant .msg-bubble {
  background: var(--assistant-bubble-bg);
  border: 1px solid var(--assistant-bubble-border);
  color: var(--text);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.msg-content p { margin: 0 0 0.7em; }
.msg-content p:last-child { margin-bottom: 0; }
.msg-content ul, .msg-content ol { margin: 0.3em 0 0.7em; padding-left: 1.3em; }
.msg-content li { margin-bottom: 0.25em; }
.msg-content strong { font-weight: 700; }
.msg-content:empty { display: none; }

/* typing indicator */
.typing-indicator {
  display: none;
  align-items: center;
  gap: 4px;
  padding: 2px 4px;
}
.msg-bubble.is-waiting .typing-indicator { display: inline-flex; }
.msg-bubble.is-waiting .msg-content { display: none; }
.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: typingBounce 1.1s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* reasoning panel */
.reasoning-panel { display: flex; flex-direction: column; gap: 4px; }
.reasoning-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  align-self: flex-start;
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
}
.reasoning-toggle:hover { color: var(--text); }
.reasoning-toggle .chevron { transition: transform 0.15s ease; }
.reasoning-panel.expanded .reasoning-toggle .chevron { transform: rotate(90deg); }
.reasoning-body {
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  white-space: pre-wrap;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  max-height: 260px;
  overflow-y: auto;
}
.reasoning-body.collapsed { display: none; }

/* message footer */
.msg-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.72rem;
  color: var(--text-faint);
}
.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
}
.copy-btn:hover { color: var(--text); border-color: var(--text-faint); }
.copy-btn.copied { color: var(--success); border-color: var(--success); }
.msg-meta { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-meta .fallback-note { color: var(--saffron-dark); font-weight: 700; }

/* error message bubble variant */
.msg-error .msg-bubble {
  background: var(--danger-bg);
  border-color: var(--danger);
  color: var(--danger);
}

/* ===================== COMPOSER ===================== */
.composer {
  flex: 0 0 auto;
  padding: 12px 20px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.model-badge {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--navy-700);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
}
.model-badge::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--saffron);
  flex: 0 0 auto;
}

.error-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 0.82rem;
}
.retry-btn {
  font-weight: 700;
  text-decoration: underline;
  color: var(--danger);
  flex: 0 0 auto;
}

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

#composerInput {
  flex: 1 1 auto;
  resize: none;
  max-height: 200px;
  min-height: 44px;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.92rem;
  line-height: 1.4;
}
#composerInput:focus {
  outline: none;
  border-color: var(--navy-700);
  box-shadow: 0 0 0 3px rgba(26, 58, 107, 0.15);
}
:root[data-theme="dark"] #composerInput:focus {
  box-shadow: 0 0 0 3px rgba(240, 165, 0, 0.18);
}
@media (prefers-color-scheme: dark) {
  #composerInput:focus { box-shadow: 0 0 0 3px rgba(240, 165, 0, 0.18); }
}

.send-btn, .stop-btn {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 11px 18px;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 0.88rem;
  height: 44px;
}
.send-btn {
  background: linear-gradient(135deg, var(--saffron), var(--saffron-dark));
  color: #1a1200;
  box-shadow: var(--shadow-sm);
}
.send-btn:hover:not(:disabled) { filter: brightness(1.05); }
.send-btn:active:not(:disabled) { transform: scale(0.98); }
.stop-btn {
  background: var(--danger);
  color: #fff;
}
.stop-btn:hover { filter: brightness(1.1); }

.composer-hint {
  font-size: 0.7rem;
  color: var(--text-faint);
  text-align: center;
}

/* ===================== RESPONSIVE ===================== */
@media (max-width: 880px) {
  .only-mobile { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    padding-top: 64px;
    transform: translateX(-100%);
    box-shadow: var(--shadow-md);
  }
  .app-body.sidebar-open .sidebar { transform: translateX(0); }
  .app-body.sidebar-open .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(4, 10, 24, 0.45);
    z-index: 14;
  }
  .msg { max-width: 92%; }
  .msg-user { max-width: 88%; }
  .brand-text .subtitle { display: none; }
}

@media (max-width: 560px) {
  .disclaimer { display: none; }
  .app-header { padding: 8px 12px; }
  .mode-bar { padding: 10px 12px; }
  .mode-field { padding: 8px 12px; }
  .messages { padding: 14px; }
  .composer { padding: 10px 12px 12px; }
  .send-btn span, .stop-btn span { display: none; }
  .send-btn, .stop-btn { padding: 11px 13px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ---- Policy Q&A grounding sources ---- */
.sources-panel {
  margin: 0.5rem 0 0.25rem;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border, #d8dee9);
  border-left: 3px solid var(--saffron, #f0a500);
  border-radius: 8px;
  background: color-mix(in srgb, var(--saffron, #f0a500) 6%, transparent);
  font-size: 0.82rem;
}
.sources-head { font-weight: 600; opacity: 0.85; margin-bottom: 0.3rem; }
.sources-list { margin: 0; padding-left: 1.1rem; display: grid; gap: 0.2rem; }
.sources-list li { line-height: 1.35; }
.sources-list .src-n { font-weight: 700; color: var(--navy-700, #0b2452); }
.sources-list a { color: var(--navy-700, #1a3a6b); }
@media (prefers-color-scheme: dark) {
  .sources-list .src-n, .sources-list a { color: var(--saffron, #f0a500); }
}

/* ---- Cross-page top nav ---- */
.top-nav { display: inline-flex; gap: 4px; margin-right: 10px; }
.top-nav a { text-decoration: none; font-size: 0.8rem; padding: 4px 10px; border-radius: 999px;
  color: var(--navy-700, #1a3a6b); border: 1px solid var(--border, #d8dee9); }
.top-nav a.active { background: var(--navy-700, #0b2452); color: #fff; border-color: transparent; }
@media (prefers-color-scheme: dark) { .top-nav a { color: var(--saffron, #f0a500); } }
