:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --accent: #e50914;
  --border: #2a2f3a;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------- Barra superior ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  background: rgba(15, 17, 21, .95);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(6px);
}
.brand { font-weight: 800; font-size: 18px; letter-spacing: .5px; }
.brand span { color: var(--accent); margin-left: 2px; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  display: inline-flex; align-items: center; gap: 6px;
  background: none; border: 0; padding: 6px 12px; border-radius: 999px; color: var(--muted);
}
.tab-icon { display: none; width: 18px; height: 18px; fill: currentColor; flex-shrink: 0; }
.cat-select { display: none; } /* solo en el teléfono */
.tabs button.active { background: var(--panel-2); color: var(--text); }
#search, #sort, #cat-filter {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 7px 10px;
}
#search { flex: 1; min-width: 160px; max-width: 420px; }
.account { margin-left: auto; color: var(--muted); font-size: 12px; text-align: right; }
.account .bad { color: #ff6b6b; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 260px 1fr; min-height: calc(100vh - 56px); }
.categories {
  position: sticky; top: 56px; height: calc(100vh - 56px);
  display: flex; flex-direction: column; gap: 8px;
  padding: 12px; border-right: 1px solid var(--border); background: var(--panel);
}
#categories { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
#categories li button {
  width: 100%; text-align: left; background: none; border: 0;
  padding: 7px 10px; border-radius: 6px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#categories li button:hover { background: var(--panel-2); color: var(--text); }
#categories li button.active { background: var(--accent); color: #fff; }

main { padding: 16px 20px 40px; min-width: 0; }
#section-title { margin: 0 0 4px; font-size: 20px; }
.status { color: var(--muted); margin-bottom: 14px; min-height: 20px; }
.status.error { color: #ff6b6b; }

/* ---------- Cuadrícula ---------- */
.grid {
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.card { position: relative; }
.card-main {
  display: block; width: 100%;
  background: none; border: 0; padding: 0; text-align: left;
  border-radius: var(--radius); outline-offset: 3px;
}
.fav-btn {
  position: absolute; top: 6px; left: 6px; z-index: 2;
  width: 32px; height: 32px; border-radius: 50%; border: 0;
  background: rgba(0, 0, 0, .7); color: #fff; font-size: 17px; line-height: 1;
  opacity: 0; transition: opacity .15s ease, transform .15s ease;
}
.card:hover .fav-btn, .fav-btn:focus-visible, .fav-btn.on { opacity: 1; }
.fav-btn:hover { transform: scale(1.12); }
.fav-btn.on, .btn.fav-label.on { color: var(--accent); }
@media (hover: none) { .fav-btn { opacity: 1; } }
.card .poster {
  position: relative; aspect-ratio: 2 / 3; border-radius: var(--radius); overflow: hidden;
  background: var(--panel-2) center / cover no-repeat;
  display: grid; place-items: center; color: var(--muted); font-size: 34px; font-weight: 700;
  transition: transform .15s ease, box-shadow .15s ease;
}
.card .poster img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.card:hover .poster, .card-main:focus-visible .poster { transform: scale(1.04); box-shadow: 0 8px 24px rgba(0,0,0,.5); }
.card .badge {
  position: absolute; top: 6px; right: 6px; z-index: 1;
  background: rgba(0,0,0,.75); padding: 1px 6px; border-radius: 4px; font-size: 12px; color: #ffd166;
}
.card .progress { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); z-index: 1; }
.card .title { margin-top: 6px; font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.sentinel { height: 1px; }
.list-more {
  display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap;
  padding: 20px 0; color: var(--muted); text-align: center;
}
.list-more[hidden] { display: none; }
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

/* ---------- Detalles ---------- */
dialog {
  width: min(900px, 94vw); max-height: 90vh; padding: 0; border: 1px solid var(--border);
  border-radius: 12px; background: var(--panel); color: var(--text); overflow: auto;
}
dialog::backdrop { background: rgba(0,0,0,.7); }
.d-hero {
  position: relative; min-height: 220px; padding: 24px; display: flex; gap: 20px; align-items: flex-end;
  background: var(--panel-2) center / cover no-repeat;
}
.d-hero::before { content: ""; position: absolute; inset: 0; background: linear-gradient(to top, var(--panel) 5%, rgba(23,26,33,.55)); }
.d-hero > * { position: relative; }
.d-hero img { width: 140px; aspect-ratio: 2/3; object-fit: cover; border-radius: var(--radius); flex-shrink: 0; }
.d-hero h3 { margin: 0 0 6px; font-size: 24px; }
.d-meta { color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; }
.d-close { position: absolute; top: 10px; right: 10px; z-index: 2; background: rgba(0,0,0,.6); border: 0; width: 34px; height: 34px; border-radius: 50%; font-size: 18px; }
.d-body { padding: 0 24px 24px; }
.d-body p { margin: 8px 0; }
.d-body .label { color: var(--muted); }
.actions { display: flex; gap: 10px; margin: 16px 0; flex-wrap: wrap; }
.btn { border: 0; border-radius: 6px; padding: 9px 18px; font-weight: 600; background: var(--panel-2); }
.btn.primary { background: var(--accent); color: #fff; }
.seasons { display: flex; gap: 6px; flex-wrap: wrap; margin: 16px 0 10px; }
.seasons button { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 12px; }
.seasons button.active { background: var(--text); color: var(--bg); }
.episodes { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.ep {
  width: 100%; display: grid; grid-template-columns: 200px 1fr; gap: 14px; align-items: center;
  text-align: left; background: var(--panel-2); border: 1px solid transparent; border-radius: var(--radius); padding: 8px;
}
.ep:hover, .ep:focus-visible { border-color: var(--border); }
.ep-thumb {
  position: relative; aspect-ratio: 16 / 9; border-radius: 6px; overflow: hidden;
  background: #000 center / cover no-repeat;
}
/* Mientras carga el fotograma se ve el fondo de la serie oscurecido. */
.ep-thumb::before { content: ""; position: absolute; inset: 0; background: rgba(0, 0, 0, .55); }
.ep-thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .3s ease; }
.ep-thumb img.loaded { opacity: 1; }
.ep-num, .ep-dur {
  position: absolute; z-index: 1; bottom: 6px; padding: 1px 6px; border-radius: 4px;
  background: rgba(0, 0, 0, .75); font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.ep-num { left: 6px; }
.ep-dur { right: 6px; }
.ep-play {
  position: absolute; z-index: 1; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; padding-left: 3px;
  background: rgba(0, 0, 0, .65); border: 2px solid #fff; font-size: 15px; opacity: 0; transition: opacity .15s ease;
}
.ep:hover .ep-play, .ep:focus-visible .ep-play { opacity: 1; }
.ep-progress { position: absolute; z-index: 1; left: 0; bottom: 0; height: 3px; background: var(--accent); }
.ep-info { min-width: 0; }
.ep-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ep-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.ep-plot { color: var(--muted); font-size: 12px; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.watched { color: var(--accent); }

/* ---------- Descargas ---------- */
.dl-badge {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
}
.dl-badge[hidden] { display: none; }
.mode-downloads .layout { grid-template-columns: 1fr; }
.mode-downloads .categories, .mode-downloads #search, .mode-downloads #sort { display: none; }
.mode-downloads #grid { display: block; }

/* Botón dentro de la ficha (con texto) y en cada episodio (solo icono). */
.btn.dl-label.dl-done { color: #4ade80; }
.btn.dl-label.dl-error { color: #fbbf24; }
.btn.dl-label.dl-downloading, .btn.dl-label.dl-queued { color: var(--muted); font-variant-numeric: tabular-nums; }
.ep-row { display: flex; gap: 8px; align-items: stretch; }
.ep-row .ep { flex: 1; min-width: 0; }
.dl-btn {
  flex: 0 0 52px; border: 1px solid transparent; border-radius: var(--radius); background: var(--panel-2);
  font-size: 15px; font-variant-numeric: tabular-nums; color: var(--text);
}
.dl-btn:hover { border-color: var(--border); }
.dl-btn.dl-done { color: #4ade80; }
.dl-btn.dl-error { color: #fbbf24; }
.dl-btn.dl-downloading, .dl-btn.dl-queued { font-size: 12px; color: var(--muted); }
.season-dl { margin-left: auto; border-radius: 999px; padding: 4px 12px; }

.dl-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; max-width: 1000px; }
.dl-row {
  display: grid; grid-template-columns: 64px 1fr auto; gap: 14px; align-items: center;
  padding: 10px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
}
.dl-art {
  width: 64px; aspect-ratio: 2 / 3; border-radius: 6px; background: var(--panel-2) center / cover no-repeat;
  display: grid; place-items: center; color: var(--muted); font-size: 22px; font-weight: 700;
}
.dl-info { min-width: 0; }
.dl-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dl-ep { font-size: 13px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dl-sub { color: var(--muted); font-size: 12px; margin-top: 4px; font-variant-numeric: tabular-nums; }
.dl-error .dl-sub { color: #fbbf24; }
.dl-watched { color: var(--accent); font-size: 12px; margin-top: 2px; }
.dl-bar { height: 4px; margin-top: 8px; border-radius: 2px; background: var(--panel-2); overflow: hidden; }
.dl-bar > div { height: 100%; background: var(--accent); transition: width .5s ease; }
.dl-queued .dl-bar > div { width: 0; }
.dl-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.dl-actions a.btn { text-decoration: none; color: var(--text); }
.dl-empty { color: var(--text); max-width: 560px; }
.dl-empty .label { color: var(--muted); }

/* ---------- Acceso y cuenta ---------- */
.auth-mode .topbar, .auth-mode .layout { display: none; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth[hidden] { display: none; }
.auth-box {
  width: min(380px, 100%); display: grid; gap: 14px; padding: 28px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
}
.auth-box .brand { font-size: 24px; text-align: center; }
.auth-intro { margin: 0; color: var(--muted); text-align: center; }
.auth-error { margin: 0; min-height: 1.2em; color: #ff6b6b; font-size: 13px; }
.field { display: grid; gap: 4px; }
.field span { color: var(--muted); font-size: 13px; }
.field input {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius); padding: 9px 10px;
}
.field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.check { display: flex; gap: 8px; align-items: center; color: var(--text); }
.link { background: none; border: 0; color: var(--muted); text-decoration: underline; justify-self: center; }
.btn.danger { color: #ff6b6b; }
.btn:disabled { opacity: .6; cursor: wait; }

.user-menu { position: relative; }
.user-menu summary {
  list-style: none; cursor: pointer; padding: 6px 12px; border-radius: 999px; background: var(--panel-2);
  white-space: nowrap; user-select: none;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 20; min-width: 200px; display: grid;
  padding: 6px; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
}
.user-menu-list button { text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 6px; }
.user-menu-list button:hover { background: var(--panel-2); }
.user-menu-list button[hidden] { display: none; }

dialog.modal { width: min(720px, 94vw); padding: 20px 24px 24px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head h3 { margin: 0; }
.modal-head .d-close { position: static; }
.modal-form { display: grid; gap: 12px; }
.modal-form h4 { margin: 8px 0 0; }
.users { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.users th, .users td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.users th { color: var(--muted); font-weight: 600; font-size: 12px; }
.user-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.user-actions .btn { padding: 5px 10px; font-size: 12px; }
.user-create { border-top: 1px solid var(--border); padding-top: 8px; margin-top: 8px; }

/* ---------- Reproductor ---------- */
.player { position: fixed; inset: 0; z-index: 100; background: #000; display: flex; flex-direction: column; }
.player[hidden] { display: none; }
.player-bar { display: flex; align-items: center; gap: 12px; padding: 8px 12px; background: #000; }
.player-bar button { background: #222; border: 0; border-radius: 6px; padding: 6px 12px; }
.player-bar #compat.on { background: var(--accent); color: #fff; }
#player-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
video { flex: 1; width: 100%; min-height: 0; background: #000; }
.compat-seek { display: flex; align-items: center; gap: 10px; padding: 8px 14px; background: #000; font-variant-numeric: tabular-nums; }
.compat-seek[hidden] { display: none; }
.compat-seek input { flex: 1; accent-color: var(--accent); }
.player-msg { position: absolute; left: 50%; top: 64px; transform: translateX(-50%); background: rgba(0,0,0,.8); padding: 8px 14px; border-radius: 6px; pointer-events: none; opacity: 0; transition: opacity .3s; max-width: 90vw; text-align: center; }
.player-msg.show { opacity: 1; }

/* Pantallas táctiles: lo que en el escritorio aparece al pasar el ratón, aquí está siempre visible. */
@media (hover: none) {
  .ep-play { opacity: .85; }
  .card:hover .poster { transform: none; box-shadow: none; }
}

/* ================= Teléfonos (y tabletas pequeñas en vertical) ================= */
@media (max-width: 760px) {
  /* Evita que iOS haga zoom al tocar un campo (lo hace con letra menor de 16 px). */
  input, select, textarea { font-size: 16px; }
  /* Al desplazar hacia un elemento (foco, "Reintentar"…) queda por encima de la barra inferior, no debajo. */
  html { scroll-padding-bottom: calc(80px + env(safe-area-inset-bottom)); }

  /* --- Barra superior: logo y cuenta; debajo, buscador y orden. Respeta la muesca. --- */
  .topbar {
    display: grid; grid-template-columns: 1fr auto; gap: 8px 10px;
    grid-template-areas: "brand user" "search sort";
    padding: calc(8px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) 8px calc(12px + env(safe-area-inset-left));
    /* Sin desenfoque: backdrop-filter haría que la navegación "fija" (que está dentro) se pegue a esta barra
       en lugar de a la parte inferior de la pantalla. */
    backdrop-filter: none; -webkit-backdrop-filter: none; background: var(--bg);
  }
  .mode-downloads .topbar { grid-template-areas: "brand user"; }
  .brand { grid-area: brand; align-self: center; }
  .user-menu { grid-area: user; }
  .user-menu summary { padding: 8px 12px; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; }
  .user-menu-list button { padding: 12px; }
  #search { grid-area: search; width: 100%; min-width: 0; max-width: none; }
  #sort { grid-area: sort; }
  .account { display: none; }

  /* --- Navegación inferior fija, al alcance del pulgar --- */
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    display: grid; grid-template-columns: repeat(3, 1fr); gap: 0;
    padding: 4px env(safe-area-inset-right) calc(4px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
    background: rgba(15, 17, 21, .97); border-top: 1px solid var(--border);
    backdrop-filter: blur(8px);
  }
  .tabs button {
    position: relative; flex-direction: column; justify-content: center; gap: 2px;
    min-height: 54px; padding: 6px 4px; border-radius: 10px; font-size: 11px;
  }
  .tabs button.active { background: none; color: var(--accent); }
  .tab-icon { display: block; width: 24px; height: 24px; }
  .dl-badge { position: absolute; top: 4px; left: calc(50% + 6px); margin: 0; }

  /* --- Contenido: sin columna de categorías (se usa el selector) --- */
  .layout { grid-template-columns: 1fr; min-height: auto; }
  .categories { display: none; }
  .cat-select {
    display: block; width: 100%; margin-bottom: 12px; padding: 10px 12px;
    background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius);
  }
  .mode-downloads .cat-select { display: none; }
  main {
    padding: 12px calc(12px + env(safe-area-inset-right)) calc(84px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left));
  }
  #section-title { font-size: 18px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 14px 10px; }
  .card .poster { font-size: 26px; }
  .card .title { font-size: 12px; }
  .fav-btn { top: 4px; left: 4px; width: 36px; height: 36px; }

  /* --- Ficha de película o serie: pantalla completa --- */
  dialog#details, dialog.modal {
    width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh;
    margin: 0; border: 0; border-radius: 0;
  }
  dialog.modal { padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom)); }
  .d-hero { min-height: 200px; gap: 14px; padding: calc(60px + env(safe-area-inset-top)) 16px 16px; }
  .d-hero img { width: 96px; }
  .d-hero h3 { font-size: 20px; }
  .d-meta { gap: 8px; font-size: 13px; }
  #details .d-close {
    position: fixed; z-index: 5; top: calc(10px + env(safe-area-inset-top)); right: 12px;
    width: 42px; height: 42px; font-size: 20px;
  }
  .d-body { padding: 0 16px calc(24px + env(safe-area-inset-bottom)); }
  /* Botones de acción grandes: el principal a todo el ancho, el resto en dos columnas. */
  .actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .actions .btn { padding: 12px 8px; }
  .actions .btn.primary { grid-column: 1 / -1; }
  /* Temporadas como botones amplios; "Descargar temporada" en su propia línea, a todo el ancho. */
  .seasons button { padding: 8px 14px; }
  .seasons .season-dl { flex-basis: 100%; margin-left: 0; padding: 10px; }
  .ep { grid-template-columns: 118px 1fr; gap: 10px; padding: 6px; }
  .ep-play { width: 32px; height: 32px; font-size: 12px; }
  .dl-btn { flex-basis: 46px; }

  /* --- Reproductor --- */
  .player-bar {
    gap: 8px;
    padding: calc(6px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) 6px calc(8px + env(safe-area-inset-left));
  }
  .player-bar button { min-width: 44px; min-height: 44px; padding: 8px 10px; font-size: 18px; line-height: 1; }
  .player-bar .btn-text { display: none; }
  #player-title { font-size: 13px; }
  .compat-seek {
    padding: 10px calc(14px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(14px + env(safe-area-inset-left));
  }
  .compat-seek input { height: 32px; }
  .player-msg { top: calc(60px + env(safe-area-inset-top)); }

  /* --- Descargas --- */
  .dl-row { grid-template-columns: 48px 1fr; }
  .dl-art { width: 48px; }
  .dl-actions { grid-column: 1 / -1; justify-content: stretch; }
  .dl-actions .btn { flex: 1 1 auto; padding: 10px; text-align: center; }

  /* --- Usuarios: la tabla se convierte en tarjetas --- */
  .users thead { display: none; }
  .users, .users tbody, .users tr, .users td { display: block; width: 100%; }
  .users tr { padding: 10px 0; border-bottom: 1px solid var(--border); }
  .users td { padding: 2px 0; border: 0; }
  .user-actions { justify-content: flex-start; margin-top: 6px; }
  .user-actions .btn { padding: 9px 12px; font-size: 13px; }
}

/* Teléfono en horizontal (sobre todo al ver un video): barras más bajas. */
@media (max-height: 480px) and (orientation: landscape) {
  .player-bar { padding-top: 2px; padding-bottom: 2px; }
  .player-bar button { padding: 6px 10px; }
  .compat-seek { padding-top: 4px; padding-bottom: calc(4px + env(safe-area-inset-bottom)); }
}
