/**
 * 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: 28px;
  right: 28px;
  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: 440px;
  height: 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);
}

/* ============================================
   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;
}

.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);
}

.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 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;
  text-align: left;
  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: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;
  border-radius: 5px;
  font-family: var(--font-family-mono, 'SF Mono', 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;
}

.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
   ============================================ */
.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 {
  margin: 6px 4px 0;
  font-size: 0.68rem;
  color: var(--ai-text-soft);
  text-align: center;
  line-height: 1.4;
}

.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: 400px;
    right: 20px;
    bottom: 92px;
  }
  .ai-fab {
    bottom: 22px;
    right: 22px;
    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: 16px;
    right: 16px;
    width: 52px;
    height: 52px;
  }
  .ai-fab__icon { width: 22px; height: 22px; }
  .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;
}
