:root {
  --dark: #1e1e24;
  --light: #f6f1e8;
  --gold: #d4a24e;
  --gold-dark: #b8873a;
  --navy: #2c3e50;
  --board-dark: #553b2e;
  --board-light: #e8d5a8;
  --green: #3d9a5f;
  --red: #c0392b;
  --text: #2b2b2b;
  --muted: #777;
}

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

/* Desactiva el "scroll anchoring" completo: evita el bucle de feedback
   cuando el encabezado sticky cambia de altura (parpadeo del título). */
html { overflow-anchor: none; }

body {
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--light);
  color: var(--text);
  line-height: 1.55;
}

.checker::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0.06;
  background-image:
    linear-gradient(45deg, var(--board-dark) 25%, transparent 25%, transparent 75%, var(--board-dark) 75%),
    linear-gradient(45deg, var(--board-dark) 25%, transparent 25%, transparent 75%, var(--board-dark) 75%);
  background-size: 46px 46px;
  background-position: 0 0, 23px 23px;
  z-index: 0;
}

/* Barrón fija: contiene el encabezado (que se encoge) y las pestañas. */
.sticky-top {
  position: sticky;
  top: 0;
  z-index: 10;
  /* Evita que el navegador "corrija" el scroll cuando el header cambia de
     altura al encogerse (eso causaba el parpadeo en el umbral). */
  overflow-anchor: none;
}

header.hero {
  position: relative;
  overflow-anchor: none;
  background: linear-gradient(135deg, var(--navy) 0%, #202a36 100%);
  color: var(--light);
  padding: 3rem 2rem;
  border-bottom: 4px solid var(--gold);
  /* Al hacer scroll, el encabezado se encoge: single línea de título. */
  transition: padding 0.25s ease, border-bottom-width 0.25s ease;
}

header.hero .inner { position: relative; z-index: 1; max-width: 980px; margin: 0 auto; }

header.hero h1 { font-size: 2rem; letter-spacing: 1px; transition: font-size 0.25s ease; }
header.hero h1 .gold { color: var(--gold); white-space: nowrap; }

/* Encogido en scroll: título chico en una sola línea, sin tagline ni sufijo. */
body.scrolled header.hero { padding: 0.6rem 1.5rem 0.55rem; border-bottom-width: 2px; }
body.scrolled header.hero h1 { font-size: 1.1rem; white-space: nowrap; }
body.scrolled header.hero h1 .section-suffix,
body.scrolled header.hero p.tagline { display: none; }

/* Piezas de ajedrez en estilo texto (sin emoji a color), nitidas, sin bold y
   un punto mas grandes para que se noten. */
header.hero h1::first-letter,
nav.tabs a::first-letter,
footer::first-letter { font-weight: 400; font-size: 1.25em; }

/* Fila de chips (categoría, modalidad, variante, puntuado) dentro de tarjetas. */
.tags-row { display: flex; flex-wrap: wrap; gap: 0.3rem; margin-bottom: 0.1rem; }
.tags-row .cat-tag { margin-bottom: 0; }

/* Tabla de detalle de un torneo: etiquetas resaltadas. */
.card .detail-table td:first-child { font-weight: 600; color: var(--navy); white-space: nowrap; }
header.hero h1 .section-suffix { color: #e8edf2; font-weight: 900; font-size: 1.65rem; letter-spacing: 0.3px; text-transform: none; }
header.hero p.tagline { margin-top: 0.4rem; font-size: 1.05rem; color: #cfd6dd; }

nav.tabs {
  background: var(--dark);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
  padding: 0.45rem 1rem;
}

.tabs-group { display: flex; gap: 0.25rem; flex-wrap: nowrap; justify-content: center; align-items: center; }

nav.tabs a {
  color: #ddd;
  text-decoration: none;
  padding: 0.4rem 0.85rem;
  border-radius: 4px;
  font-weight: 600;
  font-size: 0.9rem;
  transition: background 0.15s, color 0.15s;
}

nav.tabs a:hover, nav.tabs a.active { background: var(--gold); color: var(--dark); }

main { max-width: 980px; margin: 0 auto; padding: 2rem 1rem; }

section.block { margin-bottom: 2.5rem; }
section.block h2 {
  font-size: 1.35rem;
  color: var(--navy);
  border-bottom: 2px solid var(--gold);
  padding-bottom: 0.35rem;
  margin-bottom: 1rem;
}

.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; }
.card-grid.grid-2 { grid-template-columns: repeat(2, 1fr); }

.card {
  background: #fff;
  border: 1px solid #e2dccd;
  border-radius: 8px;
  padding: 1.1rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  /* Las tablas anchas (resultados, rankings) no escapan de la tarjeta:
     se desplazan con una barra de scroll horizontal. */
  overflow-x: auto;
}

.cat-tag {
  display: inline-block;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
  color: #fff;
  background: var(--navy);
  border-radius: 3px;
  padding: 0.15rem 0.5rem;
}
.card .cat-tag { margin-bottom: 0.5rem; }

/* Estado de un torneo: Cancelado (rojo), Desierto (celeste claro), Creado y
   En curso (púrpura, glow rojo) y Terminado (navy). */
.cat-tag.tag-cancelled { background: #c0392b; }
.cat-tag.tag-desierto { background: #E0F2FE; color: #0f3a5f; }
.cat-tag.tag-created { background: #8e44ad; animation: glow-subtle 3.2s ease-in-out infinite; }
.cat-tag.tag-live { background: #8e44ad; animation: glow-urgent 1.1s ease-in-out infinite; }
.cat-tag.tag-finished { background: var(--navy); }

@keyframes glow-subtle {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.3); }
  50% { box-shadow: 0 0 9px 3px rgba(192, 57, 43, 0.5); }
}
@keyframes glow-urgent {
  0%, 100% { box-shadow: 0 0 0 0 rgba(192, 57, 43, 0.5); }
  50% { box-shadow: 0 0 13px 5px rgba(192, 57, 43, 0.72); }
}
@media (prefers-reduced-motion: reduce) {
  .cat-tag.tag-created, .cat-tag.tag-live { animation: none; }
}

.card h3 { font-size: 1.05rem; margin-bottom: 0.4rem; }

.card p.meta { color: var(--muted); font-size: 0.9rem; margin-bottom: 0.15rem; }
.card p.meta.hora { margin-bottom: 0.4rem; }
.card .clock { font-family: 'Cascadia Mono', Consolas, monospace; color: var(--gold-dark); font-weight: 700; }
.card .desc { color: var(--navy); font-size: 0.9rem; margin-bottom: 0.5rem; }
.card .lichess-link { display: inline-block; margin-top: 0.4rem; font-weight: 600; }

.btn {
  display: inline-block;
  background: var(--gold);
  color: var(--dark);
  font-weight: 700;
  text-decoration: none;
  padding: 0.5rem 1.1rem;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
}

.btn:hover { background: var(--gold-dark); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--gold); color: var(--gold-dark); }
.btn.ghost:hover { background: var(--gold); color: var(--dark); }

table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.06); }

th, td { padding: 0.6rem 0.8rem; text-align: left; font-size: 0.92rem; white-space: nowrap; }
thead th {
  background: var(--navy);
  color: #fff;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

tbody tr:nth-child(even) { background: #faf7f0; }
tbody tr:hover { background: #f1ead9; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.podium { display: flex; gap: 1rem; flex-wrap: wrap; }
.podium .slot { background: #fff; border-radius: 8px; padding: 1rem; text-align: center; border: 1px solid #e2dccd; min-width: 160px; flex: 1; }
.podium .place { font-size: 1.6rem; font-weight: 800; }
.podium .place[data-rank='1'] { color: var(--gold-dark); }
.podium .place[data-rank='2'] { color: #8a8f98; }
.podium .place[data-rank='3'] { color: #b36540; }
.podium .user { font-weight: 700; margin-top: 0.3rem; }
.podium .score { color: var(--muted); font-size: 0.85rem; }

/* En celular el podio ordena jerárquicamente: 1º grande en su propia fila y
   2º/3º debajo, en vez de 1º y 2º juntos con 3º en la fila siguiente. */
@media (max-width: 640px) {
  .podium { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; }
  .podium .slot { min-width: 0; }
  .podium .slot[data-rank='1'] { grid-column: 1 / -1; }
  .podium .slot[data-rank='1'] .place { font-size: 2.1rem; }
}

.filters { display: flex; gap: 0.8rem; flex-wrap: wrap; align-items: center; margin-bottom: 1.2rem; }
.filters select, .filters input, .filters button {
  padding: 0.5rem 0.8rem; border-radius: 6px; border: 1px solid #ccc; font-size: 0.92rem; background: #fff;
}
.filters select:focus, .filters input:focus { outline: 2px solid var(--gold); }

/* Ranking: la tabla ancha se desplaza dentro del sitio (no rompe el layout). */
#table-wrap { overflow-x: auto; }

/* Embeds de Lichess (puzzle del día y TV): el mismo tamaño en ambos.
   La proporción oficial (400x444) es el tablero cuadrado más las filas de
   texto/los jugadores arriba y abajo; evita las franjas negras por huecos. */
.embed-frame {
  width: 400px;
  max-width: 100%;
  aspect-ratio: 9 / 10;
  display: block;
  border: 0;
  background: #fff;
}

/* Tablero en vivo (stream de Lichess): 8x8 renderizado con posición FEN.
   Ancho igual a los embeds (400px) para que queden alineados. */
#tv-board {
  width: 400px;
  max-width: 100%;
  aspect-ratio: 1/1;
  display: flex;
  flex-wrap: wrap;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}
#tv-board[hidden], iframe[hidden] { display: none !important; }
.liveboard-cell {
  width: 12.5%;
  height: 12.5%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(24px, 9vmin, 46px);
  line-height: 1;
  user-select: none;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.liveboard-cell.light { background: #ebedd0; }
.liveboard-cell.dark { background: #739552; }
.liveboard-cell.w { color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35); }
.liveboard-cell.b { color: #1c1c1c; text-shadow: 0 1px 1px rgba(255, 255, 255, 0.15); }
.liveboard-cell.last { box-shadow: inset 0 0 0 3px rgba(247, 213, 30, 0.85); }

.pager { display: flex; gap: 0.5rem; align-items: center; }

.badge { display: inline-block; padding: 0.15rem 0.55rem; border-radius: 3px; font-size: 0.75rem; font-weight: 700; color: #fff; }
.badge.finished { background: var(--navy); }
.badge.started { background: var(--green); }
.badge.pending { background: var(--gold-dark); }
.badge.cancelled { background: #c0392b; }
.badge.desierto { background: #E0F2FE; color: #0f3a5f; }

a.userlink { color: var(--navy); font-weight: 600; text-decoration: none; border-bottom: 1px dashed var(--gold); }
a.userlink:hover { color: var(--gold-dark); }

.verified { color: var(--green); }
.empty { color: var(--muted); font-style: italic; padding: 1rem 0; }
.hint { display: block; font-size: 0.78rem; color: var(--muted); margin-top: 0.25rem; }

footer { margin-top: 3rem; padding: 1.5rem 2rem; text-align: center; color: var(--muted); font-size: 0.85rem; border-top: 1px solid #e2dccd; }

/* Forms de admin: cada campo (label + control) ocupa una sola fila */
/* El atributo hidden debe ocultar de verdad aunque el campo use display:grid. */
[hidden] { display: none !important; }

#frm, #frm-edit, #frm-login { max-width: 680px; }

#frm label, #frm-edit label, #frm-login label {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) 2fr;
  gap: 0.6rem 1rem;
  align-items: center;
  margin-bottom: 0.9rem;
}

#frm label > input, #frm label > select, #frm label > textarea,
#frm-edit label > input, #frm-edit label > select, #frm-edit label > textarea,
#frm-login label > input {
  width: 100%;
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  border: 1px solid #ccc;
  font-size: 0.92rem;
  background: #fff;
  font-family: inherit;
}

/* Selector multi-día: el campo usa la misma grilla que los labels del form. */
#frm .field, #frm-edit .field {
  display: grid;
  grid-template-columns: minmax(170px, 1fr) 2fr;
  gap: 0.6rem 1rem;
  align-items: center;
  margin-bottom: 0.9rem;
}
.day-pills { display: flex; flex-wrap: wrap; gap: 0.4rem; }
#frm .day-pills label, #frm-edit .day-pills label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin: 0;
  background: #f1ead9;
  border: 1px solid #e2dccd;
  border-radius: 16px;
  padding: 0.25rem 0.6rem;
  font-size: 0.85rem;
  cursor: pointer;
}
#frm .day-pills input, #frm-edit .day-pills input { width: auto; }
.day-pills label:has(input:checked) { background: var(--gold); border-color: var(--gold-dark); font-weight: 600; }
.btn.days-all { padding: 0.15rem 0.6rem; font-size: 0.75rem; }

#frm label > input:focus, #frm label > select:focus, #frm label > textarea:focus,
#frm-edit label > input:focus, #frm-edit label > select:focus, #frm-edit label > textarea:focus,
#frm-login label > input:focus { outline: 2px solid var(--gold); }

#frm label.check, #frm-edit label.check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
#frm label.check input, #frm-edit label.check input { width: auto; }

#frm .row, #frm-edit .row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem;
}
#frm .row label, #frm-edit .row label { grid-template-columns: 1fr; }

#frm .result, #frm-edit .result { margin-top: 0.75rem; }
#frm-edit textarea, #frm textarea { resize: vertical; }

@media (max-width: 640px) {
  header.hero h1 { font-size: 1.5rem; }
  header.hero h1 .section-suffix { font-size: 1.5rem; font-weight: 500; line-height: 1.2; }
  body.scrolled header.hero h1 { font-size: 0.95rem; }
  nav.tabs { padding: 0.4rem 0.35rem; }
  nav.tabs a { font-size: 0.82rem; padding: 0.35rem 0.6rem; }
  th, td { padding: 0.45rem 0.5rem; }
  .card-grid.grid-2 { grid-template-columns: 1fr; }
}