:root {
  --bg-color: #0b111e;
  --panel-bg: #121b2d;
  --card-bg: #182338;
  --card-hover: #22304c;
  --modal-bg: #11192a;
  --text-color: #f1f5f9;
  --text-muted: #94a3b8;
  --border-color: rgba(255, 255, 255, 0.08);

  --nonmetal: #22c55e;
  --noble-gas: #a855f7;
  --alkali: #ef4444;
  --alkaline-earth: #f97316;
  --metalloid: #eab308;
  --halogen: #06b6d4;
  --transition: #3b82f6;
  --post-transition: #94a3b8;
  --lanthanide: #ec4899;
  --actinide: #d946ef;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  background:
    radial-gradient(ellipse 80% 50% at 50% -20%, rgba(59, 130, 246, 0.15), transparent),
    radial-gradient(circle at 10% 20%, rgba(34, 197, 94, 0.08), transparent 25rem),
    radial-gradient(circle at 90% 80%, rgba(217, 70, 239, 0.08), transparent 30rem),
    #0b111e;
  color: var(--text-color);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.page-shell {
  width: min(100%, 1560px);
  margin: 0 auto;
  padding: 12px 14px 28px;
}

/* Encabezado: sólo el título */
.page-header {
  text-align: center;
  margin-bottom: 12px;
}

.page-header h1 {
  font-size: clamp(1.4rem, 2.2vw, 2.2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #ffffff 30%, #93c5fd 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  display: inline-block;
}

/* Sección de controles: búsqueda y familias químicas (colocada debajo de la tabla) */
.controls-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: rgba(18, 27, 45, 0.65);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 12px 16px;
  backdrop-filter: blur(8px);
}

.legend-header {
  text-align: center;
}

.legend-title {
  font-size: 0.74rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

/* Leyenda de familias químicas */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.legend-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 32px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.13);
  border-radius: 5px;
  padding: 6px 10px;
  color: #e2e8f0;
  font-size: clamp(0.7rem, 0.76vw, 0.78rem);
  cursor: pointer;
  transition: all 0.18s ease;
  user-select: none;
}

.legend-chip:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-color);
  transform: translateY(-1px);
}

.legend-chip.active {
  color: #07111f;
  font-weight: 600;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.26);
}

.no-metal-filter.active { background: var(--nonmetal); border-color: var(--nonmetal); }
.gas-noble-filter.active { background: var(--noble-gas); border-color: var(--noble-gas); color: #fff; }
.alcalino-filter.active { background: var(--alkali); border-color: var(--alkali); color: #fff; }
.alcalinoterreo-filter.active { background: var(--alkaline-earth); border-color: var(--alkaline-earth); color: #fff; }
.metaloide-filter.active { background: var(--metalloid); border-color: var(--metalloid); }
.halogeno-filter.active { background: var(--halogen); border-color: var(--halogen); }
.transicion-filter.active { background: var(--transition); border-color: var(--transition); color: #fff; }
.post-transicion-filter.active { background: var(--post-transition); border-color: var(--post-transition); }
.lantanido-filter.active { background: var(--lanthanide); border-color: var(--lanthanide); color: #fff; }
.actinido-filter.active { background: var(--actinide); border-color: var(--actinide); color: #fff; }

.legend-chip[data-category="all"].active {
  background: #bfdbfe;
  border-color: #bfdbfe;
}

.chip-color {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.all-chip {
  background: linear-gradient(135deg, #3b82f6, #a855f7);
}

/* Contenedor adaptativo: todos los elementos visibles a la vez */
.table-scroll {
  width: 100%;
  margin-top: 12px;
  padding: 8px 6px;
  background: rgba(14, 21, 35, 0.7);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.35);
  overflow-x: auto;
  position: relative;
}

/* El hueco entre los grupos 2 y 13 alberga la leyenda de la referencia. */
.table-scroll .controls-section {
  position: absolute;
  z-index: 2;
  top: 16px;
  left: 14%;
  width: 50%;
  min-height: clamp(150px, 14.5vw, 195px);
  margin: 0;
  padding: 10px 12px;
  justify-content: center;
  background:
    radial-gradient(circle at 50% 0%, rgba(59, 130, 246, 0.12), transparent 58%),
    rgba(18, 27, 45, 0.88);
}

.table-scroll .legend {
  gap: 5px;
}

.table-scroll .legend-chip {
  min-height: 27px;
  padding: 4px 7px;
}

.periodic-table {
  display: grid;
  grid-template-columns: repeat(18, minmax(0, 1fr));
  grid-template-rows: repeat(7, minmax(0, 1fr)) clamp(6px, 1vw, 12px) repeat(2, minmax(0, 1fr));
  gap: clamp(2px, 0.32vw, 4px);
  width: 100%;
}

/* En una búsqueda o filtro, los resultados se reordenan para no dejar huecos. */
/* Tarjetas individuales de elementos */
.element-card {
  min-width: 0;
  min-height: 0;
  width: 100%;
  aspect-ratio: 1 / 1.08;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-bottom: 3px solid var(--transition);
  border-radius: 6px;
  background: var(--card-bg);
  color: var(--text-color);
  cursor: pointer;
  display: grid;
  grid-template-rows: auto 1fr auto;
  align-items: center;
  padding: clamp(2px, 0.3vw, 4px) clamp(1px, 0.25vw, 3px);
  transition: transform 0.16s cubic-bezier(0.2, 0, 0.2, 1),
              box-shadow 0.16s ease,
              border-color 0.16s ease,
              opacity 0.2s ease,
              filter 0.2s ease;
  user-select: none;
  text-align: center;
}

.element-card:hover,
.element-card:focus-visible {
  transform: translateY(-3px) scale(1.06);
  z-index: 10;
  outline: none;
  background: var(--card-hover);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.55), 0 0 10px rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
}

.element-card.is-dimmed {
  opacity: 0.18;
  filter: grayscale(0.85);
  transform: scale(0.96);
}

.element-card.is-highlighted {
  opacity: 1;
  filter: none;
  transform: scale(1.03);
  box-shadow: 0 0 16px rgba(255, 255, 255, 0.22);
}

.element-card .number {
  justify-self: start;
  font-size: clamp(0.5rem, 0.72vw, 0.72rem);
  font-weight: 600;
  line-height: 1;
  color: var(--text-muted);
  padding-left: 1px;
}

.element-card .symbol {
  font-size: clamp(0.82rem, 1.35vw, 1.35rem);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.01em;
}

.element-card .name {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: clamp(0.42rem, 0.65vw, 0.64rem);
  line-height: 1;
  color: #cbd5e1;
}

/* Colores de las familias químicas */
.no-metal { border-bottom-color: var(--nonmetal); }
.gas-noble { border-bottom-color: var(--noble-gas); }
.alcalino { border-bottom-color: var(--alkali); }
.alcalinoterreo { border-bottom-color: var(--alkaline-earth); }
.metaloide { border-bottom-color: var(--metalloid); }
.halogeno { border-bottom-color: var(--halogen); }
.transicion { border-bottom-color: var(--transition); }
.post-transicion { border-bottom-color: var(--post-transition); }
.lantanido { border-bottom-color: var(--lanthanide); }
.actinido { border-bottom-color: var(--actinide); }

/* Etiquetas de series para lantánidos y actínidos */
.series-label {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 14px;
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.series-label span {
  font-size: 0.68rem;
  opacity: 0.7;
  margin-right: 6px;
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 4px;
}

.lantanido-label { color: var(--lanthanide); }
.actinido-label { color: var(--actinide); }

/* Modal interactivo */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background-color: rgba(3, 7, 18, 0.85);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.2s ease-out;
}

.modal.is-open {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes modalScale {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.modal-content {
  position: relative;
  width: min(94vw, 860px);
  max-height: min(90vh, 750px);
  overflow-y: auto;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 16px;
  background: var(--modal-bg);
  box-shadow: 0 25px 70px rgba(0, 0, 0, 0.6), 0 0 30px rgba(59, 130, 246, 0.1);
  padding: 24px;
  animation: modalScale 0.22s cubic-bezier(0.16, 1, 0.3, 1);
}

.close-btn {
  position: absolute;
  top: 14px;
  right: 16px;
  width: 36px;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-color);
  cursor: pointer;
  font-size: 1.4rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.18s;
  z-index: 10;
}

.close-btn:hover {
  background: rgba(239, 68, 68, 0.25);
  border-color: #ef4444;
  color: #fff;
  transform: rotate(90deg);
}

/* Barra de navegación dentro del modal */
.modal-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-right: 42px;
}

.nav-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  color: var(--text-color);
  padding: 5px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.nav-btn:hover {
  background: rgba(59, 130, 246, 0.2);
  border-color: #3b82f6;
}

.category-badge {
  font-size: 0.8rem;
  font-weight: 700;
  padding: 4px 12px;
  border-radius: 20px;
  border: 1px solid transparent;
  letter-spacing: 0.02em;
}

/* Cabecera del modal */
.modal-header-info {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.symbol-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 60px;
  height: 60px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.9);
  border: 2px solid #3b82f6;
  padding: 4px 10px;
}

.symbol-sub {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1;
}

.symbol-main {
  font-size: 1.65rem;
  font-weight: 900;
  line-height: 1.1;
}

.modal-title-wrap h2 {
  font-size: clamp(1.4rem, 2.5vw, 1.85rem);
  font-weight: 800;
  letter-spacing: -0.01em;
}

.modal-subtitle {
  color: var(--text-muted);
  font-size: 0.86rem;
  margin-top: 3px;
}

/* Cuerpo del modal en 2 columnas */
.modal-body {
  display: grid;
  grid-template-columns: minmax(260px, 0.95fr) minmax(280px, 1.05fr);
  gap: 24px;
  align-items: start;
}

/* Sección de la imagen real */
.modal-media {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.image-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 4 / 3;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  overflow: hidden;
  background: #080d1a;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

#modal-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.3s ease;
}

.image-wrapper:hover #modal-image {
  transform: scale(1.03);
}

.real-photo-pill {
  position: absolute;
  bottom: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(2, 6, 23, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.18);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 600;
  color: #38bdf8;
  backdrop-filter: blur(6px);
}

.image-caption {
  font-size: 0.8rem;
  line-height: 1.4;
  color: #cbd5e1;
  font-style: italic;
  padding: 4px 2px;
  background: rgba(255, 255, 255, 0.02);
  border-left: 2px solid #38bdf8;
  padding-left: 8px;
  border-radius: 0 4px 4px 0;
}

/* Grilla de propiedades analíticas */
.modal-info {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.info-cell {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.cell-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.cell-val {
  font-size: 0.95rem;
  font-weight: 700;
  color: #ffffff;
}

.config-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85rem;
  color: #7dd3fc;
}

.section-title {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #60a5fa;
  margin-bottom: 6px;
}

.desc-box {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  padding: 12px;
}

.desc-box p {
  font-size: 0.88rem;
  line-height: 1.5;
  color: #e2e8f0;
}

.discovery-box {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 8px;
  padding: 10px 12px;
}

.discovery-box p {
  font-size: 0.84rem;
  line-height: 1.4;
  color: #cbd5e1;
}

/* Scrollbar estilizada */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: rgba(11, 17, 30, 0.8);
}

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

::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* Responsividad para móviles y pantallas pequeñas */
@media (max-width: 820px) {
  .page-shell {
    padding: 16px 10px 32px;
  }

  .modal-body {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .info-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .periodic-table {
    grid-template-columns: repeat(18, 50px);
    grid-template-rows: repeat(10, 58px);
    gap: 5px;
    min-width: 1000px;
  }

  /* En pantallas pequeñas el panel vuelve arriba para no tapar elementos. */
  .table-scroll .controls-section {
    position: static;
    width: auto;
    min-height: 0;
    margin-bottom: 12px;
  }

}
