/* ===================================================
   TaskMasters — Chatbot Widget
   UI/UX only. Backend: N8N integration pending.
   Design system: DESIGN.md "The Quiet Ledger"
   =================================================== */

/* ── Trigger Button ───────────────────────────────── */

.tm-chat-trigger {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 9999;

  /* Flex so icon + label sit side-by-side in pill mode */
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 0;
  overflow: hidden;        /* clips label as button shrinks back to circle */

  /* Default = circle */
  height: 56px;
  width: 56px;
  border-radius: 28px;    /* fixed px so pill→circle interpolates cleanly */

  background: var(--tm-navy);
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(10, 31, 61, 0.30);

  /* Morph transitions */
  transition:
    width         380ms cubic-bezier(0.34, 1.1, 0.64, 1),
    border-radius 380ms cubic-bezier(0.34, 1.1, 0.64, 1),
    gap           300ms ease,
    padding       300ms ease,
    box-shadow    220ms ease,
    transform     220ms ease;

  /* One-shot entrance — slides up with a gentle spring */
  animation: tmTriggerIn 440ms cubic-bezier(0.34, 1.2, 0.64, 1) 600ms both;
}

/* Pill state — applied on load, removed after delay or on scroll */
.tm-chat-trigger.is-pill {
  width: 222px;
  border-radius: 999px;
  padding: 0 20px 0 14px;
  gap: 9px;
  justify-content: flex-start;
}

/* Pill hover — gentle scale only, no wiggle while label is showing */
.tm-chat-trigger.is-pill:hover {
  transform: scale(1.02);
  box-shadow: 0 8px 28px rgba(10, 31, 61, 0.42);
}

/* After the entrance animation completes, JS adds .tm-rested.
   This neutralises the entrance animation so hover can take over cleanly. */
.tm-chat-trigger.tm-rested {
  animation: none;
  opacity: 1;
}

/* Circle hover — wiggle, only when rested, not pill, not open */
.tm-chat-trigger.tm-rested:not(.is-pill):not(.is-open):hover {
  animation: tmBubbleWiggle 520ms ease;
  box-shadow: 0 8px 28px rgba(10, 31, 61, 0.42);
}

.tm-chat-trigger:focus-visible {
  outline: 2px solid var(--tm-emerald);
  outline-offset: 3px;
}

/* Icon slot — holds the two swappable icons (chat ↔ close) */
.tm-ct-icon-slot {
  position: relative;
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tm-ct-icon {
  position: absolute;
  color: var(--tm-cream);
  font-size: 21px;
  line-height: 1;
  transition: opacity 200ms ease, transform 220ms ease;
}

.tm-ct-icon--open  { opacity: 1; transform: rotate(0deg) scale(1); }
.tm-ct-icon--close { opacity: 0; transform: rotate(-80deg) scale(0.6); }

.tm-chat-trigger.is-open .tm-ct-icon--open  { opacity: 0; transform: rotate(80deg) scale(0.6); }
.tm-chat-trigger.is-open .tm-ct-icon--close { opacity: 1; transform: rotate(0deg) scale(1); }

/* Pill label */
.tm-ct-label {
  font-family: var(--tm-font-body);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--tm-cream);
  white-space: nowrap;
  overflow: hidden;
  flex-shrink: 0;

  /* Hidden in circle state — zero width keeps icon centered */
  max-width: 0;
  opacity: 0;
  transition:
    max-width 380ms cubic-bezier(0.34, 1.1, 0.64, 1),
    opacity   160ms ease;                    /* fast fade-out on collapse */
}

.tm-chat-trigger.is-pill .tm-ct-label {
  max-width: 170px;
  opacity: 1;
  transition:
    max-width 380ms cubic-bezier(0.34, 1.1, 0.64, 1),
    opacity   220ms ease 200ms;              /* slight delay: button expands first */
}

/* Online dot */
.tm-chat-online-dot {
  position: absolute;
  top: 11px;
  right: 11px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--tm-emerald);
  border: 2px solid var(--tm-navy);
  animation: tmDotPulse 2.4s ease-in-out infinite;

  /* Hidden while pill is shown or chat is open */
  opacity: 0;
  transition: opacity 300ms ease 200ms;     /* fades in gently after pill collapses */
}

/* Dot only visible in collapsed-circle state */
.tm-chat-trigger:not(.is-pill) .tm-chat-online-dot { opacity: 1; }
.tm-chat-trigger.is-open        .tm-chat-online-dot { opacity: 0 !important; }

/* ── Chat Window ──────────────────────────────────── */

.tm-chatbot {
  position: fixed;
  bottom: 96px;
  right: 28px;
  z-index: 9998;
  width: 360px;
  max-height: 520px;
  border-radius: var(--tm-radius-xl);
  background: var(--tm-cream);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow:
    0 20px 60px rgba(10, 31, 61, 0.20),
    0 4px 16px  rgba(10, 31, 61, 0.10);

  /* Closed state */
  opacity: 0;
  transform: translateY(18px) scale(0.97);
  pointer-events: none;
  transition:
    opacity    240ms ease,
    transform  280ms cubic-bezier(0.34, 1.18, 0.64, 1);
}

.tm-chatbot.is-open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Header */
.tm-chatbot__header {
  background: var(--tm-navy);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  gap: 8px;
}

.tm-chatbot__header-info {
  display: flex;
  align-items: center;
  gap: 11px;
}

.tm-chatbot__avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--tm-emerald);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--tm-font-display);
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  letter-spacing: -0.01em;
  flex-shrink: 0;
}

.tm-chatbot__name {
  font-family: var(--tm-font-display);
  font-size: 14.5px;
  font-weight: 600;
  color: var(--tm-cream);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.tm-chatbot__status {
  font-family: var(--tm-font-body);
  font-size: 11px;
  color: rgba(250, 250, 245, 0.60);
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}

.tm-status-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--tm-emerald);
  animation: tmDotPulse 2.4s ease-in-out infinite;
}

.tm-chatbot__close {
  background: none;
  border: none;
  color: rgba(250, 250, 245, 0.55);
  cursor: pointer;
  padding: 5px;
  border-radius: var(--tm-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  line-height: 1;
  transition: color 160ms ease, background 160ms ease;
  flex-shrink: 0;
}

.tm-chatbot__close:hover {
  color: var(--tm-cream);
  background: rgba(255, 255, 255, 0.10);
}

/* Body / message scroll area */
.tm-chatbot__body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  scroll-behavior: smooth;
}

/* ── Messages ─────────────────────────────────────── */

.tm-msg {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  animation: tmMsgIn 200ms ease forwards;
  opacity: 0;
}

.tm-msg--bot  { flex-direction: row; }
.tm-msg--user { flex-direction: row-reverse; }

.tm-msg__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--tm-emerald);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--tm-font-display);
  font-size: 10px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

.tm-msg__bubble {
  max-width: 242px;
  padding: 9px 13px;
  font-family: var(--tm-font-body);
  font-size: 13.5px;
  line-height: 1.5;
  border-radius: 14px;
  word-break: break-word;
}

.tm-msg--bot .tm-msg__bubble {
  background: #fff;
  border: 1px solid var(--tm-line-200);
  color: var(--tm-ink-700);
  border-bottom-left-radius: 4px;
}

.tm-msg--user .tm-msg__bubble {
  background: var(--tm-navy);
  color: var(--tm-cream);
  border-bottom-right-radius: 4px;
}

/* Links inside bot bubbles */
.tm-msg--bot .tm-msg__bubble a {
  color: var(--tm-emerald);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;
}

.tm-msg--bot .tm-msg__bubble a:hover {
  opacity: 0.80;
}

/* ── Typing Indicator ─────────────────────────────── */

.tm-typing {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  animation: tmMsgIn 200ms ease forwards;
  opacity: 0;
}

.tm-typing__dots {
  background: #fff;
  border: 1px solid var(--tm-line-200);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  padding: 11px 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.tm-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--tm-ink-500);
  animation: tmTypingBounce 600ms ease-in-out infinite;
}

.tm-typing__dot:nth-child(2) { animation-delay: 0.13s; }
.tm-typing__dot:nth-child(3) { animation-delay: 0.26s; }

/* ── Quick Reply Chips ────────────────────────────── */

.tm-quick-replies {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  padding-left: 36px; /* visually aligns under bot bubble text */
}

.tm-qr {
  background: transparent;
  border: 1.5px solid var(--tm-navy-2);
  color: var(--tm-navy);
  font-family: var(--tm-font-body);
  font-size: 12.5px;
  font-weight: 500;
  padding: 6px 13px;
  border-radius: var(--tm-radius-pill);
  cursor: pointer;
  white-space: nowrap;
  transition: background 180ms ease, color 180ms ease, border-color 180ms ease;
  animation: tmQrIn 200ms ease forwards;
  opacity: 0;
}

.tm-qr:hover {
  background: var(--tm-navy);
  color: var(--tm-cream);
  border-color: var(--tm-navy);
}

/* ── Footer / Input ───────────────────────────────── */

.tm-chatbot__footer {
  border-top: 1px solid var(--tm-line-200);
  padding: 11px 13px 10px;
  flex-shrink: 0;
  background: #fff;
}

.tm-chatbot__input-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tm-chatbot__input {
  flex: 1;
  border: 1.5px solid var(--tm-line-300);
  border-radius: var(--tm-radius-pill);
  padding: 9px 15px;
  font-family: var(--tm-font-body);
  font-size: 13.5px;
  color: var(--tm-ink-700);
  background: var(--tm-cream);
  outline: none;
  transition: border-color 180ms ease;
  min-width: 0;
}

.tm-chatbot__input::placeholder { color: var(--tm-ink-500); }

.tm-chatbot__input:focus { border-color: var(--tm-emerald); }

.tm-chatbot__send {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--tm-emerald);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
  transition: background 180ms ease, transform 180ms ease;
}

.tm-chatbot__send:hover {
  background: var(--tm-emerald-deep);
  transform: scale(1.06);
}

.tm-chatbot__disclaimer {
  font-family: var(--tm-font-body);
  font-size: 10.5px;
  color: var(--tm-ink-500);
  text-align: center;
  margin-top: 7px;
}

/* ── Keyframes ────────────────────────────────────── */

@keyframes tmTriggerIn {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0);    }
}

@keyframes tmDotPulse {
  0%, 100% { transform: scale(1);   opacity: 1;   }
  50%       { transform: scale(1.4); opacity: 0.7; }
}

@keyframes tmMsgIn {
  from { opacity: 0; transform: translateY(7px); }
  to   { opacity: 1; transform: translateY(0);   }
}

@keyframes tmTypingBounce {
  0%, 60%, 100% { transform: translateY(0);    }
  30%            { transform: translateY(-5px); }
}

@keyframes tmQrIn {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: translateY(0);   }
}

@keyframes tmBubbleWiggle {
  0%   { transform: rotate(0deg);              }
  12%  { transform: rotate(-12deg) scale(1.08); }
  25%  { transform: rotate(10deg)  scale(1.05); }
  37%  { transform: rotate(-8deg)  scale(1.03); }
  50%  { transform: rotate(6deg)   scale(1.01); }
  62%  { transform: rotate(-4deg);              }
  75%  { transform: rotate(2deg);               }
  87%  { transform: rotate(-1deg);              }
  100% { transform: rotate(0deg);              }
}

/* ── Mobile ───────────────────────────────────────── */

@media (max-width: 480px) {
  .tm-chatbot {
    width: calc(100vw - 32px);
    right: 16px;
    bottom: 88px;
    max-height: 68vh;
  }

  .tm-chat-trigger {
    right: 16px;
    bottom: 20px;
  }
}

/* ── Reduced Motion ───────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .tm-chat-trigger,
  .tm-chatbot,
  .tm-ct-icon,
  .tm-ct-label,
  .tm-chat-online-dot,
  .tm-msg,
  .tm-typing,
  .tm-qr,
  .tm-chatbot__send {
    transition: none !important;
    animation: none !important;
  }

  /* Ensure trigger is visible immediately (no entrance anim) */
  .tm-chat-trigger { opacity: 1 !important; transform: none !important; }

  /* Pill label immediately visible/hidden based on class */
  .tm-ct-label           { max-width: 0;     opacity: 0; }
  .is-pill .tm-ct-label  { max-width: 170px; opacity: 1; }

  /* Dot immediately visible/hidden */
  .tm-chat-trigger:not(.is-pill) .tm-chat-online-dot { opacity: 1; }

  .tm-chatbot { opacity: 1; transform: none; }
  .tm-chatbot:not(.is-open) { display: none; }

  .tm-status-dot,
  .tm-typing__dot { animation: none !important; }

  .tm-msg,
  .tm-typing,
  .tm-qr { opacity: 1 !important; transform: none !important; }
}
