/**
 * ELEMENTA - Estilos de la Tabla Periódica Tradicional & Educativa
 * Inspirada en el póster clásico de la Tabla Periódica de los Elementos
 *
 * Diseño 100% fluido: la tabla siempre ocupa el ancho disponible (sin scroll
 * horizontal). Los tamaños usan unidades de contenedor (cqi) y el nivel de
 * detalle de cada celda se reduce por container queries en pantallas pequeñas:
 *   ≥ 1200px  → 6 campos completos (póster clásico)
 *   < 1200px  → número, masa, símbolo y nombre
 *   <  760px  → número y símbolo
 *   <  520px  → solo símbolo (móviles en vertical)
 * Los valores en px antes de cada clamp() son el respaldo para navegadores
 * sin soporte de container queries.
 */

.table-viewport-container {
  width: 100%;
  padding: 10px 0 30px 0;
  container-type: inline-size;
  container-name: ptable;

  /* Columna de periodos + 18 grupos: compartida por la tabla y las filas f */
  --pt-cols: minmax(0, 0.42fr) repeat(18, minmax(0, 1fr));
  --pt-gap: 3px;
  --pt-gap: clamp(1px, 0.22cqi, 3px);
  --pt-cell-ratio: 78 / 92;
}

/* Big Centered Title (as shown in reference image) */
.periodic-main-title {
  text-align: center;
  font-size: 28px;
  font-size: clamp(16px, 2cqi, 28px);
  font-weight: 900;
  letter-spacing: 0.5px;
  color: var(--text-primary);
  margin-bottom: 12px;
  font-family: 'Inter', sans-serif;
  text-transform: uppercase;
  line-height: 1.2;
}

/* Main 18-column Periodic Grid */
.periodic-grid-table {
  display: grid;
  grid-template-columns: var(--pt-cols);
  grid-template-rows: auto;
  grid-auto-rows: auto;
  gap: var(--pt-gap);
  width: 100%;
  margin: 0 auto;
  background: transparent;
}

/* Header Cells (Groups: IUPAC + Roman) */
.grid-header-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  font-weight: 800;
  color: var(--text-primary);
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 3px 1px;
  line-height: 1.1;
  overflow: hidden;
  transition: all var(--transition-fast);
}

.grid-header-col .iupac-num {
  font-size: 13px;
  font-size: clamp(7px, 0.9cqi, 13px);
  font-weight: 900;
}

.grid-header-col .roman-num {
  font-size: 10px;
  font-size: clamp(6px, 0.68cqi, 10px);
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.grid-header-col:hover {
  background: var(--accent-cyan);
  color: #fff;
  border-color: var(--accent-cyan);
}

.grid-header-col:hover .roman-num {
  color: #fff;
}

/* Row Period Header */
.grid-header-row {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  font-size: 14px;
  font-size: clamp(7px, 0.95cqi, 14px);
  font-weight: 900;
  color: var(--text-primary);
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.grid-header-row:hover {
  background: var(--accent-cyan);
  color: #fff;
  border-color: var(--accent-cyan);
}

.grid-corner-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
  overflow: hidden;
  font-size: 9px;
  font-size: clamp(5px, 0.55cqi, 9px);
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  text-align: center;
  line-height: 1.1;
}

/* Central "Notación / Guía" Legend Box */
.grid-notation-box {
  grid-column: 4 / 14;
  grid-row: 2 / 5;
  min-width: 0;
  background: var(--bg-surface);
  border: 2px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 12px 20px;
  padding: clamp(4px, 0.8cqi, 12px) clamp(6px, 1.4cqi, 20px);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  z-index: 10;
  margin: 4px;
  margin: clamp(1px, 0.3cqi, 4px);
  overflow: hidden;
}

.notation-card-wrapper {
  display: flex;
  align-items: center;
  gap: 24px;
  gap: clamp(8px, 1.6cqi, 24px);
}

.notation-diagram {
  position: relative;
  flex-shrink: 0;
  width: 140px;
  width: clamp(64px, 9.6cqi, 140px);
  aspect-ratio: 140 / 125;
  background: var(--bg-surface-elevated);
  border: 2px solid #0f172a;
  border-radius: 6px;
  padding: 6px 8px;
  padding: clamp(2px, 0.4cqi, 6px) clamp(3px, 0.55cqi, 8px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: 0 4px 10px rgba(0,0,0,0.1);
}

.nd-top {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  font-size: clamp(6px, 0.75cqi, 11px);
  font-family: 'JetBrains Mono', monospace;
  line-height: 1;
}

.nd-mid {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  align-items: center;
  margin: 2px 0;
}

.nd-en, .nd-ox {
  font-size: 10px;
  font-size: clamp(5px, 0.68cqi, 10px);
  font-weight: 700;
  color: var(--text-muted);
}

.nd-ox {
  text-align: right;
  color: var(--text-primary);
}

.nd-symbol {
  font-size: 26px;
  font-size: clamp(12px, 1.8cqi, 26px);
  font-weight: 900;
  text-align: center;
  line-height: 1;
}

.nd-config {
  font-size: 9.5px;
  font-size: clamp(5px, 0.65cqi, 9.5px);
  text-align: center;
  font-family: 'JetBrains Mono', monospace;
  color: var(--text-muted);
  line-height: 1;
}

.nd-name {
  font-size: 11px;
  font-size: clamp(6px, 0.75cqi, 11px);
  font-weight: 800;
  text-align: center;
  line-height: 1;
}

.notation-labels-left, .notation-labels-right {
  display: flex;
  flex-direction: column;
  gap: 8px;
  gap: clamp(2px, 0.55cqi, 8px);
  font-size: 12px;
  font-size: clamp(7px, 0.82cqi, 12px);
  color: var(--text-secondary);
  line-height: 1.2;
}

.notation-pointer-item {
  display: flex;
  align-items: center;
  gap: 6px;
  gap: clamp(3px, 0.4cqi, 6px);
}

.notation-pointer-item strong {
  color: var(--text-primary);
}

/* Classic 6-Field Element Cell (Exact to reference chart) */
.element-cell {
  position: relative;
  min-width: 0;
  aspect-ratio: var(--pt-cell-ratio);
  background: var(--bg-surface);
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 3px 5px 4px 5px;
  padding: clamp(1px, 0.22cqi, 3px) clamp(1px, 0.35cqi, 5px) clamp(1px, 0.28cqi, 4px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease, opacity 0.16s ease;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

/* Subtle category bar at cell top */
.element-cell::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  height: clamp(2px, 0.2cqi, 3px);
  background: var(--elem-accent, var(--accent-cyan));
}

.element-cell:focus-visible {
  transform: translateY(-3px) scale(1.06);
  z-index: 40;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2), 0 0 12px var(--elem-accent, rgba(2, 132, 199, 0.4));
  border-color: #0f172a;
  outline: none;
}

/* Hover zoom only on devices with a real pointer (avoids "sticky" hover on touch screens) */
@media (hover: hover) and (pointer: fine) {
  .element-cell:hover {
    transform: translateY(-3px) scale(1.06);
    z-index: 40;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2), 0 0 12px var(--elem-accent, rgba(2, 132, 199, 0.4));
    border-color: #0f172a;
  }
}

.element-cell:active {
  transform: scale(0.96);
}

.element-cell.dimmed {
  opacity: 0.15;
  filter: grayscale(80%);
}

.element-cell.highlight-column, .element-cell.highlight-row {
  border-color: var(--accent-blue);
  box-shadow: 0 0 8px rgba(37, 99, 235, 0.3);
}

/* Top Row: Atomic Num (left) & Atomic Mass (right) */
.cell-row-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 2px;
  line-height: 1;
  font-family: 'JetBrains Mono', monospace;
  min-width: 0;
}

.cell-atomic-num {
  font-weight: 900;
  font-size: 13px;
  font-size: clamp(6px, 0.88cqi, 13px);
  color: var(--text-primary);
}

.cell-atomic-mass {
  font-size: 10px;
  font-size: clamp(5px, 0.66cqi, 10px);
  font-weight: 600;
  color: var(--text-secondary);
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* Mid Row: Electronegativity (left), Symbol (center), Oxidation (right) */
.cell-row-mid {
  display: grid;
  grid-template-columns: 1fr 1.6fr 1.2fr;
  align-items: center;
  margin: 1px 0;
  min-width: 0;
}

.cell-electronegativity {
  font-size: 9px;
  font-size: clamp(5px, 0.6cqi, 9px);
  font-weight: 700;
  color: var(--text-muted);
  font-family: 'JetBrains Mono', monospace;
  line-height: 1;
}

.cell-symbol {
  font-size: 22px;
  font-size: clamp(9px, 1.5cqi, 24px);
  font-weight: 900;
  text-align: center;
  color: var(--text-primary);
  line-height: 1;
  letter-spacing: -0.5px;
}

.cell-oxidation {
  font-size: 8.5px;
  font-size: clamp(5px, 0.57cqi, 8.5px);
  font-weight: 700;
  color: var(--text-muted);
  text-align: right;
  font-family: 'JetBrains Mono', monospace;
  line-height: 1.1;
  word-break: break-all;
}

/* Sub-center Row: Electron Configuration */
.cell-electron-config {
  font-size: 8.5px;
  font-size: clamp(5px, 0.57cqi, 8.5px);
  color: var(--text-muted);
  text-align: center;
  font-family: 'JetBrains Mono', monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.1;
}

/* Bottom Row: Name */
.cell-name {
  font-size: 10.5px;
  font-size: clamp(6px, 0.72cqi, 10.5px);
  font-weight: 700;
  color: var(--text-primary);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.1;
  margin-top: 1px;
}

/* Placeholder for Lanthanides/Actinides in main table */
.grid-cell-placeholder {
  min-width: 0;
  aspect-ratio: var(--pt-cell-ratio);
  overflow: hidden;
  background: var(--bg-surface-elevated);
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.15;
}

.placeholder-range {
  font-weight: 900;
  color: var(--accent-blue);
  font-size: 12px;
  font-size: clamp(6px, 0.82cqi, 12px);
  white-space: nowrap;
}

.placeholder-label {
  font-size: 10px;
  font-size: clamp(5px, 0.68cqi, 10px);
  font-weight: 700;
  color: var(--text-secondary);
  white-space: nowrap;
}

.placeholder-series {
  font-size: 9px;
  font-size: clamp(5px, 0.6cqi, 9px);
  color: var(--text-muted);
  margin-top: 2px;
}

.placeholder-short {
  display: none;
  font-weight: 900;
  color: var(--accent-blue);
  font-size: clamp(9px, 3cqi, 16px);
  line-height: 1;
}

/* Bottom Detached Section (Lantánidos & Actínidos) — same columns as main grid */
.periodic-detached-wrapper {
  display: grid;
  grid-template-columns: var(--pt-cols);
  gap: var(--pt-gap);
  width: 100%;
  margin-top: 16px;
  margin-top: clamp(6px, 1.1cqi, 16px);
}

.detached-label {
  min-width: 0;
  overflow: hidden;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-size: clamp(6px, 0.75cqi, 11px);
  font-weight: 800;
  color: var(--text-primary);
  text-transform: uppercase;
  letter-spacing: 1px;
  text-align: center;
}

.detached-label-short {
  display: none;
  font-size: clamp(9px, 3cqi, 16px);
  color: var(--accent-blue);
  letter-spacing: 0;
}

/* State of Matter Glow in State Mode */
.element-cell.phase-solid {
  border-left: 3.5px solid #2563eb;
}

.element-cell.phase-liquid {
  border-left: 3.5px solid #0284c7;
  background: rgba(2, 132, 199, 0.08);
}

.element-cell.phase-gas {
  border-left: 3.5px solid #e11d48;
  background: rgba(225, 29, 72, 0.08);
}

/* ==========================================================================
   INTERACTIVIDAD: vista previa en vivo, encabezados activos y resaltados
   ========================================================================== */

/* El recuadro central alterna entre la leyenda y la ficha rápida del elemento */
.notation-preview {
  display: none;
  width: 100%;
  height: 100%;
  align-items: center;
  gap: clamp(8px, 1.4cqi, 20px);
  animation: previewIn 0.18s ease;
}

.grid-notation-box.is-previewing .notation-preview {
  display: flex;
}

.grid-notation-box.is-previewing .notation-card-wrapper {
  display: none;
}

.grid-notation-box.is-previewing {
  border-color: var(--accent-cyan);
}

@keyframes previewIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.np-tile {
  flex-shrink: 0;
  height: 100%;
  max-height: 150px;
  aspect-ratio: 1 / 1.1;
  border-radius: var(--radius-md);
  border: 2px solid var(--np-color);
  background: color-mix(in srgb, var(--np-color) 22%, var(--bg-surface));
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: clamp(3px, 0.5cqi, 8px);
  box-shadow: 0 0 18px color-mix(in srgb, var(--np-color) 45%, transparent);
}

.np-num {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 800;
  font-size: clamp(8px, 1cqi, 15px);
}

.np-symbol {
  text-align: center;
  font-weight: 900;
  line-height: 1;
  font-size: clamp(18px, 3.6cqi, 52px);
  color: var(--text-primary);
}

.np-mass {
  text-align: center;
  font-family: 'JetBrains Mono', monospace;
  font-size: clamp(7px, 0.8cqi, 12px);
  color: var(--text-secondary);
}

.np-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: clamp(2px, 0.4cqi, 6px);
}

.np-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 10px;
}

.np-name {
  font-weight: 900;
  font-size: clamp(12px, 1.55cqi, 24px);
  line-height: 1.1;
  color: var(--text-primary);
}

.np-cat {
  font-size: clamp(8px, 0.72cqi, 11px);
  font-weight: 700;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: color-mix(in srgb, var(--np-color) 25%, transparent);
  border: 1px solid var(--np-color);
  color: var(--text-primary);
  white-space: nowrap;
}

.np-config {
  font-family: 'JetBrains Mono', monospace;
  font-size: clamp(8px, 0.8cqi, 12px);
  color: var(--text-secondary);
}

.np-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(2px, 0.35cqi, 5px) clamp(4px, 0.8cqi, 12px);
  margin: 0;
}

.np-stats dt {
  font-size: clamp(6px, 0.6cqi, 9.5px);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.np-stats dd {
  margin: 0;
  font-size: clamp(8px, 0.82cqi, 12.5px);
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.np-cta {
  font-size: clamp(7px, 0.68cqi, 10.5px);
  font-weight: 700;
  color: var(--accent-cyan);
}

/* Encabezados de grupo/periodo del elemento señalado */
.grid-header-col.is-hot,
.grid-header-row.is-hot {
  background: var(--accent-cyan);
  border-color: var(--accent-cyan);
  color: #fff;
}

.grid-header-col.is-hot .roman-num {
  color: #fff;
}

/* Resaltado de una familia al pasar sobre la leyenda */
.is-cat-highlighting .element-cell:not(.cat-hot) {
  opacity: 0.18;
  filter: grayscale(70%);
}

.is-cat-highlighting .element-cell.cat-hot {
  border-color: var(--elem-accent);
  box-shadow: 0 0 10px var(--elem-accent);
}

/* Foco de teclado bien visible */
.element-cell:focus-visible {
  outline: 2px solid var(--accent-cyan);
  outline-offset: 1px;
}

/* ==========================================================================
   NIVELES DE DETALLE ADAPTATIVOS (según el ancho real disponible para la tabla)
   ========================================================================== */

/* Tabletas / portátiles pequeños: número, masa, símbolo y nombre */
@container ptable (max-width: 1199px) {

  .periodic-grid-table,
  .periodic-detached-wrapper {
    --pt-cell-ratio: 1 / 1.05;
  }

  .cell-electronegativity,
  .cell-oxidation,
  .cell-electron-config {
    display: none;
  }

  .cell-row-mid {
    display: block;
  }

  .cell-atomic-num { font-size: clamp(6px, 1.05cqi, 13px); }
  .cell-atomic-mass { font-size: clamp(5px, 0.8cqi, 10px); }
  .cell-symbol { font-size: clamp(9px, 2cqi, 22px); }
  .cell-name { font-size: clamp(6px, 0.9cqi, 10.5px); }

  .placeholder-series { display: none; }

  .notation-labels-left { display: none; }
  .notation-labels-right { font-size: clamp(7px, 1cqi, 12px); }
  .nd-en, .nd-ox, .nd-config { visibility: hidden; }
  .notation-diagram { width: clamp(64px, 11cqi, 120px); }
}

/* Tabletas en vertical / móviles en horizontal: número y símbolo */
@container ptable (max-width: 759px) {

  .periodic-grid-table,
  .periodic-detached-wrapper {
    --pt-cell-ratio: 1 / 1.1;
  }

  .cell-atomic-mass,
  .cell-name,
  .grid-header-col .roman-num,
  .placeholder-label {
    display: none;
  }

  .cell-row-top { justify-content: flex-start; }
  .cell-atomic-num { font-size: clamp(5px, 1.35cqi, 11px); font-weight: 700; }
  .cell-symbol { font-size: clamp(9px, 2.6cqi, 20px); }
  .element-cell { justify-content: flex-start; gap: 1px; }
  .cell-row-mid { flex: 1; display: flex; align-items: center; justify-content: center; }

  .grid-header-col .iupac-num { font-size: clamp(6px, 1.4cqi, 12px); }
  .grid-header-row { font-size: clamp(6px, 1.5cqi, 12px); }
  .grid-corner-label { visibility: hidden; }

  .placeholder-range { font-size: clamp(5px, 1.1cqi, 10px); }
  .detached-label { letter-spacing: 0; font-size: clamp(5px, 1.05cqi, 10px); }

  .notation-labels-right { display: none; }
  .nd-en, .nd-ox, .nd-config { visibility: visible; }
  .notation-diagram { width: clamp(60px, 16cqi, 120px); }
  .nd-top, .nd-name { font-size: clamp(6px, 1.3cqi, 10px); }
  .nd-en, .nd-ox, .nd-config { font-size: clamp(5px, 1.1cqi, 9px); }
  .nd-symbol { font-size: clamp(12px, 3.2cqi, 24px); }

  .element-cell::after { height: 2px; }
  .element-cell.phase-solid,
  .element-cell.phase-liquid,
  .element-cell.phase-gas { border-left-width: 2px; }
}

/* Móviles en vertical: solo el símbolo, lo más grande posible */
@container ptable (max-width: 519px) {

  .periodic-grid-table,
  .periodic-detached-wrapper {
    --pt-cols: minmax(0, 0.55fr) repeat(18, minmax(0, 1fr));
    --pt-gap: 1px;
    --pt-cell-ratio: 1 / 1.3;
  }

  .cell-row-top,
  .placeholder-range,
  .detached-label-full {
    display: none;
  }

  .placeholder-short,
  .detached-label-short {
    display: block;
  }

  .element-cell {
    padding: 0;
    border-width: 1px;
    border-radius: 2px;
    justify-content: center;
    box-shadow: none;
  }

  .cell-symbol { font-size: clamp(8px, 3.1cqi, 16px); letter-spacing: -0.3px; }

  .grid-header-col,
  .grid-header-row,
  .grid-cell-placeholder,
  .detached-label {
    border-radius: 2px;
    border-width: 1px;
  }

  .grid-header-col .iupac-num { font-size: clamp(6px, 2cqi, 11px); }
  .grid-header-row { font-size: clamp(6px, 2.2cqi, 11px); }

  /* La leyenda no cabe legible: se oculta (el modal muestra todos los datos) */
  .grid-notation-box { display: none; }
}


/* Impresión: tabla completa a lo ancho de la hoja */
@media print {
  .table-viewport-container {
    padding: 0;
  }
  .element-cell:hover,
  .element-cell:focus-visible {
    transform: none;
  }
}

/* Vista previa: menos datos cuando el recuadro central es más pequeño */
@container ptable (max-width: 1199px) {
  .np-extra { display: none; }
  .np-stats dd { font-size: clamp(7px, 1cqi, 12px); }
  .np-stats dt { font-size: clamp(6px, 0.75cqi, 9px); }
}

@container ptable (max-width: 759px) {
  .np-stats, .np-cta { display: none; }
  .np-name { font-size: clamp(11px, 2.4cqi, 18px); }
  .np-config, .np-cat { font-size: clamp(7px, 1.4cqi, 11px); }
  .np-symbol { font-size: clamp(16px, 5cqi, 36px); }
}
