:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #15171c;
  --muted: #646b78;
  --line: #e3e6eb;
  --field: #f0f2f5;
  --accent: #4f6bed;
  --accent-text: #ffffff;
  --accent-soft: #e8ecfd;
  --ok: #1f9d55;
  --danger: #d03b3b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 4px 16px rgba(16, 24, 40, .06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b21;
    --text: #eef0f4;
    --muted: #9aa1ad;
    --line: #2a2e37;
    --field: #22262e;
    --accent: #6d86ff;
    --accent-text: #0b0d12;
    --accent-soft: #232a45;
    --ok: #4cc38a;
    --danger: #ff6b6b;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

.shell {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px 48px;
}

/* Cabecera */
.top {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin: 8px 0 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; }
.logo {
  width: 32px; height: 32px; border-radius: 9px; background: var(--accent);
  display: grid; place-items: center;
}
.logo svg {
  width: 20px; height: 20px; fill: none; stroke: var(--accent-text);
  stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round;
}
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 14px; min-width: 0; }
.user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Tarjetas y formularios */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
h1 { font-size: 24px; margin: 0 0 4px; }
.muted { color: var(--muted); margin: 0 0 16px; }

label, legend { display: block; font-weight: 600; font-size: 14px; margin-bottom: 8px; }
fieldset { border: 0; padding: 0; margin: 18px 0 0; min-width: 0; }

input[type="text"], input[type="url"], select {
  width: 100%;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* evita el zoom automático en iOS */
  color: var(--text);
  background: var(--field);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 12px 14px;
  outline: none;
  transition: border-color .15s, background .15s;
}
input:focus, select:focus { border-color: var(--accent); background: var(--card); }

.url-row { display: flex; gap: 8px; }

.quality { margin-top: 18px; }

/* Selector segmentado */
.seg {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px;
  background: var(--field); border-radius: 10px; padding: 4px;
}
.seg label { margin: 0; font-weight: 500; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 9px 4px; border-radius: 7px;
  font-size: 14px; cursor: pointer; color: var(--muted); user-select: none;
}
.seg input:checked + span { background: var(--card); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Botones */
button, .primary { font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.primary {
  display: inline-flex; align-items: center; justify-content: center;
  width: 100%; margin-top: 20px; padding: 13px 16px;
  border: 0; border-radius: 10px;
  background: var(--accent); color: var(--accent-text);
  font-weight: 650; font-size: 16px; text-decoration: none;
  transition: filter .15s, opacity .15s;
}
.primary:hover { filter: brightness(1.06); }
.primary:disabled { opacity: .55; cursor: default; }
.primary.small { width: auto; margin-top: 10px; padding: 9px 14px; font-size: 14px; }
.ghost {
  flex: none; border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 10px; padding: 0 14px; font-weight: 600; font-size: 14px;
}
a.link { text-decoration: none; }
.link { border: 0; background: none; color: var(--accent); padding: 4px; font-size: 14px; font-weight: 600; }

.msg { margin: 12px 0 0; font-size: 14px; }
.error { color: var(--danger); }
.hint { margin: 12px 0 0; color: var(--muted); font-size: 13px; text-align: center; }

/* Lista de descargas */
.section-title { font-size: 15px; color: var(--muted); font-weight: 600; margin: 28px 4px 10px; }
.jobs { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }

.job {
  display: grid; grid-template-columns: 96px 1fr; gap: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow);
}
.thumb {
  width: 96px; aspect-ratio: 16 / 9; border-radius: 8px; overflow: hidden; background: var(--field);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb img:not([src]) { visibility: hidden; }
.body { min-width: 0; }
.title {
  margin: 0; font-weight: 600; font-size: 15px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
  overflow-wrap: anywhere;
}
.meta { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.bar { height: 6px; border-radius: 99px; background: var(--field); margin-top: 10px; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: inherit; transition: width .4s ease; }
.status { margin: 6px 0 0; font-size: 13px; color: var(--muted); }

.job.is-done .bar span { background: var(--ok); }
.job.is-done .status { color: var(--ok); }
.job.is-error .bar { display: none; }
.job.is-error .status { color: var(--danger); }
.job.is-active .bar span { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .6; } }

@media (max-width: 380px) {
  .job { grid-template-columns: 72px 1fr; gap: 10px; }
  .thumb { width: 72px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- Administración ---------- */
.shell.wide { max-width: 760px; }

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tile {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; min-width: 0;
}
.tile-label { margin: 0; font-size: 13px; color: var(--muted); }
.tile-value { margin: 4px 0 0; font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tile-sub { margin: 4px 0 0; font-size: 12px; color: var(--muted); }
.tile .bar { margin-top: 8px; }
.bar span.warn { background: var(--danger); }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 28px 4px 10px; }
.section-head .section-title { margin: 0; }
.section-head select { width: auto; padding: 8px 12px; font-size: 14px; }

.notice {
  margin: 0 0 10px; padding: 10px 14px; border-radius: 10px;
  background: var(--accent-soft); color: var(--text); font-size: 13px;
}
.empty { color: var(--muted); font-size: 14px; text-align: center; margin: 10px 0 0; }

.card.flush { padding: 0; }
.rows { list-style: none; margin: 0; overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-top: 1px solid var(--line); }
.row:first-child { border-top: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title {
  margin: 0; font-weight: 600; font-size: 14px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row-meta { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); overflow-wrap: anywhere; }
.row-meta.ttl { color: var(--accent); }
.row-meta.bad { color: var(--danger); }
.row-actions { display: flex; gap: 6px; flex: none; }
@media (max-width: 480px) {
  .row { flex-wrap: wrap; }
  .row-actions { width: 100%; justify-content: flex-end; }
}

.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--danger); }

.ghost.small, .danger-btn.small { padding: 7px 12px; font-size: 13px; min-height: 0; }
a.ghost { display: inline-flex; align-items: center; text-decoration: none; }
.danger-btn {
  font: inherit; font-weight: 600; font-size: 14px; cursor: pointer;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
  background: transparent; color: var(--danger); border-radius: 10px; padding: 8px 14px;
}
.danger-btn:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }
button:disabled { opacity: .55; cursor: default; }

.add-user { display: flex; gap: 8px; margin-top: 10px; }
.add-user input { flex: 1; }
.add-user .primary.small { margin-top: 0; flex: none; }

.admin-link { font-size: 14px; font-weight: 600; color: var(--accent); text-decoration: none; padding: 4px; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 14px; font-weight: 600; box-shadow: 0 6px 24px rgba(0, 0, 0, .2); max-width: calc(100% - 32px);
}
