/**
 * AI Study Assistant — Redesigned UI
 * Claude-inspired clean panel with smooth animations.
 * Integrates with StudyHub design tokens in variables.css.
 */

/* ============================================
   Local tokens (scoped to AI assistant)
   ============================================ */
:root {
  --ai-bg: #fdfcfa;
  --ai-bg-sunken: #f6f4f0;
  --ai-text: #1a1a1a;
  --ai-text-muted: #6b7280;
  --ai-text-soft: #9ca3af;
  --ai-border: rgba(20, 24, 40, 0.08);
  --ai-border-soft: rgba(20, 24, 40, 0.05);
  --ai-accent: #0071e3;
  --ai-accent-soft: rgba(0, 113, 227, 0.08);
  --ai-accent-2: #5856d6;
  --ai-accent-gradient: linear-gradient(135deg, #0071e3 0%, #5e5ce6 60%, #af52de 110%);

  --ai-radius: 20px;
  --ai-radius-md: 14px;
  --ai-radius-sm: 10px;

  --ai-shadow-fab: 0 12px 32px -8px rgba(0, 113, 227, 0.45),
                   0 4px 14px -4px rgba(88, 86, 214, 0.32);
  --ai-shadow-panel: 0 28px 64px -16px rgba(20, 24, 40, 0.22),
                     0 8px 24px -8px rgba(20, 24, 40, 0.08);

  --ai-ease: cubic-bezier(0.22, 1, 0.36, 1);
  --ai-ease-bounce: cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ============================================
   Visibility — only show when logged in
   ============================================ */
body:not(.logged-in) .ai-fab,
body:not(.logged-in) .ai-chat {
  display: none !important;
}

/* ============================================
   Floating Action Button (FAB)
   ============================================ */
.ai-fab {
  position: fixed;
  bottom: max(28px, env(safe-area-inset-bottom));
  right: max(28px, env(safe-area-inset-right));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: var(--ai-accent-gradient);
  border: 0;
  cursor: pointer;
  color: #fff;
  display: grid;
  place-items: center;
  box-shadow: var(--ai-shadow-fab);
  z-index: 600;
  isolation: isolate;
  transition:
    transform 400ms var(--ai-ease-bounce),
    box-shadow 400ms var(--ai-ease);
}

.ai-fab__halo {
  position: absolute;
  inset: -6px;
  border-radius: 50%;
  background: var(--ai-accent-gradient);
  opacity: 0.35;
  filter: blur(14px);
  z-index: -1;
  transition: opacity 300ms var(--ai-ease);
  pointer-events: none;
}

.ai-fab:hover {
  transform: translateY(-2px) scale(1.06);
}

.ai-fab:hover .ai-fab__halo {
  opacity: 0.55;
}

.ai-fab:active {
  transform: scale(0.96);
}

.ai-fab:focus-visible {
  outline: 3px solid rgba(0, 113, 227, 0.4);
  outline-offset: 4px;
}

.ai-fab__icon {
  width: 26px;
  height: 26px;
  transition: opacity 280ms var(--ai-ease), transform 280ms var(--ai-ease);
}

.ai-fab__icon--open {
  animation: aiSparkleFloat 4s ease-in-out infinite;
}

.ai-fab__icon--close {
  position: absolute;
  inset: 0;
  margin: auto;
  opacity: 0;
  transform: rotate(-90deg) scale(0.6);
}

.ai-fab[aria-expanded="true"] .ai-fab__icon--open {
  opacity: 0;
  transform: rotate(90deg) scale(0.6);
  animation: none;
}

.ai-fab[aria-expanded="true"] .ai-fab__icon--close {
  opacity: 1;
  transform: rotate(0) scale(1);
}

@keyframes aiSparkleFloat {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-2px) rotate(12deg); }
}

/* ============================================
   Chat Panel (container)
   ============================================ */
.ai-chat {
  position: fixed;
  bottom: 104px;
  right: 28px;
  /* Width/height are user-adjustable on desktop + tablet via the resize
     handles below. The custom props hold the dragged size; the fallbacks are
     the defaults. The mobile drawer (≤640px) hard-codes its own size and
     ignores these props, so it always stays a full-width sheet. */
  width: var(--ai-chat-w, 440px);
  height: var(--ai-chat-h, min(640px, calc(100dvh - 140px)));
  z-index: 600;
  pointer-events: none;
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  transform-origin: bottom right;
  transition:
    opacity 280ms var(--ai-ease),
    transform 320ms var(--ai-ease);
}

.ai-chat.is-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.ai-chat__panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: rgba(253, 252, 250, 0.92);
  border: 1px solid var(--ai-border);
  border-radius: var(--ai-radius);
  box-shadow: var(--ai-shadow-panel);
  overflow: hidden;
  backdrop-filter: blur(20px) saturate(1.2);
  -webkit-backdrop-filter: blur(20px) saturate(1.2);
}

/* ============================================
   Resize handles (desktop + tablet)
   The panel is anchored bottom-right, so it grows toward the top-left.
   Drag the left edge (width), the top edge (height) or the top-left corner
   (both). Injected by ai-assistant.js so every host page gets them. Hidden on
   the mobile full-width drawer (≤640px).
   ============================================ */
.ai-chat__resize {
  position: absolute;
  z-index: 5;
  touch-action: none;                /* pointer-drag must not scroll the page */
  -webkit-tap-highlight-color: transparent;
}

/* Left edge → width. Sits in the 0–16px gutter, clear of the header avatar. */
.ai-chat__resize--left {
  top: 16px;
  bottom: 56px;
  left: -4px;
  width: 12px;
  cursor: ew-resize;
}

/* Top edge → height. Inset from both corners so it never covers the header
   buttons (top-right) or the corner grip (top-left). */
.ai-chat__resize--top {
  top: -4px;
  left: 64px;
  right: 64px;
  height: 12px;
  cursor: ns-resize;
}

/* Top-left corner → both dimensions at once. */
.ai-chat__resize--corner {
  top: -2px;
  left: -2px;
  width: 26px;
  height: 26px;
  cursor: nwse-resize;
}

/* Faint grip so the affordance is discoverable (also visible on touch tablets
   that have no hover). Brightens on hover / while dragging. Positioned on the
   panel surface, above-left of the avatar which starts ~14px in. */
.ai-chat__resize--corner::before {
  content: "";
  position: absolute;
  top: 8px;
  left: 8px;
  width: 9px;
  height: 9px;
  border-top: 2px solid var(--ai-text-soft);
  border-left: 2px solid var(--ai-text-soft);
  border-top-left-radius: 4px;
  opacity: 0.4;
  transition: opacity 200ms var(--ai-ease), border-color 200ms var(--ai-ease);
}

.ai-chat:hover .ai-chat__resize--corner::before,
.ai-chat.is-resizing .ai-chat__resize--corner::before {
  opacity: 0.85;
  border-color: var(--ai-accent);
}

/* Freeze selection + content hover noise while dragging. */
.ai-chat.is-resizing {
  user-select: none;
}

.ai-chat.is-resizing .ai-chat__panel {
  pointer-events: none;
}

@media (max-width: 640px) {
  .ai-chat__resize { display: none; }
}

/* ============================================
   Header
   ============================================ */
.ai-chat__header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--ai-border-soft);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.2));
}

.ai-chat__avatar {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  background: var(--ai-accent-gradient);
  color: #fff;
  box-shadow: 0 6px 14px -4px rgba(0, 113, 227, 0.5);
  flex-shrink: 0;
}

.ai-chat__avatar svg {
  width: 18px;
  height: 18px;
}

.ai-chat__heading {
  flex: 1;
  min-width: 0;
}

.ai-chat__title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ai-text);
  line-height: 1.2;
}

.ai-chat__subtitle {
  margin: 2px 0 0;
  font-size: 0.74rem;
  color: var(--ai-text-muted);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Model badge — shows which AI model the assistant is currently running on. */
.ai-chat__model {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 100%;
  margin-top: 5px;
  padding: 1px 8px;
  border-radius: 999px;
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  line-height: 1.6;
  color: var(--ai-accent);
  background: var(--ai-accent-soft);
  border: 1px solid rgba(0, 113, 227, 0.2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-chat__model::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
  box-shadow: 0 0 0 2px var(--ai-accent-soft);
}

.ai-chat__header-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.ai-chat__icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: transparent;
  border: 0;
  color: var(--ai-text-muted);
  cursor: pointer;
  transition: background 180ms var(--ai-ease), color 180ms var(--ai-ease);
}

/* `display: grid` above beats the UA `[hidden]{display:none}`, so a hidden
   icon button (e.g. the injected reset-size button) needs this to stay hidden. */
.ai-chat__icon-btn[hidden] {
  display: none;
}

.ai-chat__icon-btn:hover {
  background: var(--ai-accent-soft);
  color: var(--ai-accent);
}

.ai-chat__icon-btn:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-chat__icon-btn svg {
  width: 15px;
  height: 15px;
}

/* ============================================
   API key notice (warning)
   ============================================ */
.ai-apikey-notice {
  margin: 10px 14px 0;
  padding: 8px 12px;
  background: rgba(255, 149, 0, 0.08);
  border: 1px solid rgba(255, 149, 0, 0.25);
  border-radius: var(--ai-radius-sm);
  font-size: 0.75rem;
  color: #a85a00;
  display: flex;
  gap: 8px;
  align-items: flex-start;
  line-height: 1.4;
}

.ai-apikey-notice[hidden] {
  display: none !important;
}

.ai-apikey-notice code {
  background: rgba(255, 149, 0, 0.2);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: var(--font-family-mono, 'SF Mono', monospace);
  font-size: 0.7rem;
}

/* ============================================
   Messages area
   ============================================ */
.ai-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
}

.ai-messages::-webkit-scrollbar {
  width: 6px;
}

.ai-messages::-webkit-scrollbar-track {
  background: transparent;
}

.ai-messages::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.12);
  border-radius: 3px;
}

.ai-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.22);
}

/* ============================================
   Welcome screen
   ============================================ */
.ai-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 8px 8px;
  text-align: center;
  animation: aiFadeIn 400ms var(--ai-ease) both;
}

.ai-welcome__icon {
  width: 56px;
  height: 56px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: var(--ai-accent-gradient);
  color: #fff;
  box-shadow: 0 10px 24px -8px rgba(0, 113, 227, 0.5);
  margin-bottom: 12px;
  animation: aiAvatarFloat 5s ease-in-out infinite;
}

.ai-welcome__icon svg {
  width: 28px;
  height: 28px;
}

@keyframes aiAvatarFloat {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-4px) rotate(3deg); }
}

.ai-welcome__title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: var(--ai-text);
}

.ai-welcome__subtitle {
  margin: 4px 0 16px;
  font-size: 0.825rem;
  color: var(--ai-text-muted);
  line-height: 1.45;
  max-width: 290px;
}

.ai-suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
}

.ai-suggestion {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  text-align: left;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid var(--ai-border);
  border-radius: var(--ai-radius-md);
  color: var(--ai-text);
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  line-height: 1.35;
  cursor: pointer;
  transition:
    transform 200ms var(--ai-ease),
    background 200ms var(--ai-ease),
    border-color 200ms var(--ai-ease),
    box-shadow 200ms var(--ai-ease);
}

.ai-suggestion:hover {
  background: #fff;
  border-color: rgba(0, 113, 227, 0.3);
  transform: translateY(-1px);
  box-shadow: 0 4px 14px -4px rgba(0, 113, 227, 0.18);
}

.ai-suggestion:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-suggestion__icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--ai-accent);
  margin-top: 1px;
}

.ai-suggestion__label {
  flex: 1;
}

/* ============================================
   Message bubbles
   ============================================ */
.ai-msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  animation: aiMsgSlideIn 280ms var(--ai-ease) both;
}

.ai-msg--user {
  justify-content: flex-end;
}

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

@keyframes aiFadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.ai-msg__avatar {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--ai-accent-gradient);
  box-shadow: 0 4px 10px -3px rgba(0, 113, 227, 0.45);
}

.ai-msg__avatar svg {
  width: 14px;
  height: 14px;
}

.ai-msg__bubble {
  max-width: calc(100% - 38px);
  padding: 10px 14px;
  border-radius: var(--ai-radius-md);
  font-size: 0.875rem;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.ai-msg--user .ai-msg__bubble {
  background: var(--ai-accent-gradient);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 4px 14px -4px rgba(0, 113, 227, 0.35);
}

.ai-msg--ai .ai-msg__bubble {
  background: var(--ai-bg-sunken);
  color: var(--ai-text);
  border: 1px solid var(--ai-border-soft);
  border-bottom-left-radius: 4px;
}

/* Content reset inside AI bubble */
.ai-msg--ai .ai-msg__bubble > :first-child { margin-top: 0; }
.ai-msg--ai .ai-msg__bubble > :last-child  { margin-bottom: 0; }

.ai-msg--ai p { margin: 0 0 8px; }
.ai-msg--ai p:last-child { margin-bottom: 0; }

.ai-msg--ai h2,
.ai-msg--ai h3,
.ai-msg--ai h4 {
  margin: 12px 0 6px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ai-text);
  line-height: 1.3;
}

.ai-msg--ai h2 { font-size: 1.05rem; }
.ai-msg--ai h3 { font-size: 0.95rem; }
.ai-msg--ai h4 { font-size: 0.875rem; }

.ai-msg--ai ul,
.ai-msg--ai ol {
  margin: 6px 0;
  padding-left: 20px;
}

.ai-msg--ai li {
  margin: 3px 0;
}

.ai-msg--ai li::marker {
  color: var(--ai-accent);
}

.ai-msg--ai strong { font-weight: 600; }
.ai-msg--ai em { font-style: italic; }

.ai-msg--ai hr {
  border: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.12), transparent);
  margin: 12px 0;
}

/* Inline links (links inside text — not standalone source cards) */
.ai-msg--ai a:not(.ai-source-card) {
  color: var(--ai-accent);
  text-decoration: underline;
  text-decoration-color: rgba(0, 113, 227, 0.4);
  text-underline-offset: 2px;
  transition: text-decoration-color 200ms var(--ai-ease);
}

.ai-msg--ai a:not(.ai-source-card):hover {
  text-decoration-color: var(--ai-accent);
}

/* ============================================
   Source cards — Claude-style clickable areas
   ============================================ */
.ai-source-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  margin: 6px 0;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid var(--ai-border);
  border-radius: 12px;
  color: var(--ai-text);
  text-decoration: none;
  transition:
    transform 200ms var(--ai-ease),
    border-color 200ms var(--ai-ease),
    box-shadow 200ms var(--ai-ease),
    background 200ms var(--ai-ease);
  animation: aiCardFadeIn 300ms var(--ai-ease) both;
}

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

.ai-source-card:hover {
  border-color: var(--ai-accent);
  background: #fff;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px -6px rgba(0, 113, 227, 0.22);
  text-decoration: none;
}

.ai-source-card:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-source-card__icon {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--ai-accent-soft);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.ai-source-card__icon svg {
  width: 16px;
  height: 16px;
  color: var(--ai-accent);
}

.ai-source-card__body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  overflow: hidden;
}

.ai-source-card__title {
  font-size: 0.825rem;
  font-weight: 500;
  color: var(--ai-text);
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
          line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.ai-source-card__url {
  font-size: 0.7rem;
  color: var(--ai-text-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ai-source-card__arrow {
  width: 16px;
  height: 16px;
  color: var(--ai-text-soft);
  flex-shrink: 0;
  transition: transform 200ms var(--ai-ease), color 200ms var(--ai-ease);
}

.ai-source-card:hover .ai-source-card__arrow {
  color: var(--ai-accent);
  transform: translate(2px, -2px);
}

/* ============================================
   Tables — scrollable wrapper, sticky header
   ============================================ */
.ai-table-wrap {
  margin: 10px 0;
  overflow-x: auto;
  border-radius: var(--ai-radius-sm);
  border: 1px solid var(--ai-border);
  background: rgba(255, 255, 255, 0.7);
  scrollbar-width: thin;
}

.ai-table-wrap::-webkit-scrollbar {
  height: 6px;
}

.ai-table-wrap::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.18);
  border-radius: 3px;
}

.ai-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.8125rem;
}

.ai-table th,
.ai-table td {
  padding: 8px 12px;
  margin: 0;
  text-align: left;
  /* Reset first, then only keep a bottom rule — host pages (Typora readers) put
     a full 1px border on every th/td which turned our clean table into a grid. */
  border: 0;
  border-bottom: 1px solid var(--ai-border-soft);
  white-space: nowrap;
}

.ai-table th {
  background: var(--ai-bg-sunken);
  font-weight: 600;
  color: var(--ai-text);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  position: sticky;
  top: 0;
}

.ai-table tr {
  border: 0;
}

/* Kill any host-page row striping / thead fill leaking onto our rows.
   Prefixed with .ai-msg--ai to out-specify `table tr:nth-child(2n)`. */
.ai-msg--ai .ai-table thead,
.ai-msg--ai .ai-table tbody tr {
  background: none;
}

.ai-table tr:last-child td {
  border-bottom: 0;
}

.ai-table tbody tr {
  transition: background 150ms var(--ai-ease);
}

.ai-table tbody tr:hover td {
  background: var(--ai-accent-soft);
}

/* ============================================
   Code blocks
   ============================================ */
.ai-code-inline {
  background: rgba(20, 24, 40, 0.06);
  padding: 2px 6px;
  /* Explicit resets so a host page's global `code {}` rules can't leak in
     (e.g. the Typora answer readers add a light border/background). */
  border: 0;
  box-shadow: none;
  border-radius: 5px;
  font-family: var(--font-family-mono, 'SF Mono', 'Consolas', monospace);
  font-size: 0.85em;
  color: var(--ai-text);
}

.ai-code-block {
  position: relative;
  display: block;
  margin: 10px 0;
  padding: 14px;
  padding-top: 28px;
  background: #1f2128;
  border-radius: var(--ai-radius-sm);
  overflow-x: auto;
  font-family: var(--font-family-mono, 'SF Mono', monospace);
  font-size: 0.78rem;
  line-height: 1.55;
  color: #e6e6eb;
}

.ai-code-block code {
  font-family: inherit;
  display: block;
  white-space: pre;
  /* Hard reset: host pages (e.g. the Typora answer readers) style `code` with a
     light background, border and smaller font — which turned the light code text
     invisible and misaligned inside our dark block. Force it back to inherit. */
  background: none;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  padding: 0;
  margin: 0;
  font-size: inherit;
  line-height: inherit;
  color: inherit;
}

.ai-code-block__lang {
  position: absolute;
  top: 6px;
  right: 10px;
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #8a8d99;
  font-family: var(--font-family-primary, sans-serif);
  font-weight: 600;
}

/* ============================================
   Math (KaTeX target)
   ============================================ */
.ai-math-block {
  display: block;
  text-align: center;
  margin: 10px 0;
  padding: 12px;
  background: rgba(88, 86, 214, 0.05);
  border: 1px solid rgba(88, 86, 214, 0.12);
  border-radius: var(--ai-radius-sm);
  overflow-x: auto;
}

.ai-math {
  display: inline;
}

/* Fallback styling before KaTeX renders */
.ai-math-block:not(.katex-rendered),
.ai-math:not(.katex-rendered) {
  font-family: 'Cambria Math', 'Latin Modern Math', 'Times New Roman', serif;
  font-style: italic;
  color: var(--ai-accent-2);
}

/* ============================================
   Typing indicator
   ============================================ */
.ai-typing {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.ai-typing__bubble {
  background: var(--ai-bg-sunken);
  border: 1px solid var(--ai-border-soft);
  border-radius: var(--ai-radius-md) var(--ai-radius-md) var(--ai-radius-md) 4px;
  padding: 12px 14px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.ai-typing__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ai-text-muted);
  opacity: 0.55;
  animation: aiTypingBounce 1.35s ease-in-out infinite;
}

.ai-typing__dot:nth-child(2) { animation-delay: 0.18s; }
.ai-typing__dot:nth-child(3) { animation-delay: 0.36s; }

@keyframes aiTypingBounce {
  0%, 65%, 100% { transform: translateY(0); opacity: 0.4; }
  35%           { transform: translateY(-5px); opacity: 1; }
}

/* ============================================
   Input area
   ============================================ */
/* "Continue generating" — shown under an answer cut off by the output limit. */
.ai-continue {
  margin-top: 10px;
}

.ai-continue-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ai-accent);
  background: var(--ai-accent-soft);
  border: 1px solid var(--ai-accent);
  border-radius: 999px;
  cursor: pointer;
  transition:
    background 160ms var(--ai-ease),
    transform 120ms var(--ai-ease),
    opacity 160ms var(--ai-ease);
}

.ai-continue-btn:hover { transform: translateY(-1px); }
.ai-continue-btn:active { transform: translateY(0); }
.ai-continue-btn:disabled { opacity: 0.55; cursor: default; transform: none; }

.ai-continue-btn svg {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.ai-input-area {
  flex-shrink: 0;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--ai-border-soft);
  background: linear-gradient(0deg, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0.2));
}

.ai-input-wrap {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  background: #fff;
  border: 1px solid var(--ai-border);
  border-radius: 18px;
  padding: 6px 6px 6px 14px;
  transition:
    border-color 200ms var(--ai-ease),
    box-shadow 200ms var(--ai-ease);
}

.ai-input-wrap:focus-within {
  border-color: var(--ai-accent);
  box-shadow: 0 0 0 4px var(--ai-accent-soft);
}

.ai-input {
  flex: 1;
  border: 0;
  outline: 0;
  background: transparent;
  font-family: inherit;
  font-size: 0.875rem;
  color: var(--ai-text);
  resize: none;
  min-height: 22px;
  max-height: 120px;
  line-height: 1.5;
  padding: 6px 0;
}

.ai-input::placeholder {
  color: var(--ai-text-soft);
}

.ai-input-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  flex-shrink: 0;
}

.ai-mode-toggle {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--ai-text-muted);
  border-radius: 12px;
  padding: 5px 8px 5px 7px;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  cursor: pointer;
  transition:
    background 180ms var(--ai-ease),
    color 180ms var(--ai-ease),
    border-color 180ms var(--ai-ease);
}

.ai-mode-toggle svg {
  width: 14px;
  height: 14px;
}

.ai-mode-toggle:hover {
  background: var(--ai-accent-soft);
  color: var(--ai-accent);
}

.ai-mode-toggle:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-mode-toggle[aria-pressed="true"] {
  background: var(--ai-accent-soft);
  color: var(--ai-accent);
  border-color: rgba(0, 113, 227, 0.28);
}

.ai-send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--ai-accent-gradient);
  border: 0;
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 180ms var(--ai-ease), opacity 180ms var(--ai-ease);
  flex-shrink: 0;
}

.ai-send-btn svg {
  width: 14px;
  height: 14px;
}

.ai-send-btn:hover:not(:disabled) {
  transform: scale(1.08);
}

.ai-send-btn:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 2px;
}

.ai-send-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.ai-input-hint {
  position: relative;
  margin: 6px 4px 0;
  font-size: 0.68rem;
  color: var(--ai-text-soft);
  text-align: center;
  line-height: 1.4;
}

/* Live character counter, pinned to the right of the hint row. */
.ai-input-counter {
  position: absolute;
  right: 0;
  top: 0;
  font-variant-numeric: tabular-nums;
  color: var(--ai-text-muted);
  white-space: nowrap;
}

.ai-input-counter.is-warn {
  color: #d92d20;
  font-weight: 600;
}

.ai-input-hint kbd {
  display: inline-block;
  background: rgba(20, 24, 40, 0.06);
  border: 1px solid var(--ai-border);
  border-radius: 4px;
  padding: 0 5px;
  font-family: inherit;
  font-size: 0.66rem;
  color: var(--ai-text-muted);
  line-height: 1.4;
}

/* ============================================
   Responsive
   ============================================ */

/* Tablet */
@media (max-width: 1023px) {
  .ai-chat {
    width: var(--ai-chat-w, 400px);
    right: 20px;
    bottom: 92px;
  }
  .ai-fab {
    bottom: max(22px, env(safe-area-inset-bottom));
    right: max(22px, env(safe-area-inset-right));
    width: 56px;
    height: 56px;
  }
  .ai-fab__icon { width: 24px; height: 24px; }
}

/* Mobile — full-width drawer */
@media (max-width: 640px) {
  .ai-chat {
    inset: auto 0 0 0;
    width: 100%;
    height: min(86dvh, 720px);
    transform-origin: bottom center;
    transform: translateY(20px);
  }
  .ai-chat.is-open {
    transform: translateY(0);
  }
  .ai-chat__panel {
    border-radius: var(--ai-radius) var(--ai-radius) 0 0;
  }
  .ai-fab {
    bottom: max(16px, env(safe-area-inset-bottom));
    right: max(16px, env(safe-area-inset-right));
    width: 52px;
    height: 52px;
  }
  .ai-fab__icon { width: 22px; height: 22px; }
  /* Full-width bottom drawer: keep the composer clear of the home indicator */
  .ai-input-area {
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .ai-suggestions {
    grid-template-columns: 1fr;
  }
  .ai-welcome {
    padding-top: 8px;
  }
  .ai-welcome__icon {
    width: 50px;
    height: 50px;
    border-radius: 16px;
  }
  .ai-welcome__icon svg { width: 24px; height: 24px; }
  .ai-welcome__title { font-size: 1.05rem; }
  .ai-welcome__subtitle { font-size: 0.8rem; }
}

/* Small mobile */
@media (max-width: 380px) {
  .ai-chat__avatar {
    width: 32px;
    height: 32px;
    border-radius: 10px;
  }
  .ai-chat__title { font-size: 0.875rem; }
  .ai-chat__subtitle { font-size: 0.68rem; }
  .ai-chat__icon-btn { width: 30px; height: 30px; }
  .ai-input-hint { display: none; }
  .ai-messages { padding: 12px; gap: 12px; }
  .ai-mode-toggle__label { display: none; }
  .ai-mode-toggle {
    padding: 6px;
    width: 30px;
    height: 30px;
    justify-content: center;
  }
}

@media (max-width: 320px) {
  .ai-chat__subtitle { display: none; }
}

/* ============================================
   Reduced motion
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  .ai-fab,
  .ai-chat,
  .ai-msg,
  .ai-source-card,
  .ai-fab__icon,
  .ai-welcome,
  .ai-welcome__icon,
  .ai-typing__dot {
    transition: none !important;
    animation: none !important;
  }
}

/* ============================================
   KaTeX override — keep rendered math readable
   ============================================ */
.ai-msg--ai .katex {
  font-size: 1em;
}

.ai-msg--ai .katex-display {
  margin: 8px 0;
}

/* ============================================
   Streaming cursor
   ============================================ */
@keyframes ai-blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

.ai-stream-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: currentColor;
  vertical-align: text-bottom;
  margin-left: 1px;
  animation: ai-blink 800ms step-end infinite;
}

/* ============================================
   Attached question context (learning Q&A mode)
   A "Ask AI about this" button drops the question here as a card that
   sits above the input, kept visually distinct from what the user types.
   Only rendered on pages that include an #aiAttachedContext container.
   ============================================ */
.ai-attached {
  margin-bottom: 8px;
}

.ai-attached[hidden] {
  display: none;
}

.ai-attached__card {
  position: relative;
  display: block;
  background: var(--ai-accent-soft);
  border: 1px solid rgba(0, 113, 227, 0.22);
  border-left: 3px solid var(--ai-accent);
  border-radius: var(--ai-radius-sm);
  padding: 8px 34px 9px 12px;
  animation: ai-attached-in 260ms var(--ai-ease);
}

@keyframes ai-attached-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.ai-attached__badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ai-accent);
}

.ai-attached__badge svg {
  width: 12px;
  height: 12px;
}

.ai-attached__text {
  margin: 3px 0 0;
  font-size: 0.78rem;
  line-height: 1.45;
  color: var(--ai-text);
  /* clamp long questions to a few lines */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.ai-attached__remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ai-accent);
  cursor: pointer;
  transition: background 160ms var(--ai-ease);
}

.ai-attached__remove:hover {
  background: rgba(0, 113, 227, 0.14);
}

.ai-attached__remove svg {
  width: 13px;
  height: 13px;
}

/* Attached-question quote shown inside the sent user message */
.ai-msg__context {
  max-width: 85%;
  align-self: flex-end;
  margin-bottom: 4px;
  background: rgba(0, 113, 227, 0.06);
  border-left: 3px solid rgba(0, 113, 227, 0.4);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.76rem;
  line-height: 1.45;
  color: var(--ai-text-muted);
}

.ai-msg__context-label {
  display: block;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ai-accent);
  margin-bottom: 2px;
}

/* ============================================
   Streamed-answer column — stacks the (optional) thinking panel,
   the answer bubble and the action bar to the right of the avatar.
   ============================================ */
.ai-msg__col {
  flex: 1 1 auto;
  min-width: 0;
  max-width: calc(100% - 38px);   /* avatar (28) + gap (10) */
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.ai-msg__col .ai-msg__bubble {
  max-width: 100%;
}

.ai-msg__bubble[hidden] {
  display: none !important;
}

/* ============================================
   Per-answer action bar (copy + regenerate)
   ============================================ */
.ai-msg--ai {
  flex-wrap: wrap;              /* error/notice messages still wrap gracefully */
}

.ai-msg__actions {
  margin-top: 0;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.ai-copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  line-height: 1.2;
  color: var(--ai-text-muted);
  background: transparent;
  border: 1px solid var(--ai-border);
  border-radius: 8px;
  cursor: pointer;
  transition:
    background 160ms var(--ai-ease),
    color 160ms var(--ai-ease),
    border-color 160ms var(--ai-ease);
}

.ai-copy-btn:hover {
  background: var(--ai-accent-soft);
  color: var(--ai-accent);
  border-color: rgba(0, 113, 227, 0.28);
}

.ai-copy-btn:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-copy-btn.is-copied {
  color: #1a7f43;
  background: rgba(26, 127, 67, 0.08);
  border-color: rgba(26, 127, 67, 0.35);
}

.ai-copy-btn svg {
  width: 13px;
  height: 13px;
}

/* Regenerate — same pill as Copy, sitting to its right. */
.ai-regen-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  line-height: 1.2;
  color: var(--ai-text-muted);
  background: transparent;
  border: 1px solid var(--ai-border);
  border-radius: 8px;
  cursor: pointer;
  transition:
    background 160ms var(--ai-ease),
    color 160ms var(--ai-ease),
    border-color 160ms var(--ai-ease);
}

.ai-regen-btn:hover {
  background: var(--ai-accent-soft);
  color: var(--ai-accent);
  border-color: rgba(0, 113, 227, 0.28);
}

.ai-regen-btn:focus-visible {
  outline: 2px solid var(--ai-accent);
  outline-offset: 1px;
}

.ai-regen-btn svg {
  width: 13px;
  height: 13px;
}

/* ============================================
   Stop button — the send button while a reply streams
   ============================================ */
.ai-send-btn.is-stop {
  background: var(--ai-text);
  box-shadow: 0 4px 12px -4px rgba(20, 24, 40, 0.45);
}

.ai-send-btn.is-stop::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--ai-accent);
  opacity: 0.5;
  animation: ai-stop-pulse 1.4s var(--ai-ease) infinite;
}

.ai-send-btn {
  position: relative;   /* anchor the pulse ring */
}

@keyframes ai-stop-pulse {
  0%   { transform: scale(0.85); opacity: 0.55; }
  70%  { transform: scale(1.15); opacity: 0; }
  100% { transform: scale(1.15); opacity: 0; }
}

/* ============================================
   Thinking process (deep-think) panel
   ============================================ */
.ai-thinking {
  width: 100%;
  border: 1px solid rgba(88, 86, 214, 0.22);
  border-radius: var(--ai-radius-md);
  background: rgba(88, 86, 214, 0.05);
  overflow: hidden;
  animation: aiMsgSlideIn 240ms var(--ai-ease) both;
}

.ai-thinking__toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  padding: 7px 10px;
  background: transparent;
  border: 0;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--ai-accent-2);
  text-align: left;
}

.ai-thinking__toggle > svg:first-of-type {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.ai-thinking__label {
  flex: 1;
  min-width: 0;
}

/* Soft shimmer on the label while the model is still reasoning. */
.ai-thinking.is-active .ai-thinking__label {
  animation: ai-think-shimmer 1.5s ease-in-out infinite;
}

@keyframes ai-think-shimmer {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.55; }
}

.ai-thinking__chevron {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  transition: transform 200ms var(--ai-ease);
}

.ai-thinking.is-collapsed .ai-thinking__chevron {
  transform: rotate(-90deg);
}

.ai-thinking__body {
  padding: 4px 12px 10px;
  font-size: 0.8rem;
  line-height: 1.55;
  color: var(--ai-text-muted);
  white-space: pre-wrap;          /* preserve newlines while streaming plain text */
  word-wrap: break-word;
  overflow-wrap: anywhere;
  border-top: 1px solid var(--ai-border-soft);
}

/* Once rendered as markdown, let block elements flow normally. */
.ai-thinking__body.is-markdown {
  white-space: normal;
}

.ai-thinking__body.is-markdown > :first-child { margin-top: 0; }
.ai-thinking__body.is-markdown > :last-child  { margin-bottom: 0; }
.ai-thinking__body.is-markdown p { margin: 0 0 6px; }

.ai-thinking.is-collapsed .ai-thinking__body {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .ai-send-btn.is-stop::after,
  .ai-thinking.is-active .ai-thinking__label,
  .ai-thinking {
    animation: none !important;
  }
}
