/* The study buddy: one draggable floating robot that is the whole AI Teacher interface.
 *
 * upgrade.css still owns the button's stacking order and default corner (.ai-launch); this file
 * turns it into a round badge holding the character, gives it the drag/drop behaviour that
 * ai-avatar.js drives, and describes every pose the character can hold.
 *
 * Everything is SVG + CSS transforms, so there is no image or animation file to download and no
 * canvas or JavaScript animation loop. ai-avatar.js only ever adds or removes classes here:
 *
 *   base poses (one at a time)
 *   .is-idle         the resting buddy
 *   .is-connecting   the teacher is being dialled: a soft breathing pulse
 *   .is-listening    the child is talking: still, leaning in, ears lit
 *   .is-thinking     the teacher is working it out: a tilt and little dots
 *   .is-talking      the teacher is speaking: the mouth and a small nod (also read-aloud)
 *   .is-disconnected the call has ended: sleepy eyes, then back to idle
 *   .is-error        the call could not start: a small shrug, never a red cross
 *
 *   moods and gestures (short, on top of a base pose)
 *   .is-happy .is-encouraging .is-clapping .is-nodding .is-thumbsup .is-hopping
 *   .is-tilting .is-hand-up .is-hinting .is-nudging
 *
 *   small movements
 *   .is-waving .is-blinking .is-pop
 *
 *   interaction
 *   .is-pressed .is-dragging .is-snapping .is-paused
 *
 * The legacy assistant panel (#aiTutor) is kept in the page -- the transcript inside it is the
 * teacher's memory -- but it is never shown again: the buddy is the interface.
 */

.ai-launch.is-buddy {
  position: fixed;
  left: auto;
  top: auto;
  right: 1rem;
  bottom: 1rem;
  width: 62px;
  height: 62px;
  padding: 0;
  border: 0;
  background: none;
  border-radius: 50%;
  display: grid;
  place-items: center;
  overflow: visible;
  transition: transform .18s ease, filter .18s ease;
  filter: drop-shadow(0 8px 18px rgba(23, 59, 112, .28));
  /* The whole drag experience rests on these three lines: no page scroll from a press on the
     buddy, no text selection, no long-press callout or grey tap flash on iPhone. */
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  cursor: grab;
  /* Positions are written by ai-avatar.js as left/top pixels. */
}

/* upgrade.js hides the button on pages with no lesson. The author `display: grid` above would
   otherwise beat the browser's own [hidden] rule and leave the buddy floating over Home. */
.ai-launch.is-buddy[hidden] { display: none !important; }

.ai-launch.is-buddy::before {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 35%, #ffffff, #e7f1ff 62%, #d3e6ff);
  box-shadow: inset 0 0 0 1px rgba(23, 59, 112, .12);
  z-index: 0;
}

.ai-launch.is-buddy:hover,
.ai-launch.is-buddy:focus-visible {
  transform: translateY(-2px) scale(1.06);
  filter: drop-shadow(0 12px 24px rgba(23, 59, 112, .34));
}

.ai-launch.is-buddy:focus-visible { outline: 3px solid #2f6fd0; outline-offset: 3px; }

/* ---------------------------------------------------------------- dragging
   A press is a press, not a tap: it only gets smaller. Once the pointer has travelled far enough
   ai-avatar.js adds .is-dragging and the buddy lifts under the finger. */
.ai-launch.is-buddy.is-pressed { transform: scale(.94); }

.ai-launch.is-buddy.is-dragging {
  cursor: grabbing;
  transform: scale(1.1) rotate(-2deg);
  filter: drop-shadow(0 16px 30px rgba(23, 59, 112, .4));
  transition: transform .12s ease;
}

/* The glide to the nearest side, and it must not fight the drag itself. */
.ai-launch.is-buddy.is-snapping { transition: left .26s cubic-bezier(.22,.7,.3,1), top .2s ease-out, transform .18s ease, filter .18s ease; }

/* An animation on a backgrounded tab is pure battery cost. */
.ai-launch.is-buddy.is-paused *,
.ai-launch.is-buddy.is-paused { animation-play-state: paused !important; }

.buddy {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  overflow: visible;
  transform-origin: 50% 92%;
}

/* ---------------------------------------------------------------- the parts */
.buddy-skull { fill: url(#buddySkin); }
.buddy-ear { fill: #6d55e6; }
.buddy-ear-band { fill: #ffd75e; }
.buddy-ear-dot { fill: #8ef0ff; }
.buddy-antenna { stroke: #5b45cf; stroke-width: 2.2; stroke-linecap: round; fill: none; }
.buddy-antenna-bulb { fill: #8ef0ff; }
.buddy-antenna-glow { fill: url(#buddyGlow); opacity: .7; }
.buddy-bolt { fill: #5b45cf; }
.buddy-bolt-line { stroke: #cdbcff; stroke-width: 1.1; stroke-linecap: round; fill: none; }

.buddy-sclera { fill: #f8fbff; }
.buddy-iris { fill: #35dcff; }
.buddy-pupil { fill: #141d33; }
.buddy-glint { fill: #ffffff; }
.buddy-eye-glow { fill: url(#buddyGlow); opacity: .5; }
.buddy-lid { fill: url(#buddySkin); transform-box: fill-box; transform-origin: 50% 0%; transform: scaleY(0); transition: transform .1s ease; }

.buddy-mouth { stroke: #141d33; stroke-width: 2.6; stroke-linecap: round; fill: none; }
.buddy-mouth-open { opacity: 0; }
.buddy-mouth-open ellipse:first-child { fill: #141d33; }
.buddy-tongue { fill: #ff7a90; }

.buddy-torso { fill: url(#buddySuit); }
.buddy-collar { stroke: #ffd75e; stroke-width: 1.6; stroke-linecap: round; fill: none; opacity: .8; }
.buddy-limb { stroke: #8f7bf7; stroke-width: 5; stroke-linecap: round; fill: none; }
.buddy-joint { fill: #ffd75e; }
.buddy-hand { fill: #6d55e6; }
.buddy-foot { fill: #cfd9ee; }
.buddy-shadow { fill: #173b70; opacity: .13; }

.buddy-core-glow { fill: url(#buddyGlow); opacity: .75; }
.buddy-core-ring { fill: none; stroke: #ffd75e; stroke-width: 1.8; }
.buddy-core-dot { fill: #35dcff; }

.buddy-think-dots, .buddy-bulb, .buddy-sparks { opacity: 0; }
.buddy-think-dots circle { fill: #b9c8ff; }
.buddy-bulb-glow { fill: #ffd75e; }
.buddy-bulb-rays { stroke: #ffc93c; stroke-width: 1.6; stroke-linecap: round; fill: none; }
.buddy-sparks path { fill: #ffd75e; }
.buddy-eyes-happy { opacity: 0; }
.buddy-eyes-happy path { stroke: #141d33; stroke-width: 2.8; stroke-linecap: round; fill: none; }

/* ---------------------------------------------------------------- idle
   A shallow breath, a slow sway, a pulsing antenna and core: all transforms and opacity on a 62px
   element, so the whole thing is composited and costs almost nothing. */
.buddy-body { animation: buddy-breathe 3.6s ease-in-out infinite; transform-box: view-box; transform-origin: 50% 92%; }
.buddy-head { animation: buddy-sway 5.2s ease-in-out infinite; transform-box: view-box; transform-origin: 50% 60%; }
.buddy-antenna-bulb, .buddy-antenna-glow { animation: buddy-pulse 2.6s ease-in-out infinite; transform-box: view-box; transform-origin: 50px 3.6px; }
.buddy-core-dot, .buddy-core-glow { animation: buddy-pulse 2s ease-in-out infinite; transform-box: view-box; transform-origin: 50px 73px; }

@keyframes buddy-breathe {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-1.6px); }
}
@keyframes buddy-sway {
  0%, 100% { transform: rotate(-1.3deg); }
  50% { transform: rotate(1.3deg); }
}
@keyframes buddy-pulse {
  0%, 100% { opacity: .45; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.14); }
}

.is-blinking .buddy-lid { transform: scaleY(1); }
.is-blinking .buddy-head { animation-play-state: paused; }

/* A tap is answered instantly, before the connection has time to do anything. */
.is-pop { animation: buddy-pop .32s cubic-bezier(.3,1.5,.5,1) 1; }
@keyframes buddy-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.16); filter: drop-shadow(0 10px 22px rgba(53, 220, 255, .55)); }
  100% { transform: scale(1); }
}

/* ---------------------------------------------------------------- waving */
.buddy-arm-right { transform-box: view-box; transform-origin: 63.5px 66px; }
.buddy-arm-left { transform-box: view-box; transform-origin: 36.5px 66px; }
.is-waving .buddy-arm-right { animation: buddy-wave .44s ease-in-out 2; }
@keyframes buddy-wave {
  0%, 100% { transform: rotate(0deg); }
  22% { transform: rotate(-46deg); }
  46% { transform: rotate(-16deg); }
  68% { transform: rotate(-40deg); }
}

/* ---------------------------------------------------------------- connecting
   Never a spinner and never a wait with nothing happening: a soft, quickened breath and a
   searching little sway, so the child can see the buddy is busy. */
.is-connecting .buddy-body { animation: buddy-breathe 1.5s ease-in-out infinite; }
.is-connecting .buddy-head { animation: buddy-sway 2.4s ease-in-out infinite; }
.is-connecting .buddy-antenna-bulb, .is-connecting .buddy-antenna-glow { animation: buddy-pulse .9s ease-in-out infinite; }
.is-connecting .buddy-eyes { animation: buddy-look-around 2.6s ease-in-out infinite; transform-box: view-box; transform-origin: 50px 34px; }
@keyframes buddy-look-around {
  0%, 100% { transform: translate(0, 0); }
  30% { transform: translate(-2.2px, .6px); }
  65% { transform: translate(2.2px, -.8px); }
}

/* ---------------------------------------------------------------- listening
   The child is talking, so the buddy stops fidgeting and leans in: still body, straight gaze,
   ear cans lit. Nothing here should compete with the child's own voice. */
.is-listening .buddy-body { animation: buddy-lean 3.2s ease-in-out infinite; }
.is-listening .buddy-head { animation: none; }
.is-listening .buddy-antenna-bulb, .is-listening .buddy-antenna-glow { animation: buddy-pulse 1.8s ease-in-out infinite; }
.is-listening .buddy-ear-dot { animation: buddy-ear-listen 1.4s ease-in-out infinite; }
/* The mouth stays the closed smile: listening is not talking, but it is not a blank face either. */
.is-listening .buddy-mouth-open { opacity: 0; }
@keyframes buddy-lean {
  0%, 100% { transform: translateY(0) rotate(-.6deg); }
  50% { transform: translateY(-1px) rotate(-.6deg); }
}
@keyframes buddy-ear-listen {
  0%, 100% { opacity: .5; }
  50% { opacity: 1; }
}

/* ---------------------------------------------------------------- talking */
.is-talking .buddy-mouth { opacity: 0; }
.is-talking .buddy-mouth-open { opacity: 1; animation: buddy-talk .26s ease-in-out infinite; transform-box: view-box; transform-origin: 50px 48.5px; }
.is-talking .buddy-head { animation: buddy-talk-nod 1.1s ease-in-out infinite; }
.is-talking .buddy-antenna-glow { animation: buddy-pulse .7s ease-in-out infinite; }
@keyframes buddy-talk {
  0%, 100% { transform: scaleY(.72) scaleX(.94); }
  50% { transform: scaleY(1.5) scaleX(1); }
}
@keyframes buddy-talk-nod {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(1.6deg); }
  75% { transform: rotate(-1.6deg); }
}

/* ---------------------------------------------------------------- thinking */
.is-thinking .buddy-head { animation: buddy-think-tilt 2.4s ease-in-out infinite; }
.is-thinking .buddy-eyes { transform-box: view-box; transform-origin: 50px 34px; animation: buddy-look-up 2.4s ease-in-out infinite; }
.is-thinking .buddy-think-dots { opacity: 1; }
.is-thinking .buddy-think-dots circle { animation: buddy-think-dot 1.2s ease-in-out infinite; }
.is-thinking .buddy-think-dots circle:nth-child(2) { animation-delay: .18s; }
.is-thinking .buddy-think-dots circle:nth-child(3) { animation-delay: .36s; }
@keyframes buddy-think-tilt {
  0%, 100% { transform: rotate(-2.4deg); }
  25% { transform: rotate(2deg); }
  50% { transform: rotate(1deg); }
  75% { transform: rotate(-2.6deg); }
}
@keyframes buddy-look-up {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(2.6px, -2.2px); }
}
@keyframes buddy-think-dot {
  0%, 100% { opacity: .25; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2.4px); }
}

/* ---------------------------------------------------------------- a hint */
.is-hinting .buddy-bulb { opacity: 1; animation: buddy-bulb-in .4s ease-out; transform-box: view-box; transform-origin: 78px 7px; }
.is-hinting .buddy-bulb-rays { animation: buddy-pulse 1.1s ease-in-out infinite; }
.is-hinting .buddy-arm-left { animation: buddy-raise .5s ease-out both; }
@keyframes buddy-bulb-in {
  from { opacity: 0; transform: scale(.5); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes buddy-raise {
  from { transform: rotate(0deg); }
  to { transform: rotate(24deg); }
}

/* ---------------------------------------------------------------- happy
   The face of "you got it": happy eyes, a grin and some sparks. The body language itself comes
   from the action below it (clap, nod, thumbs up or a hop), which is chosen for variety. */
.is-happy .buddy-eyes { opacity: 0; }
.is-happy .buddy-eyes-happy { opacity: 1; }
.is-happy .buddy-mouth { animation: buddy-grin .62s ease-out 2; transform-box: view-box; transform-origin: 50px 48px; }
.is-happy .buddy-sparks { opacity: 1; }
.is-happy .buddy-sparks path { animation: buddy-spark 1.1s ease-out infinite; transform-box: fill-box; transform-origin: center; }
.is-happy .buddy-sparks path:nth-child(2) { animation-delay: .16s; }
.is-happy .buddy-sparks path:nth-child(3) { animation-delay: .3s; }
@keyframes buddy-grin {
  0%, 100% { transform: scale(1); }
  40% { transform: scale(1.22); }
}
@keyframes buddy-spark {
  0% { opacity: 0; transform: scale(.4) rotate(-12deg); }
  40% { opacity: 1; transform: scale(1) rotate(4deg); }
  100% { opacity: 0; transform: scale(.7) rotate(14deg); }
}
.is-hopping .buddy-body { animation: buddy-hop .42s ease-out 3; }
@keyframes buddy-hop {
  0%, 100% { transform: translateY(0); }
  30% { transform: translateY(-7px); }
  60% { transform: translateY(1px); }
}
.is-clapping .buddy-arm-left { animation: buddy-clap-left .3s ease-in-out 5; }
.is-clapping .buddy-arm-right { animation: buddy-clap-right .3s ease-in-out 5; }
@keyframes buddy-clap-left {
  0%, 100% { transform: rotate(-72deg); }
  50% { transform: rotate(-96deg); }
}
@keyframes buddy-clap-right {
  0%, 100% { transform: rotate(72deg); }
  50% { transform: rotate(96deg); }
}
.is-nodding .buddy-head { animation: buddy-nod .5s ease-in-out 3; }
@keyframes buddy-nod {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  35% { transform: translateY(2.6px) rotate(2deg); }
  70% { transform: translateY(-1px) rotate(-1deg); }
}
.is-thumbsup .buddy-arm-right { animation: buddy-thumbs-up-hold .8s ease-out both; }
@keyframes buddy-thumbs-up-hold {
  0% { transform: rotate(0deg); }
  45% { transform: rotate(-116deg); }
  70% { transform: rotate(-104deg); }
  100% { transform: rotate(-112deg); }
}

/* ---------------------------------------------------------------- encouraging
   A wrong answer is never punished here. A warm face, a small open hand, or a gentle head tilt:
   the buddy stays on the child's side. */
.is-encouraging .buddy-mouth { animation: buddy-grin 1s ease-out 1; transform-box: view-box; transform-origin: 50px 48px; }
.is-encouraging .buddy-core-glow { animation: buddy-pulse 1.4s ease-in-out infinite; }
.is-tilting .buddy-head { animation: buddy-soft-tilt 1.6s ease-in-out 1 both; }
.is-tilting .buddy-lid { transform: scaleY(.22); }
@keyframes buddy-soft-tilt {
  0% { transform: rotate(0deg); }
  35% { transform: rotate(-7deg); }
  70% { transform: rotate(-5deg); }
  100% { transform: rotate(0deg); }
}
.is-hand-up .buddy-arm-left { animation: buddy-raise .7s ease-out both; }

/* ---------------------------------------------------------------- nudging, disconnected, error
   The 12-second reminder is a small wave and a blink; nothing is popped up in front of the child. */
.is-nudging .buddy-arm-right { animation: buddy-wave .44s ease-in-out 1; }

.is-disconnected .buddy-lid { transform: scaleY(.55); }
.is-disconnected .buddy-body { animation: buddy-breathe 5.4s ease-in-out infinite; }
.is-disconnected .buddy-head { animation: buddy-sway 7s ease-in-out infinite; }

.is-error .buddy-arm-left { animation: buddy-shrug-left .6s ease-out both; }
.is-error .buddy-arm-right { animation: buddy-shrug-right .6s ease-out both; }
.is-error .buddy-head { animation: buddy-soft-tilt 1.4s ease-in-out 1 both; }
@keyframes buddy-shrug-left {
  from { transform: rotate(0deg); }
  to { transform: rotate(26deg); }
}
@keyframes buddy-shrug-right {
  from { transform: rotate(0deg); }
  to { transform: rotate(-26deg); }
}

/* ---------------------------------------------------------------- the one line of words
   The buddy's only text: a small, quiet bubble beside it that fades on its own. */
.ai-buddy-bubble {
  position: fixed;
  z-index: 46;
  max-width: min(62vw, 210px);
  padding: .5rem .7rem;
  border-radius: 14px;
  background: #ffffff;
  color: #17233f;
  border: 1px solid #d8dcef;
  box-shadow: 0 10px 24px rgba(21, 35, 77, .18);
  font-size: .85rem;
  font-weight: 700;
  line-height: 1.3;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity .18s ease, transform .18s ease;
  pointer-events: none;
}
.ai-buddy-bubble.show { opacity: 1; transform: translateY(0); }
.ai-buddy-bubble::after {
  content: "";
  position: absolute;
  top: 50%;
  margin-top: -6px;
  border: 6px solid transparent;
}
.ai-buddy-bubble.from-right::after { right: -12px; border-left-color: #ffffff; }
.ai-buddy-bubble:not(.from-right)::after { left: -12px; border-right-color: #ffffff; }

/* The little tab on the buddy's corner: stop the teacher at any moment, without a menu, without a
   question to answer first. Shown only while a session is connecting or running (ai-avatar.js). */
.ai-buddy-close {
  position: fixed;
  z-index: 47;                                    /* over the buddy (45) and its bubble (46) */
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  border: 1px solid #d8dcef;
  background: #ffffff;
  color: #17233f;
  font: inherit;
  font-size: .78rem;
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 6px 16px rgba(21, 35, 77, .2);
  cursor: pointer;
  opacity: 0;
  transform: scale(.8);
  transition: opacity .16s ease, transform .16s ease, background .16s ease, border-color .16s ease;
  pointer-events: none;
}
.ai-buddy-close.show { opacity: 1; transform: scale(1); pointer-events: auto; }
.ai-buddy-close:hover { background: #eef2ff; border-color: #a9b7e6; }
.ai-buddy-close:focus-visible { outline: 3px solid rgba(91, 75, 219, .35); outline-offset: 2px; }

/* "It is connected": the small green tag over the buddy's head, so the child knows the moment the
   teacher is listening instead of guessing from the silence. */
.ai-buddy-connected {
  position: fixed;
  z-index: 47;
  padding: .3rem .55rem;
  border-radius: 999px;
  background: #dff8ef;
  border: 1px solid #a9e2cd;
  color: #10634c;
  font-size: .78rem;
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 8px 20px rgba(16, 99, 76, .18);
  opacity: 0;
  transform: translateY(6px) scale(.9);
  transition: opacity .18s ease, transform .18s ease;
  pointer-events: none;
}
.ai-buddy-connected.show { opacity: 1; transform: translateY(0) scale(1); }

/* The invisible probe ai-avatar.js reads to get env(safe-area-inset-*) in JavaScript. */
.ai-safe-probe {
  position: fixed;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  visibility: hidden;
  pointer-events: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* The legacy assistant panel is dead: never shown, but the transcript inside it is the teacher's
   memory, so it stays in the page. */
.ai-panel { display: none !important; }

/* ---------------------------------------------------------------- quiet mode
   With reduced motion the buddy keeps every change of expression (eyes, mouth, pose) and loses
   only the movement. */
@media (prefers-reduced-motion: reduce) {
  .ai-launch.is-buddy *,
  .ai-launch.is-buddy,
  .ai-buddy-close,
  .ai-buddy-connected { animation: none !important; transition: none !important; }
  .is-talking .buddy-mouth-open { opacity: 1; }
  .is-talking .buddy-mouth { opacity: 0; }
  .is-listening .buddy-mouth-open { opacity: 0; }
  .is-happy .buddy-eyes { opacity: 0; }
  .is-happy .buddy-eyes-happy { opacity: 1; }
  .is-thinking .buddy-think-dots,
  .is-hinting .buddy-bulb,
  .is-happy .buddy-sparks { opacity: 1; }
  .is-clapping .buddy-arm-left { transform: rotate(-72deg); }
  .is-clapping .buddy-arm-right { transform: rotate(72deg); }
  .is-thumbsup .buddy-arm-right { transform: rotate(-112deg); }
  .is-hand-up .buddy-arm-left,
  .is-error .buddy-arm-left { transform: rotate(24deg); }
  .is-error .buddy-arm-right { transform: rotate(-24deg); }
  .is-tilting .buddy-head { transform: rotate(-6deg); }
  .is-tilting .buddy-lid,
  .is-disconnected .buddy-lid { transform: scaleY(.5); }
}

@media (max-width: 700px) {
  .ai-launch.is-buddy { width: 56px; height: 56px; }
}
