/* ===== LEO - ANIMATED MASCOT ASSISTANT ===== */

/* Container that holds Leo + speech bubble - positioned fixed, draggable */
.leo-container {
  position: fixed;
  bottom: 20px;
  right: 40px;
  z-index: 900;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  transition: bottom 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.leo-container > * {
  pointer-events: auto;
}

/* The character sprite */
.leo-character {
  width: 160px;
  height: 200px;
  cursor: pointer;
  position: relative;
  /* Subtle breathing animation */
  animation: leo-breathe 3s ease-in-out infinite;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  filter: drop-shadow(0 4px 12px rgba(0,0,0,0.3));
}

.leo-character:hover {
  transform: scale(1.06);
}

.leo-character img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: bottom center;
}

/* Breathing / idle bob */
@keyframes leo-breathe {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* Walking animation */
.leo-container.walking .leo-character {
  animation: leo-walk 0.5s ease-in-out infinite;
}

@keyframes leo-walk {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-6px) rotate(-2deg); }
  75% { transform: translateY(-6px) rotate(2deg); }
}

/* Flexing animation */
.leo-container.flexing .leo-character {
  animation: leo-flex-bounce 0.6s cubic-bezier(0.16, 1, 0.3, 1) 3;
}

@keyframes leo-flex-bounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

/* Stretching animation */
.leo-container.stretching .leo-character {
  animation: leo-stretch-sway 1.2s ease-in-out 2;
}

@keyframes leo-stretch-sway {
  0%, 100% { transform: rotate(0deg); }
  33% { transform: rotate(-3deg); }
  66% { transform: rotate(3deg); }
}


/* Mirror for walking left */
.leo-container.facing-left .leo-character img {
  transform: scaleX(-1);
}

/* Name tag under character */
.leo-nametag {
  font-family: var(--font-display);
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  background: var(--color-primary);
  padding: 2px 10px;
  border-radius: var(--radius-full);
  margin-top: -8px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

/* ===== SPEECH BUBBLE ===== */
.leo-speech {
  position: relative;
  background: var(--color-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  max-width: 320px;
  min-width: 200px;
  margin-bottom: var(--space-3);
  box-shadow: 0 8px 32px rgba(0,0,0,0.15);
  opacity: 0;
  transform: translateY(8px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.leo-speech.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Speech bubble tail */
.leo-speech::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 8px solid var(--color-surface);
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.1));
}

.leo-speech::before {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-top: 9px solid var(--color-divider);
}

.leo-speech__text {
  font-family: var(--font-body);
  font-size: var(--text-sm);
  color: var(--color-text);
  line-height: 1.55;
  max-height: 200px;
  overflow-y: auto;
}

.leo-speech__text a {
  color: var(--color-accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.leo-speech__text strong {
  font-weight: 600;
}

/* Quick action chips in speech bubble */
.leo-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: var(--space-3);
}

.leo-chip {
  font-family: var(--font-body);
  font-size: 11px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: var(--radius-full);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  cursor: pointer;
  transition: all var(--transition-interactive);
  white-space: nowrap;
}

.leo-chip:hover {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}

/* Close button on speech bubble */
.leo-speech__close {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  border-radius: var(--radius-full);
  background: var(--color-surface-offset);
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
  font-size: 12px;
  line-height: 1;
  transition: all var(--transition-interactive);
}

.leo-speech__close:hover {
  background: var(--color-text);
  color: var(--color-surface);
}

/* ===== CHAT MODE (expanded) ===== */
.leo-chat-panel {
  position: absolute;
  bottom: calc(100% + var(--space-2));
  right: 0;
  width: 380px;
  max-width: calc(100vw - 40px);
  background: var(--color-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-xl);
  box-shadow: 0 12px 48px rgba(0,0,0,0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(12px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1),
              transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  height: 440px;
}

.leo-chat-panel.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* Chat header */
.leo-chat-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-primary);
  color: #fff;
  flex-shrink: 0;
}

.leo-chat-header__name {
  font-family: var(--font-display);
  font-size: var(--text-sm);
  font-weight: 700;
  flex: 1;
}

.leo-chat-header__close {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-full);
  background: rgba(255,255,255,0.15);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: none;
}

.leo-chat-header__close:hover {
  background: rgba(255,255,255,0.25);
}

/* Chat messages */
.leo-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.leo-chat-messages::-webkit-scrollbar { width: 4px; }
.leo-chat-messages::-webkit-scrollbar-thumb { background: var(--color-divider); border-radius: 4px; }

.leo-msg {
  display: flex;
  gap: var(--space-2);
  max-width: 88%;
  animation: leo-msg-in 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes leo-msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.leo-msg--bot { align-self: flex-start; }
.leo-msg--user { align-self: flex-end; flex-direction: row-reverse; }

.leo-msg__bubble {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-lg);
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
}

.leo-msg--bot .leo-msg__bubble {
  background: var(--color-surface-offset);
  color: var(--color-text);
  border-bottom-left-radius: var(--radius-sm);
}

.leo-msg--user .leo-msg__bubble {
  background: var(--color-primary);
  color: #fff;
  border-bottom-right-radius: var(--radius-sm);
}

.leo-msg--bot .leo-msg__bubble a {
  color: var(--color-accent);
  text-decoration: underline;
}

/* Typing dots */
.leo-typing {
  display: flex;
  gap: 4px;
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface-offset);
  border-radius: var(--radius-lg);
  border-bottom-left-radius: var(--radius-sm);
  align-self: flex-start;
}

.leo-typing__dot {
  width: 5px;
  height: 5px;
  border-radius: var(--radius-full);
  background: var(--color-text-faint);
  animation: leo-typing-bounce 1.4s ease-in-out infinite;
}

.leo-typing__dot:nth-child(2) { animation-delay: 0.2s; }
.leo-typing__dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes leo-typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-3px); opacity: 1; }
}

/* Chat input */
.leo-chat-input-area {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-3);
  border-top: 1px solid var(--color-divider);
  flex-shrink: 0;
}

.leo-chat-input {
  flex: 1;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-full);
  padding: var(--space-2) var(--space-3);
  font-size: 13px;
  background: var(--color-bg);
  color: var(--color-text);
  outline: none;
  min-height: 36px;
}

.leo-chat-input::placeholder { color: var(--color-text-faint); }
.leo-chat-input:focus { border-color: var(--color-primary); }

.leo-chat-send {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
}

.leo-chat-send:hover { background: var(--color-accent-hover); }
.leo-chat-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ===== MOBILE ===== */
@media (max-width: 480px) {
  .leo-container {
    right: 10px;
    bottom: 10px;
  }

  .leo-character {
    width: 110px;
    height: 140px;
  }

  .leo-speech {
    max-width: 240px;
    min-width: 160px;
  }

  /* Hide the floating Leo while chat is open so he doesn't cover the panel */
  .leo-container.chat-open .leo-character,
  .leo-container.chat-open .leo-speech {
    display: none;
  }

  .leo-chat-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100vw;
    max-width: 100vw;
    height: 55dvh;
    max-height: 55dvh;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .leo-chat-header {
    padding: 10px 14px;
    flex-shrink: 0;
  }

  .leo-chat-header__name {
    font-size: 15px;
  }

  .leo-chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    padding: 10px 12px;
    font-size: 14px;
    -webkit-overflow-scrolling: touch;
  }

  .leo-chat-input-area {
    padding: 8px 10px;
    gap: 6px;
    flex-shrink: 0;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  .leo-chat-input {
    font-size: 16px; /* prevents iOS auto-zoom on focus */
    padding: 8px 12px;
    min-height: 38px;
  }

  .leo-chat-send {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
  }
}

/* ===== LEO MINIMIZE / DRAG / PUCK ===== */

/* Make the character grabbable */
.leo-character {
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
}

.leo-container.draggable .leo-character {
  cursor: grab;
}

.leo-container.dragging .leo-character {
  cursor: grabbing;
  opacity: 0.92;
}

/* When user is actively dragging, lock things down */
.leo-container.dragging {
  transition: none !important;
}

/* Minimize button - small dash in top-right of Leo */
.leo-minimize {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: rgba(20, 20, 20, 0.85);
  color: #fff;
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 5;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.15s ease, background 0.15s ease;
  padding: 0;
}

.leo-minimize:hover {
  background: var(--color-accent, #c41e3a);
  transform: scale(1.1);
}

.leo-character {
  position: relative;
}

/* Show the minimize button on hover/focus of Leo, and always on touch devices */
.leo-character:hover .leo-minimize,
.leo-minimize:focus,
.leo-container.show-controls .leo-minimize {
  display: flex;
  opacity: 1;
}

@media (hover: none) {
  /* Touch devices: always show minimize button so users can find it */
  .leo-minimize {
    display: flex;
    opacity: 0.85;
  }
}

/* Hide the entire Leo character + speech + nametag when minimized */
.leo-container.minimized .leo-character,
.leo-container.minimized .leo-speech,
.leo-container.minimized .leo-nametag,
.leo-container.minimized .leo-chat-panel {
  display: none !important;
}

/* The minimized puck - small round button */
.leo-puck {
  display: none;
  position: absolute;
  bottom: 0;
  right: 0;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 2px solid var(--color-accent, #c41e3a);
  background: #fff;
  padding: 0;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  overflow: hidden;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  animation: leoPuckPulse 2.5s ease-in-out infinite;
}

.leo-puck:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 22px rgba(196, 30, 58, 0.4);
}

.leo-puck img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
}

.leo-container.minimized .leo-puck {
  display: block;
}

@keyframes leoPuckPulse {
  0%, 100% { box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25), 0 0 0 0 rgba(196, 30, 58, 0.5); }
  50%      { box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25), 0 0 0 10px rgba(196, 30, 58, 0); }
}
