/* ─────────────────────────────────────────────────────────────
   Chat Widget — Tienda Mágica
   Usa tokens del design system (:root en design-system.css)
   ───────────────────────────────────────────────────────────── */

/* Botón flotante */
#tm-chat-toggle {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--color-primary, #7C3AED);
  color: var(--color-light, #fff);
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(124,58,237,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s, transform .2s;
}
#tm-chat-toggle:hover {
  background: var(--color-primary-dark, #5B21B6);
  transform: scale(1.07);
}
#tm-chat-toggle:focus-visible {
  outline: 3px solid var(--color-cta, #F97316);
  outline-offset: 3px;
}

/* Badge de mensajes no leídos */
#tm-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: var(--color-cta, #F97316);
  color: #fff;
  font-family: var(--font-body, sans-serif);
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  padding: 0 4px;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
#tm-badge.visible { display: flex; }

/* Panel */
#tm-chat-panel {
  position: fixed;
  bottom: 92px;
  right: 24px;
  z-index: 9998;
  width: 360px;
  max-width: calc(100vw - 48px);
  height: 520px;
  max-height: calc(100vh - 120px);
  background: var(--color-bg, #fff);
  border: 1px solid var(--color-border, #E8E0FA);
  border-radius: var(--radius-card, 16px);
  box-shadow: 0 8px 32px rgba(30,27,75,.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
  transition: opacity .2s, transform .2s;
}
#tm-chat-panel[hidden] {
  display: none;
}
#tm-chat-panel.tm-entering {
  animation: tm-panel-in .2s ease forwards;
}
@keyframes tm-panel-in {
  from { opacity: 0; transform: scale(.95) translateY(8px); }
  to   { opacity: 1; transform: scale(1)  translateY(0); }
}

/* Header */
#tm-chat-header {
  display: flex;
  align-items: center;
  gap: var(--space-xs, 8px);
  padding: 14px var(--space-sm, 16px);
  background: var(--color-primary, #7C3AED);
  color: var(--color-light, #fff);
  flex-shrink: 0;
}
#tm-chat-avatar {
  width: 36px;
  height: 36px;
  background: rgba(255,255,255,.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}
#tm-chat-title {
  font-family: var(--font-heading, sans-serif);
  font-weight: 700;
  font-size: 15px;
}
#tm-chat-status {
  font-family: var(--font-body, sans-serif);
  font-size: 12px;
  opacity: .8;
}
#tm-chat-status.connected  { color: #86efac; opacity: 1; }
#tm-chat-status.error      { color: #fca5a5; opacity: 1; }

#tm-chat-close {
  margin-left: auto;
  background: none;
  border: none;
  color: inherit;
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
  border-radius: 4px;
  opacity: .8;
}
#tm-chat-close:hover { opacity: 1; }

/* Mensajes */
#tm-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-sm, 16px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.tm-msg {
  display: flex;
  flex-direction: column;
  max-width: 82%;
  gap: 3px;
}
.tm-msg.tm-bot  { align-self: flex-start; }
.tm-msg.tm-user { align-self: flex-end; }
.tm-msg.tm-alert { align-self: center; max-width: 92%; }

.tm-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-family: var(--font-body, sans-serif);
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
}
.tm-bot  .tm-bubble {
  background: var(--color-surface, #F8F5FF);
  color: var(--color-text, #1E1B4B);
  border-bottom-left-radius: 4px;
}
.tm-user .tm-bubble {
  background: var(--color-primary, #7C3AED);
  color: var(--color-light, #fff);
  border-bottom-right-radius: 4px;
}
.tm-alert .tm-bubble {
  background: var(--color-surface-2, #FFF7ED);
  border: 1px solid var(--color-cta, #F97316);
  color: var(--color-text, #1E1B4B);
  border-radius: 10px;
  font-size: 13px;
  text-align: center;
}

.tm-time {
  font-family: var(--font-body, sans-serif);
  font-size: 11px;
  color: var(--color-text-muted, #64748B);
  padding: 0 4px;
}
.tm-user .tm-time { text-align: right; }

/* Typing indicator */
.tm-typing .tm-bubble {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 12px 16px;
}
.tm-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--color-primary, #7C3AED);
  animation: tm-bounce .9s infinite ease-in-out;
}
.tm-dot:nth-child(2) { animation-delay: .15s; }
.tm-dot:nth-child(3) { animation-delay: .30s; }
@keyframes tm-bounce {
  0%,80%,100% { transform: translateY(0); opacity:.4; }
  40%          { transform: translateY(-5px); opacity:1; }
}

/* Input */
#tm-chat-input-area {
  display: flex;
  gap: 8px;
  padding: 12px var(--space-sm, 16px);
  border-top: 1px solid var(--color-border, #E8E0FA);
  flex-shrink: 0;
  background: var(--color-bg, #fff);
}
#tm-chat-input {
  flex: 1;
  border: 1px solid var(--color-border, #E8E0FA);
  border-radius: var(--radius-input, 8px);
  padding: 9px 12px;
  font-family: var(--font-body, sans-serif);
  font-size: 14px;
  color: var(--color-text, #1E1B4B);
  background: var(--color-surface, #F8F5FF);
  outline: none;
  transition: border-color .2s;
}
#tm-chat-input:focus {
  border-color: var(--color-primary, #7C3AED);
}
#tm-chat-input:disabled {
  opacity: .5;
  cursor: not-allowed;
}
#tm-chat-send {
  width: 40px; height: 40px;
  border-radius: var(--radius-input, 8px);
  background: var(--color-primary, #7C3AED);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .2s;
}
#tm-chat-send:hover  { background: var(--color-primary-dark, #5B21B6); }
#tm-chat-send:disabled { opacity: .5; cursor: not-allowed; }

/* Mobile */
@media (max-width: 480px) {
  #tm-chat-panel {
    right: 0; bottom: 0;
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }
  #tm-chat-toggle {
    bottom: 16px;
    right: 16px;
  }
}
