/* ==========================================================================
   WhitChatDrawer — unified side-drawer chat component
   Used by: exit ticket analysis, whit quiz analysis, admin benchmark analytics,
            essay chat and assignment dashboard
   Paired with: /static/js/whit-chat-drawer.js
                /templates/jarvis/includes/_whit_chat_drawer.html
   ========================================================================== */

:root {
  /* Brand palette */
  --wcd-purple-50:  #faf5ff;
  --wcd-purple-100: #f3e8ff;
  --wcd-purple-200: #e9d5ff;
  --wcd-purple-300: #d8b4fe;
  --wcd-purple-500: #8b5cf6;
  --wcd-purple-600: #7c3aed;
  --wcd-purple-700: #6d28d9;
  --wcd-purple-800: #5b21b6;
  --wcd-purple-900: #4c1d95;
  --wcd-indigo-500: #6366f1;

  --wcd-gray-50:  #f9fafb;
  --wcd-gray-100: #f3f4f6;
  --wcd-gray-200: #e5e7eb;
  --wcd-gray-300: #d1d5db;
  --wcd-gray-400: #9ca3af;
  --wcd-gray-600: #4b5563;
  --wcd-gray-700: #374151;
  --wcd-gray-800: #1f2937;
  --wcd-gray-900: #111827;

  /* Composed tokens */
  --wcd-header-gradient: linear-gradient(135deg, var(--wcd-purple-600) 0%, var(--wcd-indigo-500) 50%, var(--wcd-purple-500) 100%);
  --wcd-body-gradient:   linear-gradient(180deg, #ffffff 0%, var(--wcd-purple-50) 100%);
  --wcd-shadow:          -16px 0 32px rgba(124, 58, 237, 0.18), -2px 0 8px rgba(124, 58, 237, 0.10);
  --wcd-backdrop:        rgba(15, 23, 42, 0.45);

  /* Width tokens */
  --wcd-width-standard: 520px;
  --wcd-width-wide:     560px;
  --wcd-width-tablet:   460px;

  /* Z-index */
  --wcd-z-backdrop: 1400;
  --wcd-z-drawer:   1401;
}

/* --------------------------------------------------------------------------
   Backdrop & shell
   -------------------------------------------------------------------------- */

.whit-chat-backdrop {
  position: fixed;
  inset: 0;
  background: var(--wcd-backdrop);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: var(--wcd-z-backdrop);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.whit-chat-backdrop.is-open {
  opacity: 1;
  visibility: visible;
}

.whit-chat-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--wcd-width-standard);
  max-width: 100vw;
  background: var(--wcd-body-gradient);
  box-shadow: var(--wcd-shadow);
  z-index: var(--wcd-z-drawer);
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.whit-chat-drawer[data-size="wide"] {
  width: var(--wcd-width-wide);
}

.whit-chat-backdrop.is-open .whit-chat-drawer {
  transform: translateX(0);
}

@media (max-width: 1024px) {
  .whit-chat-drawer { width: var(--wcd-width-tablet); }
  .whit-chat-drawer[data-size="wide"] { width: var(--wcd-width-tablet); }
}

@media (max-width: 640px) {
  .whit-chat-drawer,
  .whit-chat-drawer[data-size="wide"] { width: 100vw; }
}

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

.whit-chat-header {
  padding: 1rem 1.1rem 0.85rem;
  background: var(--wcd-header-gradient);
  color: #fff;
  position: relative;
  border-bottom: 1px solid rgba(124, 58, 237, 0.18);
}

.whit-chat-kicker {
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.78);
  margin: 0 0 0.2rem 0;
}

.whit-chat-title {
  font-family: 'Playfair Display', Georgia, serif;
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0;
  line-height: 1.2;
  color: #fff;
}

.whit-chat-subtitle {
  margin: 0.25rem 0 0 0;
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.92);
  line-height: 1.35;
}

.whit-chat-close {
  position: absolute;
  top: 0.85rem;
  right: 0.9rem;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  line-height: 1;
  transition: background 0.15s ease, transform 0.15s ease;
}

.whit-chat-close:hover {
  background: rgba(255, 255, 255, 0.32);
  transform: scale(1.05);
}

.whit-chat-close:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

/* --------------------------------------------------------------------------
   Messages area
   -------------------------------------------------------------------------- */

.whit-chat-messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}

.whit-chat-msg {
  display: flex;
  max-width: 100%;
}

.whit-chat-msg[data-role="user"] {
  justify-content: flex-end;
}

.whit-chat-msg[data-role="assistant"],
.whit-chat-msg[data-role="system"] {
  justify-content: flex-start;
}

.whit-chat-bubble {
  max-width: 85%;
  padding: 0.7rem 0.85rem;
  border-radius: 14px;
  font-size: 0.9rem;
  line-height: 1.5;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.whit-chat-msg[data-role="user"] .whit-chat-bubble {
  background: var(--wcd-purple-600);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.whit-chat-msg[data-role="assistant"] .whit-chat-bubble {
  background: #fff;
  border: 1px solid var(--wcd-purple-200);
  color: var(--wcd-gray-800);
  border-bottom-left-radius: 4px;
}

.whit-chat-msg[data-role="system"] .whit-chat-bubble {
  background: var(--wcd-gray-100);
  color: var(--wcd-gray-700);
  border: 1px solid var(--wcd-gray-200);
  font-style: italic;
  font-size: 0.82rem;
}

.whit-chat-msg[data-role="error"] .whit-chat-bubble {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  font-size: 0.82rem;
}

.whit-chat-sender {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--wcd-purple-700);
  margin-bottom: 0.25rem;
  letter-spacing: 0.01em;
}

/* ----- Markdown content inside assistant bubbles -----
   Tuned for a narrow side-drawer panel: tighter than document defaults
   so long structured responses (headers + lists + paragraphs) don't
   feel airy or disjointed. */

/* ------------------------------------------------------------------
   Override legacy global rules from alias-styles.css.
   That file ships `.streaming-content { white-space: pre-wrap }` and
   `.streaming-content p { display: inline; margin: 0 }` for older
   chats (assignment_dashboard, student_debate) that wanted
   inline paragraphs. Inside this drawer we want proper block layout
   for headers/paragraphs/lists. Higher specificity wins.
   ------------------------------------------------------------------ */
.whit-chat-bubble .streaming-content {
  white-space: normal;
  line-height: inherit;
}
.whit-chat-bubble .streaming-content p {
  display: block;
  margin: 0 0 0.4rem 0;
}
.whit-chat-bubble .streaming-content p:last-child { margin-bottom: 0; }

.whit-chat-bubble > *:first-child { margin-top: 0; }
.whit-chat-bubble > *:last-child  { margin-bottom: 0; }

.whit-chat-bubble p {
  margin: 0 0 0.4rem 0;
}
.whit-chat-bubble p:last-child { margin-bottom: 0; }
.whit-chat-bubble p:empty { display: none; }

.whit-chat-bubble h1,
.whit-chat-bubble h2,
.whit-chat-bubble h3,
.whit-chat-bubble h4 {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 700;
  color: var(--wcd-purple-900);
  margin: 0.55rem 0 0.25rem 0;
  line-height: 1.25;
}
.whit-chat-bubble h1 { font-size: 1.05rem; }
.whit-chat-bubble h2 { font-size: 0.98rem; }
.whit-chat-bubble h3,
.whit-chat-bubble h4 { font-size: 0.92rem; }

/* Header immediately followed by content shouldn't add a second top gap */
.whit-chat-bubble h1 + p, .whit-chat-bubble h2 + p,
.whit-chat-bubble h3 + p, .whit-chat-bubble h4 + p,
.whit-chat-bubble h1 + ul, .whit-chat-bubble h2 + ul,
.whit-chat-bubble h3 + ul, .whit-chat-bubble h4 + ul,
.whit-chat-bubble h1 + ol, .whit-chat-bubble h2 + ol,
.whit-chat-bubble h3 + ol, .whit-chat-bubble h4 + ol,
.whit-chat-bubble h1 + blockquote, .whit-chat-bubble h2 + blockquote,
.whit-chat-bubble h3 + blockquote, .whit-chat-bubble h4 + blockquote {
  margin-top: 0.15rem;
}

.whit-chat-bubble ul,
.whit-chat-bubble ol {
  margin: 0.2rem 0 0.4rem 1.15rem;
  padding: 0;
}

.whit-chat-bubble li {
  margin-bottom: 0.1rem;
}

/* A list item containing only a paragraph: collapse the inner <p>
   margin so the bullet doesn't look double-spaced. */
.whit-chat-bubble li > p {
  display: inline;
  margin: 0;
}
.whit-chat-bubble li > p + ul,
.whit-chat-bubble li > p + ol {
  display: block;
  margin-top: 0.2rem;
}
.whit-chat-bubble li + li { margin-top: 0.1rem; }

.whit-chat-bubble blockquote {
  margin: 0.3rem 0 0.4rem 0;
  padding: 0.15rem 0 0.15rem 0.7rem;
  border-left: 3px solid var(--wcd-purple-200);
  color: var(--wcd-gray-700);
}
.whit-chat-bubble blockquote > *:first-child { margin-top: 0; }
.whit-chat-bubble blockquote > *:last-child  { margin-bottom: 0; }

.whit-chat-bubble hr {
  border: 0;
  border-top: 1px solid var(--wcd-purple-200);
  margin: 0.5rem 0;
}

.whit-chat-bubble strong { color: var(--wcd-purple-900); font-weight: 700; }

.whit-chat-bubble code {
  background: var(--wcd-purple-50);
  color: var(--wcd-purple-800);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85em;
  font-family: 'SF Mono', Menlo, Consolas, monospace;
}

.whit-chat-bubble pre {
  background: var(--wcd-gray-900);
  color: var(--wcd-gray-100);
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
  margin: 0.5rem 0;
  font-size: 0.82rem;
}

.whit-chat-bubble pre code {
  background: transparent;
  color: inherit;
  padding: 0;
}

.whit-chat-bubble table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin: 0.5rem 0;
}

.whit-chat-bubble th,
.whit-chat-bubble td {
  border: 1px solid var(--wcd-purple-200);
  padding: 0.4rem 0.55rem;
  text-align: left;
}

.whit-chat-bubble th {
  background: var(--wcd-purple-50);
  font-weight: 600;
  color: var(--wcd-purple-900);
}

.whit-chat-bubble a {
  color: var(--wcd-purple-700);
  text-decoration: underline;
}

/* Resolved alias spans (from AliasResolver) — match brand styling */
.whit-chat-bubble .student-alias.real-name-display {
  color: var(--wcd-purple-700);
  font-weight: 600;
}

.whit-chat-bubble .student-alias {
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.85em;
  color: var(--wcd-gray-600);
}

/* --------------------------------------------------------------------------
   Streaming indicator (blinking caret at end of last paragraph)
   -------------------------------------------------------------------------- */

.whit-chat-bubble.is-streaming::after {
  content: '';
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--wcd-purple-600);
  vertical-align: text-bottom;
  margin-left: 2px;
  animation: whit-chat-caret 1s steps(2) infinite;
}

@keyframes whit-chat-caret {
  to { opacity: 0; }
}

/* --------------------------------------------------------------------------
   Typing indicator (animated dots)
   -------------------------------------------------------------------------- */

.whit-chat-typing {
  display: none;
  align-items: center;
  gap: 0.45rem;
  padding: 0.4rem 0.85rem;
  color: var(--wcd-purple-700);
  font-size: 0.78rem;
}

.whit-chat-typing.is-visible { display: inline-flex; }

.whit-chat-typing-dots {
  display: inline-flex;
  gap: 3px;
}

.whit-chat-typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--wcd-purple-500);
  animation: whit-chat-bounce 1.2s infinite ease-in-out;
}

.whit-chat-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.whit-chat-typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes whit-chat-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40%           { transform: scale(1);   opacity: 1;   }
}

/* --------------------------------------------------------------------------
   Starter prompt chips (inline, fade-out on first send)
   -------------------------------------------------------------------------- */

.whit-chat-chips {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  padding: 0.4rem 0 0.2rem 0;
  transition: opacity 0.25s ease;
}

.whit-chat-chips.is-dismissing { opacity: 0; pointer-events: none; }

.whit-chat-chips-label {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--wcd-gray-400);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 0;
}

.whit-chat-chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.whit-chat-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  background: #fff;
  border: 1px solid var(--wcd-purple-200);
  color: var(--wcd-purple-700);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(124, 58, 237, 0.08);
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.whit-chat-chip:hover {
  background: var(--wcd-purple-50);
  border-color: var(--wcd-purple-300);
  color: var(--wcd-purple-800);
}

.whit-chat-chip:active { transform: scale(0.97); }

.whit-chat-chip-icon {
  font-size: 0.9rem;
  color: var(--wcd-purple-600);
}

/* --------------------------------------------------------------------------
   Scroll-to-latest button (appears when user scrolls up during streaming)
   -------------------------------------------------------------------------- */

.whit-chat-scroll-latest {
  position: absolute;
  bottom: 5.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--wcd-purple-600);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 0.4rem 0.85rem;
  font-size: 0.78rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(124, 58, 237, 0.35);
  display: none;
  align-items: center;
  gap: 0.35rem;
  z-index: 2;
}

.whit-chat-scroll-latest.is-visible { display: inline-flex; }

/* --------------------------------------------------------------------------
   Footer (input area)
   -------------------------------------------------------------------------- */

.whit-chat-footer {
  border-top: 1px solid var(--wcd-purple-200);
  background: #fff;
  padding: 0.7rem 0.85rem 0.85rem;
  position: relative;
}

.whit-chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.whit-chat-prompts-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--wcd-purple-200);
  background: #fff;
  color: var(--wcd-purple-600);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.whit-chat-prompts-btn:hover {
  background: var(--wcd-purple-50);
  border-color: var(--wcd-purple-300);
}

.whit-chat-prompts-btn[hidden] { display: none; }

.whit-chat-textarea {
  flex: 1 1 auto;
  min-height: 38px;
  max-height: 160px;
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--wcd-purple-200);
  border-radius: 10px;
  font: inherit;
  font-size: 0.9rem;
  line-height: 1.4;
  color: var(--wcd-gray-800);
  background: #fff;
  resize: none;
  overflow-y: auto;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  font-family: inherit;
}

.whit-chat-textarea:focus {
  border-color: var(--wcd-purple-600);
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.16);
}

.whit-chat-textarea::placeholder {
  color: var(--wcd-gray-400);
}

.whit-chat-send {
  flex: 0 0 auto;
  height: 38px;
  padding: 0 0.95rem;
  border-radius: 10px;
  background: var(--wcd-purple-600);
  color: #fff;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  transition: background 0.15s ease, transform 0.1s ease;
}

.whit-chat-send:hover:not(:disabled) { background: var(--wcd-purple-700); }
.whit-chat-send:active:not(:disabled) { transform: scale(0.97); }

.whit-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.whit-chat-footer-hint {
  font-size: 0.7rem;
  color: var(--wcd-gray-400);
  margin: 0.4rem 0 0 0;
  text-align: right;
}

/* --------------------------------------------------------------------------
   Demo / disabled mode
   -------------------------------------------------------------------------- */

.whit-chat-demo-notice {
  margin: 0.5rem 1.1rem;
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  background: var(--wcd-purple-50);
  border: 1px dashed var(--wcd-purple-300);
  color: var(--wcd-purple-800);
  font-size: 0.8rem;
  line-height: 1.4;
}

.whit-chat-drawer[data-enabled="false"] .whit-chat-textarea,
.whit-chat-drawer[data-enabled="false"] .whit-chat-send,
.whit-chat-drawer[data-enabled="false"] .whit-chat-prompts-btn {
  opacity: 0.5;
  pointer-events: none;
}
