/* ───────────────────────────────────────────
   ARTI CHAT WIDGET — STYLES
   Mobile-first. 8px grid. 44px touch targets.
   Brand tokens inherited from styles.css.
   ─────────────────────────────────────────── */

/* ═══════════════════════════════════════════
   STACKING — keep all bottom-right floaters from colliding.
   body[data-arti-loaded] is set by arti.js on init.

   Mobile-first: BASE styles assume the site's .mobile-cta-bar is
   visible (it is at ≤767px). We stack: bar → trigger → back-to-top.

   Desktop enhancement (min-width: 768px): cta-bar hidden, so we drop
   the lift and just stack: trigger → back-to-top.
   ═══════════════════════════════════════════ */

/* BASE (mobile) — cta-bar visible, lift trigger + widget above it */
body[data-arti-loaded] #arti-trigger,
body[data-arti-loaded] #arti-widget {
  bottom: calc(56px + env(safe-area-inset-bottom) + 8px);
}
body[data-arti-loaded] .back-to-top {
  bottom: calc(56px + env(safe-area-inset-bottom) + 8px + 56px + 8px);
}

/* Desktop enhancement: cta-bar is hidden, no lift required */
@media (min-width: 768px) {
  body[data-arti-loaded] #arti-trigger,
  body[data-arti-loaded] #arti-widget {
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px));
  }
  body[data-arti-loaded] .back-to-top {
    bottom: calc(2rem + 56px + 12px);
  }
}

#arti-root {
  --arti-bg: var(--bg, #16140F);
  --arti-bg-elev: var(--bg2, #1C1A15);
  --arti-bg-msg: var(--bg3, #24211A);
  --arti-cedar: var(--cedar, #C4A87A);
  --arti-sand: var(--sand, #B5A88F);
  --arti-lime: var(--lime, #F0E8D8);
  --arti-mortar: var(--mortar, #B8AFA4);
  --arti-amber: var(--amber, #D4A04A);
  --arti-sage: var(--sage, #8B9A6D);
  --arti-glow: var(--glow, rgba(196, 168, 122, 0.08));
  --arti-radius: 12px;
  --arti-shadow: 0 12px 40px rgba(0, 0, 0, 0.45), 0 2px 8px rgba(0, 0, 0, 0.3);
  --arti-font: var(--sans, 'DM Sans', system-ui, sans-serif);

  position: fixed;
  inset: auto 0 0 auto;
  z-index: 9999;
  font-family: var(--arti-font);
}

/* ───────────────────────────────────────────
   TRIGGER BUTTON
   ─────────────────────────────────────────── */

/* BASE (mobile, ≤480px): icon-only circle. Label hidden. */
#arti-trigger {
  position: fixed;
  /* Safe-area-inset prevents the pill from sitting under the iPhone home
     indicator (iPhone X+) and below the notch on landscape orientation.
     Falls back to 16px on devices/browsers without env() support. */
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px));
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 56px;
  width: 56px;
  padding: 0;
  background: var(--arti-bg);
  color: var(--arti-cedar);
  border: 1.5px solid var(--arti-cedar);
  border-radius: 999px;
  font: 600 15px / 1 var(--arti-font);
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow: var(--arti-shadow);
  transition: transform 180ms ease, background 180ms ease, color 180ms ease;
}

#arti-trigger .arti-label {
  display: none;
}

/* Tablet+ enhancement: expand to a labeled pill */
@media (min-width: 481px) {
  #arti-trigger {
    width: auto;
    justify-content: flex-start;
    padding: 0 18px 0 14px;
  }
  #arti-trigger .arti-label {
    display: inline;
  }
}

#arti-trigger:hover,
#arti-trigger:focus-visible {
  background: var(--arti-cedar);
  color: var(--arti-bg);
  transform: translateY(-2px);
}

#arti-trigger:focus-visible {
  outline: 2px solid var(--arti-amber);
  outline-offset: 3px;
}

#arti-trigger .arti-icon {
  font: 700 14px var(--mono, 'JetBrains Mono', monospace);
  letter-spacing: 0;
  opacity: 0.9;
}

/* Pulse — 3 cycles every 30s, then stop */
#arti-trigger.arti-pulse::before {
  content: '';
  position: absolute;
  inset: -4px;
  border-radius: 999px;
  border: 2px solid var(--arti-cedar);
  animation: arti-pulse 1.4s ease-out 3;
  pointer-events: none;
}

@keyframes arti-pulse {
  0%   { transform: scale(1);    opacity: 0.7; }
  100% { transform: scale(1.25); opacity: 0;   }
}

/* Hide trigger when widget open */
#arti-root[data-open="true"] #arti-trigger {
  transform: scale(0.85);
  opacity: 0;
  pointer-events: none;
}

/* ───────────────────────────────────────────
   WIDGET SHELL
   ─────────────────────────────────────────── */

/* BASE (mobile, ≤480px): near-full-width panel anchored bottom. */
#arti-widget {
  position: fixed;
  /* Safe-area-inset matches trigger so the panel anchors flush above it
     and clears iPhone home indicator + notch in landscape. */
  right: max(8px, env(safe-area-inset-right));
  bottom: max(8px, calc(env(safe-area-inset-bottom) + 8px));
  width: calc(100vw - 16px);
  max-width: none;
  height: min(80dvh, calc(100dvh - 16px));
  background: var(--arti-bg-elev);
  border: 1px solid rgba(196, 168, 122, 0.25);
  border-radius: var(--arti-radius);
  box-shadow: var(--arti-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.98);
  pointer-events: none;
  transition: opacity 220ms ease, transform 220ms ease;
}

/* Tablet+ enhancement: narrower 360px-cap panel, larger edge inset */
@media (min-width: 481px) {
  #arti-widget {
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, calc(env(safe-area-inset-bottom) + 8px));
    width: calc(100vw - 32px);
    max-width: 360px;
    height: min(560px, calc(100dvh - 32px));
  }
}

#arti-root[data-open="true"] #arti-widget {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ───────────────────────────────────────────
   HEADER
   ─────────────────────────────────────────── */

.arti__header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  background: var(--arti-bg);
  border-bottom: 1px solid rgba(196, 168, 122, 0.15);
}

.arti__brand {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.arti__name {
  display: flex;
  align-items: center;
  gap: 8px;
  font: 600 15px var(--arti-font);
  color: var(--arti-cedar);
  letter-spacing: 0.01em;
}

/* AI disclosure badge — required for compliance with CA SB 1001,
   UT SB 226, FTC §5, TX TRAIGA. Visible on every widget open. */
.arti__ai-badge {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  padding: 2px 6px;
  background: var(--arti-bg-msg);
  color: var(--arti-cedar);
  border: 1px solid rgba(196, 168, 122, 0.3);
  border-radius: 3px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}


.arti__name::before {
  content: '◆';
  color: var(--arti-amber);
  font-size: 10px;
}

.arti__tagline {
  font: 400 12px var(--arti-font);
  color: var(--arti-sand);
  opacity: 0.75;
}

.arti__close {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--arti-sand);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  transition: background 160ms ease, color 160ms ease, border-color 160ms ease;
}

.arti__close:hover,
.arti__close:focus-visible {
  background: rgba(196, 168, 122, 0.1);
  color: var(--arti-cedar);
  border-color: rgba(196, 168, 122, 0.3);
}

.arti__close:focus-visible {
  outline: 2px solid var(--arti-amber);
  outline-offset: 2px;
}

/* ───────────────────────────────────────────
   MESSAGES
   ─────────────────────────────────────────── */

.arti__messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--arti-cedar) transparent;
  /* Prevent scroll chaining: pulling down at the top of the messages
     list won't trigger pull-to-refresh on the host page. */
  overscroll-behavior: contain;
}

.arti__messages::-webkit-scrollbar {
  width: 6px;
}

.arti__messages::-webkit-scrollbar-thumb {
  background: rgba(196, 168, 122, 0.3);
  border-radius: 3px;
}

.arti-msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--arti-radius);
  font: 400 14px / 1.5 var(--arti-font);
  word-wrap: break-word;
  animation: arti-msg-in 220ms ease;
}

.arti-msg--bot {
  align-self: flex-start;
  background: var(--arti-bg-msg);
  color: var(--arti-lime);
  border-bottom-left-radius: 4px;
}

.arti-msg--user {
  align-self: flex-end;
  background: var(--arti-cedar);
  color: var(--arti-bg);
  border-bottom-right-radius: 4px;
}

.arti-msg--error {
  align-self: flex-start;
  background: rgba(196, 76, 76, 0.15);
  color: #E8B5B5;
  border: 1px solid rgba(196, 76, 76, 0.3);
  font-size: 13px;
}

.arti-msg strong { font-weight: 700; }
.arti-msg em { font-style: italic; }

/* Typewriter reveal: preserve newlines during char-by-char typing.
   After completion, swaps to formatted DOM and this class is removed. */
.arti-msg__reveal {
  white-space: pre-wrap;
}

.arti-msg__reveal::after {
  content: '▍';
  display: inline-block;
  margin-left: 1px;
  color: var(--arti-cedar);
  animation: arti-caret 1s steps(1) infinite;
  opacity: 0.7;
  font-weight: 400;
}

@keyframes arti-caret {
  0%, 50%   { opacity: 0.7; }
  51%, 100% { opacity: 0; }
}

.arti-msg a {
  color: var(--arti-amber);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.arti-msg--user a { color: var(--arti-bg); }

@keyframes arti-msg-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Typing dots */
.arti-typing {
  align-self: flex-start;
  display: inline-flex;
  gap: 4px;
  padding: 14px;
  background: var(--arti-bg-msg);
  border-radius: var(--arti-radius);
  border-bottom-left-radius: 4px;
}

.arti-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--arti-sand);
  animation: arti-bounce 1.2s infinite ease-in-out;
}

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

@keyframes arti-bounce {
  0%, 60%, 100% { transform: translateY(0);   opacity: 0.5; }
  30%           { transform: translateY(-4px); opacity: 1; }
}

/* ───────────────────────────────────────────
   QUICK-REPLY CHIPS
   ─────────────────────────────────────────── */

.arti__chips {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 16px 12px;
}

.arti-chip {
  text-align: left;
  padding: 10px 14px;
  min-height: 44px;
  background: transparent;
  color: var(--arti-cedar);
  border: 1px solid rgba(196, 168, 122, 0.4);
  border-radius: 8px;
  font: 500 14px var(--arti-font);
  cursor: pointer;
  transition: background 160ms ease, border-color 160ms ease, color 160ms ease;
}

.arti-chip:hover,
.arti-chip:focus-visible {
  background: rgba(196, 168, 122, 0.1);
  border-color: var(--arti-cedar);
  color: var(--arti-lime);
}

.arti-chip:focus-visible {
  outline: 2px solid var(--arti-amber);
  outline-offset: 2px;
}

/* ───────────────────────────────────────────
   INPUT
   ─────────────────────────────────────────── */

.arti__input {
  display: flex;
  gap: 8px;
  padding: 12px;
  background: var(--arti-bg);
  border-top: 1px solid rgba(196, 168, 122, 0.15);
}

.arti__field {
  flex: 1;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 14px;
  background: var(--arti-bg-msg);
  color: var(--arti-lime);
  border: 1px solid rgba(196, 168, 122, 0.2);
  border-radius: 8px;
  /* font-size 16px is the iOS zoom-prevention threshold — DO NOT lower.
     Tune perceived size with line-height + padding instead. */
  font: 400 16px / 1.35 var(--arti-font);
  resize: none;
  transition: border-color 160ms ease;
}

.arti__field::placeholder {
  color: var(--arti-sand);
  opacity: 0.5;
}

.arti__field:focus {
  outline: none;
  border-color: var(--arti-cedar);
}

.arti__field:focus-visible {
  outline: 2px solid var(--arti-amber);
  outline-offset: 1px;
}

.arti__send {
  min-width: 44px;
  min-height: 44px;
  padding: 0 16px;
  background: var(--arti-cedar);
  color: var(--arti-bg);
  border: none;
  border-radius: 8px;
  font: 600 14px var(--arti-font);
  cursor: pointer;
  transition: background 160ms ease, transform 100ms ease;
}

.arti__send:hover { background: var(--arti-amber); }
.arti__send:active { transform: scale(0.96); }

.arti__send:focus-visible {
  outline: 2px solid var(--arti-amber);
  outline-offset: 2px;
}

.arti__send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* ───────────────────────────────────────────
   ACCESSIBILITY — REDUCED MOTION
   ─────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  /* Kill transforms, scales, slides, bounces — but allow opacity to
     fade (vestibular-safe). transition-duration: 0.01ms collapses any
     remaining transitions to effectively instant without disabling the
     property entirely (some browsers have edge cases with `none`). */
  #arti-trigger,
  #arti-widget,
  .arti__close,
  .arti-chip,
  .arti__send,
  .arti-msg {
    animation: none !important;
    transition-duration: 0.01ms !important;
  }

  /* Pulse rings: hide entirely */
  #arti-trigger.arti-pulse::before {
    display: none;
  }

  /* Typing dots: stop the bounce animation, leave visible at 0.7 opacity */
  .arti-typing span {
    animation: none;
    opacity: 0.7;
  }

  /* Typewriter caret: typewriter itself is disabled in JS under
     reduced-motion, but if the reveal span exists for any reason, kill
     the blink. */
  .arti-msg__reveal::after {
    animation: none;
    opacity: 0;
  }

  /* Smooth scroll can disorient — snap instead. */
  .arti__messages {
    scroll-behavior: auto;
  }
}

/* ───────────────────────────────────────────
   SCREEN READER UTILITIES
   ─────────────────────────────────────────── */

.arti-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;
}
