/* ==========================================================================
   Ace Assistant chat widget
   Uses the site's colour tokens from styles.css, with fallbacks.
   ========================================================================== */

#ace-chat-toggle,
#ace-chat-panel {
  --chat-bg: var(--ink, #1b1c1e);
  --chat-bar: var(--ink-deep, #141516);
  --chat-bubble: #2a2b2f;
  --chat-input: #232427;
  --chat-line: rgba(194, 124, 69, 0.25);
  --chat-text: var(--text-on-dark, #f4f0ea);
  --chat-muted: var(--text-on-dark-muted, #c4bdb4);
  --chat-accent: var(--brick, #c0182f);
  --chat-accent-light: var(--brick-light, #d42a3f);
  --chat-accent-dark: var(--brick-dark, #9a1226);
  --chat-copper: var(--copper-light, #e2a873);
}

#ace-chat-toggle {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 250;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 48px;
  padding: 0.85rem 1.3rem;
  font-family: var(--font-body, sans-serif);
  font-size: 0.98rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(180deg, var(--chat-accent-light) 0%, var(--chat-accent) 50%, var(--chat-accent-dark) 100%);
  border: 0;
  border-radius: 999px;
  box-shadow: 0 4px 15px rgba(192, 24, 47, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.35);
  cursor: pointer;
}

#ace-chat-toggle:hover {
  box-shadow: 0 10px 28px rgba(192, 24, 47, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

#ace-chat-toggle svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

#ace-chat-toggle .ace-chat-close-icon,
#ace-chat-toggle[aria-expanded="true"] .ace-chat-open-icon {
  display: none;
}

#ace-chat-toggle[aria-expanded="true"] .ace-chat-close-icon {
  display: block;
}

#ace-chat-panel {
  position: fixed;
  right: 24px;
  bottom: 92px;
  z-index: 250;
  display: flex;
  flex-direction: column;
  width: min(380px, calc(100vw - 32px));
  max-height: min(600px, calc(100vh - 130px));
  max-height: min(600px, calc(100dvh - 130px));
  overflow: hidden;
  font-family: var(--font-body, sans-serif);
  color: var(--chat-text);
  background: var(--chat-bg);
  border: 1px solid var(--chat-line);
  border-radius: 16px;
  box-shadow: 0 28px 56px rgba(0, 0, 0, 0.45), 0 10px 20px rgba(0, 0, 0, 0.3);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#ace-chat-panel.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

#ace-chat-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
}

.ace-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.9rem 1rem 0.9rem 1.2rem;
  background: var(--chat-bar);
  border-bottom: 1px solid var(--chat-line);
}

.ace-chat-header h2 {
  margin: 0;
  font-family: var(--font-heading, serif);
  font-size: 1.1rem;
  color: #fff;
}

.ace-chat-header p {
  margin: 0.15rem 0 0;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--chat-muted);
}

.ace-chat-header-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  color: var(--chat-muted);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.ace-chat-header-close:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}

.ace-chat-messages {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 0.7rem;
  min-height: 0;
  padding: 1rem 1.2rem;
  overflow-y: auto;
}

.ace-chat-bubble {
  max-width: 85%;
  padding: 0.65rem 0.9rem;
  font-size: 0.9rem;
  line-height: 1.5;
  border-radius: 14px;
  overflow-wrap: break-word;
}

.ace-chat-bubble.bot {
  align-self: flex-start;
  color: var(--chat-text);
  background: var(--chat-bubble);
  border-bottom-left-radius: 4px;
}

.ace-chat-bubble.user {
  align-self: flex-end;
  color: #fff;
  background: var(--chat-accent);
  border-bottom-right-radius: 4px;
}

.ace-chat-quick-options {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0 1.2rem 0.8rem;
}

.ace-chat-quick-btn {
  min-height: 36px;
  padding: 0.5rem 0.85rem;
  font: inherit;
  font-size: 0.82rem;
  color: var(--chat-copper);
  background: transparent;
  border: 1px solid rgba(194, 124, 69, 0.5);
  border-radius: 999px;
  cursor: pointer;
}

.ace-chat-quick-btn:hover {
  color: #fff;
  background: rgba(194, 124, 69, 0.18);
}

.ace-chat-typing {
  display: flex;
  align-self: flex-start;
  gap: 4px;
  padding: 0.65rem 0.9rem;
  background: var(--chat-bubble);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
}

.ace-chat-typing span {
  width: 6px;
  height: 6px;
  background: var(--chat-muted);
  border-radius: 50%;
  opacity: 0.4;
}

.ace-chat-footer {
  padding: 0.8rem 1rem;
  background: var(--chat-bar);
  border-top: 1px solid var(--chat-line);
}

.ace-chat-input-row {
  display: flex;
  gap: 0.5rem;
}

.ace-chat-input-row input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0.6rem 1rem;
  font: inherit;
  font-size: 0.9rem;
  color: #fff;
  background: var(--chat-input);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
}

.ace-chat-input-row input:focus,
.ace-chat-lead-form input:focus,
.ace-chat-lead-form select:focus,
.ace-chat-lead-form textarea:focus {
  outline: 2px solid var(--chat-copper);
  outline-offset: 1px;
}

.ace-chat-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  color: #fff;
  background: var(--chat-accent);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.ace-chat-send:hover {
  background: var(--chat-accent-light);
}

.ace-chat-alt-action {
  margin: 0.6rem 0 0;
  font-size: 0.78rem;
  text-align: center;
  color: var(--chat-muted);
}

.ace-chat-alt-action button {
  min-height: 32px;
  padding: 0;
  font: inherit;
  color: var(--chat-copper);
  text-decoration: underline;
  background: none;
  border: 0;
  cursor: pointer;
}

/* Lead form */
.ace-chat-subbar {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  padding: 0 1rem;
  border-bottom: 1px solid var(--chat-line);
}

.ace-chat-back {
  min-height: 40px;
  padding: 0 0.25rem;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--chat-copper);
  background: none;
  border: 0;
  cursor: pointer;
}

.ace-chat-back:hover {
  text-decoration: underline;
}

.ace-chat-lead-form {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 0.7rem;
  min-height: 0;
  padding: 1rem 1.2rem;
  overflow-y: auto;
}

.ace-chat-lead-form label {
  display: block;
  margin-bottom: 0.3rem;
  font-size: 0.8rem;
  color: var(--chat-muted);
}

.ace-chat-lead-form input,
.ace-chat-lead-form select,
.ace-chat-lead-form textarea {
  width: 100%;
  min-height: 44px;
  padding: 0.6rem 0.8rem;
  font: inherit;
  font-size: 0.9rem;
  color: #fff;
  background: var(--chat-input);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
}

.ace-chat-lead-form textarea {
  min-height: 80px;
  resize: vertical;
}

.ace-chat-lead-honeypot {
  position: absolute;
  left: -9999px;
}

.ace-chat-lead-submit {
  min-height: 48px;
  padding: 0.7rem 1rem;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(180deg, var(--chat-accent-light) 0%, var(--chat-accent) 50%, var(--chat-accent-dark) 100%);
  border: 0;
  border-radius: 999px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35);
  cursor: pointer;
}

.ace-chat-lead-submit:disabled {
  opacity: 0.6;
  cursor: default;
}

.ace-chat-lead-error {
  font-size: 0.82rem;
  color: #ff9d9d;
}

.ace-chat-lead-error:empty {
  display: none;
}

.ace-chat-lead-success {
  padding: 1.2rem;
  font-size: 0.92rem;
  text-align: center;
}

@media (max-width: 480px) {
  #ace-chat-panel {
    right: 16px;
    left: 16px;
    bottom: 84px;
    width: auto;
  }

  #ace-chat-toggle {
    right: 16px;
    bottom: 16px;
  }
}

@media (prefers-reduced-motion: no-preference) {
  #ace-chat-toggle {
    transition: transform 0.25s ease, box-shadow 0.25s ease;
  }

  #ace-chat-toggle:hover {
    transform: translateY(-2px);
  }

  /* Visibility flips instantly on open (so focus can move in) and only after the fade on close */
  #ace-chat-panel {
    transform: translateY(16px) scale(0.98);
    transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1), transform 0.25s cubic-bezier(0.22, 1, 0.36, 1), visibility 0s linear 0.25s;
  }

  #ace-chat-panel.open {
    transform: translateY(0) scale(1);
    transition: opacity 0.25s cubic-bezier(0.22, 1, 0.36, 1), transform 0.25s cubic-bezier(0.22, 1, 0.36, 1), visibility 0s;
  }

  .ace-chat-messages {
    scroll-behavior: smooth;
  }

  .ace-chat-typing span {
    animation: ace-typing-pulse 1.2s ease-in-out infinite;
  }

  .ace-chat-typing span:nth-child(2) { animation-delay: 0.15s; }
  .ace-chat-typing span:nth-child(3) { animation-delay: 0.3s; }

  @keyframes ace-typing-pulse {
    0%, 60%, 100% { opacity: 0.4; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-3px); }
  }
}
