/* ───────────────────────── MHW AI Chat Widget ─────────────────────────────
 * Self-contained widget. All selectors prefixed with .ai-chat- to avoid
 * collisions with the existing site theme.
 * --------------------------------------------------------------------------*/

/* Floating launcher button — sits directly above #return-to-top
   (#return-to-top is fixed at bottom: 5px, height 35px → its top edge is at 40px;
   leave an 8px gap above it.) */
.ai-chat-button {
  position: fixed;
  bottom: 48px;
  right: 5px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: linear-gradient(135deg, #0066cc 0%, #00a3a3 100%);
  color: white;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  z-index: 9998;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.ai-chat-button:hover {
  transform: scale(1.06);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.24);
}
.ai-chat-button[aria-expanded="true"] {
  display: none;
}

/* Modal panel — initial position bottom-right; user can drag elsewhere via the header. */
.ai-chat-panel {
  position: fixed;
  bottom: 48px;
  right: 5px;
  width: 420px;
  max-width: calc(100vw - 16px);
  height: 620px;
  max-height: calc(100vh - 64px);
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 9999;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: #2c3e50;
}
/* When the user has dragged the panel, JS sets left/top inline; clear bottom/right
   so the panel sticks to the dragged position. */
.ai-chat-panel.is-dragged {
  bottom: auto;
  right: auto;
}
.ai-chat-panel.is-open {
  display: flex;
}

/* Header bar — also acts as the drag handle. */
.ai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: linear-gradient(135deg, #0066cc 0%, #00a3a3 100%);
  color: white;
  cursor: grab;
  user-select: none;
}
.ai-chat-header:active { cursor: grabbing; }
.ai-chat-header.is-dragging { cursor: grabbing; }
/* Buttons and selects inside the header still feel like buttons, not draggable. */
.ai-chat-header .ai-chat-close,
.ai-chat-header .ai-chat-model-select { cursor: pointer; }
.ai-chat-header-title {
  font-weight: 600;
  font-size: 15px;
}
.ai-chat-header-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.ai-chat-model-select {
  background: rgba(255, 255, 255, 0.18);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  outline: none;
  cursor: pointer;
}
.ai-chat-model-select option {
  color: #2c3e50;
}
.ai-chat-close {
  background: transparent;
  color: white;
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.ai-chat-close:hover { opacity: 0.8; }

/* Message area */
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: #f7f9fc;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.ai-chat-empty {
  color: #7a8694;
  font-size: 13px;
  text-align: center;
  margin: 24px 12px;
  line-height: 1.5;
}
.ai-chat-empty code {
  background: #e8edf3;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 12px;
}

/* Bubbles */
.ai-chat-msg {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: 12px;
  line-height: 1.5;
  word-wrap: break-word;
}
.ai-chat-msg.user {
  align-self: flex-end;
  background: #0066cc;
  color: white;
  border-bottom-right-radius: 4px;
}
.ai-chat-msg.assistant {
  align-self: flex-start;
  background: white;
  color: #2c3e50;
  border: 1px solid #e1e7ee;
  border-bottom-left-radius: 4px;
}
.ai-chat-msg.error {
  align-self: stretch;
  background: #fff3f3;
  color: #c0392b;
  border: 1px solid #f4c1c1;
  font-size: 13px;
}

/* Markdown styling inside assistant bubbles */
.ai-chat-msg.assistant p { margin: 0 0 8px 0; }
.ai-chat-msg.assistant p:last-child { margin-bottom: 0; }
.ai-chat-msg.assistant h1,
.ai-chat-msg.assistant h2,
.ai-chat-msg.assistant h3 {
  margin: 10px 0 6px 0;
  font-weight: 600;
  line-height: 1.3;
}
.ai-chat-msg.assistant h1 { font-size: 16px; }
.ai-chat-msg.assistant h2 { font-size: 15px; }
.ai-chat-msg.assistant h3 { font-size: 14px; }
.ai-chat-msg.assistant ul,
.ai-chat-msg.assistant ol { margin: 4px 0 8px 0; padding-left: 20px; }
.ai-chat-msg.assistant li { margin-bottom: 2px; }
.ai-chat-msg.assistant code {
  background: #eef2f6;
  padding: 1px 5px;
  border-radius: 3px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
}
.ai-chat-msg.assistant pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 12px;
  margin: 6px 0;
}
.ai-chat-msg.assistant pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}
.ai-chat-msg.assistant a {
  color: #0066cc;
  text-decoration: underline;
}
.ai-chat-msg.assistant table {
  border-collapse: collapse;
  margin: 6px 0;
  font-size: 13px;
}
.ai-chat-msg.assistant th,
.ai-chat-msg.assistant td {
  border: 1px solid #d1d9e3;
  padding: 4px 8px;
  text-align: left;
}
.ai-chat-msg.assistant th { background: #f0f4f8; font-weight: 600; }
.ai-chat-msg.assistant blockquote {
  border-left: 3px solid #0066cc;
  margin: 6px 0;
  padding: 0 10px;
  color: #5a6678;
}
.ai-chat-msg.assistant img {
  max-width: 100%;
  border-radius: 6px;
  margin: 6px 0;
}

/* Tool-activity pill (shown while a tool runs) */
.ai-chat-tool-pill {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #e8f2ff;
  color: #0066cc;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-style: italic;
  border: 1px solid #c5dcf7;
}
.ai-chat-tool-pill::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #0066cc;
  animation: ai-chat-pulse 1.2s ease-in-out infinite;
}
@keyframes ai-chat-pulse {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50%      { opacity: 1;   transform: scale(1.25); }
}

/* Input area */
.ai-chat-input-wrap {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: white;
  border-top: 1px solid #e1e7ee;
  align-items: flex-end;
}
.ai-chat-input {
  flex: 1;
  resize: none;
  border: 1px solid #d1d9e3;
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 14px;
  min-height: 36px;
  max-height: 120px;
  outline: none;
  color: #2c3e50;
  line-height: 1.4;
}
.ai-chat-input:focus {
  border-color: #0066cc;
  box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.15);
}
.ai-chat-send {
  background: #0066cc;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 0 16px;
  height: 36px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  transition: background 0.15s ease;
}
.ai-chat-send:hover:not(:disabled) { background: #0055aa; }
.ai-chat-send:disabled {
  background: #b0bec5;
  cursor: not-allowed;
}

/* Mobile */
@media (max-width: 480px) {
  .ai-chat-panel {
    bottom: 0;
    right: 0;
    width: 100%;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
  }
  .ai-chat-panel.is-dragged {
    /* On mobile we don't honour drag — keep the panel full-screen. */
    top: 0 !important;
    left: 0 !important;
    bottom: 0;
    right: 0;
  }
  .ai-chat-button {
    bottom: 48px;
    right: 5px;
  }
  .ai-chat-header { cursor: default; }
}
