/* ==========================================================================
   Chatbot Ferrum - Estilos Globales, Fallbacks y Compatibilidad Móvil
   ========================================================================== */

/* 1. Reset, Variables Base y Protección contra FOUC (Flash of Unstyled Content) */
:root {
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --bg-dark: #22295B;
  --bg-surface: #1e254d;
  --primary-red: #ff4d4d;
  --text-main: #ffffff;
  --text-muted: rgba(255, 255, 255, 0.7);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  margin: 0;
  padding: 0;
  font-family: var(--font-mono);
  background-color: var(--bg-dark);
  background-image: url('fondochat2.png');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  background-repeat: no-repeat;
  color: var(--text-main);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Transición ultra-suave al cargar para evitar parpadeos de estilos */
body {
  opacity: 1;
}

/* ==========================================================================
   Fallback Estructural (Sin romper prioridad de .hidden)
   ========================================================================== */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.justify-center { justify-content: center; }
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.inset-0 { top: 0; right: 0; bottom: 0; left: 0; }
.w-full { width: 100%; }
.h-full { height: 100%; }
.rounded-full { border-radius: 9999px; }
.text-center { text-align: center; }
.pointer-events-none { pointer-events: none; }

/* .hidden SIEMPRE tiene la máxima prioridad */
.hidden, [hidden] {
  display: none !important;
}

/* Estructura Base de Encabezado y Navegación */
header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 20;
}

header a, header button {
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}

/* Estructura Base de Contenido Principal */
#main-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

/* Caja de entrada flotante */
.chat-floating-area {
  display: flex;
  flex-direction: column;
  z-index: 20;
}

#chat-input-container {
  background: rgba(26, 32, 73, 0.75);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

#chat-input {
  background: transparent;
  border: none;
  outline: none;
  color: #ffffff;
  font-family: inherit;
  resize: none;
}

#chat-input::placeholder {
  color: rgba(219, 234, 254, 0.45);
}

#chat-send {
  cursor: pointer;
  border: none;
}

/* 2. Correcciones de Canvas y GPU para iOS Safari y Android */
#antigravity-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
  opacity: 0.75;
}

#antigravity-canvas canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

/* Aislamiento de capas para evitar bugs de WebKit/Safari */
.layer-isolate {
  isolation: isolate;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
}

/* 3. Soporte para Glassmorphism / Backdrop Blur */
.glass-panel {
  background: rgba(30, 37, 77, 0.75);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.glass-pill {
  background: rgba(255, 255, 255, 0.08);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

/* 4. Animaciones del Chat */
@keyframes messageSlideUp {
  from {
    opacity: 0;
    transform: translateY(16px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

#chat-log > * {
  animation: messageSlideUp 0.35s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;
}

/* Transición suave para ocultar pantalla inicial */
main {
  transition: opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1), transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

main.chat-main-hidden {
  opacity: 0 !important;
  transform: translateY(-8px) scale(0.98) !important;
  pointer-events: none !important;
  visibility: hidden !important;
  height: 0 !important;
  min-height: 0 !important;
  max-height: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden !important;
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s, height 0.2s, min-height 0.2s, max-height 0.2s, padding 0.2s, margin 0.2s !important;
}

/* Expansión del área de chat */
.chat-floating-area.chat-expanded {
  height: calc(100dvh - 6.5rem) !important;
}

.chat-floating-area.chat-expanded #chat-log {
  max-height: none !important;
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Indicador de carga */
.chat-loading {
  display: none;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.45rem;
  color: rgba(148, 163, 184, 0.9);
  font-size: 0.75rem;
  letter-spacing: 0.02em;
}

.chat-loading.is-visible {
  display: flex;
}

.chat-spinner {
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255, 77, 77, 0.2);
  border-top-color: rgba(255, 77, 77, 0.9);
  border-radius: 999px;
  animation: chatSpin 0.9s linear infinite;
}

.chat-loader-row {
  margin-top: 0.15rem;
}

.chat-loader-bubble {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.65rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(226, 232, 240, 0.8);
  color: rgba(100, 116, 139, 0.9);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  font-size: 0.75rem;
  letter-spacing: 0.02em;
}

@keyframes chatSpin {
  to {
    transform: rotate(360deg);
  }
}

/* Ocultar barra de scroll del chat pero manteniendo desplazamiento táctil y con rueda */
#chat-log {
  scrollbar-width: none !important;
  -ms-overflow-style: none !important;
  -webkit-overflow-scrolling: touch;
}

#chat-log::-webkit-scrollbar {
  display: none !important;
  width: 0 !important;
  height: 0 !important;
}

/* Scrollbar discreto solo para la tabla del Panel de Auditoría */
.custom-scrollbar {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
  -webkit-overflow-scrolling: touch;
}

.custom-scrollbar::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 4px;
}

/* Responsive adjustments para pantallas pequeñas (móviles) */
@media (max-width: 640px) {
  #main-content {
    padding-top: 3.5rem !important;
    padding-bottom: 6rem !important;
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }
  
  .chat-floating-area {
    bottom: 0.75rem !important;
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
  }
  
  .chat-floating-area.chat-expanded {
    height: calc(100dvh - 4rem) !important;
  }
  
  #greeting-text {
    font-size: 0.8rem !important;
  }
  
  /* Header más compacto en móvil */
  header {
    padding: 0.5rem 0.75rem !important;
  }
  
  /* Modal de auditoría: full-screen en móvil con botón cerrar visible */
  #admin-modal > div {
    max-height: 95vh !important;
    margin: 0.25rem !important;
    border-radius: 1rem !important;
  }
  
  #admin-legend-modal > div {
    max-height: 90vh !important;
    margin: 0.5rem !important;
    overflow-y: auto;
  }
  
  /* Disclaimer más compacto */
  .absolute.bottom-3 {
    font-size: 0.6rem !important;
    bottom: 0.15rem !important;
  }
}

/* Pantallas muy pequeñas (< 380px, ej: iPhone SE) */
@media (max-width: 380px) {
  #main-content {
    padding-top: 3rem !important;
    padding-bottom: 5rem !important;
  }
  
  header img {
    max-width: 6rem !important;
    height: auto !important;
  }
}

