/* Variables con unidades relativas y paleta minimalista */
:root {
  /* Colores minimalistas */
  --color-white: #ffffff;
  --color-black: #000000;
  --color-gray-100: #f5f5f5;
  --color-gray-200: #eeeeee;
  --color-gray-300: #e0e0e0;
  --color-gray-900: #212121;
  --color-link: #0073aa;
  --color-link-hover: #00a0d2;

  /* Dimensiones base usando rem */
  --base-unit: 1rem;
  --chat-width: 22rem;
  --chat-height: 30rem;
  --toggle-size: 3.75rem;

  /* Espaciado */
  --space-xs: calc(var(--base-unit) * 0.25);
  --space-sm: calc(var(--base-unit) * 0.5);
  --space-md: var(--base-unit);
  --space-lg: calc(var(--base-unit) * 1.5);

  /* Efectos */
  --shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.1);
  --transition: 0.3s ease;
}

.chatbot-widget {
  position: fixed;
  bottom: var(--space-lg);
  right: var(--space-lg);
  z-index: 9999;
  font-size: var(--base-unit);
  line-height: 1.5;

  /* Botón toggle */
  .chatbot-toggle {
    width: var(--toggle-size);
    height: var(--toggle-size);
    padding: 0;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    background: var(--color-black);
    box-shadow: var(--shadow);
    transition: transform var(--transition);
    overflow: hidden;

    &:hover {
      transform: scale(1.1);
    }

    .toggle-logo-container {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;

      img {
        width: 75%;
        height: 75%;
        object-fit: cover;
        border-radius: 50%;
      }
    }
  }

  /* Contenedor del chat */
  #chatbot-container {
    position: fixed;
    bottom: calc(var(--toggle-size) + var(--space-lg));
    right: var(--space-lg);
    width: var(--chat-width);
    height: var(--chat-height);
    display: none;
    flex-direction: column;
    background: var(--color-white);
    border-radius: calc(var(--base-unit)* 0.75);
    box-shadow: var(--shadow);
    margin-block: 1rem;
    overflow: hidden;

    /* Estilos para contenido HTML en mensajes */
    .message-content {
      font-size: 0.9rem;
      line-height: 1.5;

      p {
        margin: 0.5rem 0;

        &:first-child {
          margin-top: 0;
        }

        &:last-child {
          margin-bottom: 0;
        }
      }

      a {
        color: var(--color-link);
        text-decoration: underline;
        word-break: break-word;

        &:hover {
          color: var(--color-link-hover);
        }
      }

      img {
        max-width: 100%;
        height: auto;
        margin: 0.5rem 0;
        border-radius: 0.25rem;
      }

      ul,
      ol {
        margin: 0.5rem 0;
        padding-left: 1.5rem;
      }

      li {
        margin: 0.25rem 0;
      }

      blockquote {
        margin: 0.5rem 0;
        padding-left: 1rem;
        border-left: 3px solid var(--color-gray-300);
        color: var(--color-gray-900);
        font-style: italic;
      }

      h1,
      h2,
      h3,
      h4,
      h5,
      h6 {
        margin: 0.75rem 0;
        line-height: 1.2;
        color: var(--color-gray-900);
      }

      code {
        background: var(--color-gray-100);
        padding: 0.2rem 0.4rem;
        border-radius: 0.25rem;
        font-family: monospace;
        font-size: 0.85em;
      }

      table {
        width: 100%;
        margin: 0.5rem 0;
        border-collapse: collapse;
        font-size: 0.85em;

        th,
        td {
          padding: 0.25rem;
          border: 1px solid var(--color-gray-300);
          text-align: left;
        }

        th {
          background: var(--color-gray-100);
        }
      }
    }

    .chatbot-whatsapp-contact {
      display: flex;
      width: 100%;
      padding: 1rem;

      .whatsapp-button {
        display: flex;
        gap: var(--space-sm);
        padding: var(--space-sm) var(--space-md);
        background: #25D366;
        color: var(--color-white);
        text-decoration: none;
        border-radius: 1.5rem;
        transition: var(--transition);
        font-size: 0.875rem;
        width: 100%;
        height: 2.5rem;
        text-align: center;
        text-wrap: nowrap;
        justify-content: center;
        align-items: center;

        &:hover {
          background: #128C7E;
        }

        img {
          width: max-content;
          height: 100%;
        }

        span {
          white-space: nowrap;
        }
      }
    }

    /* Header */
    .chatbot-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: var(--space-md);
      background: var(--color-black);
      color: var(--color-white);
      flex: 0 0 auto;

      .chatbot-header-info {
        display: flex;
        align-items: center;
        gap: var(--space-sm);

        .header-logo-container {
          width: 2rem;
          height: 2rem;
          border-radius: 50%;
          overflow: hidden;

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
          }
        }
      }

      .chatbot-close {
        background: none;
        border: none;
        color: var(--color-white);
        cursor: pointer;
        padding: var(--space-xs);
        opacity: 0.8;
        transition: opacity var(--transition);

        &:hover {
          opacity: 1;
        }
      }
    }

    /* Área de mensajes */
    #chatbot-messages {
      flex: 1 1 auto;
      display: flex;
      flex-direction: column;
      gap: var(--space-sm);
      padding: var(--space-md);
      overflow-y: auto;
      min-height: 0;
      background: var(--color-white);

      /* Scrollbar personalizado */
      &::-webkit-scrollbar {
        width: 0.375rem;
      }

      &::-webkit-scrollbar-track {
        background: var(--color-gray-200);
      }

      &::-webkit-scrollbar-thumb {
        background: var(--color-gray-300);
        border-radius: 0.375rem;

        &:hover {
          background: var(--color-gray-900);
        }
      }

      scrollbar-width: thin;
      scrollbar-color: var(--color-gray-300) var(--color-gray-200);

      /* Mensajes */
      .message {
        display: flex;
        align-items: flex-start;
        max-width: 85%;
        position: relative;

        &.bot {
          margin-right: auto;
          padding-left: 2.5rem;

          .message-content {
            background: var(--color-gray-100);
            color: var(--color-black);
            border-radius: 0 1rem 1rem 1rem;
          }

          .bot-avatar {
            position: absolute;
            left: 0;
            top: 0;
            width: 2rem;
            height: 2rem;
            border-radius: 50%;
            object-fit: cover;
          }
        }

        &.user {
          margin-left: auto;

          .message-content {
            background: var(--color-black);
            color: var(--color-white);
            border-radius: 1rem 0 1rem 1rem;
          }
        }

        .message-content {
          padding: var(--space-sm) var(--space-md);
          word-wrap: break-word;
          overflow-wrap: break-word;
          font-size: 0.95em;
          line-height: 1.5;

          /* Elementos de texto básicos */
          p {
            margin: 0.5em 0;

            &:first-child {
              margin-top: 0;
            }

            &:last-child {
              margin-bottom: 0;
            }
          }

          /* Formato de texto */
          strong,
          b {
            font-weight: 700;
          }

          em,
          i {
            font-style: italic;
          }

          u {
            text-decoration: underline;
          }

          strike,
          del {
            text-decoration: line-through;
            opacity: 0.7;
          }

          /* Enlaces */
          a {
            text-decoration: underline;

            .message.bot & {
              color: var(--color-link);

              &:hover {
                color: var(--color-link-hover);
              }
            }

            .message.user & {
              color: rgba(255, 255, 255, 0.9);

              &:hover {
                color: var(--color-white);
              }
            }
          }

          /* Encabezados */
          h1,
          h2,
          h3,
          h4,
          h5,
          h6 {
            font-weight: 600;
            line-height: 1.3;
            margin: 0.75em 0 0.5em;
          }

          h1 {
            font-size: 1.4em;
          }

          h2 {
            font-size: 1.3em;
          }

          h3 {
            font-size: 1.2em;
          }

          h4 {
            font-size: 1.1em;
          }

          h5 {
            font-size: 1em;
          }

          h6 {
            font-size: 0.9em;
          }

          /* Listas */
          ul,
          ol {
            padding-left: 1.5em;
            margin: 0.5em 0;
          }

          ul {
            list-style-type: disc;
          }

          ol {
            list-style-type: decimal;
          }

          li {
            margin: 0.3em 0;
          }

          /* Elementos especiales */
          blockquote {
            border-left: 3px solid var(--color-gray-300);
            margin: 1em 0;
            padding: 0.5em 0 0.5em 1em;
            font-style: italic;
            opacity: 0.9;
          }

          code {
            font-family: monospace;
            background: rgba(0, 0, 0, 0.05);
            padding: 0.2em 0.4em;
            border-radius: 3px;
            font-size: 0.9em;
          }

          pre {
            background: rgba(0, 0, 0, 0.05);
            padding: 1em;
            border-radius: 4px;
            overflow-x: auto;
            margin: 0.5em 0;

            code {
              background: none;
              padding: 0;
            }
          }

          /* Tablas */
          table {
            width: 100%;
            border-collapse: collapse;
            margin: 0.5em 0;
            font-size: 0.9em;

            th,
            td {
              border: 1px solid var(--color-gray-300);
              padding: 0.5em;
              text-align: left;
            }

            th {
              background: rgba(0, 0, 0, 0.05);
              font-weight: 600;
            }
          }

          /* Imágenes */
          img {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
            margin: 0.5em 0;
            display: block;
          }

          /* Alineación de texto */
          .text-left {
            text-align: left;
          }

          .text-center {
            text-align: center;
          }

          .text-right {
            text-align: right;
          }

          .text-justify {
            text-align: justify;
          }
        }
      }
    }

    /* Input */
    #chatbot-input-container {
      position: sticky;
      bottom: 0;
      padding: var(--space-md);
      background: var(--color-white);
      border-top: 1px solid var(--color-gray-200);
      z-index: 1;
      display: flex;
      align-items: center;
      gap: var(--space-sm);

      input {
        flex: 1;
        font-size: 0.9rem;

        &:focus {
          border-color: var(--color-gray-900);
        }
      }

      .input-icon {
        flex-shrink: 0;
        color: var(--color-gray-900);
        cursor: pointer;
        padding: var(--space-xs);
        transition: color var(--transition);

        &:hover {
          color: var(--color-black);
        }
      }
    }

    /* Sugerencias */
    #chatbot-suggested-questions {
      flex: 0 0 auto;
      padding: var(--space-md);
      display: flex;
      flex-wrap: wrap;
      gap: var(--space-sm);
      border-bottom: 1px solid var(--color-gray-200);

      .chatbot-suggested-question {
        background: var(--color-gray-100);
        border: none;
        border-radius: 1rem;
        padding: var(--space-sm) var(--space-md);
        cursor: pointer;
        transition: background var(--transition);
        font-size: 0.9rem;
        color: var(--color-gray-900);

        &:hover {
          background: var(--color-gray-200);
        }
      }
    }
  }
}

/* Animaciones */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message {
  animation: fadeIn 0.3s ease-out;
}

/* Media queries con unidades relativas */
@media (max-width: 768px) {
  :root {
    --chat-width: 100vw;
    --chat-height: 100vh;
  }

  .chatbot-widget {
    #chatbot-container {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      border-radius: 0;
      margin: 0;
    }

    .chatbot-toggle {
      width: calc(var(--toggle-size) * 0.8);
      height: calc(var(--toggle-size) * 0.8);
    }
  }
}

@media (max-width: 480px) {
  .chatbot-widget {
    #chatbot-messages {
      .message {
        max-width: 90%;

        &.bot,
        &.user {
          .message-content {
            font-size: 0.85rem;
          }
        }
      }
    }

    #chatbot-suggested-questions {
      .chatbot-suggested-question {
        font-size: 0.8rem;
      }
    }
  }
}

/* Animación de escribiendo... */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  margin-block: auto;
}

.typing-indicator span {
  display: inline-block;
  width: 0.2rem;
  height: 0.2rem;
  background: var(--color-gray-300);
  border-radius: 50%;
  animation: typing 1.5s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.4s;
}

/* Animación de puntos */
@keyframes typing {
  0%, 80%, 100% {
    transform: scale(0);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Anidar en mensajes */
#chatbot-messages .message.typing {
  margin-right: auto;
  padding-left: 2.5rem;

  .message-content {
    background: none;
    box-shadow: none;
  }

  .typing-indicator {
    justify-content: flex-start;
  }
}
