/* ============================================================
   ai-chat.css — StudyMetrics AI Chat Interface
   Matches the existing violet-cobalt-cyan design system.
   ============================================================ */

/* ── AI Page Hero ─────────────────────────────────────────── */
.ai-hero {
  padding: var(--s7) 0 var(--s5);
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--border-soft);
}
.ai-hero .grid-bg {
  position: absolute; inset: 0;
  background-image:
    linear-gradient(var(--border-soft) 1px, transparent 1px),
    linear-gradient(90deg, var(--border-soft) 1px, transparent 1px);
  background-size: 56px 56px;
  opacity: .4;
  -webkit-mask-image: radial-gradient(110% 90% at 30% 0%, #000, transparent 72%);
  mask-image:         radial-gradient(110% 90% at 30% 0%, #000, transparent 72%);
  pointer-events: none;
}
.ai-hero-inner {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: var(--s5);
}
.ai-hero-icon {
  width: 52px; height: 52px;
  border-radius: 14px;
  background: var(--accent-dim);
  color: var(--accent-strong);
  display: grid; place-items: center;
  flex-shrink: 0;
}
.ai-hero-icon svg { width: 26px; height: 26px; }
.ai-hero-text h1 {
  font-size: var(--step-2xl);
  letter-spacing: -.03em;
  margin-bottom: var(--s2);
}
.ai-hero-text p {
  color: var(--ink-2);
  font-size: var(--step-md);
  line-height: 1.5;
  max-width: 62ch;
}
.ai-beta-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 20px;
  background: var(--accent-dim);
  color: var(--accent-strong);
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  margin-left: var(--s3);
  vertical-align: middle;
}

/* ── Chat Layout ──────────────────────────────────────────── */
.ai-layout {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: var(--s6);
  padding: var(--s6) 0 var(--s8);
  align-items: start;
}
.ai-main { min-width: 0; }

/* ── API Key Setup Banner ─────────────────────────────────── */
.ai-key-banner {
  background: var(--warn-dim);
  border: 1px solid oklch(.7 .15 65 / .3);
  border-radius: var(--r-md);
  padding: var(--s4) var(--s5);
  margin-bottom: var(--s5);
  display: flex;
  align-items: flex-start;
  gap: var(--s3);
}
.ai-key-banner svg { width: 20px; height: 20px; color: var(--warn); flex-shrink: 0; margin-top: 2px; }
.ai-key-banner .warn-text { font-size: var(--step-sm); color: var(--ink); line-height: 1.55; }
.ai-key-banner .warn-text b { color: var(--ink); }
.ai-key-banner .warn-text code {
  font-family: 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  font-size: .8em;
  background: oklch(.7 .15 65 / .15);
  padding: 1px 5px;
  border-radius: 4px;
}
.ai-key-form {
  display: flex;
  gap: var(--s3);
  margin-top: var(--s3);
  flex-wrap: wrap;
}
.ai-key-form .input { flex: 1; min-width: 200px; font-family: monospace; font-size: var(--step-sm); }
.ai-key-dismiss { font-size: var(--step-sm); color: var(--ink-3); cursor: pointer; text-decoration: underline; margin-top: var(--s2); display: inline-block; }
.ai-key-set-label {
  display: flex; align-items: center; gap: 6px;
  font-size: var(--step-sm); color: var(--ok); font-weight: 500;
  padding: var(--s3);
  background: var(--ok-dim);
  border: 1px solid oklch(.58 .13 155 / .2);
  border-radius: var(--r-md);
  margin-bottom: var(--s5);
}
.ai-key-set-label svg { width: 16px; height: 16px; }

/* ── Chat Window ──────────────────────────────────────────── */
.chat-window {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.chat-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s4) var(--s5);
  border-bottom: 1px solid var(--border-soft);
  background: var(--surface-2);
  gap: var(--s3);
}
.chat-toolbar-left {
  display: flex;
  align-items: center;
  gap: var(--s3);
}
.chat-status-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px var(--ok-dim);
  flex-shrink: 0;
}
.chat-status-dot.loading {
  background: var(--warn);
  box-shadow: 0 0 0 3px var(--warn-dim);
  animation: pulse-dot 1.2s ease-in-out infinite;
}
.chat-status-label {
  font-size: var(--step-sm);
  font-weight: 500;
  color: var(--ink-2);
}
.chat-clear-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--step-sm);
  font-weight: 500;
  color: var(--ink-3);
  padding: 6px 12px;
  border-radius: var(--r-md);
  border: 1px solid var(--border);
  transition: all .15s var(--ease);
}
.chat-clear-btn:hover { border-color: var(--danger-dim); color: var(--danger); background: var(--danger-dim); }
.chat-clear-btn svg { width: 14px; height: 14px; }

/* ── Messages Area ────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--s5);
  display: flex;
  flex-direction: column;
  gap: var(--s4);
  min-height: 420px;
  max-height: 520px;
  scroll-behavior: smooth;
}
.chat-messages::-webkit-scrollbar { width: 6px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Welcome State ─────────────────────────────────────────── */
.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  flex: 1;
  padding: var(--s7) var(--s5);
  gap: var(--s4);
}
.chat-welcome-icon {
  width: 64px; height: 64px;
  border-radius: 20px;
  background: var(--accent-dim);
  color: var(--accent-strong);
  display: grid; place-items: center;
}
.chat-welcome-icon svg { width: 32px; height: 32px; }
.chat-welcome h3 { font-size: var(--step-lg); letter-spacing: -.02em; }
.chat-welcome p { font-size: var(--step-sm); color: var(--ink-2); max-width: 38ch; line-height: 1.6; }
.chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
  justify-content: center;
  margin-top: var(--s2);
}
.chat-suggestion {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: var(--step-sm);
  color: var(--ink-2);
  background: var(--surface-2);
  cursor: pointer;
  transition: all .15s var(--ease);
  text-align: left;
}
.chat-suggestion:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
  background: var(--accent-dim);
}

/* ── Message Bubbles ──────────────────────────────────────── */
.msg {
  display: flex;
  gap: var(--s3);
  align-items: flex-start;
  animation: msgIn .25s var(--ease);
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}
.msg.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: grid; place-items: center;
  flex-shrink: 0;
  font-size: .7rem;
  font-weight: 700;
}
.msg.user .msg-avatar {
  background: var(--ink);
  color: var(--bg);
}
.msg.assistant .msg-avatar {
  background: var(--accent-dim);
  color: var(--accent-strong);
}
.msg.assistant .msg-avatar svg { width: 16px; height: 16px; }

.msg-body { flex: 1; max-width: min(82%, 600px); }
.msg.user .msg-body { align-items: flex-end; display: flex; flex-direction: column; }

.msg-bubble {
  padding: var(--s3) var(--s4);
  border-radius: var(--r-md);
  font-size: var(--step-sm);
  line-height: 1.65;
  word-wrap: break-word;
}
.msg.user .msg-bubble {
  background: var(--ink);
  color: var(--bg);
  border-radius: var(--r-md) var(--r-md) 4px var(--r-md);
}
.msg.assistant .msg-bubble {
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md) var(--r-md) var(--r-md) 4px;
  color: var(--ink);
}

/* ── Markdown rendering inside bubbles ────────────────────── */
.msg-bubble strong { font-weight: 600; }
.msg-bubble em { font-style: italic; }
.msg-bubble code {
  font-family: 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
  font-size: .82em;
  background: oklch(.9 .008 74);
  padding: 1px 5px;
  border-radius: 4px;
  color: var(--accent-strong);
}
.msg.user .msg-bubble code { background: oklch(.35 .02 55); color: oklch(.9 .01 74); }
.msg-bubble pre {
  background: var(--ink);
  color: var(--bg);
  border-radius: var(--r-md);
  padding: var(--s4);
  overflow-x: auto;
  margin: var(--s3) 0;
  font-size: .8em;
  font-family: 'Fira Code', 'Consolas', monospace;
}
.msg-bubble ul, .msg-bubble ol {
  padding-left: var(--s5);
  margin: var(--s2) 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.msg-bubble li { line-height: 1.6; }
.msg-bubble p { margin: var(--s2) 0; }
.msg-bubble p:first-child { margin-top: 0; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: -.02em;
  margin: var(--s3) 0 var(--s2);
}

/* ── Message Actions (copy button) ────────────────────────── */
.msg-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
  opacity: 0;
  transition: opacity .15s var(--ease);
}
.msg:hover .msg-actions { opacity: 1; }
.msg-copy-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: .72rem;
  font-weight: 500;
  color: var(--ink-3);
  padding: 4px 9px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  transition: all .15s var(--ease);
  cursor: pointer;
}
.msg-copy-btn:hover { color: var(--accent-strong); border-color: var(--accent); background: var(--accent-dim); }
.msg-copy-btn svg { width: 12px; height: 12px; }

/* ── Typing Indicator ─────────────────────────────────────── */
.typing-indicator .msg-bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: var(--s3) var(--s5);
  min-width: 60px;
}
.typing-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--ink-3);
  animation: typing-bounce .9s ease-in-out infinite;
}
.typing-dot:nth-child(2) { animation-delay: .15s; }
.typing-dot:nth-child(3) { animation-delay: .3s; }
@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .5; }
  30%            { transform: translateY(-6px); opacity: 1; }
}

/* ── Error Message ────────────────────────────────────────── */
.chat-error {
  display: flex;
  align-items: flex-start;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  background: var(--danger-dim);
  border: 1px solid oklch(.55 .19 25 / .2);
  border-radius: var(--r-md);
  font-size: var(--step-sm);
  color: var(--danger);
  animation: msgIn .25s var(--ease);
}
.chat-error svg { width: 16px; height: 16px; flex-shrink: 0; margin-top: 2px; }

/* ── Input Area ───────────────────────────────────────────── */
.chat-input-area {
  padding: var(--s4) var(--s5);
  border-top: 1px solid var(--border-soft);
  background: var(--surface-2);
}
.chat-input-row {
  display: flex;
  gap: var(--s3);
  align-items: flex-end;
}
.chat-textarea {
  flex: 1;
  padding: 11px var(--s4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-size: var(--step-sm);
  font-family: inherit;
  color: var(--ink);
  resize: none;
  min-height: 46px;
  max-height: 160px;
  line-height: 1.55;
  transition: border-color .16s var(--ease), box-shadow .16s;
  overflow-y: auto;
}
.chat-textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.chat-textarea::placeholder { color: var(--ink-3); }
.chat-textarea:disabled { opacity: .5; cursor: not-allowed; }

.chat-send-btn {
  width: 46px; height: 46px;
  border-radius: var(--r-md);
  background: var(--ink);
  color: var(--bg);
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: background .15s var(--ease), transform .12s;
  border: none;
  cursor: pointer;
}
.chat-send-btn:hover:not(:disabled) { background: var(--accent-strong); }
.chat-send-btn:active:not(:disabled) { transform: scale(.94); }
.chat-send-btn:disabled { opacity: .4; cursor: not-allowed; }
.chat-send-btn svg { width: 18px; height: 18px; }
.chat-send-btn .spin { animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.chat-input-hint {
  margin-top: var(--s2);
  font-size: var(--step-xs);
  color: var(--ink-3);
}

/* ── Sidebar ──────────────────────────────────────────────── */
.ai-rail {
  position: sticky;
  top: 88px;
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}
.ai-info-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s5);
}
.ai-info-card h3 {
  font-size: var(--step-base);
  margin-bottom: var(--s3);
  letter-spacing: -.02em;
}
.ai-topic-list {
  list-style: none;
  padding: 0; margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.ai-topic-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--step-sm);
  color: var(--ink-2);
}
.ai-topic-list li svg { width: 14px; height: 14px; color: var(--ok); flex-shrink: 0; }

.ai-tool-links {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: var(--s5);
}
.ai-tool-links h3 { font-size: var(--step-base); margin-bottom: var(--s3); letter-spacing: -.02em; }
.ai-tool-link {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: 9px var(--s3);
  border-radius: var(--r-md);
  font-size: var(--step-sm);
  font-weight: 500;
  color: var(--ink-2);
  transition: all .15s var(--ease);
  margin-bottom: 2px;
}
.ai-tool-link:hover {
  background: var(--accent-dim);
  color: var(--accent-strong);
}
.ai-tool-link svg { width: 15px; height: 15px; opacity: .6; }

/* ── Pulse animation ──────────────────────────────────────── */
@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50%       { opacity: .4; }
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 900px) {
  .ai-layout { grid-template-columns: 1fr; }
  .ai-rail { position: static; }
  .chat-messages { min-height: 360px; max-height: 420px; }
}
@media (max-width: 600px) {
  .ai-hero-inner { flex-direction: column; gap: var(--s3); }
  .chat-suggestions { flex-direction: column; align-items: stretch; }
  .chat-suggestion { text-align: center; }
  .chat-messages { min-height: 300px; max-height: 380px; }
  .msg-bubble { font-size: .85rem; }
}

/* ── Coach Topic Selector (Phase 8.3) ──────────────────────── */
.coach-topics {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
  margin-bottom: var(--s4);
}

.coach-topic-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 20px;
  font-size: var(--step-sm);
  font-weight: 500;
  font-family: inherit;
  color: var(--ink-2);
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  cursor: pointer;
  transition: background .15s var(--ease), color .15s, border-color .15s, transform .1s;
  white-space: nowrap;
}
.coach-topic-btn:hover {
  background: var(--accent-dim);
  color: var(--accent-strong);
  border-color: oklch(.6 .19 32 / .3);
}
.coach-topic-btn.active {
  background: var(--accent-dim);
  color: var(--accent-strong);
  border-color: oklch(.6 .19 32 / .5);
  font-weight: 600;
}
.coach-topic-btn:active { transform: scale(.97); }
.coach-topic-icon { font-size: 1em; line-height: 1; }
.coach-topic-label { line-height: 1.2; }

/* ── Coach Context Card (sidebar) ───────────────────────────── */
.coach-context-card {
  display: none;
  align-items: center;
  gap: var(--s3);
  background: var(--accent-dim);
  border: 1.5px solid oklch(.6 .19 32 / .25);
  border-radius: var(--r-md);
  padding: var(--s4);
  margin-bottom: var(--s4);
  animation: coachCardIn .2s var(--ease);
}
@keyframes coachCardIn {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: none; }
}
.coach-card-icon {
  font-size: 1.6rem;
  line-height: 1;
  flex-shrink: 0;
}
.coach-card-body strong {
  display: block;
  font-size: var(--step-sm);
  font-weight: 600;
  color: var(--accent-strong);
  margin-bottom: 2px;
}
.coach-card-body p {
  font-size: .75rem;
  color: var(--ink-2);
  line-height: 1.5;
  margin: 0;
}

/* ── Coach injected tool links ──────────────────────────────── */
.coach-tool-links {
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: coachCardIn .18s var(--ease);
}

/* ── Updated suggestion chips (larger, no emoji prefix spacing) */
.chat-suggestion {
  text-align: left;
  font-size: var(--step-sm);
  line-height: 1.4;
}

/* ── Responsive adjustments ─────────────────────────────────── */
@media (max-width: 640px) {
  .coach-topics {
    gap: 6px;
  }
  .coach-topic-btn {
    font-size: .72rem;
    padding: 6px 11px;
  }
}
