/*
 * PSOS Assistant — chat widget styles.
 * Uses the site's existing design tokens (css/variables.css) so it reads as
 * native, not bolted on. Loaded only where the widget appears.
 */

.psos-chat {
  position: fixed;
  right: var(--space-lg);
  bottom: var(--space-lg);
  z-index: var(--z-popover, 500);
  font-family: var(--font-body);
  color: var(--text-dark);
}

/* Coexist with the floating back-to-top button (also bottom-right, 48px):
   the launcher owns the corner; nudge back-to-top up so they stack. */
.back-to-top {
  bottom: calc(var(--space-lg) + 72px) !important;
}

/* --- Launcher ------------------------------------------------------------ */
.psos-chat__launcher {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 0.7rem 1.1rem;
  background: var(--color-primary);
  color: var(--navy);
  border: var(--border-medium) solid var(--navy);
  border-radius: var(--radius-full);
  box-shadow: var(--shadow-retro-md);
  font-family: var(--font-heading);
  font-weight: var(--weight-bold);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: transform var(--transition-bounce), box-shadow var(--transition-base);
}
.psos-chat__launcher:hover {
  transform: translateY(var(--lift-sm));
  box-shadow: var(--shadow-retro-lg);
}
.psos-chat__launcher:focus-visible {
  outline: var(--border-medium) solid var(--retro-teal);
  outline-offset: 2px;
}
.psos-chat[data-open="true"] .psos-chat__launcher {
  display: none;
}

/* --- Panel --------------------------------------------------------------- */
.psos-chat__panel {
  position: fixed;
  right: var(--space-lg);
  bottom: var(--space-lg);
  width: min(384px, calc(100vw - 2 * var(--space-lg)));
  height: min(600px, calc(100vh - 2 * var(--space-lg)));
  display: flex;
  flex-direction: column;
  background: var(--cream);
  border: var(--border-medium) solid var(--navy);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
}

.psos-chat__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-xs);
  padding: 0.75rem 1rem;
  background: var(--navy);
  color: var(--cream);
}
.psos-chat__title {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-display);
  font-weight: var(--weight-bold);
  font-size: var(--text-lg);
}
.psos-chat__badge {
  font-family: var(--font-body);
  font-size: 0.65rem;
  font-weight: var(--weight-bold);
  letter-spacing: 0.05em;
  padding: 0.1rem 0.4rem;
  border-radius: var(--radius-sm);
  background: var(--retro-teal);
  color: #fff;
}
.psos-chat__header-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}
.psos-chat__reset {
  background: transparent;
  border: 1px solid rgba(252, 246, 232, 0.5);
  color: var(--cream);
  font-family: var(--font-body);
  font-size: var(--text-xs);
  padding: 0.25rem 0.55rem;
  border-radius: var(--radius-full);
  cursor: pointer;
}
.psos-chat__reset:hover { background: rgba(252, 246, 232, 0.12); }
.psos-chat__close {
  background: transparent;
  border: none;
  color: var(--cream);
  cursor: pointer;
  padding: 0.2rem;
  display: inline-flex;
  border-radius: var(--radius-sm);
}
.psos-chat__reset:focus-visible,
.psos-chat__close:focus-visible {
  outline: 2px solid var(--retro-yellow);
  outline-offset: 2px;
}

.psos-chat__body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

/* --- Log / messages ------------------------------------------------------ */
.psos-chat__log {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}
.psos-chat__intro p {
  font-size: var(--text-sm);
  color: var(--text-contrast-improved);
  margin: 0 0 0.75rem;
  line-height: var(--leading-relaxed);
}
.psos-chat__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}
.psos-chat__chip {
  background: #fff;
  border: 2px solid var(--navy);
  border-radius: var(--radius-full);
  padding: 0.35rem 0.7rem;
  font-family: var(--font-body);
  font-size: var(--text-xs);
  color: var(--navy);
  cursor: pointer;
  text-align: left;
  transition: transform var(--transition-fast), background var(--transition-fast);
}
.psos-chat__chip:hover { transform: translateY(-1px); background: var(--sand); }
.psos-chat__chip:focus-visible { outline: 2px solid var(--retro-teal); outline-offset: 2px; }

.psos-chat__msg { display: flex; }
.psos-chat__msg--user { justify-content: flex-end; }
.psos-chat__msg--assistant { justify-content: flex-start; }
.psos-chat__bubble {
  max-width: 85%;
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.psos-chat__bubble p { margin: 0 0 0.5rem; }
.psos-chat__bubble p:last-child { margin-bottom: 0; }
.psos-chat__bubble a { color: inherit; text-decoration: underline; font-weight: var(--weight-semibold); }
.psos-chat__msg--assistant .psos-chat__bubble {
  background: #fff;
  border: 2px solid var(--navy);
  color: var(--text-dark);
  border-bottom-left-radius: var(--radius-sm);
}
.psos-chat__msg--user .psos-chat__bubble {
  background: var(--retro-teal);
  color: #fff;
  border-bottom-right-radius: var(--radius-sm);
}
.psos-chat__msg--user .psos-chat__bubble a { color: #fff; }

.psos-chat__note {
  align-self: center;
  font-size: var(--text-xs);
  color: var(--error-red);
  background: #fff;
  border: 1px solid var(--error-red);
  border-radius: var(--radius-md);
  padding: 0.4rem 0.7rem;
  text-align: center;
}

/* typing dots */
.psos-chat__dots { display: inline-flex; gap: 4px; align-items: center; }
.psos-chat__dots i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-muted);
  animation: psos-chat-blink 1.2s infinite ease-in-out both;
}
.psos-chat__dots i:nth-child(2) { animation-delay: 0.2s; }
.psos-chat__dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes psos-chat-blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

/* --- Composer ------------------------------------------------------------ */
.psos-chat__form {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.6rem;
  border-top: 2px solid var(--border-light);
  background: var(--cream);
}
.psos-chat__input {
  flex: 1;
  resize: none;
  border: 2px solid var(--navy);
  border-radius: var(--radius-lg);
  padding: 0.55rem 0.7rem;
  font-family: var(--font-body);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  background: #fff;
  color: var(--text-dark);
  max-height: 120px;
}
.psos-chat__input:focus-visible { outline: none; border-color: var(--retro-teal); box-shadow: 0 0 0 2px rgba(35, 128, 120, 0.25); }
.psos-chat__send {
  flex: 0 0 auto;
  width: 42px; height: 42px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--color-primary);
  color: var(--navy);
  border: 2px solid var(--navy);
  border-radius: var(--radius-lg);
  cursor: pointer;
}
.psos-chat__send:hover { background: var(--color-primary-hover); }
.psos-chat__send:disabled { opacity: 0.5; cursor: not-allowed; }
.psos-chat__send:focus-visible { outline: 2px solid var(--retro-teal); outline-offset: 2px; }

.psos-chat__handoff {
  padding: 0 0.6rem 0.6rem;
  background: var(--cream);
}
.psos-chat__human {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--retro-teal);
  font-family: var(--font-body);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  padding: 0.3rem;
  border-radius: var(--radius-sm);
}
.psos-chat__human:hover { text-decoration: underline; }
.psos-chat__human:focus-visible { outline: 2px solid var(--retro-teal); outline-offset: 2px; }

/* --- Lead form ----------------------------------------------------------- */
.psos-chat__lead {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.9rem;
  border-top: 2px solid var(--border-light);
  background: var(--cream);
  overflow-y: auto;
}
.psos-chat__lead-intro { margin: 0; font-size: var(--text-sm); color: var(--text-contrast-improved); }
.psos-chat__field {
  border: 2px solid var(--navy);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.65rem;
  font-family: var(--font-body);
  font-size: var(--text-sm);
  background: #fff;
  color: var(--text-dark);
}
.psos-chat__field:focus-visible { outline: none; border-color: var(--retro-teal); box-shadow: 0 0 0 2px rgba(35, 128, 120, 0.25); }
.psos-chat__hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
.psos-chat__check { display: flex; align-items: center; gap: 0.45rem; font-size: var(--text-xs); color: var(--text-muted); }
.psos-chat__lead-actions { display: flex; gap: 0.5rem; }
.psos-chat__lead-send {
  flex: 1;
  background: var(--color-primary);
  color: var(--navy);
  border: 2px solid var(--navy);
  border-radius: var(--radius-md);
  padding: 0.55rem;
  font-family: var(--font-heading);
  font-weight: var(--weight-bold);
  font-size: var(--text-sm);
  cursor: pointer;
}
.psos-chat__lead-send:disabled { opacity: 0.6; cursor: not-allowed; }
.psos-chat__lead-cancel {
  background: transparent;
  border: 2px solid var(--navy);
  color: var(--navy);
  border-radius: var(--radius-md);
  padding: 0.55rem 0.8rem;
  font-size: var(--text-sm);
  cursor: pointer;
}
.psos-chat__lead-status { font-size: var(--text-xs); color: var(--text-muted); min-height: 1em; }
.psos-chat__lead-done { font-size: var(--text-sm); color: var(--text-dark); line-height: var(--leading-relaxed); }

/* --- Mobile sheet (<640px) ---------------------------------------------- */
@media (max-width: 640px) {
  .psos-chat { right: var(--space-sm); bottom: var(--space-sm); }
  .psos-chat__launcher-label { display: none; }
  .psos-chat__launcher { padding: 0.7rem; }
  .psos-chat__panel {
    right: 0; left: 0; bottom: 0; top: 0;
    width: 100vw; height: 100vh;
    height: 100dvh;
    border-radius: 0;
    border: none;
  }
}

/* --- Reduced motion ------------------------------------------------------ */
@media (prefers-reduced-motion: reduce) {
  .psos-chat *,
  .psos-chat__log {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
