/* =============================================
   VARIABLES Y ESTILOS BASE
   ============================================= */
:root {
  /* Tipografía */
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "DM Sans", system-ui, sans-serif;

  /* Paleta Principal */
  --color-bg-page: #f0f2f5;
  --color-bg-container: #ffffff;
  --color-bg-light: #ffffff;
  --color-text-primary: #1c1e21;
  --color-text-secondary: #606770;
  --color-accent: #0866ff;
  --color-accent-dark: #0058e6;

  /* Colores UI */
  --color-border: #ccd0d5;
  --color-border-light: #e4e6eb;
  --color-bg-hover: #f2f3f5;
  --color-bg-disabled: #e4e6eb;
  --color-text-disabled: #bcc0c4;
  --color-shadow: rgba(0, 0, 0, 0.1);
  --color-shadow-focus: rgba(8, 102, 255, 0.2);

  /* Colores Estado Error */
  --color-error-text: #c52a1a;
  --color-error-bg: #ffebe8;
  --color-error-border: #c52a1a;

  /* Paleta Heatmap (específica de Seriómetro) */
  --color-rating-low: #CB644E;
  --color-rating-mid: #FEE09F;
  --color-rating-high: #27ae60;

  /* Colores UI específicos */
  --color-switch-inactive: #ccd0d5;
  --color-bg-no-rating: #f5f5f5;
}

/* =============================================
   MODO OSCURO
   ============================================= */
html[data-theme="dark"] {
  color-scheme: dark;
  --color-bg-page: #18191a;
  --color-bg-container: #242526;
  --color-bg-light: #3a3b3c;
  --color-bg-no-rating: #1e1e1e;
  --color-text-primary: #e4e6eb;
  --color-text-secondary: #b0b3b8;
  --color-accent: #4e9bff;
  --color-accent-dark: #3d8ae0;
  --color-border: #3a3b3c;
  --color-border-light: #3a3b3c;
  --color-bg-hover: #3a3b3c;
  --color-bg-disabled: #3a3b3c;
  --color-text-disabled: #606770;
  --color-shadow: rgba(0, 0, 0, 0.4);
  --color-shadow-focus: rgba(78, 155, 255, 0.2);
  --color-error-text: #ff6b6b;
  --color-error-bg: #3a1a1a;
  --color-error-border: #c52a1a;
  --color-switch-inactive: #555;
  --color-bg-no-rating: #2a2a2a;
}

html {
  box-sizing: border-box;
  color-scheme: light;
}

*, *:before, *:after {
  box-sizing: inherit;
}

button, input, select, textarea {
  font-family: inherit;
}

body {
    font-family: var(--font-body);
    line-height: 1.6;
    margin: 0;
    background-color: var(--color-bg-page);
    color: var(--color-text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
    transition: background-color 0.2s, color 0.2s;
}

.page-wrapper {
    display: flex;
    justify-content: center;
    width: 100%;
    padding: 20px;
    flex-grow: 1;
}

.app-container {
    background: var(--color-bg-container);
    width: 100%;
    max-width: 1200px;
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 10px 35px var(--color-shadow);
    margin: 20px 0;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* =============================================
   CABECERA
   ============================================= */
.app-header {
  position: relative;
  text-align: center;
  margin-bottom: 20px;
}

h1 {
    font-family: var(--font-display);
    font-style: italic;
    text-align: center;
    margin-top: 0;
    margin-bottom: 12px;
    font-size: 2.6em;
    font-weight: 700;
    text-wrap: balance;
}

.app-tagline {
    text-align: center;
    color: var(--color-text-secondary);
    max-width: 60ch;
    margin: 0 auto 30px;
    text-wrap: balance;
}

.results-title {
    text-align: center;
    margin-top: 20px;
    margin-bottom: 20px;
    font-size: 1.5em;
    font-weight: 600;
}

/* =============================================
   MENÚ DE AJUSTES (tema + idioma)
   ============================================= */
.settings-wrapper {
  position: absolute;
  top: 0;
  right: 0;
}

.theme-toggle-btn {
  background-color: transparent;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  width: 44px;
  height: 44px;
  cursor: pointer;
  font-size: 1.1em;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s, border-color 0.2s;
}

.theme-toggle-btn:hover {
  background: var(--color-bg-hover);
  border-color: var(--color-accent);
}

.settings-menu {
  position: absolute;
  top: 52px;
  right: 0;
  z-index: 30;
  min-width: 230px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: left;
  background: var(--color-bg-container);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: 0 8px 24px var(--color-shadow);
}

.settings-menu[hidden] { display: none; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.settings-label {
  font-weight: 600;
  font-size: 0.9em;
}

.settings-options { display: flex; gap: 6px; }

.settings-pill {
  min-height: 44px;
  min-width: 44px;
  padding: 6px 12px;
  border: 1px solid var(--color-border);
  border-radius: 999px;
  background: var(--color-bg-container);
  color: var(--color-text-primary);
  font-size: 0.9em;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.2s, border-color 0.2s, color 0.2s;
}

.settings-pill:hover { background: var(--color-bg-hover); }

.settings-pill.active {
  background: var(--color-accent);
  border-color: var(--color-accent);
  color: #ffffff;
}

/* =============================================
   SECCIÓN DE BÚSQUEDA
   ============================================= */
.search-section {
    display: flex;
    justify-content: center;
    margin-bottom: 35px;
}

.search-container {
    display: flex;
    width: 100%;
    max-width: 500px;
}

#seriesNameInput {
    padding: 12px 15px;
    font-size: 1em;
    font-family: var(--font-body);
    border: 1px solid var(--color-border);
    border-radius: 6px 0 0 6px;
    width: 70%;
    background: var(--color-bg-container);
    color: var(--color-text-primary);
    transition: border-color 0.2s, box-shadow 0.2s;
}
#seriesNameInput:focus {
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px var(--color-shadow-focus);
    outline: none;
}

#searchButton {
    padding: 12px 20px;
    font-size: 1em;
    font-family: var(--font-body);
    background-color: var(--color-accent);
    border: none;
    border-radius: 0 6px 6px 0;
    cursor: pointer;
    font-weight: 600;
    color: #ffffff;
    transition: background-color 0.2s;
}

#searchButton:hover {
    background-color: var(--color-accent-dark);
}

/* =============================================
   CONTROLES DE TABLA
   ============================================= */
#tableControlsContainer { display: flex; justify-content: center; align-items: flex-start; gap: 50px; margin-bottom: 25px; padding: 10px 0; flex-wrap: wrap; }
.control-wrapper { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.main-control-label { font-size: .9em; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.control-group { display: flex; align-items: center; gap: 15px; }
.control-label { font-size: 1.1em; font-weight: 500; cursor: default; transition: color .2s ease-in-out; color: var(--color-text-secondary); }
.control-label.active { font-weight: 700; color: var(--color-text-primary); }
.switch { position: relative; display: inline-flex; align-items: center; width: 50px; height: 44px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; left: 0; right: 0; top: 50%; transform: translateY(-50%); height: 24px; background-color: var(--color-switch-inactive); transition: .4s; }
.slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 4px; bottom: 4px; background-color: #ffffff; transition: .4s; }
.switch input:checked + .slider { background-color: var(--color-accent); }
.switch input:checked + .slider:before { transform: translateX(26px); }
.slider.round { border-radius: 24px; }
.slider.round:before { border-radius: 50%; }

/* =============================================
   LISTA DE RESULTADOS DE BÚSQUEDA
   ============================================= */
.search-results-list { list-style: none; padding: 0; margin: 0; }
.search-results-list li { padding: 12px 15px; border: 1px solid var(--color-border); background-color: var(--color-bg-light); margin-bottom: 10px; border-radius: 6px; cursor: pointer; transition: background-color 0.2s; }
.search-results-list li:hover { background-color: var(--color-bg-hover); }

/* =============================================
   INFO DE SERIE
   ============================================= */
.series-info-full {
    display: flex;
    align-items: flex-start;
    gap: 25px;
    margin-bottom: 30px;
    padding: 25px;
    border-radius: 6px;
    background-color: var(--color-bg-light);
    border: 1px solid var(--color-border);
    position: relative;
    overflow: hidden;
    transition: max-height 0.5s ease-in-out, background-color 0.2s, border-color 0.2s;
    max-height: 5000px;
}

.series-info-full img.series-poster,
.series-info-full .series-details-text {
    opacity: 1;
    transition: max-height 0.3s 0.2s, max-width 0.3s 0.2s, opacity 0.3s 0.2s, margin 0.3s 0.2s, padding 0.3s 0.2s;
}

.expand-prompt {
    position: absolute;
    left: 25px;
    right: 50px;
    top: 15px;
    height: 24px;
    display: flex;
    align-items: center;
    margin: 0;
    font-weight: 500;
    color: var(--color-text-secondary);
    opacity: 0;
    transition: opacity 0.3s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toggle-details-btn {
    position: absolute;
    top: 5px;
    right: 5px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    z-index: 5;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.4s ease-in-out;
}
.toggle-details-btn img {
    width: 24px;
    height: 24px;
    transition: filter 0.2s;
}

html[data-theme="dark"] .toggle-details-btn img {
    filter: brightness(0) invert(1);
}

.series-info-full.collapsed {
    max-height: 54px;
}
.series-info-full.collapsed .expand-prompt {
    opacity: 1;
    transition-delay: 0.2s;
}
.series-info-full.collapsed img.series-poster,
.series-info-full.collapsed .series-details-text {
    max-height: 0;
    max-width: 0;
    opacity: 0;
    padding: 0;
    margin: 0;
    border: 0;
    transition: max-height 0.3s, max-width 0.3s, opacity 0.2s, margin 0.3s, padding 0.3s;
}
.series-info-full.collapsed .toggle-details-btn {
    transform: rotate(180deg);
}

/* =============================================
   TABLA DE CALIFICACIONES
   ============================================= */
#ratingsTableContainer { overflow-x: auto; display: flex; }
#ratingsTableContainer > .ratings-table { flex-shrink: 0; }
@media (max-width: 768px) {
  /* Sombra solo en modo normal: en compacto la tabla cabe entera y la sombra
     taparía la última columna. :has() excluye la tabla compacta. */
  #ratingsTableContainer:has(.ratings-table:not(.compact))::after {
    content: "";
    position: sticky;
    top: 0;
    right: 0;
    height: 100%;
    width: 24px;
    margin-left: -24px;
    flex-shrink: 0;
    background: linear-gradient(to right, transparent, var(--color-shadow));
    pointer-events: none;
    z-index: 25;
  }
}
.ratings-table { border-collapse: collapse; margin: 0 auto; width: 100%; table-layout: fixed; }
/* En modo normal la tabla nunca se comprime por debajo de sus anchos de columna
   (52px etiquetas + 100px por temporada): crece y el contenedor hace scroll
   horizontal. --season-count lo setea buildRatingsTable en script.js.
   NO usar min-width: max-content — con table-layout: fixed Chrome lo computa
   como un ancho gigante. En compacto sí se permite encoger. */
.ratings-table:not(.compact) { min-width: calc(52px + var(--season-count, 1) * 100px); }
.ratings-table th, .ratings-table td { border: 1px solid var(--color-border); text-align: center; padding: 6px 4px; vertical-align: middle; position: relative; }
.ratings-table th.table-main-header, .ratings-table td.chapter-label { width: 52px; }
.ratings-table th.season-header { width: 100px; }
.ratings-table th { background-color: var(--color-bg-hover); }
.ratings-table th.table-main-header, .ratings-table td.chapter-label { background-color: var(--color-bg-hover); position: sticky; left: 0; z-index: 10; }
.ratings-table th.table-main-header { z-index: 20; }
.ratings-table.compact th.season-header { writing-mode: vertical-rl; text-orientation: mixed; }
.ratings-table td.chapter-label { font-weight: 600; }
.rating-cell-content { display: flex; flex-direction: column; justify-content: center; align-items: center; }
.rating-value { font-size: 1.3em; font-weight: bold; color: rgba(0, 0, 0, 0.8); }
.episode-name-display { font-size: .65em; max-height: 4.4em; overflow: hidden; display: none; margin-top: 2px; color: rgba(0, 0, 0, 0.75); overflow-wrap: break-word; }
.ratings-table.show-episode-names .episode-name-display { display: block; }
.ratings-table td.no-rating { background-color: var(--color-bg-no-rating); }

html[data-theme="dark"] .ratings-table td.no-rating .rating-value,
html[data-theme="dark"] .ratings-table td.no-rating .episode-name-display {
  color: rgba(255, 255, 255, 0.5);
}

/* Modo compacto — solo color, sin texto. Las columnas de temporada NO llevan
   width fijo: se reparten el ancho disponible para que la tabla completa
   quepa en pantalla sin scroll (es la gracia del modo compacto). */
.ratings-table.compact th.table-main-header,
.ratings-table.compact td.chapter-label { width: 28px; }
/* height explícito: Safari/WebKit no computa la altura de fila a partir de
   texto vertical (writing-mode) — sin esto la cabecera colapsa y "S01" se recorta */
.ratings-table.compact th.season-header { width: auto; height: 34px; }
.ratings-table.compact th.season-header,
.ratings-table.compact th.table-main-header,
.ratings-table.compact td.chapter-label { font-size: 0.6em; padding: 2px 1px; }
.ratings-table.compact td.rating-cell { padding: 0; }
.ratings-table.compact .rating-cell-content { height: 26px; }
.ratings-table.compact .rating-value,
.ratings-table.compact .episode-name-display { display: none; }

/* =============================================
   MENSAJES (LOADING, ERROR)
   ============================================= */
#loading { text-align: center; margin: 20px; font-weight: bold; color: var(--color-text-secondary); }
#error { text-align: center; margin: 20px; font-weight: bold; color: var(--color-error-text); }

/* =============================================
   FOOTER
   ============================================= */
.site-footer { text-align: center; padding: 20px 0; margin-top: 20px; font-size: 0.9em; color: var(--color-text-secondary); font-weight: 500; flex-shrink: 0; }

/* =============================================
   TOP EPISODIOS
   ============================================= */
.top-episodes-section {
    margin-top: 40px;
    padding: 25px;
    background-color: var(--color-bg-light);
    border-radius: 6px;
    border: 1px solid var(--color-border);
    transition: background-color 0.2s, border-color 0.2s;
}

.top-episodes-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
}

.top-episodes-table th,
.top-episodes-table td {
    padding: 12px 15px;
    border: 1px solid var(--color-border);
    text-align: center;
}

.top-episodes-table thead th {
    background-color: var(--color-bg-hover);
    font-weight: 600;
}

.top-episodes-table tbody tr:nth-of-type(even) {
    background-color: var(--color-bg-no-rating);
}

/* Clases en vez de [data-label="..."]: el data-label ahora está traducido */
.top-episodes-table td.top-col-rating,
.top-episodes-table td.top-col-rank {
    font-weight: bold;
    text-align: center;
}

/* =============================================
   TOOLTIP PERSONALIZADO (SOLO ESCRITORIO)
   ============================================= */
.custom-tooltip {
    position: fixed;
    z-index: 1000;
    background-color: var(--color-bg-light);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
    box-shadow: 0 4px 16px var(--color-shadow);
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.85em;
    max-width: 400px;
    text-align: left;
    white-space: pre-wrap;
    display: none;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
}

/* =============================================
   MODAL DE EPISODIO (MÓVIL)
   ============================================= */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    padding: 15px;
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}
.modal-content {
    background: var(--color-bg-light);
    color: var(--color-text-primary);
    padding: 25px;
    border-radius: 8px;
    max-width: 500px;
    width: 100%;
    position: relative;
    box-shadow: 0 5px 20px var(--color-shadow);
    transform: scale(0.9);
    transition: transform 0.3s ease-in-out, background-color 0.2s;
}
#modalEpisodeDetails h4 {
    margin-top: 0;
    font-size: 1.4em;
    color: var(--color-text-primary);
}
#modalEpisodeDetails p {
    margin-bottom: 10px;
    line-height: 1.5;
}
#modalEpisodeDetails .modal-synopsis {
    margin-top: 15px;
    font-size: 0.95em;
    color: var(--color-text-secondary);
}

.modal-close-btn {
    position: absolute;
    top: 10px;
    right: 15px;
    background: transparent;
    border: none;
    font-size: 2em;
    line-height: 1;
    color: var(--color-text-secondary);
    cursor: pointer;
}

/* =============================================
   RESPONSIVIDAD
   ============================================= */
@media (max-width: 768px) {
    .page-wrapper { padding: 0 15px; }
    .app-container { padding: 20px; }
    .series-info-full { flex-direction: column; align-items: center; text-align: center; padding: 20px; }
    .series-info-full.collapsed {
        padding-top: 15px;
        padding-bottom: 15px;
    }
    .series-info-full.collapsed .expand-prompt { left: 10%; transform: translateX(-10%); top: 15px; }
    .series-info-full.collapsed .series-poster { max-height: 0; margin-left: 0; margin-bottom: -25px; }
    .series-info-full img.series-poster { width: 160px; height: auto; max-width: 160px; max-height: 240px; }
    #searchResultsListContainer { width: 90%; }

    .top-episodes-section { padding: 15px; }
    .top-episodes-table { font-size: .8em; }
    .top-episodes-table thead { display: none; }
    .top-episodes-table, .top-episodes-table tbody, .top-episodes-table tr, .top-episodes-table td {
        display: block;
        width: 100%;
    }
    .top-episodes-table tr {
        margin-bottom: 15px;
        border: 1px solid var(--color-border);
        border-radius: 4px;
        overflow: hidden;
    }
    .top-episodes-table td {
        text-align: right;
        padding-left: 50%;
        position: relative;
        border: none;
        border-bottom: 1px solid var(--color-bg-container);
    }
    .top-episodes-table td:before {
        content: attr(data-label);
        position: absolute;
        left: 10px;
        width: calc(50% - 20px);
        padding-right: 10px;
        white-space: nowrap;
        text-align: left;
        font-weight: bold;
    }
    .top-episodes-table td:last-child { border-bottom: 0; }
    .top-episodes-table td.top-col-rating,
    .top-episodes-table td.top-col-rank { text-align: right; }
}

@media (max-width: 576px) {
    h1 { font-size: 1.9em; }
    .search-container { flex-direction: column; }
    #seriesNameInput, #searchButton { width: 100%; border-radius: 6px; }
    #seriesNameInput { margin-bottom: 10px; }
    #tableControlsContainer { gap: 20px; }
    .series-info-full img.series-poster { width: 140px; max-width: 140px; }
    #searchResultsListContainer { width: 95%; margin: 20px auto; }
    .search-results-list li { padding: 10px; font-size: .9em; }
}

/* =============================================
   MOVIMIENTO REDUCIDO
   ============================================= */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
