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

/* Resize handles — 4 edges + 4 corners. Invisible; the cursor change is the
   affordance. They sit inside the panel (so .is-dragged + overflow:hidden
   still bound them) and above sibling content via z-index. The header is the
   top-most visual element but the N/NW/NE handles overlay its first 6px to
   give the resize a chance before drag wins. */
.ai-chat-resize {
  position: absolute;
  z-index: 5;
  background: transparent;
  touch-action: none;
}
.ai-chat-resize-n  { top: 0;    left: 12px;  right: 12px;  height: 6px;  cursor: ns-resize; }
.ai-chat-resize-s  { bottom: 0; left: 12px;  right: 12px;  height: 6px;  cursor: ns-resize; }
.ai-chat-resize-e  { top: 12px; right: 0;    bottom: 12px; width:  6px;  cursor: ew-resize; }
.ai-chat-resize-w  { top: 12px; left:  0;    bottom: 12px; width:  6px;  cursor: ew-resize; }
.ai-chat-resize-nw { top: 0;    left:  0;    width: 14px;  height: 14px; cursor: nwse-resize; }
.ai-chat-resize-ne { top: 0;    right: 0;    width: 14px;  height: 14px; cursor: nesw-resize; }
.ai-chat-resize-sw { bottom: 0; left:  0;    width: 14px;  height: 14px; cursor: nesw-resize; }
.ai-chat-resize-se { bottom: 0; right: 0;    width: 14px;  height: 14px; cursor: nwse-resize; }

/* 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;
}
/* Plot attachments rendered inline. Stay within the bubble width and sit
 * as a sibling of the .ai-chat-text node — see appendAssistantBubble in
 * ai_chat.js for the DOM structure. */
.ai-chat-msg.assistant img {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: 6px;
  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;
}

/* ── Help: "?" button in header + page-level modal ────────────────────────── */
.ai-chat-help-btn {
  background: rgba(255, 255, 255, 0.18);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 50%;
  width: 22px;
  height: 22px;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
  vertical-align: middle;
}
.ai-chat-help-btn:hover { background: rgba(255, 255, 255, 0.32); }

/* Page-level overlay — covers the whole site, sits above the chat panel. */
.ai-chat-help-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 45, 0.55);
  z-index: 10000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.ai-chat-help-overlay.is-open { display: flex; }

.ai-chat-help-modal {
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 760px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: #2c3e50;
}
.ai-chat-help-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: linear-gradient(135deg, #0066cc 0%, #00a3a3 100%);
  color: white;
}
.ai-chat-help-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}
.ai-chat-help-close {
  background: transparent;
  color: white;
  border: none;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.ai-chat-help-close:hover { opacity: 0.8; }

.ai-chat-help-body {
  padding: 18px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  font-size: 13px;
  line-height: 1.5;
}
@media (max-width: 640px) {
  .ai-chat-help-body { grid-template-columns: 1fr; }
}
.ai-chat-help-col h3 {
  font-size: 14px;
  margin: 0 0 12px 0;
  font-weight: 700;
  color: #1f2c3d;
}
.ai-chat-help-section { margin-bottom: 14px; }
.ai-chat-help-section h4 {
  margin: 0 0 6px 0;
  font-size: 11px;
  font-weight: 700;
  color: #50627a;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.ai-chat-help-chips {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.ai-chat-help-chip {
  text-align: left;
  background: #f0f4f8;
  border: 1px solid #d1dbe5;
  border-radius: 8px;
  padding: 7px 10px;
  font-size: 13px;
  color: #2c3e50;
  cursor: pointer;
  font-family: inherit;
  line-height: 1.35;
  width: 100%;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
}
.ai-chat-help-chip:hover {
  background: #e3edf7;
  border-color: #0066cc;
}
.ai-chat-help-chip:active { transform: translateY(1px); }
.ai-chat-help-avoid {
  margin: 0;
  padding-left: 18px;
}
.ai-chat-help-avoid li {
  margin-bottom: 8px;
}
.ai-chat-help-avoid strong { color: #2c3e50; }
.ai-chat-help-avoid em {
  color: #6f7e92;
  font-style: italic;
}

/* 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; }
  .ai-chat-resize { display: none; }
}
