/*
 * Cosmo chat widget.
 *
 * Matches the site's existing language: square corners, hard offset shadows,
 * Space Grotesk with a mono label face, and the blue from apex-grid.css. The
 * tokens are redeclared locally so the widget renders correctly regardless of
 * stylesheet order, and every selector is scoped under .cosmo-root so nothing
 * here can reach the rest of the page.
 *
 * No blur, no gradient chrome, no emoji. Targets are >=44px, focus is always
 * visible, and each transition names its own properties so nothing animates by
 * accident.
 */

.cosmo-root {
  --cosmo-bg: #08111e;
  --cosmo-bg-raised: #0a1524;
  --cosmo-line: #1e3248;
  --cosmo-line-strong: #315271;
  --cosmo-ink: #f4f8ff;
  --cosmo-muted: #91a0b5;
  --cosmo-dim: #66758a;
  --cosmo-blue: #51a7ff;
  --cosmo-blue-strong: #237bd1;
  --cosmo-mono: "SFMono-Regular", Consolas, "Liberation Mono", ui-monospace, monospace;
  --cosmo-sans: "Space Grotesk", system-ui, -apple-system, sans-serif;
}

.cosmo-root *,
.cosmo-root *::before,
.cosmo-root *::after {
  box-sizing: border-box;
}

/* --------------------------------------------------------------------------
   Launcher
   -------------------------------------------------------------------------- */

.cosmo-launcher {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 9997;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 6px 16px 6px 6px;
  border: 1px solid var(--cosmo-line-strong);
  border-radius: 0;
  background: var(--cosmo-bg);
  color: var(--cosmo-ink);
  font: 700 13px/1.2 var(--cosmo-sans);
  cursor: pointer;
  box-shadow: 6px 6px 0 rgba(35, 123, 209, 0.16);
  transition: border-color 140ms ease, background-color 140ms ease, box-shadow 140ms ease;
}

.cosmo-launcher:hover {
  border-color: var(--cosmo-blue);
  background: var(--cosmo-bg-raised);
  box-shadow: 6px 6px 0 rgba(35, 123, 209, 0.3);
}

.cosmo-launcher[hidden] {
  display: none;
}

.cosmo-launcher-avatar {
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  border: 1px solid var(--cosmo-line);
  background: var(--cosmo-bg-raised);
  object-fit: cover;
  object-position: center top;
}

.cosmo-launcher-label {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
}

.cosmo-launcher-label-short {
  display: none;
}

.cosmo-launcher-eyebrow {
  color: var(--cosmo-blue);
  font: 600 10px/1 var(--cosmo-mono);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* Unread marker for an operator reply that lands while the panel is closed. */
.cosmo-launcher-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  background: var(--cosmo-blue);
}

.cosmo-launcher-dot[hidden] {
  display: none;
}

/* --------------------------------------------------------------------------
   Panel
   -------------------------------------------------------------------------- */

.cosmo-panel {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  width: min(384px, calc(100vw - 32px));
  max-height: min(600px, calc(100vh - 32px));
  max-height: min(600px, calc(100svh - 32px));
  border: 1px solid var(--cosmo-line-strong);
  background: var(--cosmo-bg);
  color: var(--cosmo-ink);
  font: 500 14px/1.55 var(--cosmo-sans);
  box-shadow: 12px 12px 0 rgba(35, 123, 209, 0.14);
}

.cosmo-panel[hidden] {
  display: none;
}

.cosmo-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 14px 14px 16px;
  border-bottom: 1px solid var(--cosmo-line);
  background: var(--cosmo-bg-raised);
}

.cosmo-avatar {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border: 1px solid var(--cosmo-line-strong);
  background: var(--cosmo-bg);
  object-fit: cover;
  object-position: center top;
}

.cosmo-header-text {
  flex: 1 1 auto;
  min-width: 0;
}

.cosmo-title {
  margin: 0;
  font: 700 15px/1.2 var(--cosmo-sans);
  color: var(--cosmo-ink);
}

.cosmo-subtitle {
  margin: 3px 0 0;
  color: var(--cosmo-muted);
  font: 600 10px/1.3 var(--cosmo-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.cosmo-close {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  background: transparent;
  color: var(--cosmo-muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  transition: color 140ms ease, border-color 140ms ease;
}

.cosmo-close:hover {
  color: var(--cosmo-ink);
  border-color: var(--cosmo-line-strong);
}

/* --------------------------------------------------------------------------
   Message log
   -------------------------------------------------------------------------- */

.cosmo-log {
  flex: 1 1 auto;
  min-height: 180px;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.cosmo-msg {
  max-width: 88%;
  padding: 10px 12px;
  border: 1px solid var(--cosmo-line);
  background: var(--cosmo-bg-raised);
  color: var(--cosmo-ink);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.cosmo-msg-visitor {
  align-self: flex-end;
  border-color: var(--cosmo-blue-strong);
  background: rgba(81, 167, 255, 0.1);
}

.cosmo-msg-agent {
  border-left: 3px solid var(--cosmo-blue);
}

.cosmo-msg-system {
  align-self: stretch;
  max-width: 100%;
  border-style: dashed;
  border-color: var(--cosmo-line-strong);
  background: transparent;
  color: var(--cosmo-muted);
  font-size: 13px;
}

.cosmo-msg-author {
  display: block;
  margin-bottom: 4px;
  color: var(--cosmo-blue);
  font: 600 10px/1 var(--cosmo-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.cosmo-sources {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.cosmo-source-link {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 4px 10px;
  border: 1px solid var(--cosmo-line-strong);
  color: var(--cosmo-blue);
  font: 700 11px/1.2 var(--cosmo-sans);
  text-decoration: none;
  transition: border-color 140ms ease, color 140ms ease;
}

.cosmo-source-link:hover {
  border-color: var(--cosmo-blue);
  color: var(--cosmo-ink);
}

.cosmo-typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  align-self: flex-start;
  padding: 12px;
  border: 1px solid var(--cosmo-line);
}

.cosmo-typing[hidden] {
  display: none;
}

.cosmo-typing span {
  width: 6px;
  height: 6px;
  background: var(--cosmo-muted);
  animation: cosmo-blink 1.2s infinite ease-in-out;
}

.cosmo-typing span:nth-child(2) {
  animation-delay: 0.18s;
}

.cosmo-typing span:nth-child(3) {
  animation-delay: 0.36s;
}

@keyframes cosmo-blink {
  0%, 70%, 100% { opacity: 0.25; }
  35% { opacity: 1; }
}

/* --------------------------------------------------------------------------
   Footer: handoff prompt, contact form, composer
   -------------------------------------------------------------------------- */

.cosmo-footer {
  flex: 0 0 auto;
  min-height: 0;
  border-top: 1px solid var(--cosmo-line);
  background: var(--cosmo-bg-raised);
}

/* Contact capture is a panel view, not another fixed block beneath the chat.
   This keeps every field/button reachable when browser chrome or the mobile
   keyboard leaves only a few hundred vertical pixels. */
.cosmo-panel-contact .cosmo-log {
  display: none;
}

.cosmo-panel-contact .cosmo-footer {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  -webkit-overflow-scrolling: touch;
}

.cosmo-handoff {
  padding: 12px 16px;
  border-bottom: 1px solid var(--cosmo-line);
}

.cosmo-handoff[hidden],
.cosmo-contact[hidden],
.cosmo-composer[hidden] {
  display: none;
}

.cosmo-contact {
  padding: 14px 16px;
  display: grid;
  gap: 10px;
}

.cosmo-contact-intro {
  margin: 0;
  color: var(--cosmo-muted);
  font-size: 13px;
}

.cosmo-field {
  display: grid;
  gap: 4px;
}

.cosmo-label {
  color: var(--cosmo-muted);
  font: 600 10px/1 var(--cosmo-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.cosmo-input,
.cosmo-textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--cosmo-line-strong);
  border-radius: 0;
  background: var(--cosmo-bg);
  color: var(--cosmo-ink);
  font: 500 14px/1.4 var(--cosmo-sans);
  transition: border-color 140ms ease;
}

.cosmo-input:hover,
.cosmo-textarea:hover {
  border-color: var(--cosmo-blue-strong);
}

.cosmo-input::placeholder,
.cosmo-textarea::placeholder {
  color: var(--cosmo-dim);
}

.cosmo-textarea {
  min-height: 46px;
  max-height: 120px;
  resize: none;
}

.cosmo-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.cosmo-btn {
  appearance: none;
  min-height: 44px;
  padding: 10px 16px;
  border: 1px solid var(--cosmo-line-strong);
  border-radius: 0;
  background: transparent;
  color: var(--cosmo-ink);
  font: 700 13px/1.2 var(--cosmo-sans);
  cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.cosmo-btn:hover {
  border-color: var(--cosmo-blue);
  background: rgba(81, 167, 255, 0.1);
}

.cosmo-btn-primary {
  border-color: var(--cosmo-blue);
  background: var(--cosmo-blue);
  color: #03101e;
}

.cosmo-btn-primary:hover {
  background: var(--cosmo-ink);
  border-color: var(--cosmo-ink);
  color: #03101e;
}

.cosmo-btn[disabled] {
  opacity: 0.55;
  cursor: not-allowed;
}

.cosmo-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px;
}

.cosmo-composer .cosmo-textarea {
  flex: 1 1 auto;
}

.cosmo-send {
  flex: 0 0 auto;
  min-width: 64px;
}

.cosmo-note {
  margin: 0;
  padding: 0 16px 12px;
  color: var(--cosmo-dim);
  font-size: 12px;
}

.cosmo-error {
  margin: 0;
  padding: 10px 16px;
  border-top: 1px solid #7f2a3a;
  background: rgba(190, 60, 80, 0.12);
  color: #ffc9d2;
  font-size: 13px;
}

.cosmo-error[hidden] {
  display: none;
}

/* --------------------------------------------------------------------------
   Focus, motion, responsive
   -------------------------------------------------------------------------- */

.cosmo-root :focus-visible {
  outline: 2px solid var(--cosmo-blue);
  outline-offset: 2px;
}

.cosmo-input:focus-visible,
.cosmo-textarea:focus-visible {
  border-color: var(--cosmo-blue);
  outline-offset: 0;
}

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

@media (prefers-reduced-motion: reduce) {
  .cosmo-root *,
  .cosmo-root *::before,
  .cosmo-root *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

@media (max-width: 640px) {
  .cosmo-panel {
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
    max-height: calc(100vh - 16px);
    max-height: calc(100svh - 16px);
    box-shadow: 6px 6px 0 rgba(35, 123, 209, 0.14);
  }

  .cosmo-launcher {
    right: 12px;
    bottom: 12px;
  }

  .cosmo-msg {
    max-width: 92%;
  }
}

/* Keep the entry point named on narrow phones without crowding either corner. */
@media (max-width: 400px) {
  .cosmo-launcher-eyebrow,
  .cosmo-launcher-label-long {
    display: none;
  }

  .cosmo-launcher-label-short {
    display: inline;
  }

  .cosmo-launcher {
    gap: 8px;
    padding: 6px 10px 6px 6px;
  }
}
