/* chat.css — SBR Chat Assistant UI
 *
 * Shared by:
 *   - The floating drawer (mounted on every cockpit page via chat.js)
 *   - The dedicated SBR-BOT page at /sbr-bot.html
 *
 * Inherits brand colours / spacing from styles.css.
 */

:root {
  --chat-bubble-size: 56px;
  --chat-drawer-width: 420px;
  --chat-pane-bg: #ffffff;
  --chat-sidebar-bg: #f8f9fb;
  --chat-border: #e1e5ec;
  --chat-ink: #1a1f2c;
  --chat-ink-soft: #687180;
  --chat-user-bubble: #2d4a82;
  --chat-user-text: #ffffff;
  --chat-assistant-bubble: #f0f2f6;
  --chat-assistant-text: #1a1f2c;
  --chat-status-pending: #f0f1f4;
  --chat-status-pending-ink: #4b5563;
  --chat-status-running: #e3effc;
  --chat-status-running-ink: #0a6cdf;
  --chat-status-done: #e1f3e9;
  --chat-status-done-ink: #197447;
  --chat-status-failed: #fbe7e0;
  --chat-status-failed-ink: #c5350a;
}

/* ===================================================================
 * Floating bubble (cross-page)
 * =================================================================== */

.sbr-chat-bubble {
  position: fixed;
  bottom: 1.2rem;
  right: 1.2rem;
  width: var(--chat-bubble-size);
  height: var(--chat-bubble-size);
  border-radius: 50%;
  background: var(--chat-user-bubble);
  color: white;
  font-size: 1.6rem;
  border: none;
  box-shadow: 0 4px 14px rgba(15, 20, 40, 0.18);
  cursor: pointer;
  z-index: 9000;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.sbr-chat-bubble:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(15, 20, 40, 0.25);
}
.sbr-chat-bubble[hidden] { display: none; }

.sbr-chat-bubble .notif-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ff5a4a;
  border: 2px solid white;
  display: none;
}
.sbr-chat-bubble.has-update .notif-dot { display: block; }

/* ===================================================================
 * Drawer (right slide-in panel; floats above pages)
 * =================================================================== */

.sbr-chat-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--chat-drawer-width);
  max-width: 100vw;
  background: var(--chat-pane-bg);
  border-left: 1px solid var(--chat-border);
  box-shadow: -8px 0 22px rgba(15, 20, 40, 0.10);
  display: flex;
  flex-direction: column;
  z-index: 8500;
  transform: translateX(0);
  transition: transform 0.18s ease;
  font-family: 'Inter Tight', system-ui, sans-serif;
}
.sbr-chat-drawer.collapsed {
  transform: translateX(110%);
  pointer-events: none;
}

.sbr-chat-drawer header.chat-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
}
.sbr-chat-drawer header.chat-header h2 {
  margin: 0;
  font-family: 'Fraunces', serif;
  font-size: 1.1rem;
  font-weight: 600;
  flex: 1;
}
.sbr-chat-drawer header.chat-header button {
  background: none;
  border: 1px solid var(--chat-border);
  border-radius: 4px;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--chat-ink);
}
.sbr-chat-drawer header.chat-header button.close {
  border: none;
  font-size: 1.4rem;
  padding: 0 0.5rem;
  color: var(--chat-ink-soft);
}

.sbr-chat-drawer .drawer-thread-list {
  max-height: 140px;
  overflow-y: auto;
  border-bottom: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
}

/* ===================================================================
 * SBR-BOT full page (two-pane layout)
 * =================================================================== */

main.sbr-bot-page {
  display: flex;
  height: calc(100vh - var(--brandbar-height, 56px));
  font-family: 'Inter Tight', system-ui, sans-serif;
}
main.sbr-bot-page > .sidebar {
  width: 280px;        /* default; resizer overrides via inline style */
  flex-shrink: 0;
  position: relative;  /* anchor for the resize handle */
  border-right: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
main.sbr-bot-page > .conversation {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

main.sbr-bot-page .sidebar-header {
  padding: 0.75rem 0.9rem;
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}
main.sbr-bot-page .sidebar-header button#new-thread-btn {
  padding: 0.5rem 0.75rem;
  background: var(--chat-user-bubble);
  color: white;
  border: none;
  border-radius: 4px;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.85rem;
}
main.sbr-bot-page .sidebar-header button#new-thread-btn:hover { filter: brightness(1.1); }

main.sbr-bot-page .filter-tabs {
  display: flex;
  gap: 0.25rem;
}
main.sbr-bot-page .filter-tabs button,
.sbr-chat-drawer .filter-tabs button {
  flex: 1;
  padding: 0.3rem 0.5rem;
  background: white;
  border: 1px solid var(--chat-border);
  border-radius: 4px;
  font-size: 0.78rem;
  cursor: pointer;
  color: var(--chat-ink-soft);
}
main.sbr-bot-page .filter-tabs button.active,
.sbr-chat-drawer .filter-tabs button.active {
  background: var(--chat-ink);
  color: white;
  border-color: var(--chat-ink);
}

/* ===================================================================
 * Thread list (used by both drawer and page sidebar)
 * =================================================================== */

ul.thread-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
ul.thread-list li {
  padding: 0.55rem 0.9rem;
  border-bottom: 1px solid var(--chat-border);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}
ul.thread-list li:hover { background: rgba(15, 20, 40, 0.04); }
ul.thread-list li.active { background: rgba(45, 74, 130, 0.10); }
ul.thread-list li .thread-title {
  font-weight: 500;
  font-size: 0.88rem;
  color: var(--chat-ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
ul.thread-list li .thread-meta {
  font-size: 0.74rem;
  color: var(--chat-ink-soft);
  display: flex;
  gap: 0.4rem;
  align-items: center;
}
ul.thread-list li .thread-meta .author {
  font-family: 'JetBrains Mono', monospace;
}
ul.thread-list li .inflight-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--chat-status-running-ink);
  animation: chat-pulse 1.2s ease-in-out infinite;
  display: inline-block;
}
ul.thread-list li.empty-row {
  text-align: center;
  color: var(--chat-ink-soft);
  cursor: default;
  font-size: 0.85rem;
  padding: 1.5rem 0.9rem;
}
ul.thread-list li.empty-row:hover { background: none; }

/* ===================================================================
 * Conversation pane (used by both surfaces)
 * =================================================================== */

.conversation-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--chat-sidebar-bg);
}
.conversation-header h3 {
  margin: 0;
  font-family: 'Fraunces', serif;
  font-size: 1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-header .meta {
  font-size: 0.78rem;
  color: var(--chat-ink-soft);
}

.message-pane {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  background: white;
}

.chat-message {
  max-width: 80%;
  padding: 0.55rem 0.8rem;
  border-radius: 12px;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: 0.9rem;
  line-height: 1.4;
}
.chat-message.user {
  align-self: flex-end;
  background: var(--chat-user-bubble);
  color: var(--chat-user-text);
  border-bottom-right-radius: 4px;
}
.chat-message.assistant {
  align-self: flex-start;
  background: var(--chat-assistant-bubble);
  color: var(--chat-assistant-text);
  border-bottom-left-radius: 4px;
}
.chat-message.inflight {
  align-self: flex-start;
  background: var(--chat-status-pending);
  color: var(--chat-ink-soft);
  font-style: italic;
}
.chat-message .meta {
  font-size: 0.72rem;
  color: var(--chat-ink-soft);
  margin-top: 0.3rem;
  display: block;
}
.chat-message.user .meta { color: rgba(255, 255, 255, 0.7); }

/* Animated typing dots for in-flight messages */
.typing-dots {
  display: inline-block;
}
.typing-dots span {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  margin: 0 1px;
  animation: chat-pulse 1.2s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes chat-pulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.9); }
  40% { opacity: 1; transform: scale(1.2); }
}

.chat-status-pill {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 99px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-left: 0.3rem;
  background: var(--chat-status-pending);
  color: var(--chat-status-pending-ink);
}
.chat-status-pill.running {
  background: var(--chat-status-running);
  color: var(--chat-status-running-ink);
}
.chat-status-pill.done {
  background: var(--chat-status-done);
  color: var(--chat-status-done-ink);
}
.chat-status-pill.failed {
  background: var(--chat-status-failed);
  color: var(--chat-status-failed-ink);
}

/* ===================================================================
 * Composer + read-only banner
 * =================================================================== */

.chat-readonly-banner {
  background: #fffbe7;
  border: 1px solid #f3e6a8;
  color: #6f5b1c;
  padding: 0.55rem 0.8rem;
  font-size: 0.82rem;
  margin: 0.5rem 1rem;
  border-radius: 4px;
}

.composer {
  border-top: 1px solid var(--chat-border);
  padding: 0.75rem;
  background: var(--chat-sidebar-bg);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.composer textarea {
  width: 100%;
  border: 1px solid var(--chat-border);
  border-radius: 4px;
  padding: 0.5rem 0.6rem;
  font-family: 'Inter Tight', system-ui, sans-serif;
  font-size: 0.88rem;
  resize: vertical;
  min-height: 3rem;
  background: white;
  box-sizing: border-box;
}
.composer textarea:disabled {
  background: #f0f1f4;
  color: var(--chat-ink-soft);
  cursor: not-allowed;
}
main.sbr-bot-page .composer textarea {
  min-height: 6rem;
}
.composer .composer-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}
.composer .hint {
  font-size: 0.75rem;
  color: var(--chat-ink-soft);
}
.composer button.send {
  padding: 0.4rem 1rem;
  background: var(--chat-user-bubble);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.composer button.send:disabled {
  background: #b1bac7;
  cursor: not-allowed;
}

/* Mobile: drawer becomes full-screen */
@media (max-width: 540px) {
  .sbr-chat-drawer { width: 100vw; }
  main.sbr-bot-page { grid-template-columns: 1fr; }
  main.sbr-bot-page > .sidebar { display: none; }
}

/* SBR-BOT page: hide the floating bubble (the page IS the bot) */
body.sbr-bot-page-mode .sbr-chat-bubble,
body.sbr-bot-page-mode .sbr-chat-drawer { display: none; }

/* ===================================================================
 * Rendered-markdown content inside assistant bubbles
 * =================================================================== */

.chat-message.assistant .md-h {
  font-family: 'Fraunces', serif;
  font-weight: 600;
  margin: 0.6rem 0 0.3rem;
  line-height: 1.25;
}
.chat-message.assistant h3.md-h { font-size: 1.05rem; }
.chat-message.assistant h4.md-h { font-size: 0.98rem; }
.chat-message.assistant h5.md-h { font-size: 0.92rem; color: var(--chat-ink-soft); }
.chat-message.assistant .md-h:first-child { margin-top: 0; }

.chat-message.assistant .md-p {
  margin: 0.45rem 0;
  line-height: 1.45;
}
.chat-message.assistant .md-p:first-child { margin-top: 0; }
.chat-message.assistant .md-p:last-child { margin-bottom: 0; }

.chat-message.assistant .md-list {
  margin: 0.45rem 0;
  padding-left: 1.4rem;
  line-height: 1.45;
}
.chat-message.assistant .md-list li {
  margin: 0.2rem 0;
}
.chat-message.assistant ol.md-list li {
  padding-left: 0.15rem;
}

.chat-message.assistant strong { font-weight: 600; }
.chat-message.assistant em { font-style: italic; }
.chat-message.assistant .md-icode {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85em;
  background: rgba(15, 20, 40, 0.06);
  padding: 0.05rem 0.3rem;
  border-radius: 3px;
}
.chat-message.assistant .md-code {
  margin: 0.5rem 0;
  padding: 0.6rem 0.8rem;
  background: rgba(15, 20, 40, 0.06);
  border-radius: 4px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.82em;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ===================================================================
 * Resize handle — draggable strip on the drawer's left edge or the
 * SBR-BOT page sidebar's right edge. 6px wide, full height, subtle
 * by default, brand-coloured during drag.
 * =================================================================== */

.chat-resize-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 6px;
  cursor: col-resize;
  background: transparent;
  z-index: 50;
  touch-action: none;          /* prevents scroll-while-drag on touch */
  transition: background 0.12s ease;
}
.chat-resize-handle:hover,
.chat-resize-handle.active {
  background: rgba(45, 74, 130, 0.35);
}
/* Drawer handle sits on the LEFT edge */
.sbr-chat-drawer .chat-resize-handle-left {
  left: -3px;
}
/* SBR-BOT page sidebar handle sits on the RIGHT edge */
main.sbr-bot-page > .sidebar .chat-resize-handle-right {
  right: -3px;
}

/* While resizing: kill text selection + tell the cursor to be col-resize
   everywhere on the page so the visual stays consistent during drag */
body.chat-resizing {
  cursor: col-resize !important;
  user-select: none;
}
body.chat-resizing * {
  cursor: col-resize !important;
}
