:root {
  --bg: #f4f4f6;
  --panel: #ffffff;
  --text: #1c1c22;
  --muted: #6b6b78;
  --line: #e2e2e8;
  --accent: #e60012;
  --good: #0a8a4a;
  --radius: 10px;
  --band: 26px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15151a;
    --panel: #1f1f26;
    --text: #ececf1;
    --muted: #9a9aa8;
    --line: #2e2e38;
    --good: #3ccf86;
  }
}

* { box-sizing: border-box; }
/* Reglas como `#view-games { display: grid }` pisan el display:none del atributo hidden. */
[hidden] { display: none !important; }
body { margin: 0; font: 14px/1.4 system-ui, "Segoe UI", sans-serif; background: var(--bg); color: var(--text); }
button, input, select { font: inherit; color: inherit; }
button, .btn {
  background: var(--accent); color: #fff; border: 0; border-radius: 6px; padding: 7px 12px;
  cursor: pointer; text-decoration: none; display: inline-block;
}
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button.small { padding: 3px 8px; font-size: 12px; }
input, select { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; width: 100%; }
.muted { color: var(--muted); }
.pill { background: var(--accent); color: #fff; border-radius: 999px; padding: 0 6px; font-size: 11px; }
.pill:empty { display: none; }

.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 24px;
  padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.topbar h1 { margin: 0; font-size: 18px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.topbar h1 .logo { width: 28px; height: 28px; display: block; }
.topbar h1 .accent { color: var(--accent); }
.topbar h1 .store { font-weight: 500; color: var(--muted); }
.topbar nav { display: flex; gap: 6px; }
.topbar nav button { background: transparent; color: var(--muted); }
.topbar nav button.active { color: var(--text); background: var(--bg); }
.status { margin-left: auto; display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 12px; }

#view-games { display: grid; grid-template-columns: 250px 1fr; gap: 20px; padding: 20px; }
.filters {
  position: sticky; top: 70px; align-self: start;
  max-height: calc(100vh - 90px); overflow-y: auto; overscroll-behavior: contain; padding-right: 4px;
}
.filters form { display: flex; flex-direction: column; gap: 12px; }
.filters-head, .filters-backdrop, .filters-btn, .filters-footer .apply { display: none; }
.filters-footer { display: flex; gap: 8px; }
.filters-footer button { flex: 1; }
.filters label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.filters label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 13px; }
.filters label.check input { width: auto; }
.filters fieldset { border: 0; padding: 0; margin: 0; }
.filters legend { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.row { display: flex; gap: 6px; }

.results-head { margin-bottom: 10px; color: var(--muted); }
.toolbar { display: flex; gap: 8px; margin-bottom: 10px; }
.toolbar input[type=search] { flex: 1 1 auto; min-width: 0; }
.toolbar select { flex: 0 0 auto; width: auto; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 14px; }
.empty { grid-column: 1 / -1; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; display: flex; flex-direction: column;
}
.card.is-hidden { opacity: .55; }
/* Igual que la tienda: la banda Switch 2 sobresale arriba y el resto de las tarjetas se alinean debajo. */
.card:not(.ns2) { margin-top: var(--band); }
.card.ns2 { border-color: var(--accent); }
.ns2-band {
  height: var(--band); background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
}
.ns2-icon { height: 15px; width: auto; }
.card .cover { padding: 0; background: #fff; border-radius: 0; aspect-ratio: 16 / 9; position: relative; }
.flags { display: flex; gap: 4px; flex-wrap: wrap; }
.flag { border-radius: 999px; padding: 1px 8px; font-size: 11px; font-weight: 600; color: #fff; }
.flag.new { background: var(--accent); }
.flag.pre, .flag.soon { background: #6b6b78; }
.flag.demo { background: #4b5bd6; }
.flag.nolang { background: #c26a00; }
.languages { font-size: 13px; color: var(--muted); }
.metascore {
  display: inline-flex; align-items: center; justify-content: center; min-width: 30px; height: 26px;
  padding: 0 4px; border-radius: 6px; font-weight: 700; font-size: 14px; color: #fff;
}
.cover .metascore { position: absolute; right: 6px; bottom: 6px; box-shadow: 0 1px 3px rgb(0 0 0 / .4); }
.metascore.high { background: #00a03e; }
.metascore.mid { background: #e8b600; color: #1c1c22; }
.metascore.low { background: #d6261f; }
.metascore.big { min-width: 48px; height: 48px; font-size: 22px; border-radius: 8px; }
.scores { display: flex; align-items: center; gap: 12px; margin: 12px 0; }
.scores a, a.ext { color: var(--accent); font-size: 13px; }
.card .cover img, .noimg { width: 100%; height: 100%; object-fit: cover; display: block; }
.card .body { padding: 10px; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.card h3 { margin: 0; font-size: 14px; cursor: pointer; }
.card h3:hover { color: var(--accent); }
.sub { color: var(--muted); font-size: 12px; }
.price { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; margin-top: auto; }
.price s { color: var(--muted); font-size: 12px; }
.price strong { font-size: 16px; }
.price .tax { color: var(--muted); font-size: 11px; }
.breakdown { margin: 10px 0; border-collapse: collapse; font-size: 13px; }
.breakdown td, .breakdown th { padding: 2px 16px 2px 0; text-align: left; }
.breakdown td:last-child, .breakdown th:last-child { text-align: right; }
.breakdown th { border-top: 1px solid var(--line); }
.badge { background: var(--accent); color: #fff; border-radius: 4px; padding: 1px 5px; font-size: 12px; font-weight: 600; }
.meta { display: flex; gap: 4px; flex-wrap: wrap; }
.tag { border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-size: 11px; color: var(--muted); }
.tag.good { color: var(--good); border-color: currentColor; }
.hidden-by { font-size: 11px; color: var(--accent); }
.card .actions { display: flex; gap: 6px; padding: 0 10px 10px; }

.pager { display: flex; justify-content: center; align-items: center; gap: 14px; margin: 24px 0; }

dialog { border: 0; border-radius: var(--radius); padding: 0; background: var(--panel); color: var(--text); width: min(860px, 94vw); }
.viewer { aspect-ratio: 16 / 9; background: #000; border-radius: 6px; overflow: hidden; }
.viewer img, .viewer video { width: 100%; height: 100%; object-fit: contain; display: block; border-radius: 0; }
.thumbs { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 4px; }
.thumb {
  flex: 0 0 auto; width: 112px; aspect-ratio: 16 / 9; padding: 0; background: #000; border-radius: 4px;
  overflow: hidden; border: 2px solid transparent; position: relative; opacity: .7;
}
.thumb.active { border-color: var(--accent); opacity: 1; }
.thumb:hover { opacity: 1; }
.thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 0; }
.thumb.video::after {
  content: '▶'; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 20px; text-shadow: 0 1px 4px rgb(0 0 0 / .8);
}
/* En las tarjetas el editor se ve como texto y se marca como link al pasar el mouse. */
button.link.sub-link { color: inherit; text-decoration: none; }
button.link.sub-link:hover { color: var(--accent); text-decoration: underline; }
button.link {
  background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline;
  text-underline-offset: 2px; cursor: pointer; font: inherit;
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
.detail { padding: 20px; position: relative; }
.detail img { width: 100%; border-radius: 6px; }
.detail h2 { margin: 12px 0 4px; }
.detail .close { position: absolute; top: 26px; right: 26px; background: var(--panel); }
.detail-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 16px; }
.chart { width: 100%; height: 180px; background: var(--bg); border-radius: 6px; }
.chart path { fill: none; stroke-width: 2; }
.chart .regular { stroke: var(--muted); stroke-dasharray: 4 3; }
.chart .effective { stroke: var(--accent); }
.chart text { fill: var(--muted); font-size: 11px; }

.bl-wrap { max-width: 860px; margin: 0 auto; padding: 20px; }
.bl-form { display: grid; grid-template-columns: 180px 1fr 1fr auto; gap: 8px; margin: 16px 0; }
.bl-table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; }
.bl-table th, .bl-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); opacity: 0;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 6px; transition: .2s;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 760px) {
  /* Header compacto en una línea; el estado detallado no entra en celular. */
  /* Como administrador hay más botones: si no entran, el header pasa a dos líneas. */
  .topbar { position: static; gap: 6px 10px; padding: 8px 16px; flex-wrap: wrap; }
  .status { margin-left: auto; }
  .topbar h1 { font-size: 16px; gap: 6px; }
  .topbar h1 .logo { width: 24px; height: 24px; }
  .topbar nav button { padding: 6px 8px; }
  #status-text { display: none; }
  .status { gap: 6px; }
  .status button { padding: 6px 10px; font-size: 13px; }

  #view-games { grid-template-columns: 1fr; gap: 0; padding: 0 16px 16px; }

  /* Buscador, orden y botón de filtros quedan fijos arriba al hacer scroll. */
  .toolbar {
    position: sticky; top: 0; z-index: 6; margin: 0 -16px 8px; padding: 8px 16px;
    background: var(--bg); border-bottom: 1px solid var(--line); flex-wrap: wrap;
  }
  .toolbar input[type=search] { flex-basis: 100%; }
  .toolbar select { flex: 1 1 auto; min-width: 0; }
  .filters-btn { display: inline-flex; align-items: center; gap: 6px; flex: 0 0 auto; }

  /* Filtros como panel lateral desplegable. */
  .filters {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 30; width: min(340px, 88vw);
    /* align-self:start (escritorio) también aplica a un fixed: sin esto toma la altura del contenido. */
    align-self: stretch; height: 100vh; height: 100dvh;
    max-height: none; padding: 0; background: var(--panel); box-shadow: 0 0 24px rgb(0 0 0 / .25);
    transform: translateX(-105%); transition: transform .2s ease; display: flex; flex-direction: column;
  }
  body.filters-open .filters { transform: none; }
  .filters-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; border-bottom: 1px solid var(--line);
  }
  .filters-head h2 { margin: 0; font-size: 17px; }
  .filters form { flex: 1; overflow-y: auto; padding: 14px 16px 0; overscroll-behavior: contain; }
  .filters-footer {
    position: sticky; bottom: 0; margin: auto -16px 0; padding: 12px 16px;
    background: var(--panel); border-top: 1px solid var(--line);
  }
  .filters-footer .apply { display: block; }
  .filters-backdrop {
    display: block; position: fixed; inset: 0; z-index: 29; background: rgb(0 0 0 / .45);
    opacity: 0; pointer-events: none; transition: opacity .2s;
  }
  body.filters-open .filters-backdrop { opacity: 1; pointer-events: auto; }
  body.filters-open { overflow: hidden; }

  /* Inputs a 16px: evita el zoom automático de iOS al enfocar. */
  input, select { font-size: 16px; }

  .bl-form { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  .filters, .filters-backdrop { transition: none; }
}

/* Modo lectura: los visitantes no ven acciones de administrador. */
body.readonly .admin-only { display: none !important; }
#login { width: min(360px, 94vw); }
.login { display: flex; flex-direction: column; gap: 10px; padding: 20px; }
.login h2 { margin: 0 0 6px; font-size: 18px; }
.login .row { justify-content: flex-end; }
.error { color: var(--accent); margin: 0; font-size: 13px; }

/* Indicadores de carga */
#progress {
  position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 20; overflow: hidden;
  opacity: 0; transition: opacity .2s;
}
body.busy #progress { opacity: 1; }
#progress::after {
  content: ''; position: absolute; top: 0; bottom: 0; width: 35%; background: var(--accent);
  animation: progress 1.1s ease-in-out infinite;
}
@keyframes progress { from { left: -35%; } to { left: 100%; } }
.grid { transition: opacity .15s; }
.grid.loading { opacity: .45; pointer-events: none; }
#total.loading::after {
  content: ''; display: inline-block; width: 12px; height: 12px; margin-left: 8px; vertical-align: -1px;
  border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  animation: spin .7s linear infinite;
}
.detail-loading { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 260px; gap: 12px; }
.spinner {
  width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  #progress::after, .spinner, #total.loading::after { animation-duration: 3s; }
}
