/* ==========================================================================
   1. VARIABLES Y ESTILOS GLOBALES
   ========================================================================== */

:root {
  /* Paleta de colores */
  --c-brand: #4f46e5;
  --c-brand-hover: #4338ca;
  --c-text: #111827;
  --c-text-muted: #6b7280;
  --c-bg: #f9fafb;
  --c-surface: #ffffff;
  --c-border: #e5e7eb;
  --c-focus-ring: #818cf8;

  /* Sombras */
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);

  /* Tipografía y Espaciado */
  --font-sans: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --border-radius: 0.75rem;
  --spacing-md: 1.25rem;
  --spacing-lg: 2.5rem;
  --transition-speed: 200ms;

  /* Anti-CLS (Cumulative Layout Shift) */
  --h1-line-height: 1.2;
  --h1-min-height: calc(2.5rem * var(--h1-line-height));
}

/* Reseteo y configuración global */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  scroll-padding-top: 2rem;
}

body {
  font-family: var(--font-sans);
  background-color: var(--c-bg);
  color: var(--c-text);
  margin: 0;
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Accesibilidad del foco */
:focus-visible {
  outline: 3px solid var(--c-focus-ring);
  outline-offset: 2px;
  border-radius: 6px;
}
:focus:not(:focus-visible) {
  outline: none;
}


/* ==========================================================================
   2. LAYOUT, CABECERA Y FILTROS
   ========================================================================== */

.page-header {
  background: linear-gradient(135deg, #1e293b 0%, #111827 100%);
  color: #f8fafc;
  padding: var(--spacing-lg) var(--spacing-md);
  text-align: center;
  border-bottom: 4px solid var(--c-brand);
}
.page-header h1 {
  margin: 0;
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: -0.025em;
  line-height: var(--h1-line-height);
  min-height: var(--h1-min-height);
}
.page-header p {
  margin: 0.5rem auto 0;
  font-size: 1.125rem;
  color: #9ca3af;
  max-width: 65ch;
}

.main-content {
  max-width: 1400px;
  margin: var(--spacing-lg) auto;
  padding: 0 var(--spacing-md);
}

.filter-section {
  margin-bottom: var(--spacing-lg);
}

/* Estilos de filtros para PC (flex-wrap se encargará de la primera fase responsive) */
.filter-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1rem;
  background-color: var(--c-surface);
  padding: 1.5rem;
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--c-border);
}

.filter-group {
    flex: 1 1 200px; /* Crecer y encoger desde una base de 200px */
    display: flex;
    flex-direction: column;
}
.filter-group label {
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 0.5rem;
    color: var(--c-text);
}
select {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  padding: 0.75rem 2.5rem 0.75rem 1rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid var(--c-border);
  border-radius: 0.5rem;
  background-color: var(--c-surface);
  cursor: pointer;
  transition: border-color var(--transition-speed) ease, box-shadow var(--transition-speed) ease;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 0.5rem center;
  background-repeat: no-repeat;
  background-size: 1.5em 1.5em;
}
select:disabled {
  cursor: not-allowed;
  background-color: #f3f4f6;
  opacity: 0.7;
}
select:focus {
  border-color: var(--c-brand);
  box-shadow: 0 0 0 3px var(--c-focus-ring);
}

.filter-group--button {
    flex-grow: 0; /* Evita que el contenedor del botón crezca innecesariamente */
}
#sortButton {
  background-color: var(--c-brand);
  color: var(--c-surface);
  border: none;
  border-radius: 0.5rem;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  transition: background-color var(--transition-speed) ease, transform 100ms ease;
}
#sortButton:hover {
  background-color: var(--c-brand-hover);
}
#sortButton:active {
  transform: translateY(1px);
}
#sortButton svg {
  width: 1.25em;
  height: 1.25em;
}


/* ==========================================================================
   3. TARJETAS DE EXÁMENES (CSS GRID)
   ========================================================================== */

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.75rem;
}

.card {
  background-color: var(--c-surface);
  border-radius: var(--border-radius);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--c-border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-speed) ease, box-shadow var(--transition-speed) ease, border-color var(--transition-speed) ease;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}
.card:hover {
  transform: translateY(-6px);
  box-shadow: var(--shadow-lg);
  border-color: var(--c-brand);
}
.card:hover .card-image {
  transform: scale(1.05);
}

.card-image {
  width: 100%;
  aspect-ratio: 16 / 10;
  object-fit: cover;
  border-bottom: 1px solid var(--c-border);
  transition: transform var(--transition-speed) ease;
}

.card-content {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  flex-grow: 1;
}
.card-content h3 {
    font-size: 1.125rem;
    font-weight: 600;
    margin: 0 0 0.75rem;
}
.card-content p {
  font-size: 0.9rem;
  color: var(--c-text-muted);
  margin: 0 0 0.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.card-content p strong {
    font-weight: 500;
    color: var(--c-text);
}

.icon {
  width: 1.1em;
  height: 1.1em;
  flex-shrink: 0;
  color: var(--c-brand);
}


/* ==========================================================================
   4. PAGINACIÓN Y ESTADOS
   ========================================================================== */

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  margin-top: var(--spacing-lg);
}
.pagination button {
  background-color: var(--c-surface);
  color: var(--c-brand);
  border: 1px solid var(--c-border);
  border-radius: 0.5rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0.6rem 1.2rem;
  transition: background-color var(--transition-speed) ease, color var(--transition-speed) ease;
}
.pagination button:hover:not(:disabled) {
  background-color: var(--c-brand);
  color: var(--c-surface);
}
.pagination button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
#pageInfo {
  font-weight: 500;
  color: var(--c-text-muted);
  padding: 0 0.75rem;
  font-variant-numeric: tabular-nums;
}

.loader {
  width: 48px;
  height: 48px;
  border: 5px solid var(--c-border);
  border-bottom-color: var(--c-brand);
  border-radius: 50%;
  display: block;
  margin: var(--spacing-lg) auto;
  animation: rotation 1s linear infinite;
}
@keyframes rotation {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-results-message {
  text-align: center;
  font-size: 1.125rem;
  color: var(--c-text-muted);
  padding: 4rem 1rem;
  background-color: var(--c-surface);
  border: 1px dashed var(--c-border);
  border-radius: var(--border-radius);
}


/* ==========================================================================
   5. RESPONSIVIDAD
   ========================================================================== */

@media (max-width: 768px) {
  :root {
    --h1-min-height: calc(2rem * var(--h1-line-height));
  }
  .page-header h1 {
    font-size: 2rem;
  }
  .page-header p {
    font-size: 1rem;
  }
  
  /* --- FIX DEFINITIVO PARA MÓVIL --- */
  .filter-controls {
    flex-direction: column; /* Apila todos los grupos de filtros verticalmente */
    align-items: stretch; /* Estira los elementos para que ocupen el ancho completo */
  }
  .filter-group {
    flex-basis: auto; /* Resetea la base flexible para que se comporten como bloques */
  }
  #sortButton {
    width: 100%; /* Asegura que el botón ocupe todo el ancho */
    justify-content: center; /* Centra el texto y el icono del botón */
  }
}

@media (max-width: 480px) {
    .cards-grid {
        grid-template-columns: 1fr;
    }
}


/* ==========================================================================
   6. MOVIMIENTO REDUCIDO (ACCESIBILIDAD)
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}