/* ==========================================================================
   Maxer Photonics — Chatbot widget
   Additive-only: reuses the site's existing design tokens (css/tokens.css)
   exactly, introduces no new colors/fonts/radii. Loaded on every page
   alongside the existing stylesheets; does not modify any of them.
   ========================================================================== */

#mx-chat-root {
  position: fixed;
  right: clamp(14px, 3vw, 24px);
  bottom: var(--mx-chat-bottom-offset, 20px);
  z-index: 1990; /* above normal content, below the mobile nav-overlay (2050) and site header (100) — but see JS: repositions above the GDPR banner (2000) when it is visible, rather than sitting under it. */
  font-family: var(--font-body);
  transition: bottom var(--dur-base) var(--ease-out);
}

/* ---------------------------------- Launcher button ---------------------- */
#mx-chat-toggle {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--ink-2);
  border: 1px solid var(--line-1);
  color: var(--text-on-dark);
  cursor: pointer;
  box-shadow: var(--shadow-card);
  transition: border-color var(--dur-fast) var(--ease-standard), transform var(--dur-fast) var(--ease-standard);
}
#mx-chat-toggle:hover { border-color: var(--accent-cyan); transform: translateY(-2px); }
#mx-chat-toggle:focus-visible { outline: 2px solid var(--accent-cyan); outline-offset: 3px; }
#mx-chat-toggle svg { width: 24px; height: 24px; stroke: currentColor; fill: none; stroke-width: 1.8; }
#mx-chat-toggle .mx-chat-icon-close { display: none; }
#mx-chat-root.is-open #mx-chat-toggle .mx-chat-icon-chat { display: none; }
#mx-chat-root.is-open #mx-chat-toggle .mx-chat-icon-close { display: block; }

/* Small unread/attention dot on first load — purely decorative, subtle. */
#mx-chat-toggle .mx-chat-dot {
  position: absolute;
  top: 4px; right: 4px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--accent-cyan);
  border: 2px solid var(--ink-0);
}
#mx-chat-root.is-open #mx-chat-toggle .mx-chat-dot,
#mx-chat-root.mx-chat-dismissed #mx-chat-toggle .mx-chat-dot { display: none; }

/* ---------------------------------- Panel --------------------------------- */
#mx-chat-panel {
  position: absolute;
  right: 0;
  bottom: 68px;
  width: 380px;
  max-width: calc(100vw - 28px);
  height: min(600px, calc(100vh - var(--mx-chat-bottom-offset, 20px) - 96px));
  max-height: 600px;
  background: var(--ink-1);
  border: 1px solid var(--line-0);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transform: translateY(12px) scale(0.98);
  transform-origin: bottom right;
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out), visibility 0s var(--dur-base);
}
#mx-chat-root.is-open #mx-chat-panel {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
  transition: opacity var(--dur-base) var(--ease-out), transform var(--dur-base) var(--ease-out), visibility 0s;
}
/* Minimized = collapsed to just the header bar (which keeps its own
   restore/close controls visible and clickable), not the whole panel —
   the header must never disappear or there'd be no way back. */
#mx-chat-root.is-minimized #mx-chat-panel { height: auto; max-height: none; }
#mx-chat-root.is-minimized #mx-chat-panel__header { border-bottom: none; }

@media (prefers-reduced-motion: reduce) {
  #mx-chat-panel, #mx-chat-root { transition: none; }
}

/* ---------------------------------- Panel header --------------------------- */
#mx-chat-panel__header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.85rem 0.9rem;
  border-bottom: 1px solid var(--line-0);
  background: var(--ink-2);
}
#mx-chat-panel__header .mx-chat-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: var(--ink-0);
  border: 1px solid var(--line-1);
  display: flex; align-items: center; justify-content: center;
  flex: none;
}
#mx-chat-panel__header .mx-chat-avatar svg { width: 16px; height: 16px; stroke: var(--accent-cyan); fill: none; stroke-width: 1.8; }
#mx-chat-panel__header .mx-chat-title { flex: 1; min-width: 0; }
#mx-chat-panel__header .mx-chat-title strong {
  display: block; font-family: var(--font-display); font-weight: 700; font-size: 0.92rem; color: var(--text-on-dark);
}
#mx-chat-panel__header .mx-chat-title span {
  display: flex; align-items: center; gap: 0.35rem;
  font-size: 0.72rem; color: var(--text-on-dark-faint);
}
#mx-chat-panel__header .mx-chat-title span::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%; background: var(--accent-cyan); flex: none;
}
#mx-chat-panel__header .mx-chat-header-actions { display: flex; align-items: center; gap: 0.25rem; flex: none; }
#mx-chat-panel__header button {
  width: 28px; height: 28px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-on-dark-dim);
  transition: background var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}
#mx-chat-panel__header button:hover { background: var(--ink-3); color: var(--text-on-dark); }
#mx-chat-panel__header button svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.8; }

/* ---------------------------------- Messages ------------------------------- */
#mx-chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
#mx-chat-root.is-minimized #mx-chat-messages { display: none; }

.mx-chat-msg {
  max-width: 86%;
  padding: 0.55rem 0.75rem;
  border-radius: var(--radius-md);
  font-size: 0.84rem;
  line-height: 1.45;
  word-wrap: break-word;
}
.mx-chat-msg--bot {
  align-self: flex-start;
  background: var(--ink-2);
  border: 1px solid var(--line-0);
  color: var(--text-on-dark);
  border-bottom-left-radius: 2px;
}
.mx-chat-msg--user {
  align-self: flex-end;
  background: var(--accent-blue-dim);
  border: 1px solid rgba(57, 104, 255, 0.3);
  color: var(--text-on-dark);
  border-bottom-right-radius: 2px;
}
.mx-chat-msg--error {
  align-self: flex-start;
  background: var(--ink-2);
  border: 1px solid var(--line-1);
  color: var(--text-on-dark-dim);
}
.mx-chat-msg a { color: var(--accent-cyan); text-decoration: underline; text-underline-offset: 2px; }

.mx-chat-msg--typing { display: flex; align-items: center; gap: 4px; padding: 0.7rem 0.85rem; }
.mx-chat-msg--typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-on-dark-faint);
  animation: mx-chat-bounce 1.1s infinite ease-in-out;
}
.mx-chat-msg--typing span:nth-child(2) { animation-delay: 0.15s; }
.mx-chat-msg--typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes mx-chat-bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-3px); opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .mx-chat-msg--typing span { animation: none; opacity: 0.8; }
}

/* ---------------------------------- Quick links / suggestions -------------- */
.mx-chat-suggestions { display: flex; flex-wrap: wrap; gap: 0.4rem; padding: 0 0.9rem 0.7rem; flex: none; }
.mx-chat-suggestions[hidden] { display: none; }
#mx-chat-root.is-minimized .mx-chat-suggestions { display: none; }
.mx-chat-suggestion {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.02em;
  color: var(--text-on-dark-dim);
  background: var(--ink-2);
  border: 1px solid var(--line-0);
  border-radius: 999px;
  padding: 0.35rem 0.65rem;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}
.mx-chat-suggestion:hover { border-color: var(--accent-cyan); color: var(--text-on-dark); }

/* ---------------------------------- Input row ------------------------------ */
#mx-chat-form {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.7rem;
  border-top: 1px solid var(--line-0);
  background: var(--ink-1);
}
#mx-chat-root.is-minimized #mx-chat-form { display: none; }
#mx-chat-input {
  flex: 1;
  resize: none;
  max-height: 96px;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line-1);
  border-radius: var(--radius-sm);
  background: var(--ink-0);
  color: var(--text-on-dark);
  font-family: var(--font-body);
  font-size: 0.84rem;
  line-height: 1.4;
}
#mx-chat-input:focus-visible { outline: 2px solid var(--accent-cyan); outline-offset: 1px; }
#mx-chat-input::placeholder { color: var(--text-on-dark-faint); }

#mx-chat-send {
  flex: none;
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-blue);
  color: #fff;
  transition: background var(--dur-fast) var(--ease-standard), opacity var(--dur-fast) var(--ease-standard);
}
#mx-chat-send:hover { background: var(--accent-blue-strong); }
#mx-chat-send:disabled { opacity: 0.5; cursor: default; }
#mx-chat-send svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 2; }

.mx-chat-footnote {
  flex: none;
  padding: 0.4rem 0.9rem 0.65rem;
  font-size: 0.65rem;
  color: var(--text-on-dark-faint);
  text-align: center;
}
#mx-chat-root.is-minimized .mx-chat-footnote { display: none; }
.mx-chat-footnote a { color: var(--text-on-dark-faint); text-decoration: underline; text-underline-offset: 2px; }

/* ---------------------------------- Mobile -------------------------------- */
@media (max-width: 640px) {
  #mx-chat-root { right: 12px; }
  #mx-chat-toggle { width: 52px; height: 52px; }
  #mx-chat-panel {
    position: fixed;
    top: 12px;
    right: 10px; left: 10px;
    bottom: calc(var(--mx-chat-bottom-offset, 20px) + 64px);
    width: auto;
    max-width: none;
    height: auto;
    max-height: none;
  }
}
