/* ============ Floating AI assistant widget ============ */
.cw {
  --cw-w: 384px;
  position: fixed;
  right: clamp(14px, 2.4vw, 28px);
  bottom: clamp(14px, 2.4vw, 28px);
  z-index: 9999;
  font-family: var(--font);
}

/* ---------- launcher ---------- */
.cw__launcher {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 1.15rem 0.55rem 0.6rem;
  border: 0;
  border-radius: var(--radius-pill);
  color: #fff;
  background: linear-gradient(135deg, var(--red-bright) 0%, var(--oxblood) 100%);
  box-shadow: var(--shadow-lift);
  transition: transform var(--dur) var(--ease), box-shadow var(--dur), opacity var(--dur);
}
.cw__launcher:hover { transform: translateY(-2px) scale(1.02); box-shadow: 0 14px 30px rgba(124,10,34,0.4); }
.cw__launcher-ic {
  display: grid; place-items: center;
  width: 40px; height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.16);
}
.cw__launcher-tx { display: flex; flex-direction: column; line-height: 1.15; text-align: left; }
.cw__launcher-tx b { font-size: 0.96rem; font-weight: 700; }
.cw__launcher-tx span { font-size: 0.72rem; opacity: 0.85; }
.cw__launcher::after {
  content: "";
  position: absolute; top: 6px; right: 6px;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #36d27a;
  box-shadow: 0 0 0 3px rgba(54,210,122,0.3);
  animation: cw-pulse 2.4s var(--ease) infinite;
}
@keyframes cw-pulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(54,210,122,0.3); }
  50% { box-shadow: 0 0 0 7px rgba(54,210,122,0); }
}

/* one-time greeting bubble above launcher */
.cw__nudge {
  position: absolute;
  right: 4px;
  bottom: calc(100% + 12px);
  width: 230px;
  padding: 0.7rem 0.9rem;
  background: #fff;
  color: var(--ink);
  font-size: 0.85rem;
  border-radius: 14px 14px 4px 14px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line-cool);
  transform-origin: bottom right;
  animation: cw-pop var(--dur) var(--ease) both;
}
.cw__nudge b { color: var(--red); }
.cw__nudge-x {
  position: absolute; top: -8px; right: -8px;
  width: 22px; height: 22px; border-radius: 50%;
  border: 1px solid var(--line-cool); background: #fff; color: var(--muted);
  font-size: 0.8rem; line-height: 1;
}

/* ---------- panel ---------- */
.cw__panel {
  position: absolute;
  right: 0;
  bottom: 0;
  width: min(var(--cw-w), calc(100vw - 28px));
  height: min(640px, calc(100vh - 40px));
  display: flex;
  flex-direction: column;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  border: 1px solid var(--line-cool);
  overflow: hidden;
  transform-origin: bottom right;
  animation: cw-open var(--dur) var(--ease) both;
}
@keyframes cw-open {
  from { opacity: 0; transform: translateY(16px) scale(0.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes cw-pop {
  from { opacity: 0; transform: scale(0.8); }
  to { opacity: 1; transform: scale(1); }
}
.cw[hidden] { display: none; }
[hidden] { display: none !important; }

/* header */
.cw__head {
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.8rem 0.9rem;
  color: #fff;
  background: linear-gradient(135deg, var(--oxblood) 0%, var(--red) 60%, var(--red-bright) 100%);
  position: relative;
}
.cw__head::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 3px;
  background: linear-gradient(90deg, var(--gold), var(--gold-soft), var(--gold));
}
.cw__avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: #fff; padding: 2px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}
.cw__id { flex: 1; line-height: 1.2; }
.cw__id b { font-size: 0.98rem; font-weight: 700; }
.cw__id .cw__status { display: flex; align-items: center; gap: 0.35rem; font-size: 0.74rem; opacity: 0.92; }
.cw__status::before {
  content: ""; width: 7px; height: 7px; border-radius: 50%; background: #36d27a;
  box-shadow: 0 0 0 2px rgba(255,255,255,0.25);
}
.cw__head-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: 0; background: rgba(255,255,255,0.14); color: #fff;
  display: grid; place-items: center; font-size: 1.1rem;
  transition: background var(--dur-fast);
}
.cw__head-btn:hover { background: rgba(255,255,255,0.28); }

/* messages */
.cw__body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3);
  display: flex; flex-direction: column; gap: 0.7rem;
  background:
    radial-gradient(600px 200px at 100% 0, var(--pink) 0%, transparent 70%),
    var(--bg-tint);
  /* no scroll-behavior:smooth — per-chunk smooth scrolls fight each other and stutter.
     JS pins to bottom instantly while a steady rAF reveal keeps motion smooth. */
  overflow-anchor: none;
}
.cw__body::-webkit-scrollbar { width: 8px; }
.cw__body::-webkit-scrollbar-thumb { background: #d9d2c8; border-radius: 8px; }

.msg { display: flex; gap: 0.5rem; max-width: 90%; animation: cw-msg var(--dur) var(--ease) both; }
@keyframes cw-msg { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.msg__ava {
  flex: none; width: 28px; height: 28px; border-radius: 50%;
  background: #fff; border: 1px solid var(--line); padding: 1px; align-self: flex-end;
}
.msg__bubble {
  padding: 0.6rem 0.85rem;
  border-radius: 14px;
  font-size: 0.92rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg--bot { align-self: flex-start; }
.msg--bot .msg__bubble {
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line-cool);
  border-bottom-left-radius: 4px;
}
.msg--user { align-self: flex-end; flex-direction: row-reverse; }
.msg--user .msg__bubble {
  background: linear-gradient(135deg, var(--red) 0%, var(--oxblood) 100%);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg__bubble strong { font-weight: 700; }
.msg__bubble a { color: var(--gold); text-decoration: underline; }
.msg--bot .msg__bubble a { color: var(--red); }
.msg__bubble ol, .msg__bubble ul { margin: 0.3rem 0; padding-inline-start: 1.3rem; }
.msg__bubble li { margin: 0.15rem 0; }
.msg__bubble code {
  background: var(--paper-2, #f3ece2); padding: 0 4px; border-radius: 4px; font-size: 0.88em;
}

/* welcome card */
.cw__welcome {
  background: #fff;
  border: 1px solid var(--line-cool);
  border-radius: 14px;
  padding: var(--space-3);
  box-shadow: var(--shadow-sm);
}
.cw__welcome h3 { margin: 0 0 0.3rem; font-size: 1.02rem; color: var(--oxblood); }
.cw__welcome p { margin: 0 0 var(--space-2); font-size: 0.88rem; color: var(--ink-soft); }
.cw__welcome .cw__label { font-size: 0.76rem; font-weight: 700; color: var(--muted); text-transform: none; margin-bottom: 0.4rem; }

/* quick-reply chips */
.cw__chips { display: flex; flex-wrap: wrap; gap: 0.45rem; }
.cw__chip {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.5rem 0.8rem;
  font-size: 0.84rem; font-weight: 600;
  color: var(--oxblood);
  background: var(--pink);
  border: 1px solid var(--pink-line);
  border-radius: var(--radius-pill);
  transition: transform var(--dur-fast), background var(--dur-fast), box-shadow var(--dur-fast);
}
.cw__chip:hover { background: #fff; transform: translateY(-1px); box-shadow: var(--shadow-sm); border-color: var(--red); }
.cw__chip .ic { font-size: 1rem; }

.cw__suggest { display: flex; flex-direction: column; gap: 0.4rem; align-self: flex-start; max-width: 92%; }
.cw__suggest .cw__suggest-label { font-size: 0.72rem; color: var(--muted); padding-inline-start: 0.2rem; }

/* typing indicator */
.cw__typing { display: inline-flex; gap: 4px; padding: 0.7rem 0.9rem; }
.cw__typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--muted);
  animation: cw-bounce 1.2s var(--ease) infinite;
}
.cw__typing span:nth-child(2) { animation-delay: 0.18s; }
.cw__typing span:nth-child(3) { animation-delay: 0.36s; }
@keyframes cw-bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* streaming text + typewriter caret */
.msg__bubble.is-streaming { min-height: 1.15em; }
.msg__txt { white-space: pre-wrap; }
.cw__cursor {
  display: inline-block;
  width: 7px; height: 1.02em;
  margin-left: 2px;
  vertical-align: -2px;
  border-radius: 2px;
  background: var(--red);
  animation: cw-caret 1.05s steps(1) infinite;
}
@keyframes cw-caret { 0%, 49% { opacity: 0.85; } 50%, 100% { opacity: 0.12; } }
/* fade the formatted answer in when it replaces the plain stream (opacity only → no layout shift) */
.msg__bubble.is-final { animation: cw-fadein var(--dur) var(--ease) both; }
@keyframes cw-fadein { from { opacity: 0.6; } to { opacity: 1; } }

/* suggestion chips ease in, staggered via inline animation-delay */
.cw__suggest .cw__chip { animation: cw-chipin var(--dur) var(--ease) both; }
@keyframes cw-chipin { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* footer / composer */
.cw__foot {
  border-top: 1px solid var(--line-cool);
  background: #fff;
  padding: 0.6rem 0.7rem 0.5rem;
}
.cw__composer { display: flex; align-items: flex-end; gap: 0.5rem; }
.cw__input {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 0.6rem 0.8rem;
  font-family: inherit; font-size: 0.92rem; line-height: 1.5;
  color: var(--ink);
  border: 1.5px solid var(--line-cool);
  border-radius: 14px;
  background: var(--bg-tint);
  transition: border-color var(--dur-fast), box-shadow var(--dur-fast);
}
.cw__input:focus { outline: none; border-color: var(--red); box-shadow: 0 0 0 3px var(--pink); background: #fff; }
.cw__send {
  flex: none;
  width: 42px; height: 42px;
  border: 0; border-radius: 50%;
  color: #fff;
  background: linear-gradient(135deg, var(--red) 0%, var(--oxblood) 100%);
  display: grid; place-items: center;
  box-shadow: 0 4px 12px rgba(124,10,34,0.3);
  transition: transform var(--dur-fast), filter var(--dur-fast), opacity var(--dur-fast);
}
.cw__send:hover:not(:disabled) { transform: scale(1.06); filter: brightness(1.06); }
.cw__send:disabled { opacity: 0.45; cursor: not-allowed; }
.cw__disclaimer { margin: 0.4rem 0 0; text-align: center; font-size: 0.68rem; color: var(--muted); }

/* ---------- voice: mic, read-aloud, call overlay ---------- */
.cw__mic {
  flex: none;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1.5px solid var(--line-cool);
  background: #fff;
  color: var(--oxblood);
  display: grid; place-items: center;
  transition: border-color var(--dur-fast), background var(--dur-fast), color var(--dur-fast);
}
.cw__mic svg { width: 20px; height: 20px; }
.cw__mic:hover { border-color: var(--red); }
.cw__mic.is-listening {
  background: linear-gradient(135deg, var(--red), var(--oxblood));
  color: #fff;
  border-color: transparent;
  animation: cw-mic-pulse 1.3s var(--ease) infinite;
}
@keyframes cw-mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(200, 16, 46, 0.5); }
  50% { box-shadow: 0 0 0 11px rgba(200, 16, 46, 0); }
}

.cw__speak {
  display: flex; align-items: center; justify-content: center;
  width: 26px; height: 26px;
  margin-top: 0.45rem;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--line-cool);
  background: #fff;
  color: var(--muted);
  transition: color var(--dur-fast), border-color var(--dur-fast), background var(--dur-fast);
}
.cw__speak svg { width: 15px; height: 15px; }
.cw__speak:hover { color: var(--red); border-color: var(--red); }
.cw__speak.is-playing {
  color: #fff; background: var(--red); border-color: var(--red);
  animation: cw-mic-pulse 1.3s var(--ease) infinite;
}

.cw__voice {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  padding: 1.5rem;
  text-align: center;
  color: #fff;
  background: radial-gradient(circle at 50% 32%, #8d0f28 0%, #5c0716 60%, #2c0410 100%);
  animation: cw-fadein var(--dur) var(--ease) both;
}
.cw__voice-title { font-size: 1.05rem; font-weight: 700; opacity: 0.92; }
.cw__orb {
  position: relative;
  width: 150px; height: 150px;
  border-radius: 50%;
  border: 0;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
}
.cw__orb img { width: 78px; height: 78px; border-radius: 50%; background: #fff; padding: 3px; }
.cw__orb-ring { position: absolute; inset: 0; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.3); }
.cw__orb[data-mode="listening"] .cw__orb-ring { border-color: #36d27a; animation: cw-orb-listen 1.5s var(--ease) infinite; }
.cw__orb[data-mode="thinking"] .cw__orb-ring { border-color: rgba(255, 255, 255, 0.25); border-top-color: #fff; animation: cw-orb-spin 0.9s linear infinite; }
.cw__orb[data-mode="speaking"] .cw__orb-ring { border-color: var(--gold-soft); animation: cw-orb-speak 0.7s var(--ease) infinite; }
@keyframes cw-orb-listen { 0%, 100% { box-shadow: 0 0 0 0 rgba(54, 210, 122, 0.45); } 50% { box-shadow: 0 0 0 24px rgba(54, 210, 122, 0); } }
@keyframes cw-orb-spin { to { transform: rotate(360deg); } }
@keyframes cw-orb-speak { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.07); } }
.cw__voice-status { font-size: 1.05rem; font-weight: 700; }
.cw__voice-caption { min-height: 2.6em; max-width: 88%; font-size: 0.95rem; line-height: 1.55; opacity: 0.92; }
.cw__voice-hang {
  margin-top: 0.4rem;
  padding: 0.7rem 2.2rem;
  border: 0;
  border-radius: var(--radius-pill);
  background: #fff;
  color: var(--oxblood);
  font-weight: 800;
  font-size: 1rem;
  box-shadow: var(--shadow);
  transition: transform var(--dur-fast);
}
.cw__voice-hang:hover { transform: translateY(-1px); }

@media (max-width: 480px) {
  .cw { right: 10px; bottom: 10px; }
  .cw__launcher-tx { display: none; }
}

/* honor the OS "reduce motion" setting — fully stop looping + entrance animations */
@media (prefers-reduced-motion: reduce) {
  .cw__launcher::after,
  .cw__nudge,
  .cw__panel,
  .msg,
  .cw__typing span,
  .msg__bubble.is-final,
  .cw__suggest,
  .cw__suggest .cw__chip,
  .cw__voice,
  .cw__orb-ring,
  .cw__mic.is-listening,
  .cw__speak.is-playing {
    animation: none !important;
    transition: none !important;
    opacity: 1 !important;
    transform: none !important;
  }
  .cw__cursor { animation: none !important; opacity: 0.85; }
}
