/*
 * The chatbot's launcher as the Med Right mark, with a greeting bubble (resources/views/layouts/chat-launcher.blade.php,
 * behaviour in js/chat-launcher.js). Only transform and opacity are animated, so it stays smooth on slow phones.
 *
 * .mr-bot is a zero-size point at the centre of the bot's own launcher. The chat frame (#chatbotIframe, styled in
 * layouts/scripts.blade.php) sits 20px from the corner (10px on phones up to 768px), and the bot draws its launcher's
 * middle 48px from the frame's right and 54px from its bottom. Keep these numbers in step with the frame.
 */
.mr-bot {
  --mr-red: #DC2626;
  --mr-navy: #1F2937;
  --mr-ease: cubic-bezier(.22, 1, .36, 1);
  --mr-pop: cubic-bezier(.34, 1.4, .5, 1);
  --size: 56px;
  position: fixed;
  right: 68px;
  bottom: 74px;
  z-index: 999999; /* over the chat frame (same level, later in the page); under the phone menu panel (1000000) */
  width: 0;
  height: 0;
  pointer-events: none; /* clicks on the mark reach the bot's launcher underneath */
}
@media (max-width: 768px) {
  .mr-bot {
    right: 58px;
    bottom: 64px;
  }
}

/* While the mark covers it, only the circle under the mark is left of the closed frame, so none of the bot's own
   icon shows and only that circle catches clicks. */
#chatbotIframe.has-mark:not(.bot-open) {
  -webkit-clip-path: circle(28px at calc(100% - 48px) calc(100% - 54px));
  clip-path: circle(28px at calc(100% - 48px) calc(100% - 54px));
}
/* Between 660px and 768px the bot minimises to its own wide pill once it has been used; leave that to the bot. */
@media (min-width: 660px) and (max-width: 768px) {
  #chatbotIframe.has-mark.bot-used:not(.bot-open) {
    -webkit-clip-path: inset(calc(100% - var(--bot-hit-h)) 0 0 calc(100% - var(--bot-hit-w)));
    clip-path: inset(calc(100% - var(--bot-hit-h)) 0 0 calc(100% - var(--bot-hit-w)));
  }
  #chatbotIframe.bot-used ~ .mr-bot {
    display: none;
  }
}

/* ---- The mark ---------------------------------------------------------------------------------------------------- */

.mr-bot__badge {
  position: absolute;
  left: calc(var(--size) / -2);
  top: calc(var(--size) / -2);
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: #fff; /* covers the bot's icon while the disc rebuilds */
  box-shadow: 0 14px 28px -12px rgba(220, 38, 38, .6), 0 2px 8px rgba(17, 24, 39, .14);
  /* No fade back in when the chat closes: the frame is clipped back to the circle at once, and a fading mark would
     let the bot's own icon show through. The mark comes back whole and then rebuilds (js/chat-launcher.js). */
  transition: transform .45s var(--mr-ease);
}
.mr-bot.is-hover .mr-bot__badge {
  transform: scale(1.08);
}

.mr-bot__disc {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: 50%;
  background: radial-gradient(circle at 70% 28%, #E52B2F 0, #DC2626 40%, #C81E1E 72%, #991B1B 100%);
  box-shadow: inset 5.6px 1.7px 0 rgba(60, 0, 10, .16);
  transform-origin: 49.5% 53.5%; /* grows out of the check's corner */
}
.mr-bot__shine {
  position: absolute;
  top: -20%;
  bottom: -20%;
  left: 0;
  width: 38%;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, .42), rgba(255, 255, 255, 0));
  transform: translate3d(-140%, 0, 0) skewX(-18deg);
}
.mr-bot__ring {
  position: absolute;
  inset: 0;
  border: 2px solid var(--mr-red);
  border-radius: 50%;
  opacity: 0;
}
.mr-bot__tick {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.mr-bot__tick path {
  fill: none;
  stroke-width: 15;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.mr-bot__draw {
  stroke: var(--mr-red);
  stroke-dasharray: 80 160;
  stroke-dashoffset: 81;
  opacity: 0;
}
.mr-bot__white {
  transform-origin: 49.5px 53.5px;
}

/* 1. Undo: the disc folds back into the check's corner and the white check goes. */
.mr-bot.is-out .mr-bot__disc {
  transform: scale(0);
  transition: transform .3s cubic-bezier(.55, 0, .75, .1);
}
.mr-bot.is-out .mr-bot__white {
  opacity: 0;
  transform: scale(.5);
  transition: opacity .2s ease, transform .3s ease-in;
}

/* 2. Build, as in the page loader: the check draws in red, the disc grows behind it, the check turns white, a ripple
      runs out and a light sweeps across. Every step ends in the finished mark, so removing the class changes nothing. */
.mr-bot.is-building .mr-bot__draw {
  opacity: 1;
  animation: mrBotDraw .62s cubic-bezier(.65, 0, .35, 1) .1s both, mrBotFade .25s ease .9s forwards;
}
.mr-bot.is-building .mr-bot__disc {
  animation: mrBotGrow .75s var(--mr-pop) .62s both;
}
.mr-bot.is-building .mr-bot__white {
  animation: mrBotPop .5s cubic-bezier(.34, 1.56, .64, 1) .8s both;
}
.mr-bot.is-building .mr-bot__ring {
  animation: mrBotRing 1.1s cubic-bezier(.2, .6, .3, 1) 1.05s forwards; /* not "both": it would show the ring while it waits */
}
.mr-bot.is-building .mr-bot__ring--late {
  animation-delay: 1.4s;
}
.mr-bot.is-building .mr-bot__shine {
  animation: mrBotShine 1.1s cubic-bezier(.45, 0, .25, 1) 1.45s both;
}

/* A tap on the bubble that the bot did not answer points at the mark instead (js/chat-launcher.js). */
.mr-bot.is-nudge .mr-bot__badge {
  animation: mrBotNudge .7s var(--mr-ease);
}
.mr-bot.is-nudge .mr-bot__ring {
  animation: mrBotRing .9s cubic-bezier(.2, .6, .3, 1) forwards;
}

@keyframes mrBotDraw { to { stroke-dashoffset: 0; } }
@keyframes mrBotFade { to { opacity: 0; } }
@keyframes mrBotGrow { from { transform: scale(0); } to { transform: none; } }
@keyframes mrBotPop { from { opacity: 0; transform: scale(.6); } to { opacity: 1; transform: none; } }
@keyframes mrBotRing {
  0% { opacity: .55; transform: scale(1); }
  100% { opacity: 0; transform: scale(1.75); }
}
@keyframes mrBotShine {
  from { transform: translate3d(-140%, 0, 0) skewX(-18deg); }
  to { transform: translate3d(330%, 0, 0) skewX(-18deg); }
}
@keyframes mrBotNudge {
  0% { transform: none; }
  30% { transform: scale(1.18); }
  55% { transform: scale(.94); }
  80% { transform: scale(1.05); }
  100% { transform: none; }
}

/* ---- The greeting bubble, to the left of the mark (in both languages: the mark is on the right in both) ---------- */

.mr-bot__bubble {
  position: absolute;
  right: calc(var(--size) / 2 + 14px);
  bottom: calc(var(--size) / -2);
  box-sizing: border-box;
  width: max-content;
  max-width: min(280px, calc(100vw - 132px));
  border-radius: 18px 18px 6px 18px;
  background: #fff;
  color: var(--mr-navy);
  box-shadow: 0 20px 44px -18px rgba(17, 24, 39, .5), 0 2px 10px rgba(17, 24, 39, .1);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.5;
  text-align: start;
  pointer-events: auto;
  opacity: 0;
  visibility: hidden;
  transform: translate3d(12px, 6px, 0) scale(.86);
  transform-origin: 100% 100%;
  transition: opacity .25s ease, transform .35s var(--mr-ease), visibility 0s linear .35s;
}
.mr-bot.is-talking .mr-bot__bubble {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity .3s ease, transform .55s var(--mr-pop), visibility 0s;
}
/* The whole message is the button that opens the chat. */
.mr-bot__open {
  display: block;
  width: 100%;
  margin: 0;
  padding: 11px 16px 12px;
  border: 0;
  border-radius: inherit;
  background: none;
  color: inherit;
  font: inherit;
  text-align: inherit;
  cursor: pointer;
}
.mr-bot__open:focus-visible {
  outline: 2px solid var(--mr-red);
  outline-offset: 3px;
}
/* The tail, pointing at the mark. */
.mr-bot__bubble::after {
  content: "";
  position: absolute;
  right: -5px;
  bottom: calc(var(--size) / 2 - 6px);
  width: 12px;
  height: 12px;
  border-radius: 2px;
  background: #fff;
  transform: rotate(45deg);
}

.mr-bot__name {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
  color: var(--mr-red);
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: .02em;
  white-space: nowrap;
}
/* "Online" dot. */
.mr-bot__name::before {
  content: "";
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #1fae6b;
  box-shadow: 0 0 0 3px rgba(31, 174, 107, .18);
}

/* The full message lays out the bubble before it is typed, so the bubble never changes size while typing. */
.mr-bot__text {
  display: grid;
}
.mr-bot__sizer,
.mr-bot__typed,
.mr-bot__dots {
  grid-area: 1 / 1;
}
.mr-bot__sizer {
  visibility: hidden;
}
.mr-bot.is-typing .mr-bot__typed::after {
  content: "";
  display: inline-block;
  width: 2px;
  height: 1.05em;
  margin-inline-start: 2px;
  vertical-align: -.15em;
  border-radius: 1px;
  background: var(--mr-red);
  animation: mrBotCaret .8s steps(1) infinite;
}
.mr-bot__dots {
  display: none;
  align-self: start;
  gap: 5px;
  padding-top: 8px;
}
.mr-bot.is-dots .mr-bot__dots {
  display: inline-flex;
}
.mr-bot__dots i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--mr-navy);
  opacity: .3;
  animation: mrBotDot 1s ease-in-out infinite;
}
.mr-bot__dots i:nth-child(2) { animation-delay: .15s; }
.mr-bot__dots i:nth-child(3) { animation-delay: .3s; }

@keyframes mrBotCaret { 50% { opacity: 0; } }
@keyframes mrBotDot {
  0%, 60%, 100% { opacity: .3; transform: none; }
  30% { opacity: .9; transform: translate3d(0, -4px, 0); }
}

/* Hide the bubble for the rest of the visit. */
.mr-bot__hide {
  position: absolute;
  top: -10px;
  left: -10px;
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #fff;
  color: var(--mr-navy);
  box-shadow: 0 4px 12px -4px rgba(17, 24, 39, .45), 0 0 0 1px rgba(17, 24, 39, .06);
  cursor: pointer;
  transition: background-color .2s ease, color .2s ease, transform .3s var(--mr-ease);
}
.mr-bot__hide svg {
  width: 12px;
  height: 12px;
}
.mr-bot__hide:hover,
.mr-bot__hide:focus-visible {
  background: var(--mr-red);
  color: #fff;
  transform: rotate(90deg);
}
.mr-bot__hide:focus-visible {
  outline: 2px solid var(--mr-navy);
  outline-offset: 2px;
}

[dir="rtl"] .mr-bot__bubble {
  font-size: 14.5px;
  font-weight: 600;
}
[dir="rtl"] .mr-bot__name {
  letter-spacing: 0;
}

@media (max-width: 768px) {
  .mr-bot__bubble {
    max-width: calc(100vw - 118px);
    font-size: 13.5px;
  }
  .mr-bot__open {
    padding: 10px 14px 11px;
  }
}

/* ---- Hidden while the chat is open ------------------------------------------------------------------------------ */

#chatbotIframe.bot-open ~ .mr-bot .mr-bot__badge {
  opacity: 0;
  transition: opacity .25s ease;
}
#chatbotIframe.bot-open ~ .mr-bot .mr-bot__bubble {
  opacity: 0;
  visibility: hidden;
  transition: opacity .2s ease, visibility 0s linear .2s;
}

@media print {
  .mr-bot {
    display: none;
  }
}

/* Less motion: the finished mark, and messages that simply appear. */
@media (prefers-reduced-motion: reduce) {
  .mr-bot *,
  .mr-bot *::after {
    animation: none !important;
    transition-duration: .01s !important;
  }
  .mr-bot__draw,
  .mr-bot__ring {
    display: none;
  }
}
