/* The agent widget: a pill in the bottom-right corner that opens into a card
   where visitors call or chat with Centillion's AI agent. agent-widget.js
   opens and closes it; agent-demo.js runs the call and the chat.

   Opening, the white shell grows from the pill into the card, anchored at the
   corner, and the card's content stays put while the shell uncovers it. The
   orb travels from the pill's left end to the middle of the card, the pill's
   label fades away, and the message box slides up from below. Closing plays
   it backwards. Every position is relative to the shell's bottom-right
   corner, which never moves. */

.aw {
  --aw-inset: 16px;
  --aw-pill-w: 252px; /* measured by agent-widget.js */
  --aw-pill-h: 56px;
  --aw-card-w: min(380px, calc(100vw - 2 * var(--aw-inset)));
  --aw-card-h: min(480px, calc(100vh - 2 * var(--aw-inset)));
  --aw-radius: 28px;
  --aw-orb: 160px;
  --aw-dur: 440ms;
  --aw-ease: cubic-bezier(0.4, 0, 0.1, 1);
  --aw-fade: 260ms cubic-bezier(0.25, 0.46, 0.45, 0.94); /* ease-out-quad */

  position: fixed;
  right: max(var(--aw-inset), env(safe-area-inset-right));
  bottom: max(var(--aw-inset), env(safe-area-inset-bottom));
  z-index: 60;
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
}
@supports (height: 100dvh) {
  .aw { --aw-card-h: min(480px, calc(100dvh - 2 * var(--aw-inset))); }
}
@media (min-width: 768px) {
  .aw { --aw-inset: 24px; }
}
.aw.is-ready {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity 300ms var(--ease-out), transform 300ms var(--ease-out);
}
/* Sizes are measured before the pill shows; nothing animates into them. */
.aw:not(.is-ready) * { transition: none !important; }

/* ---------------------------------------------------------------- shell */

.aw-shell {
  position: absolute;
  right: 0;
  bottom: 0;
  width: var(--aw-pill-w);
  height: var(--aw-pill-h);
  overflow: hidden;
  isolation: isolate;
  border-radius: var(--aw-radius);
  background: var(--bg);
  box-shadow: 0 0 0 1px rgba(10, 10, 10, 0.06), 0 2px 4px rgba(10, 10, 10, 0.04), 0 6px 16px rgba(10, 10, 10, 0.06);
  transition:
    width var(--aw-dur) var(--aw-ease),
    height var(--aw-dur) var(--aw-ease),
    box-shadow var(--aw-dur) var(--aw-ease);
}
.aw[data-open="true"] .aw-shell {
  width: var(--aw-card-w);
  height: var(--aw-card-h);
  box-shadow: 0 0 0 1px rgba(10, 10, 10, 0.06), 0 2px 4px rgba(10, 10, 10, 0.04), 0 16px 40px rgba(10, 10, 10, 0.12);
}

/* ---------------------------------------------------------------- pill */

.aw-pill {
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: 2;
  width: max-content;
  height: var(--aw-pill-h);
  padding: 0 22px 0 58px;
  display: flex;
  align-items: center;
  border: 0;
  border-radius: var(--aw-radius);
  background: transparent;
  font: inherit;
  font-size: 15px;
  font-weight: 500;
  line-height: 1;
  color: var(--ink);
  white-space: nowrap;
  cursor: pointer;
  /* Closing: the label waits for the shell to come most of the way back, then
     fades in as it slides home. */
  transition:
    transform var(--aw-dur) var(--aw-ease),
    opacity var(--aw-fade) 300ms,
    background-color var(--motion);
}
.aw-pill:focus-visible { outline: none; box-shadow: inset 0 0 0 2px var(--ink); }
@media (hover: hover) and (pointer: fine) {
  .aw-pill:hover { background: var(--bg-sunken); }
}
.aw[data-open="true"] .aw-pill {
  pointer-events: none;
  opacity: 0;
  /* As in the shell's growth: the label drifts by half the added width. */
  transform: translateX(calc((var(--aw-card-w) - var(--aw-pill-w)) / 2));
  transition:
    transform var(--aw-dur) var(--aw-ease),
    opacity var(--aw-fade),
    background-color var(--motion);
}

/* ---------------------------------------------------------------- card */

/* The card keeps its full size, pinned to the shell's corner, and the shell
   shows as much of it as it covers. No z-index: the orb inside it stacks
   above the pill. */
.aw-card {
  position: absolute;
  right: 0;
  bottom: 0;
  width: var(--aw-card-w);
  height: var(--aw-card-h);
  outline: none;
}
.aw:not([data-open="true"]) .aw-card { pointer-events: none; }

.aw-head {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  opacity: 0;
  transition: opacity var(--aw-fade);
}
.aw[data-open="true"] .aw-head { opacity: 1; }
.aw-title {
  margin-right: auto;
  padding-left: 46px;
  font-size: 14px;
  font-weight: 500;
  line-height: 40px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 200ms ease, visibility 0s linear 200ms;
}
.aw-icon-btn {
  width: 40px;
  height: 40px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: var(--radius-pill);
  background: var(--bg-subtle);
  color: var(--ink);
  cursor: pointer;
  transition: background-color var(--motion), transform 160ms var(--ease-out), opacity 200ms ease, visibility 0s linear 0s;
}
@media (hover: hover) and (pointer: fine) {
  .aw-icon-btn:hover { background: var(--bg-muted); }
}
.aw-icon-btn:active { transform: scale(0.95); }
.aw-icon-btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.aw-to-voice {
  opacity: 0;
  visibility: hidden;
  transition: background-color var(--motion), transform 160ms var(--ease-out), opacity 200ms ease, visibility 0s linear 200ms;
}
.aw-card[data-mode="chat"] .aw-title,
.aw-card[data-mode="chat"] .aw-to-voice {
  opacity: 1;
  visibility: visible;
  transition: background-color var(--motion), transform 160ms var(--ease-out), opacity 240ms ease-out 160ms, visibility 0s linear 0s;
}

/* ---------------------------------------------------------------- orb */

/* The orb's box is --aw-orb square at full size and scales from its
   bottom-right corner, which sits on the shell's. Pill: 38 px at the left end.
   Voice: centred above the middle. Chat: 32 px at the top-left. */
.aw-orb {
  position: absolute;
  right: 0;
  bottom: 0;
  z-index: 3;
  width: var(--aw-orb);
  height: var(--aw-orb);
  pointer-events: none;
  transform-origin: 100% 100%;
  transform: translate(calc(47px - var(--aw-pill-w)), -9px) scale(0.2375);
  transition: transform var(--aw-dur) var(--aw-ease);
}
.aw[data-open="true"] .aw-orb {
  transform: translate(calc((var(--aw-orb) - var(--aw-card-w)) / 2), calc(var(--aw-card-h) / -2)) scale(1);
}
.aw[data-open="true"] .aw-card[data-mode="chat"] .aw-orb {
  transform: translate(calc(48px - var(--aw-card-w)), calc(48px - var(--aw-card-h))) scale(0.2);
}
/* Same canvas bleed as the agent preview's orb, so the shadow and the
   caustic under the drops aren't cut off. */
.aw-orb-canvas { position: absolute; left: -14%; top: -14%; width: 128%; height: 128%; }
.aw-orb .orb-fallback { inset: 8%; width: 84%; height: 84%; }
.aw-card:not(.has-orb) .aw-orb-canvas { display: none; }

/* The call button sits under the drops, as in the agent preview, and rides
   with the orb. */
.aw-call {
  position: absolute;
  left: 50%;
  top: calc(100% - 4px);
  width: 44px;
  height: 44px;
  margin-left: -22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: var(--radius-pill);
  background: var(--ink);
  color: var(--on-ink);
  box-shadow: 0 1px 2px rgba(10, 10, 10, 0.12), 0 6px 16px rgba(10, 10, 10, 0.16);
  cursor: pointer;
  pointer-events: auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 120ms ease-out, visibility 0s linear 120ms, transform 160ms var(--ease-out), background-color var(--motion), color var(--motion);
}
.aw[data-open="true"] .aw-card[data-mode="voice"] .aw-call {
  opacity: 1;
  visibility: visible;
  transition: opacity 220ms ease-out 200ms, visibility 0s linear 0s, transform 160ms var(--ease-out), background-color var(--motion), color var(--motion);
}
@media (hover: hover) and (pointer: fine) {
  .aw-call:hover { transform: scale(1.06); }
}
.aw-call:active { transform: scale(0.95); }
.aw-call:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
.aw-call .orb-call-end { display: none; }
.aw-call.is-live { background: var(--bg); color: var(--ink); box-shadow: inset 0 0 0 1px var(--line-strong), 0 1px 2px rgba(10, 10, 10, 0.08), 0 6px 16px rgba(10, 10, 10, 0.10); }
.aw-call.is-live .orb-call-start { display: none; }
.aw-call.is-live .orb-call-end { display: block; }

/* ---------------------------------------------------------------- voice */

.aw-voice,
.aw-chat {
  position: absolute;
  left: 0;
  right: 0;
  opacity: 1;
  visibility: visible;
  transition: opacity 240ms ease-out 140ms, visibility 0s linear 0s;
}
/* agent-demo.js hides the pane it isn't showing; here it fades instead. */
.aw-voice[hidden],
.aw-chat[hidden] {
  display: block;
  opacity: 0;
  visibility: hidden;
  transition: opacity 160ms ease-out, visibility 0s linear 160ms;
}
.aw-voice {
  top: calc(50% + 56px);
  padding: 0 28px;
  text-align: center;
}
.aw-status { font-size: 14px; font-weight: 500; line-height: 20px; }
.aw-sub { display: grid; margin-top: 4px; }
/* The orb crosses this text on its way to the middle of the card, so the
   text comes in behind it and leaves before it comes back. */
.aw-voice > * {
  opacity: 0;
  transition: opacity 140ms ease-out;
}
.aw[data-open="true"] .aw-voice > * {
  opacity: 1;
  transition: opacity var(--aw-fade) 170ms;
}
.aw-desc,
.aw-caption {
  grid-area: 1 / 1;
  font-size: 13px;
  line-height: 19px;
  color: var(--ink-3);
  transition: opacity 200ms ease;
}
.aw-desc { text-wrap: balance; }
.aw-desc span { display: block; margin-top: 6px; font-size: 12px; line-height: 16px; color: var(--ink-4); }
.aw-caption { opacity: 0; }
.aw-card:not([data-state="idle"]) .aw-desc { opacity: 0; }
.aw-card:not([data-state="idle"]) .aw-caption { opacity: 1; }

/* ---------------------------------------------------------------- chat */

/* The conversation runs down from under the header, in bubbles: the agent's
   filled warm grey, the visitor's white with a hairline ring. Each bubble's
   top corner on its speaker's side is tighter than the others. */
.aw-chat { top: 56px; bottom: 76px; }
.aw .chat-log {
  height: 100%;
  min-height: 0;
  max-height: none;
  justify-content: flex-start;
  gap: 8px;
  padding: 8px 16px;
  overscroll-behavior: contain;
  /* Bubbles scrolled up under the header fade out instead of being cut. */
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 8px);
  mask-image: linear-gradient(to bottom, transparent 0, #000 8px);
}
.aw .chat-msg { max-width: 90%; }
.aw .chat-msg p {
  padding: 11px 16px;
  font-size: 15px;
  line-height: 22px;
  color: var(--ink);
  overflow-wrap: anywhere;
}
.aw .chat-agent p {
  border-radius: 8px 20px 20px 20px;
  background: #f5f3f1;
}
.aw .chat-user p {
  padding: 11px 18px;
  border-radius: 20px 8px 20px 20px;
  background: var(--bg);
  box-shadow: 0 0 0 0.5px rgba(10, 10, 10, 0.14), 0 1px 1px rgba(10, 10, 10, 0.04), 0 2px 4px rgba(10, 10, 10, 0.04);
}
.aw .chat-msg.is-typing p { color: var(--ink-4); }

.aw-form {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 16px;
  width: auto;
  margin: 0;
  /* Closing: down and out of the shell, at once. */
  transform: translateY(80px);
  transition: transform 280ms cubic-bezier(0.25, 0.46, 0.45, 0.94), box-shadow var(--motion);
}
.aw[data-open="true"] .aw-form {
  transform: none;
  /* Opening: up from below once the shell has room for it. */
  transition: transform 300ms cubic-bezier(0.165, 0.84, 0.44, 1) 200ms, box-shadow var(--motion);
}

@media (prefers-reduced-motion: reduce) {
  .aw.is-ready,
  .aw-shell,
  .aw-pill,
  .aw[data-open="true"] .aw-pill,
  .aw-head,
  .aw-title,
  .aw-icon-btn,
  .aw-to-voice,
  .aw-card[data-mode="chat"] .aw-title,
  .aw-card[data-mode="chat"] .aw-to-voice,
  .aw-orb,
  .aw-call,
  .aw[data-open="true"] .aw-card[data-mode="voice"] .aw-call,
  .aw-voice,
  .aw-chat,
  .aw-voice > *,
  .aw[data-open="true"] .aw-voice > *,
  .aw-voice[hidden],
  .aw-chat[hidden],
  .aw-desc,
  .aw-caption,
  .aw-form,
  .aw[data-open="true"] .aw-form { transition: none; }
  .aw { transform: none; }
  .aw-icon-btn:active,
  .aw-call:active { transform: none; }
}
