/* ═══════════════════════════════════════════
   MDRACING — Chat Widget
   ═══════════════════════════════════════════ */

#md-chat-btn {
  position: fixed;
  bottom: 90px;
  right: 24px;
  z-index: 9998;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: #d10000;
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(209,0,0,.45);
  transition: transform .2s, box-shadow .2s;
  /* prevent double-tap zoom on mobile */
  touch-action: manipulation;
}
#md-chat-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 28px rgba(209,0,0,.6);
}
#md-chat-btn svg { width: 26px; height: 26px; }

/* Tooltip — desktop only */
@media (min-width: 481px) {
  #md-chat-btn::before {
    content: 'Consultar con Madi IA';
    position: absolute;
    right: 64px;
    white-space: nowrap;
    background: #111;
    color: #fff;
    font-family: 'Inter', sans-serif;
    font-size: 12px;
    padding: 5px 10px;
    border-radius: 6px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
  }
  #md-chat-btn:hover::before { opacity: 1; }
}

/* ── Backdrop (mobile) ── */
#md-chat-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 9997;
  opacity: 0;
  transition: opacity .25s;
}
#md-chat-backdrop.open {
  opacity: 1;
}

/* ── Ventana ── */
#md-chat-window {
  position: fixed;
  bottom: 160px;
  right: 24px;
  z-index: 9999;
  width: 360px;
  max-height: 540px;
  display: flex;
  flex-direction: column;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(0,0,0,.18);
  overflow: hidden;
  transform: scale(.92) translateY(16px);
  opacity: 0;
  pointer-events: none;
  transition: transform .25s cubic-bezier(.4,0,.2,1), opacity .25s;
}
#md-chat-window.open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

/* Header */
#md-chat-header {
  background: #0a0a0a;
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
#md-chat-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #d10000;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#md-chat-header-avatar svg { width: 20px; height: 20px; }
#md-chat-header-info { flex: 1; }
#md-chat-header-name {
  font-family: 'Barlow Condensed', sans-serif;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: .5px;
}
#md-chat-header-status {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  color: rgba(255,255,255,.55);
  display: flex;
  align-items: center;
  gap: 4px;
}
#md-chat-header-status::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
}
#md-chat-close {
  background: none;
  border: none;
  color: rgba(255,255,255,.5);
  cursor: pointer;
  padding: 8px;
  border-radius: 4px;
  transition: color .15s;
  touch-action: manipulation;
}
#md-chat-close:hover { color: #fff; }
#md-chat-close svg { width: 18px; height: 18px; display: block; }

/* Messages area */
#md-chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch; /* iOS momentum scroll */
  overscroll-behavior: contain;      /* prevent scroll leaking to page */
  touch-action: pan-y;               /* allow vertical touch scroll */
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #f6f6f8;
  scroll-behavior: smooth;
  min-height: 0; /* important for flex child scroll */
}
#md-chat-messages::-webkit-scrollbar { width: 4px; }
#md-chat-messages::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }

/* Bubbles */
.md-bubble {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: 14px;
  font-family: 'Inter', sans-serif;
  font-size: 13.5px;
  line-height: 1.5;
  word-break: break-word;
}
.md-bubble-bot {
  background: #fff;
  color: #111;
  border-bottom-left-radius: 4px;
  align-self: flex-start;
  box-shadow: 0 1px 4px rgba(0,0,0,.08);
}
.md-bubble-user {
  background: #d10000;
  color: #fff;
  border-bottom-right-radius: 4px;
  align-self: flex-end;
}

/* Typing indicator */
.md-typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 12px 14px;
}
.md-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #aaa;
  animation: md-bounce .9s infinite;
}
.md-typing span:nth-child(2) { animation-delay: .15s; }
.md-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes md-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* Input area */
#md-chat-input-area {
  padding: 12px 14px;
  background: #fff;
  border-top: 1px solid #e8e8ea;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-shrink: 0;
}
#md-chat-input {
  flex: 1;
  border: 1.5px solid #e0e0e4;
  border-radius: 10px;
  padding: 9px 12px;
  font-family: 'Inter', sans-serif;
  font-size: 13.5px;
  resize: none;
  outline: none;
  max-height: 100px;
  line-height: 1.45;
  color: #111;
  background: #fafafa;
  transition: border-color .2s;
}
#md-chat-input:focus { border-color: #d10000; background: #fff; }
#md-chat-input::placeholder { color: #aaa; }
#md-chat-send {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: #d10000;
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .2s, transform .15s;
  touch-action: manipulation;
}
#md-chat-send:hover { background: #e50000; }
#md-chat-send:active { transform: scale(.93); }
#md-chat-send:disabled { background: #ccc; cursor: default; }
#md-chat-send svg { width: 17px; height: 17px; }

/* Footer note */
#md-chat-footer {
  padding: 6px 14px 10px;
  text-align: center;
  font-family: 'Inter', sans-serif;
  font-size: 10.5px;
  color: #aaa;
  background: #fff;
}

/* ── Mobile ── */
@media (max-width: 600px) {
  #md-chat-btn { bottom: 80px; right: 16px; }

  #md-chat-backdrop { display: block; pointer-events: none; }
  #md-chat-backdrop.open { pointer-events: auto; }

  #md-chat-window {
    /* Sheet deslizable desde abajo */
    position: fixed;
    bottom: 0;
    right: 0;
    left: 0;
    top: auto;
    width: 100%;
    max-height: 82dvh;    /* dvh ajusta con teclado virtual */
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 32px rgba(0,0,0,.18);
    /* Animación desde abajo en mobile */
    transform: translateY(24px) scale(1);
    transition: transform .3s cubic-bezier(.4,0,.2,1), opacity .3s;
  }
  #md-chat-window.open {
    transform: translateY(0) scale(1);
    opacity: 1;
  }

  /* Pastilla "drag handle" visual */
  #md-chat-header::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(255,255,255,.25);
  }
  #md-chat-header { position: relative; padding-top: 20px; }

  /* Evita zoom de iOS al enfocar el textarea */
  #md-chat-input {
    font-size: 16px;
  }

  .md-bubble { font-size: 14px; }
  #md-chat-header-name { font-size: 15px; }
}
