/**
 * Forge AI Concierge — Chat Widget Styles
 *
 * All visual tokens are CSS custom properties.
 * The plugin's enqueue hook sets defaults; each Forge site overrides
 * only the variables it needs — no forking of this file.
 *
 * Variable reference:
 *   --fac-accent          Primary accent color (buttons, highlights)
 *   --fac-accent-hover    Accent on hover/focus
 *   --fac-accent-soft     Low-opacity accent (backgrounds, chips)
 *   --fac-bg              Panel background
 *   --fac-bg-elevated     Slightly lighter surface (header, input bar)
 *   --fac-fg              Primary text color
 *   --fac-fg-muted        Secondary / timestamp text
 *   --fac-bubble-user     User message bubble background
 *   --fac-bubble-user-fg  User message bubble text
 *   --fac-bubble-ai       AI message bubble background
 *   --fac-bubble-ai-fg    AI message bubble text
 *   --fac-radius          Base border-radius
 *   --fac-radius-lg       Large border-radius (panel, bubbles)
 *   --fac-shadow          Panel drop shadow
 *   --fac-font-family     Widget font stack
 *   --fac-z-index         z-index for the floating widget
 *
 * BST defaults reflect the Stealth Tactical design direction:
 *   accent  = #b3e000 (radioactive yellow-green)
 *   base    = #0d100c (near-black graphite)
 *
 * @package Ragnarok\ForgeAiConcierge
 */

/* =========================================================================
   1. CSS CUSTOM PROPERTIES — defaults (BST / Tactical)
   Overridden inline by wp_add_inline_style() from saved wp_options.
   ========================================================================= */

:root {
  --fac-accent:          #b3e000;
  --fac-accent-hover:    #c4ec33;
  --fac-accent-soft:     rgba(179, 224, 0, 0.14);
  --fac-bg:              #161a13;
  --fac-bg-elevated:     #1d211a;
  --fac-fg:              #dde2d4;
  --fac-fg-muted:        #8d9385;
  --fac-border:          rgba(221, 226, 212, 0.10);
  --fac-bubble-user:     #b3e000;
  --fac-bubble-user-fg:  #0d100c;
  --fac-bubble-ai:       #1d211a;
  --fac-bubble-ai-fg:    #dde2d4;
  --fac-radius:          0.5rem;
  --fac-radius-lg:       1rem;
  --fac-shadow:          0 8px 32px rgba(0, 0, 0, 0.6), 0 2px 8px rgba(0, 0, 0, 0.4);
  --fac-font-family:     'Manrope', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --fac-z-index:         9000;
  --fac-panel-width:     380px;
  --fac-panel-height:    560px;
  --fac-trigger-size:    56px;
  --fac-trigger-bottom:  24px;
  --fac-trigger-right:   24px;
}

/* =========================================================================
   2. RESET / SCOPING
   All widget elements are scoped to .fac-widget to avoid bleed into theme.
   ========================================================================= */

.fac-widget,
.fac-widget *,
.fac-widget *::before,
.fac-widget *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.fac-widget {
  font-family: var(--fac-font-family);
  font-size: 0.9375rem; /* 15px at 16px root */
  line-height: 1.5;
  color: var(--fac-fg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* =========================================================================
   3. TRIGGER BUTTON (floating bubble)
   ========================================================================= */

.fac-trigger {
  position: fixed;
  bottom: var(--fac-trigger-bottom);
  right: var(--fac-trigger-right);
  z-index: var(--fac-z-index);
  width: var(--fac-trigger-size);
  height: var(--fac-trigger-size);
  border-radius: 50%;
  border: none;
  background: var(--fac-accent);
  color: var(--fac-bubble-user-fg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5), 0 0 0 0 var(--fac-accent);
  transition: transform 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
  /* Respect reduced motion — pulse is removed there too */
}

.fac-trigger:hover,
.fac-trigger:focus-visible {
  background: var(--fac-accent-hover);
  transform: scale(1.08);
  outline: 2px solid var(--fac-accent-hover);
  outline-offset: 3px;
}

.fac-trigger:active {
  transform: scale(0.95);
}

.fac-trigger[aria-expanded="true"] {
  /* Show X icon state — handled via JS class swap */
  background: var(--fac-bg-elevated);
  color: var(--fac-fg);
}

.fac-trigger[aria-expanded="true"]:hover {
  background: var(--fac-bg);
}

/* Pulse animation — first-visit only, suppressed after interaction */
@keyframes fac-pulse {
  0%   { box-shadow: 0 4px 16px rgba(0,0,0,0.5), 0 0 0 0 rgba(179, 224, 0, 0.6); }
  70%  { box-shadow: 0 4px 16px rgba(0,0,0,0.5), 0 0 0 12px rgba(179, 224, 0, 0); }
  100% { box-shadow: 0 4px 16px rgba(0,0,0,0.5), 0 0 0 0 rgba(179, 224, 0, 0); }
}

.fac-trigger--pulse {
  animation: fac-pulse 2s ease-in-out 3; /* fires 3 times then stops */
}

@media (prefers-reduced-motion: reduce) {
  .fac-trigger--pulse {
    animation: none;
  }
}

/* Trigger icon + label layout */
.fac-trigger__icon {
  width: 24px;
  height: 24px;
  flex-shrink: 0;
  pointer-events: none;
}

/* Visually-hidden label for SR */
.fac-trigger__label {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* =========================================================================
   4. PANEL (expanded chat window)
   ========================================================================= */

.fac-panel {
  position: fixed;
  bottom: calc(var(--fac-trigger-bottom) + var(--fac-trigger-size) + 12px);
  right: var(--fac-trigger-right);
  z-index: var(--fac-z-index);
  width: var(--fac-panel-width);
  height: var(--fac-panel-height);
  background: var(--fac-bg);
  border: 1px solid var(--fac-border);
  border-radius: var(--fac-radius-lg);
  box-shadow: var(--fac-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  /* Open/close transitions */
  transform-origin: bottom right;
  transform: scale(0.92) translateY(8px);
  opacity: 0;
  pointer-events: none;
  transition: transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1),
              opacity 0.18s ease;
}

.fac-panel--open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

@media (prefers-reduced-motion: reduce) {
  .fac-panel {
    transition: opacity 0.15s ease;
    transform: none;
  }
}

/* =========================================================================
   5. PANEL HEADER
   ========================================================================= */

.fac-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
  background: var(--fac-bg-elevated);
  border-bottom: 1px solid var(--fac-border);
  flex-shrink: 0;
}

.fac-header__avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--fac-accent-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--fac-accent);
}

.fac-header__avatar svg {
  width: 20px;
  height: 20px;
}

.fac-header__info {
  flex: 1;
  min-width: 0;
}

.fac-header__name {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--fac-fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fac-header__disclosure {
  font-size: 0.6875rem; /* 11px */
  color: var(--fac-fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.fac-header__close {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--fac-fg-muted);
  cursor: pointer;
  border-radius: var(--fac-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s, color 0.15s;
}

.fac-header__close:hover,
.fac-header__close:focus-visible {
  background: var(--fac-border);
  color: var(--fac-fg);
  outline: 2px solid var(--fac-accent);
  outline-offset: 1px;
}

.fac-header__close svg {
  width: 18px;
  height: 18px;
}

/* =========================================================================
   6. CONSENT BANNER (first-visit gate inside the panel)
   ========================================================================= */

.fac-consent {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem 1.25rem;
  text-align: center;
  gap: 1rem;
}

.fac-consent__icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--fac-accent-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--fac-accent);
}

.fac-consent__icon svg {
  width: 24px;
  height: 24px;
}

.fac-consent__title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--fac-fg);
}

.fac-consent__text {
  font-size: 0.8125rem; /* 13px */
  color: var(--fac-fg-muted);
  line-height: 1.6;
  max-width: 28ch;
}

.fac-consent__btn {
  padding: 0.6875rem 1.5rem;
  background: var(--fac-accent);
  color: var(--fac-bubble-user-fg);
  border: none;
  border-radius: var(--fac-radius);
  font-size: 0.9375rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.12s;
  min-height: 44px; /* touch target */
}

.fac-consent__btn:hover,
.fac-consent__btn:focus-visible {
  background: var(--fac-accent-hover);
  outline: 2px solid var(--fac-accent-hover);
  outline-offset: 2px;
}

.fac-consent__btn:active {
  transform: scale(0.97);
}

/* =========================================================================
   7. MESSAGES AREA
   ========================================================================= */

.fac-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  /* Custom scrollbar for dark theme */
  scrollbar-width: thin;
  scrollbar-color: var(--fac-border) transparent;
}

.fac-messages::-webkit-scrollbar {
  width: 4px;
}

.fac-messages::-webkit-scrollbar-track {
  background: transparent;
}

.fac-messages::-webkit-scrollbar-thumb {
  background: var(--fac-border);
  border-radius: 2px;
}

/* =========================================================================
   8. SUGGESTED QUESTION CHIPS
   ========================================================================= */

.fac-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.fac-chip {
  padding: 0.3125rem 0.75rem;
  background: var(--fac-accent-soft);
  color: var(--fac-accent);
  border: 1px solid rgba(179, 224, 0, 0.25);
  border-radius: 100px;
  font-size: 0.8125rem;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
  text-align: left;
  min-height: 36px;
  display: inline-flex;
  align-items: center;
}

.fac-chip:hover,
.fac-chip:focus-visible {
  background: rgba(179, 224, 0, 0.22);
  border-color: rgba(179, 224, 0, 0.5);
  outline: 2px solid var(--fac-accent);
  outline-offset: 1px;
}

/* Hide chips after first message sent */
.fac-chips--hidden {
  display: none;
}

/* =========================================================================
   9. MESSAGE BUBBLES
   ========================================================================= */

.fac-message {
  display: flex;
  flex-direction: column;
  max-width: 82%;
}

.fac-message--user {
  align-self: flex-end;
  align-items: flex-end;
}

.fac-message--ai {
  align-self: flex-start;
  align-items: flex-start;
}

.fac-message__bubble {
  padding: 0.625rem 0.875rem;
  border-radius: var(--fac-radius-lg);
  font-size: 0.875rem;
  line-height: 1.55;
  word-break: break-word;
}

.fac-message--user .fac-message__bubble {
  background: var(--fac-bubble-user);
  color: var(--fac-bubble-user-fg);
  border-bottom-right-radius: 4px;
}

.fac-message--ai .fac-message__bubble {
  background: var(--fac-bubble-ai);
  color: var(--fac-bubble-ai-fg);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--fac-border);
}

.fac-message__timestamp {
  font-size: 0.6875rem;
  color: var(--fac-fg-muted);
  margin-top: 0.25rem;
  padding: 0 0.25rem;
}

/* Streaming cursor — animated blinking pipe */
.fac-message--streaming .fac-message__bubble::after {
  content: '|';
  display: inline-block;
  color: var(--fac-accent);
  animation: fac-blink 0.8s step-end infinite;
  margin-left: 1px;
}

@keyframes fac-blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .fac-message--streaming .fac-message__bubble::after {
    animation: none;
  }
}

/* =========================================================================
   10. MARKDOWN RENDERING (inside AI bubbles)
   ========================================================================= */

.fac-message__bubble p {
  margin-bottom: 0.4em;
}

.fac-message__bubble p:last-child {
  margin-bottom: 0;
}

.fac-message__bubble strong {
  font-weight: 700;
  color: inherit;
}

.fac-message__bubble em {
  font-style: italic;
}

.fac-message__bubble a {
  color: var(--fac-accent);
  text-decoration: underline;
  word-break: break-all;
}

.fac-message__bubble a:hover {
  color: var(--fac-accent-hover);
}

.fac-message__bubble ul,
.fac-message__bubble ol {
  padding-left: 1.25em;
  margin: 0.3em 0;
}

.fac-message__bubble li {
  margin-bottom: 0.2em;
}

.fac-message__bubble code {
  font-family: 'JetBrains Mono', 'Fira Code', 'Courier New', monospace;
  font-size: 0.8125em;
  background: rgba(0, 0, 0, 0.35);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  color: var(--fac-accent);
}

.fac-message__bubble pre {
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid var(--fac-border);
  border-radius: var(--fac-radius);
  padding: 0.75rem;
  overflow-x: auto;
  margin: 0.4em 0;
}

.fac-message__bubble pre code {
  background: transparent;
  padding: 0;
  font-size: 0.8125em;
}

/* =========================================================================
   11. ERROR TOAST
   ========================================================================= */

.fac-error {
  display: none;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0.875rem;
  background: rgba(220, 38, 38, 0.15);
  border: 1px solid rgba(220, 38, 38, 0.4);
  border-radius: var(--fac-radius);
  color: #fca5a5;
  font-size: 0.8125rem;
  margin: 0 1rem 0.5rem;
}

.fac-error--visible {
  display: flex;
}

.fac-error__text {
  flex: 1;
}

.fac-error__retry {
  background: transparent;
  border: 1px solid rgba(220, 38, 38, 0.5);
  color: #fca5a5;
  font-family: inherit;
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: var(--fac-radius);
  cursor: pointer;
  flex-shrink: 0;
  min-height: 32px;
  transition: background-color 0.15s;
}

.fac-error__retry:hover,
.fac-error__retry:focus-visible {
  background: rgba(220, 38, 38, 0.25);
  outline: 2px solid rgba(220, 38, 38, 0.6);
  outline-offset: 1px;
}

/* =========================================================================
   12. INPUT BAR
   ========================================================================= */

.fac-inputbar {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--fac-bg-elevated);
  border-top: 1px solid var(--fac-border);
  flex-shrink: 0;
}

.fac-inputbar__textarea {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  padding: 0.5625rem 0.75rem;
  background: var(--fac-bg);
  border: 1px solid var(--fac-border);
  border-radius: var(--fac-radius);
  color: var(--fac-fg);
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.5;
  resize: none;
  overflow-y: auto;
  transition: border-color 0.15s;
  scrollbar-width: thin;
  scrollbar-color: var(--fac-border) transparent;
}

.fac-inputbar__textarea::placeholder {
  color: var(--fac-fg-muted);
}

.fac-inputbar__textarea:focus {
  outline: none;
  border-color: var(--fac-accent);
}

.fac-inputbar__textarea:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Send button */
.fac-inputbar__send {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: none;
  background: var(--fac-accent);
  color: var(--fac-bubble-user-fg);
  border-radius: var(--fac-radius);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s, transform 0.1s;
  min-height: 44px; /* a11y touch target */
  min-width: 44px;
}

.fac-inputbar__send:hover,
.fac-inputbar__send:focus-visible {
  background: var(--fac-accent-hover);
  outline: 2px solid var(--fac-accent-hover);
  outline-offset: 2px;
}

.fac-inputbar__send:active {
  transform: scale(0.93);
}

.fac-inputbar__send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.fac-inputbar__send svg {
  width: 18px;
  height: 18px;
}

/* Cancel (stream abort) button — visible only during streaming */
.fac-inputbar__cancel {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 1px solid var(--fac-border);
  background: transparent;
  color: var(--fac-fg-muted);
  border-radius: var(--fac-radius);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  transition: background-color 0.15s, color 0.15s;
  min-height: 44px;
  min-width: 44px;
}

.fac-inputbar__cancel--visible {
  display: flex;
}

.fac-inputbar__cancel:hover,
.fac-inputbar__cancel:focus-visible {
  background: var(--fac-border);
  color: var(--fac-fg);
  outline: 2px solid var(--fac-accent);
  outline-offset: 1px;
}

.fac-inputbar__cancel svg {
  width: 16px;
  height: 16px;
}

/* =========================================================================
   13. MOBILE — full-screen panel at < 768px
   ========================================================================= */

@media (max-width: 767px) {
  .fac-panel {
    /* Full-screen overlay on mobile */
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 0;
    bottom: 0;
    right: 0;
    border: none;
    transform: translateY(100%);
    transform-origin: bottom center;
  }

  .fac-panel--open {
    transform: translateY(0);
  }

  @media (prefers-reduced-motion: reduce) {
    .fac-panel {
      transform: none;
    }
  }

  .fac-trigger {
    bottom: 16px;
    right: 16px;
  }
}

/* =========================================================================
   14. UTILITY — visually hidden (for ARIA-only content)
   ========================================================================= */

.fac-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* =========================================================================
   15. LOADING DOTS (while waiting for first streaming token)
   ========================================================================= */

.fac-typing {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 4px 2px;
}

.fac-typing__dot {
  width: 7px;
  height: 7px;
  background: var(--fac-fg-muted);
  border-radius: 50%;
  animation: fac-dots 1.2s ease-in-out infinite;
}

.fac-typing__dot:nth-child(2) { animation-delay: 0.2s; }
.fac-typing__dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes fac-dots {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.5; }
  40%            { transform: scale(1);   opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .fac-typing__dot {
    animation: none;
    opacity: 0.6;
  }
}
