/* ENGDOCS-002 — Browser docs-grounded chat UI */

#ai-chat-launcher {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: #0F4FBF;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  z-index: 9998;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
#ai-chat-launcher:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(0,0,0,0.30); }
#ai-chat-launcher:focus { outline: 3px solid #94B5F1; outline-offset: 2px; }

#ai-chat-panel {
  /* Font scale — body font-size; header / chips / code derive from this. */
  --chat-fs-body: 13px;
  --chat-fs-header: calc(var(--chat-fs-body) + 2px);
  --chat-fs-small: calc(var(--chat-fs-body) - 2px);
  --chat-fs-code: calc(var(--chat-fs-body) - 1.5px);

  position: fixed;
  right: 24px;
  bottom: 96px;
  width: 620px;
  max-width: calc(100vw - 32px);
  height: 720px;
  max-height: calc(100vh - 120px);
  background: #fff;
  border: 1px solid #d8dde6;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.18);
  z-index: 9997;
  display: none;
  flex-direction: column;
  overflow: hidden;
  font-family: 'Outfit', system-ui, sans-serif;
  min-width: 360px;
  min-height: 420px;
}

/* Custom 4-corner resize handles. Native CSS `resize: both` only gives a
   bottom-right handle, so we replace it with four 14px grab squares. */
.ai-chat-resize-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  z-index: 2;
  background: transparent;
  touch-action: none;
}
.ai-chat-resize-nw { top: 0; left: 0; cursor: nwse-resize; }
.ai-chat-resize-ne { top: 0; right: 0; cursor: nesw-resize; }
.ai-chat-resize-sw { bottom: 0; left: 0; cursor: nesw-resize; }
.ai-chat-resize-se { bottom: 0; right: 0; cursor: nwse-resize; }
/* Faint visual cue on bottom-right (most-used corner) only. */
.ai-chat-resize-se::after {
  content: '';
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 8px;
  height: 8px;
  border-right: 2px solid rgba(15, 79, 191, 0.35);
  border-bottom: 2px solid rgba(15, 79, 191, 0.35);
  border-bottom-right-radius: 2px;
}
/* In large/full modes, handles are hidden (panel snaps to preset dims). */
#ai-chat-panel.size-large .ai-chat-resize-handle,
#ai-chat-panel.size-full .ai-chat-resize-handle { display: none; }
#ai-chat-panel[data-open="true"] { display: flex; }

/* Font scale classes — applied to #ai-chat-panel via JS. */
#ai-chat-panel.fs-small  { --chat-fs-body: 13px; }
#ai-chat-panel.fs-medium { --chat-fs-body: 15px; }
#ai-chat-panel.fs-large  { --chat-fs-body: 17px; }

/* Window-size mode classes — applied to #ai-chat-panel via JS. */
#ai-chat-panel.size-large {
  width: min(1100px, 90vw);
  height: 90vh;
  max-width: 100vw;
  max-height: 100vh;
  right: 24px;
  bottom: 24px;
  resize: none;
}
#ai-chat-panel.size-full {
  inset: 0;
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  border-radius: 0;
  resize: none;
}

#ai-chat-header {
  background: #0F4FBF;
  color: #fff;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: var(--chat-fs-header);
  font-weight: 600;
}
#ai-chat-header .title { display: flex; align-items: center; gap: 8px; flex: 0 1 auto; min-width: 0; }
#ai-chat-header .title > div:last-child { min-width: 0; overflow: hidden; }
#ai-chat-header .subtitle { font-size: var(--chat-fs-small); opacity: 0.85; font-weight: 400; }
#ai-chat-header .ai-chat-header-actions { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }

/* Display controls (font A−/A+, size cycle) in the header. */
.ai-chat-display-group { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.ai-chat-display-btn {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
  min-width: 28px;
  border-radius: 4px;
  letter-spacing: 0.02em;
  font-family: inherit;
}
.ai-chat-display-btn:hover:not(:disabled) { background: rgba(255,255,255,0.28); }
.ai-chat-display-btn:focus { outline: 2px solid #fff; outline-offset: 2px; }
.ai-chat-display-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.ai-chat-size-btn { font-size: 14px; padding: 3px 8px; }
#ai-chat-new-conv {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  line-height: 1;
  padding: 4px 10px;
  border-radius: 4px;
  letter-spacing: 0.02em;
}
#ai-chat-new-conv:hover { background: rgba(255,255,255,0.25); }
#ai-chat-new-conv:focus { outline: 2px solid #fff; outline-offset: 2px; }
.ai-chat-new-conv-notice {
  color: #6b7280 !important;
  font-style: italic;
  font-size: 12px !important;
  background: transparent !important;
  padding: 4px 0 !important;
}
#ai-chat-close {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 4px;
}
#ai-chat-close:hover { background: rgba(255,255,255,0.15); }
#ai-chat-close:focus { outline: 2px solid #fff; outline-offset: 2px; }

#ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #f7f8fa;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ai-chat-msg {
  max-width: 95%;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: var(--chat-fs-body);
  line-height: 1.5;
  word-wrap: break-word;
}
.ai-chat-msg.user { align-self: flex-end; background: #0F4FBF; color: #fff; white-space: pre-wrap; }
.ai-chat-msg.assistant { align-self: flex-start; background: #fff; border: 1px solid #e1e6ee; color: #1a1f2e; }
.ai-chat-msg.ai-chat-typing-msg { align-self: flex-start; background: #fff; border: 1px solid #e1e6ee; padding: 6px 12px; }
.ai-chat-msg.assistant.coverage-refused,
.ai-chat-msg.assistant.coverage-not_covered,
.ai-chat-msg.assistant.coverage-clarification_needed { border-color: #f59e0b; }
.ai-chat-msg.assistant.coverage-error { border-color: #dc2626; background: #fef2f2; }
.ai-chat-msg.system { align-self: stretch; text-align: center; color: #6b7280; font-size: 11px; }

/* Rendered markdown body inside an assistant message. Tight spacing so the
 * bubble stays compact even with multiple blocks. */
.ai-chat-msg-body { font-size: var(--chat-fs-body); line-height: 1.5; }
.ai-chat-msg-body > *:first-child { margin-top: 0; }
.ai-chat-msg-body > *:last-child { margin-bottom: 0; }
.ai-chat-msg-body h1,
.ai-chat-msg-body h2,
.ai-chat-msg-body h3,
.ai-chat-msg-body h4 {
  margin: 8px 0 4px 0;
  font-weight: 600;
  line-height: 1.3;
}
.ai-chat-msg-body h1 { font-size: 15px; }
.ai-chat-msg-body h2 { font-size: 14px; }
.ai-chat-msg-body h3 { font-size: 13px; }
.ai-chat-msg-body h4 { font-size: 12px; color: #4b5563; }
.ai-chat-msg-body p { margin: 6px 0; }
.ai-chat-msg-body ul,
.ai-chat-msg-body ol {
  margin: 6px 0;
  padding-left: 22px;
}
.ai-chat-msg-body li { margin: 2px 0; }
.ai-chat-msg-body li > p { margin: 0; }
.ai-chat-msg-body a {
  color: #0F4FBF;
  text-decoration: underline;
  word-break: break-word;
}
.ai-chat-msg-body a:hover { color: #0d40a3; }
.ai-chat-msg-body code {
  background: #eef2ff;
  color: #1e1b4b;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: 'Source Code Pro', 'Courier New', monospace;
  font-size: 11.5px;
  word-break: break-word;
}
.ai-chat-msg-body pre {
  background: #1f2937;
  color: #e5e7eb;
  padding: 10px 12px;
  border-radius: 6px;
  margin: 8px 0;
  overflow-x: auto;
  font-size: 11.5px;
  line-height: 1.45;
}
.ai-chat-msg-body pre code {
  background: transparent;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: 11.5px;
}
.ai-chat-msg-body strong { font-weight: 600; color: #111827; }
.ai-chat-msg-body em { font-style: italic; }
.ai-chat-msg-body blockquote {
  margin: 6px 0;
  padding: 4px 10px;
  border-left: 3px solid #c7d2fe;
  background: #eef2ff;
  color: #1e1b4b;
}
.ai-chat-msg-body hr {
  border: none;
  border-top: 1px solid #e1e6ee;
  margin: 10px 0;
}
.ai-chat-msg-body .table-wrap {
  margin: 8px 0;
  overflow-x: auto;
  border: 1px solid #e1e6ee;
  border-radius: 6px;
}
.ai-chat-msg-body table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  line-height: 1.4;
}
.ai-chat-msg-body th,
.ai-chat-msg-body td {
  border-bottom: 1px solid #e5e7eb;
  border-right: 1px solid #e5e7eb;
  padding: 6px 10px;
  text-align: left;
  vertical-align: top;
  word-break: break-word;
}
.ai-chat-msg-body th:last-child,
.ai-chat-msg-body td:last-child { border-right: none; }
.ai-chat-msg-body tbody tr:last-child td { border-bottom: none; }
.ai-chat-msg-body th {
  background: #eef2ff;
  color: #1e1b4b;
  font-weight: 600;
  white-space: nowrap;
}
.ai-chat-msg-body tbody tr:nth-child(even) { background: #f9fafb; }
.ai-chat-msg-body table code {
  font-size: 11px;
  white-space: normal;
}

.ai-chat-coverage-pill {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
  background: #e0e7ff;
  color: #1e40af;
}
.ai-chat-coverage-pill.coverage-answered { background: #dcfce7; color: #166534; }
.ai-chat-coverage-pill.coverage-partial { background: #fef3c7; color: #92400e; }
.ai-chat-coverage-pill.coverage-not_covered { background: #fef3c7; color: #92400e; }
.ai-chat-coverage-pill.coverage-clarification_needed { background: #fef3c7; color: #92400e; }
.ai-chat-coverage-pill.coverage-refused { background: #fee2e2; color: #991b1b; }
.ai-chat-coverage-pill.coverage-error { background: #fee2e2; color: #991b1b; }

.ai-chat-sources { margin-top: 8px; padding-top: 8px; border-top: 1px solid #e1e6ee; font-size: 12px; }
.ai-chat-sources .label { font-weight: 600; color: #475569; margin-bottom: 4px; }
.ai-chat-sources ul { list-style: none; margin: 0; padding: 0; }
.ai-chat-sources li { font-family: 'Source Code Pro', 'Courier New', monospace; font-size: 11px; color: #1f2937; padding: 2px 0; }

.ai-chat-feedback {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
}
.ai-chat-feedback button {
  font-size: 14px;
  background: transparent;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
}
.ai-chat-feedback button:hover { border-color: #0F4FBF; }
.ai-chat-feedback button.sent { background: #dcfce7; border-color: #86efac; color: #166534; }

/* Inline feedback form shown after thumbs-down — mirrors uw-rewrite FeedbackModal */
.ai-chat-feedback[data-expanded="true"] { flex-direction: column; align-items: stretch; gap: 0; }
.ai-chat-feedback-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid #FEE2E2;
  background: #FEF2F2;
  border-radius: 8px;
  width: 100%;
}
.ai-chat-feedback-form-title { font-size: 12px; font-weight: 600; color: #991B1B; }
.ai-chat-feedback-pills { display: flex; flex-wrap: wrap; gap: 6px; }
.ai-chat-feedback-pill {
  font-size: 11px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid #E5E7EB;
  background: #fff;
  color: #4B5563;
  cursor: pointer;
}
.ai-chat-feedback-pill:hover { background: #F3F4F6; }
.ai-chat-feedback-pill.selected { background: #FEE2E2; color: #B91C1C; border-color: #FCA5A5; }
.ai-chat-feedback-comment {
  width: 100%;
  font-family: inherit;
  font-size: 12px;
  padding: 8px;
  border: 1px solid #E5E7EB;
  border-radius: 6px;
  resize: vertical;
  min-height: 60px;
  box-sizing: border-box;
}
.ai-chat-feedback-comment:focus {
  outline: none;
  border-color: #DC2626;
  box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.15);
}
.ai-chat-feedback-actions { display: flex; justify-content: flex-end; gap: 6px; }
.ai-chat-feedback-btn {
  font-size: 11px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
}
.ai-chat-feedback-btn.cancel { background: transparent; color: #6B7280; }
.ai-chat-feedback-btn.cancel:hover { background: #F3F4F6; }
.ai-chat-feedback-btn.submit { background: #DC2626; color: #fff; }
.ai-chat-feedback-btn.submit:hover:not(:disabled) { background: #B91C1C; }
.ai-chat-feedback-btn.submit:disabled { opacity: 0.5; cursor: not-allowed; }
.ai-chat-feedback-thanks {
  font-size: 12px;
  font-weight: 500;
  color: #166534;
  background: #DCFCE7;
  border: 1px solid #86EFAC;
  padding: 4px 10px;
  border-radius: 6px;
}

#ai-chat-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px;
  background: #fff;
  border-top: 1px solid #e1e6ee;
}

/* Mode chip bar (Auto / Dev / QA / Product) — appears above the input */
#ai-chat-mode-bar {
  display: flex;
  gap: 6px;
  flex-basis: 100%;
  margin-bottom: 2px;
}
.ai-chat-mode-chip {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid #d1d5db;
  background: #fff;
  color: #4B5563;
  cursor: pointer;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.ai-chat-mode-chip:hover { background: #F3F4F6; }
.ai-chat-mode-chip.active {
  background: #0F4FBF;
  color: #fff;
  border-color: #0F4FBF;
}
.ai-chat-mode-chip:focus { outline: 2px solid #94B5F1; outline-offset: 2px; }

/* Suggested follow-up chips under the assistant message */
.ai-chat-followups {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ai-chat-followups .label {
  font-size: 11px;
  font-weight: 600;
  color: #6B7280;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.ai-chat-followup-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.ai-chat-followup-chip {
  font-size: 11px;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid #C7D2FE;
  background: #EEF2FF;
  color: #3730A3;
  cursor: pointer;
  text-align: left;
  line-height: 1.3;
}
.ai-chat-followup-chip:hover { background: #E0E7FF; border-color: #A5B4FC; }

/* Clarification chips — same widget, different color to signal "refine my question"
   rather than "go deeper". Amber/orange to match the existing freshness-warning hue. */
.ai-chat-clarify .label { color: #92400E; }
.ai-chat-clarify-chip {
  background: #FEF3C7;
  border-color: #FCD34D;
  color: #92400E;
}
.ai-chat-clarify-chip:hover {
  background: #FDE68A;
  border-color: #F59E0B;
}

/* Freshness pill on each source citation */
.ai-chat-source-fresh {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 999px;
  margin-left: 6px;
  border: 1px solid transparent;
  white-space: nowrap;
  vertical-align: middle;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  cursor: help;
}
.ai-chat-source-fresh-fresh { background: #DCFCE7; color: #166534; border-color: #86EFAC; }
.ai-chat-source-fresh-aging { background: #F3F4F6; color: #4B5563; border-color: #E5E7EB; }
.ai-chat-source-fresh-stale { background: #FEF3C7; color: #92400E; border-color: #FCD34D; }
#ai-chat-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  font-size: var(--chat-fs-body);
  resize: none;
  font-family: inherit;
  min-height: 36px;
  max-height: 100px;
}
#ai-chat-input:focus { outline: 2px solid #0F4FBF; outline-offset: 1px; border-color: #0F4FBF; }
#ai-chat-send {
  padding: 8px 14px;
  background: #0F4FBF;
  color: #fff;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: var(--chat-fs-body);
}
#ai-chat-send:hover { background: #0d40a3; }
#ai-chat-send:disabled { background: #9ca3af; cursor: not-allowed; }
#ai-chat-send:focus { outline: 2px solid #94B5F1; outline-offset: 2px; }

.ai-chat-typing { display: inline-flex; gap: 4px; padding: 8px 0; }
.ai-chat-typing span {
  display: inline-block;
  width: 6px;
  height: 6px;
  background: #9ca3af;
  border-radius: 50%;
  animation: ai-chat-bounce 1.2s infinite ease-in-out;
}
.ai-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
.ai-chat-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes ai-chat-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

@media (max-width: 680px) {
  #ai-chat-panel,
  #ai-chat-panel.size-small,
  #ai-chat-panel.size-large,
  #ai-chat-panel.size-full {
    inset: auto;
    right: 8px;
    left: 8px;
    bottom: 80px;
    width: auto;
    max-width: none;
    height: calc(100vh - 96px);
    resize: none;
    border-radius: 12px;
  }
  #ai-chat-launcher { right: 16px; bottom: 16px; }
  /* Window-size cycling and corner resize are meaningless on mobile. */
  .ai-chat-size-btn,
  .ai-chat-resize-handle { display: none; }
}

/* Phase 1.K — project badge on source chips so users can disambiguate
   cross-project hits (e.g. enterprise-api vs file-manager-api). */
.ai-chat-source-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: 'Source Code Pro', 'Consolas', monospace;
  font-size: 12px;
  line-height: 1.5;
}
.ai-chat-source-project {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 9999px;
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
  text-transform: lowercase;
  letter-spacing: 0.02em;
  background: #e0e7ff;
  color: #1e40af;
  flex: 0 0 auto;
}

/* Source citation link — wraps the project badge + path so the whole chip
   navigates to the underlying doc via Docsify's hash route. */
.ai-chat-source-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  text-decoration: none;
  color: inherit;
  cursor: pointer;
  min-width: 0;
  flex: 1 1 auto;
}
.ai-chat-source-link:hover,
.ai-chat-source-link:focus-visible {
  color: #1d4ed8;
  outline: none;
}
.ai-chat-source-link:hover .ai-chat-source-path,
.ai-chat-source-link:focus-visible .ai-chat-source-path {
  text-decoration: underline;
}
.ai-chat-source-link:hover .ai-chat-source-project,
.ai-chat-source-link:focus-visible .ai-chat-source-project {
  background: #c7d2fe;
}
.ai-chat-source-path {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

/* External GitHub link next to the source chip path. Opens raw file on
   GitHub in a new tab. Hidden unless window.AI_CHAT_GITHUB_BASE is set. */
.ai-chat-source-gh {
  margin-left: 6px;
  padding: 1px 6px;
  font-family: 'Outfit', system-ui, sans-serif;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #4b5563;
  background: #fff;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  text-decoration: none;
  flex: 0 0 auto;
  line-height: 1.4;
  cursor: pointer;
}
.ai-chat-source-gh:hover,
.ai-chat-source-gh:focus-visible {
  color: #0F4FBF;
  border-color: #0F4FBF;
  text-decoration: none;
}
